git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@138012 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2005-01-16 19:34:34 +00:00
parent cd9922ef8b
commit e3f46b722e
1 changed files with 73 additions and 2 deletions

View File

@ -15,6 +15,8 @@
*/ */
package org.apache.commons.lang.builder; package org.apache.commons.lang.builder;
import java.util.Arrays;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
@ -27,7 +29,7 @@ import junit.textui.TestRunner;
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Maarten Coene * @author Maarten Coene
* @version $Id: EqualsBuilderTest.java,v 1.11 2004/08/26 05:46:45 ggregory Exp $ * @version $Id: EqualsBuilderTest.java,v 1.12 2005/01/16 19:34:34 ggregory Exp $
*/ */
public class EqualsBuilderTest extends TestCase { public class EqualsBuilderTest extends TestCase {
@ -110,7 +112,7 @@ public class EqualsBuilderTest extends TestCase {
return b; return b;
} }
} }
static class TestEmptySubObject extends TestObject { static class TestEmptySubObject extends TestObject {
public TestEmptySubObject(int a) { public TestEmptySubObject(int a) {
super(a); super(a);
@ -873,5 +875,74 @@ public class EqualsBuilderTest extends TestCase {
array1[1] = true; array1[1] = true;
assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals()); assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
} }
public static class TestACanEqualB {
private int a;
public TestACanEqualB(int a) {
this.a = a;
}
public boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof TestACanEqualB)
return this.a == ((TestACanEqualB) o).getA();
if (o instanceof TestBCanEqualA)
return this.a == ((TestBCanEqualA) o).getB();
return false;
}
public int getA() {
return this.a;
}
}
public static class TestBCanEqualA {
private int b;
public TestBCanEqualA(int b) {
this.b = b;
}
public boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof TestACanEqualB)
return this.b == ((TestACanEqualB) o).getA();
if (o instanceof TestBCanEqualA)
return this.b == ((TestBCanEqualA) o).getB();
return false;
}
public int getB() {
return this.b;
}
}
/**
* Tests two instances of classes that can be equal and that are not "related". The two classes are not subclasses
* of each other and do not share a parent aside from Object.
* See http://issues.apache.org/bugzilla/show_bug.cgi?id=33069
*/
public void testUnrelatedClasses() {
Object[] x = new Object[]{new TestACanEqualB(1)};
Object[] y = new Object[]{new TestBCanEqualA(1)};
// sanity checks:
assertTrue(Arrays.equals(x, x));
assertTrue(Arrays.equals(y, y));
assertTrue(Arrays.equals(x, y));
assertTrue(Arrays.equals(y, x));
// real tests:
assertTrue(x[0].equals(x[0]));
assertTrue(y[0].equals(y[0]));
assertTrue(x[0].equals(y[0]));
assertTrue(y[0].equals(x[0]));
assertTrue(new EqualsBuilder().append(x, x).isEquals());
assertTrue(new EqualsBuilder().append(y, y).isEquals());
assertTrue(new EqualsBuilder().append(x, y).isEquals());
assertTrue(new EqualsBuilder().append(y, x).isEquals());
}
} }