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.math.BigInteger;
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.CompareToBuilder}.
0028: *
0029: * @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
0030: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
0031: * @version $Id: CompareToBuilderTest.java 437554 2006-08-28 06:21:41Z bayard $
0032: */
0033: public class CompareToBuilderTest extends TestCase {
0034:
0035: public CompareToBuilderTest(String name) {
0036: super (name);
0037: }
0038:
0039: public static void main(String[] args) {
0040: TestRunner.run(suite());
0041: }
0042:
0043: public static Test suite() {
0044: TestSuite suite = new TestSuite(CompareToBuilderTest.class);
0045: suite.setName("CompareToBuilder Tests");
0046: return suite;
0047: }
0048:
0049: protected void setUp() throws Exception {
0050: super .setUp();
0051: }
0052:
0053: protected void tearDown() throws Exception {
0054: super .tearDown();
0055: }
0056:
0057: //-----------------------------------------------------------------------
0058:
0059: static class TestObject implements Comparable {
0060: private int a;
0061:
0062: public TestObject(int a) {
0063: this .a = a;
0064: }
0065:
0066: public boolean equals(Object o) {
0067: if (o == this ) {
0068: return true;
0069: }
0070: if (!(o instanceof TestObject)) {
0071: return false;
0072: }
0073: TestObject rhs = (TestObject) o;
0074: return (a == rhs.a);
0075: }
0076:
0077: public void setA(int a) {
0078: this .a = a;
0079: }
0080:
0081: public int getA() {
0082: return a;
0083: }
0084:
0085: public int compareTo(Object o) {
0086: TestObject rhs = (TestObject) o;
0087: return (a < rhs.a) ? -1 : (a > rhs.a) ? +1 : 0;
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:
0115: static class TestTransientSubObject extends TestObject {
0116: private transient int t;
0117:
0118: public TestTransientSubObject(int a, int t) {
0119: super (a);
0120: this .t = t;
0121: }
0122: }
0123:
0124: public void testReflectionCompare() {
0125: TestObject o1 = new TestObject(4);
0126: TestObject o2 = new TestObject(4);
0127: assertTrue(CompareToBuilder.reflectionCompare(o1, o1) == 0);
0128: assertTrue(CompareToBuilder.reflectionCompare(o1, o2) == 0);
0129: o2.setA(5);
0130: assertTrue(CompareToBuilder.reflectionCompare(o1, o2) < 0);
0131: assertTrue(CompareToBuilder.reflectionCompare(o2, o1) > 0);
0132: }
0133:
0134: public void testReflectionCompareEx1() {
0135: TestObject o1 = new TestObject(4);
0136: try {
0137: CompareToBuilder.reflectionCompare(o1, null);
0138: } catch (NullPointerException ex) {
0139: return;
0140: }
0141: fail();
0142: }
0143:
0144: public void testReflectionCompareEx2() {
0145: TestObject o1 = new TestObject(4);
0146: Object o2 = new Object();
0147: try {
0148: CompareToBuilder.reflectionCompare(o1, o2);
0149: fail();
0150: } catch (ClassCastException ex) {
0151: }
0152: }
0153:
0154: public void testReflectionHierarchyCompare() {
0155: testReflectionHierarchyCompare(false, null);
0156: }
0157:
0158: public void testReflectionHierarchyCompareExcludeFields() {
0159: String[] excludeFields = new String[] { "b" };
0160: testReflectionHierarchyCompare(true, excludeFields);
0161:
0162: TestSubObject x;
0163: TestSubObject y;
0164: TestSubObject z;
0165:
0166: x = new TestSubObject(1, 1);
0167: y = new TestSubObject(2, 1);
0168: z = new TestSubObject(3, 1);
0169: assertXYZCompareOrder(x, y, z, true, excludeFields);
0170:
0171: x = new TestSubObject(1, 3);
0172: y = new TestSubObject(2, 2);
0173: z = new TestSubObject(3, 1);
0174: assertXYZCompareOrder(x, y, z, true, excludeFields);
0175: }
0176:
0177: public void testReflectionHierarchyCompareTransients() {
0178: testReflectionHierarchyCompare(true, null);
0179:
0180: TestTransientSubObject x;
0181: TestTransientSubObject y;
0182: TestTransientSubObject z;
0183:
0184: x = new TestTransientSubObject(1, 1);
0185: y = new TestTransientSubObject(2, 2);
0186: z = new TestTransientSubObject(3, 3);
0187: assertXYZCompareOrder(x, y, z, true, null);
0188:
0189: x = new TestTransientSubObject(1, 1);
0190: y = new TestTransientSubObject(1, 2);
0191: z = new TestTransientSubObject(1, 3);
0192: assertXYZCompareOrder(x, y, z, true, null);
0193: }
0194:
0195: private void assertXYZCompareOrder(Object x, Object y, Object z,
0196: boolean testTransients, String[] excludeFields) {
0197: assertTrue(0 == CompareToBuilder.reflectionCompare(x, x,
0198: testTransients, null, excludeFields));
0199: assertTrue(0 == CompareToBuilder.reflectionCompare(y, y,
0200: testTransients, null, excludeFields));
0201: assertTrue(0 == CompareToBuilder.reflectionCompare(z, z,
0202: testTransients, null, excludeFields));
0203:
0204: assertTrue(0 > CompareToBuilder.reflectionCompare(x, y,
0205: testTransients, null, excludeFields));
0206: assertTrue(0 > CompareToBuilder.reflectionCompare(x, z,
0207: testTransients, null, excludeFields));
0208: assertTrue(0 > CompareToBuilder.reflectionCompare(y, z,
0209: testTransients, null, excludeFields));
0210:
0211: assertTrue(0 < CompareToBuilder.reflectionCompare(y, x,
0212: testTransients, null, excludeFields));
0213: assertTrue(0 < CompareToBuilder.reflectionCompare(z, x,
0214: testTransients, null, excludeFields));
0215: assertTrue(0 < CompareToBuilder.reflectionCompare(z, y,
0216: testTransients, null, excludeFields));
0217: }
0218:
0219: public void testReflectionHierarchyCompare(boolean testTransients,
0220: String[] excludeFields) {
0221: TestObject to1 = new TestObject(1);
0222: TestObject to2 = new TestObject(2);
0223: TestObject to3 = new TestObject(3);
0224: TestSubObject tso1 = new TestSubObject(1, 1);
0225: TestSubObject tso2 = new TestSubObject(2, 2);
0226: TestSubObject tso3 = new TestSubObject(3, 3);
0227:
0228: assertReflectionCompareContract(to1, to1, to1, false,
0229: excludeFields);
0230: assertReflectionCompareContract(to1, to2, to3, false,
0231: excludeFields);
0232: assertReflectionCompareContract(tso1, tso1, tso1, false,
0233: excludeFields);
0234: assertReflectionCompareContract(tso1, tso2, tso3, false,
0235: excludeFields);
0236: assertReflectionCompareContract("1", "2", "3", false,
0237: excludeFields);
0238:
0239: assertTrue(0 != CompareToBuilder.reflectionCompare(tso1,
0240: new TestSubObject(1, 0), testTransients));
0241: assertTrue(0 != CompareToBuilder.reflectionCompare(tso1,
0242: new TestSubObject(0, 1), testTransients));
0243:
0244: // root class
0245: assertXYZCompareOrder(to1, to2, to3, true, null);
0246: // subclass
0247: assertXYZCompareOrder(tso1, tso2, tso3, true, null);
0248: }
0249:
0250: /**
0251: * See "Effective Java" under "Consider Implementing Comparable".
0252: *
0253: * @param x an object to compare
0254: * @param y an object to compare
0255: * @param z an object to compare
0256: * @param testTransients Whether to include transients in the comparison
0257: * @param excludeFields fields to exclude
0258: */
0259: public void assertReflectionCompareContract(Object x, Object y,
0260: Object z, boolean testTransients, String[] excludeFields) {
0261:
0262: // signum
0263: assertTrue(reflectionCompareSignum(x, y, testTransients,
0264: excludeFields) == -reflectionCompareSignum(y, x,
0265: testTransients, excludeFields));
0266:
0267: // transitive
0268: if (CompareToBuilder.reflectionCompare(x, y, testTransients,
0269: null, excludeFields) > 0
0270: && CompareToBuilder.reflectionCompare(y, z,
0271: testTransients, null, excludeFields) > 0) {
0272: assertTrue(CompareToBuilder.reflectionCompare(x, z,
0273: testTransients, null, excludeFields) > 0);
0274: }
0275:
0276: // un-named
0277: if (CompareToBuilder.reflectionCompare(x, y, testTransients,
0278: null, excludeFields) == 0) {
0279: assertTrue(reflectionCompareSignum(x, z, testTransients,
0280: excludeFields) == -reflectionCompareSignum(y, z,
0281: testTransients, excludeFields));
0282: }
0283:
0284: // strongly recommended but not strictly required
0285: assertTrue((CompareToBuilder.reflectionCompare(x, y,
0286: testTransients) == 0) == EqualsBuilder
0287: .reflectionEquals(x, y, testTransients));
0288: }
0289:
0290: /**
0291: * Returns the signum of the result of comparing x and y with
0292: * <code>CompareToBuilder.reflectionCompare</code>
0293: *
0294: * @param lhs The "left-hand-side" of the comparison.
0295: * @param rhs The "right-hand-side" of the comparison.
0296: * @param testTransients Whether to include transients in the comparison
0297: * @param excludeFields fields to exclude
0298: * @return int The signum
0299: */
0300: private int reflectionCompareSignum(Object lhs, Object rhs,
0301: boolean testTransients, String[] excludeFields) {
0302: return BigInteger.valueOf(
0303: CompareToBuilder.reflectionCompare(lhs, rhs,
0304: testTransients)).signum();
0305: }
0306:
0307: public void testAppendSuper() {
0308: TestObject o1 = new TestObject(4);
0309: TestObject o2 = new TestObject(5);
0310: assertTrue(new CompareToBuilder().appendSuper(0).append(o1, o1)
0311: .toComparison() == 0);
0312: assertTrue(new CompareToBuilder().appendSuper(0).append(o1, o2)
0313: .toComparison() < 0);
0314: assertTrue(new CompareToBuilder().appendSuper(0).append(o2, o1)
0315: .toComparison() > 0);
0316:
0317: assertTrue(new CompareToBuilder().appendSuper(-1)
0318: .append(o1, o1).toComparison() < 0);
0319: assertTrue(new CompareToBuilder().appendSuper(-1)
0320: .append(o1, o2).toComparison() < 0);
0321:
0322: assertTrue(new CompareToBuilder().appendSuper(1).append(o1, o1)
0323: .toComparison() > 0);
0324: assertTrue(new CompareToBuilder().appendSuper(1).append(o1, o2)
0325: .toComparison() > 0);
0326: }
0327:
0328: public void testObject() {
0329: TestObject o1 = new TestObject(4);
0330: TestObject o2 = new TestObject(4);
0331: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0332: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() == 0);
0333: o2.setA(5);
0334: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0);
0335: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0);
0336:
0337: assertTrue(new CompareToBuilder().append(o1, null)
0338: .toComparison() > 0);
0339: assertTrue(new CompareToBuilder().append((Object) null,
0340: (Object) null).toComparison() == 0);
0341: assertTrue(new CompareToBuilder().append(null, o1)
0342: .toComparison() < 0);
0343: }
0344:
0345: public void testObjectEx2() {
0346: TestObject o1 = new TestObject(4);
0347: Object o2 = new Object();
0348: try {
0349: new CompareToBuilder().append(o1, o2);
0350: fail();
0351: } catch (ClassCastException ex) {
0352: }
0353: }
0354:
0355: public void testObjectComparator() {
0356: String o1 = "Fred";
0357: String o2 = "Fred";
0358: assertTrue(new CompareToBuilder().append(o1, o1,
0359: String.CASE_INSENSITIVE_ORDER).toComparison() == 0);
0360: assertTrue(new CompareToBuilder().append(o1, o2,
0361: String.CASE_INSENSITIVE_ORDER).toComparison() == 0);
0362: o2 = "FRED";
0363: assertTrue(new CompareToBuilder().append(o1, o2,
0364: String.CASE_INSENSITIVE_ORDER).toComparison() == 0);
0365: assertTrue(new CompareToBuilder().append(o2, o1,
0366: String.CASE_INSENSITIVE_ORDER).toComparison() == 0);
0367: o2 = "FREDA";
0368: assertTrue(new CompareToBuilder().append(o1, o2,
0369: String.CASE_INSENSITIVE_ORDER).toComparison() < 0);
0370: assertTrue(new CompareToBuilder().append(o2, o1,
0371: String.CASE_INSENSITIVE_ORDER).toComparison() > 0);
0372:
0373: assertTrue(new CompareToBuilder().append(o1, null,
0374: String.CASE_INSENSITIVE_ORDER).toComparison() > 0);
0375: assertTrue(new CompareToBuilder().append((Object) null,
0376: (Object) null, String.CASE_INSENSITIVE_ORDER)
0377: .toComparison() == 0);
0378: assertTrue(new CompareToBuilder().append(null, o1,
0379: String.CASE_INSENSITIVE_ORDER).toComparison() < 0);
0380: }
0381:
0382: public void testObjectComparatorNull() {
0383: String o1 = "Fred";
0384: String o2 = "Fred";
0385: assertTrue(new CompareToBuilder().append(o1, o1, null)
0386: .toComparison() == 0);
0387: assertTrue(new CompareToBuilder().append(o1, o2, null)
0388: .toComparison() == 0);
0389: o2 = "Zebra";
0390: assertTrue(new CompareToBuilder().append(o1, o2, null)
0391: .toComparison() < 0);
0392: assertTrue(new CompareToBuilder().append(o2, o1, null)
0393: .toComparison() > 0);
0394:
0395: assertTrue(new CompareToBuilder().append(o1, null, null)
0396: .toComparison() > 0);
0397: assertTrue(new CompareToBuilder().append((Object) null,
0398: (Object) null, null).toComparison() == 0);
0399: assertTrue(new CompareToBuilder().append(null, o1, null)
0400: .toComparison() < 0);
0401: }
0402:
0403: public void testLong() {
0404: long o1 = 1L;
0405: long o2 = 2L;
0406: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0407: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0);
0408: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0);
0409: assertTrue(new CompareToBuilder().append(o1, Long.MAX_VALUE)
0410: .toComparison() < 0);
0411: assertTrue(new CompareToBuilder().append(Long.MAX_VALUE, o1)
0412: .toComparison() > 0);
0413: assertTrue(new CompareToBuilder().append(o1, Long.MIN_VALUE)
0414: .toComparison() > 0);
0415: assertTrue(new CompareToBuilder().append(Long.MIN_VALUE, o1)
0416: .toComparison() < 0);
0417: }
0418:
0419: public void testInt() {
0420: int o1 = 1;
0421: int o2 = 2;
0422: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0423: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0);
0424: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0);
0425: assertTrue(new CompareToBuilder().append(o1, Integer.MAX_VALUE)
0426: .toComparison() < 0);
0427: assertTrue(new CompareToBuilder().append(Integer.MAX_VALUE, o1)
0428: .toComparison() > 0);
0429: assertTrue(new CompareToBuilder().append(o1, Integer.MIN_VALUE)
0430: .toComparison() > 0);
0431: assertTrue(new CompareToBuilder().append(Integer.MIN_VALUE, o1)
0432: .toComparison() < 0);
0433: }
0434:
0435: public void testShort() {
0436: short o1 = 1;
0437: short o2 = 2;
0438: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0439: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0);
0440: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0);
0441: assertTrue(new CompareToBuilder().append(o1, Short.MAX_VALUE)
0442: .toComparison() < 0);
0443: assertTrue(new CompareToBuilder().append(Short.MAX_VALUE, o1)
0444: .toComparison() > 0);
0445: assertTrue(new CompareToBuilder().append(o1, Short.MIN_VALUE)
0446: .toComparison() > 0);
0447: assertTrue(new CompareToBuilder().append(Short.MIN_VALUE, o1)
0448: .toComparison() < 0);
0449: }
0450:
0451: public void testChar() {
0452: char o1 = 1;
0453: char o2 = 2;
0454: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0455: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0);
0456: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0);
0457: assertTrue(new CompareToBuilder().append(o1,
0458: Character.MAX_VALUE).toComparison() < 0);
0459: assertTrue(new CompareToBuilder().append(Character.MAX_VALUE,
0460: o1).toComparison() > 0);
0461: assertTrue(new CompareToBuilder().append(o1,
0462: Character.MIN_VALUE).toComparison() > 0);
0463: assertTrue(new CompareToBuilder().append(Character.MIN_VALUE,
0464: o1).toComparison() < 0);
0465: }
0466:
0467: public void testByte() {
0468: byte o1 = 1;
0469: byte o2 = 2;
0470: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0471: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0);
0472: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0);
0473: assertTrue(new CompareToBuilder().append(o1, Byte.MAX_VALUE)
0474: .toComparison() < 0);
0475: assertTrue(new CompareToBuilder().append(Byte.MAX_VALUE, o1)
0476: .toComparison() > 0);
0477: assertTrue(new CompareToBuilder().append(o1, Byte.MIN_VALUE)
0478: .toComparison() > 0);
0479: assertTrue(new CompareToBuilder().append(Byte.MIN_VALUE, o1)
0480: .toComparison() < 0);
0481: }
0482:
0483: public void testDouble() {
0484: double o1 = 1;
0485: double o2 = 2;
0486: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0487: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0);
0488: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0);
0489: assertTrue(new CompareToBuilder().append(o1, Double.MAX_VALUE)
0490: .toComparison() < 0);
0491: assertTrue(new CompareToBuilder().append(Double.MAX_VALUE, o1)
0492: .toComparison() > 0);
0493: assertTrue(new CompareToBuilder().append(o1, Double.MIN_VALUE)
0494: .toComparison() > 0);
0495: assertTrue(new CompareToBuilder().append(Double.MIN_VALUE, o1)
0496: .toComparison() < 0);
0497: assertTrue(new CompareToBuilder()
0498: .append(Double.NaN, Double.NaN).toComparison() == 0);
0499: assertTrue(new CompareToBuilder().append(Double.NaN,
0500: Double.MAX_VALUE).toComparison() > 0);
0501: assertTrue(new CompareToBuilder().append(
0502: Double.POSITIVE_INFINITY, Double.MAX_VALUE)
0503: .toComparison() > 0);
0504: assertTrue(new CompareToBuilder().append(
0505: Double.NEGATIVE_INFINITY, Double.MIN_VALUE)
0506: .toComparison() < 0);
0507: assertTrue(new CompareToBuilder().append(o1, Double.NaN)
0508: .toComparison() < 0);
0509: assertTrue(new CompareToBuilder().append(Double.NaN, o1)
0510: .toComparison() > 0);
0511: assertTrue(new CompareToBuilder().append(-0.0, 0.0)
0512: .toComparison() < 0);
0513: assertTrue(new CompareToBuilder().append(0.0, -0.0)
0514: .toComparison() > 0);
0515: }
0516:
0517: public void testFloat() {
0518: float o1 = 1;
0519: float o2 = 2;
0520: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0521: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() < 0);
0522: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() > 0);
0523: assertTrue(new CompareToBuilder().append(o1, Float.MAX_VALUE)
0524: .toComparison() < 0);
0525: assertTrue(new CompareToBuilder().append(Float.MAX_VALUE, o1)
0526: .toComparison() > 0);
0527: assertTrue(new CompareToBuilder().append(o1, Float.MIN_VALUE)
0528: .toComparison() > 0);
0529: assertTrue(new CompareToBuilder().append(Float.MIN_VALUE, o1)
0530: .toComparison() < 0);
0531: assertTrue(new CompareToBuilder().append(Float.NaN, Float.NaN)
0532: .toComparison() == 0);
0533: assertTrue(new CompareToBuilder().append(Float.NaN,
0534: Float.MAX_VALUE).toComparison() > 0);
0535: assertTrue(new CompareToBuilder().append(
0536: Float.POSITIVE_INFINITY, Float.MAX_VALUE)
0537: .toComparison() > 0);
0538: assertTrue(new CompareToBuilder().append(
0539: Float.NEGATIVE_INFINITY, Float.MIN_VALUE)
0540: .toComparison() < 0);
0541: assertTrue(new CompareToBuilder().append(o1, Float.NaN)
0542: .toComparison() < 0);
0543: assertTrue(new CompareToBuilder().append(Float.NaN, o1)
0544: .toComparison() > 0);
0545: assertTrue(new CompareToBuilder().append(-0.0, 0.0)
0546: .toComparison() < 0);
0547: assertTrue(new CompareToBuilder().append(0.0, -0.0)
0548: .toComparison() > 0);
0549: }
0550:
0551: public void testBoolean() {
0552: boolean o1 = true;
0553: boolean o2 = false;
0554: assertTrue(new CompareToBuilder().append(o1, o1).toComparison() == 0);
0555: assertTrue(new CompareToBuilder().append(o2, o2).toComparison() == 0);
0556: assertTrue(new CompareToBuilder().append(o1, o2).toComparison() > 0);
0557: assertTrue(new CompareToBuilder().append(o2, o1).toComparison() < 0);
0558: }
0559:
0560: public void testObjectArray() {
0561: TestObject[] obj1 = new TestObject[2];
0562: obj1[0] = new TestObject(4);
0563: obj1[1] = new TestObject(5);
0564: TestObject[] obj2 = new TestObject[2];
0565: obj2[0] = new TestObject(4);
0566: obj2[1] = new TestObject(5);
0567: TestObject[] obj3 = new TestObject[3];
0568: obj3[0] = new TestObject(4);
0569: obj3[1] = new TestObject(5);
0570: obj3[2] = new TestObject(6);
0571:
0572: assertTrue(new CompareToBuilder().append(obj1, obj1)
0573: .toComparison() == 0);
0574: assertTrue(new CompareToBuilder().append(obj1, obj2)
0575: .toComparison() == 0);
0576: assertTrue(new CompareToBuilder().append(obj1, obj3)
0577: .toComparison() < 0);
0578: assertTrue(new CompareToBuilder().append(obj3, obj1)
0579: .toComparison() > 0);
0580:
0581: obj1[1] = new TestObject(7);
0582: assertTrue(new CompareToBuilder().append(obj1, obj2)
0583: .toComparison() > 0);
0584: assertTrue(new CompareToBuilder().append(obj2, obj1)
0585: .toComparison() < 0);
0586:
0587: assertTrue(new CompareToBuilder().append(obj1, null)
0588: .toComparison() > 0);
0589: assertTrue(new CompareToBuilder().append((Object[]) null,
0590: (Object[]) null).toComparison() == 0);
0591: assertTrue(new CompareToBuilder().append(null, obj1)
0592: .toComparison() < 0);
0593: }
0594:
0595: public void testLongArray() {
0596: long[] obj1 = new long[2];
0597: obj1[0] = 5L;
0598: obj1[1] = 6L;
0599: long[] obj2 = new long[2];
0600: obj2[0] = 5L;
0601: obj2[1] = 6L;
0602: long[] obj3 = new long[3];
0603: obj3[0] = 5L;
0604: obj3[1] = 6L;
0605: obj3[2] = 7L;
0606:
0607: assertTrue(new CompareToBuilder().append(obj1, obj1)
0608: .toComparison() == 0);
0609: assertTrue(new CompareToBuilder().append(obj1, obj2)
0610: .toComparison() == 0);
0611: assertTrue(new CompareToBuilder().append(obj1, obj3)
0612: .toComparison() < 0);
0613: assertTrue(new CompareToBuilder().append(obj3, obj1)
0614: .toComparison() > 0);
0615:
0616: obj1[1] = 7;
0617: assertTrue(new CompareToBuilder().append(obj1, obj2)
0618: .toComparison() > 0);
0619: assertTrue(new CompareToBuilder().append(obj2, obj1)
0620: .toComparison() < 0);
0621:
0622: assertTrue(new CompareToBuilder().append(obj1, null)
0623: .toComparison() > 0);
0624: assertTrue(new CompareToBuilder().append((long[]) null,
0625: (long[]) null).toComparison() == 0);
0626: assertTrue(new CompareToBuilder().append(null, obj1)
0627: .toComparison() < 0);
0628: }
0629:
0630: public void testIntArray() {
0631: int[] obj1 = new int[2];
0632: obj1[0] = 5;
0633: obj1[1] = 6;
0634: int[] obj2 = new int[2];
0635: obj2[0] = 5;
0636: obj2[1] = 6;
0637: int[] obj3 = new int[3];
0638: obj3[0] = 5;
0639: obj3[1] = 6;
0640: obj3[2] = 7;
0641:
0642: assertTrue(new CompareToBuilder().append(obj1, obj1)
0643: .toComparison() == 0);
0644: assertTrue(new CompareToBuilder().append(obj1, obj2)
0645: .toComparison() == 0);
0646: assertTrue(new CompareToBuilder().append(obj1, obj3)
0647: .toComparison() < 0);
0648: assertTrue(new CompareToBuilder().append(obj3, obj1)
0649: .toComparison() > 0);
0650:
0651: obj1[1] = 7;
0652: assertTrue(new CompareToBuilder().append(obj1, obj2)
0653: .toComparison() > 0);
0654: assertTrue(new CompareToBuilder().append(obj2, obj1)
0655: .toComparison() < 0);
0656:
0657: assertTrue(new CompareToBuilder().append(obj1, null)
0658: .toComparison() > 0);
0659: assertTrue(new CompareToBuilder().append((int[]) null,
0660: (int[]) null).toComparison() == 0);
0661: assertTrue(new CompareToBuilder().append(null, obj1)
0662: .toComparison() < 0);
0663: }
0664:
0665: public void testShortArray() {
0666: short[] obj1 = new short[2];
0667: obj1[0] = 5;
0668: obj1[1] = 6;
0669: short[] obj2 = new short[2];
0670: obj2[0] = 5;
0671: obj2[1] = 6;
0672: short[] obj3 = new short[3];
0673: obj3[0] = 5;
0674: obj3[1] = 6;
0675: obj3[2] = 7;
0676:
0677: assertTrue(new CompareToBuilder().append(obj1, obj1)
0678: .toComparison() == 0);
0679: assertTrue(new CompareToBuilder().append(obj1, obj2)
0680: .toComparison() == 0);
0681: assertTrue(new CompareToBuilder().append(obj1, obj3)
0682: .toComparison() < 0);
0683: assertTrue(new CompareToBuilder().append(obj3, obj1)
0684: .toComparison() > 0);
0685:
0686: obj1[1] = 7;
0687: assertTrue(new CompareToBuilder().append(obj1, obj2)
0688: .toComparison() > 0);
0689: assertTrue(new CompareToBuilder().append(obj2, obj1)
0690: .toComparison() < 0);
0691:
0692: assertTrue(new CompareToBuilder().append(obj1, null)
0693: .toComparison() > 0);
0694: assertTrue(new CompareToBuilder().append((short[]) null,
0695: (short[]) null).toComparison() == 0);
0696: assertTrue(new CompareToBuilder().append(null, obj1)
0697: .toComparison() < 0);
0698: }
0699:
0700: public void testCharArray() {
0701: char[] obj1 = new char[2];
0702: obj1[0] = 5;
0703: obj1[1] = 6;
0704: char[] obj2 = new char[2];
0705: obj2[0] = 5;
0706: obj2[1] = 6;
0707: char[] obj3 = new char[3];
0708: obj3[0] = 5;
0709: obj3[1] = 6;
0710: obj3[2] = 7;
0711:
0712: assertTrue(new CompareToBuilder().append(obj1, obj1)
0713: .toComparison() == 0);
0714: assertTrue(new CompareToBuilder().append(obj1, obj2)
0715: .toComparison() == 0);
0716: assertTrue(new CompareToBuilder().append(obj1, obj3)
0717: .toComparison() < 0);
0718: assertTrue(new CompareToBuilder().append(obj3, obj1)
0719: .toComparison() > 0);
0720:
0721: obj1[1] = 7;
0722: assertTrue(new CompareToBuilder().append(obj1, obj2)
0723: .toComparison() > 0);
0724: assertTrue(new CompareToBuilder().append(obj2, obj1)
0725: .toComparison() < 0);
0726:
0727: assertTrue(new CompareToBuilder().append(obj1, null)
0728: .toComparison() > 0);
0729: assertTrue(new CompareToBuilder().append((char[]) null,
0730: (char[]) null).toComparison() == 0);
0731: assertTrue(new CompareToBuilder().append(null, obj1)
0732: .toComparison() < 0);
0733: }
0734:
0735: public void testByteArray() {
0736: byte[] obj1 = new byte[2];
0737: obj1[0] = 5;
0738: obj1[1] = 6;
0739: byte[] obj2 = new byte[2];
0740: obj2[0] = 5;
0741: obj2[1] = 6;
0742: byte[] obj3 = new byte[3];
0743: obj3[0] = 5;
0744: obj3[1] = 6;
0745: obj3[2] = 7;
0746:
0747: assertTrue(new CompareToBuilder().append(obj1, obj1)
0748: .toComparison() == 0);
0749: assertTrue(new CompareToBuilder().append(obj1, obj2)
0750: .toComparison() == 0);
0751: assertTrue(new CompareToBuilder().append(obj1, obj3)
0752: .toComparison() < 0);
0753: assertTrue(new CompareToBuilder().append(obj3, obj1)
0754: .toComparison() > 0);
0755:
0756: obj1[1] = 7;
0757: assertTrue(new CompareToBuilder().append(obj1, obj2)
0758: .toComparison() > 0);
0759: assertTrue(new CompareToBuilder().append(obj2, obj1)
0760: .toComparison() < 0);
0761:
0762: assertTrue(new CompareToBuilder().append(obj1, null)
0763: .toComparison() > 0);
0764: assertTrue(new CompareToBuilder().append((byte[]) null,
0765: (byte[]) null).toComparison() == 0);
0766: assertTrue(new CompareToBuilder().append(null, obj1)
0767: .toComparison() < 0);
0768: }
0769:
0770: public void testDoubleArray() {
0771: double[] obj1 = new double[2];
0772: obj1[0] = 5;
0773: obj1[1] = 6;
0774: double[] obj2 = new double[2];
0775: obj2[0] = 5;
0776: obj2[1] = 6;
0777: double[] obj3 = new double[3];
0778: obj3[0] = 5;
0779: obj3[1] = 6;
0780: obj3[2] = 7;
0781:
0782: assertTrue(new CompareToBuilder().append(obj1, obj1)
0783: .toComparison() == 0);
0784: assertTrue(new CompareToBuilder().append(obj1, obj2)
0785: .toComparison() == 0);
0786: assertTrue(new CompareToBuilder().append(obj1, obj3)
0787: .toComparison() < 0);
0788: assertTrue(new CompareToBuilder().append(obj3, obj1)
0789: .toComparison() > 0);
0790:
0791: obj1[1] = 7;
0792: assertTrue(new CompareToBuilder().append(obj1, obj2)
0793: .toComparison() > 0);
0794: assertTrue(new CompareToBuilder().append(obj2, obj1)
0795: .toComparison() < 0);
0796:
0797: assertTrue(new CompareToBuilder().append(obj1, null)
0798: .toComparison() > 0);
0799: assertTrue(new CompareToBuilder().append((double[]) null,
0800: (double[]) null).toComparison() == 0);
0801: assertTrue(new CompareToBuilder().append(null, obj1)
0802: .toComparison() < 0);
0803: }
0804:
0805: public void testFloatArray() {
0806: float[] obj1 = new float[2];
0807: obj1[0] = 5;
0808: obj1[1] = 6;
0809: float[] obj2 = new float[2];
0810: obj2[0] = 5;
0811: obj2[1] = 6;
0812: float[] obj3 = new float[3];
0813: obj3[0] = 5;
0814: obj3[1] = 6;
0815: obj3[2] = 7;
0816:
0817: assertTrue(new CompareToBuilder().append(obj1, obj1)
0818: .toComparison() == 0);
0819: assertTrue(new CompareToBuilder().append(obj1, obj2)
0820: .toComparison() == 0);
0821: assertTrue(new CompareToBuilder().append(obj1, obj3)
0822: .toComparison() < 0);
0823: assertTrue(new CompareToBuilder().append(obj3, obj1)
0824: .toComparison() > 0);
0825:
0826: obj1[1] = 7;
0827: assertTrue(new CompareToBuilder().append(obj1, obj2)
0828: .toComparison() > 0);
0829: assertTrue(new CompareToBuilder().append(obj2, obj1)
0830: .toComparison() < 0);
0831:
0832: assertTrue(new CompareToBuilder().append(obj1, null)
0833: .toComparison() > 0);
0834: assertTrue(new CompareToBuilder().append((float[]) null,
0835: (float[]) null).toComparison() == 0);
0836: assertTrue(new CompareToBuilder().append(null, obj1)
0837: .toComparison() < 0);
0838: }
0839:
0840: public void testBooleanArray() {
0841: boolean[] obj1 = new boolean[2];
0842: obj1[0] = true;
0843: obj1[1] = false;
0844: boolean[] obj2 = new boolean[2];
0845: obj2[0] = true;
0846: obj2[1] = false;
0847: boolean[] obj3 = new boolean[3];
0848: obj3[0] = true;
0849: obj3[1] = false;
0850: obj3[2] = true;
0851:
0852: assertTrue(new CompareToBuilder().append(obj1, obj1)
0853: .toComparison() == 0);
0854: assertTrue(new CompareToBuilder().append(obj1, obj2)
0855: .toComparison() == 0);
0856: assertTrue(new CompareToBuilder().append(obj1, obj3)
0857: .toComparison() < 0);
0858: assertTrue(new CompareToBuilder().append(obj3, obj1)
0859: .toComparison() > 0);
0860:
0861: obj1[1] = true;
0862: assertTrue(new CompareToBuilder().append(obj1, obj2)
0863: .toComparison() > 0);
0864: assertTrue(new CompareToBuilder().append(obj2, obj1)
0865: .toComparison() < 0);
0866:
0867: assertTrue(new CompareToBuilder().append(obj1, null)
0868: .toComparison() > 0);
0869: assertTrue(new CompareToBuilder().append((boolean[]) null,
0870: (boolean[]) null).toComparison() == 0);
0871: assertTrue(new CompareToBuilder().append(null, obj1)
0872: .toComparison() < 0);
0873: }
0874:
0875: public void testMultiLongArray() {
0876: long[][] array1 = new long[2][2];
0877: long[][] array2 = new long[2][2];
0878: long[][] array3 = new long[2][3];
0879: for (int i = 0; i < array1.length; ++i) {
0880: for (int j = 0; j < array1[0].length; j++) {
0881: array1[i][j] = (i + 1) * (j + 1);
0882: array2[i][j] = (i + 1) * (j + 1);
0883: array3[i][j] = (i + 1) * (j + 1);
0884: }
0885: }
0886: array3[1][2] = 100;
0887: array3[1][2] = 100;
0888:
0889: assertTrue(new CompareToBuilder().append(array1, array1)
0890: .toComparison() == 0);
0891: assertTrue(new CompareToBuilder().append(array1, array2)
0892: .toComparison() == 0);
0893: assertTrue(new CompareToBuilder().append(array1, array3)
0894: .toComparison() < 0);
0895: assertTrue(new CompareToBuilder().append(array3, array1)
0896: .toComparison() > 0);
0897: array1[1][1] = 200;
0898: assertTrue(new CompareToBuilder().append(array1, array2)
0899: .toComparison() > 0);
0900: assertTrue(new CompareToBuilder().append(array2, array1)
0901: .toComparison() < 0);
0902: }
0903:
0904: public void testMultiIntArray() {
0905: int[][] array1 = new int[2][2];
0906: int[][] array2 = new int[2][2];
0907: int[][] array3 = new int[2][3];
0908: for (int i = 0; i < array1.length; ++i) {
0909: for (int j = 0; j < array1[0].length; j++) {
0910: array1[i][j] = (i + 1) * (j + 1);
0911: array2[i][j] = (i + 1) * (j + 1);
0912: array3[i][j] = (i + 1) * (j + 1);
0913: }
0914: }
0915: array3[1][2] = 100;
0916: array3[1][2] = 100;
0917:
0918: assertTrue(new CompareToBuilder().append(array1, array1)
0919: .toComparison() == 0);
0920: assertTrue(new CompareToBuilder().append(array1, array2)
0921: .toComparison() == 0);
0922: assertTrue(new CompareToBuilder().append(array1, array3)
0923: .toComparison() < 0);
0924: assertTrue(new CompareToBuilder().append(array3, array1)
0925: .toComparison() > 0);
0926: array1[1][1] = 200;
0927: assertTrue(new CompareToBuilder().append(array1, array2)
0928: .toComparison() > 0);
0929: assertTrue(new CompareToBuilder().append(array2, array1)
0930: .toComparison() < 0);
0931: }
0932:
0933: public void testMultiShortArray() {
0934: short[][] array1 = new short[2][2];
0935: short[][] array2 = new short[2][2];
0936: short[][] array3 = new short[2][3];
0937: for (short i = 0; i < array1.length; ++i) {
0938: for (short j = 0; j < array1[0].length; j++) {
0939: array1[i][j] = (short) ((i + 1) * (j + 1));
0940: array2[i][j] = (short) ((i + 1) * (j + 1));
0941: array3[i][j] = (short) ((i + 1) * (j + 1));
0942: }
0943: }
0944: array3[1][2] = 100;
0945: array3[1][2] = 100;
0946:
0947: assertTrue(new CompareToBuilder().append(array1, array1)
0948: .toComparison() == 0);
0949: assertTrue(new CompareToBuilder().append(array1, array2)
0950: .toComparison() == 0);
0951: assertTrue(new CompareToBuilder().append(array1, array3)
0952: .toComparison() < 0);
0953: assertTrue(new CompareToBuilder().append(array3, array1)
0954: .toComparison() > 0);
0955: array1[1][1] = 200;
0956: assertTrue(new CompareToBuilder().append(array1, array2)
0957: .toComparison() > 0);
0958: assertTrue(new CompareToBuilder().append(array2, array1)
0959: .toComparison() < 0);
0960: }
0961:
0962: public void testMultiCharArray() {
0963: char[][] array1 = new char[2][2];
0964: char[][] array2 = new char[2][2];
0965: char[][] array3 = new char[2][3];
0966: for (short i = 0; i < array1.length; ++i) {
0967: for (short j = 0; j < array1[0].length; j++) {
0968: array1[i][j] = (char) ((i + 1) * (j + 1));
0969: array2[i][j] = (char) ((i + 1) * (j + 1));
0970: array3[i][j] = (char) ((i + 1) * (j + 1));
0971: }
0972: }
0973: array3[1][2] = 100;
0974: array3[1][2] = 100;
0975:
0976: assertTrue(new CompareToBuilder().append(array1, array1)
0977: .toComparison() == 0);
0978: assertTrue(new CompareToBuilder().append(array1, array2)
0979: .toComparison() == 0);
0980: assertTrue(new CompareToBuilder().append(array1, array3)
0981: .toComparison() < 0);
0982: assertTrue(new CompareToBuilder().append(array3, array1)
0983: .toComparison() > 0);
0984: array1[1][1] = 200;
0985: assertTrue(new CompareToBuilder().append(array1, array2)
0986: .toComparison() > 0);
0987: assertTrue(new CompareToBuilder().append(array2, array1)
0988: .toComparison() < 0);
0989: }
0990:
0991: public void testMultiByteArray() {
0992: byte[][] array1 = new byte[2][2];
0993: byte[][] array2 = new byte[2][2];
0994: byte[][] array3 = new byte[2][3];
0995: for (byte i = 0; i < array1.length; ++i) {
0996: for (byte j = 0; j < array1[0].length; j++) {
0997: array1[i][j] = (byte) ((i + 1) * (j + 1));
0998: array2[i][j] = (byte) ((i + 1) * (j + 1));
0999: array3[i][j] = (byte) ((i + 1) * (j + 1));
1000: }
1001: }
1002: array3[1][2] = 100;
1003: array3[1][2] = 100;
1004:
1005: assertTrue(new CompareToBuilder().append(array1, array1)
1006: .toComparison() == 0);
1007: assertTrue(new CompareToBuilder().append(array1, array2)
1008: .toComparison() == 0);
1009: assertTrue(new CompareToBuilder().append(array1, array3)
1010: .toComparison() < 0);
1011: assertTrue(new CompareToBuilder().append(array3, array1)
1012: .toComparison() > 0);
1013: array1[1][1] = 127;
1014: assertTrue(new CompareToBuilder().append(array1, array2)
1015: .toComparison() > 0);
1016: assertTrue(new CompareToBuilder().append(array2, array1)
1017: .toComparison() < 0);
1018: }
1019:
1020: public void testMultiFloatArray() {
1021: float[][] array1 = new float[2][2];
1022: float[][] array2 = new float[2][2];
1023: float[][] array3 = new float[2][3];
1024: for (int i = 0; i < array1.length; ++i) {
1025: for (int j = 0; j < array1[0].length; j++) {
1026: array1[i][j] = ((i + 1) * (j + 1));
1027: array2[i][j] = ((i + 1) * (j + 1));
1028: array3[i][j] = ((i + 1) * (j + 1));
1029: }
1030: }
1031: array3[1][2] = 100;
1032: array3[1][2] = 100;
1033:
1034: assertTrue(new CompareToBuilder().append(array1, array1)
1035: .toComparison() == 0);
1036: assertTrue(new CompareToBuilder().append(array1, array2)
1037: .toComparison() == 0);
1038: assertTrue(new CompareToBuilder().append(array1, array3)
1039: .toComparison() < 0);
1040: assertTrue(new CompareToBuilder().append(array3, array1)
1041: .toComparison() > 0);
1042: array1[1][1] = 127;
1043: assertTrue(new CompareToBuilder().append(array1, array2)
1044: .toComparison() > 0);
1045: assertTrue(new CompareToBuilder().append(array2, array1)
1046: .toComparison() < 0);
1047: }
1048:
1049: public void testMultiDoubleArray() {
1050: double[][] array1 = new double[2][2];
1051: double[][] array2 = new double[2][2];
1052: double[][] array3 = new double[2][3];
1053: for (int i = 0; i < array1.length; ++i) {
1054: for (int j = 0; j < array1[0].length; j++) {
1055: array1[i][j] = ((i + 1) * (j + 1));
1056: array2[i][j] = ((i + 1) * (j + 1));
1057: array3[i][j] = ((i + 1) * (j + 1));
1058: }
1059: }
1060: array3[1][2] = 100;
1061: array3[1][2] = 100;
1062:
1063: assertTrue(new CompareToBuilder().append(array1, array1)
1064: .toComparison() == 0);
1065: assertTrue(new CompareToBuilder().append(array1, array2)
1066: .toComparison() == 0);
1067: assertTrue(new CompareToBuilder().append(array1, array3)
1068: .toComparison() < 0);
1069: assertTrue(new CompareToBuilder().append(array3, array1)
1070: .toComparison() > 0);
1071: array1[1][1] = 127;
1072: assertTrue(new CompareToBuilder().append(array1, array2)
1073: .toComparison() > 0);
1074: assertTrue(new CompareToBuilder().append(array2, array1)
1075: .toComparison() < 0);
1076: }
1077:
1078: public void testMultiBooleanArray() {
1079: boolean[][] array1 = new boolean[2][2];
1080: boolean[][] array2 = new boolean[2][2];
1081: boolean[][] array3 = new boolean[2][3];
1082: for (int i = 0; i < array1.length; ++i) {
1083: for (int j = 0; j < array1[0].length; j++) {
1084: array1[i][j] = ((i == 1) ^ (j == 1));
1085: array2[i][j] = ((i == 1) ^ (j == 1));
1086: array3[i][j] = ((i == 1) ^ (j == 1));
1087: }
1088: }
1089: array3[1][2] = false;
1090: array3[1][2] = false;
1091:
1092: assertTrue(new CompareToBuilder().append(array1, array1)
1093: .toComparison() == 0);
1094: assertTrue(new CompareToBuilder().append(array1, array2)
1095: .toComparison() == 0);
1096: assertTrue(new CompareToBuilder().append(array1, array3)
1097: .toComparison() < 0);
1098: assertTrue(new CompareToBuilder().append(array3, array1)
1099: .toComparison() > 0);
1100: array1[1][1] = true;
1101: assertTrue(new CompareToBuilder().append(array1, array2)
1102: .toComparison() > 0);
1103: assertTrue(new CompareToBuilder().append(array2, array1)
1104: .toComparison() < 0);
1105: }
1106:
1107: public void testRaggedArray() {
1108: long array1[][] = new long[2][];
1109: long array2[][] = new long[2][];
1110: long array3[][] = new long[3][];
1111: for (int i = 0; i < array1.length; ++i) {
1112: array1[i] = new long[2];
1113: array2[i] = new long[2];
1114: array3[i] = new long[3];
1115: for (int j = 0; j < array1[i].length; ++j) {
1116: array1[i][j] = (i + 1) * (j + 1);
1117: array2[i][j] = (i + 1) * (j + 1);
1118: array3[i][j] = (i + 1) * (j + 1);
1119: }
1120: }
1121: array3[1][2] = 100;
1122: array3[1][2] = 100;
1123:
1124: assertTrue(new CompareToBuilder().append(array1, array1)
1125: .toComparison() == 0);
1126: assertTrue(new CompareToBuilder().append(array1, array2)
1127: .toComparison() == 0);
1128: assertTrue(new CompareToBuilder().append(array1, array3)
1129: .toComparison() < 0);
1130: assertTrue(new CompareToBuilder().append(array3, array1)
1131: .toComparison() > 0);
1132: array1[1][1] = 200;
1133: assertTrue(new CompareToBuilder().append(array1, array2)
1134: .toComparison() > 0);
1135: assertTrue(new CompareToBuilder().append(array2, array1)
1136: .toComparison() < 0);
1137: }
1138:
1139: public void testMixedArray() {
1140: Object array1[] = new Object[2];
1141: Object array2[] = new Object[2];
1142: Object array3[] = new Object[2];
1143: for (int i = 0; i < array1.length; ++i) {
1144: array1[i] = new long[2];
1145: array2[i] = new long[2];
1146: array3[i] = new long[3];
1147: for (int j = 0; j < 2; ++j) {
1148: ((long[]) array1[i])[j] = (i + 1) * (j + 1);
1149: ((long[]) array2[i])[j] = (i + 1) * (j + 1);
1150: ((long[]) array3[i])[j] = (i + 1) * (j + 1);
1151: }
1152: }
1153: ((long[]) array3[0])[2] = 1;
1154: ((long[]) array3[1])[2] = 1;
1155: assertTrue(new CompareToBuilder().append(array1, array1)
1156: .toComparison() == 0);
1157: assertTrue(new CompareToBuilder().append(array1, array2)
1158: .toComparison() == 0);
1159: assertTrue(new CompareToBuilder().append(array1, array3)
1160: .toComparison() < 0);
1161: assertTrue(new CompareToBuilder().append(array3, array1)
1162: .toComparison() > 0);
1163: ((long[]) array1[1])[1] = 200;
1164: assertTrue(new CompareToBuilder().append(array1, array2)
1165: .toComparison() > 0);
1166: assertTrue(new CompareToBuilder().append(array2, array1)
1167: .toComparison() < 0);
1168: }
1169:
1170: public void testObjectArrayHiddenByObject() {
1171: TestObject[] array1 = new TestObject[2];
1172: array1[0] = new TestObject(4);
1173: array1[1] = new TestObject(5);
1174: TestObject[] array2 = new TestObject[2];
1175: array2[0] = new TestObject(4);
1176: array2[1] = new TestObject(5);
1177: TestObject[] array3 = new TestObject[3];
1178: array3[0] = new TestObject(4);
1179: array3[1] = new TestObject(5);
1180: array3[2] = new TestObject(6);
1181:
1182: Object obj1 = array1;
1183: Object obj2 = array2;
1184: Object obj3 = array3;
1185:
1186: assertTrue(new CompareToBuilder().append(obj1, obj1)
1187: .toComparison() == 0);
1188: assertTrue(new CompareToBuilder().append(obj1, obj2)
1189: .toComparison() == 0);
1190: assertTrue(new CompareToBuilder().append(obj1, obj3)
1191: .toComparison() < 0);
1192: assertTrue(new CompareToBuilder().append(obj3, obj1)
1193: .toComparison() > 0);
1194:
1195: array1[1] = new TestObject(7);
1196: assertTrue(new CompareToBuilder().append(obj1, obj2)
1197: .toComparison() > 0);
1198: assertTrue(new CompareToBuilder().append(obj2, obj1)
1199: .toComparison() < 0);
1200: }
1201:
1202: public void testLongArrayHiddenByObject() {
1203: long[] array1 = new long[2];
1204: array1[0] = 5L;
1205: array1[1] = 6L;
1206: long[] array2 = new long[2];
1207: array2[0] = 5L;
1208: array2[1] = 6L;
1209: long[] array3 = new long[3];
1210: array3[0] = 5L;
1211: array3[1] = 6L;
1212: array3[2] = 7L;
1213: Object obj1 = array1;
1214: Object obj2 = array2;
1215: Object obj3 = array3;
1216: assertTrue(new CompareToBuilder().append(obj1, obj1)
1217: .toComparison() == 0);
1218: assertTrue(new CompareToBuilder().append(obj1, obj2)
1219: .toComparison() == 0);
1220: assertTrue(new CompareToBuilder().append(obj1, obj3)
1221: .toComparison() < 0);
1222: assertTrue(new CompareToBuilder().append(obj3, obj1)
1223: .toComparison() > 0);
1224:
1225: array1[1] = 7;
1226: assertTrue(new CompareToBuilder().append(obj1, obj2)
1227: .toComparison() > 0);
1228: assertTrue(new CompareToBuilder().append(obj2, obj1)
1229: .toComparison() < 0);
1230: }
1231:
1232: public void testIntArrayHiddenByObject() {
1233: int[] array1 = new int[2];
1234: array1[0] = 5;
1235: array1[1] = 6;
1236: int[] array2 = new int[2];
1237: array2[0] = 5;
1238: array2[1] = 6;
1239: int[] array3 = new int[3];
1240: array3[0] = 5;
1241: array3[1] = 6;
1242: array3[2] = 7;
1243: Object obj1 = array1;
1244: Object obj2 = array2;
1245: Object obj3 = array3;
1246: assertTrue(new CompareToBuilder().append(obj1, obj1)
1247: .toComparison() == 0);
1248: assertTrue(new CompareToBuilder().append(obj1, obj2)
1249: .toComparison() == 0);
1250: assertTrue(new CompareToBuilder().append(obj1, obj3)
1251: .toComparison() < 0);
1252: assertTrue(new CompareToBuilder().append(obj3, obj1)
1253: .toComparison() > 0);
1254:
1255: array1[1] = 7;
1256: assertTrue(new CompareToBuilder().append(obj1, obj2)
1257: .toComparison() > 0);
1258: assertTrue(new CompareToBuilder().append(obj2, obj1)
1259: .toComparison() < 0);
1260: }
1261:
1262: public void testShortArrayHiddenByObject() {
1263: short[] array1 = new short[2];
1264: array1[0] = 5;
1265: array1[1] = 6;
1266: short[] array2 = new short[2];
1267: array2[0] = 5;
1268: array2[1] = 6;
1269: short[] array3 = new short[3];
1270: array3[0] = 5;
1271: array3[1] = 6;
1272: array3[2] = 7;
1273: Object obj1 = array1;
1274: Object obj2 = array2;
1275: Object obj3 = array3;
1276: assertTrue(new CompareToBuilder().append(obj1, obj1)
1277: .toComparison() == 0);
1278: assertTrue(new CompareToBuilder().append(obj1, obj2)
1279: .toComparison() == 0);
1280: assertTrue(new CompareToBuilder().append(obj1, obj3)
1281: .toComparison() < 0);
1282: assertTrue(new CompareToBuilder().append(obj3, obj1)
1283: .toComparison() > 0);
1284:
1285: array1[1] = 7;
1286: assertTrue(new CompareToBuilder().append(obj1, obj2)
1287: .toComparison() > 0);
1288: assertTrue(new CompareToBuilder().append(obj2, obj1)
1289: .toComparison() < 0);
1290: }
1291:
1292: public void testCharArrayHiddenByObject() {
1293: char[] array1 = new char[2];
1294: array1[0] = 5;
1295: array1[1] = 6;
1296: char[] array2 = new char[2];
1297: array2[0] = 5;
1298: array2[1] = 6;
1299: char[] array3 = new char[3];
1300: array3[0] = 5;
1301: array3[1] = 6;
1302: array3[2] = 7;
1303: Object obj1 = array1;
1304: Object obj2 = array2;
1305: Object obj3 = array3;
1306: assertTrue(new CompareToBuilder().append(obj1, obj1)
1307: .toComparison() == 0);
1308: assertTrue(new CompareToBuilder().append(obj1, obj2)
1309: .toComparison() == 0);
1310: assertTrue(new CompareToBuilder().append(obj1, obj3)
1311: .toComparison() < 0);
1312: assertTrue(new CompareToBuilder().append(obj3, obj1)
1313: .toComparison() > 0);
1314:
1315: array1[1] = 7;
1316: assertTrue(new CompareToBuilder().append(obj1, obj2)
1317: .toComparison() > 0);
1318: assertTrue(new CompareToBuilder().append(obj2, obj1)
1319: .toComparison() < 0);
1320: }
1321:
1322: public void testByteArrayHiddenByObject() {
1323: byte[] array1 = new byte[2];
1324: array1[0] = 5;
1325: array1[1] = 6;
1326: byte[] array2 = new byte[2];
1327: array2[0] = 5;
1328: array2[1] = 6;
1329: byte[] array3 = new byte[3];
1330: array3[0] = 5;
1331: array3[1] = 6;
1332: array3[2] = 7;
1333: Object obj1 = array1;
1334: Object obj2 = array2;
1335: Object obj3 = array3;
1336: assertTrue(new CompareToBuilder().append(obj1, obj1)
1337: .toComparison() == 0);
1338: assertTrue(new CompareToBuilder().append(obj1, obj2)
1339: .toComparison() == 0);
1340: assertTrue(new CompareToBuilder().append(obj1, obj3)
1341: .toComparison() < 0);
1342: assertTrue(new CompareToBuilder().append(obj3, obj1)
1343: .toComparison() > 0);
1344:
1345: array1[1] = 7;
1346: assertTrue(new CompareToBuilder().append(obj1, obj2)
1347: .toComparison() > 0);
1348: assertTrue(new CompareToBuilder().append(obj2, obj1)
1349: .toComparison() < 0);
1350: }
1351:
1352: public void testDoubleArrayHiddenByObject() {
1353: double[] array1 = new double[2];
1354: array1[0] = 5;
1355: array1[1] = 6;
1356: double[] array2 = new double[2];
1357: array2[0] = 5;
1358: array2[1] = 6;
1359: double[] array3 = new double[3];
1360: array3[0] = 5;
1361: array3[1] = 6;
1362: array3[2] = 7;
1363: Object obj1 = array1;
1364: Object obj2 = array2;
1365: Object obj3 = array3;
1366: assertTrue(new CompareToBuilder().append(obj1, obj1)
1367: .toComparison() == 0);
1368: assertTrue(new CompareToBuilder().append(obj1, obj2)
1369: .toComparison() == 0);
1370: assertTrue(new CompareToBuilder().append(obj1, obj3)
1371: .toComparison() < 0);
1372: assertTrue(new CompareToBuilder().append(obj3, obj1)
1373: .toComparison() > 0);
1374:
1375: array1[1] = 7;
1376: assertTrue(new CompareToBuilder().append(obj1, obj2)
1377: .toComparison() > 0);
1378: assertTrue(new CompareToBuilder().append(obj2, obj1)
1379: .toComparison() < 0);
1380: }
1381:
1382: public void testFloatArrayHiddenByObject() {
1383: float[] array1 = new float[2];
1384: array1[0] = 5;
1385: array1[1] = 6;
1386: float[] array2 = new float[2];
1387: array2[0] = 5;
1388: array2[1] = 6;
1389: float[] array3 = new float[3];
1390: array3[0] = 5;
1391: array3[1] = 6;
1392: array3[2] = 7;
1393: Object obj1 = array1;
1394: Object obj2 = array2;
1395: Object obj3 = array3;
1396: assertTrue(new CompareToBuilder().append(obj1, obj1)
1397: .toComparison() == 0);
1398: assertTrue(new CompareToBuilder().append(obj1, obj2)
1399: .toComparison() == 0);
1400: assertTrue(new CompareToBuilder().append(obj1, obj3)
1401: .toComparison() < 0);
1402: assertTrue(new CompareToBuilder().append(obj3, obj1)
1403: .toComparison() > 0);
1404:
1405: array1[1] = 7;
1406: assertTrue(new CompareToBuilder().append(obj1, obj2)
1407: .toComparison() > 0);
1408: assertTrue(new CompareToBuilder().append(obj2, obj1)
1409: .toComparison() < 0);
1410: }
1411:
1412: public void testBooleanArrayHiddenByObject() {
1413: boolean[] array1 = new boolean[2];
1414: array1[0] = true;
1415: array1[1] = false;
1416: boolean[] array2 = new boolean[2];
1417: array2[0] = true;
1418: array2[1] = false;
1419: boolean[] array3 = new boolean[3];
1420: array3[0] = true;
1421: array3[1] = false;
1422: array3[2] = true;
1423: Object obj1 = array1;
1424: Object obj2 = array2;
1425: Object obj3 = array3;
1426: assertTrue(new CompareToBuilder().append(obj1, obj1)
1427: .toComparison() == 0);
1428: assertTrue(new CompareToBuilder().append(obj1, obj2)
1429: .toComparison() == 0);
1430: assertTrue(new CompareToBuilder().append(obj1, obj3)
1431: .toComparison() < 0);
1432: assertTrue(new CompareToBuilder().append(obj3, obj1)
1433: .toComparison() > 0);
1434:
1435: array1[1] = true;
1436: assertTrue(new CompareToBuilder().append(obj1, obj2)
1437: .toComparison() > 0);
1438: assertTrue(new CompareToBuilder().append(obj2, obj1)
1439: .toComparison() < 0);
1440: }
1441:
1442: }
|