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.assertTrue;
21
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.apache.commons.configuration.tree.ConfigurationNode;
26 import org.apache.commons.configuration.tree.DefaultConfigurationNode;
27 import org.apache.commons.jxpath.JXPathContext;
28 import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
29 import org.junit.Before;
30 import org.junit.Test;
31
32
33
34
35
36
37
38
39
40
41
42 public class TestConfigurationNodePointerFactory extends AbstractXPathTest
43 {
44
45 JXPathContext context;
46
47 @Override
48 @Before
49 public void setUp() throws Exception
50 {
51 super.setUp();
52 JXPathContextReferenceImpl
53 .addNodePointerFactory(new ConfigurationNodePointerFactory());
54 context = JXPathContext.newContext(root);
55 context.setLenient(true);
56 }
57
58
59
60
61 @Test
62 public void testSimpleXPath()
63 {
64 List<?> nodes = context.selectNodes(CHILD_NAME1);
65 assertEquals("Incorrect number of results", 2, nodes.size());
66 for (Iterator<?> it = nodes.iterator(); it.hasNext();)
67 {
68 ConfigurationNode node = (ConfigurationNode) it.next();
69 assertEquals("Incorrect node name", CHILD_NAME1, node.getName());
70 assertEquals("Incorrect parent node", root, node.getParentNode());
71 }
72
73 nodes = context.selectNodes("/" + CHILD_NAME1);
74 assertEquals("Incorrect number of results", 2, nodes.size());
75
76 nodes = context.selectNodes(CHILD_NAME2 + "/" + CHILD_NAME1 + "/"
77 + CHILD_NAME2);
78 assertEquals("Incorrect number of results", 18, nodes.size());
79 }
80
81
82
83
84 @Test
85 public void testIndices()
86 {
87 assertEquals("Incorrect value", "1.2.3", context.getValue("/"
88 + CHILD_NAME2 + "[1]/" + CHILD_NAME1 + "[1]/" + CHILD_NAME2
89 + "[2]"));
90 assertEquals("Incorrect value of last node", String
91 .valueOf(CHILD_COUNT), context.getValue(CHILD_NAME2
92 + "[last()]"));
93
94 List<?> nodes = context.selectNodes("/" + CHILD_NAME1 + "[1]/*");
95 assertEquals("Wrong number of children", CHILD_COUNT, nodes.size());
96 int index = 1;
97 for (Iterator<?> it = nodes.iterator(); it.hasNext(); index++)
98 {
99 ConfigurationNode node = (ConfigurationNode) it.next();
100 assertEquals("Wrong node value for child " + index, "2." + index,
101 node.getValue());
102 }
103 }
104
105
106
107
108 @Test
109 public void testAttributes()
110 {
111 root.addAttribute(new DefaultConfigurationNode("testAttr", "true"));
112 assertEquals("Did not find attribute of root node", "true", context
113 .getValue("@testAttr"));
114 assertEquals("Incorrect attribute value", "1", context.getValue("/"
115 + CHILD_NAME2 + "[1]/@" + ATTR_NAME));
116
117 assertTrue("Found elements with name attribute", context.selectNodes(
118 "//" + CHILD_NAME2 + "[@name]").isEmpty());
119 ConfigurationNode node = root.getChild(2).getChild(
120 1).getChildren(CHILD_NAME2).get(1);
121 node.addAttribute(new DefaultConfigurationNode("name", "testValue"));
122 List<?> nodes = context.selectNodes("//" + CHILD_NAME2 + "[@name]");
123 assertEquals("Name attribute not found", 1, nodes.size());
124 assertEquals("Wrong node returned", node, nodes.get(0));
125 }
126
127
128
129
130 @Test
131 public void testText()
132 {
133 List<?> nodes = context.selectNodes("//" + CHILD_NAME2
134 + "[text()='1.1.1']");
135 assertEquals("Incorrect number of result nodes", 1, nodes.size());
136 }
137
138
139
140
141 @Test
142 public void testParentAxis()
143 {
144 List<?> nodes = context.selectNodes("/" + CHILD_NAME2 + "/parent::*");
145 assertEquals("Wrong number of parent nodes", 1, nodes.size());
146 }
147
148
149
150
151 @Test
152 public void testFollowingSiblingAxis()
153 {
154 List<?> nodes = context.selectNodes("/" + CHILD_NAME1
155 + "[2]/following-sibling::*");
156 assertEquals("Wrong number of following siblings", 1, nodes.size());
157 ConfigurationNode node = (ConfigurationNode) nodes.get(0);
158 assertEquals("Wrong node type", CHILD_NAME2, node.getName());
159 assertEquals("Wrong index", String.valueOf(CHILD_COUNT), node
160 .getValue());
161 }
162
163
164
165
166 @Test
167 public void testPrecedingSiblingAxis()
168 {
169 List<?> nodes = context.selectNodes("/" + CHILD_NAME1
170 + "[2]/preceding-sibling::*");
171 assertEquals("Wrong number of preceding siblings", 3, nodes.size());
172 for (int index = 0, value = 3; index < nodes.size(); index++, value--)
173 {
174 assertEquals("Wrong node index", String.valueOf(value),
175 ((ConfigurationNode) nodes.get(index)).getValue());
176 }
177 }
178 }