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    *     https://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  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   * Test class for {@code ConfigurationNodePointer}.
35   */
36  public class TestConfigurationNodePointer extends AbstractXPathTest {
37  
38      /** Stores the node pointer to be tested. */
39      private ConfigurationNodePointer<ImmutableNode> pointer;
40  
41      /**
42       * Recursive helper method for testing the returned iterators.
43       *
44       * @param p the node pointer to test
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       * Tests whether a comparison of child node pointers handle the case that the child nodes are unknown. (This should not
75       * happen in practice.)
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       * Tests comparing child node pointers for child nodes.
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       * Tests the attribute flag.
100      */
101     @Test
102     void testIsAttribute() {
103         assertFalse(pointer.isAttribute());
104     }
105 
106     /**
107      * Tests the leaf flag for a real leaf node.
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      * Tests if leaves in the tree are correctly detected.
118      */
119     @Test
120     void testIsLeave() {
121         assertFalse(pointer.isLeaf());
122     }
123 
124     /**
125      * Tests the iterators returned by the node pointer.
126      */
127     @Test
128     void testIterators() {
129         checkIterators(pointer);
130     }
131 
132     /**
133      * Tests that no new value can be set.
134      */
135     @Test
136     void testSetValue() {
137         assertThrows(UnsupportedOperationException.class, () -> pointer.setValue("newValue"));
138     }
139 }