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 2014/03/23 21:36:35 UTC

svn commit: r1580605 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/INIConfiguration.java test/java/org/apache/commons/configuration/TestINIConfiguration.java

Author: oheger
Date: Sun Mar 23 20:36:34 2014
New Revision: 1580605

URL: http://svn.apache.org/r1580605
Log:
Completed NodeHandler implementation for the global section.

The handler now filters all child nodes of the root node which are section
nodes.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java?rev=1580605&r1=1580604&r2=1580605&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java Sun Mar 23 20:36:34 2014
@@ -919,6 +919,41 @@ public class INIConfiguration extends Ba
                 }
 
                 @Override
+                public List<ImmutableNode> getChildren(ImmutableNode node,
+                        String name)
+                {
+                    List<ImmutableNode> children =
+                            super.getChildren(node, name);
+                    return filterChildrenOfGlobalSection(node, children);
+                }
+
+                @Override
+                public int getChildrenCount(ImmutableNode node, String name)
+                {
+                    List<ImmutableNode> children =
+                            (name != null) ? super.getChildren(node, name)
+                                    : super.getChildren(node);
+                    return filterChildrenOfGlobalSection(node, children).size();
+                }
+
+                @Override
+                public ImmutableNode getChild(ImmutableNode node, int index)
+                {
+                    List<ImmutableNode> children = super.getChildren(node);
+                    return filterChildrenOfGlobalSection(node, children).get(
+                            index);
+                }
+
+                @Override
+                public int indexOfChild(ImmutableNode parent,
+                        ImmutableNode child)
+                {
+                    List<ImmutableNode> children = super.getChildren(parent);
+                    return filterChildrenOfGlobalSection(parent, children)
+                            .indexOf(child);
+                }
+
+                @Override
                 protected NodeHandler<ImmutableNode> getDecoratedNodeHandler()
                 {
                     return GlobalSectionNodeModel.super.getNodeHandler();

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java?rev=1580605&r1=1580604&r2=1580605&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java Sun Mar 23 20:36:34 2014
@@ -20,6 +20,7 @@ package org.apache.commons.configuration
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.FileWriter;
@@ -40,6 +41,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.ex.ConfigurationException;
 import org.apache.commons.configuration.sync.ReadWriteSynchronizer;
 import org.apache.commons.configuration.tree.ImmutableNode;
+import org.apache.commons.configuration.tree.NodeHandler;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
@@ -541,7 +543,7 @@ public class TestINIConfiguration
     @Test
     public void testGetSectionsWithGlobal() throws ConfigurationException
     {
-        checkSectionNames(INI_DATA_GLOBAL, new String[] {
+        checkSectionNames(INI_DATA_GLOBAL, new String[]{
                 null, "section1", "section2", "section3"
         });
     }
@@ -552,7 +554,7 @@ public class TestINIConfiguration
     @Test
     public void testGetSectionsNoGlobal() throws ConfigurationException
     {
-        checkSectionNames(INI_DATA, new String[] {
+        checkSectionNames(INI_DATA, new String[]{
                 "section1", "section2", "section3"
         });
     }
@@ -564,8 +566,8 @@ public class TestINIConfiguration
     @Test
     public void testGetSectionsGlobalOnly() throws ConfigurationException
     {
-        checkSectionNames(INI_DATA_GLOBAL_ONLY, new String[] {
-            null
+        checkSectionNames(INI_DATA_GLOBAL_ONLY, new String[]{
+                null
         });
     }
 
@@ -593,7 +595,7 @@ public class TestINIConfiguration
     {
         INIConfiguration config = setUpConfig(INI_DATA2);
         config.addProperty("section5.test", Boolean.TRUE);
-        checkSectionNames(config, new String[] {
+        checkSectionNames(config, new String[]{
                 "section4", "section5"
         });
     }
@@ -1007,6 +1009,104 @@ public class TestINIConfiguration
     }
 
     /**
+     * Tests whether only properties with values occur in the enumeration of the
+     * global section.
+     */
+    @Test
+    public void testKeysOfGlobalSection() throws ConfigurationException
+    {
+        INIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
+        HierarchicalConfiguration<ImmutableNode> sub = config.getSection(null);
+        Iterator<String> keys = sub.getKeys();
+        assertEquals("Wrong key", "globalVar", keys.next());
+        if (keys.hasNext())
+        {
+            StringBuilder buf = new StringBuilder();
+            do
+            {
+                buf.append(keys.next()).append(' ');
+            } while (keys.hasNext());
+            fail("Got additional keys: " + buf);
+        }
+    }
+
+    /**
+     * Tests whether the node handler of a global section correctly filters
+     * named children.
+     */
+    @Test
+    public void testGlobalSectionNodeHandlerGetChildrenByName()
+            throws ConfigurationException
+    {
+        INIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
+        SubnodeConfiguration sub =
+                (SubnodeConfiguration) config.getSection(null);
+        NodeHandler<ImmutableNode> handler = sub.getModel().getNodeHandler();
+        assertTrue("Sections not filtered",
+                handler.getChildren(sub.getRootNode(), "section1").isEmpty());
+    }
+
+    /**
+     * Tests whether the node handler of a global section correctly determines
+     * the number of children.
+     */
+    @Test
+    public void testGlobalSectionNodeHandlerGetChildrenCount()
+            throws ConfigurationException
+    {
+        INIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
+        SubnodeConfiguration sub =
+                (SubnodeConfiguration) config.getSection(null);
+        NodeHandler<ImmutableNode> handler = sub.getModel().getNodeHandler();
+        assertEquals("Wrong number of children", 1,
+                handler.getChildrenCount(handler.getRootNode(), null));
+    }
+
+    /**
+     * Tests whether the node handler of a global section correctly returns a
+     * child by index.
+     */
+    @Test
+    public void testGlobalSectionNodeHandlerGetChildByIndex()
+            throws ConfigurationException
+    {
+        INIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
+        SubnodeConfiguration sub =
+                (SubnodeConfiguration) config.getSection(null);
+        NodeHandler<ImmutableNode> handler = sub.getModel().getNodeHandler();
+        ImmutableNode child = handler.getChild(handler.getRootNode(), 0);
+        assertEquals("Wrong child", "globalVar", child.getNodeName());
+        try
+        {
+            handler.getChild(handler.getRootNode(), 1);
+            fail("Could obtain child with invalid index!");
+        }
+        catch (IndexOutOfBoundsException iex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tests whether the node handler of a global section correctly determines
+     * the index of a child.
+     */
+    @Test
+    public void testGlobalSectionNodeHandlerIndexOfChild()
+            throws ConfigurationException
+    {
+        INIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
+        SubnodeConfiguration sub =
+                (SubnodeConfiguration) config.getSection(null);
+        NodeHandler<ImmutableNode> handler = sub.getModel().getNodeHandler();
+        List<ImmutableNode> children = handler.getRootNode().getChildren();
+        assertEquals("Wrong index", 0,
+                handler.indexOfChild(handler.getRootNode(), children.get(0)));
+        assertEquals("Wrong index of section child", -1,
+                handler.indexOfChild(handler.getRootNode(), children.get(1)));
+    }
+
+    /**
      * A thread class for testing concurrent access to the global section.
      */
     private static class GlobalSectionTestThread extends Thread