Author: tchemit Date: 2012-04-03 15:33:26 +0200 (Tue, 03 Apr 2012) New Revision: 2316 Url: http://nuiton.org/repositories/revision/nuiton-utils/2316 Log: fixes #2048: BeanMonitor says null objects are not equals! Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanMonitor.java trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanMonitorTest.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanMonitor.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanMonitor.java 2012-03-22 20:49:19 UTC (rev 2315) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanMonitor.java 2012-04-03 13:33:26 UTC (rev 2316) @@ -24,6 +24,7 @@ */ package org.nuiton.util.beans; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -71,12 +72,6 @@ /** Names of monitored and modified properties for the attached bean. */ protected final Map<String, PropertyDiff> propertyDiffs; -// /** Names of monitored and modified properties for the attached bean. */ -// protected final Set<String> modifiedProperties; -// -// /** To store the original values when watched properties are changing. */ -// protected final Map<String, Object> originalValues; - /** * The {@link PropertyChangeListener} which listen modification on bean * only on monitored properties. @@ -93,8 +88,6 @@ */ public BeanMonitor(String... propertyNames) { this.propertyNames = Arrays.asList(propertyNames); -// modifiedProperties = new LinkedHashSet<String>(); -// originalValues = new TreeMap<String, Object>(); propertyDiffs = new LinkedHashMap<String, PropertyDiff>(); listener = new PropertyChangeListener() { @@ -122,26 +115,29 @@ // check if value did not come back to original Object originalValue = propertyDiff.getSourceValue(); - if (originalValue == null && newValue == null || - originalValue != null && originalValue.equals(newValue)) { + if (ObjectUtils.equals(originalValue, newValue)) { + // coming back to original value // the property is no more modified propertyDiffs.remove(propertyName); -// return; } } else { - propertyDiff = new PropertyDiff( - propertyName, - oldValue, - propertyName, - newValue, - null - ); - // add it to modified properties - propertyDiffs.put(propertyName, propertyDiff); + // check old value and newvalue are real not equals + if (ObjectUtils.notEqual(newValue, oldValue)) { + propertyDiff = new PropertyDiff( + propertyName, + oldValue, + propertyName, + newValue, + null + ); + // add it to modified properties + propertyDiffs.put(propertyName, propertyDiff); + } + } // if (modifiedProperties.contains(propertyName)) { Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanMonitorTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanMonitorTest.java 2012-03-22 20:49:19 UTC (rev 2315) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanMonitorTest.java 2012-04-03 13:33:26 UTC (rev 2316) @@ -104,6 +104,24 @@ } + @Test + public void testMonitorAno2048() throws Exception { + + BeanMonitor monitor = new BeanMonitor(BeanA.PROPERTY_A, + BeanA.PROPERTY_B, + BeanA.PROPERTY_E + ); + + BeanA bean = new BeanA(); + bean.setB(null); + + monitor.setBean(bean); + + // ano #2048 : no modification with two null properties + bean.setB(null); + assertMonitor(monitor); + } + protected void assertMonitor(BeanMonitor monitor, Object... propertyNamesAndOriginalValues) {