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.configuration.tree.xpath;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.Locale;
24  
25  import org.apache.commons.configuration.tree.ConfigurationNode;
26  import org.apache.commons.configuration.tree.DefaultConfigurationNode;
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.Before;
31  import org.junit.Test;
32  
33  /**
34   * Test class for ConfigurationNodePointer.
35   *
36   * @author <a
37   * href="http://commons.apache.org/configuration/team-list.html">Commons
38   * Configuration team</a>
39   * @version $Id: TestConfigurationNodePointer.java 1226104 2011-12-31 15:37:16Z oheger $
40   */
41  public class TestConfigurationNodePointer extends AbstractXPathTest
42  {
43      /** Stores the node pointer to be tested. */
44      NodePointer pointer;
45  
46      @Override
47      @Before
48      public void setUp() throws Exception
49      {
50          super.setUp();
51          pointer = new ConfigurationNodePointer(root, Locale.getDefault());
52      }
53  
54      /**
55       * Tests comparing child node pointers for child nodes.
56       */
57      @Test
58      public void testCompareChildNodePointersChildren()
59      {
60          NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(1));
61          NodePointer p2 = new ConfigurationNodePointer(pointer, root.getChild(3));
62          assertEquals("Incorrect order", -1, pointer.compareChildNodePointers(
63                  p1, p2));
64          assertEquals("Incorrect symmetric order", 1, pointer
65                  .compareChildNodePointers(p2, p1));
66      }
67  
68      /**
69       * Tests comparing child node pointers for attribute nodes.
70       */
71      @Test
72      public void testCompareChildNodePointersAttributes()
73      {
74          root.addAttribute(new DefaultConfigurationNode("attr1", "test1"));
75          root.addAttribute(new DefaultConfigurationNode("attr2", "test2"));
76          NodePointer p1 = new ConfigurationNodePointer(pointer, root
77                  .getAttribute(0));
78          NodePointer p2 = new ConfigurationNodePointer(pointer, root
79                  .getAttribute(1));
80          assertEquals("Incorrect order", -1, pointer.compareChildNodePointers(
81                  p1, p2));
82          assertEquals("Incorrect symmetric order", 1, pointer
83                  .compareChildNodePointers(p2, p1));
84      }
85  
86      /**
87       * tests comparing child node pointers for both child and attribute nodes.
88       */
89      @Test
90      public void testCompareChildNodePointersChildAndAttribute()
91      {
92          root.addAttribute(new DefaultConfigurationNode("attr1", "test1"));
93          NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(2));
94          NodePointer p2 = new ConfigurationNodePointer(pointer, root
95                  .getAttribute(0));
96          assertEquals("Incorrect order for attributes", 1, pointer
97                  .compareChildNodePointers(p1, p2));
98          assertEquals("Incorrect symmetric order for attributes", -1, pointer
99                  .compareChildNodePointers(p2, p1));
100     }
101 
102     /**
103      * Tests comparing child node pointers for child nodes that do not belong to
104      * the parent node.
105      */
106     @Test
107     public void testCompareChildNodePointersInvalidChildren()
108     {
109         ConfigurationNode node = root.getChild(1);
110         NodePointer p1 = new ConfigurationNodePointer(pointer, node.getChild(1));
111         NodePointer p2 = new ConfigurationNodePointer(pointer, node.getChild(3));
112         assertEquals("Non child nodes could be sorted", 0, pointer
113                 .compareChildNodePointers(p1, p2));
114         assertEquals("Non child nodes could be sorted symmetrically", 0,
115                 pointer.compareChildNodePointers(p2, p1));
116     }
117 
118     /**
119      * Tests the attribute flag.
120      */
121     @Test
122     public void testIsAttribute()
123     {
124         ConfigurationNode node = new DefaultConfigurationNode("test", "testval");
125         NodePointer p = new ConfigurationNodePointer(pointer, node);
126         assertFalse("Node is an attribute", p.isAttribute());
127         node.setAttribute(true);
128         assertTrue("Node is no attribute", p.isAttribute());
129     }
130 
131     /**
132      * Tests if leaves in the tree are correctly detected.
133      */
134     @Test
135     public void testIsLeave()
136     {
137         assertFalse("Root node is leaf", pointer.isLeaf());
138 
139         NodePointer p = pointer;
140         while (!p.isLeaf())
141         {
142             ConfigurationNode node = (ConfigurationNode) p.getNode();
143             assertTrue("Node has no children", node.getChildrenCount() > 0);
144             p = new ConfigurationNodePointer(p, node.getChild(0));
145         }
146         assertTrue("Node has children", ((ConfigurationNode) p.getNode())
147                 .getChildrenCount() == 0);
148     }
149 
150     /**
151      * Tests the iterators returned by the node pointer.
152      */
153     @Test
154     public void testIterators()
155     {
156         checkIterators(pointer);
157     }
158 
159     /**
160      * Recursive helper method for testing the returned iterators.
161      *
162      * @param p the node pointer to test
163      */
164     private void checkIterators(NodePointer p)
165     {
166         ConfigurationNode node = (ConfigurationNode) p.getNode();
167         NodeIterator it = p.childIterator(null, false, null);
168         assertEquals("Iterator count differs from children count", node
169                 .getChildrenCount(), iteratorSize(it));
170 
171         for (int index = 1; it.setPosition(index); index++)
172         {
173             NodePointer pchild = it.getNodePointer();
174             assertEquals("Wrong child", node.getChild(index - 1), pchild
175                     .getNode());
176             checkIterators(pchild);
177         }
178 
179         it = p.attributeIterator(new QName(null, "*"));
180         assertEquals("Iterator count differs from attribute count", node
181                 .getAttributeCount(), iteratorSize(it));
182         for (int index = 1; it.setPosition(index); index++)
183         {
184             NodePointer pattr = it.getNodePointer();
185             assertTrue("Node pointer is no attribute", pattr.isAttribute());
186             assertEquals("Wrong attribute", node.getAttribute(index - 1), pattr
187                     .getNode());
188             checkIterators(pattr);
189         }
190     }
191 }