You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/04/13 16:45:15 UTC

svn commit: r1467644 - in /commons/proper/configuration/trunk/src: changes/changes.xml main/java/org/apache/commons/configuration/MapConfiguration.java test/java/org/apache/commons/configuration/TestSystemConfiguration.java

Author: oheger
Date: Sat Apr 13 14:45:15 2013
New Revision: 1467644

URL: http://svn.apache.org/r1467644
Log:
[CONFIGURATION-540] MapConfiguration now uses a passed in Properties object
directly as its data store rather than copying it.

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/MapConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1467644&r1=1467643&r2=1467644&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Sat Apr 13 14:45:15 2013
@@ -27,6 +27,13 @@
   <body>
     <release version="2.0" date="in SVN"
       description="TBD">
+      <action dev="oheger" type="update" issue="CONFIGURATION-540">
+        MapConfiguration now directly uses a Properties object passed to its
+        constructor as data store rather than copying it. This allows
+        SystemConfiguration to be connected to system properties; i.e.
+        changing a property through SystemConfiguration immediately affects
+        the corresponding system property.
+      </action>
       <action dev="oheger" type="update" issue="CONFIGURATION-539">
         The deprectated INIConfiguration class was removed.
         HierarchicalINIConfiguration was renamed to INIConfiguration.

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/MapConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/MapConfiguration.java?rev=1467644&r1=1467643&r2=1467644&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/MapConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/MapConfiguration.java Sat Apr 13 14:45:15 2013
@@ -18,7 +18,6 @@
 package org.apache.commons.configuration;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -108,15 +107,16 @@ public class MapConfiguration extends Ab
     }
 
     /**
-     * Creates a new instance of {@code MapConfiguration} and initializes its
-     * content from the specified {@code Properties} object. The resulting
-     * configuration is not connected to the {@code Properties} object, but all
-     * keys which are strings are copied (keys of other types are ignored).
+     * Creates a new instance of {@code MapConfiguration} which uses the
+     * specified {@code Properties} object as its data store. All changes of
+     * this configuration affect the given {@code Properties} object and
+     * vice versa. Note that while {@code Properties} actually
+     * implements {@code Map<Object, Object>}, we expect it to contain only
+     * string keys. Other key types will lead to {@code ClassCastException}
+     * exceptions on certain methods.
      *
      * @param props the {@code Properties} object defining the content of this
      *        configuration
-     * @throws NullPointerException if the {@code Properties} object is
-     *         <b>null</b>
      * @since 1.8
      */
     public MapConfiguration(Properties props)
@@ -250,22 +250,19 @@ public class MapConfiguration extends Ab
     }
 
     /**
-     * Helper method for copying all string keys from the given
-     * {@code Properties} object to a newly created map.
+     * Helper method for converting the type of the {@code Properties} object
+     * to a supported map type. As stated by the comment of the constructor,
+     * we expect the {@code Properties} object to contain only String key;
+     * therefore, it is safe to do this cast.
      *
      * @param props the {@code Properties} to be copied
      * @return a newly created map with all string keys of the properties
      */
+    @SuppressWarnings("unchecked")
     private static Map<String, Object> convertPropertiesToMap(Properties props)
     {
-        Map<String, Object> map = new HashMap<String, Object>();
-        for (Map.Entry<Object, Object> e : props.entrySet())
-        {
-            if (e.getKey() instanceof String)
-            {
-                map.put((String) e.getKey(), e.getValue());
-            }
-        }
+        @SuppressWarnings("rawtypes")
+        Map map = props;
         return map;
     }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java?rev=1467644&r1=1467643&r2=1467644&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestSystemConfiguration.java Sat Apr 13 14:45:15 2013
@@ -78,4 +78,17 @@ public class TestSystemConfiguration
         SystemConfiguration sconf = new SystemConfiguration();
         assertTrue("Property from file not found", sconf.getBoolean("fromFile"));
     }
+
+    /**
+     * Tests whether the configuration can be used to change system properties.
+     */
+    @Test
+    public void testChangeSystemProperties()
+    {
+        String testProperty = "someTest";
+        SystemConfiguration config = new SystemConfiguration();
+        config.setProperty(testProperty, "true");
+        assertEquals("System property not changed", "true",
+                System.getProperty(testProperty));
+    }
 }