1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration.tree.xpath;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Locale;
22  
23  import org.apache.commons.configuration.tree.ConfigurationNode;
24  import org.apache.commons.configuration.tree.DefaultConfigurationNode;
25  import org.apache.commons.jxpath.ri.Compiler;
26  import org.apache.commons.jxpath.ri.QName;
27  import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
28  import org.apache.commons.jxpath.ri.compiler.NodeTest;
29  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
30  import org.apache.commons.jxpath.ri.compiler.ProcessingInstructionTest;
31  import org.apache.commons.jxpath.ri.model.NodeIterator;
32  import org.apache.commons.jxpath.ri.model.NodePointer;
33  
34  /***
35   * Test class for ConfigurationNodeIteratorChildren.
36   *
37   * @author Oliver Heger
38   * @version $Id: TestConfigurationNodeIteratorChildren.java 439648 2006-09-02 20:42:10Z oheger $
39   */
40  public class TestConfigurationNodeIteratorChildren extends XPathTest
41  {
42      /*** Stores the node pointer to the root node. */
43      NodePointer rootPointer;
44  
45      protected void setUp() throws Exception
46      {
47          super.setUp();
48          rootPointer = new ConfigurationNodePointer(root, Locale.getDefault());
49      }
50  
51      /***
52       * Tests to iterate over all children of the root node.
53       */
54      public void testIterateAllChildren()
55      {
56          ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
57                  rootPointer, null, false, null);
58          assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
59          checkValues(it, new int[]
60          { 1, 2, 3, 4, 5 });
61      }
62  
63      /***
64       * Tests a reverse iteration.
65       */
66      public void testIterateReverse()
67      {
68          ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
69                  rootPointer, null, true, null);
70          assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
71          checkValues(it, new int[]
72          { 5, 4, 3, 2, 1 });
73      }
74  
75      /***
76       * Tests using a node test with a wildcard name.
77       */
78      public void testIterateWithWildcardTest()
79      {
80          NodeNameTest test = new NodeNameTest(new QName(null, "*"));
81          ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
82                  rootPointer, test, false, null);
83          assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
84      }
85  
86      /***
87       * Tests using a node test that defines a namespace prefix. Because
88       * namespaces are not supported, no elements should be in the iteration.
89       */
90      public void testIterateWithPrefixTest()
91      {
92          NodeNameTest test = new NodeNameTest(new QName("prefix", "*"));
93          ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
94                  rootPointer, test, false, null);
95          assertNull("Undefined node pointer not returned", it.getNodePointer());
96          assertEquals("Prefix was not evaluated", 0, iteratorSize(it));
97      }
98  
99      /***
100      * Tests using a node test that selects a certain sub node name.
101      */
102     public void testIterateWithNameTest()
103     {
104         NodeNameTest test = new NodeNameTest(new QName(null, CHILD_NAME2));
105         ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
106                 rootPointer, test, false, null);
107         assertTrue("No children found", iteratorSize(it) > 0);
108         for (Iterator elemIt = iterationElements(it).iterator(); elemIt
109                 .hasNext();)
110         {
111             assertEquals("Wrong child element", CHILD_NAME2,
112                     ((ConfigurationNode) elemIt.next()).getName());
113         }
114     }
115 
116     /***
117      * Tests using a not supported test class. This should yield an empty
118      * iteration.
119      */
120     public void testIterateWithUnknownTest()
121     {
122         NodeTest test = new ProcessingInstructionTest("test");
123         ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
124                 rootPointer, test, false, null);
125         assertEquals("Unknown test was not evaluated", 0, iteratorSize(it));
126     }
127 
128     /***
129      * Tests using a type test for nodes. This should return all nodes.
130      */
131     public void testIterateWithNodeType()
132     {
133         NodeTypeTest test = new NodeTypeTest(Compiler.NODE_TYPE_NODE);
134         ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
135                 rootPointer, test, false, null);
136         assertEquals("Node type not evaluated", CHILD_COUNT, iteratorSize(it));
137     }
138 
139     /***
140      * Tests using a type test for a non supported type. This should return an
141      * empty iteration.
142      */
143     public void testIterateWithUnknownType()
144     {
145         NodeTypeTest test = new NodeTypeTest(Compiler.NODE_TYPE_COMMENT);
146         ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
147                 rootPointer, test, false, null);
148         assertEquals("Unknown node type not evaluated", 0, iteratorSize(it));
149     }
150 
151     /***
152      * Tests defining a start node for the iteration.
153      */
154     public void testIterateStartsWith()
155     {
156         NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
157                 root.getChild(2));
158         ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
159                 rootPointer, null, false, childPointer);
160         assertEquals("Wrong start position", 0, it.getPosition());
161         List nodes = iterationElements(it);
162         assertEquals("Wrong size of iteration", CHILD_COUNT - 3, nodes.size());
163         int index = 4;
164         for (Iterator it2 = nodes.iterator(); it2.hasNext(); index++)
165         {
166             ConfigurationNode node = (ConfigurationNode) it2.next();
167             assertEquals("Wrong node value", String.valueOf(index), node
168                     .getValue());
169         }
170     }
171 
172     /***
173      * Tests defining a start node for a reverse iteration.
174      */
175     public void testIterateStartsWithReverse()
176     {
177         NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
178                 root.getChild(3));
179         ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
180                 rootPointer, null, true, childPointer);
181         int value = 3;
182         for (int index = 1; it.setPosition(index); index++, value--)
183         {
184             ConfigurationNode node = (ConfigurationNode) it.getNodePointer()
185                     .getNode();
186             assertEquals("Incorrect value at index " + index, String
187                     .valueOf(value), node.getValue());
188         }
189         assertEquals("Iteration ended not at end node", 0, value);
190     }
191 
192     /***
193      * Tests iteration with an invalid start node. This should cause the
194      * iteration to start at the first position.
195      */
196     public void testIterateStartsWithInvalid()
197     {
198         NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
199                 new DefaultConfigurationNode("newNode"));
200         ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
201                 rootPointer, null, false, childPointer);
202         assertEquals("Wrong size of iteration", CHILD_COUNT, iteratorSize(it));
203         it.setPosition(1);
204         ConfigurationNode node = (ConfigurationNode) it.getNodePointer()
205                 .getNode();
206         assertEquals("Wrong start node", "1", node.getValue());
207     }
208 
209     /***
210      * Helper method for checking the values of the nodes returned by an
211      * iterator. Because the values indicate the order of the child nodes with
212      * this test it can be checked whether the nodes were returned in the
213      * correct order.
214      *
215      * @param iterator the iterator
216      * @param expectedIndices an array with the expected indices
217      */
218     private void checkValues(NodeIterator iterator, int[] expectedIndices)
219     {
220         List nodes = iterationElements(iterator);
221         for (int i = 0; i < expectedIndices.length; i++)
222         {
223             ConfigurationNode child = (ConfigurationNode) nodes.get(i);
224             assertTrue("Wrong index value for child " + i, child.getValue()
225                     .toString().endsWith(String.valueOf(expectedIndices[i])));
226         }
227     }
228 }