diff --git a/src/java/org/apache/commons/lang/builder/CompareToBuilder.java b/src/java/org/apache/commons/lang/builder/CompareToBuilder.java index 854b05414..6d2d31009 100644 --- a/src/java/org/apache/commons/lang/builder/CompareToBuilder.java +++ b/src/java/org/apache/commons/lang/builder/CompareToBuilder.java @@ -103,8 +103,9 @@ import org.apache.commons.lang.NumberUtils; * * @author Steve Downey * @author Stephen Colebourne + * @author Gary Gregory * @since 1.0 - * @version $Id: CompareToBuilder.java,v 1.9 2002/12/25 22:00:31 scolebourne Exp $ + * @version $Id: CompareToBuilder.java,v 1.10 2003/01/19 17:35:21 scolebourne Exp $ */ public class CompareToBuilder { @@ -139,7 +140,7 @@ public class CompareToBuilder { *
Transient members will be not be tested, as they are likely derived * fields, and not part of the value of the object.
* - *Static fields will not be tested.
+ *Static fields will not be tested. Superclass fields will be included.
* * @param lhsthis
object
* @param rhs the other object
@@ -151,7 +152,7 @@ public class CompareToBuilder {
* from being compared to this Object.
*/
public static int reflectionCompare(Object lhs, Object rhs) {
- return reflectionCompare(lhs, rhs, false);
+ return reflectionCompare(lhs, rhs, false, null);
}
/**
@@ -167,7 +168,7 @@ public class CompareToBuilder {
* transient members will be tested, otherwise they are ignored, as they
* are likely derived fields, and not part of the value of the object.
*
- * Static fields will not be tested.
+ *Static fields will not be tested. Superclass fields will be included.
* * @param lhsthis
object
* @param rhs the other object
@@ -180,6 +181,38 @@ public class CompareToBuilder {
* from being compared to this Object.
*/
public static int reflectionCompare(Object lhs, Object rhs, boolean testTransients) {
+ return reflectionCompare(lhs, rhs, testTransients, null);
+ }
+
+ /**
+ * This method uses reflection to determine if the two Objects are + * equal.
+ * + *It uses Field.setAccessible
to gain access to private
+ * fields. This means that it will throw a security exception if run under
+ * a security manger, if the permissions are not set up correctly. It is
+ * also not as efficient as testing explicitly.
If the testTransients
is set to true
,
+ * transient members will be tested, otherwise they are ignored, as they
+ * are likely derived fields, and not part of the value of the object.
Static fields will not be included. Superclass fields will be appended + * up to and including the specified superclass. A null superclass is treated + * as java.lang.Object.
+ * + * @param lhsthis
object
+ * @param rhs the other object
+ * @param testTransients whether to include transient fields
+ * @param reflectUpToClass the superclass to reflect up to (inclusive), may be null
+ * @return a negative integer, zero, or a positive integer as this
+ * Object is less than, equal to, or greater than the specified Object.
+ * @throws NullPointerException if either (but not both) parameter is
+ * null
+ * @throws ClassCastException if the specified Object's type prevents it
+ * from being compared to this Object.
+ */
+ public static int reflectionCompare(Object lhs, Object rhs, boolean testTransients, Class reflectUpToClass) {
if (lhs == rhs) {
return 0;
}
@@ -190,24 +223,41 @@ public class CompareToBuilder {
if (!c1.isInstance(rhs)) {
throw new ClassCastException();
}
- Field[] fields = c1.getDeclaredFields();
- Field.setAccessible(fields, true);
CompareToBuilder compareToBuilder = new CompareToBuilder();
- for (int i = 0; i < fields.length && compareToBuilder.comparison == 0; ++i) {
+ reflectionAppend(lhs, rhs, c1, compareToBuilder, testTransients);
+ while (c1.getSuperclass() != null && c1 != reflectUpToClass) {
+ c1 = c1.getSuperclass();
+ reflectionAppend(lhs, rhs, c1, compareToBuilder, testTransients);
+ }
+ return compareToBuilder.toComparison();
+ }
+
+ /**
+ * Appends the fields and values defined by the given object of the
+ * given Class.
+ *
+ * @param lhs the left hand object
+ * @param rhs the right hand object
+ * @param clazz the class to append details of
+ * @param builder the builder to append to
+ * @param useTransients whether to test transient fields
+ */
+ private static void reflectionAppend(Object lhs, Object rhs, Class clazz, CompareToBuilder builder, boolean useTransients) {
+ Field[] fields = clazz.getDeclaredFields();
+ Field.setAccessible(fields, true);
+ for (int i = 0; i < fields.length && builder.comparison == 0; i++) {
Field f = fields[i];
- if (testTransients || !Modifier.isTransient(f.getModifiers())) {
+ if (useTransients || !Modifier.isTransient(f.getModifiers())) {
if (!Modifier.isStatic(f.getModifiers())) {
try {
- compareToBuilder.append(f.get(lhs), f.get(rhs));
- } catch (IllegalAccessException ex) {
+ builder.append(f.get(lhs), f.get(rhs));
+ } catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
- throw new InternalError("Unexpected IllegalAccessException");
}
}
}
}
- return compareToBuilder.toComparison();
}
//-------------------------------------------------------------------------
diff --git a/src/java/org/apache/commons/lang/builder/EqualsBuilder.java b/src/java/org/apache/commons/lang/builder/EqualsBuilder.java
index d950e736a..68723c9c8 100644
--- a/src/java/org/apache/commons/lang/builder/EqualsBuilder.java
+++ b/src/java/org/apache/commons/lang/builder/EqualsBuilder.java
@@ -106,8 +106,9 @@ import java.lang.reflect.Modifier;
*
* @author Steve Downey
* @author Stephen Colebourne
+ * @author Gary Gregory
* @since 1.0
- * @version $Id: EqualsBuilder.java,v 1.9 2003/01/15 20:54:00 bayard Exp $
+ * @version $Id: EqualsBuilder.java,v 1.10 2003/01/19 17:35:21 scolebourne Exp $
*/
public class EqualsBuilder {
/**
@@ -139,14 +140,14 @@ public class EqualsBuilder {
* Transient members will be not be tested, as they are likely derived * fields, and not part of the value of the Object.
* - *Static fields will not be tested.
+ *Static fields will not be tested. Superclass fields will be included.
* - * @param lhs Left Hand Side - * @param rhs Right Hand Side + * @param lhsthis
object
+ * @param rhs the other object
* @return true
if the two Objects have tested equals.
*/
public static boolean reflectionEquals(Object lhs, Object rhs) {
- return reflectionEquals(lhs, rhs, false);
+ return reflectionEquals(lhs, rhs, false, null);
}
/**
@@ -161,43 +162,113 @@ public class EqualsBuilder {
* members will be tested, otherwise they are ignored, as they are likely
* derived fields, and not part of the value of the Object.
*
- * Static fields will not be tested.
+ *Static fields will not be tested. Superclass fields will be included.
* - * @param lhs Left Hand Side - * @param rhs Right Hand Side + * @param lhsthis
object
+ * @param rhs the other object
* @param testTransients whether to include transient fields
* @return true
if the two Objects have tested equals.
*/
- public static boolean reflectionEquals(Object lhs, Object rhs,
- boolean testTransients) {
+ public static boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients) {
+ return reflectionEquals(lhs, rhs, testTransients, null);
+ }
+
+ /**
+ * This method uses reflection to determine if the two Object are equal.
+ * + *It uses Field.setAccessible
to gain access to private
+ * fields. This means that it will throw a security exception if run under
+ * a security manger, if the permissions are not set up correctly. It is also
+ * not as efficient as testing explicitly.
If the testTransients parameter is set to true
, transient
+ * members will be tested, otherwise they are ignored, as they are likely
+ * derived fields, and not part of the value of the Object.
Static fields will not be included. Superclass fields will be appended + * up to and including the specified superclass. A null superclass is treated + * as java.lang.Object.
+ * + * @param lhsthis
object
+ * @param rhs the other object
+ * @param testTransients whether to include transient fields
+ * @param reflectUpToClass the superclass to reflect up to (inclusive), may be null
+ * @return true
if the two Objects have tested equals.
+ */
+ public static boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients, Class reflectUpToClass) {
if (lhs == rhs) {
return true;
}
if (lhs == null || rhs == null) {
return false;
}
- Class c1 = lhs.getClass();
- if (!c1.isInstance(rhs)) {
+ // Find the leaf class since there may be transients in the leaf
+ // class or in classes between the leaf and root.
+ // If we are not testing transients or a subclass has no ivars,
+ // then a subclass can test equals to a superclass.
+ Class lhsClass = lhs.getClass();
+ Class rhsClass = rhs.getClass();
+ Class testClass;
+ if (lhsClass.isInstance(rhs)) {
+ testClass = lhsClass;
+ if (!rhsClass.isInstance(lhs)) {
+ // rhsClass is a subclass of lhsClass
+ testClass = rhsClass;
+ }
+ } else if (rhsClass.isInstance(lhs)) {
+ testClass = rhsClass;
+ if (!lhsClass.isInstance(rhs)) {
+ // lhsClass is a subclass of rhsClass
+ testClass = lhsClass;
+ }
+ } else {
+ // The two classes are not related.
return false;
}
- Field[] fields = c1.getDeclaredFields();
- Field.setAccessible(fields, true);
EqualsBuilder equalsBuilder = new EqualsBuilder();
- for (int i = 0; i < fields.length && equalsBuilder.isEquals; ++i) {
+ try {
+ reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients);
+ while (testClass.getSuperclass() != null && testClass != reflectUpToClass) {
+ testClass = testClass.getSuperclass();
+ reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients);
+ }
+ } catch (IllegalArgumentException e) {
+ // In this case, we tried to test a subclass vs. a superclass and
+ // the subclass has ivars or the ivars are transient and
+ // we are testing transients.
+ // If a subclass has ivars that we are trying to test them, we get an
+ // exception and we know that the objects are not equal.
+ return false;
+ }
+ return equalsBuilder.isEquals();
+ }
+
+ /**
+ * Appends the fields and values defined by the given object of the
+ * given Class.
+ *
+ * @param lhs the left hand object
+ * @param rhs the right hand object
+ * @param clazz the class to append details of
+ * @param builder the builder to append to
+ * @param useTransients whether to test transient fields
+ */
+ private static void reflectionAppend(Object lhs, Object rhs, Class clazz, EqualsBuilder builder, boolean useTransients) {
+ Field[] fields = clazz.getDeclaredFields();
+ Field.setAccessible(fields, true);
+ for (int i = 0; i < fields.length && builder.isEquals; i++) {
Field f = fields[i];
- if (testTransients || !Modifier.isTransient(f.getModifiers())) {
+ if (useTransients || !Modifier.isTransient(f.getModifiers())) {
if (!Modifier.isStatic(f.getModifiers())) {
try {
- equalsBuilder.append(f.get(lhs), f.get(rhs));
+ builder.append(f.get(lhs), f.get(rhs));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
- throw new InternalError("Unexpected IllegalAccessException");
}
}
}
}
- return equalsBuilder.isEquals();
}
//-------------------------------------------------------------------------
diff --git a/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java b/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java
index 9130ea902..a847b19b4 100644
--- a/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java
+++ b/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java
@@ -106,8 +106,9 @@ import java.lang.reflect.Modifier;
*
*
* @author Stephen Colebourne
+ * @author Gary Gregory
* @since 1.0
- * @version $Id: HashCodeBuilder.java,v 1.8 2003/01/15 20:51:57 bayard Exp $
+ * @version $Id: HashCodeBuilder.java,v 1.9 2003/01/19 17:35:21 scolebourne Exp $
*/
public class HashCodeBuilder {
@@ -179,14 +180,14 @@ public class HashCodeBuilder {
* Transient members will be not be used, as they are likely derived * fields, and not part of the value of the Object.
* - *Static fields will not be tested.
+ *Static fields will not be tested. Superclass fields will be included.
* * @param object the Object to create ahashCode
for
* @return int hash code
* @throws IllegalArgumentException if the object is null
*/
public static int reflectionHashCode(Object object) {
- return reflectionHashCode(object, false);
+ return reflectionHashCode(17, 37, object, false, null);
}
/**
@@ -204,7 +205,7 @@ public class HashCodeBuilder {
* members will be tested, otherwise they are ignored, as they are likely
* derived fields, and not part of the value of the Object.
*
- * Static fields will not be tested.
+ *Static fields will not be tested. Superclass fields will be included.
* * @param object the Object to create ahashCode
for
* @param testTransients whether to include transient fields
@@ -212,7 +213,7 @@ public class HashCodeBuilder {
* @throws IllegalArgumentException if the object is null
*/
public static int reflectionHashCode(Object object, boolean testTransients) {
- return reflectionHashCode(17, 37, object, testTransients);
+ return reflectionHashCode(17, 37, object, testTransients, null);
}
/**
@@ -226,7 +227,7 @@ public class HashCodeBuilder {
* Transient members will be not be used, as they are likely derived * fields, and not part of the value of the Object.
* - *Static fields will not be tested.
+ *Static fields will not be tested. Superclass fields will be included.
* *Two randomly chosen, non-zero, odd numbers must be passed in. Ideally * these should be different for each class, however this is not vital. @@ -240,9 +241,8 @@ public class HashCodeBuilder { * @throws IllegalArgumentException if the number is zero or even */ public static int reflectionHashCode( - int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, - Object object) { - return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, false); + int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object object) { + return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, false, null); } /** @@ -257,7 +257,7 @@ public class HashCodeBuilder { * members will be tested, otherwise they are ignored, as they are likely * derived fields, and not part of the value of the Object.
* - *Static fields will not be tested.
+ *Static fields will not be tested. Superclass fields will be included.
* *Two randomly chosen, non-zero, odd numbers must be passed in. Ideally * these should be different for each class, however this is not vital. @@ -274,29 +274,81 @@ public class HashCodeBuilder { public static int reflectionHashCode( int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object object, boolean testTransients) { + return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, testTransients, null); + } + + /** + *
This method uses reflection to build a valid hash code.
+ * + *It uses Field.setAccessible
to gain access to private
+ * fields. This means that it will throw a security exception if run under
+ * a security manger, if the permissions are not set up correctly. It is also
+ * not as efficient as testing explicitly.
If the TestTransients parameter is set to true
, transient
+ * members will be tested, otherwise they are ignored, as they are likely
+ * derived fields, and not part of the value of the Object.
Static fields will not be included. Superclass fields will be included + * up to and including the specified superclass. A null superclass is treated + * as java.lang.Object.
+ * + *Two randomly chosen, non-zero, odd numbers must be passed in. Ideally + * these should be different for each class, however this is not vital. + * Prime numbers are preferred, especially for the multiplier.
+ * + * @param initialNonZeroOddNumber + * @param multiplierNonZeroOddNumber + * @param object the Object to create ahashCode
for
+ * @param testTransients whether to include transient fields
+ * @param reflectUpToClass the superclass to reflect up to (inclusive), may be null
+ * @return int hash code
+ * @throws IllegalArgumentException if the Object is null
+ * @throws IllegalArgumentException if the number is zero or even
+ */
+ public static int reflectionHashCode(
+ int initialNonZeroOddNumber, int multiplierNonZeroOddNumber,
+ Object object, boolean testTransients, Class reflectUpToClass) {
if (object == null) {
throw new IllegalArgumentException("The object to build a hash code for must not be null");
}
- HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(initialNonZeroOddNumber, multiplierNonZeroOddNumber);
- Field[] fields = object.getClass().getDeclaredFields();
+ HashCodeBuilder builder = new HashCodeBuilder(initialNonZeroOddNumber, multiplierNonZeroOddNumber);
+ Class clazz = object.getClass();
+ reflectionAppend(object, clazz, builder, testTransients);
+ while (clazz.getSuperclass() != null && clazz != reflectUpToClass) {
+ clazz = clazz.getSuperclass();
+ reflectionAppend(object, clazz, builder, testTransients);
+ }
+ return builder.toHashCode();
+ }
+
+ /**
+ * Appends the fields and values defined by the given object of the
+ * given Class.
+ *
+ * @param object the object to append details of
+ * @param clazz the class to append details of
+ * @param builder the builder to append to
+ * @param useTransients whether to use transient fields
+ */
+ private static void reflectionAppend(Object object, Class clazz, HashCodeBuilder builder, boolean useTransients) {
+ Field[] fields = clazz.getDeclaredFields();
Field.setAccessible(fields, true);
- for (int i = 0; i < fields.length; ++i) {
+ for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
- if (testTransients || !Modifier.isTransient(f.getModifiers())) {
+ if (useTransients || !Modifier.isTransient(f.getModifiers())) {
if (!Modifier.isStatic(f.getModifiers())) {
try {
- hashCodeBuilder.append(f.get(object));
+ builder.append(f.get(object));
} catch (IllegalAccessException e) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
- throw new InternalError("Unexpected IllegalAccessException");
}
}
}
}
- return hashCodeBuilder.toHashCode();
- }
+ }
//-------------------------------------------------------------------------
diff --git a/src/java/org/apache/commons/lang/builder/ToStringBuilder.java b/src/java/org/apache/commons/lang/builder/ToStringBuilder.java
index 045c001eb..f87d8f3bc 100644
--- a/src/java/org/apache/commons/lang/builder/ToStringBuilder.java
+++ b/src/java/org/apache/commons/lang/builder/ToStringBuilder.java
@@ -113,7 +113,7 @@ import java.lang.reflect.Modifier;
* @author Stephen Colebourne
* @author Gary Gregory
* @since 1.0
- * @version $Id: ToStringBuilder.java,v 1.12 2002/12/31 20:21:34 scolebourne Exp $
+ * @version $Id: ToStringBuilder.java,v 1.13 2003/01/19 17:35:21 scolebourne Exp $
*/
public class ToStringBuilder {
@@ -331,8 +331,7 @@ public class ToStringBuilder {
* up to and including the specified superclass. A null superclass is treated
* as java.lang.Object.
*
- *
- * If the style is null
, the default
+ *
If the style is null
, the default
* ToStringStyle
is used.
CompareToBuilder.reflectionCompare
+ *
+ * @param lhs The "left-hand-side" of the comparison.
+ * @param rhs The "right-hand-side" of the comparison.
+ * @param testTransients Whether to include transients in the comparison
+ * @return int The signum
+ */
+ private int reflectionCompareSignum(Object lhs, Object rhs, boolean testTransients) {
+ return BigInteger.valueOf(CompareToBuilder.reflectionCompare(lhs, rhs, testTransients)).signum();
+ }
+
public void testAppendSuper() {
TestObject o1 = new TestObject(4);
TestObject o2 = new TestObject(5);
diff --git a/src/test/org/apache/commons/lang/builder/EqualsBuilderTest.java b/src/test/org/apache/commons/lang/builder/EqualsBuilderTest.java
index 2638febde..3cc3b49b4 100644
--- a/src/test/org/apache/commons/lang/builder/EqualsBuilderTest.java
+++ b/src/test/org/apache/commons/lang/builder/EqualsBuilderTest.java
@@ -62,7 +62,7 @@ import junit.textui.TestRunner;
*
* @author Steve Downey
* @author Stephen Colebourne
- * @version $Id: EqualsBuilderTest.java,v 1.2 2002/12/08 21:10:11 scolebourne Exp $
+ * @version $Id: EqualsBuilderTest.java,v 1.3 2003/01/19 17:35:20 scolebourne Exp $
*/
public class EqualsBuilderTest extends TestCase {
@@ -92,6 +92,8 @@ public class EqualsBuilderTest extends TestCase {
static class TestObject {
private int a;
+ public TestObject() {
+ }
public TestObject(int a) {
this.a = a;
}
@@ -115,6 +117,78 @@ public class EqualsBuilderTest extends TestCase {
}
}
+ static class TestSubObject extends TestObject {
+ private int b;
+ public TestSubObject() {
+ super(0);
+ }
+ public TestSubObject(int a, int b) {
+ super(a);
+ this.b = b;
+ }
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (!(o instanceof TestSubObject)) {
+ return false;
+ }
+ TestSubObject rhs = (TestSubObject) o;
+ return super.equals(o) && (b == rhs.b);
+ }
+
+ public void setB(int b) {
+ this.b = b;
+ }
+
+ public int getB() {
+ return b;
+ }
+ }
+
+ static class TestEmptySubObject extends TestObject {
+ public TestEmptySubObject(int a) {
+ super(a);
+ }
+ }
+
+ static class TestTSubObject extends TestObject {
+ private transient int t;
+ public TestTSubObject(int a, int t) {
+ super(a);
+ this.t = t;
+ }
+ }
+
+ static class TestTTSubObject extends TestTSubObject {
+ private transient int tt;
+ public TestTTSubObject(int a, int t, int tt) {
+ super(a, t);
+ this.tt = tt;
+ }
+ }
+
+ static class TestTTLeafObject extends TestTTSubObject {
+ private int leafValue;
+ public TestTTLeafObject(int a, int t, int tt, int leafValue) {
+ super(a, t, tt);
+ this.leafValue = leafValue;
+ }
+ }
+
+ static class TestTSubObject2 extends TestObject {
+ private transient int t;
+ public TestTSubObject2(int a, int t) {
+ super(a);
+ }
+ public int getT() {
+ return t;
+ }
+ public void setT(int t) {
+ this.t = t;
+ }
+ }
+
public void testReflectionEquals() {
TestObject o1 = new TestObject(4);
TestObject o2 = new TestObject(5);
@@ -124,11 +198,136 @@ public class EqualsBuilderTest extends TestCase {
assertTrue(EqualsBuilder.reflectionEquals(o1, o2));
assertTrue(!EqualsBuilder.reflectionEquals(o1, this));
-
+
assertTrue(!EqualsBuilder.reflectionEquals(o1, null));
assertTrue(!EqualsBuilder.reflectionEquals(null, o2));
assertTrue(EqualsBuilder.reflectionEquals((Object) null, (Object) null));
}
+
+ public void testReflectionHierarchyEquals() {
+ testReflectionHierarchyEquals(false);
+ testReflectionHierarchyEquals(true);
+ // Transients
+ assertTrue(EqualsBuilder.reflectionEquals(new TestTTLeafObject(1, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 4), true));
+ assertTrue(EqualsBuilder.reflectionEquals(new TestTTLeafObject(1, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 4), false));
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestTTLeafObject(1, 0, 0, 4), new TestTTLeafObject(1, 2, 3, 4), true));
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestTTLeafObject(1, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 0), true));
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestTTLeafObject(0, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 4), true));
+ }
+
+ public void testReflectionHierarchyEquals(boolean testTransients) {
+ TestObject to1 = new TestObject(4);
+ TestObject to1Bis = new TestObject(4);
+ TestObject to1Ter = new TestObject(4);
+ TestObject to2 = new TestObject(5);
+ TestEmptySubObject teso = new TestEmptySubObject(4);
+ TestTSubObject ttso = new TestTSubObject(4, 1);
+ TestTTSubObject tttso = new TestTTSubObject(4, 1, 2);
+ TestTTLeafObject ttlo = new TestTTLeafObject(4, 1, 2, 3);
+ TestSubObject tso1 = new TestSubObject(1, 4);
+ TestSubObject tso1bis = new TestSubObject(1, 4);
+ TestSubObject tso1ter = new TestSubObject(1, 4);
+ TestSubObject tso2 = new TestSubObject(2, 5);
+
+ testReflectionEqualsEquivalenceRelationship(to1, to1Bis, to1Ter, to2, new TestObject(), testTransients);
+ testReflectionEqualsEquivalenceRelationship(tso1, tso1bis, tso1ter, tso2, new TestSubObject(), testTransients);
+
+ // More sanity checks:
+
+ // same values
+ assertTrue(EqualsBuilder.reflectionEquals(ttlo, ttlo, testTransients));
+ assertTrue(EqualsBuilder.reflectionEquals(new TestSubObject(1, 10), new TestSubObject(1, 10), testTransients));
+ // same super values, diff sub values
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1, 10), new TestSubObject(1, 11), testTransients));
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1, 11), new TestSubObject(1, 10), testTransients));
+ // diff super values, same sub values
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(0, 10), new TestSubObject(1, 10), testTransients));
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1, 10), new TestSubObject(0, 10), testTransients));
+
+ // mix super and sub types: equals
+ assertTrue(EqualsBuilder.reflectionEquals(to1, teso, testTransients));
+ assertTrue(EqualsBuilder.reflectionEquals(teso, to1, testTransients));
+
+ assertTrue(EqualsBuilder.reflectionEquals(to1, ttso, false)); // Force testTransients = false for this assert
+ assertTrue(EqualsBuilder.reflectionEquals(ttso, to1, false)); // Force testTransients = false for this assert
+
+ assertTrue(EqualsBuilder.reflectionEquals(to1, tttso, false)); // Force testTransients = false for this assert
+ assertTrue(EqualsBuilder.reflectionEquals(tttso, to1, false)); // Force testTransients = false for this assert
+
+ assertTrue(EqualsBuilder.reflectionEquals(ttso, tttso, false)); // Force testTransients = false for this assert
+ assertTrue(EqualsBuilder.reflectionEquals(tttso, ttso, false)); // Force testTransients = false for this assert
+
+ // mix super and sub types: NOT equals
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(0), new TestEmptySubObject(1), testTransients));
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestEmptySubObject(1), new TestObject(0), testTransients));
+
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(0), new TestTSubObject(1, 1), testTransients));
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestTSubObject(1, 1), new TestObject(0), testTransients));
+
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(1), new TestSubObject(0, 10), testTransients));
+ assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(0, 10), new TestObject(1), testTransients));
+
+ assertTrue(!EqualsBuilder.reflectionEquals(to1, ttlo));
+ assertTrue(!EqualsBuilder.reflectionEquals(tso1, this));
+ }
+
+ /**
+ * Equivalence relationship tests inspired by "Effective Java":
+ *