commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r641254 - /commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java
Date Wed, 26 Mar 2008 11:05:20 GMT
Author: oheger
Date: Wed Mar 26 04:05:18 2008
New Revision: 641254

URL: http://svn.apache.org/viewvc?rev=641254&view=rev
Log:
Base class for testing concrete NodeCombiner implementations

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java
      - copied, changed from r636104, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/AbstractCombinerTest.java

Copied: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java
(from r636104, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/AbstractCombinerTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/AbstractCombinerTest.java&r1=636104&r2=641254&rev=641254&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/AbstractCombinerTest.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java
Wed Mar 26 04:05:18 2008
@@ -14,17 +14,19 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.commons.configuration2.tree;
+package org.apache.commons.configuration2.combined;
 
 import java.io.File;
+import java.util.Collection;
 
 import org.apache.commons.configuration2.ConfigurationAssert;
 import org.apache.commons.configuration2.ConfigurationException;
-import org.apache.commons.configuration2.HierarchicalConfiguration;
+import org.apache.commons.configuration2.InMemoryConfiguration;
 import org.apache.commons.configuration2.XMLConfiguration;
+import org.apache.commons.configuration2.expr.ConfigurationNodeHandler;
+import org.apache.commons.configuration2.expr.NodeHandler;
 import org.apache.commons.configuration2.tree.ConfigurationNode;
 import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
-import org.apache.commons.configuration2.tree.NodeCombiner;
 
 import junit.framework.TestCase;
 
@@ -39,17 +41,24 @@
 public abstract class AbstractCombinerTest extends TestCase
 {
     /** Constant for the first test configuration. */
-    static File CONF1 = ConfigurationAssert.getTestFile("testcombine1.xml");
+    private static File CONF1 = ConfigurationAssert
+            .getTestFile("testcombine1.xml");
 
     /** Constant for the second test configuration. */
-    static File CONF2 = ConfigurationAssert.getTestFile("testcombine2.xml");
+    private static File CONF2 = ConfigurationAssert
+            .getTestFile("testcombine2.xml");
+
+    /** The node handler for configuration nodes. */
+    protected NodeHandler<ConfigurationNode> handler;
 
     /** The combiner to be tested. */
     protected NodeCombiner combiner;
 
+    @Override
     protected void setUp() throws Exception
     {
         super.setUp();
+        handler = new ConfigurationNodeHandler();
         combiner = createCombiner();
     }
 
@@ -67,16 +76,58 @@
      * @return the union configuration
      * @throws ConfigurationException if an error occurs
      */
-    protected HierarchicalConfiguration createCombinedConfiguration()
+    protected InMemoryConfiguration createCombinedConfiguration()
             throws ConfigurationException
     {
         XMLConfiguration conf1 = new XMLConfiguration(CONF1);
         XMLConfiguration conf2 = new XMLConfiguration(CONF2);
-        ConfigurationNode cn = combiner.combine(conf1.getRootNode(), conf2
-                .getRootNode());
+        CombinedNode cn = combiner.combine(conf1.getRootNode(), handler, conf2
+                .getRootNode(), handler);
+
+        InMemoryConfiguration result = new InMemoryConfiguration();
+        result.setRootNode(convert(cn));
+
+        return result;
+    }
 
-        HierarchicalConfiguration result = new HierarchicalConfiguration();
-        result.setRootNode(cn);
+    /**
+     * Converts a node hierarchy containing combined nodes and configuration
+     * nodes into a structure that only consists of configuration nodes.
+     *
+     * @param node the root combined node
+     * @return the corresponding root configuration node
+     */
+    private ConfigurationNode convert(Object node)
+    {
+        if (node instanceof ConfigurationNode)
+        {
+            return (ConfigurationNode) node;
+        }
+
+        CombinedNode cn = (CombinedNode) node;
+        ConfigurationNode result = new DefaultConfigurationNode(cn.getName(),
+                cn.getValue());
+
+        for (String n : cn.getAttributes())
+        {
+            Object v = cn.getAttribute(n);
+            if (v instanceof Collection)
+            {
+                for (Object av : (Collection<?>) v)
+                {
+                    result.addAttribute(new DefaultConfigurationNode(n, av));
+                }
+            }
+            else
+            {
+                result.addAttribute(new DefaultConfigurationNode(n, v));
+            }
+        }
+
+        for (Object c : cn.getChildren())
+        {
+            result.addChild(convert(c));
+        }
 
         return result;
     }
@@ -87,8 +138,7 @@
     public void testInit()
     {
         assertTrue("Combiner has list nodes", combiner.getListNodes().isEmpty());
-        assertFalse("Node is list node", combiner
-                .isListNode(new DefaultConfigurationNode("test")));
+        assertFalse("Node is list node", combiner.isListNode(
+                new DefaultConfigurationNode("test"), handler));
     }
-
 }



Mime
View raw message