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.assertTrue;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.commons.configuration.tree.ConfigurationNode;
26  import org.apache.commons.configuration.tree.DefaultConfigurationNode;
27  import org.apache.commons.jxpath.JXPathContext;
28  import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  /**
33   * Test class for ConfigurationNodePointerFactory. This class does not directly
34   * call the factory's methods, but rather checks if it can be installed in a
35   * {@code JXPathContext} and if XPath expressions can be evaluated.
36   *
37   * @author <a
38   * href="http://commons.apache.org/configuration/team-list.html">Commons
39   * Configuration team</a>
40   * @version $Id: TestConfigurationNodePointerFactory.java 1226104 2011-12-31 15:37:16Z oheger $
41   */
42  public class TestConfigurationNodePointerFactory extends AbstractXPathTest
43  {
44      /** Stores the JXPathContext used for testing. */
45      JXPathContext context;
46  
47      @Override
48      @Before
49      public void setUp() throws Exception
50      {
51          super.setUp();
52          JXPathContextReferenceImpl
53                  .addNodePointerFactory(new ConfigurationNodePointerFactory());
54          context = JXPathContext.newContext(root);
55          context.setLenient(true);
56      }
57  
58      /**
59       * Tests simple XPath expressions.
60       */
61      @Test
62      public void testSimpleXPath()
63      {
64          List<?> nodes = context.selectNodes(CHILD_NAME1);
65          assertEquals("Incorrect number of results", 2, nodes.size());
66          for (Iterator<?> it = nodes.iterator(); it.hasNext();)
67          {
68              ConfigurationNode node = (ConfigurationNode) it.next();
69              assertEquals("Incorrect node name", CHILD_NAME1, node.getName());
70              assertEquals("Incorrect parent node", root, node.getParentNode());
71          }
72  
73          nodes = context.selectNodes("/" + CHILD_NAME1);
74          assertEquals("Incorrect number of results", 2, nodes.size());
75  
76          nodes = context.selectNodes(CHILD_NAME2 + "/" + CHILD_NAME1 + "/"
77                  + CHILD_NAME2);
78          assertEquals("Incorrect number of results", 18, nodes.size());
79      }
80  
81      /**
82       * Tests using indices to specify elements.
83       */
84      @Test
85      public void testIndices()
86      {
87          assertEquals("Incorrect value", "1.2.3", context.getValue("/"
88                  + CHILD_NAME2 + "[1]/" + CHILD_NAME1 + "[1]/" + CHILD_NAME2
89                  + "[2]"));
90          assertEquals("Incorrect value of last node", String
91                  .valueOf(CHILD_COUNT), context.getValue(CHILD_NAME2
92                  + "[last()]"));
93  
94          List<?> nodes = context.selectNodes("/" + CHILD_NAME1 + "[1]/*");
95          assertEquals("Wrong number of children", CHILD_COUNT, nodes.size());
96          int index = 1;
97          for (Iterator<?> it = nodes.iterator(); it.hasNext(); index++)
98          {
99              ConfigurationNode node = (ConfigurationNode) it.next();
100             assertEquals("Wrong node value for child " + index, "2." + index,
101                     node.getValue());
102         }
103     }
104 
105     /**
106      * Tests accessing attributes.
107      */
108     @Test
109     public void testAttributes()
110     {
111         root.addAttribute(new DefaultConfigurationNode("testAttr", "true"));
112         assertEquals("Did not find attribute of root node", "true", context
113                 .getValue("@testAttr"));
114         assertEquals("Incorrect attribute value", "1", context.getValue("/"
115                 + CHILD_NAME2 + "[1]/@" + ATTR_NAME));
116 
117         assertTrue("Found elements with name attribute", context.selectNodes(
118                 "//" + CHILD_NAME2 + "[@name]").isEmpty());
119         ConfigurationNode node = root.getChild(2).getChild(
120                 1).getChildren(CHILD_NAME2).get(1);
121         node.addAttribute(new DefaultConfigurationNode("name", "testValue"));
122         List<?> nodes = context.selectNodes("//" + CHILD_NAME2 + "[@name]");
123         assertEquals("Name attribute not found", 1, nodes.size());
124         assertEquals("Wrong node returned", node, nodes.get(0));
125     }
126 
127     /**
128      * Tests accessing a node's text.
129      */
130     @Test
131     public void testText()
132     {
133         List<?> nodes = context.selectNodes("//" + CHILD_NAME2
134                 + "[text()='1.1.1']");
135         assertEquals("Incorrect number of result nodes", 1, nodes.size());
136     }
137 
138     /**
139      * Tests accessing the parent axis.
140      */
141     @Test
142     public void testParentAxis()
143     {
144         List<?> nodes = context.selectNodes("/" + CHILD_NAME2 + "/parent::*");
145         assertEquals("Wrong number of parent nodes", 1, nodes.size());
146     }
147 
148     /**
149      * Tests accessing the following sibling axis.
150      */
151     @Test
152     public void testFollowingSiblingAxis()
153     {
154         List<?> nodes = context.selectNodes("/" + CHILD_NAME1
155                 + "[2]/following-sibling::*");
156         assertEquals("Wrong number of following siblings", 1, nodes.size());
157         ConfigurationNode node = (ConfigurationNode) nodes.get(0);
158         assertEquals("Wrong node type", CHILD_NAME2, node.getName());
159         assertEquals("Wrong index", String.valueOf(CHILD_COUNT), node
160                 .getValue());
161     }
162 
163     /**
164      * Tests accessing the preceding sibling axis.
165      */
166     @Test
167     public void testPrecedingSiblingAxis()
168     {
169         List<?> nodes = context.selectNodes("/" + CHILD_NAME1
170                 + "[2]/preceding-sibling::*");
171         assertEquals("Wrong number of preceding siblings", 3, nodes.size());
172         for (int index = 0, value = 3; index < nodes.size(); index++, value--)
173         {
174             assertEquals("Wrong node index", String.valueOf(value),
175                     ((ConfigurationNode) nodes.get(index)).getValue());
176         }
177     }
178 }