Author: oheger
Date: Fri Feb  7 20:38:36 2014
New Revision: 1565803

URL: http://svn.apache.org/r1565803
Log:
Adapted test class for ConfigurationNodePointerFactory.

Now a node structure of ImmutableNode objects is queried.

Modified:
    
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/AbstractXPathTest.java
    
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointerFactory.java

Modified: 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/AbstractXPathTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/AbstractXPathTest.java?rev=1565803&r1=1565802&r2=1565803&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/AbstractXPathTest.java
 (original)
+++ 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/AbstractXPathTest.java
 Fri Feb  7 20:38:36 2014
@@ -39,6 +39,9 @@ public abstract class AbstractXPathTest
     /** Constant for the name of the counter attribute. */
     protected static final String ATTR_NAME = "counter";
 
+    /** Constant for a name of an attribute of the root node. */
+    protected static final String ATTR_ROOT = "rootAttr";
+
     /** Constant for the name of the first child. */
     protected static final String CHILD_NAME1 = "subNode";
 
@@ -78,7 +81,8 @@ public abstract class AbstractXPathTest
      * child nodes having the names {@code CHILD_NAME1} or
      * {@code CHILD_NAME2}. Their values are named like their parent
      * node with an additional index. Each node has an attribute with a counter
-     * value.
+     * value. The root node has a special attribute named {@value #ATTR_ROOT}
+     * with the value {@code true}.
      *
      * @param levels the number of levels in the hierarchy
      * @return the root node of the hierarchy
@@ -87,6 +91,7 @@ public abstract class AbstractXPathTest
     {
         ImmutableNode.Builder resultBuilder = new ImmutableNode.Builder();
         createLevel(resultBuilder, null, levels);
+        resultBuilder.addAttribute(ATTR_ROOT, String.valueOf(true));
         return resultBuilder.create();
     }
 

Modified: 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointerFactory.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointerFactory.java?rev=1565803&r1=1565802&r2=1565803&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointerFactory.java
 (original)
+++ 
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointerFactory.java
 Fri Feb  7 20:38:36 2014
@@ -17,16 +17,15 @@
 package org.apache.commons.configuration.tree.xpath;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.commons.configuration.tree.ConfigurationNode;
-import org.apache.commons.configuration.tree.DefaultConfigurationNode;
+import org.apache.commons.configuration.tree.ImmutableNode;
 import org.apache.commons.jxpath.JXPathContext;
 import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
 /**
@@ -34,25 +33,26 @@ import org.junit.Test;
  * call the factory's methods, but rather checks if it can be installed in a
  * {@code JXPathContext} and if XPath expressions can be evaluated.
  *
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html";>Commons
- * Configuration team</a>
  * @version $Id$
  */
 public class TestConfigurationNodePointerFactory extends AbstractXPathTest
 {
     /** Stores the JXPathContext used for testing. */
-    JXPathContext context;
+    private JXPathContext context;
+
+    @BeforeClass
+    public static void setUpBeforeClass() throws Exception
+    {
+        JXPathContextReferenceImpl
+                .addNodePointerFactory(new ConfigurationNodePointerFactory());
+    }
 
     @Override
     @Before
     public void setUp() throws Exception
     {
         super.setUp();
-        JXPathContextReferenceImpl
-                .addNodePointerFactory(new ConfigurationNodePointerFactory());
-        context = JXPathContext.newContext(root);
-        context.setLenient(true);
+        context = new XPathContextFactory().createContext(root, handler);
     }
 
     /**
@@ -61,21 +61,19 @@ public class TestConfigurationNodePointe
     @Test
     public void testSimpleXPath()
     {
-        List<?> nodes = context.selectNodes(CHILD_NAME1);
-        assertEquals("Incorrect number of results", 2, nodes.size());
-        for (Iterator<?> it = nodes.iterator(); it.hasNext();)
-        {
-            ConfigurationNode node = (ConfigurationNode) it.next();
-            assertEquals("Incorrect node name", CHILD_NAME1, node.getName());
-            assertEquals("Incorrect parent node", root, node.getParentNode());
+        List<?> results = context.selectNodes(CHILD_NAME1);
+        assertEquals("Incorrect number of results", 2, results.size());
+        for (Object result : results) {
+            ImmutableNode node = (ImmutableNode) result;
+            assertEquals("Incorrect node name", CHILD_NAME1, 
node.getNodeName());
         }
 
-        nodes = context.selectNodes("/" + CHILD_NAME1);
-        assertEquals("Incorrect number of results", 2, nodes.size());
+        results = context.selectNodes("/" + CHILD_NAME1);
+        assertEquals("Incorrect number of results", 2, results.size());
 
-        nodes = context.selectNodes(CHILD_NAME2 + "/" + CHILD_NAME1 + "/"
+        results = context.selectNodes(CHILD_NAME2 + "/" + CHILD_NAME1 + "/"
                 + CHILD_NAME2);
-        assertEquals("Incorrect number of results", 18, nodes.size());
+        assertEquals("Incorrect number of results", 18, results.size());
     }
 
     /**
@@ -96,32 +94,30 @@ public class TestConfigurationNodePointe
         int index = 1;
         for (Iterator<?> it = nodes.iterator(); it.hasNext(); index++)
         {
-            ConfigurationNode node = (ConfigurationNode) it.next();
+            ImmutableNode node = (ImmutableNode) it.next();
             assertEquals("Wrong node value for child " + index, "2." + index,
                     node.getValue());
         }
     }
 
     /**
-     * Tests accessing attributes.
+     * Tests whether the attribute of a node can be queried.
      */
     @Test
-    public void testAttributes()
+    public void testQueryAttribute()
     {
-        root.addAttribute(new DefaultConfigurationNode("testAttr", "true"));
-        assertEquals("Did not find attribute of root node", "true", context
-                .getValue("@testAttr"));
         assertEquals("Incorrect attribute value", "1", context.getValue("/"
                 + CHILD_NAME2 + "[1]/@" + ATTR_NAME));
+    }
 
-        assertTrue("Found elements with name attribute", context.selectNodes(
-                "//" + CHILD_NAME2 + "[@name]").isEmpty());
-        ConfigurationNode node = root.getChild(2).getChild(
-                1).getChildren(CHILD_NAME2).get(1);
-        node.addAttribute(new DefaultConfigurationNode("name", "testValue"));
-        List<?> nodes = context.selectNodes("//" + CHILD_NAME2 + "[@name]");
-        assertEquals("Name attribute not found", 1, nodes.size());
-        assertEquals("Wrong node returned", node, nodes.get(0));
+    /**
+     * Tests whether an attribute of the root node can be queried.
+     */
+    @Test
+    public void testQueryRootAttribute()
+    {
+        assertEquals("Did not find attribute of root node", "true", context
+                .getValue("@" + ATTR_ROOT));
     }
 
     /**
@@ -154,8 +150,8 @@ public class TestConfigurationNodePointe
         List<?> nodes = context.selectNodes("/" + CHILD_NAME1
                 + "[2]/following-sibling::*");
         assertEquals("Wrong number of following siblings", 1, nodes.size());
-        ConfigurationNode node = (ConfigurationNode) nodes.get(0);
-        assertEquals("Wrong node type", CHILD_NAME2, node.getName());
+        ImmutableNode node = (ImmutableNode) nodes.get(0);
+        assertEquals("Wrong node type", CHILD_NAME2, node.getNodeName());
         assertEquals("Wrong index", String.valueOf(CHILD_COUNT), node
                 .getValue());
     }
@@ -172,7 +168,7 @@ public class TestConfigurationNodePointe
         for (int index = 0, value = 3; index < nodes.size(); index++, value--)
         {
             assertEquals("Wrong node index", String.valueOf(value),
-                    ((ConfigurationNode) nodes.get(index)).getValue());
+                    ((ImmutableNode) nodes.get(index)).getValue());
         }
     }
 }


Reply via email to