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