BeanPropertyComparator implements Comparator, Serializable { public int compare(Object o1, Object o2) { int compared = 0; try { for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) { String currentProperty = i.next().toString(); Comparator currentComparator = null; try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty); Class propertyClass = propertyDescriptor.getPropertyType(); if (propertyClass.equals(String.class)) { currentComparator = this.stringComparator; } else if (TypeUtils.isBooleanClass(propertyClass)) { currentComparator = this.booleanComparator; } else { currentComparator = this.genericComparator; } } catch (NullPointerException e) { throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e); } Object value1 = PropertyUtils.getProperty(o1, currentProperty); Object value2 = PropertyUtils.getProperty(o2, currentProperty); if ( value1 == null && value2 == null) return 0; else if ( value1 == null) return -1; else if ( value2 == null ) return 1; compared = currentComparator.compare(value1, value2); } } catch (IllegalAccessException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (NoSuchMethodException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (InvocationTargetException e) { throw new BeanComparisonException("unable to compare property values", e); } return compared; } BeanPropertyComparator(List propertyNames);  BeanPropertyComparator(List propertyNames, boolean ignoreCase); int compare(Object o1, Object o2);  }
@Test public void testCompare_unknownPropertyNames() { List unknownProperties = Arrays.asList(new String[] { "one", "two", "three" }); BeanPropertyComparator bpc = new BeanPropertyComparator(unknownProperties); A a = new A("something", new Integer(0), Boolean.valueOf(false)); B b = new B("something else", new Integer(1), Boolean.valueOf(true)); boolean failedAsExpected = false; try { bpc.compare(a, b); } catch (BeanComparisonException e) { if (e.getCause() instanceof NullPointerException) { failedAsExpected = true; } } Assert.assertTrue(failedAsExpected); }
@Test public void testCompare_propertyTypeMismatch() { List mismatchedProperties = Arrays.asList(new String[] { "i", "b" }); BeanPropertyComparator bpc = new BeanPropertyComparator(mismatchedProperties); A a = new A("something", new Integer(0), Boolean.valueOf(false)); C c = new C("something else", 1, true); boolean failedAsExpected = false; try { bpc.compare(a, c); } catch (ClassCastException e) { failedAsExpected = true; } Assert.assertTrue(failedAsExpected); }
@Test public void testCompare_privateProperty() { List privateProperty = Arrays.asList(new String[] { "s" }); BeanPropertyComparator bpc = new BeanPropertyComparator(privateProperty); C c = new C("something else", 1, true); A a = new A("something", new Integer(0), Boolean.valueOf(false)); boolean failedAsExpected = false; try { bpc.compare(c, a); } catch (BeanComparisonException e) { if (e.getCause() instanceof NullPointerException) { failedAsExpected = true; } } Assert.assertTrue(failedAsExpected); }
@Test public void testCompare_oneProperty_string() { List properties = Arrays.asList(new String[] { "s" }); BeanPropertyComparator bpc = new BeanPropertyComparator(properties); A lesser = new A("One", new Integer(0), Boolean.valueOf(false)); B greater = new B("Two", new Integer(0), Boolean.valueOf(false)); int lessThan = bpc.compare(lesser, greater); Assert.assertTrue(lessThan < 0); int greaterThan = bpc.compare(greater, lesser); Assert.assertTrue(greaterThan > 0); int equal = bpc.compare(greater, greater); Assert.assertTrue(equal == 0); }
@Test public void testCompare_oneProperty_integer() { List properties = Arrays.asList(new String[] { "i" }); BeanPropertyComparator bpc = new BeanPropertyComparator(properties); A lesser = new A("One", new Integer(-1), Boolean.valueOf(false)); B greater = new B("One", new Integer(1), Boolean.valueOf(false)); int lessThan = bpc.compare(lesser, greater); Assert.assertTrue(lessThan < 0); int greaterThan = bpc.compare(greater, lesser); Assert.assertTrue(greaterThan > 0); int equal = bpc.compare(greater, greater); Assert.assertTrue(equal == 0); }
@Test public void testCompare_oneProperty_boolean() { List properties = Arrays.asList(new String[] { "b" }); BeanPropertyComparator bpc = new BeanPropertyComparator(properties); A lesser = new A("One", new Integer(0), Boolean.valueOf(false)); B greater = new B("One", new Integer(0), Boolean.valueOf(true)); int lessThan = bpc.compare(lesser, greater); Assert.assertTrue(lessThan < 0); int greaterThan = bpc.compare(greater, lesser); Assert.assertTrue(greaterThan > 0); int equal = bpc.compare(greater, greater); Assert.assertTrue(equal == 0); }
@Test public void testCompare_oneLevel() { List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" }); BeanPropertyComparator bpcSIB = new BeanPropertyComparator(propertiesSIB); A lesser = new A("One", new Integer(0), Boolean.valueOf(false)); B greater = new B("Two", new Integer(0), Boolean.valueOf(false)); int lessThan = bpcSIB.compare(lesser, greater); Assert.assertTrue(lessThan < 0); int greaterThan = bpcSIB.compare(greater, lesser); Assert.assertTrue(greaterThan > 0); int equal = bpcSIB.compare(greater, greater); Assert.assertTrue(equal == 0); }
@Test public void testCompare_twoLevels() { List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" }); BeanPropertyComparator bpc = new BeanPropertyComparator(propertiesSIB); A lesser = new A("Same", new Integer(-1), Boolean.valueOf(false)); B greater = new B("Same", new Integer(1), Boolean.valueOf(false)); int lessThan = bpc.compare(lesser, greater); Assert.assertTrue(lessThan < 0); int greaterThan = bpc.compare(greater, lesser); Assert.assertTrue(greaterThan > 0); int equal = bpc.compare(greater, greater); Assert.assertTrue(equal == 0); }
@Test public void testCompare_threeLevels() { List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" }); BeanPropertyComparator bpc = new BeanPropertyComparator(propertiesSIB); A lesser = new A("Same", new Integer(1), Boolean.valueOf(false)); B greater = new B("Same", new Integer(1), Boolean.valueOf(true)); int lessThan = bpc.compare(lesser, greater); Assert.assertTrue(lessThan < 0); int greaterThan = bpc.compare(greater, lesser); Assert.assertTrue(greaterThan > 0); int equal = bpc.compare(greater, greater); Assert.assertTrue(equal == 0); }
@Test public void testCompare_differentCases() { List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" }); BeanPropertyComparator sensitive = new BeanPropertyComparator(propertiesSIB, false); BeanPropertyComparator insensitive = new BeanPropertyComparator(propertiesSIB, true); A lesser = new A("SomeThing", new Integer(1), Boolean.valueOf(false)); B greater = new B("something", new Integer(1), Boolean.valueOf(false)); int equal = insensitive.compare(greater, lesser); Assert.assertTrue(equal == 0); int inequal = sensitive.compare(greater, lesser); Assert.assertTrue(inequal != 0); }
@Test public void testCompare_differentDates() throws ParseException { List propertiesD = Arrays.asList(new String[] { "d" }); DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT); BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD); D lesser = new D(dateFormat.parse("01/01/1990")); D greater = new D(dateFormat.parse("01/02/1990")); int result = comparator.compare(greater, lesser); Assert.assertEquals(1, result); result = comparator.compare(lesser, greater); Assert.assertEquals(-1, result); result = comparator.compare(lesser, lesser); Assert.assertEquals(0, result); result = comparator.compare(greater, greater); Assert.assertEquals(0, result); }
@Test public void testCompare_firstNullDates() throws ParseException { List propertiesD = Arrays.asList(new String[] { "d" }); DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT); BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD); D lesser = new D(null); D greater = new D(dateFormat.parse("01/02/1990")); int result = comparator.compare(greater, lesser); Assert.assertEquals(1, result); result = comparator.compare(lesser, greater); Assert.assertEquals(-1, result); result = comparator.compare(lesser, lesser); Assert.assertEquals(0, result); result = comparator.compare(greater, greater); Assert.assertEquals(0, result); }
@Test public void testCompare_secondNullDates() throws ParseException { List propertiesD = Arrays.asList(new String[] { "d" }); DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT); BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD); D lesser = new D(dateFormat.parse("01/02/1990")); D greater = new D(null); int result = comparator.compare(greater, lesser); Assert.assertEquals(-1, result); result = comparator.compare(lesser, greater); Assert.assertEquals(1, result); result = comparator.compare(lesser, lesser); Assert.assertEquals(0, result); result = comparator.compare(greater, greater); Assert.assertEquals(0, result); }