001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.configuration.tree.xpath;
018:
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Locale;
022:
023: import org.apache.commons.configuration.tree.ConfigurationNode;
024: import org.apache.commons.configuration.tree.DefaultConfigurationNode;
025: import org.apache.commons.jxpath.ri.Compiler;
026: import org.apache.commons.jxpath.ri.QName;
027: import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
028: import org.apache.commons.jxpath.ri.compiler.NodeTest;
029: import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
030: import org.apache.commons.jxpath.ri.compiler.ProcessingInstructionTest;
031: import org.apache.commons.jxpath.ri.model.NodeIterator;
032: import org.apache.commons.jxpath.ri.model.NodePointer;
033:
034: /**
035: * Test class for ConfigurationNodeIteratorChildren.
036: *
037: * @author Oliver Heger
038: * @version $Id: TestConfigurationNodeIteratorChildren.java 502705 2007-02-02 19:55:37Z oheger $
039: */
040: public class TestConfigurationNodeIteratorChildren extends
041: AbstractXPathTest {
042: /** Stores the node pointer to the root node. */
043: NodePointer rootPointer;
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047: rootPointer = new ConfigurationNodePointer(root, Locale
048: .getDefault());
049: }
050:
051: /**
052: * Tests to iterate over all children of the root node.
053: */
054: public void testIterateAllChildren() {
055: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
056: rootPointer, null, false, null);
057: assertEquals("Wrong number of elements", CHILD_COUNT,
058: iteratorSize(it));
059: checkValues(it, new int[] { 1, 2, 3, 4, 5 });
060: }
061:
062: /**
063: * Tests a reverse iteration.
064: */
065: public void testIterateReverse() {
066: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
067: rootPointer, null, true, null);
068: assertEquals("Wrong number of elements", CHILD_COUNT,
069: iteratorSize(it));
070: checkValues(it, new int[] { 5, 4, 3, 2, 1 });
071: }
072:
073: /**
074: * Tests using a node test with a wildcard name.
075: */
076: public void testIterateWithWildcardTest() {
077: NodeNameTest test = new NodeNameTest(new QName(null, "*"));
078: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
079: rootPointer, test, false, null);
080: assertEquals("Wrong number of elements", CHILD_COUNT,
081: iteratorSize(it));
082: }
083:
084: /**
085: * Tests using a node test that defines a namespace prefix. Because
086: * namespaces are not supported, no elements should be in the iteration.
087: */
088: public void testIterateWithPrefixTest() {
089: NodeNameTest test = new NodeNameTest(new QName("prefix", "*"));
090: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
091: rootPointer, test, false, null);
092: assertNull("Undefined node pointer not returned", it
093: .getNodePointer());
094: assertEquals("Prefix was not evaluated", 0, iteratorSize(it));
095: }
096:
097: /**
098: * Tests using a node test that selects a certain sub node name.
099: */
100: public void testIterateWithNameTest() {
101: NodeNameTest test = new NodeNameTest(new QName(null,
102: CHILD_NAME2));
103: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
104: rootPointer, test, false, null);
105: assertTrue("No children found", iteratorSize(it) > 0);
106: for (Iterator elemIt = iterationElements(it).iterator(); elemIt
107: .hasNext();) {
108: assertEquals("Wrong child element", CHILD_NAME2,
109: ((ConfigurationNode) elemIt.next()).getName());
110: }
111: }
112:
113: /**
114: * Tests using a not supported test class. This should yield an empty
115: * iteration.
116: */
117: public void testIterateWithUnknownTest() {
118: NodeTest test = new ProcessingInstructionTest("test");
119: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
120: rootPointer, test, false, null);
121: assertEquals("Unknown test was not evaluated", 0,
122: iteratorSize(it));
123: }
124:
125: /**
126: * Tests using a type test for nodes. This should return all nodes.
127: */
128: public void testIterateWithNodeType() {
129: NodeTypeTest test = new NodeTypeTest(Compiler.NODE_TYPE_NODE);
130: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
131: rootPointer, test, false, null);
132: assertEquals("Node type not evaluated", CHILD_COUNT,
133: iteratorSize(it));
134: }
135:
136: /**
137: * Tests using a type test for a non supported type. This should return an
138: * empty iteration.
139: */
140: public void testIterateWithUnknownType() {
141: NodeTypeTest test = new NodeTypeTest(Compiler.NODE_TYPE_COMMENT);
142: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
143: rootPointer, test, false, null);
144: assertEquals("Unknown node type not evaluated", 0,
145: iteratorSize(it));
146: }
147:
148: /**
149: * Tests defining a start node for the iteration.
150: */
151: public void testIterateStartsWith() {
152: NodePointer childPointer = new ConfigurationNodePointer(
153: rootPointer, root.getChild(2));
154: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
155: rootPointer, null, false, childPointer);
156: assertEquals("Wrong start position", 0, it.getPosition());
157: List nodes = iterationElements(it);
158: assertEquals("Wrong size of iteration", CHILD_COUNT - 3, nodes
159: .size());
160: int index = 4;
161: for (Iterator it2 = nodes.iterator(); it2.hasNext(); index++) {
162: ConfigurationNode node = (ConfigurationNode) it2.next();
163: assertEquals("Wrong node value", String.valueOf(index),
164: node.getValue());
165: }
166: }
167:
168: /**
169: * Tests defining a start node for a reverse iteration.
170: */
171: public void testIterateStartsWithReverse() {
172: NodePointer childPointer = new ConfigurationNodePointer(
173: rootPointer, root.getChild(3));
174: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
175: rootPointer, null, true, childPointer);
176: int value = 3;
177: for (int index = 1; it.setPosition(index); index++, value--) {
178: ConfigurationNode node = (ConfigurationNode) it
179: .getNodePointer().getNode();
180: assertEquals("Incorrect value at index " + index, String
181: .valueOf(value), node.getValue());
182: }
183: assertEquals("Iteration ended not at end node", 0, value);
184: }
185:
186: /**
187: * Tests iteration with an invalid start node. This should cause the
188: * iteration to start at the first position.
189: */
190: public void testIterateStartsWithInvalid() {
191: NodePointer childPointer = new ConfigurationNodePointer(
192: rootPointer, new DefaultConfigurationNode("newNode"));
193: ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
194: rootPointer, null, false, childPointer);
195: assertEquals("Wrong size of iteration", CHILD_COUNT,
196: iteratorSize(it));
197: it.setPosition(1);
198: ConfigurationNode node = (ConfigurationNode) it
199: .getNodePointer().getNode();
200: assertEquals("Wrong start node", "1", node.getValue());
201: }
202:
203: /**
204: * Helper method for checking the values of the nodes returned by an
205: * iterator. Because the values indicate the order of the child nodes with
206: * this test it can be checked whether the nodes were returned in the
207: * correct order.
208: *
209: * @param iterator the iterator
210: * @param expectedIndices an array with the expected indices
211: */
212: private void checkValues(NodeIterator iterator,
213: int[] expectedIndices) {
214: List nodes = iterationElements(iterator);
215: for (int i = 0; i < expectedIndices.length; i++) {
216: ConfigurationNode child = (ConfigurationNode) nodes.get(i);
217: assertTrue("Wrong index value for child " + i, child
218: .getValue().toString().endsWith(
219: String.valueOf(expectedIndices[i])));
220: }
221: }
222: }
|