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;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertSame;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.util.List;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  /**
31   * Test class for NodeAddData.
32   *
33   * @author <a
34   * href="http://commons.apache.org/configuration/team-list.html">Commons
35   * Configuration team</a>
36   * @version $Id: TestNodeAddData.java 1226097 2011-12-31 15:16:02Z oheger $
37   */
38  public class TestNodeAddData
39  {
40      /** Constant for the default parent node used for testing. */
41      private static final ConfigurationNode TEST_PARENT = new DefaultConfigurationNode(
42              "parent");
43  
44      /** Constant for the name of the new node. */
45      private static final String TEST_NODENAME = "testNewNode";
46  
47      /** Constant for the name of a path node. */
48      private static final String PATH_NODE_NAME = "PATHNODE";
49  
50      /** Constant for the number of path nodes to be added. */
51      private static final int PATH_NODE_COUNT = 10;
52  
53      /** The object to be tested. */
54      NodeAddData addData;
55  
56      @Before
57      public void setUp() throws Exception
58      {
59          addData = new NodeAddData(TEST_PARENT, TEST_NODENAME);
60      }
61  
62      /**
63       * Tests the default values of an uninitialized instance.
64       */
65      @Test
66      public void testUninitialized()
67      {
68          addData = new NodeAddData();
69          assertNull("A parent is set", addData.getParent());
70          assertNull("Node has a name", 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 the constructor that initializes the most important fields.
77       */
78      @Test
79      public void testInitialized()
80      {
81          assertSame("Wrong parent", TEST_PARENT, addData.getParent());
82          assertEquals("Wrong node name", TEST_NODENAME, addData.getNewNodeName());
83          assertFalse("Attribute flag is set", addData.isAttribute());
84          assertTrue("Path nodes are not empty", addData.getPathNodes().isEmpty());
85      }
86  
87      /**
88       * Tests adding path nodes.
89       */
90      @Test
91      public void testAddPathNode()
92      {
93          for (int i = 0; i < PATH_NODE_COUNT; i++)
94          {
95              addData.addPathNode(PATH_NODE_NAME + i);
96          }
97  
98          List<String> nodes = addData.getPathNodes();
99          assertEquals("Incorrect number of path nodes", PATH_NODE_COUNT, nodes
100                 .size());
101         for (int i = 0; i < PATH_NODE_COUNT; i++)
102         {
103             assertEquals("Wrong path node at position" + i, PATH_NODE_NAME + i,
104                     nodes.get(i));
105         }
106     }
107 }