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 private ConfigurationNodePointer<ImmutableNode> pointer;
39
40
41
42
43
44
45 private void checkIterators(final NodePointer p) {
46 final ImmutableNode node = (ImmutableNode) p.getNode();
47 NodeIterator it = p.childIterator(null, false, null);
48 assertEquals(node.getChildren().size(), iteratorSize(it));
49
50 for (int index = 1; it.setPosition(index); index++) {
51 final NodePointer pchild = it.getNodePointer();
52 assertEquals(node.getChildren().get(index - 1), pchild.getNode());
53 checkIterators(pchild);
54 }
55
56 it = p.attributeIterator(new QName(null, "*"));
57 assertEquals(node.getAttributes().size(), iteratorSize(it));
58 for (int index = 1; it.setPosition(index); index++) {
59 final NodePointer pattr = it.getNodePointer();
60 assertTrue(pattr.isAttribute());
61 assertTrue(node.getAttributes().containsKey(pattr.getName().getName()));
62 }
63 }
64
65 @Override
66 @BeforeEach
67 public void setUp() throws Exception {
68 super.setUp();
69 pointer = new ConfigurationNodePointer<>(root, Locale.getDefault(), handler);
70 }
71
72
73
74
75
76 @Test
77 public void testCompareChildNodePointersAttributes() {
78 final ImmutableNode n1 = new ImmutableNode.Builder().name("n1").create();
79 final ImmutableNode n2 = new ImmutableNode.Builder().name("n2").create();
80 final NodePointer p1 = new ConfigurationNodePointer<>(pointer, n1, handler);
81 final NodePointer p2 = new ConfigurationNodePointer<>(pointer, n2, handler);
82 assertEquals(0, pointer.compareChildNodePointers(p1, p2));
83 assertEquals(0, pointer.compareChildNodePointers(p2, p1));
84 }
85
86
87
88
89 @Test
90 public void testCompareChildNodePointersChildren() {
91 final NodePointer p1 = new ConfigurationNodePointer<>(pointer, root.getChildren().get(1), handler);
92 final NodePointer p2 = new ConfigurationNodePointer<>(pointer, root.getChildren().get(3), handler);
93 assertEquals(-1, pointer.compareChildNodePointers(p1, p2));
94 assertEquals(1, pointer.compareChildNodePointers(p2, p1));
95 }
96
97
98
99
100 @Test
101 public void testIsAttribute() {
102 assertFalse(pointer.isAttribute());
103 }
104
105
106
107
108 @Test
109 public void testIsLeafTrue() {
110 final ImmutableNode leafNode = new ImmutableNode.Builder().name("leafNode").create();
111 pointer = new ConfigurationNodePointer<>(pointer, leafNode, handler);
112 assertTrue(pointer.isLeaf());
113 }
114
115
116
117
118 @Test
119 public void testIsLeave() {
120 assertFalse(pointer.isLeaf());
121 }
122
123
124
125
126 @Test
127 public void testIterators() {
128 checkIterators(pointer);
129 }
130
131
132
133
134 @Test
135 public void testSetValue() {
136 assertThrows(UnsupportedOperationException.class, () -> pointer.setValue("newValue"));
137 }
138 }