commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r639014 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/ test/java/org/apache/commons/configuration2/
Date Wed, 19 Mar 2008 20:58:25 GMT
Author: oheger
Date: Wed Mar 19 13:58:23 2008
New Revision: 639014

URL: http://svn.apache.org/viewvc?rev=639014&view=rev
Log:
Added support for attributes with multiple values to AbstractHierarchicalConfiguration. Now
InMemoryConfiguration should be a full replacement for HierarchicalConfiguration.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/InMemoryConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java?rev=639014&r1=639013&r2=639014&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
Wed Mar 19 13:58:23 2008
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration2;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
@@ -176,7 +177,15 @@
                 Object value = nodes.getValue(i, getNodeHandler());
                 if (value != null)
                 {
-                    list.add(value);
+                    if (value instanceof Collection)
+                    {
+                        // there may be multiple values
+                        list.addAll((Collection<?>) value);
+                    }
+                    else
+                    {
+                        list.add(value);
+                    }
                 }
             }
 
@@ -579,7 +588,24 @@
      */
     public int getMaxIndex(String key)
     {
-        return fetchNodeList(key).size() - 1;
+        NodeList<T> nodes = fetchNodeList(key);
+        int cnt = 0;
+
+        for (int index = 0; index < nodes.size(); index++)
+        {
+            Object value = nodes.getValue(index, getNodeHandler());
+            if (value instanceof Collection)
+            {
+                // if there are multiple values, count them all
+                cnt += ((Collection<?>) value).size();
+            }
+            else
+            {
+                cnt++;
+            }
+        }
+
+        return cnt - 1;
     }
 
     /**
@@ -709,7 +735,7 @@
         // Add the new property
         if (data.isAttribute())
         {
-            getNodeHandler().setAttributeValue(node, data.getNewNodeName(),
+            getNodeHandler().addAttributeValue(node, data.getNewNodeName(),
                     value);
             return null;
         }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/InMemoryConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/InMemoryConfiguration.java?rev=639014&r1=639013&r2=639014&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/InMemoryConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/InMemoryConfiguration.java
Wed Mar 19 13:58:23 2008
@@ -27,7 +27,6 @@
 import org.apache.commons.configuration2.expr.NodeAddData;
 import org.apache.commons.configuration2.expr.NodeHandler;
 import org.apache.commons.configuration2.expr.NodeList;
-import org.apache.commons.configuration2.expr.NodeVisitor;
 import org.apache.commons.configuration2.expr.NodeVisitorAdapter;
 import org.apache.commons.configuration2.tree.ConfigurationNode;
 import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
@@ -147,7 +146,7 @@
      * @param c the configuration that is to be copied (if <b>null</b>, this
      * constructor will behave like the standard constructor)
      */
-    public InMemoryConfiguration(InMemoryConfiguration c)
+    public InMemoryConfiguration(AbstractHierarchicalConfiguration<? extends ConfigurationNode>
c)
     {
         this();
         if (c != null)
@@ -233,18 +232,6 @@
             parent = processNodeAddData(addData, null);
         }
 
-        // a visitor to ensure that the nodes' references are cleared; this is
-        // necessary if the nodes are moved from another configuration
-        NodeVisitor<ConfigurationNode> clearRefVisitor = new NodeVisitorAdapter<ConfigurationNode>()
-        {
-            @Override
-            public void visitBeforeChildren(ConfigurationNode node,
-                    NodeHandler<ConfigurationNode> handler)
-            {
-                node.setReference(null);
-            }
-        };
-
         for (ConfigurationNode child : nodes)
         {
             if (child.isAttribute())
@@ -255,7 +242,7 @@
             {
                 parent.addChild(child);
             }
-            visit(child, clearRefVisitor);
+            clearReferences(child);
         }
         fireEvent(EVENT_ADD_NODES, key, nodes, false);
     }
@@ -414,6 +401,10 @@
                     NodeHandler<ConfigurationNode> handler)
             {
                 node.setReference(null);
+                for (ConfigurationNode attr : node.getAttributes())
+                {
+                    attr.setReference(null);
+                }
             }
         });
     }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java?rev=639014&r1=639013&r2=639014&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
Wed Mar 19 13:58:23 2008
@@ -419,6 +419,17 @@
         }
     }
 
+    /**
+     * Tests querying the maximum index for attributes with multiple values.
+     */
+    public void testGetMaxIndexAttributesMultipleValues()
+    {
+        config.addProperty("tables.table(0)[@mode]", "test");
+        config.addProperty("tables.table(0)[@mode]", "production");
+        config.addProperty("tables.table(1)[@mode]", "staging");
+        assertEquals("Wrong max index", 2, config.getMaxIndex("tables.table[@mode]"));
+    }
+
     public void testSubset()
     {
         // test the subset on the first table
@@ -914,6 +925,49 @@
 	    InMemoryConfiguration copy = new InMemoryConfiguration(null);
 		assertTrue("Configuration not empty", copy.isEmpty());
 	}
+
+	/**
+     * Tests adding multiple values to an attribute.
+     */
+    public void testAddMultipleAttributeValues()
+    {
+        final String attrKey = "tables.table(0)[@mode]";
+        config.addProperty(attrKey, "system");
+        config.addProperty(attrKey, "security");
+        assertEquals("Wrong first attribute", "system", config
+                .getString(attrKey));
+        List<?> values = config.getList(attrKey);
+        assertEquals("Wrong number of values", 2, values.size());
+        assertEquals("Wrong value 1", "system", values.get(0));
+        assertEquals("Wrong value 2", "security", values.get(1));
+    }
+
+    /**
+     * Tests overriding an attribute with multiple values.
+     */
+    public void testOverrideMultipleAttributeValues()
+    {
+        final String attrKey = "tables.table(0)[@mode]";
+        testAddMultipleAttributeValues(); // set attribute values
+        config.setProperty(attrKey, "NewValue");
+        List<?> values = config.getList(attrKey);
+        assertEquals("Wrong number of values", 1, values.size());
+        assertEquals("Wrong value 1", "NewValue", values.get(0));
+    }
+
+    /**
+     * Tests querying attributes with multiple values.
+     */
+    public void testQueryMultipleAttributeValues()
+    {
+        testAddMultipleAttributeValues();  // add attribute values
+        config.addProperty("tables.table(1)[@mode]", "test");
+        List<?> values = config.getList("tables.table[@mode]");
+        assertEquals("Wrong number of values", 3, values.size());
+        assertEquals("Wrong value 1", "system", values.get(0));
+        assertEquals("Wrong value 2", "security", values.get(1));
+        assertEquals("Wrong value 3", "test", values.get(2));
+    }
 
 	/**
      * Helper method for testing the getKeys(String) method.



Mime
View raw message