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