001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.configuration.tree;
018    
019    import static org.junit.Assert.assertEquals;
020    
021    import org.junit.Before;
022    import org.junit.Test;
023    
024    /**
025     * Test class for ViewNode.
026     *
027     * @version $Id: TestViewNode.java 1226098 2011-12-31 15:18:45Z oheger $
028     */
029    public class TestViewNode
030    {
031        /** Stores the view node to be tested. */
032        ViewNode viewNode;
033    
034        /** Stores a regular node. */
035        ConfigurationNode node;
036    
037        /** A child node of the regular node. */
038        ConfigurationNode child;
039    
040        /** An attribute node of the regular node. */
041        ConfigurationNode attr;
042    
043        @Before
044        public void setUp() throws Exception
045        {
046            node = new DefaultConfigurationNode();
047            child = new DefaultConfigurationNode("child");
048            attr = new DefaultConfigurationNode("attr");
049            node.addChild(child);
050            node.addAttribute(attr);
051            viewNode = new ViewNode();
052        }
053    
054        /**
055         * Tests adding a child to the view node.
056         */
057        @Test
058        public void testAddChild()
059        {
060            viewNode.addChild(child);
061            assertEquals("Parent was changed", node, child.getParentNode());
062            assertEquals("Child was not added", 1, viewNode.getChildrenCount());
063        }
064    
065        /**
066         * Tests adding a null child to the view node. This should throw an
067         * exception.
068         */
069        @Test(expected = IllegalArgumentException.class)
070        public void testAddNullChild()
071        {
072            viewNode.addChild(null);
073        }
074    
075        /**
076         * Tests adding an attribute to the view node.
077         */
078        @Test
079        public void testAddAttribute()
080        {
081            viewNode.addAttribute(attr);
082            assertEquals("Parent was changed", node, attr.getParentNode());
083            assertEquals("Attribute was not added", 1, viewNode.getAttributeCount());
084        }
085    
086        /**
087         * Tests adding a null attribute to the view node. This should cause an
088         * exception.
089         */
090        @Test(expected = IllegalArgumentException.class)
091        public void testAddNullAttribute()
092        {
093            viewNode.addAttribute(null);
094        }
095    
096        /**
097         * Tests appending all children to a view node.
098         */
099        @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    }