0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.commons.lang.builder;
0018:
0019: import java.util.Arrays;
0020:
0021: import junit.framework.Test;
0022: import junit.framework.TestCase;
0023: import junit.framework.TestSuite;
0024: import junit.textui.TestRunner;
0025:
0026: /**
0027: * Unit tests {@link org.apache.commons.lang.builder.EqualsBuilder}.
0028: *
0029: * @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
0030: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
0031: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
0032: * @author Maarten Coene
0033: * @version $Id: EqualsBuilderTest.java 437554 2006-08-28 06:21:41Z bayard $
0034: */
0035: public class EqualsBuilderTest extends TestCase {
0036:
0037: public EqualsBuilderTest(String name) {
0038: super (name);
0039: }
0040:
0041: public static void main(String[] args) {
0042: TestRunner.run(suite());
0043: }
0044:
0045: public static Test suite() {
0046: TestSuite suite = new TestSuite(EqualsBuilderTest.class);
0047: suite.setName("EqualsBuilder Tests");
0048: return suite;
0049: }
0050:
0051: protected void setUp() throws Exception {
0052: super .setUp();
0053: }
0054:
0055: protected void tearDown() throws Exception {
0056: super .tearDown();
0057: }
0058:
0059: //-----------------------------------------------------------------------
0060:
0061: static class TestObject {
0062: private int a;
0063:
0064: public TestObject() {
0065: }
0066:
0067: public TestObject(int a) {
0068: this .a = a;
0069: }
0070:
0071: public boolean equals(Object o) {
0072: if (o == this ) {
0073: return true;
0074: }
0075: if (!(o instanceof TestObject)) {
0076: return false;
0077: }
0078: TestObject rhs = (TestObject) o;
0079: return (a == rhs.a);
0080: }
0081:
0082: public void setA(int a) {
0083: this .a = a;
0084: }
0085:
0086: public int getA() {
0087: return a;
0088: }
0089: }
0090:
0091: static class TestSubObject extends TestObject {
0092: private int b;
0093:
0094: public TestSubObject() {
0095: super (0);
0096: }
0097:
0098: public TestSubObject(int a, int b) {
0099: super (a);
0100: this .b = b;
0101: }
0102:
0103: public boolean equals(Object o) {
0104: if (o == this ) {
0105: return true;
0106: }
0107: if (!(o instanceof TestSubObject)) {
0108: return false;
0109: }
0110: TestSubObject rhs = (TestSubObject) o;
0111: return super .equals(o) && (b == rhs.b);
0112: }
0113:
0114: public void setB(int b) {
0115: this .b = b;
0116: }
0117:
0118: public int getB() {
0119: return b;
0120: }
0121: }
0122:
0123: static class TestEmptySubObject extends TestObject {
0124: public TestEmptySubObject(int a) {
0125: super (a);
0126: }
0127: }
0128:
0129: static class TestTSubObject extends TestObject {
0130: private transient int t;
0131:
0132: public TestTSubObject(int a, int t) {
0133: super (a);
0134: this .t = t;
0135: }
0136: }
0137:
0138: static class TestTTSubObject extends TestTSubObject {
0139: private transient int tt;
0140:
0141: public TestTTSubObject(int a, int t, int tt) {
0142: super (a, t);
0143: this .tt = tt;
0144: }
0145: }
0146:
0147: static class TestTTLeafObject extends TestTTSubObject {
0148: private int leafValue;
0149:
0150: public TestTTLeafObject(int a, int t, int tt, int leafValue) {
0151: super (a, t, tt);
0152: this .leafValue = leafValue;
0153: }
0154: }
0155:
0156: static class TestTSubObject2 extends TestObject {
0157: private transient int t;
0158:
0159: public TestTSubObject2(int a, int t) {
0160: super (a);
0161: }
0162:
0163: public int getT() {
0164: return t;
0165: }
0166:
0167: public void setT(int t) {
0168: this .t = t;
0169: }
0170: }
0171:
0172: public void testReflectionEquals() {
0173: TestObject o1 = new TestObject(4);
0174: TestObject o2 = new TestObject(5);
0175: assertTrue(EqualsBuilder.reflectionEquals(o1, o1));
0176: assertTrue(!EqualsBuilder.reflectionEquals(o1, o2));
0177: o2.setA(4);
0178: assertTrue(EqualsBuilder.reflectionEquals(o1, o2));
0179:
0180: assertTrue(!EqualsBuilder.reflectionEquals(o1, this ));
0181:
0182: assertTrue(!EqualsBuilder.reflectionEquals(o1, null));
0183: assertTrue(!EqualsBuilder.reflectionEquals(null, o2));
0184: assertTrue(EqualsBuilder.reflectionEquals((Object) null,
0185: (Object) null));
0186: }
0187:
0188: public void testReflectionHierarchyEquals() {
0189: testReflectionHierarchyEquals(false);
0190: testReflectionHierarchyEquals(true);
0191: // Transients
0192: assertTrue(EqualsBuilder.reflectionEquals(new TestTTLeafObject(
0193: 1, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 4), true));
0194: assertTrue(EqualsBuilder.reflectionEquals(new TestTTLeafObject(
0195: 1, 2, 3, 4), new TestTTLeafObject(1, 2, 3, 4), false));
0196: assertTrue(!EqualsBuilder.reflectionEquals(
0197: new TestTTLeafObject(1, 0, 0, 4), new TestTTLeafObject(
0198: 1, 2, 3, 4), true));
0199: assertTrue(!EqualsBuilder.reflectionEquals(
0200: new TestTTLeafObject(1, 2, 3, 4), new TestTTLeafObject(
0201: 1, 2, 3, 0), true));
0202: assertTrue(!EqualsBuilder.reflectionEquals(
0203: new TestTTLeafObject(0, 2, 3, 4), new TestTTLeafObject(
0204: 1, 2, 3, 4), true));
0205: }
0206:
0207: public void testReflectionHierarchyEquals(boolean testTransients) {
0208: TestObject to1 = new TestObject(4);
0209: TestObject to1Bis = new TestObject(4);
0210: TestObject to1Ter = new TestObject(4);
0211: TestObject to2 = new TestObject(5);
0212: TestEmptySubObject teso = new TestEmptySubObject(4);
0213: TestTSubObject ttso = new TestTSubObject(4, 1);
0214: TestTTSubObject tttso = new TestTTSubObject(4, 1, 2);
0215: TestTTLeafObject ttlo = new TestTTLeafObject(4, 1, 2, 3);
0216: TestSubObject tso1 = new TestSubObject(1, 4);
0217: TestSubObject tso1bis = new TestSubObject(1, 4);
0218: TestSubObject tso1ter = new TestSubObject(1, 4);
0219: TestSubObject tso2 = new TestSubObject(2, 5);
0220:
0221: testReflectionEqualsEquivalenceRelationship(to1, to1Bis,
0222: to1Ter, to2, new TestObject(), testTransients);
0223: testReflectionEqualsEquivalenceRelationship(tso1, tso1bis,
0224: tso1ter, tso2, new TestSubObject(), testTransients);
0225:
0226: // More sanity checks:
0227:
0228: // same values
0229: assertTrue(EqualsBuilder.reflectionEquals(ttlo, ttlo,
0230: testTransients));
0231: assertTrue(EqualsBuilder.reflectionEquals(new TestSubObject(1,
0232: 10), new TestSubObject(1, 10), testTransients));
0233: // same super values, diff sub values
0234: assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1,
0235: 10), new TestSubObject(1, 11), testTransients));
0236: assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1,
0237: 11), new TestSubObject(1, 10), testTransients));
0238: // diff super values, same sub values
0239: assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(0,
0240: 10), new TestSubObject(1, 10), testTransients));
0241: assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(1,
0242: 10), new TestSubObject(0, 10), testTransients));
0243:
0244: // mix super and sub types: equals
0245: assertTrue(EqualsBuilder.reflectionEquals(to1, teso,
0246: testTransients));
0247: assertTrue(EqualsBuilder.reflectionEquals(teso, to1,
0248: testTransients));
0249:
0250: assertTrue(EqualsBuilder.reflectionEquals(to1, ttso, false)); // Force testTransients = false for this assert
0251: assertTrue(EqualsBuilder.reflectionEquals(ttso, to1, false)); // Force testTransients = false for this assert
0252:
0253: assertTrue(EqualsBuilder.reflectionEquals(to1, tttso, false)); // Force testTransients = false for this assert
0254: assertTrue(EqualsBuilder.reflectionEquals(tttso, to1, false)); // Force testTransients = false for this assert
0255:
0256: assertTrue(EqualsBuilder.reflectionEquals(ttso, tttso, false)); // Force testTransients = false for this assert
0257: assertTrue(EqualsBuilder.reflectionEquals(tttso, ttso, false)); // Force testTransients = false for this assert
0258:
0259: // mix super and sub types: NOT equals
0260: assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(0),
0261: new TestEmptySubObject(1), testTransients));
0262: assertTrue(!EqualsBuilder.reflectionEquals(
0263: new TestEmptySubObject(1), new TestObject(0),
0264: testTransients));
0265:
0266: assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(0),
0267: new TestTSubObject(1, 1), testTransients));
0268: assertTrue(!EqualsBuilder.reflectionEquals(new TestTSubObject(
0269: 1, 1), new TestObject(0), testTransients));
0270:
0271: assertTrue(!EqualsBuilder.reflectionEquals(new TestObject(1),
0272: new TestSubObject(0, 10), testTransients));
0273: assertTrue(!EqualsBuilder.reflectionEquals(new TestSubObject(0,
0274: 10), new TestObject(1), testTransients));
0275:
0276: assertTrue(!EqualsBuilder.reflectionEquals(to1, ttlo));
0277: assertTrue(!EqualsBuilder.reflectionEquals(tso1, this ));
0278: }
0279:
0280: /**
0281: * Equivalence relationship tests inspired by "Effective Java":
0282: * <ul>
0283: * <li>reflection</li>
0284: * <li>symmetry</li>
0285: * <li>transitive</li>
0286: * <li>consistency</li>
0287: * <li>non-null reference</li>
0288: * </ul>
0289: * @param to a TestObject
0290: * @param toBis a TestObject, equal to to and toTer
0291: * @param toTer Left hand side, equal to to and toBis
0292: * @param to2 a different TestObject
0293: * @param oToChange a TestObject that will be changed
0294: */
0295: public void testReflectionEqualsEquivalenceRelationship(
0296: TestObject to, TestObject toBis, TestObject toTer,
0297: TestObject to2, TestObject oToChange, boolean testTransients) {
0298:
0299: // reflection test
0300: assertTrue(EqualsBuilder.reflectionEquals(to, to,
0301: testTransients));
0302: assertTrue(EqualsBuilder.reflectionEquals(to2, to2,
0303: testTransients));
0304:
0305: // symmetry test
0306: assertTrue(EqualsBuilder.reflectionEquals(to, toBis,
0307: testTransients)
0308: && EqualsBuilder.reflectionEquals(toBis, to,
0309: testTransients));
0310:
0311: // transitive test
0312: assertTrue(EqualsBuilder.reflectionEquals(to, toBis,
0313: testTransients)
0314: && EqualsBuilder.reflectionEquals(toBis, toTer,
0315: testTransients)
0316: && EqualsBuilder.reflectionEquals(to, toTer,
0317: testTransients));
0318:
0319: // consistency test
0320: oToChange.setA(to.getA());
0321: if (oToChange instanceof TestSubObject) {
0322: ((TestSubObject) oToChange).setB(((TestSubObject) to)
0323: .getB());
0324: }
0325: assertTrue(EqualsBuilder.reflectionEquals(oToChange, to,
0326: testTransients));
0327: assertTrue(EqualsBuilder.reflectionEquals(oToChange, to,
0328: testTransients));
0329: oToChange.setA(to.getA() + 1);
0330: if (oToChange instanceof TestSubObject) {
0331: ((TestSubObject) oToChange).setB(((TestSubObject) to)
0332: .getB() + 1);
0333: }
0334: assertTrue(!EqualsBuilder.reflectionEquals(oToChange, to,
0335: testTransients));
0336: assertTrue(!EqualsBuilder.reflectionEquals(oToChange, to,
0337: testTransients));
0338:
0339: // non-null reference test
0340: assertTrue(!EqualsBuilder.reflectionEquals(to, null,
0341: testTransients));
0342: assertTrue(!EqualsBuilder.reflectionEquals(to2, null,
0343: testTransients));
0344: assertTrue(!EqualsBuilder.reflectionEquals(null, to,
0345: testTransients));
0346: assertTrue(!EqualsBuilder.reflectionEquals(null, to2,
0347: testTransients));
0348: assertTrue(EqualsBuilder.reflectionEquals((Object) null,
0349: (Object) null, testTransients));
0350: }
0351:
0352: public void testSuper() {
0353: TestObject o1 = new TestObject(4);
0354: TestObject o2 = new TestObject(5);
0355: assertEquals(true, new EqualsBuilder().appendSuper(true)
0356: .append(o1, o1).isEquals());
0357: assertEquals(false, new EqualsBuilder().appendSuper(false)
0358: .append(o1, o1).isEquals());
0359: assertEquals(false, new EqualsBuilder().appendSuper(true)
0360: .append(o1, o2).isEquals());
0361: assertEquals(false, new EqualsBuilder().appendSuper(false)
0362: .append(o1, o2).isEquals());
0363: }
0364:
0365: public void testObject() {
0366: TestObject o1 = new TestObject(4);
0367: TestObject o2 = new TestObject(5);
0368: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0369: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0370: o2.setA(4);
0371: assertTrue(new EqualsBuilder().append(o1, o2).isEquals());
0372:
0373: assertTrue(!new EqualsBuilder().append(o1, this ).isEquals());
0374:
0375: assertTrue(!new EqualsBuilder().append(o1, null).isEquals());
0376: assertTrue(!new EqualsBuilder().append(null, o2).isEquals());
0377: assertTrue(new EqualsBuilder().append((Object) null,
0378: (Object) null).isEquals());
0379: }
0380:
0381: public void testLong() {
0382: long o1 = 1L;
0383: long o2 = 2L;
0384: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0385: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0386: }
0387:
0388: public void testInt() {
0389: int o1 = 1;
0390: int o2 = 2;
0391: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0392: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0393: }
0394:
0395: public void testShort() {
0396: short o1 = 1;
0397: short o2 = 2;
0398: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0399: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0400: }
0401:
0402: public void testChar() {
0403: char o1 = 1;
0404: char o2 = 2;
0405: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0406: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0407: }
0408:
0409: public void testByte() {
0410: byte o1 = 1;
0411: byte o2 = 2;
0412: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0413: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0414: }
0415:
0416: public void testDouble() {
0417: double o1 = 1;
0418: double o2 = 2;
0419: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0420: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0421: assertTrue(!new EqualsBuilder().append(o1, Double.NaN)
0422: .isEquals());
0423: assertTrue(new EqualsBuilder().append(Double.NaN, Double.NaN)
0424: .isEquals());
0425: assertTrue(new EqualsBuilder().append(Double.POSITIVE_INFINITY,
0426: Double.POSITIVE_INFINITY).isEquals());
0427: }
0428:
0429: public void testFloat() {
0430: float o1 = 1;
0431: float o2 = 2;
0432: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0433: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0434: assertTrue(!new EqualsBuilder().append(o1, Float.NaN)
0435: .isEquals());
0436: assertTrue(new EqualsBuilder().append(Float.NaN, Float.NaN)
0437: .isEquals());
0438: assertTrue(new EqualsBuilder().append(Float.POSITIVE_INFINITY,
0439: Float.POSITIVE_INFINITY).isEquals());
0440: }
0441:
0442: public void testAccessors() {
0443: EqualsBuilder equalsBuilder = new EqualsBuilder();
0444: assertTrue(equalsBuilder.isEquals());
0445: equalsBuilder.setEquals(true);
0446: assertTrue(equalsBuilder.isEquals());
0447: equalsBuilder.setEquals(false);
0448: assertFalse(equalsBuilder.isEquals());
0449: }
0450:
0451: public void testBoolean() {
0452: boolean o1 = true;
0453: boolean o2 = false;
0454: assertTrue(new EqualsBuilder().append(o1, o1).isEquals());
0455: assertTrue(!new EqualsBuilder().append(o1, o2).isEquals());
0456: }
0457:
0458: public void testObjectArray() {
0459: TestObject[] obj1 = new TestObject[3];
0460: obj1[0] = new TestObject(4);
0461: obj1[1] = new TestObject(5);
0462: obj1[2] = null;
0463: TestObject[] obj2 = new TestObject[3];
0464: obj2[0] = new TestObject(4);
0465: obj2[1] = new TestObject(5);
0466: obj2[2] = null;
0467:
0468: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0469: assertTrue(new EqualsBuilder().append(obj2, obj2).isEquals());
0470: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0471: obj1[1].setA(6);
0472: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0473: obj1[1].setA(5);
0474: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0475: obj1[2] = obj1[1];
0476: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0477: obj1[2] = null;
0478: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0479:
0480: obj2 = null;
0481: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0482: obj1 = null;
0483: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0484: }
0485:
0486: public void testLongArray() {
0487: long[] obj1 = new long[2];
0488: obj1[0] = 5L;
0489: obj1[1] = 6L;
0490: long[] obj2 = new long[2];
0491: obj2[0] = 5L;
0492: obj2[1] = 6L;
0493: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0494: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0495: obj1[1] = 7;
0496: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0497:
0498: obj2 = null;
0499: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0500: obj1 = null;
0501: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0502: }
0503:
0504: public void testIntArray() {
0505: int[] obj1 = new int[2];
0506: obj1[0] = 5;
0507: obj1[1] = 6;
0508: int[] obj2 = new int[2];
0509: obj2[0] = 5;
0510: obj2[1] = 6;
0511: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0512: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0513: obj1[1] = 7;
0514: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0515:
0516: obj2 = null;
0517: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0518: obj1 = null;
0519: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0520: }
0521:
0522: public void testShortArray() {
0523: short[] obj1 = new short[2];
0524: obj1[0] = 5;
0525: obj1[1] = 6;
0526: short[] obj2 = new short[2];
0527: obj2[0] = 5;
0528: obj2[1] = 6;
0529: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0530: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0531: obj1[1] = 7;
0532: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0533:
0534: obj2 = null;
0535: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0536: obj1 = null;
0537: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0538: }
0539:
0540: public void testCharArray() {
0541: char[] obj1 = new char[2];
0542: obj1[0] = 5;
0543: obj1[1] = 6;
0544: char[] obj2 = new char[2];
0545: obj2[0] = 5;
0546: obj2[1] = 6;
0547: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0548: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0549: obj1[1] = 7;
0550: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0551:
0552: obj2 = null;
0553: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0554: obj1 = null;
0555: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0556: }
0557:
0558: public void testByteArray() {
0559: byte[] obj1 = new byte[2];
0560: obj1[0] = 5;
0561: obj1[1] = 6;
0562: byte[] obj2 = new byte[2];
0563: obj2[0] = 5;
0564: obj2[1] = 6;
0565: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0566: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0567: obj1[1] = 7;
0568: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0569:
0570: obj2 = null;
0571: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0572: obj1 = null;
0573: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0574: }
0575:
0576: public void testDoubleArray() {
0577: double[] obj1 = new double[2];
0578: obj1[0] = 5;
0579: obj1[1] = 6;
0580: double[] obj2 = new double[2];
0581: obj2[0] = 5;
0582: obj2[1] = 6;
0583: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0584: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0585: obj1[1] = 7;
0586: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0587:
0588: obj2 = null;
0589: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0590: obj1 = null;
0591: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0592: }
0593:
0594: public void testFloatArray() {
0595: float[] obj1 = new float[2];
0596: obj1[0] = 5;
0597: obj1[1] = 6;
0598: float[] obj2 = new float[2];
0599: obj2[0] = 5;
0600: obj2[1] = 6;
0601: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0602: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0603: obj1[1] = 7;
0604: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0605:
0606: obj2 = null;
0607: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0608: obj1 = null;
0609: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0610: }
0611:
0612: public void testBooleanArray() {
0613: boolean[] obj1 = new boolean[2];
0614: obj1[0] = true;
0615: obj1[1] = false;
0616: boolean[] obj2 = new boolean[2];
0617: obj2[0] = true;
0618: obj2[1] = false;
0619: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0620: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0621: obj1[1] = true;
0622: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0623:
0624: obj2 = null;
0625: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0626: obj1 = null;
0627: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0628: }
0629:
0630: public void testMultiLongArray() {
0631: long[][] array1 = new long[2][2];
0632: long[][] array2 = new long[2][2];
0633: for (int i = 0; i < array1.length; ++i) {
0634: for (int j = 0; j < array1[0].length; j++) {
0635: array1[i][j] = (i + 1) * (j + 1);
0636: array2[i][j] = (i + 1) * (j + 1);
0637: }
0638: }
0639: assertTrue(new EqualsBuilder().append(array1, array1)
0640: .isEquals());
0641: assertTrue(new EqualsBuilder().append(array1, array2)
0642: .isEquals());
0643: array1[1][1] = 0;
0644: assertTrue(!new EqualsBuilder().append(array1, array2)
0645: .isEquals());
0646: }
0647:
0648: public void testMultiIntArray() {
0649: int[][] array1 = new int[2][2];
0650: int[][] array2 = new int[2][2];
0651: for (int i = 0; i < array1.length; ++i) {
0652: for (int j = 0; j < array1[0].length; j++) {
0653: array1[i][j] = (i + 1) * (j + 1);
0654: array2[i][j] = (i + 1) * (j + 1);
0655: }
0656: }
0657: assertTrue(new EqualsBuilder().append(array1, array1)
0658: .isEquals());
0659: assertTrue(new EqualsBuilder().append(array1, array2)
0660: .isEquals());
0661: array1[1][1] = 0;
0662: assertTrue(!new EqualsBuilder().append(array1, array2)
0663: .isEquals());
0664: }
0665:
0666: public void testMultiShortArray() {
0667: short[][] array1 = new short[2][2];
0668: short[][] array2 = new short[2][2];
0669: for (short i = 0; i < array1.length; ++i) {
0670: for (short j = 0; j < array1[0].length; j++) {
0671: array1[i][j] = i;
0672: array2[i][j] = i;
0673: }
0674: }
0675: assertTrue(new EqualsBuilder().append(array1, array1)
0676: .isEquals());
0677: assertTrue(new EqualsBuilder().append(array1, array2)
0678: .isEquals());
0679: array1[1][1] = 0;
0680: assertTrue(!new EqualsBuilder().append(array1, array2)
0681: .isEquals());
0682: }
0683:
0684: public void testMultiCharArray() {
0685: char[][] array1 = new char[2][2];
0686: char[][] array2 = new char[2][2];
0687: for (char i = 0; i < array1.length; ++i) {
0688: for (char j = 0; j < array1[0].length; j++) {
0689: array1[i][j] = i;
0690: array2[i][j] = i;
0691: }
0692: }
0693: assertTrue(new EqualsBuilder().append(array1, array1)
0694: .isEquals());
0695: assertTrue(new EqualsBuilder().append(array1, array2)
0696: .isEquals());
0697: array1[1][1] = 0;
0698: assertTrue(!new EqualsBuilder().append(array1, array2)
0699: .isEquals());
0700: }
0701:
0702: public void testMultiByteArray() {
0703: byte[][] array1 = new byte[2][2];
0704: byte[][] array2 = new byte[2][2];
0705: for (byte i = 0; i < array1.length; ++i) {
0706: for (byte j = 0; j < array1[0].length; j++) {
0707: array1[i][j] = i;
0708: array2[i][j] = i;
0709: }
0710: }
0711: assertTrue(new EqualsBuilder().append(array1, array1)
0712: .isEquals());
0713: assertTrue(new EqualsBuilder().append(array1, array2)
0714: .isEquals());
0715: array1[1][1] = 0;
0716: assertTrue(!new EqualsBuilder().append(array1, array2)
0717: .isEquals());
0718: }
0719:
0720: public void testMultiFloatArray() {
0721: float[][] array1 = new float[2][2];
0722: float[][] array2 = new float[2][2];
0723: for (int i = 0; i < array1.length; ++i) {
0724: for (int j = 0; j < array1[0].length; j++) {
0725: array1[i][j] = (i + 1) * (j + 1);
0726: array2[i][j] = (i + 1) * (j + 1);
0727: }
0728: }
0729: assertTrue(new EqualsBuilder().append(array1, array1)
0730: .isEquals());
0731: assertTrue(new EqualsBuilder().append(array1, array2)
0732: .isEquals());
0733: array1[1][1] = 0;
0734: assertTrue(!new EqualsBuilder().append(array1, array2)
0735: .isEquals());
0736: }
0737:
0738: public void testMultiDoubleArray() {
0739: double[][] array1 = new double[2][2];
0740: double[][] array2 = new double[2][2];
0741: for (int i = 0; i < array1.length; ++i) {
0742: for (int j = 0; j < array1[0].length; j++) {
0743: array1[i][j] = (i + 1) * (j + 1);
0744: array2[i][j] = (i + 1) * (j + 1);
0745: }
0746: }
0747: assertTrue(new EqualsBuilder().append(array1, array1)
0748: .isEquals());
0749: assertTrue(new EqualsBuilder().append(array1, array2)
0750: .isEquals());
0751: array1[1][1] = 0;
0752: assertTrue(!new EqualsBuilder().append(array1, array2)
0753: .isEquals());
0754: }
0755:
0756: public void testMultiBooleanArray() {
0757: boolean[][] array1 = new boolean[2][2];
0758: boolean[][] array2 = new boolean[2][2];
0759: for (int i = 0; i < array1.length; ++i) {
0760: for (int j = 0; j < array1[0].length; j++) {
0761: array1[i][j] = (i == 1) || (j == 1);
0762: array2[i][j] = (i == 1) || (j == 1);
0763: }
0764: }
0765: assertTrue(new EqualsBuilder().append(array1, array1)
0766: .isEquals());
0767: assertTrue(new EqualsBuilder().append(array1, array2)
0768: .isEquals());
0769: array1[1][1] = false;
0770: assertTrue(!new EqualsBuilder().append(array1, array2)
0771: .isEquals());
0772:
0773: // compare 1 dim to 2.
0774: boolean[] array3 = new boolean[] { true, true };
0775: assertFalse(new EqualsBuilder().append(array1, array3)
0776: .isEquals());
0777: assertFalse(new EqualsBuilder().append(array3, array1)
0778: .isEquals());
0779: assertFalse(new EqualsBuilder().append(array2, array3)
0780: .isEquals());
0781: assertFalse(new EqualsBuilder().append(array3, array2)
0782: .isEquals());
0783: }
0784:
0785: public void testRaggedArray() {
0786: long array1[][] = new long[2][];
0787: long array2[][] = new long[2][];
0788: for (int i = 0; i < array1.length; ++i) {
0789: array1[i] = new long[2];
0790: array2[i] = new long[2];
0791: for (int j = 0; j < array1[i].length; ++j) {
0792: array1[i][j] = (i + 1) * (j + 1);
0793: array2[i][j] = (i + 1) * (j + 1);
0794: }
0795: }
0796: assertTrue(new EqualsBuilder().append(array1, array1)
0797: .isEquals());
0798: assertTrue(new EqualsBuilder().append(array1, array2)
0799: .isEquals());
0800: array1[1][1] = 0;
0801: assertTrue(!new EqualsBuilder().append(array1, array2)
0802: .isEquals());
0803: }
0804:
0805: public void testMixedArray() {
0806: Object array1[] = new Object[2];
0807: Object array2[] = new Object[2];
0808: for (int i = 0; i < array1.length; ++i) {
0809: array1[i] = new long[2];
0810: array2[i] = new long[2];
0811: for (int j = 0; j < 2; ++j) {
0812: ((long[]) array1[i])[j] = (i + 1) * (j + 1);
0813: ((long[]) array2[i])[j] = (i + 1) * (j + 1);
0814: }
0815: }
0816: assertTrue(new EqualsBuilder().append(array1, array1)
0817: .isEquals());
0818: assertTrue(new EqualsBuilder().append(array1, array2)
0819: .isEquals());
0820: ((long[]) array1[1])[1] = 0;
0821: assertTrue(!new EqualsBuilder().append(array1, array2)
0822: .isEquals());
0823: }
0824:
0825: public void testObjectArrayHiddenByObject() {
0826: TestObject[] array1 = new TestObject[2];
0827: array1[0] = new TestObject(4);
0828: array1[1] = new TestObject(5);
0829: TestObject[] array2 = new TestObject[2];
0830: array2[0] = new TestObject(4);
0831: array2[1] = new TestObject(5);
0832: Object obj1 = array1;
0833: Object obj2 = array2;
0834: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0835: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0836: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0837: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0838: array1[1].setA(6);
0839: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0840: }
0841:
0842: public void testLongArrayHiddenByObject() {
0843: long[] array1 = new long[2];
0844: array1[0] = 5L;
0845: array1[1] = 6L;
0846: long[] array2 = new long[2];
0847: array2[0] = 5L;
0848: array2[1] = 6L;
0849: Object obj1 = array1;
0850: Object obj2 = array2;
0851: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0852: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0853: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0854: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0855: array1[1] = 7;
0856: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0857: }
0858:
0859: public void testIntArrayHiddenByObject() {
0860: int[] array1 = new int[2];
0861: array1[0] = 5;
0862: array1[1] = 6;
0863: int[] array2 = new int[2];
0864: array2[0] = 5;
0865: array2[1] = 6;
0866: Object obj1 = array1;
0867: Object obj2 = array2;
0868: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0869: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0870: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0871: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0872: array1[1] = 7;
0873: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0874: }
0875:
0876: public void testShortArrayHiddenByObject() {
0877: short[] array1 = new short[2];
0878: array1[0] = 5;
0879: array1[1] = 6;
0880: short[] array2 = new short[2];
0881: array2[0] = 5;
0882: array2[1] = 6;
0883: Object obj1 = array1;
0884: Object obj2 = array2;
0885: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0886: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0887: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0888: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0889: array1[1] = 7;
0890: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0891: }
0892:
0893: public void testCharArrayHiddenByObject() {
0894: char[] array1 = new char[2];
0895: array1[0] = 5;
0896: array1[1] = 6;
0897: char[] array2 = new char[2];
0898: array2[0] = 5;
0899: array2[1] = 6;
0900: Object obj1 = array1;
0901: Object obj2 = array2;
0902: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0903: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0904: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0905: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0906: array1[1] = 7;
0907: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0908: }
0909:
0910: public void testByteArrayHiddenByObject() {
0911: byte[] array1 = new byte[2];
0912: array1[0] = 5;
0913: array1[1] = 6;
0914: byte[] array2 = new byte[2];
0915: array2[0] = 5;
0916: array2[1] = 6;
0917: Object obj1 = array1;
0918: Object obj2 = array2;
0919: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0920: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0921: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0922: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0923: array1[1] = 7;
0924: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0925: }
0926:
0927: public void testDoubleArrayHiddenByObject() {
0928: double[] array1 = new double[2];
0929: array1[0] = 5;
0930: array1[1] = 6;
0931: double[] array2 = new double[2];
0932: array2[0] = 5;
0933: array2[1] = 6;
0934: Object obj1 = array1;
0935: Object obj2 = array2;
0936: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0937: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0938: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0939: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0940: array1[1] = 7;
0941: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0942: }
0943:
0944: public void testFloatArrayHiddenByObject() {
0945: float[] array1 = new float[2];
0946: array1[0] = 5;
0947: array1[1] = 6;
0948: float[] array2 = new float[2];
0949: array2[0] = 5;
0950: array2[1] = 6;
0951: Object obj1 = array1;
0952: Object obj2 = array2;
0953: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0954: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0955: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0956: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0957: array1[1] = 7;
0958: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0959: }
0960:
0961: public void testBooleanArrayHiddenByObject() {
0962: boolean[] array1 = new boolean[2];
0963: array1[0] = true;
0964: array1[1] = false;
0965: boolean[] array2 = new boolean[2];
0966: array2[0] = true;
0967: array2[1] = false;
0968: Object obj1 = array1;
0969: Object obj2 = array2;
0970: assertTrue(new EqualsBuilder().append(obj1, obj1).isEquals());
0971: assertTrue(new EqualsBuilder().append(obj1, array1).isEquals());
0972: assertTrue(new EqualsBuilder().append(obj1, obj2).isEquals());
0973: assertTrue(new EqualsBuilder().append(obj1, array2).isEquals());
0974: array1[1] = true;
0975: assertTrue(!new EqualsBuilder().append(obj1, obj2).isEquals());
0976: }
0977:
0978: public static class TestACanEqualB {
0979: private int a;
0980:
0981: public TestACanEqualB(int a) {
0982: this .a = a;
0983: }
0984:
0985: public boolean equals(Object o) {
0986: if (o == this )
0987: return true;
0988: if (o instanceof TestACanEqualB)
0989: return this .a == ((TestACanEqualB) o).getA();
0990: if (o instanceof TestBCanEqualA)
0991: return this .a == ((TestBCanEqualA) o).getB();
0992: return false;
0993: }
0994:
0995: public int getA() {
0996: return this .a;
0997: }
0998: }
0999:
1000: public static class TestBCanEqualA {
1001: private int b;
1002:
1003: public TestBCanEqualA(int b) {
1004: this .b = b;
1005: }
1006:
1007: public boolean equals(Object o) {
1008: if (o == this )
1009: return true;
1010: if (o instanceof TestACanEqualB)
1011: return this .b == ((TestACanEqualB) o).getA();
1012: if (o instanceof TestBCanEqualA)
1013: return this .b == ((TestBCanEqualA) o).getB();
1014: return false;
1015: }
1016:
1017: public int getB() {
1018: return this .b;
1019: }
1020: }
1021:
1022: /**
1023: * Tests two instances of classes that can be equal and that are not "related". The two classes are not subclasses
1024: * of each other and do not share a parent aside from Object.
1025: * See http://issues.apache.org/bugzilla/show_bug.cgi?id=33069
1026: */
1027: public void testUnrelatedClasses() {
1028: Object[] x = new Object[] { new TestACanEqualB(1) };
1029: Object[] y = new Object[] { new TestBCanEqualA(1) };
1030:
1031: // sanity checks:
1032: assertTrue(Arrays.equals(x, x));
1033: assertTrue(Arrays.equals(y, y));
1034: assertTrue(Arrays.equals(x, y));
1035: assertTrue(Arrays.equals(y, x));
1036: // real tests:
1037: assertTrue(x[0].equals(x[0]));
1038: assertTrue(y[0].equals(y[0]));
1039: assertTrue(x[0].equals(y[0]));
1040: assertTrue(y[0].equals(x[0]));
1041: assertTrue(new EqualsBuilder().append(x, x).isEquals());
1042: assertTrue(new EqualsBuilder().append(y, y).isEquals());
1043: assertTrue(new EqualsBuilder().append(x, y).isEquals());
1044: assertTrue(new EqualsBuilder().append(y, x).isEquals());
1045: }
1046:
1047: /**
1048: * Test from http://issues.apache.org/bugzilla/show_bug.cgi?id=33067
1049: */
1050: public void testNpeForNullElement() {
1051: Object[] x1 = new Object[] { new Integer(1), null,
1052: new Integer(3) };
1053: Object[] x2 = new Object[] { new Integer(1), new Integer(2),
1054: new Integer(3) };
1055:
1056: // causes an NPE in 2.0 according to:
1057: // http://issues.apache.org/bugzilla/show_bug.cgi?id=33067
1058: new EqualsBuilder().append(x1, x2);
1059: }
1060:
1061: public void testReflectionEqualsExcludeFields() throws Exception {
1062: TestObjectWithMultipleFields x1 = new TestObjectWithMultipleFields(
1063: 1, 2, 3);
1064: TestObjectWithMultipleFields x2 = new TestObjectWithMultipleFields(
1065: 1, 3, 4);
1066:
1067: // not equal when including all fields
1068: assertTrue(!EqualsBuilder.reflectionEquals(x1, x2));
1069:
1070: // doesn't barf on null, empty array, or non-existent field, but still tests as not equal
1071: assertTrue(!EqualsBuilder.reflectionEquals(x1, x2,
1072: (String[]) null));
1073: assertTrue(!EqualsBuilder.reflectionEquals(x1, x2,
1074: new String[] {}));
1075: assertTrue(!EqualsBuilder.reflectionEquals(x1, x2,
1076: new String[] { "xxx" }));
1077:
1078: // not equal if only one of the differing fields excluded
1079: assertTrue(!EqualsBuilder.reflectionEquals(x1, x2,
1080: new String[] { "two" }));
1081: assertTrue(!EqualsBuilder.reflectionEquals(x1, x2,
1082: new String[] { "three" }));
1083:
1084: // equal if both differing fields excluded
1085: assertTrue(EqualsBuilder.reflectionEquals(x1, x2, new String[] {
1086: "two", "three" }));
1087:
1088: // still equal as long as both differing fields are among excluded
1089: assertTrue(EqualsBuilder.reflectionEquals(x1, x2, new String[] {
1090: "one", "two", "three" }));
1091: assertTrue(EqualsBuilder.reflectionEquals(x1, x2, new String[] {
1092: "one", "two", "three", "xxx" }));
1093: }
1094:
1095: static class TestObjectWithMultipleFields {
1096: private TestObject one;
1097: private TestObject two;
1098: private TestObject three;
1099:
1100: public TestObjectWithMultipleFields(int one, int two, int three) {
1101: this .one = new TestObject(one);
1102: this .two = new TestObject(two);
1103: this .three = new TestObject(three);
1104: }
1105: }
1106: }
|