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.assertFalse;
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.Locale;
24
25 import org.apache.commons.configuration.tree.ConfigurationNode;
26 import org.apache.commons.configuration.tree.DefaultConfigurationNode;
27 import org.apache.commons.jxpath.ri.QName;
28 import org.apache.commons.jxpath.ri.model.NodeIterator;
29 import org.apache.commons.jxpath.ri.model.NodePointer;
30 import org.junit.Before;
31 import org.junit.Test;
32
33
34
35
36
37
38
39
40
41 public class TestConfigurationNodePointer extends AbstractXPathTest
42 {
43
44 NodePointer pointer;
45
46 @Override
47 @Before
48 public void setUp() throws Exception
49 {
50 super.setUp();
51 pointer = new ConfigurationNodePointer(root, Locale.getDefault());
52 }
53
54
55
56
57 @Test
58 public void testCompareChildNodePointersChildren()
59 {
60 NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(1));
61 NodePointer p2 = new ConfigurationNodePointer(pointer, root.getChild(3));
62 assertEquals("Incorrect order", -1, pointer.compareChildNodePointers(
63 p1, p2));
64 assertEquals("Incorrect symmetric order", 1, pointer
65 .compareChildNodePointers(p2, p1));
66 }
67
68
69
70
71 @Test
72 public void testCompareChildNodePointersAttributes()
73 {
74 root.addAttribute(new DefaultConfigurationNode("attr1", "test1"));
75 root.addAttribute(new DefaultConfigurationNode("attr2", "test2"));
76 NodePointer p1 = new ConfigurationNodePointer(pointer, root
77 .getAttribute(0));
78 NodePointer p2 = new ConfigurationNodePointer(pointer, root
79 .getAttribute(1));
80 assertEquals("Incorrect order", -1, pointer.compareChildNodePointers(
81 p1, p2));
82 assertEquals("Incorrect symmetric order", 1, pointer
83 .compareChildNodePointers(p2, p1));
84 }
85
86
87
88
89 @Test
90 public void testCompareChildNodePointersChildAndAttribute()
91 {
92 root.addAttribute(new DefaultConfigurationNode("attr1", "test1"));
93 NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(2));
94 NodePointer p2 = new ConfigurationNodePointer(pointer, root
95 .getAttribute(0));
96 assertEquals("Incorrect order for attributes", 1, pointer
97 .compareChildNodePointers(p1, p2));
98 assertEquals("Incorrect symmetric order for attributes", -1, pointer
99 .compareChildNodePointers(p2, p1));
100 }
101
102
103
104
105
106 @Test
107 public void testCompareChildNodePointersInvalidChildren()
108 {
109 ConfigurationNode node = root.getChild(1);
110 NodePointer p1 = new ConfigurationNodePointer(pointer, node.getChild(1));
111 NodePointer p2 = new ConfigurationNodePointer(pointer, node.getChild(3));
112 assertEquals("Non child nodes could be sorted", 0, pointer
113 .compareChildNodePointers(p1, p2));
114 assertEquals("Non child nodes could be sorted symmetrically", 0,
115 pointer.compareChildNodePointers(p2, p1));
116 }
117
118
119
120
121 @Test
122 public void testIsAttribute()
123 {
124 ConfigurationNode node = new DefaultConfigurationNode("test", "testval");
125 NodePointer p = new ConfigurationNodePointer(pointer, node);
126 assertFalse("Node is an attribute", p.isAttribute());
127 node.setAttribute(true);
128 assertTrue("Node is no attribute", p.isAttribute());
129 }
130
131
132
133
134 @Test
135 public void testIsLeave()
136 {
137 assertFalse("Root node is leaf", pointer.isLeaf());
138
139 NodePointer p = pointer;
140 while (!p.isLeaf())
141 {
142 ConfigurationNode node = (ConfigurationNode) p.getNode();
143 assertTrue("Node has no children", node.getChildrenCount() > 0);
144 p = new ConfigurationNodePointer(p, node.getChild(0));
145 }
146 assertTrue("Node has children", ((ConfigurationNode) p.getNode())
147 .getChildrenCount() == 0);
148 }
149
150
151
152
153 @Test
154 public void testIterators()
155 {
156 checkIterators(pointer);
157 }
158
159
160
161
162
163
164 private void checkIterators(NodePointer p)
165 {
166 ConfigurationNode node = (ConfigurationNode) p.getNode();
167 NodeIterator it = p.childIterator(null, false, null);
168 assertEquals("Iterator count differs from children count", node
169 .getChildrenCount(), iteratorSize(it));
170
171 for (int index = 1; it.setPosition(index); index++)
172 {
173 NodePointer pchild = it.getNodePointer();
174 assertEquals("Wrong child", node.getChild(index - 1), pchild
175 .getNode());
176 checkIterators(pchild);
177 }
178
179 it = p.attributeIterator(new QName(null, "*"));
180 assertEquals("Iterator count differs from attribute count", node
181 .getAttributeCount(), iteratorSize(it));
182 for (int index = 1; it.setPosition(index); index++)
183 {
184 NodePointer pattr = it.getNodePointer();
185 assertTrue("Node pointer is no attribute", pattr.isAttribute());
186 assertEquals("Wrong attribute", node.getAttribute(index - 1), pattr
187 .getNode());
188 checkIterators(pattr);
189 }
190 }
191 }