@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); }
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 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 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); }
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); }
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);  }