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 junit.framework.TestCase;
020:
021: /**
022: * Test class for ViewNode.
023: *
024: * @version $Id: TestViewNode.java 439648 2006-09-02 20:42:10Z oheger $
025: */
026: public class TestViewNode extends TestCase {
027: /** Stores the view node to be tested. */
028: ViewNode viewNode;
029:
030: /** Stores a regular node. */
031: ConfigurationNode node;
032:
033: /** A child node of the regular node. */
034: ConfigurationNode child;
035:
036: /** An attribute node of the regular node. */
037: ConfigurationNode attr;
038:
039: protected void setUp() throws Exception {
040: super .setUp();
041: node = new DefaultConfigurationNode();
042: child = new DefaultConfigurationNode("child");
043: attr = new DefaultConfigurationNode("attr");
044: node.addChild(child);
045: node.addAttribute(attr);
046: viewNode = new ViewNode();
047: }
048:
049: /**
050: * Tests adding a child to the view node.
051: */
052: public void testAddChild() {
053: viewNode.addChild(child);
054: assertEquals("Parent was changed", node, child.getParentNode());
055: assertEquals("Child was not added", 1, viewNode
056: .getChildrenCount());
057: }
058:
059: /**
060: * Tests adding a null child to the view node. This should throw an
061: * exception.
062: */
063: public void testAddNullChild() {
064: try {
065: viewNode.addChild(null);
066: fail("Could add null child!");
067: } catch (IllegalArgumentException iex) {
068: // ok
069: }
070: }
071:
072: /**
073: * Tests adding an attribute to the view node.
074: */
075: public void testAddAttribute() {
076: viewNode.addAttribute(attr);
077: assertEquals("Parent was changed", node, attr.getParentNode());
078: assertEquals("Attribute was not added", 1, viewNode
079: .getAttributeCount());
080: }
081:
082: /**
083: * Tests adding a null attribute to the view node. This should cause an
084: * exception.
085: */
086: public void testAddNullAttribute() {
087: try {
088: viewNode.addAttribute(null);
089: fail("Could add null attribute");
090: } catch (IllegalArgumentException iex) {
091: // ok
092: }
093: }
094:
095: /**
096: * Tests appending all children to a view node.
097: */
098: public void testAppendChildren() {
099: viewNode.addChild(new DefaultConfigurationNode("testNode"));
100: viewNode.appendChildren(node);
101: assertEquals("Wrong number of children", 2, viewNode
102: .getChildrenCount());
103: assertEquals("Cannot find child", child, viewNode.getChild(1));
104: assertEquals("Parent was changed", node,
105: ((ConfigurationNode) viewNode.getChild(1))
106: .getParentNode());
107: }
108:
109: /**
110: * Tests appending children from a null source. This should be a noop.
111: */
112: public void testAppendNullChildren() {
113: viewNode.appendChildren(null);
114: assertEquals("Wrong number of children", 0, viewNode
115: .getChildrenCount());
116: }
117:
118: /**
119: * tests appending all attributes to a view node.
120: */
121: public void testAppendAttributes() {
122: viewNode.appendAttributes(node);
123: assertEquals("Wrong number of attributes", 1, viewNode
124: .getAttributeCount());
125: assertEquals("Cannot find attribute", attr, viewNode
126: .getAttribute(0));
127: assertEquals("Parent was changed", node,
128: ((ConfigurationNode) viewNode.getAttribute(0))
129: .getParentNode());
130: }
131:
132: /**
133: * Tests appending attributes from a null source. This should be a noop.
134: */
135: public void testAppendNullAttributes() {
136: viewNode.appendAttributes(null);
137: assertEquals("Wrong number of attributes", 0, viewNode
138: .getAttributeCount());
139: }
140: }
|