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 java.util.List;
20  
21  import junit.framework.TestCase;
22  
23  /***
24   * Test class for NodeAddData.
25   *
26   * @author Oliver Heger
27   */
28  public class TestNodeAddData extends TestCase
29  {
30      /*** Constant for the default parent node used for testing. */
31      private static final ConfigurationNode TEST_PARENT = new DefaultConfigurationNode(
32              "parent");
33  
34      /*** Constant for the name of the new node. */
35      private static final String TEST_NODENAME = "testNewNode";
36  
37      /*** Constant for the name of a path node. */
38      private static final String PATH_NODE_NAME = "PATHNODE";
39  
40      /*** Constant for the number of path nodes to be added. */
41      private static final int PATH_NODE_COUNT = 10;
42  
43      /*** The object to be tested. */
44      NodeAddData addData;
45  
46      protected void setUp() throws Exception
47      {
48          super.setUp();
49          addData = new NodeAddData(TEST_PARENT, TEST_NODENAME);
50      }
51  
52      /***
53       * Tests the default values of an unitialized instance.
54       */
55      public void testUninitialized()
56      {
57          addData = new NodeAddData();
58          assertNull("A parent is set", addData.getParent());
59          assertNull("Node has a name", addData.getNewNodeName());
60          assertFalse("Attribute flag is set", addData.isAttribute());
61          assertTrue("Path nodes are not empty", addData.getPathNodes().isEmpty());
62      }
63  
64      /***
65       * Tests the constructor that initializes the most important fields.
66       */
67      public void testInitialized()
68      {
69          assertSame("Wrong parent", TEST_PARENT, addData.getParent());
70          assertEquals("Wrong node name", TEST_NODENAME, addData.getNewNodeName());
71          assertFalse("Attribute flag is set", addData.isAttribute());
72          assertTrue("Path nodes are not empty", addData.getPathNodes().isEmpty());
73      }
74  
75      /***
76       * Tests adding path nodes.
77       */
78      public void testAddPathNode()
79      {
80          for (int i = 0; i < PATH_NODE_COUNT; i++)
81          {
82              addData.addPathNode(PATH_NODE_NAME + i);
83          }
84  
85          List nodes = addData.getPathNodes();
86          assertEquals("Incorrect number of path nodes", PATH_NODE_COUNT, nodes
87                  .size());
88          for (int i = 0; i < PATH_NODE_COUNT; i++)
89          {
90              assertEquals("Wrong path node at position" + i, PATH_NODE_NAME + i,
91                      nodes.get(i));
92          }
93      }
94  }