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 2008/04/19 17:14:23 UTC

svn commit: r649825 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/PreferencesConfiguration.java test/java/org/apache/commons/configuration2/TestPreferencesConfiguration.java

Author: oheger
Date: Sat Apr 19 08:14:21 2008
New Revision: 649825

URL: http://svn.apache.org/viewvc?rev=649825&view=rev
Log:
Added new constructor that takes a Preferences node. Some javadoc improvements.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PreferencesConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPreferencesConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PreferencesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PreferencesConfiguration.java?rev=649825&r1=649824&r2=649825&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PreferencesConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PreferencesConfiguration.java Sat Apr 19 08:14:21 2008
@@ -45,6 +45,8 @@
  * <li>the user root node</li>
  * <li>a system node corresponding to a specific package</li>
  * <li>a user node corresponding to a specific package</li>
+ * <li>alternatively a specific <code>Preferences</code> node can be passed to
+ * a constructor, which will become the new root node.</li>
  * </ul>
  * This corresponds to the static factory methods provided by the
  * <code>Preferences</code> class. It is also possible to change this node
@@ -56,8 +58,22 @@
  * interface can be used for interacting with <code>Preferences</code> nodes.
  * Note however that some features provided by the <code>Configuration</code>
  * interface are not supported by the <code>Preferences</code> API. One
- * example of such a feature is the support for multiple values for a property.
+ * example of such a feature is the support for multiple values for a property:
+ * If you call <code>addProperty()</code> multiple times with the same key, only
+ * the last value will be stored.
  * </p>
+ * <p>
+ * The values stored in the underlying <code>Preferences</code> nodes can be
+ * accessed per default using the dot notation that is also used by other
+ * <code>Configuration</code> implementations (e.g.
+ * <code>config.getString("path.to.property.name");</code>). Internally the
+ * property values are mapped to <em>attribute</em> nodes in this hierarchical
+ * configuration. The {@link ExpressionEngine} used by this class hides this
+ * fact by defining the dot as both property delimiter and attribute marker. If
+ * another expression engine is set or if this configuration is added to a
+ * combined configuration, the keys have to be adapted, for instance - when
+ * using the default expression engine:
+ * <code>config.getString("path.to.property[@name]");</code></p>
  *
  * @author Oliver Heger
  * @version $Id$
@@ -126,6 +142,24 @@
         setExpressionEngine(setUpExpressionEngine());
         setAssociatedClass(c);
         setSystem(system);
+    }
+
+    /**
+     * Creates a new instance of <code>PreferencesConfiguration</code> and
+     * initializes it with the given node. This node will become the new root
+     * node.
+     *
+     * @param rootNode the root node (must not be <b>null</b>)
+     * @throws IllegalArgumentException if the passed in node is <b>null</b>
+     */
+    public PreferencesConfiguration(Preferences rootNode)
+    {
+        this(false, null);
+        if (rootNode == null)
+        {
+            throw new IllegalArgumentException("Root node must be null!");
+        }
+        root = rootNode;
     }
 
     /**

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPreferencesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPreferencesConfiguration.java?rev=649825&r1=649824&r2=649825&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPreferencesConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestPreferencesConfiguration.java Sat Apr 19 08:14:21 2008
@@ -123,9 +123,9 @@
      */
     private PreferencesConfiguration setUpTestConfig()
     {
-        node = Preferences.systemNodeForPackage(getClass());
+        node = Preferences.userNodeForPackage(getClass());
         setUpTestData();
-        return new PreferencesConfiguration(true, getClass());
+        return new PreferencesConfiguration(false, getClass());
     }
 
     /**
@@ -169,6 +169,34 @@
     {
         PreferencesConfiguration config = setUpTestConfig();
         checkProperties(config);
+    }
+
+    /**
+     * Tests setting a specific node as root node.
+     */
+    public void testGetPropertiesSpecificNode()
+    {
+        node = Preferences.userNodeForPackage(getClass());
+        setUpTestData();
+        PreferencesConfiguration config = new PreferencesConfiguration(node);
+        checkProperties(config);
+    }
+
+    /**
+     * Tests creating a configuration with a null node. This should cause an
+     * exception.
+     */
+    public void testInitNullNode()
+    {
+        try
+        {
+            new PreferencesConfiguration((Preferences) null);
+            fail("Could create instance with null node!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
     }
 
     /**