View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test class for ConfigurationNodePointerFactory. This class does not directly call the factory's methods, but rather
33   * checks if it can be installed in a {@code JXPathContext} and if XPath expressions can be evaluated.
34   */
35  public class TestConfigurationNodePointerFactory extends AbstractXPathTest {
36      @BeforeAll
37      public static void setUpBeforeClass() throws Exception {
38          JXPathContextReferenceImpl.addNodePointerFactory(new ConfigurationNodePointerFactory());
39      }
40  
41      /** Stores the JXPathContext used for testing. */
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       * Tests accessing the following sibling axis.
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       * Tests using indices to specify elements.
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       * Tests accessing the parent axis.
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       * Tests accessing the preceding sibling axis.
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      * Tests whether the attribute of a node can be queried.
103      */
104     @Test
105     public void testQueryAttribute() {
106         assertEquals("1", context.getValue("/" + CHILD_NAME2 + "[1]/@" + ATTR_NAME));
107     }
108 
109     /**
110      * Tests whether an attribute of the root node can be queried.
111      */
112     @Test
113     public void testQueryRootAttribute() {
114         assertEquals("true", context.getValue("@" + ATTR_ROOT));
115     }
116 
117     /**
118      * Tests simple XPath expressions.
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      * Tests accessing a node's text.
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 }