/**
   * See "Effective Java" under "Consider Implementing Comparable".
   *
   * @param x an object to compare
   * @param y an object to compare
   * @param z an object to compare
   * @param testTransients Whether to include transients in the comparison
   * @param excludeFields fields to exclude
   */
  private void assertReflectionCompareContract(
      final Object x,
      final Object y,
      final Object z,
      final boolean testTransients,
      final String[] excludeFields) {

    // signum
    assertTrue(
        reflectionCompareSignum(x, y, testTransients, excludeFields)
            == -reflectionCompareSignum(y, x, testTransients, excludeFields));

    // transitive
    if (CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields) > 0
        && CompareToBuilder.reflectionCompare(y, z, testTransients, null, excludeFields) > 0) {
      assertTrue(CompareToBuilder.reflectionCompare(x, z, testTransients, null, excludeFields) > 0);
    }

    // un-named
    if (CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields) == 0) {
      assertTrue(
          reflectionCompareSignum(x, z, testTransients, excludeFields)
              == -reflectionCompareSignum(y, z, testTransients, excludeFields));
    }

    // strongly recommended but not strictly required
    assertTrue(
        CompareToBuilder.reflectionCompare(x, y, testTransients)
            == 0
            == EqualsBuilder.reflectionEquals(x, y, testTransients));
  }
  private void testReflectionHierarchyCompare(
      final boolean testTransients, final String[] excludeFields) {
    final TestObject to1 = new TestObject(1);
    final TestObject to2 = new TestObject(2);
    final TestObject to3 = new TestObject(3);
    final TestSubObject tso1 = new TestSubObject(1, 1);
    final TestSubObject tso2 = new TestSubObject(2, 2);
    final TestSubObject tso3 = new TestSubObject(3, 3);

    assertReflectionCompareContract(to1, to1, to1, false, excludeFields);
    assertReflectionCompareContract(to1, to2, to3, false, excludeFields);
    assertReflectionCompareContract(tso1, tso1, tso1, false, excludeFields);
    assertReflectionCompareContract(tso1, tso2, tso3, false, excludeFields);
    assertReflectionCompareContract("1", "2", "3", false, excludeFields);

    assertTrue(
        0 != CompareToBuilder.reflectionCompare(tso1, new TestSubObject(1, 0), testTransients));
    assertTrue(
        0 != CompareToBuilder.reflectionCompare(tso1, new TestSubObject(0, 1), testTransients));

    // root class
    assertXYZCompareOrder(to1, to2, to3, true, null);
    // subclass
    assertXYZCompareOrder(tso1, tso2, tso3, true, null);
  }
 @Test
 public void testReflectionCompare() {
   final TestObject o1 = new TestObject(4);
   final TestObject o2 = new TestObject(4);
   assertTrue(CompareToBuilder.reflectionCompare(o1, o1) == 0);
   assertTrue(CompareToBuilder.reflectionCompare(o1, o2) == 0);
   o2.setA(5);
   assertTrue(CompareToBuilder.reflectionCompare(o1, o2) < 0);
   assertTrue(CompareToBuilder.reflectionCompare(o2, o1) > 0);
 }
 /**
  * Returns the signum of the result of comparing x and y with <code>
  * CompareToBuilder.reflectionCompare</code>
  *
  * @param lhs The "left-hand-side" of the comparison.
  * @param rhs The "right-hand-side" of the comparison.
  * @param testTransients Whether to include transients in the comparison
  * @param excludeFields fields to exclude
  * @return int The signum
  */
 private int reflectionCompareSignum(
     final Object lhs,
     final Object rhs,
     final boolean testTransients,
     final String[] excludeFields) {
   return BigInteger.valueOf(CompareToBuilder.reflectionCompare(lhs, rhs, testTransients))
       .signum();
 }
  private void assertXYZCompareOrder(
      final Object x,
      final Object y,
      final Object z,
      final boolean testTransients,
      final String[] excludeFields) {
    assertTrue(0 == CompareToBuilder.reflectionCompare(x, x, testTransients, null, excludeFields));
    assertTrue(0 == CompareToBuilder.reflectionCompare(y, y, testTransients, null, excludeFields));
    assertTrue(0 == CompareToBuilder.reflectionCompare(z, z, testTransients, null, excludeFields));

    assertTrue(0 > CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields));
    assertTrue(0 > CompareToBuilder.reflectionCompare(x, z, testTransients, null, excludeFields));
    assertTrue(0 > CompareToBuilder.reflectionCompare(y, z, testTransients, null, excludeFields));

    assertTrue(0 < CompareToBuilder.reflectionCompare(y, x, testTransients, null, excludeFields));
    assertTrue(0 < CompareToBuilder.reflectionCompare(z, x, testTransients, null, excludeFields));
    assertTrue(0 < CompareToBuilder.reflectionCompare(z, y, testTransients, null, excludeFields));
  }
Exemplo n.º 6
0
 public int compareTo(Object o) {
   return CompareToBuilder.reflectionCompare(this, o);
 }
 @Test(expected = ClassCastException.class)
 public void testReflectionCompareEx2() {
   final TestObject o1 = new TestObject(4);
   final Object o2 = new Object();
   CompareToBuilder.reflectionCompare(o1, o2);
 }
 @Test(expected = NullPointerException.class)
 public void testReflectionCompareEx1() {
   final TestObject o1 = new TestObject(4);
   CompareToBuilder.reflectionCompare(o1, null);
 }