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: /**
018: * @author Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.io.Serializable;
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025: import java.util.Vector;
026: import javax.swing.JTree.DynamicUtilTreeNode;
027: import javax.swing.tree.DefaultMutableTreeNode;
028:
029: public class JTree_DynamicUtilTreeNodeTest extends BasicSwingTestCase {
030: private JTree.DynamicUtilTreeNode node;
031:
032: public JTree_DynamicUtilTreeNodeTest(final String name) {
033: super (name);
034: }
035:
036: @Override
037: protected void setUp() throws Exception {
038: node = new DynamicUtilTreeNode("value", null);
039: }
040:
041: @Override
042: protected void tearDown() throws Exception {
043: node = null;
044: }
045:
046: public void testDynamicUtilTreeNode() throws Exception {
047: assertNull(node.childValue);
048: assertFalse(node.hasChildren);
049: assertTrue(node.loadedChildren);
050: assertEquals("value", node.getUserObject());
051: assertFalse(node.getAllowsChildren());
052: assertTrue(node.isLeaf());
053: assertEquals(0, node.getChildCount());
054: node = new DynamicUtilTreeNode("value", "children value");
055: assertEquals(node.childValue, "children value");
056: assertTrue(node.loadedChildren);
057: node = new DynamicUtilTreeNode("value", new Object[] { "1" });
058: assertFalse(node.loadedChildren);
059: node = new DynamicUtilTreeNode("value", new Object[] {});
060: assertFalse(node.loadedChildren);
061: }
062:
063: public void testCreateChildren() throws Exception {
064: DynamicUtilTreeNode.createChildren(null, "any");
065: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
066: DynamicUtilTreeNode.createChildren(root, "any");
067: assertEquals(0, root.getChildCount());
068: assertTrue(root.isLeaf());
069: int[] privitiveArrayChildren = new int[] { 1, 2, 3 };
070: DynamicUtilTreeNode
071: .createChildren(root, privitiveArrayChildren);
072: assertEquals(0, root.getChildCount());
073: assertTrue(root.isLeaf());
074: assertTrue(root.getAllowsChildren());
075: Object[] objectArrayChildren = new Object[] { "a", "b", "c" };
076: DynamicUtilTreeNode.createChildren(root, objectArrayChildren);
077: assertEquals(3, root.getChildCount());
078: assertTrue(root.getChildAt(0) instanceof JTree.DynamicUtilTreeNode);
079: assertFalse(root.isLeaf());
080: assertEquals("a", ((DefaultMutableTreeNode) root.getChildAt(0))
081: .getUserObject());
082: Vector<String> vectorChildren = new Vector<String>();
083: vectorChildren.add("1");
084: vectorChildren.add("2");
085: DynamicUtilTreeNode.createChildren(root, vectorChildren);
086: assertEquals(5, root.getChildCount());
087: assertTrue(root.getChildAt(4) instanceof JTree.DynamicUtilTreeNode);
088: assertTrue(root.getChildAt(4).isLeaf());
089: assertFalse(root.getChildAt(4).getAllowsChildren());
090: assertEquals("1", ((DefaultMutableTreeNode) root.getChildAt(3))
091: .getUserObject());
092: Hashtable<String, String> hashChildren = new Hashtable<String, String>();
093: hashChildren.put("key1", "value1");
094: hashChildren.put("key2", "value2");
095: DynamicUtilTreeNode.createChildren(root, hashChildren);
096: assertEquals(7, root.getChildCount());
097: assertTrue(root.getChildAt(5) instanceof JTree.DynamicUtilTreeNode);
098: assertEquals(hashChildren.keys().nextElement(),
099: ((DefaultMutableTreeNode) root.getChildAt(5))
100: .getUserObject());
101: assertEquals(0, root.getChildAt(5).getChildCount());
102: root = new DefaultMutableTreeNode();
103: Hashtable<String, String> subSubChildren = new Hashtable<String, String>();
104: subSubChildren.put("221", "any");
105: subSubChildren.put("222", "any");
106: Vector<Serializable> subChildren = new Vector<Serializable>();
107: subChildren.add("21");
108: subChildren.add(subSubChildren);
109: subChildren.add("23");
110: Object[] complexChildren = new Object[] { "1", subChildren, "3" };
111: DynamicUtilTreeNode.createChildren(root, complexChildren);
112: assertEquals(3, root.getChildCount());
113: DynamicUtilTreeNode child1 = (DynamicUtilTreeNode) root
114: .getChildAt(0);
115: assertFalse(child1.getAllowsChildren());
116: assertEquals(0, child1.getChildCount());
117: assertEquals("1", child1.getUserObject());
118: assertEquals("1", child1.childValue);
119: assertTrue(child1.loadedChildren);
120: DynamicUtilTreeNode child2 = (DynamicUtilTreeNode) root
121: .getChildAt(1);
122: assertTrue(child2.getAllowsChildren());
123: assertEquals(3, child2.getChildCount());
124: assertEquals(subChildren, child2.getUserObject());
125: assertSame(subChildren, child2.childValue);
126: assertTrue(child2.loadedChildren);
127: assertEquals(0, root.getChildAt(2).getChildCount());
128: assertEquals("3", ((DefaultMutableTreeNode) root.getChildAt(2))
129: .getUserObject());
130: assertEquals(3, child2.getChildCount());
131: assertEquals(0, child2.getChildAt(0).getChildCount());
132: assertEquals(2, child2.getChildAt(1).getChildCount());
133: assertEquals(0, child2.getChildAt(2).getChildCount());
134: }
135:
136: public void testIsLeaf() throws Exception {
137: DynamicUtilTreeNode node = new DynamicUtilTreeNode("value",
138: null);
139: assertFalse(node.getAllowsChildren());
140: assertTrue(node.isLeaf());
141: node.setAllowsChildren(true);
142: assertFalse(node.isLeaf());
143: }
144:
145: public void testGetChildCount() throws Exception {
146: DynamicUtilTreeNode node = new DynamicUtilTreeNode("value",
147: new Object[] { "1", "2" });
148: assertFalse(node.loadedChildren);
149: assertEquals(2, node.getChildCount());
150: assertTrue(node.loadedChildren);
151: }
152:
153: public void testGetChildAt() throws Exception {
154: DynamicUtilTreeNode node = new DynamicUtilTreeNode("value",
155: new Object[] { "1", "2" });
156: assertFalse(node.loadedChildren);
157: assertEquals("1", ((DynamicUtilTreeNode) node.getChildAt(0))
158: .getUserObject());
159: assertTrue(node.loadedChildren);
160: }
161:
162: public void testChildren() throws Exception {
163: DynamicUtilTreeNode node = new DynamicUtilTreeNode("value",
164: new Object[] { "1", "2" });
165: assertFalse(node.loadedChildren);
166: Enumeration<?> children = node.children();
167: assertTrue(node.loadedChildren);
168: assertEquals("1", ((DefaultMutableTreeNode) children
169: .nextElement()).getUserObject());
170: assertEquals("2", ((DefaultMutableTreeNode) children
171: .nextElement()).getUserObject());
172: }
173:
174: public void testLoadChildren() throws Exception {
175: Object[] children = new Object[] { "1", "2" };
176: DynamicUtilTreeNode node = new DynamicUtilTreeNode("value",
177: children);
178: assertFalse(node.loadedChildren);
179: assertEquals(children, node.childValue);
180: assertEquals("value", node.getUserObject());
181: node.loadChildren();
182: assertTrue(node.loadedChildren);
183: assertEquals(2, node.getChildCount());
184: node.childValue = "any";
185: node.loadChildren();
186: assertEquals(2, node.getChildCount());
187: assertEquals("value", node.getUserObject());
188: node.childValue = new Object[] { "3", "4", "5" };
189: node.loadChildren();
190: assertTrue(node.loadedChildren);
191: assertEquals(5, node.getChildCount());
192: assertEquals("5", ((DefaultMutableTreeNode) node.getChildAt(4))
193: .getUserObject());
194: node.childValue = new Object[] { "6" };
195: assertEquals(5, node.getChildCount());
196: node.loadedChildren = false;
197: assertEquals(6, node.getChildCount());
198: }
199: }
|