    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;
    18
    19  import junit.framework.TestCase;
    20
    21  /***
    22   * Test class for ViewNode.
    23   *
    24   * @version $Id: TestViewNode.java 439648 2006-09-02 20:42:10Z oheger $
    25   */
    26  public class TestViewNode extends TestCase
    27  {
    28      /*** Stores the view node to be tested. */
    29      ViewNode viewNode;
    30
    31      /*** Stores a regular node. */
    32      ConfigurationNode node;
    33
    34      /*** A child node of the regular node. */
    35      ConfigurationNode child;
    36
    37      /*** An attribute node of the regular node. */
    38      ConfigurationNode attr;
    39
    40      protected void setUp() throws Exception
    41      {
    42          super.setUp();
    43          node = new DefaultConfigurationNode();
    44          child = new DefaultConfigurationNode("child");
    45          attr = new DefaultConfigurationNode("attr");
    46          node.addChild(child);
    47          node.addAttribute(attr);
    48          viewNode = new ViewNode();
    49      }
    50
    51      /***
    52       * Tests adding a child to the view node.
    53       */
    54      public void testAddChild()
    55      {
    56          viewNode.addChild(child);
    57          assertEquals("Parent was changed", node, child.getParentNode());
    58          assertEquals("Child was not added", 1, viewNode.getChildrenCount());
    59      }
    60
    61      /***
    62       * Tests adding a null child to the view node. This should throw an
    63       * exception.
    64       */
    65      public void testAddNullChild()
    66      {
    67          try
    68          {
    69              viewNode.addChild(null);
    70              fail("Could add null child!");
    71          }
    72          catch (IllegalArgumentException iex)
    73          {
    74              // ok
    75          }
    76      }
    77
    78      /***
    79       * Tests adding an attribute to the view node.
    80       */
    81      public void testAddAttribute()
    82      {
    83          viewNode.addAttribute(attr);
    84          assertEquals("Parent was changed", node, attr.getParentNode());
    85          assertEquals("Attribute was not added", 1, viewNode.getAttributeCount());
    86      }
    87
    88      /***
    89       * Tests adding a null attribute to the view node. This should cause an
    90       * exception.
    91       */
    92      public void testAddNullAttribute()
    93      {
    94          try
    95          {
    96              viewNode.addAttribute(null);
    97              fail("Could add null attribute");
    98          }
    99          catch (IllegalArgumentException iex)
    100         {
    101             // ok
    102         }
    103     }
    104
    105     /***
    106      * Tests appending all children to a view node.
    107      */
    108     public void testAppendChildren()
    109     {
    110         viewNode.addChild(new DefaultConfigurationNode("testNode"));
    111         viewNode.appendChildren(node);
    112         assertEquals("Wrong number of children", 2, viewNode.getChildrenCount());
    113         assertEquals("Cannot find child", child, viewNode.getChild(1));
    114         assertEquals("Parent was changed", node, ((ConfigurationNode) viewNode
    115                 .getChild(1)).getParentNode());
    116     }
    117
    118     /***
    119      * Tests appending children from a null source. This should be a noop.
    120      */
    121     public void testAppendNullChildren()
    122     {
    123         viewNode.appendChildren(null);
    124         assertEquals("Wrong number of children", 0, viewNode.getChildrenCount());
    125     }
    126
    127     /***
    128      * tests appending all attributes to a view node.
    129      */
    130     public void testAppendAttributes()
    131     {
    132         viewNode.appendAttributes(node);
    133         assertEquals("Wrong number of attributes", 1, viewNode
    134                 .getAttributeCount());
    135         assertEquals("Cannot find attribute", attr, viewNode.getAttribute(0));
    136         assertEquals("Parent was changed", node, ((ConfigurationNode) viewNode
    137                 .getAttribute(0)).getParentNode());
    138     }
    139
    140     /***
    141      * Tests appending attributes from a null source. This should be a noop.
    142      */
    143     public void testAppendNullAttributes()
    144     {
    145         viewNode.appendAttributes(null);
    146         assertEquals("Wrong number of attributes", 0, viewNode
    147                 .getAttributeCount());
    148     }
    149 }