1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.tree.xpath;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Locale;
26
27 import org.apache.commons.configuration.tree.ConfigurationNode;
28 import org.apache.commons.configuration.tree.DefaultConfigurationNode;
29 import org.apache.commons.jxpath.ri.Compiler;
30 import org.apache.commons.jxpath.ri.QName;
31 import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
32 import org.apache.commons.jxpath.ri.compiler.NodeTest;
33 import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
34 import org.apache.commons.jxpath.ri.compiler.ProcessingInstructionTest;
35 import org.apache.commons.jxpath.ri.model.NodeIterator;
36 import org.apache.commons.jxpath.ri.model.NodePointer;
37 import org.junit.Before;
38 import org.junit.Test;
39
40
41
42
43
44
45
46
47
48 public class TestConfigurationNodeIteratorChildren extends AbstractXPathTest
49 {
50
51 NodePointer rootPointer;
52
53 @Override
54 @Before
55 public void setUp() throws Exception
56 {
57 super.setUp();
58 rootPointer = new ConfigurationNodePointer(root, Locale.getDefault());
59 }
60
61
62
63
64 @Test
65 public void testIterateAllChildren()
66 {
67 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
68 rootPointer, null, false, null);
69 assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
70 checkValues(it, new int[]
71 { 1, 2, 3, 4, 5 });
72 }
73
74
75
76
77 @Test
78 public void testIterateReverse()
79 {
80 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
81 rootPointer, null, true, null);
82 assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
83 checkValues(it, new int[]
84 { 5, 4, 3, 2, 1 });
85 }
86
87
88
89
90 @Test
91 public void testIterateWithWildcardTest()
92 {
93 NodeNameTest test = new NodeNameTest(new QName(null, "*"));
94 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
95 rootPointer, test, false, null);
96 assertEquals("Wrong number of elements", CHILD_COUNT, iteratorSize(it));
97 }
98
99
100
101
102
103 @Test
104 public void testIterateWithPrefixTest()
105 {
106 NodeNameTest test = new NodeNameTest(new QName("prefix", "*"));
107 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
108 rootPointer, test, false, null);
109 assertNull("Undefined node pointer not returned", it.getNodePointer());
110 assertEquals("Prefix was not evaluated", 0, iteratorSize(it));
111 }
112
113
114
115
116 @Test
117 public void testIterateWithNameTest()
118 {
119 NodeNameTest test = new NodeNameTest(new QName(null, CHILD_NAME2));
120 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
121 rootPointer, test, false, null);
122 assertTrue("No children found", iteratorSize(it) > 0);
123 for (ConfigurationNode nd : iterationElements(it))
124 {
125 assertEquals("Wrong child element", CHILD_NAME2, nd.getName());
126 }
127 }
128
129
130
131
132
133 @Test
134 public void testIterateWithUnknownTest()
135 {
136 NodeTest test = new ProcessingInstructionTest("test");
137 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
138 rootPointer, test, false, null);
139 assertEquals("Unknown test was not evaluated", 0, iteratorSize(it));
140 }
141
142
143
144
145 @Test
146 public void testIterateWithNodeType()
147 {
148 NodeTypeTest test = new NodeTypeTest(Compiler.NODE_TYPE_NODE);
149 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
150 rootPointer, test, false, null);
151 assertEquals("Node type not evaluated", CHILD_COUNT, iteratorSize(it));
152 }
153
154
155
156
157
158 @Test
159 public void testIterateWithUnknownType()
160 {
161 NodeTypeTest test = new NodeTypeTest(Compiler.NODE_TYPE_COMMENT);
162 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
163 rootPointer, test, false, null);
164 assertEquals("Unknown node type not evaluated", 0, iteratorSize(it));
165 }
166
167
168
169
170 @Test
171 public void testIterateStartsWith()
172 {
173 NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
174 root.getChild(2));
175 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
176 rootPointer, null, false, childPointer);
177 assertEquals("Wrong start position", 0, it.getPosition());
178 List<ConfigurationNode> nodes = iterationElements(it);
179 assertEquals("Wrong size of iteration", CHILD_COUNT - 3, nodes.size());
180 int index = 4;
181 for (Iterator<ConfigurationNode> it2 = nodes.iterator(); it2.hasNext(); index++)
182 {
183 ConfigurationNode node = it2.next();
184 assertEquals("Wrong node value", String.valueOf(index), node
185 .getValue());
186 }
187 }
188
189
190
191
192 @Test
193 public void testIterateStartsWithReverse()
194 {
195 NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
196 root.getChild(3));
197 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
198 rootPointer, null, true, childPointer);
199 int value = 3;
200 for (int index = 1; it.setPosition(index); index++, value--)
201 {
202 ConfigurationNode node = (ConfigurationNode) it.getNodePointer()
203 .getNode();
204 assertEquals("Incorrect value at index " + index, String
205 .valueOf(value), node.getValue());
206 }
207 assertEquals("Iteration ended not at end node", 0, value);
208 }
209
210
211
212
213
214 @Test
215 public void testIterateStartsWithInvalid()
216 {
217 NodePointer childPointer = new ConfigurationNodePointer(rootPointer,
218 new DefaultConfigurationNode("newNode"));
219 ConfigurationNodeIteratorChildren it = new ConfigurationNodeIteratorChildren(
220 rootPointer, null, false, childPointer);
221 assertEquals("Wrong size of iteration", CHILD_COUNT, iteratorSize(it));
222 it.setPosition(1);
223 ConfigurationNode node = (ConfigurationNode) it.getNodePointer()
224 .getNode();
225 assertEquals("Wrong start node", "1", node.getValue());
226 }
227
228
229
230
231
232
233
234
235
236
237 private void checkValues(NodeIterator iterator, int[] expectedIndices)
238 {
239 List<ConfigurationNode> nodes = iterationElements(iterator);
240 for (int i = 0; i < expectedIndices.length; i++)
241 {
242 ConfigurationNode child = nodes.get(i);
243 assertTrue("Wrong index value for child " + i, child.getValue()
244 .toString().endsWith(String.valueOf(expectedIndices[i])));
245 }
246 }
247 }