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 import static org.junit.Assert.assertFalse; 021 import static org.junit.Assert.assertNull; 022 import static org.junit.Assert.assertSame; 023 import static org.junit.Assert.assertTrue; 024 025 import java.util.List; 026 027 import org.junit.Before; 028 import org.junit.Test; 029 030 /** 031 * Test class for NodeAddData. 032 * 033 * @author <a 034 * href="http://commons.apache.org/configuration/team-list.html">Commons 035 * Configuration team</a> 036 * @version $Id: TestNodeAddData.java 1226097 2011-12-31 15:16:02Z oheger $ 037 */ 038 public class TestNodeAddData 039 { 040 /** Constant for the default parent node used for testing. */ 041 private static final ConfigurationNode TEST_PARENT = new DefaultConfigurationNode( 042 "parent"); 043 044 /** Constant for the name of the new node. */ 045 private static final String TEST_NODENAME = "testNewNode"; 046 047 /** Constant for the name of a path node. */ 048 private static final String PATH_NODE_NAME = "PATHNODE"; 049 050 /** Constant for the number of path nodes to be added. */ 051 private static final int PATH_NODE_COUNT = 10; 052 053 /** The object to be tested. */ 054 NodeAddData addData; 055 056 @Before 057 public void setUp() throws Exception 058 { 059 addData = new NodeAddData(TEST_PARENT, TEST_NODENAME); 060 } 061 062 /** 063 * Tests the default values of an uninitialized instance. 064 */ 065 @Test 066 public void testUninitialized() 067 { 068 addData = new NodeAddData(); 069 assertNull("A parent is set", addData.getParent()); 070 assertNull("Node has a name", addData.getNewNodeName()); 071 assertFalse("Attribute flag is set", addData.isAttribute()); 072 assertTrue("Path nodes are not empty", addData.getPathNodes().isEmpty()); 073 } 074 075 /** 076 * Tests the constructor that initializes the most important fields. 077 */ 078 @Test 079 public void testInitialized() 080 { 081 assertSame("Wrong parent", TEST_PARENT, addData.getParent()); 082 assertEquals("Wrong node name", TEST_NODENAME, addData.getNewNodeName()); 083 assertFalse("Attribute flag is set", addData.isAttribute()); 084 assertTrue("Path nodes are not empty", addData.getPathNodes().isEmpty()); 085 } 086 087 /** 088 * Tests adding path nodes. 089 */ 090 @Test 091 public void testAddPathNode() 092 { 093 for (int i = 0; i < PATH_NODE_COUNT; i++) 094 { 095 addData.addPathNode(PATH_NODE_NAME + i); 096 } 097 098 List<String> nodes = addData.getPathNodes(); 099 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 }