commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1587017 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java test/java/org/apache/commons/configuration/TestCombinedConfiguration.java
Date Sun, 13 Apr 2014 16:02:30 GMT
Author: oheger
Date: Sun Apr 13 16:02:30 2014
New Revision: 1587017

URL: http://svn.apache.org/r1587017
Log:
Added tests for creating sub configurations.

It has to be ensured that the synchronizer is called before the sub
configurations are otained. Otherwise, the combined configuration is not yet
initialized.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestCombinedConfiguration.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java?rev=1587017&r1=1587016&r2=1587017&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
Sun Apr 13 16:02:30 2014
@@ -226,8 +226,16 @@ public class BaseHierarchicalConfigurati
     public HierarchicalConfiguration<ImmutableNode> configurationAt(String key,
             boolean supportUpdates)
     {
-        return supportUpdates ? createConnectedSubConfiguration(key)
-                : createIndependentSubConfiguration(key);
+        beginRead(false);
+        try
+        {
+            return supportUpdates ? createConnectedSubConfiguration(key)
+                    : createIndependentSubConfiguration(key);
+        }
+        finally
+        {
+            endRead();
+        }
     }
 
     /**
@@ -429,7 +437,17 @@ public class BaseHierarchicalConfigurati
     public List<HierarchicalConfiguration<ImmutableNode>> configurationsAt(
             String key)
     {
-        List<ImmutableNode> nodes = fetchFilteredNodeResults(key);
+        List<ImmutableNode> nodes;
+        beginRead(false);
+        try
+        {
+            nodes = fetchFilteredNodeResults(key);
+        }
+        finally
+        {
+            endRead();
+        }
+
         List<HierarchicalConfiguration<ImmutableNode>> results =
                 new ArrayList<HierarchicalConfiguration<ImmutableNode>>(
                         nodes.size());
@@ -455,7 +473,17 @@ public class BaseHierarchicalConfigurati
             return configurationsAt(key);
         }
 
-        InMemoryNodeModel parentModel = getSubConfigurationParentModel();
+        InMemoryNodeModel parentModel;
+        beginRead(false);
+        try
+        {
+            parentModel = getSubConfigurationParentModel();
+        }
+        finally
+        {
+            endRead();
+        }
+
         Collection<NodeSelector> selectors =
                 parentModel.selectAndTrackNodes(key, this);
         return createConnectedSubConfigurations(parentModel, selectors);

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestCombinedConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestCombinedConfiguration.java?rev=1587017&r1=1587016&r2=1587017&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestCombinedConfiguration.java
(original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestCombinedConfiguration.java
Sun Apr 13 16:02:30 2014
@@ -79,6 +79,9 @@ public class TestCombinedConfiguration
     /** Constant for the name of the second child configuration.*/
     private static final String CHILD2 = TEST_NAME + "2";
 
+    /** Constant for the key for a sub configuration. */
+    private static final String SUB_KEY = "test.sub.config";
+
     /** Helper object for managing temporary files. */
     @Rule
     public TemporaryFolder folder = new TemporaryFolder();
@@ -971,12 +974,76 @@ public class TestCombinedConfiguration
     }
 
     /**
+     * Prepares the test configuration for a test for sub configurations. Some
+     * child configurations are added.
+     *
+     * @return the sub configuration at the test sub key
+     */
+    private AbstractConfiguration setUpSubConfigTest()
+    {
+        AbstractConfiguration srcConfig = setUpTestConfiguration();
+        config.addConfiguration(srcConfig, "source", SUB_KEY);
+        config.addConfiguration(setUpTestConfiguration());
+        config.addConfiguration(setUpTestConfiguration(), "otherTest",
+                "other.prefix");
+        return srcConfig;
+    }
+
+    /**
+     * Tests whether a sub configuration survives updates of its parent.
+     */
+    @Test
+    public void testSubConfigurationWithUpdates()
+    {
+        AbstractConfiguration srcConfig = setUpSubConfigTest();
+        HierarchicalConfiguration<ImmutableNode> sub =
+                config.configurationAt(SUB_KEY, true);
+        assertTrue("Wrong value before update", sub.getBoolean(TEST_KEY));
+        srcConfig.setProperty(TEST_KEY, Boolean.FALSE);
+        assertFalse("Wrong value from combined configuration",
+                config.getBoolean(SUB_KEY + '.' + TEST_KEY));
+        assertFalse("Wrong value after update", sub.getBoolean(TEST_KEY));
+    }
+
+    /**
+     * Checks the configurationsAt() method.
+     * @param withUpdates flag whether updates are supported
+     */
+    private void checkConfigurationsAt(boolean withUpdates)
+    {
+        setUpSubConfigTest();
+        List<HierarchicalConfiguration<ImmutableNode>> subs =
+                config.configurationsAt(SUB_KEY, withUpdates);
+        assertEquals("Wrong number of sub configurations", 1, subs.size());
+        assertTrue("Wrong value in sub configuration",
+                subs.get(0).getBoolean(TEST_KEY));
+    }
+
+    /**
+     * Tests whether sub configurations can be created from a key.
+     */
+    @Test
+    public void testConfigurationsAt()
+    {
+        checkConfigurationsAt(false);
+    }
+
+    /**
+     * Tests whether sub configurations can be created which are attached.
+     */
+    @Test
+    public void testConfigurationsAtWithUpdates()
+    {
+        checkConfigurationsAt(true);
+    }
+
+    /**
      * Helper method for creating a test configuration to be added to the
      * combined configuration.
      *
      * @return the test configuration
      */
-    private AbstractConfiguration setUpTestConfiguration()
+    private static AbstractConfiguration setUpTestConfiguration()
     {
         BaseHierarchicalConfiguration config = new BaseHierarchicalConfiguration();
         config.addProperty(TEST_KEY, Boolean.TRUE);



Mime
View raw message