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 2008/03/12 21:54:10 UTC
svn commit: r636506 -
/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/
Author: oheger
Date: Wed Mar 12 13:53:56 2008
New Revision: 636506
URL: http://svn.apache.org/viewvc?rev=636506&view=rev
Log:
Tests for helper classes for XPathExpressionEngine
Added:
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/AbstractXPathTest.java
- copied, changed from r635359, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/AbstractXPathTest.java
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java (with props)
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationIteratorAttributes.java
- copied, changed from r635359, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationIteratorAttributes.java
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodeIteratorChildren.java
- copied, changed from r635359, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationNodeIteratorChildren.java
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodePointer.java
- copied, changed from r635359, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationNodePointer.java
Copied: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/AbstractXPathTest.java (from r635359, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/AbstractXPathTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/AbstractXPathTest.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/AbstractXPathTest.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/AbstractXPathTest.java&r1=635359&r2=636506&rev=636506&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/AbstractXPathTest.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/AbstractXPathTest.java Wed Mar 12 13:53:56 2008
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.configuration2.tree.xpath;
+package org.apache.commons.configuration2.expr.xpath;
import java.util.ArrayList;
import java.util.List;
@@ -22,6 +22,7 @@
import org.apache.commons.configuration2.tree.ConfigurationNode;
import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
import org.apache.commons.jxpath.ri.model.NodeIterator;
+import org.apache.commons.jxpath.ri.model.NodePointer;
import junit.framework.TestCase;
@@ -53,6 +54,7 @@
/** Stores the root node of the hierarchy. */
protected ConfigurationNode root;
+ @Override
protected void setUp() throws Exception
{
super.setUp();
@@ -62,6 +64,7 @@
/**
* Clears the test environment.
*/
+ @Override
protected void tearDown() throws Exception
{
root = null;
@@ -108,19 +111,18 @@
}
/**
- * Returns a list with all configuration nodes contained in the specified
- * iteration. It is assumed that the iteration contains only elements of
- * this type.
+ * Returns a list with all node pointers contained in the specified
+ * iteration.
*
* @param iterator the iterator
- * @return a list with configuration nodes obtained from the iterator
+ * @return a list with the node pointers obtained from the iterator
*/
- protected List iterationElements(NodeIterator iterator)
+ protected List<NodePointer> iterationElements(NodeIterator iterator)
{
- List result = new ArrayList();
+ List<NodePointer> result = new ArrayList<NodePointer>();
for (int pos = 1; iterator.setPosition(pos); pos++)
{
- result.add(iterator.getNodePointer().getNode());
+ result.add(iterator.getNodePointer());
}
return result;
}
Added: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java?rev=636506&view=auto
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java (added)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java Wed Mar 12 13:53:56 2008
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration2.expr.xpath;
+
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.commons.configuration2.expr.ConfigurationNodeHandler;
+import org.apache.commons.configuration2.tree.ConfigurationNode;
+import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
+import org.apache.commons.jxpath.ri.Compiler;
+import org.apache.commons.jxpath.ri.QName;
+import org.apache.commons.jxpath.ri.compiler.NodeTest;
+import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for ConfigurationAttributePointer.
+ *
+ * @author Oliver Heger
+ * @version $Id$
+ */
+public class TestConfigurationAttributePointer extends TestCase
+{
+ /** Constant for the name of the test attribute. */
+ private static final String ATTR_NAME = "myAttr";
+
+ /** Constant for the value of the test attribute. */
+ private static final String ATTR_VALUE = "myValue";
+
+ /** Stores the parent node pointer. */
+ private ConfigurationNodePointer<ConfigurationNode> parent;
+
+ /** The attribute pointer to be tested. */
+ private ConfigurationAttributePointer<ConfigurationNode> pointer;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ ConfigurationNode nd = new DefaultConfigurationNode("parent");
+ ConfigurationNode attr = new DefaultConfigurationNode(ATTR_NAME,
+ ATTR_VALUE);
+ nd.addAttribute(attr);
+ parent = new ConfigurationNodePointer<ConfigurationNode>(nd,
+ new ConfigurationNodeHandler(), Locale.ENGLISH);
+ pointer = new ConfigurationAttributePointer<ConfigurationNode>(parent,
+ ATTR_NAME);
+ }
+
+ /**
+ * Tests whether the correct pointer is returned.
+ */
+ public void testGetParentPointer()
+ {
+ assertSame("Wrong parent pointer", parent, pointer.getParentPointer());
+ }
+
+ /**
+ * Tests querying the base value.
+ */
+ public void testGetBaseValue()
+ {
+ assertEquals("Wrong base value", ATTR_VALUE, pointer.getBaseValue());
+ }
+
+ /**
+ * Tests querying the immediate node.
+ */
+ public void testGetImmediateNode()
+ {
+ assertEquals("Wrong immediate node", parent.getConfigurationNode(),
+ pointer.getImmediateNode());
+ }
+
+ /**
+ * Tests the length.
+ */
+ public void testGetLength()
+ {
+ assertEquals("Wrong length", 1, pointer.getLength());
+ }
+
+ /**
+ * Tests querying the node name.
+ */
+ public void testGetName()
+ {
+ QName name = pointer.getName();
+ assertEquals("Wrong name", ATTR_NAME, name.getName());
+ assertNull("Prefix not null", name.getPrefix());
+ }
+
+ /**
+ * Tests the collection flag.
+ */
+ public void testIsCollection()
+ {
+ assertFalse("Wrong collection flag", pointer.isCollection());
+ }
+
+ /**
+ * Tests the leaf flag.
+ */
+ public void testIsLeaf()
+ {
+ assertTrue("Wrong leaf flag", pointer.isLeaf());
+ }
+
+ /**
+ * Tests the attribute flag.
+ */
+ public void testIsAttribute()
+ {
+ assertTrue("Not an attribute", pointer.isAttribute());
+ }
+
+ /**
+ * Tests querying the attribute's value.
+ */
+ public void testGetValue()
+ {
+ assertEquals("Wrong value", ATTR_VALUE, pointer.getValue());
+ }
+
+ /**
+ * Tests setting a new value.
+ */
+ public void testSetValue()
+ {
+ pointer.setValue("newValue");
+ List<ConfigurationNode> attrs = parent.getConfigurationNode()
+ .getAttributes();
+ assertEquals("Wrong number of attributes", 1, attrs.size());
+ assertEquals("Wrong attribute value", "newValue", attrs.get(0)
+ .getValue());
+ }
+
+ /**
+ * Tests querying an iterator for attributes. Result should be null.
+ */
+ public void testAttributeIterator()
+ {
+ assertNull("Returned an attribute iterator", pointer
+ .attributeIterator(new QName(null, "test")));
+ }
+
+ /**
+ * Tests querying an iterator for children. Result should be null.
+ */
+ public void testChildIterator()
+ {
+ assertNull("Returned an iterator for children", pointer.childIterator(
+ null, false, null));
+ }
+
+ /**
+ * Tests the testNode() method.
+ */
+ public void testTestNode()
+ {
+ NodeTest test = new NodeTypeTest(Compiler.NODE_TYPE_TEXT);
+ assertTrue("No a text node", pointer.testNode(test));
+ test = new NodeTypeTest(Compiler.NODE_TYPE_COMMENT);
+ assertFalse("A comment node", pointer.testNode(test));
+ }
+}
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationIteratorAttributes.java (from r635359, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationIteratorAttributes.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationIteratorAttributes.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationIteratorAttributes.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationIteratorAttributes.java&r1=635359&r2=636506&rev=636506&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationIteratorAttributes.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationIteratorAttributes.java Wed Mar 12 13:53:56 2008
@@ -14,21 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.configuration2.tree.xpath;
+package org.apache.commons.configuration2.expr.xpath;
import java.util.List;
import java.util.Locale;
+import org.apache.commons.configuration2.expr.ConfigurationNodeHandler;
import org.apache.commons.configuration2.tree.ConfigurationNode;
import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
-import org.apache.commons.configuration2.tree.xpath.ConfigurationNodeIteratorAttribute;
-import org.apache.commons.configuration2.tree.xpath.ConfigurationNodePointer;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* Test class for ConfigurationIteratorAttributes.
- *
+ *
* @author Oliver Heger
* @version $Id$
*/
@@ -36,18 +35,20 @@
{
/** Constant for the name of another test attribute.*/
private static final String TEST_ATTR = "test";
-
+
/** Stores the node pointer of the test node.*/
- NodePointer pointer;
-
+ private ConfigurationNodePointer<ConfigurationNode> pointer;
+
+ @Override
protected void setUp() throws Exception
{
super.setUp();
-
+
// Adds further attributes to the test node
ConfigurationNode testNode = root.getChild(1);
testNode.addAttribute(new DefaultConfigurationNode(TEST_ATTR, "yes"));
- pointer = new ConfigurationNodePointer(testNode, Locale.getDefault());
+ pointer = new ConfigurationNodePointer<ConfigurationNode>(testNode,
+ new ConfigurationNodeHandler(), Locale.getDefault());
}
/**
@@ -55,39 +56,48 @@
*/
public void testIterateAllAttributes()
{
- ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, "*"));
+ ConfigurationNodeIteratorAttribute<ConfigurationNode> it = new ConfigurationNodeIteratorAttribute<ConfigurationNode>(
+ pointer, new QName(null, "*"));
assertEquals("Wrong number of attributes", 2, iteratorSize(it));
- List attrs = iterationElements(it);
- assertEquals("Wrong first attribute", ATTR_NAME, ((ConfigurationNode) attrs.get(0)).getName());
- assertEquals("Wrong first attribute", TEST_ATTR, ((ConfigurationNode) attrs.get(1)).getName());
+ List<NodePointer> attrs = iterationElements(it);
+ assertEquals("Wrong number of attributes", 2, attrs.size());
+ assertEquals("Wrong 1st attribute", ATTR_NAME, attrs.get(0).getName()
+ .getName());
+ assertEquals("Wrong 2nd attribute", TEST_ATTR, attrs.get(1).getName()
+ .getName());
+ assertEquals("Wrong value of 2nd attribute", "yes", attrs.get(1).getValue());
}
-
+
/**
* Tests to iterate over attributes with a specific name.
*/
public void testIterateSpecificAttribute()
{
- ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, TEST_ATTR));
+ ConfigurationNodeIteratorAttribute<ConfigurationNode> it = new ConfigurationNodeIteratorAttribute<ConfigurationNode>(
+ pointer, new QName(null, TEST_ATTR));
assertEquals("Wrong number of attributes", 1, iteratorSize(it));
- assertEquals("Wrong attribute", TEST_ATTR, ((ConfigurationNode) iterationElements(it).get(0)).getName());
+ assertEquals("Wrong attribute", TEST_ATTR, iterationElements(it).get(0)
+ .getName().getName());
}
-
+
/**
* Tests to iterate over non existing attributes.
*/
public void testIterateUnknownAttribute()
{
- ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, "unknown"));
+ ConfigurationNodeIteratorAttribute<ConfigurationNode> it = new ConfigurationNodeIteratorAttribute<ConfigurationNode>(
+ pointer, new QName(null, "unknown"));
assertEquals("Found attributes", 0, iteratorSize(it));
}
-
+
/**
* Tests iteration when a namespace is specified. This is not supported, so
* the iteration should be empty.
*/
public void testIterateNamespace()
{
- ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName("test", "*"));
+ ConfigurationNodeIteratorAttribute<ConfigurationNode> it = new ConfigurationNodeIteratorAttribute<ConfigurationNode>(
+ pointer, new QName("test", "*"));
assertEquals("Found attributes", 0, iteratorSize(it));
}
}
Copied: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodeIteratorChildren.java (from r635359, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationNodeIteratorChildren.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodeIteratorChildren.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodeIteratorChildren.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationNodeIteratorChildren.java&r1=635359&r2=636506&rev=636506&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationNodeIteratorChildren.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodeIteratorChildren.java Wed Mar 12 13:53:56 2008
@@ -14,16 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.configuration2.tree.xpath;
+package org.apache.commons.configuration2.expr.xpath;
-import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import org.apache.commons.configuration2.expr.ConfigurationNodeHandler;
import org.apache.commons.configuration2.tree.ConfigurationNode;
import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
-import org.apache.commons.configuration2.tree.xpath.ConfigurationNodeIteratorChildren;
-import org.apache.commons.configuration2.tree.xpath.ConfigurationNodePointer;
import org.apache.commons.jxpath.ri.Compiler;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
@@ -42,12 +40,14 @@
public class TestConfigurationNodeIteratorChildren extends AbstractXPathTest
{
/** Stores the node pointer to the root node. */
- NodePointer rootPointer;
+ private ConfigurationNodePointer<ConfigurationNode> rootPointer;
+ @Override
protected void setUp() throws Exception
{
super.setUp();
- rootPointer = new ConfigurationNodePointer(root, Locale.getDefault());
+ rootPointer = new ConfigurationNodePointer<ConfigurationNode>(root,
+ new ConfigurationNodeHandler(), Locale.getDefault());
}
/**
@@ -55,11 +55,10 @@
*/
public void testIterateAllChildren()
{
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, null, false, null);
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, null, false, null);
assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
- checkValues(it, new int[]
- { 1, 2, 3, 4, 5 });
+ checkValues(it, new int[] { 1, 2, 3, 4, 5 });
}
/**
@@ -67,11 +66,10 @@
*/
public void testIterateReverse()
{
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, null, true, null);
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, null, true, null);
assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
- checkValues(it, new int[]
- { 5, 4, 3, 2, 1 });
+ checkValues(it, new int[] { 5, 4, 3, 2, 1 });
}
/**
@@ -80,8 +78,8 @@
public void testIterateWithWildcardTest()
{
NodeNameTest test = new NodeNameTest(new QName(null, "*"));
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, test, false, null);
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, test, false, null);
assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
}
@@ -92,8 +90,8 @@
public void testIterateWithPrefixTest()
{
NodeNameTest test = new NodeNameTest(new QName("prefix", "*"));
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, test, false, null);
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, test, false, null);
assertNull("Undefined node pointer not returned", it.getNodePointer());
assertEquals("Prefix was not evaluated", 0, iteratorSize(it));
}
@@ -104,14 +102,12 @@
public void testIterateWithNameTest()
{
NodeNameTest test = new NodeNameTest(new QName(null, CHILD_NAME2));
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, test, false, null);
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, test, false, null);
assertTrue("No children found", iteratorSize(it) > 0);
- for (Iterator elemIt = iterationElements(it).iterator(); elemIt
- .hasNext();)
+ for (NodePointer np : iterationElements(it))
{
- assertEquals("Wrong child element", CHILD_NAME2,
- ((ConfigurationNode) elemIt.next()).getName());
+ assertEquals("Wrong child element", CHILD_NAME2, np.getName().getName());
}
}
@@ -122,8 +118,8 @@
public void testIterateWithUnknownTest()
{
NodeTest test = new ProcessingInstructionTest("test");
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, test, false, null);
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, test, false, null);
assertEquals("Unknown test was not evaluated", 0, iteratorSize(it));
}
@@ -133,8 +129,8 @@
public void testIterateWithNodeType()
{
NodeTypeTest test = new NodeTypeTest(Compiler.NODE_TYPE_NODE);
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, test, false, null);
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, test, false, null);
assertEquals("Node type not evaluated", CHILD_COUNT, iteratorSize(it));
}
@@ -145,8 +141,8 @@
public void testIterateWithUnknownType()
{
NodeTypeTest test = new NodeTypeTest(Compiler.NODE_TYPE_COMMENT);
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, test, false, null);
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, test, false, null);
assertEquals("Unknown node type not evaluated", 0, iteratorSize(it));
}
@@ -155,18 +151,19 @@
*/
public void testIterateStartsWith()
{
- NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
- root.getChild(2));
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, null, false, childPointer);
+ ConfigurationNodePointer<ConfigurationNode> childPointer =
+ new ConfigurationNodePointer<ConfigurationNode>(rootPointer,
+ root.getChild(2), new ConfigurationNodeHandler());
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, null, false, childPointer);
assertEquals("Wrong start position", 0, it.getPosition());
- List nodes = iterationElements(it);
+ List<NodePointer> nodes = iterationElements(it);
assertEquals("Wrong size of iteration", CHILD_COUNT - 3, nodes.size());
int index = 4;
- for (Iterator it2 = nodes.iterator(); it2.hasNext(); index++)
+ for (NodePointer np : nodes)
{
- ConfigurationNode node = (ConfigurationNode) it2.next();
- assertEquals("Wrong node value", String.valueOf(index), node
+ ConfigurationNode node = (ConfigurationNode) np.getImmediateNode();
+ assertEquals("Wrong node value", String.valueOf(index++), node
.getValue());
}
}
@@ -176,10 +173,11 @@
*/
public void testIterateStartsWithReverse()
{
- NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
- root.getChild(3));
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, null, true, childPointer);
+ ConfigurationNodePointer<ConfigurationNode> childPointer =
+ new ConfigurationNodePointer<ConfigurationNode>(rootPointer,
+ root.getChild(3), new ConfigurationNodeHandler());
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, null, true, childPointer);
int value = 3;
for (int index = 1; it.setPosition(index); index++, value--)
{
@@ -197,10 +195,11 @@
*/
public void testIterateStartsWithInvalid()
{
- NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
- new DefaultConfigurationNode("newNode"));
- ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
- rootPointer, null, false, childPointer);
+ ConfigurationNodePointer<ConfigurationNode> childPointer =
+ new ConfigurationNodePointer<ConfigurationNode>(rootPointer,
+ new DefaultConfigurationNode("newNode"), new ConfigurationNodeHandler());
+ ConfigurationNodeIteratorChildren<ConfigurationNode> it =
+ new ConfigurationNodeIteratorChildren<ConfigurationNode>(rootPointer, null, false, childPointer);
assertEquals("Wrong size of iteration", CHILD_COUNT, iteratorSize(it));
it.setPosition(1);
ConfigurationNode node = (ConfigurationNode) it.getNodePointer()
@@ -219,10 +218,10 @@
*/
private void checkValues(NodeIterator iterator, int[] expectedIndices)
{
- List nodes = iterationElements(iterator);
+ List<NodePointer> nodes = iterationElements(iterator);
for (int i = 0; i < expectedIndices.length; i++)
{
- ConfigurationNode child = (ConfigurationNode) nodes.get(i);
+ ConfigurationNode child = (ConfigurationNode) nodes.get(i).getImmediateNode();
assertTrue("Wrong index value for child " + i, child.getValue()
.toString().endsWith(String.valueOf(expectedIndices[i])));
}
Copied: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodePointer.java (from r635359, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationNodePointer.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodePointer.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodePointer.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationNodePointer.java&r1=635359&r2=636506&rev=636506&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/xpath/TestConfigurationNodePointer.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationNodePointer.java Wed Mar 12 13:53:56 2008
@@ -14,13 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.configuration2.tree.xpath;
+package org.apache.commons.configuration2.expr.xpath;
import java.util.Locale;
+import org.apache.commons.configuration2.expr.ConfigurationNodeHandler;
import org.apache.commons.configuration2.tree.ConfigurationNode;
import org.apache.commons.configuration2.tree.DefaultConfigurationNode;
-import org.apache.commons.configuration2.tree.xpath.ConfigurationNodePointer;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
@@ -34,12 +34,18 @@
public class TestConfigurationNodePointer extends AbstractXPathTest
{
/** Stores the node pointer to be tested. */
- NodePointer pointer;
+ private ConfigurationNodePointer<ConfigurationNode> pointer;
+ /** Stores the node handler to be used for the node pointers.*/
+ private ConfigurationNodeHandler handler;
+
+ @Override
protected void setUp() throws Exception
{
super.setUp();
- pointer = new ConfigurationNodePointer(root, Locale.getDefault());
+ handler = new ConfigurationNodeHandler();
+ pointer = new ConfigurationNodePointer<ConfigurationNode>(root,
+ handler, Locale.getDefault());
}
/**
@@ -47,8 +53,10 @@
*/
public void testCompareChildNodePointersChildren()
{
- NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(1));
- NodePointer p2 = new ConfigurationNodePointer(pointer, root.getChild(3));
+ NodePointer p1 = new ConfigurationNodePointer<ConfigurationNode>(
+ pointer, root.getChild(1), handler);
+ NodePointer p2 = new ConfigurationNodePointer<ConfigurationNode>(
+ pointer, root.getChild(3), handler);
assertEquals("Incorrect order", -1, pointer.compareChildNodePointers(
p1, p2));
assertEquals("Incorrect symmetric order", 1, pointer
@@ -56,34 +64,38 @@
}
/**
- * Tests comparing child node pointers for attribute nodes.
+ * Tests comparing child node pointers for attribute nodes. Attributes are
+ * not taken into account, so result should be 0.
*/
public void testCompareChildNodePointersAttributes()
{
root.addAttribute(new DefaultConfigurationNode("attr1", "test1"));
root.addAttribute(new DefaultConfigurationNode("attr2", "test2"));
- NodePointer p1 = new ConfigurationNodePointer(pointer, root
- .getAttribute(0));
- NodePointer p2 = new ConfigurationNodePointer(pointer, root
- .getAttribute(1));
- assertEquals("Incorrect order", -1, pointer.compareChildNodePointers(
+ NodePointer p1 = new ConfigurationNodePointer<ConfigurationNode>(pointer,
+ root.getAttribute(0), handler);
+ NodePointer p2 = new ConfigurationNodePointer<ConfigurationNode>(pointer,
+ root.getAttribute(1), handler);
+ assertEquals("Incorrect order", 0, pointer.compareChildNodePointers(
p1, p2));
- assertEquals("Incorrect symmetric order", 1, pointer
+ assertEquals("Incorrect symmetric order", 0, pointer
.compareChildNodePointers(p2, p1));
}
/**
- * tests comparing child node pointers for both child and attribute nodes.
+ * Tests comparing child node pointers for both child and attribute nodes.
+ * Attributes are not taken into account, so the child node should be
+ * sorted first.
*/
public void testCompareChildNodePointersChildAndAttribute()
{
root.addAttribute(new DefaultConfigurationNode("attr1", "test1"));
- NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(2));
- NodePointer p2 = new ConfigurationNodePointer(pointer, root
- .getAttribute(0));
- assertEquals("Incorrect order for attributes", 1, pointer
+ NodePointer p1 = new ConfigurationNodePointer<ConfigurationNode>(pointer,
+ root.getChild(2), handler);
+ NodePointer p2 = new ConfigurationNodePointer<ConfigurationNode>(pointer,
+ root.getAttribute(0), handler);
+ assertEquals("Incorrect order for attributes", -1, pointer
.compareChildNodePointers(p1, p2));
- assertEquals("Incorrect symmetric order for attributes", -1, pointer
+ assertEquals("Incorrect symmetric order for attributes", 1, pointer
.compareChildNodePointers(p2, p1));
}
@@ -94,8 +106,10 @@
public void testCompareChildNodePointersInvalidChildren()
{
ConfigurationNode node = root.getChild(1);
- NodePointer p1 = new ConfigurationNodePointer(pointer, node.getChild(1));
- NodePointer p2 = new ConfigurationNodePointer(pointer, node.getChild(3));
+ NodePointer p1 = new ConfigurationNodePointer<ConfigurationNode>(pointer,
+ node.getChild(1), handler);
+ NodePointer p2 = new ConfigurationNodePointer<ConfigurationNode>(pointer,
+ node.getChild(3), handler);
assertEquals("Non child nodes could be sorted", 0, pointer
.compareChildNodePointers(p1, p2));
assertEquals("Non child nodes could be sorted symmetrically", 0,
@@ -103,33 +117,48 @@
}
/**
- * Tests the attribute flag.
+ * Tests the attribute flag. Node pointers of this type never represent
+ * attributes. So the result should always be false.
*/
public void testIsAttribute()
{
ConfigurationNode node = new DefaultConfigurationNode("test", "testval");
- NodePointer p = new ConfigurationNodePointer(pointer, node);
+ NodePointer p = new ConfigurationNodePointer<ConfigurationNode>(pointer, node, handler);
assertFalse("Node is an attribute", p.isAttribute());
node.setAttribute(true);
- assertTrue("Node is no attribute", p.isAttribute());
+ assertFalse("Node is now an attribute", p.isAttribute());
}
/**
- * Tests if leaves in the tree are correctly detected.
+ * Tests isLeaf() for a node with attributes.
*/
- public void testIsLeave()
+ public void testIsLeafAttributes()
{
- assertFalse("Root node is leaf", pointer.isLeaf());
+ ConfigurationNode node = new DefaultConfigurationNode("test");
+ node.addAttribute(new DefaultConfigurationNode("attr", "test"));
+ NodePointer p = new ConfigurationNodePointer<ConfigurationNode>(pointer, node, handler);
+ assertFalse("Node is a leaf", p.isLeaf());
+ }
- NodePointer p = pointer;
- while (!p.isLeaf())
- {
- ConfigurationNode node = (ConfigurationNode) p.getNode();
- assertTrue("Node has no children", node.getChildrenCount() > 0);
- p = new ConfigurationNodePointer(p, node.getChild(0));
- }
- assertTrue("Node has children", ((ConfigurationNode) p.getNode())
- .getChildrenCount() == 0);
+ /**
+ * Tests isLeaf() for a node with children.
+ */
+ public void testIsLeafChildren()
+ {
+ ConfigurationNode node = new DefaultConfigurationNode("test");
+ node.addChild(new DefaultConfigurationNode("child", "test"));
+ NodePointer p = new ConfigurationNodePointer<ConfigurationNode>(pointer, node, handler);
+ assertFalse("Node is a leaf", p.isLeaf());
+ }
+
+ /**
+ * Tests isLeaf() for a real leaf.
+ */
+ public void testIsLeafTrue()
+ {
+ ConfigurationNode node = new DefaultConfigurationNode("test");
+ NodePointer p = new ConfigurationNodePointer<ConfigurationNode>(pointer, node, handler);
+ assertTrue("Node is no leaf", p.isLeaf());
}
/**
@@ -167,9 +196,8 @@
{
NodePointer pattr = it.getNodePointer();
assertTrue("Node pointer is no attribute", pattr.isAttribute());
- assertEquals("Wrong attribute", node.getAttribute(index - 1), pattr
- .getNode());
- checkIterators(pattr);
+ assertEquals("Wrong attribute", node.getAttribute(index - 1).getValue(),
+ pattr.getValue());
}
}
}