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