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