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
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.apache.commons.configuration2.tree.ImmutableNode;
25 import org.apache.commons.jxpath.JXPathContext;
26 import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
27 import org.junit.jupiter.api.BeforeAll;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34
35 public class TestConfigurationNodePointerFactory extends AbstractXPathTest {
36 @BeforeAll
37 public static void setUpBeforeClass() throws Exception {
38 JXPathContextReferenceImpl.addNodePointerFactory(new ConfigurationNodePointerFactory());
39 }
40
41
42 private JXPathContext context;
43
44 @Override
45 @BeforeEach
46 public void setUp() throws Exception {
47 super.setUp();
48 context = new XPathContextFactory().createContext(root, handler);
49 }
50
51
52
53
54 @Test
55 public void testFollowingSiblingAxis() {
56 final List<?> nodes = context.selectNodes("/" + CHILD_NAME1 + "[2]/following-sibling::*");
57 assertEquals(1, nodes.size());
58 final ImmutableNode node = (ImmutableNode) nodes.get(0);
59 assertEquals(CHILD_NAME2, node.getNodeName());
60 assertEquals(String.valueOf(CHILD_COUNT), node.getValue());
61 }
62
63
64
65
66 @Test
67 public void testIndices() {
68 assertEquals("1.2.3", context.getValue("/" + CHILD_NAME2 + "[1]/" + CHILD_NAME1 + "[1]/" + CHILD_NAME2 + "[2]"));
69 assertEquals(String.valueOf(CHILD_COUNT), context.getValue(CHILD_NAME2 + "[last()]"));
70
71 final List<?> nodes = context.selectNodes("/" + CHILD_NAME1 + "[1]/*");
72 assertEquals(CHILD_COUNT, nodes.size());
73 int index = 1;
74 for (final Iterator<?> it = nodes.iterator(); it.hasNext(); index++) {
75 final ImmutableNode node = (ImmutableNode) it.next();
76 assertEquals("2." + index, node.getValue(), "Wrong node value for child " + index);
77 }
78 }
79
80
81
82
83 @Test
84 public void testParentAxis() {
85 final List<?> nodes = context.selectNodes("/" + CHILD_NAME2 + "/parent::*");
86 assertEquals(1, nodes.size());
87 }
88
89
90
91
92 @Test
93 public void testPrecedingSiblingAxis() {
94 final List<?> nodes = context.selectNodes("/" + CHILD_NAME1 + "[2]/preceding-sibling::*");
95 assertEquals(3, nodes.size());
96 for (int index = 0, value = 3; index < nodes.size(); index++, value--) {
97 assertEquals(String.valueOf(value), ((ImmutableNode) nodes.get(index)).getValue());
98 }
99 }
100
101
102
103
104 @Test
105 public void testQueryAttribute() {
106 assertEquals("1", context.getValue("/" + CHILD_NAME2 + "[1]/@" + ATTR_NAME));
107 }
108
109
110
111
112 @Test
113 public void testQueryRootAttribute() {
114 assertEquals("true", context.getValue("@" + ATTR_ROOT));
115 }
116
117
118
119
120 @Test
121 public void testSimpleXPath() {
122 List<?> results = context.selectNodes(CHILD_NAME1);
123 assertEquals(2, results.size());
124 for (final Object result : results) {
125 final ImmutableNode node = (ImmutableNode) result;
126 assertEquals(CHILD_NAME1, node.getNodeName());
127 }
128
129 results = context.selectNodes("/" + CHILD_NAME1);
130 assertEquals(2, results.size());
131
132 results = context.selectNodes(CHILD_NAME2 + "/" + CHILD_NAME1 + "/" + CHILD_NAME2);
133 assertEquals(18, results.size());
134 }
135
136
137
138
139 @Test
140 public void testText() {
141 final List<?> nodes = context.selectNodes("//" + CHILD_NAME2 + "[text()='1.1.1']");
142 assertEquals(1, nodes.size());
143 }
144 }