Add superclass behaviour to builder classes

from Gary Gregory


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137228 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-01-19 17:35:21 +00:00
parent 9247caa92e
commit 5c40090fec
7 changed files with 609 additions and 62 deletions

View File

@ -103,8 +103,9 @@ import org.apache.commons.lang.NumberUtils;
*
* @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
* @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 {
* <p>Transient members will be not be tested, as they are likely derived
* fields, and not part of the value of the object.</p>
*
* <p>Static fields will not be tested.</p>
* <p>Static fields will not be tested. Superclass fields will be included.</p>
*
* @param lhs <code>this</code> 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.</p>
*
* <p>Static fields will not be tested.</p>
* <p>Static fields will not be tested. Superclass fields will be included.</p>
*
* @param lhs <code>this</code> 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);
}
/**
* <p>This method uses reflection to determine if the two Objects are
* equal.</p>
*
* <p>It uses <code>Field.setAccessible</code> 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.</p>
*
* <p>If the <code>testTransients</code> is set to <code>true</code>,
* transient members will be tested, otherwise they are ignored, as they
* are likely derived fields, and not part of the value of the object.</p>
*
* <p>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.</p>
*
* @param lhs <code>this</code> 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
* <code>null</code>
* @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();
}
//-------------------------------------------------------------------------

View File

@ -106,8 +106,9 @@ import java.lang.reflect.Modifier;
*
* @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
* @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 {
* <p>Transient members will be not be tested, as they are likely derived
* fields, and not part of the value of the Object.</p>
*
* <p>Static fields will not be tested.</p>
* <p>Static fields will not be tested. Superclass fields will be included.</p>
*
* @param lhs Left Hand Side
* @param rhs Right Hand Side
* @param lhs <code>this</code> object
* @param rhs the other object
* @return <code>true</code> 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.</p>
*
* <p>Static fields will not be tested.</p>
* <p>Static fields will not be tested. Superclass fields will be included.</p>
*
* @param lhs Left Hand Side
* @param rhs Right Hand Side
* @param lhs <code>this</code> object
* @param rhs the other object
* @param testTransients whether to include transient fields
* @return <code>true</code> 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);
}
/**
* <p>This method uses reflection to determine if the two Object are equal.</p>
*
* <p>It uses <code>Field.setAccessible</code> 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.</p>
*
* <p>If the testTransients parameter is set to <code>true</code>, transient
* members will be tested, otherwise they are ignored, as they are likely
* derived fields, and not part of the value of the Object.</p>
*
* <p>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.</p>
*
* @param lhs <code>this</code> 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 <code>true</code> 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();
}
//-------------------------------------------------------------------------

View File

@ -106,8 +106,9 @@ import java.lang.reflect.Modifier;
* </pre>
*
* @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 {
* <p>Transient members will be not be used, as they are likely derived
* fields, and not part of the value of the Object.</p>
*
* <p>Static fields will not be tested.</p>
* <p>Static fields will not be tested. Superclass fields will be included.</p>
*
* @param object the Object to create a <code>hashCode</code> for
* @return int hash code
* @throws IllegalArgumentException if the object is <code>null</code>
*/
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.</p>
*
* <p>Static fields will not be tested.</p>
* <p>Static fields will not be tested. Superclass fields will be included.</p>
*
* @param object the Object to create a <code>hashCode</code> for
* @param testTransients whether to include transient fields
@ -212,7 +213,7 @@ public class HashCodeBuilder {
* @throws IllegalArgumentException if the object is <code>null</code>
*/
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 {
* <p>Transient members will be not be used, as they are likely derived
* fields, and not part of the value of the Object.</p>
*
* <p>Static fields will not be tested.</p>
* <p>Static fields will not be tested. Superclass fields will be included.</p>
*
* <p>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.</p>
*
* <p>Static fields will not be tested.</p>
* <p>Static fields will not be tested. Superclass fields will be included.</p>
*
* <p>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);
}
/**
* <p>This method uses reflection to build a valid hash code.</p>
*
* <p>It uses <code>Field.setAccessible</code> 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.</p>
*
* <p>If the TestTransients parameter is set to <code>true</code>, transient
* members will be tested, otherwise they are ignored, as they are likely
* derived fields, and not part of the value of the Object.</p>
*
* <p>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.</p>
*
* <p>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.</p>
*
* @param initialNonZeroOddNumber
* @param multiplierNonZeroOddNumber
* @param object the Object to create a <code>hashCode</code> 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 <code>null</code>
* @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();
}
}
//-------------------------------------------------------------------------

View File

@ -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.</p>
*
* <p>
* If the style is <code>null</code>, the default
* <p>If the style is <code>null</code>, the default
* <code>ToStringStyle</code> is used.</p>
*
* @param object the Object to be output
@ -367,14 +366,14 @@ public class ToStringBuilder {
* @param object the object to append details of
* @param clazz the class to append details of
* @param builder the builder to append to
* @param outputTransients whether to output transient fields
* @param useTransients whether to output transient fields
*/
private static void reflectionAppend(Object object, Class clazz, ToStringBuilder builder, boolean outputTransients) {
private static void reflectionAppend(Object object, Class clazz, ToStringBuilder 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 (outputTransients || !Modifier.isTransient(f.getModifiers())) {
if (useTransients || !Modifier.isTransient(f.getModifiers())) {
if (!Modifier.isStatic(f.getModifiers())) {
try {
builder.append(f.getName(), f.get(object));

View File

@ -53,7 +53,7 @@
*/
package org.apache.commons.lang.builder;
import org.apache.commons.lang.builder.CompareToBuilder;
import java.math.BigInteger;
import junit.framework.Test;
import junit.framework.TestCase;
@ -64,7 +64,7 @@ import junit.textui.TestRunner;
*
* @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: CompareToBuilderTest.java,v 1.3 2002/12/08 21:43:34 scolebourne Exp $
* @version $Id: CompareToBuilderTest.java,v 1.4 2003/01/19 17:35:20 scolebourne Exp $
*/
public class CompareToBuilderTest extends TestCase {
@ -91,7 +91,7 @@ public class CompareToBuilderTest extends TestCase {
//-----------------------------------------------------------------------
static class TestObject implements Comparable{
static class TestObject implements Comparable {
private int a;
public TestObject(int a) {
this.a = a;
@ -120,6 +120,35 @@ public class CompareToBuilderTest 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);
}
}
static class TestTransientSubObject extends TestObject {
private transient int t;
public TestTransientSubObject(int a, int t) {
super(a);
this.t = t;
}
}
public void testReflectionCompare() {
TestObject o1 = new TestObject(4);
TestObject o2 = new TestObject(4);
@ -149,6 +178,105 @@ public class CompareToBuilderTest extends TestCase {
} catch (ClassCastException ex) {}
}
public void testReflectionHierarchyCompare() {
testReflectionHierarchyCompare(false);
}
public void testReflectionHierarchyCompareTransients() {
testReflectionHierarchyCompare(true);
TestTransientSubObject x;
TestTransientSubObject y;
TestTransientSubObject z;
x = new TestTransientSubObject(1, 1);
y = new TestTransientSubObject(2, 2);
z = new TestTransientSubObject(3, 3);
assertXYZCompareOrder(x, y, z, true);
x = new TestTransientSubObject(1, 1);
y = new TestTransientSubObject(1, 2);
z = new TestTransientSubObject(1, 3);
assertXYZCompareOrder(x, y, z, true);
}
private void assertXYZCompareOrder(Object x, Object y, Object z, boolean testTransients) {
assertTrue(0 == CompareToBuilder.reflectionCompare(x, x, testTransients));
assertTrue(0 == CompareToBuilder.reflectionCompare(y, y, testTransients));
assertTrue(0 == CompareToBuilder.reflectionCompare(z, z, testTransients));
assertTrue(0 > CompareToBuilder.reflectionCompare(x, y, testTransients));
assertTrue(0 > CompareToBuilder.reflectionCompare(x, z, testTransients));
assertTrue(0 > CompareToBuilder.reflectionCompare(y, z, testTransients));
assertTrue(0 < CompareToBuilder.reflectionCompare(y, x, testTransients));
assertTrue(0 < CompareToBuilder.reflectionCompare(z, x, testTransients));
assertTrue(0 < CompareToBuilder.reflectionCompare(z, y, testTransients));
}
public void testReflectionHierarchyCompare(boolean testTransients) {
TestObject to1 = new TestObject(1);
TestObject to2 = new TestObject(2);
TestObject to3 = new TestObject(3);
TestSubObject tso1 = new TestSubObject(1, 1);
TestSubObject tso2 = new TestSubObject(2, 2);
TestSubObject tso3 = new TestSubObject(3, 3);
assertReflectionCompareContract(to1, to1, to1, false);
assertReflectionCompareContract(to1, to2, to3, false);
assertReflectionCompareContract(tso1, tso1, tso1, false);
assertReflectionCompareContract(tso1, tso2, tso3, false);
assertReflectionCompareContract("1", "2", "3", false);
assertTrue(0 != CompareToBuilder.reflectionCompare(tso1, new TestSubObject(1, 0), testTransients));
assertTrue(0 != CompareToBuilder.reflectionCompare(tso1, new TestSubObject(0, 1), testTransients));
// root class
assertXYZCompareOrder(to1, to2, to3, true);
// subclass
assertXYZCompareOrder(tso1, tso2, tso3, true);
}
/**
* See "Effective Java" under "Consider Implementing Comparable".
*
* @param x an object to compare
* @param y an object to compare
* @param z an object to compare
* @param testTransients Whether to include transients in the comparison
*/
public void assertReflectionCompareContract(Object x, Object y, Object z, boolean testTransients) {
// signum
assertTrue(reflectionCompareSignum(x, y, testTransients) == -reflectionCompareSignum(y, x, testTransients));
// transitive
if (CompareToBuilder.reflectionCompare(x, y, testTransients) > 0 && CompareToBuilder.reflectionCompare(y, z, testTransients) > 0){
assertTrue(CompareToBuilder.reflectionCompare(x, z, testTransients) > 0);
}
// un-named
if (CompareToBuilder.reflectionCompare(x, y, testTransients) == 0) {
assertTrue(reflectionCompareSignum(x, z, testTransients) == -reflectionCompareSignum(y, z, testTransients));
}
// strongly recommended but not strictly required
assertTrue((CompareToBuilder.reflectionCompare(x, y, testTransients) ==0 ) == EqualsBuilder.reflectionEquals(x, y, testTransients));
}
/**
* Returns the signum of the result of comparing x and y with
* <code>CompareToBuilder.reflectionCompare</code>
*
* @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);

View File

@ -62,7 +62,7 @@ import junit.textui.TestRunner;
*
* @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @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":
* <ul>
* <li>reflection</li>
* <li>symetry</li>
* <li>transitive</li>
* <li>consistency</li>
* <li>non-null reference</li>
* </ul>
* @param to a TestObject
* @param toBis a TestObject, equal to to and toTer
* @param toTer Left hand side, equal to to and toBis
* @param to2 a different TestObject
* @param oToChange a TestObject that will be changed
*/
public void testReflectionEqualsEquivalenceRelationship(
TestObject to,
TestObject toBis,
TestObject toTer,
TestObject to2,
TestObject oToChange,
boolean testTransients) {
// reflection test
assertTrue(EqualsBuilder.reflectionEquals(to, to, testTransients));
assertTrue(EqualsBuilder.reflectionEquals(to2, to2, testTransients));
// symetry test
assertTrue(EqualsBuilder.reflectionEquals(to, toBis, testTransients) && EqualsBuilder.reflectionEquals(toBis, to, testTransients));
// transitive test
assertTrue(
EqualsBuilder.reflectionEquals(to, toBis, testTransients)
&& EqualsBuilder.reflectionEquals(toBis, toTer, testTransients)
&& EqualsBuilder.reflectionEquals(to, toTer, testTransients));
// consistency test
oToChange.setA(to.getA());
if (oToChange instanceof TestSubObject) {
((TestSubObject) oToChange).setB(((TestSubObject) to).getB());
}
assertTrue(EqualsBuilder.reflectionEquals(oToChange, to, testTransients));
assertTrue(EqualsBuilder.reflectionEquals(oToChange, to, testTransients));
oToChange.setA(to.getA() + 1);
if (oToChange instanceof TestSubObject) {
((TestSubObject) oToChange).setB(((TestSubObject) to).getB() + 1);
}
assertTrue(!EqualsBuilder.reflectionEquals(oToChange, to, testTransients));
assertTrue(!EqualsBuilder.reflectionEquals(oToChange, to, testTransients));
// non-null reference test
assertTrue(!EqualsBuilder.reflectionEquals(to, null, testTransients));
assertTrue(!EqualsBuilder.reflectionEquals(to2, null, testTransients));
assertTrue(!EqualsBuilder.reflectionEquals(null, to, testTransients));
assertTrue(!EqualsBuilder.reflectionEquals(null, to2, testTransients));
assertTrue(EqualsBuilder.reflectionEquals((Object) null, (Object) null, testTransients));
}
public void testSuper() {
TestObject o1 = new TestObject(4);

View File

@ -61,7 +61,7 @@ import junit.textui.TestRunner;
* Unit tests {@link org.apache.commons.lang.HashCodeBuilder}.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: HashCodeBuilderTest.java,v 1.2 2002/12/08 21:22:42 scolebourne Exp $
* @version $Id: HashCodeBuilderTest.java,v 1.3 2003/01/19 17:35:20 scolebourne Exp $
*/
public class HashCodeBuilderTest extends TestCase {
@ -134,11 +134,59 @@ public class HashCodeBuilderTest extends TestCase {
}
}
static class TestSubObject extends TestObject {
private int b;
transient private int t;
public TestSubObject() {
super(0);
}
public TestSubObject(int a, int b, int t) {
super(a);
this.b = b;
this.t = t;
}
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 testReflectionHashCode() {
assertEquals(17 * 37, HashCodeBuilder.reflectionHashCode(new TestObject(0)));
assertEquals(17 * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestObject(123456)));
}
public void testReflectionHierarchyHashCode() {
assertEquals(17 * 37 * 37, HashCodeBuilder.reflectionHashCode(new TestSubObject(0, 0, 0)));
assertEquals(17 * 37 * 37 * 37, HashCodeBuilder.reflectionHashCode(new TestSubObject(0, 0, 0), true));
assertEquals((17 * 37 + 7890) * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestSubObject(123456, 7890, 0)));
assertEquals(((17 * 37 + 7890) * 37 + 0) * 37 + 123456, HashCodeBuilder.reflectionHashCode(new TestSubObject(123456, 7890, 0), true));
}
public void testReflectionHierarchyHashCodeEx1() {
try {
HashCodeBuilder.reflectionHashCode(0, 0, new TestSubObject(0, 0, 0), true);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testReflectionHierarchyHashCodeEx2() {
try {
HashCodeBuilder.reflectionHashCode(2, 2, new TestSubObject(0, 0, 0), true);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testReflectionHashCodeEx1() {
try {
HashCodeBuilder.reflectionHashCode(0, 0, new TestObject(0), true);