BeanCompare { public List<BeanDifference<?>> compare(Bean bean1, Bean bean2) { ArgumentChecker.notNull(bean1, "bean1"); ArgumentChecker.notNull(bean2, "bean2"); if (!sameClass(bean1, bean2)) { throw new IllegalArgumentException("Beans must be of the same class. bean1 class: " + bean1.getClass() + ", bean2 class: " + bean2.getClass()); } return compare(bean1, bean2, Collections.<MetaProperty<?>>emptyList()); } BeanCompare();  BeanCompare(
      Map<MetaProperty<?>, Comparator<Object>> propertyComparators,
      Map<Class<?>, Comparator<Object>> typeComparators); @Deprecated static boolean equalIgnoring(Bean bean1, Bean bean2, MetaProperty<?>... properties); List<BeanDifference<?>> compare(Bean bean1, Bean bean2);  }
@Test public void propertyComparators_same() { UniqueId uid1 = UniqueId.of("uid", "123"); UniqueId uid2 = UniqueId.of("uid", "123"); ExternalIdBundle eid1 = ExternalIdBundle.of(ExternalId.of("eid1", "321")); ExternalIdBundle eid2 = ExternalIdBundle.of(ExternalId.of("eid1", "321")); Bean bean1 = createBean(uid1, eid1, "name1"); Bean bean2 = createBean(uid2, eid2, "name1"); BeanCompare beanCompare = new BeanCompare(); List<BeanDifference<?>> diff = beanCompare.compare(bean1, bean2); assertTrue(diff.isEmpty()); }
@Test public void propertyComparators_different() { UniqueId uid1 = UniqueId.of("uid", "123"); UniqueId uid2 = UniqueId.of("uid", "123"); ExternalIdBundle eid1 = ExternalIdBundle.of(ExternalId.of("eid1", "321")); ExternalIdBundle eid2 = ExternalIdBundle.of(ExternalId.of("eid2", "abc")); Bean bean1 = createBean(uid1, eid1, "name1"); Bean bean2 = createBean(uid2, eid2, "name1"); BeanCompare beanCompare = new BeanCompare(); List<BeanDifference<?>> diff = beanCompare.compare(bean1, bean2); assertFalse(diff.isEmpty()); }
@Test public void propertyComparators_ignoreDifferences() { Comparator<Object> alwaysEqualComparator = new Comparator<Object>() { @Override public int compare(Object notUsed1, Object notUsed2) { return 0; } }; UniqueId uid1 = UniqueId.of("uid", "123"); UniqueId uid2 = UniqueId.of("uid", "321"); ExternalIdBundle eid1 = ExternalIdBundle.of(ExternalId.of("eid1", "321")); ExternalIdBundle eid2 = ExternalIdBundle.of(ExternalId.of("eid2", "abc")); Bean bean1 = createBean(uid1, eid1, "name1"); Bean bean2 = createBean(uid2, eid2, "name1"); MetaProperty<Object> uniqueIdMeta = bean1.property(UNIQUE_ID).metaProperty(); MetaProperty<Object> externalIdMeta = bean1.property(EXTERNAL_ID_BUNDLE).metaProperty(); Map<MetaProperty<?>, Comparator<Object>> comparators = ImmutableMap.<MetaProperty<?>, Comparator<Object>>of( uniqueIdMeta, alwaysEqualComparator, externalIdMeta, alwaysEqualComparator); BeanCompare beanCompare = new BeanCompare(comparators, Collections.<Class<?>, Comparator<Object>>emptyMap()); List<BeanDifference<?>> diff = beanCompare.compare(bean1, bean2); assertTrue(diff.toString(), diff.isEmpty()); }