1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.tree.xpath;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Locale;
25
26 import org.apache.commons.configuration2.tree.ImmutableNode;
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.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 public class TestConfigurationNodePointer extends AbstractXPathTest {
37
38
39 private ConfigurationNodePointer<ImmutableNode> pointer;
40
41
42
43
44
45
46 private void checkIterators(final NodePointer p) {
47 final ImmutableNode node = (ImmutableNode) p.getNode();
48 NodeIterator it = p.childIterator(null, false, null);
49 assertEquals(node.getChildren().size(), iteratorSize(it));
50
51 for (int index = 1; it.setPosition(index); index++) {
52 final NodePointer pchild = it.getNodePointer();
53 assertEquals(node.getChildren().get(index - 1), pchild.getNode());
54 checkIterators(pchild);
55 }
56
57 it = p.attributeIterator(new QName(null, "*"));
58 assertEquals(node.getAttributes().size(), iteratorSize(it));
59 for (int index = 1; it.setPosition(index); index++) {
60 final NodePointer pattr = it.getNodePointer();
61 assertTrue(pattr.isAttribute());
62 assertTrue(node.getAttributes().containsKey(pattr.getName().getName()));
63 }
64 }
65
66 @Override
67 @BeforeEach
68 public void setUp() throws Exception {
69 super.setUp();
70 pointer = new ConfigurationNodePointer<>(root, Locale.getDefault(), handler);
71 }
72
73
74
75
76
77 @Test
78 void testCompareChildNodePointersAttributes() {
79 final ImmutableNode n1 = new ImmutableNode.Builder().name("n1").create();
80 final ImmutableNode n2 = new ImmutableNode.Builder().name("n2").create();
81 final NodePointer p1 = new ConfigurationNodePointer<>(pointer, n1, handler);
82 final NodePointer p2 = new ConfigurationNodePointer<>(pointer, n2, handler);
83 assertEquals(0, pointer.compareChildNodePointers(p1, p2));
84 assertEquals(0, pointer.compareChildNodePointers(p2, p1));
85 }
86
87
88
89
90 @Test
91 void testCompareChildNodePointersChildren() {
92 final NodePointer p1 = new ConfigurationNodePointer<>(pointer, root.getChildren().get(1), handler);
93 final NodePointer p2 = new ConfigurationNodePointer<>(pointer, root.getChildren().get(3), handler);
94 assertEquals(-1, pointer.compareChildNodePointers(p1, p2));
95 assertEquals(1, pointer.compareChildNodePointers(p2, p1));
96 }
97
98
99
100
101 @Test
102 void testIsAttribute() {
103 assertFalse(pointer.isAttribute());
104 }
105
106
107
108
109 @Test
110 void testIsLeafTrue() {
111 final ImmutableNode leafNode = new ImmutableNode.Builder().name("leafNode").create();
112 pointer = new ConfigurationNodePointer<>(pointer, leafNode, handler);
113 assertTrue(pointer.isLeaf());
114 }
115
116
117
118
119 @Test
120 void testIsLeave() {
121 assertFalse(pointer.isLeaf());
122 }
123
124
125
126
127 @Test
128 void testIterators() {
129 checkIterators(pointer);
130 }
131
132
133
134
135 @Test
136 void testSetValue() {
137 assertThrows(UnsupportedOperationException.class, () -> pointer.setValue("newValue"));
138 }
139 }