commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skitch...@apache.org
Subject svn commit: r178925 - /jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
Date Sun, 29 May 2005 04:10:05 GMT
Author: skitching
Date: Sat May 28 21:10:04 2005
New Revision: 178925

URL: http://svn.apache.org/viewcvs?rev=178925&view=rev
Log:
Test case to show bug for setNestedProperty with pattern "a(b)" when a is a Map.

Modified:
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java?rev=178925&r1=178924&r2=178925&view=diff
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
Sat May 28 21:10:04 2005
@@ -3646,6 +3646,49 @@
         assertEquals("Cannot set mapped no-getter property", "MAP:Epsilon", bean.getSecret());
     }
     
+    /**
+     * There was a bug in setNestedProperty/getNestedProperty when the
+     * target bean is a map. This test case ensures that the problem is
+     * fixed.
+     */
+    public void testBeanImplementingMap() throws Exception {
+        HashMap map = new HashMap();
+        HashMap submap = new HashMap();
+        BetaBean betaBean1 = new BetaBean("test1");
+        BetaBean betaBean2 = new BetaBean("test2");
+        
+        // map.put("submap", submap)
+        PropertyUtils.setNestedProperty(map, "submap", submap);
+        
+        // map.get("submap").put("beta1", betaBean1)
+        PropertyUtils.setNestedProperty(map, "submap.beta1", betaBean1);
+        assertEquals("Unexpected keys in map", "submap", keysToString(map));
+        assertEquals("Unexpected keys in submap", "beta1", keysToString(submap));
+
+        // map.get("submap").put("beta2", betaBean2)
+        PropertyUtils.setNestedProperty(map, "submap(beta2)", betaBean2);
+        assertEquals("Unexpected keys in map", "submap", keysToString(map));
+        assertEquals("Unexpected keys in submap", "beta1, beta2", keysToString(submap));
+    }
+
+    /**
+     * Returns a single string containing all the keys in the map,
+     * sorted in alphabetical order and separated by ", ".
+     * <p>
+     * If there are no keys, an empty string is returned.
+     */
+    private String keysToString(Map map) {
+        Object[] mapKeys = map.keySet().toArray();
+        java.util.Arrays.sort(mapKeys);
+        StringBuffer buf = new StringBuffer();
+        for(int i=0; i<mapKeys.length; ++i) {
+            if (i != 0)
+                buf.append(", ");
+            buf.append(mapKeys[i]);
+        }
+        return buf.toString();
+    }
+
     /** 
      * This tests to see that classes that implement Map can have 
      * their standard properties set.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message