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/08 22:33:38 UTC

svn commit: r646063 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/flat/ test/java/org/apache/commons/configuration2/flat/

Author: oheger
Date: Tue Apr  8 13:33:37 2008
New Revision: 646063

URL: http://svn.apache.org/viewvc?rev=646063&view=rev
Log:
Added support for cloning to AbstractFlatConfiguration

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java?rev=646063&r1=646062&r2=646063&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/AbstractFlatConfiguration.java Tue Apr  8 13:33:37 2008
@@ -86,26 +86,7 @@
     {
         lockRoot = new ReentrantLock();
         nodeHandler = new FlatNodeHandler(this);
-
-        // Add event handler that invalidates the node structure if required
-        addConfigurationListener(new ConfigurationListener()
-        {
-            /**
-             * Reacts on change events. Asks the node handler whether the
-             * received event was caused by an update of the node structure. If
-             * not, the structure has to be invalidated.
-             */
-            public void configurationChanged(ConfigurationEvent event)
-            {
-                if (!event.isBeforeUpdate())
-                {
-                    if (!getNodeHandler().isInternalUpdate())
-                    {
-                        invalidateRootNode();
-                    }
-                }
-            }
-        });
+        registerChangeListener();
     }
 
     /**
@@ -196,6 +177,24 @@
     }
 
     /**
+     * Creates a copy of this instance. This implementation ensures that a
+     * change listener for invalidating the node structure is registered at the
+     * copy.
+     *
+     * @return a copy of this object
+     * @throws CloneNotSupportedException if cloning is not supported
+     */
+    @Override
+    protected Object clone() throws CloneNotSupportedException
+    {
+        AbstractFlatConfiguration copy = (AbstractFlatConfiguration) super
+                .clone();
+        copy.clearConfigurationListeners();
+        copy.registerChangeListener();
+        return copy;
+    }
+
+    /**
      * Returns the maximum index of the property with the given key. This method
      * can be used to find out how many values are stored for a given property.
      * A return value of -1 means that this property is unknown. A value of 0
@@ -271,4 +270,30 @@
      * @return a flag whether the value could be removed
      */
     protected abstract boolean clearPropertyValueDirect(String key, int index);
+
+    /**
+     * Registers a change listener at this configuration that causes the node
+     * structure to be invalidated whenever the content is changed.
+     */
+    private void registerChangeListener()
+    {
+        addConfigurationListener(new ConfigurationListener()
+        {
+            /**
+             * Reacts on change events. Asks the node handler whether the
+             * received event was caused by an update of the node structure. If
+             * not, the structure has to be invalidated.
+             */
+            public void configurationChanged(ConfigurationEvent event)
+            {
+                if (!event.isBeforeUpdate())
+                {
+                    if (!getNodeHandler().isInternalUpdate())
+                    {
+                        invalidateRootNode();
+                    }
+                }
+            }
+        });
+    }
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java?rev=646063&r1=646062&r2=646063&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/FlatConfigurationMockImpl.java Tue Apr  8 13:33:37 2008
@@ -31,7 +31,7 @@
  *         Configuration team</a>
  * @version $Id$
  */
-class FlatConfigurationMockImpl extends AbstractFlatConfiguration
+class FlatConfigurationMockImpl extends AbstractFlatConfiguration implements Cloneable
 {
     /** Constant for the name of the test property. */
     public static final String NAME = "testFlatNode";

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java?rev=646063&r1=646062&r2=646063&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestAbstractFlatConfiguration.java Tue Apr  8 13:33:37 2008
@@ -60,7 +60,7 @@
     }
 
     /**
-     * Prepares the mock configuration to expect initialization steps for a
+     * Prepares the test configuration to expect initialization steps for a
      * number of getRootNode() invocations. This method adds the corresponding
      * expectations for method calls.
      *
@@ -69,6 +69,20 @@
      */
     private void prepareGetRootNode(int count)
     {
+        prepareGetRootNode(config, count);
+    }
+
+    /**
+     * Prepares the specified mock configuration to expect initialization steps for a
+     * number of getRootNode() invocations. This method adds the corresponding
+     * expectations for method calls.
+     *
+     * @param config the configuration to initialize
+     * @param count the number of expected getRootNode() invocations (that
+     *        require a re-creation of the node structure)
+     */
+    private void prepareGetRootNode(FlatConfigurationMockImpl config, int count)
+    {
         config.keyList = Arrays.asList(KEYS);
         for (int cnt = 0; cnt < count; cnt++)
         {
@@ -209,6 +223,22 @@
         config.addConfigurationListener(l);
         config.clearPropertyValue(FlatConfigurationMockImpl.NAME, 0);
         l.verify();
+    }
+
+    /**
+     * Tests cloning a flat configuration. We have to check whether the event
+     * listeners are correctly registered.
+     */
+    public void testClone() throws CloneNotSupportedException
+    {
+        prepareGetRootNode(1);
+        checkNodeStructure(config.getRootNode());
+        FlatConfigurationMockImpl copy = (FlatConfigurationMockImpl) config.clone();
+        prepareGetRootNode(copy, 2);
+        FlatNode root = copy.getRootNode();
+        checkNodeStructure(root);
+        copy.clearPropertyValue(FlatConfigurationMockImpl.NAME, 0);
+        assertNotSame("Structure was not re-created", root, copy.getRootNode());
     }
 
     /**