Skip to content

Commit

Permalink
Fix NPE when evaluating objects that can lead to cycle/infinite recur…
Browse files Browse the repository at this point in the history
…sion (#1868)
  • Loading branch information
joel-costigliola committed May 8, 2020
1 parent a64f3fb commit 11189e9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ private static boolean isPotentialCyclingValue(Object object) {
if (object == null) return false;
// java.lang are base types that can't cycle to themselves of other types
// we could check more type, but that's a good start
return !object.getClass().getCanonicalName().startsWith("java.lang");
String canonicalName = object.getClass().getCanonicalName();
// canonicalName is null for anonymous and local classes, return true as they can cycle back to other objects.
if (canonicalName == null) return true;
return !canonicalName.startsWith("java.lang");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2020 the original author or authors.
*/
package org.assertj.core.api.recursive.comparison;

import static org.assertj.core.api.recursive.comparison.ColorWithCode.GREEN;

enum ColorWithCode {
RED {
@Override
String code() {
return "#FF0000";
}
},
GREEN {
@Override
String code() {
return "#00FF00";
}
},
BLUE {
@Override
String code() {
return "#0000FF";
}
};

abstract String code();

}

class Theme {
ColorWithCode color = GREEN;

public Theme(ColorWithCode color) {
this.color = color;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package org.assertj.core.api.recursive.comparison;

import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.recursive.comparison.Color.GREEN;
import static org.assertj.core.api.recursive.comparison.ColorWithCode.RED;
import static org.assertj.core.util.Lists.list;

import java.util.List;
Expand Down Expand Up @@ -49,6 +51,13 @@ static Stream<Arguments> values() {
person2.otherFriends.add(person2);
person2.otherFriends.add(person1);

class LocalClass {
@Override
public String toString() {
return "LocalClass";
}
}

return Stream.of(Arguments.of(null, person2, false),
Arguments.of(person1, null, false),
Arguments.of(person1, "abc", false),
Expand All @@ -64,6 +73,20 @@ static Stream<Arguments> values() {
Arguments.of(person1, person2, true),
Arguments.of(list(person1), list(person1), true),
Arguments.of(list(person1), list(person2), true),
Arguments.of(new LocalClass(), new LocalClass(), true),
Arguments.of(new Light(GREEN), new Light(GREEN), true),
Arguments.of(new Theme(RED), new Theme(RED), true), // for #1866
Arguments.of(new DualValue_hasPotentialCyclingValues_Test().new Inner(),
new DualValue_hasPotentialCyclingValues_Test().new Inner(), true),
Arguments.of(list(person1, person2), list(person2, person1), true));
}

class Inner {
@Override
public String toString() {
return "Inner Class";
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.recursive.comparison.Color.BLUE;
import static org.assertj.core.api.recursive.comparison.Color.GREEN;
import static org.assertj.core.api.recursive.comparison.ColorWithCode.RED;
import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
import static org.assertj.core.test.AlwaysEqualComparator.ALWAY_EQUALS_STRING;
Expand Down Expand Up @@ -171,6 +172,7 @@ private static Stream<Arguments> recursivelyEqualObjects() {
return Stream.of(arguments(person1, person2, "same data, same type"),
arguments(person2, person1, "same data, same type reversed"),
arguments(person3, person4, "same data, different type"),
arguments(new Theme(RED), new Theme(RED), "same data with enum overriding methods - covers #1866"),
arguments(person4, person3, "same data, different type"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public class FriendlyPerson extends Person {
public Set<FriendlyPerson> otherFriends = new HashSet<>();

public FriendlyPerson() {
super();
super();
}

public FriendlyPerson(String name) {
super(name);
super(name);
}

public static FriendlyPerson friend(String name) {
Expand Down

0 comments on commit 11189e9

Please sign in to comment.