0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Anton Avtamonov
0019: * @version $Revision$
0020: */package javax.swing.tree;
0021:
0022: import java.util.Enumeration;
0023: import java.util.NoSuchElementException;
0024: import javax.swing.BasicSwingTestCase;
0025:
0026: public class DefaultMutableTreeNodeTest extends BasicSwingTestCase {
0027: private DefaultMutableTreeNode node;
0028:
0029: public DefaultMutableTreeNodeTest(final String name) {
0030: super (name);
0031: }
0032:
0033: @Override
0034: protected void setUp() throws Exception {
0035: node = new DefaultMutableTreeNode();
0036: }
0037:
0038: @Override
0039: protected void tearDown() throws Exception {
0040: node = null;
0041: }
0042:
0043: public void testEMPTY_ENUMERATION() throws Exception {
0044: assertFalse(DefaultMutableTreeNode.EMPTY_ENUMERATION
0045: .hasMoreElements());
0046: }
0047:
0048: public void testDefaultMutableTreeNode() throws Exception {
0049: assertNull(node.parent);
0050: assertNull(node.children);
0051: assertEquals(0, node.getChildCount());
0052: assertNull(node.userObject);
0053: assertTrue(node.allowsChildren);
0054: node = new DefaultMutableTreeNode("user object");
0055: assertNull(node.parent);
0056: assertNull(node.children);
0057: assertEquals(0, node.getChildCount());
0058: assertEquals("user object", node.userObject);
0059: assertTrue(node.allowsChildren);
0060: node = new DefaultMutableTreeNode("user object", false);
0061: assertNull(node.parent);
0062: assertNull(node.children);
0063: assertEquals(0, node.getChildCount());
0064: assertEquals("user object", node.userObject);
0065: assertFalse(node.allowsChildren);
0066: }
0067:
0068: public void testInsert() throws Exception {
0069: DefaultMutableTreeNode root1 = new DefaultMutableTreeNode();
0070: final DefaultMutableTreeNode root2 = new DefaultMutableTreeNode();
0071: final DefaultMutableTreeNode insertingChild = new DefaultMutableTreeNode();
0072: root1.add(insertingChild);
0073: assertEquals(1, root1.getChildCount());
0074: assertSame(root1, insertingChild.getParent());
0075: root2.add(new DefaultMutableTreeNode());
0076: root2.add(new DefaultMutableTreeNode());
0077: assertEquals(2, root2.getChildCount());
0078: root2.insert(insertingChild, 1);
0079: assertEquals(3, root2.getChildCount());
0080: assertEquals(0, root1.getChildCount());
0081: assertSame(root2, insertingChild.getParent());
0082: assertSame(insertingChild, root2.getChildAt(1));
0083: root2.insert(insertingChild, 0);
0084: assertEquals(3, root2.getChildCount());
0085: assertSame(root2, insertingChild.getParent());
0086: assertSame(insertingChild, root2.getChildAt(0));
0087: root2.insert(insertingChild, 2);
0088: assertEquals(3, root2.getChildCount());
0089: assertSame(root2, insertingChild.getParent());
0090: assertSame(insertingChild, root2.getChildAt(2));
0091: testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
0092: @Override
0093: public void exceptionalAction() throws Exception {
0094: root2.insert(insertingChild, 3);
0095: }
0096: });
0097: testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
0098: @Override
0099: public void exceptionalAction() throws Exception {
0100: new DefaultMutableTreeNode().insert(
0101: new DefaultMutableTreeNode(), 2);
0102: }
0103: });
0104: testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
0105: @Override
0106: public void exceptionalAction() throws Exception {
0107: new DefaultMutableTreeNode().insert(
0108: new DefaultMutableTreeNode(), -1);
0109: }
0110: });
0111: testExceptionalCase(new IllegalArgumentCase() {
0112: @Override
0113: public void exceptionalAction() throws Exception {
0114: new DefaultMutableTreeNode().insert(null, 0);
0115: }
0116: });
0117: testExceptionalCase(new IllegalArgumentCase() {
0118: @Override
0119: public void exceptionalAction() throws Exception {
0120: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0121: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0122: root.insert(child, 0);
0123: child.insert(root, 0);
0124: }
0125: });
0126: testExceptionalCase(new IllegalArgumentCase() {
0127: @Override
0128: public void exceptionalAction() throws Exception {
0129: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0130: root.insert(root, 0);
0131: }
0132: });
0133: testExceptionalCase(new IllegalStateCase() {
0134: @Override
0135: public void exceptionalAction() throws Exception {
0136: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0137: root.setAllowsChildren(false);
0138: root.insert(new DefaultMutableTreeNode(), 0);
0139: }
0140: });
0141: }
0142:
0143: public void testRemove() throws Exception {
0144: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0145: node.add(child);
0146: node.add(new DefaultMutableTreeNode());
0147: assertEquals(2, node.getChildCount());
0148: node.remove(1);
0149: assertEquals(1, node.getChildCount());
0150: node.remove(child);
0151: assertEquals(0, node.getChildCount());
0152: assertNull(child.getParent());
0153: testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
0154: @Override
0155: public void exceptionalAction() throws Exception {
0156: node.remove(0);
0157: }
0158: });
0159: testExceptionalCase(new IllegalArgumentCase() {
0160: @Override
0161: public void exceptionalAction() throws Exception {
0162: node.remove(null);
0163: }
0164: });
0165: testExceptionalCase(new IllegalArgumentCase() {
0166: @Override
0167: public void exceptionalAction() throws Exception {
0168: node.remove(new DefaultMutableTreeNode());
0169: }
0170: });
0171: }
0172:
0173: public void testGetSetParent() throws Exception {
0174: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0175: node.add(child);
0176: assertSame(node, child.getParent());
0177: assertEquals(1, node.getChildCount());
0178: DefaultMutableTreeNode parent = new DefaultMutableTreeNode();
0179: child.setParent(parent);
0180: assertSame(parent, child.getParent());
0181: assertEquals(1, node.getChildCount());
0182: assertEquals(child, node.getChildAt(0));
0183: assertEquals(0, parent.getChildCount());
0184: }
0185:
0186: public void testGetChildAt() throws Exception {
0187: final DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0188: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode();
0189: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode();
0190: root.add(child1);
0191: root.add(child2);
0192: assertSame(child1, root.getChildAt(0));
0193: assertSame(child2, root.getChildAt(1));
0194: DefaultMutableTreeNode child3 = new DefaultMutableTreeNode();
0195: root.insert(child3, 1);
0196: assertSame(child1, root.getChildAt(0));
0197: assertSame(child3, root.getChildAt(1));
0198: assertSame(child2, root.getChildAt(2));
0199: testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
0200: @Override
0201: public void exceptionalAction() throws Exception {
0202: root.getChildAt(3);
0203: }
0204: });
0205: testExceptionalCase(new ArrayIndexOutOfBoundsCase() {
0206: @Override
0207: public void exceptionalAction() throws Exception {
0208: root.getChildAt(-1);
0209: }
0210: });
0211: }
0212:
0213: public void testGetChildCount() throws Exception {
0214: assertEquals(0, node.getChildCount());
0215: node.add(new DefaultMutableTreeNode());
0216: assertEquals(1, node.getChildCount());
0217: node.add(new DefaultMutableTreeNode());
0218: assertEquals(2, node.getChildCount());
0219: node.remove(0);
0220: assertEquals(1, node.getChildCount());
0221: }
0222:
0223: public void testGetIndex() throws Exception {
0224: assertEquals(-1, node.getIndex(new DefaultMutableTreeNode()));
0225: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0226: node.add(new DefaultMutableTreeNode());
0227: node.add(child);
0228: node.add(new DefaultMutableTreeNode());
0229: assertEquals(1, node.getIndex(child));
0230: testExceptionalCase(new IllegalArgumentCase() {
0231: @Override
0232: public void exceptionalAction() throws Exception {
0233: node.getIndex(null);
0234: }
0235: });
0236: }
0237:
0238: public void testChildren() throws Exception {
0239: assertSame(DefaultMutableTreeNode.EMPTY_ENUMERATION, node
0240: .children());
0241: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode();
0242: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode();
0243: node.add(child1);
0244: node.add(child2);
0245: Enumeration<?> children = node.children();
0246: assertSame(child1, children.nextElement());
0247: assertSame(child2, children.nextElement());
0248: }
0249:
0250: public void testGetSetAllowsChildren() throws Exception {
0251: assertTrue(node.getAllowsChildren());
0252: node.add(new DefaultMutableTreeNode());
0253: node.add(new DefaultMutableTreeNode());
0254: node.setAllowsChildren(false);
0255: assertFalse(node.getAllowsChildren());
0256: assertEquals(0, node.getChildCount());
0257: assertNotNull(node.children);
0258: assertTrue(node.children.isEmpty());
0259: node.setAllowsChildren(true);
0260: assertTrue(node.getAllowsChildren());
0261: assertEquals(0, node.getChildCount());
0262: node.add(new DefaultMutableTreeNode());
0263: node.setAllowsChildren(true);
0264: assertEquals(1, node.getChildCount());
0265: }
0266:
0267: public void testGetSetUserObject() throws Exception {
0268: assertNull(node.getUserObject());
0269: Object user = new Object();
0270: node.setUserObject(user);
0271: assertSame(user, node.getUserObject());
0272: }
0273:
0274: public void testRemoveFromParent() throws Exception {
0275: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0276: root.add(node);
0277: node.add(new DefaultMutableTreeNode());
0278: assertSame(root, node.getParent());
0279: assertEquals(1, node.getChildCount());
0280: node.removeFromParent();
0281: assertNull(node.getParent());
0282: assertEquals(1, node.getChildCount());
0283: assertEquals(0, root.getChildCount());
0284: }
0285:
0286: public void testRemoveAllChildren() throws Exception {
0287: node.removeAllChildren();
0288: assertEquals(0, node.getChildCount());
0289: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode();
0290: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode();
0291: node.add(child1);
0292: node.add(child2);
0293: assertEquals(2, node.getChildCount());
0294: node.removeAllChildren();
0295: assertEquals(0, node.getChildCount());
0296: assertNull(child1.getParent());
0297: assertNull(child2.getParent());
0298: }
0299:
0300: public void testAdd() throws Exception {
0301: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode();
0302: node.add(child1);
0303: assertSame(child1, node.getChildAt(0));
0304: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode();
0305: node.add(child2);
0306: assertSame(child2, node.getChildAt(1));
0307: assertEquals(2, node.getChildCount());
0308: node.add(child1);
0309: assertEquals(2, node.getChildCount());
0310: assertSame(child2, node.getChildAt(0));
0311: assertSame(child1, node.getChildAt(1));
0312: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0313: root.add(child1);
0314: assertEquals(1, node.getChildCount());
0315: assertEquals(1, root.getChildCount());
0316: assertEquals(root, child1.getParent());
0317: testExceptionalCase(new IllegalArgumentCase() {
0318: @Override
0319: public void exceptionalAction() throws Exception {
0320: node.add(null);
0321: }
0322: });
0323: testExceptionalCase(new IllegalStateCase() {
0324: @Override
0325: public void exceptionalAction() throws Exception {
0326: node.setAllowsChildren(false);
0327: node.add(new DefaultMutableTreeNode());
0328: }
0329: });
0330: }
0331:
0332: public void testIsNodeAncestor() throws Exception {
0333: assertFalse(node.isNodeAncestor(null));
0334: assertFalse(node.isNodeAncestor(new DefaultMutableTreeNode()));
0335: assertTrue(node.isNodeAncestor(node));
0336: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0337: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0338: node.add(child);
0339: child.add(childChild);
0340: assertTrue(childChild.isNodeAncestor(child));
0341: assertTrue(child.isNodeAncestor(node));
0342: assertTrue(childChild.isNodeAncestor(node));
0343: child.setParent(null);
0344: assertFalse(child.isNodeAncestor(node));
0345: }
0346:
0347: public void testIsNodeDescendant() throws Exception {
0348: assertFalse(node.isNodeDescendant(null));
0349: assertFalse(node.isNodeDescendant(new DefaultMutableTreeNode()));
0350: assertTrue(node.isNodeDescendant(node));
0351: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0352: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0353: node.add(child);
0354: child.add(childChild);
0355: assertTrue(child.isNodeDescendant(childChild));
0356: assertTrue(node.isNodeDescendant(child));
0357: assertTrue(node.isNodeDescendant(childChild));
0358: child.setParent(null);
0359: assertFalse(node.isNodeDescendant(child));
0360: }
0361:
0362: public void testGetSharedAncestor() throws Exception {
0363: assertNull(node.getSharedAncestor(null));
0364: assertEquals(node, node.getSharedAncestor(node));
0365: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0366: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0367: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0368: root.add(child);
0369: child.add(childChild);
0370: DefaultMutableTreeNode nodeRoot = new DefaultMutableTreeNode();
0371: nodeRoot.add(node);
0372: child.add(nodeRoot);
0373: assertEquals(child, node.getSharedAncestor(childChild));
0374: assertEquals(child, node.getSharedAncestor(child));
0375: assertEquals(child, child.getSharedAncestor(node));
0376: assertEquals(child, childChild.getSharedAncestor(nodeRoot));
0377: assertEquals(root, node.getSharedAncestor(root));
0378: nodeRoot.setParent(null);
0379: assertEquals(nodeRoot, node.getSharedAncestor(nodeRoot));
0380: assertNull(node.getSharedAncestor(child));
0381: }
0382:
0383: public void testIsNodeRelated() throws Exception {
0384: assertFalse(node.isNodeRelated(null));
0385: assertTrue(node.isNodeRelated(node));
0386: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0387: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0388: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0389: root.add(child);
0390: child.add(childChild);
0391: DefaultMutableTreeNode nodeRoot = new DefaultMutableTreeNode();
0392: nodeRoot.add(node);
0393: child.add(nodeRoot);
0394: assertTrue(node.isNodeRelated(childChild));
0395: assertTrue(node.isNodeRelated(child));
0396: assertTrue(child.isNodeRelated(node));
0397: assertTrue(childChild.isNodeRelated(nodeRoot));
0398: assertTrue(node.isNodeRelated(root));
0399: nodeRoot.setParent(null);
0400: assertTrue(node.isNodeRelated(nodeRoot));
0401: assertFalse(node.isNodeRelated(child));
0402: }
0403:
0404: public void testGetDepth() throws Exception {
0405: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0406: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0407: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0408: root.add(child);
0409: child.add(childChild);
0410: child.add(node);
0411: node.add(new DefaultMutableTreeNode());
0412: assertEquals(1, node.getDepth());
0413: assertEquals(2, child.getDepth());
0414: assertEquals(3, root.getDepth());
0415: DefaultMutableTreeNode childChildChild = new DefaultMutableTreeNode();
0416: childChild.add(childChildChild);
0417: assertEquals(3, root.getDepth());
0418: DefaultMutableTreeNode childChildChildChild = new DefaultMutableTreeNode();
0419: childChildChild.add(childChildChildChild);
0420: assertEquals(4, root.getDepth());
0421: assertEquals(0, childChildChildChild.getDepth());
0422: }
0423:
0424: public void testGetLevel() throws Exception {
0425: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0426: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0427: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0428: root.add(child);
0429: child.add(childChild);
0430: child.add(node);
0431: assertEquals(0, root.getLevel());
0432: assertEquals(2, node.getLevel());
0433: assertEquals(2, childChild.getLevel());
0434: }
0435:
0436: public void testGetPath() throws Exception {
0437: assertEquals(1, node.getPath().length);
0438: assertEquals(node, node.getPath()[0]);
0439: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0440: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0441: root.add(child);
0442: child.add(node);
0443: assertEquals(3, node.getPath().length);
0444: assertEquals(root, node.getPath()[0]);
0445: assertEquals(child, node.getPath()[1]);
0446: assertEquals(node, node.getPath()[2]);
0447: child.setParent(null);
0448: assertEquals(2, node.getPath().length);
0449: assertSame(child, node.getPath()[0]);
0450: assertSame(node, node.getPath()[1]);
0451: DefaultMutableTreeNode otherRoot = new DefaultMutableTreeNode();
0452: child.setParent(otherRoot);
0453: assertEquals(3, node.getPath().length);
0454: assertSame(otherRoot, node.getPath()[0]);
0455: assertSame(child, node.getPath()[1]);
0456: assertSame(node, node.getPath()[2]);
0457: }
0458:
0459: public void testGetPathToRoot() throws Exception {
0460: DefaultMutableTreeNode anyNode = new DefaultMutableTreeNode();
0461: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0462: root.add(node);
0463: TreeNode[] path = anyNode.getPathToRoot(root, 100);
0464: assertEquals(101, path.length);
0465: assertSame(root, path[0]);
0466: assertNull(path[1]);
0467: assertNull(path[100]);
0468: path = anyNode.getPathToRoot(node, 100);
0469: assertEquals(102, path.length);
0470: assertSame(root, path[0]);
0471: assertSame(node, path[1]);
0472: assertNull(path[2]);
0473: assertNull(path[101]);
0474: path = anyNode.getPathToRoot(null, 100);
0475: assertEquals(100, path.length);
0476: }
0477:
0478: public void testGetUserObjectPath() throws Exception {
0479: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0480: DefaultMutableTreeNode node = new DefaultMutableTreeNode("node");
0481: root.add(node);
0482: Object[] path = root.getUserObjectPath();
0483: assertEquals(1, path.length);
0484: assertSame(root.getUserObject(), path[0]);
0485: path = node.getUserObjectPath();
0486: assertEquals(2, path.length);
0487: assertSame(root.getUserObject(), path[0]);
0488: assertSame(node.getUserObject(), path[1]);
0489: }
0490:
0491: public void testGetRoot() throws Exception {
0492: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0493: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0494: root.add(child);
0495: child.add(node);
0496: assertSame(root, root.getRoot());
0497: assertSame(root, child.getRoot());
0498: assertSame(root, node.getRoot());
0499: }
0500:
0501: public void testIsRoot() throws Exception {
0502: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0503: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0504: root.add(child);
0505: assertFalse(child.isRoot());
0506: assertTrue(root.isRoot());
0507: assertTrue(node.isRoot());
0508: }
0509:
0510: public void testGetNextNode() throws Exception {
0511: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0512: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0513: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0514: root.add(child);
0515: child.add(childChild);
0516: root.add(node);
0517: assertNull(node.getNextNode());
0518: assertSame(child, root.getNextNode());
0519: assertSame(childChild, child.getNextNode());
0520: assertSame(node, childChild.getNextNode());
0521: }
0522:
0523: public void testGetPreviousNode() throws Exception {
0524: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0525: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0526: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0527: root.add(child);
0528: child.add(childChild);
0529: root.add(node);
0530: assertSame(childChild, node.getPreviousNode());
0531: assertSame(child, childChild.getPreviousNode());
0532: assertSame(root, child.getPreviousNode());
0533: assertNull(root.getPreviousNode());
0534: }
0535:
0536: public void testPreorderEnumeration() throws Exception {
0537: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0538: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0539: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0540: root.add(child);
0541: child.add(childChild);
0542: root.add(node);
0543: Enumeration<?> preEnum = node.preorderEnumeration();
0544: assertSame(node, preEnum.nextElement());
0545: assertFalse(preEnum.hasMoreElements());
0546: preEnum = childChild.preorderEnumeration();
0547: assertSame(childChild, preEnum.nextElement());
0548: assertFalse(preEnum.hasMoreElements());
0549: preEnum = child.preorderEnumeration();
0550: assertSame(child, preEnum.nextElement());
0551: assertSame(childChild, preEnum.nextElement());
0552: assertFalse(preEnum.hasMoreElements());
0553: preEnum = root.preorderEnumeration();
0554: assertSame(root, preEnum.nextElement());
0555: assertSame(child, preEnum.nextElement());
0556: assertSame(childChild, preEnum.nextElement());
0557: assertSame(node, preEnum.nextElement());
0558: assertFalse(preEnum.hasMoreElements());
0559: preEnum = root.preorderEnumeration();
0560: root.remove(0);
0561: assertSame(root, preEnum.nextElement());
0562: assertSame(node, preEnum.nextElement());
0563: assertFalse(preEnum.hasMoreElements());
0564: preEnum = node.preorderEnumeration();
0565: root.remove(0);
0566: assertSame(node, preEnum.nextElement());
0567: assertFalse(preEnum.hasMoreElements());
0568: testExceptionalCase(new ExceptionalCase() {
0569: @Override
0570: public void exceptionalAction() throws Exception {
0571: Enumeration<?> preEnum = node.preorderEnumeration();
0572: preEnum.nextElement();
0573: preEnum.nextElement();
0574: }
0575: });
0576: }
0577:
0578: public void testPostorderEnumeration() throws Exception {
0579: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0580: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
0581: "child");
0582: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode(
0583: "childChild");
0584: DefaultMutableTreeNode nodeChild = new DefaultMutableTreeNode(
0585: "nodeChild");
0586: root.add(child);
0587: child.add(childChild);
0588: root.add(node);
0589: node.add(nodeChild);
0590: Enumeration<?> postEnum = node.postorderEnumeration();
0591: assertSame(nodeChild, postEnum.nextElement());
0592: assertSame(node, postEnum.nextElement());
0593: assertFalse(postEnum.hasMoreElements());
0594: postEnum = nodeChild.postorderEnumeration();
0595: assertSame(nodeChild, postEnum.nextElement());
0596: assertFalse(postEnum.hasMoreElements());
0597: postEnum = childChild.postorderEnumeration();
0598: assertSame(childChild, postEnum.nextElement());
0599: assertFalse(postEnum.hasMoreElements());
0600: postEnum = child.postorderEnumeration();
0601: assertSame(childChild, postEnum.nextElement());
0602: assertSame(child, postEnum.nextElement());
0603: assertFalse(postEnum.hasMoreElements());
0604: postEnum = root.postorderEnumeration();
0605: assertSame(childChild, postEnum.nextElement());
0606: assertSame(child, postEnum.nextElement());
0607: assertSame(nodeChild, postEnum.nextElement());
0608: assertSame(node, postEnum.nextElement());
0609: assertSame(root, postEnum.nextElement());
0610: assertFalse(postEnum.hasMoreElements());
0611: final DefaultMutableTreeNode nodeChildChild = new DefaultMutableTreeNode(
0612: "nodeChildChild");
0613: nodeChild.add(nodeChildChild);
0614: postEnum = root.postorderEnumeration();
0615: assertSame(childChild, postEnum.nextElement());
0616: assertSame(child, postEnum.nextElement());
0617: assertSame(nodeChildChild, postEnum.nextElement());
0618: assertSame(nodeChild, postEnum.nextElement());
0619: assertSame(node, postEnum.nextElement());
0620: assertSame(root, postEnum.nextElement());
0621: assertFalse(postEnum.hasMoreElements());
0622: DefaultMutableTreeNode childChild2 = new DefaultMutableTreeNode(
0623: "childChild2");
0624: child.add(childChild2);
0625: postEnum = root.postorderEnumeration();
0626: assertSame(childChild, postEnum.nextElement());
0627: assertSame(childChild2, postEnum.nextElement());
0628: assertSame(child, postEnum.nextElement());
0629: assertSame(nodeChildChild, postEnum.nextElement());
0630: assertSame(nodeChild, postEnum.nextElement());
0631: assertSame(node, postEnum.nextElement());
0632: assertSame(root, postEnum.nextElement());
0633: assertFalse(postEnum.hasMoreElements());
0634: if (isHarmony()) {
0635: testExceptionalCase(new ExceptionalCase() {
0636: @Override
0637: public void exceptionalAction() throws Exception {
0638: Enumeration<?> postEnum = nodeChildChild
0639: .postorderEnumeration();
0640: postEnum.nextElement();
0641: postEnum.nextElement();
0642: }
0643: });
0644: }
0645: }
0646:
0647: public void testDepthFirstEnumeration() throws Exception {
0648: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0649: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
0650: "child");
0651: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode(
0652: "childChild");
0653: DefaultMutableTreeNode nodeChild = new DefaultMutableTreeNode(
0654: "nodeChild");
0655: root.add(child);
0656: child.add(childChild);
0657: root.add(node);
0658: node.add(nodeChild);
0659: Enumeration<?> depthEnum = node.depthFirstEnumeration();
0660: assertSame(nodeChild, depthEnum.nextElement());
0661: assertSame(node, depthEnum.nextElement());
0662: assertFalse(depthEnum.hasMoreElements());
0663: depthEnum = nodeChild.depthFirstEnumeration();
0664: assertSame(nodeChild, depthEnum.nextElement());
0665: assertFalse(depthEnum.hasMoreElements());
0666: depthEnum = childChild.depthFirstEnumeration();
0667: assertSame(childChild, depthEnum.nextElement());
0668: assertFalse(depthEnum.hasMoreElements());
0669: depthEnum = child.depthFirstEnumeration();
0670: assertSame(childChild, depthEnum.nextElement());
0671: assertSame(child, depthEnum.nextElement());
0672: assertFalse(depthEnum.hasMoreElements());
0673: depthEnum = root.depthFirstEnumeration();
0674: assertSame(childChild, depthEnum.nextElement());
0675: assertSame(child, depthEnum.nextElement());
0676: assertSame(nodeChild, depthEnum.nextElement());
0677: assertSame(node, depthEnum.nextElement());
0678: assertSame(root, depthEnum.nextElement());
0679: assertFalse(depthEnum.hasMoreElements());
0680: final DefaultMutableTreeNode nodeChildChild = new DefaultMutableTreeNode(
0681: "nodeChildChild");
0682: nodeChild.add(nodeChildChild);
0683: depthEnum = root.depthFirstEnumeration();
0684: assertSame(childChild, depthEnum.nextElement());
0685: assertSame(child, depthEnum.nextElement());
0686: assertSame(nodeChildChild, depthEnum.nextElement());
0687: assertSame(nodeChild, depthEnum.nextElement());
0688: assertSame(node, depthEnum.nextElement());
0689: assertSame(root, depthEnum.nextElement());
0690: assertFalse(depthEnum.hasMoreElements());
0691: DefaultMutableTreeNode childChild2 = new DefaultMutableTreeNode(
0692: "childChild2");
0693: child.add(childChild2);
0694: depthEnum = root.depthFirstEnumeration();
0695: assertSame(childChild, depthEnum.nextElement());
0696: assertSame(childChild2, depthEnum.nextElement());
0697: assertSame(child, depthEnum.nextElement());
0698: assertSame(nodeChildChild, depthEnum.nextElement());
0699: assertSame(nodeChild, depthEnum.nextElement());
0700: assertSame(node, depthEnum.nextElement());
0701: assertSame(root, depthEnum.nextElement());
0702: assertFalse(depthEnum.hasMoreElements());
0703: if (isHarmony()) {
0704: testExceptionalCase(new ExceptionalCase() {
0705: @Override
0706: public void exceptionalAction() throws Exception {
0707: Enumeration<?> depthEnum = nodeChildChild
0708: .depthFirstEnumeration();
0709: depthEnum.nextElement();
0710: depthEnum.nextElement();
0711: }
0712: });
0713: }
0714: }
0715:
0716: public void testPathFromAncestorEnumeration() throws Exception {
0717: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0718: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
0719: "child");
0720: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode(
0721: "childChild");
0722: DefaultMutableTreeNode nodeChild = new DefaultMutableTreeNode(
0723: "nodeChild");
0724: root.add(child);
0725: child.add(childChild);
0726: root.add(node);
0727: node.add(nodeChild);
0728: Enumeration<?> pathEnum = node
0729: .pathFromAncestorEnumeration(root);
0730: assertSame(root, pathEnum.nextElement());
0731: assertSame(node, pathEnum.nextElement());
0732: assertFalse(pathEnum.hasMoreElements());
0733: pathEnum = childChild.pathFromAncestorEnumeration(root);
0734: assertSame(root, pathEnum.nextElement());
0735: assertSame(child, pathEnum.nextElement());
0736: assertSame(childChild, pathEnum.nextElement());
0737: assertFalse(pathEnum.hasMoreElements());
0738: pathEnum = childChild.pathFromAncestorEnumeration(childChild);
0739: assertSame(childChild, pathEnum.nextElement());
0740: assertFalse(pathEnum.hasMoreElements());
0741: testExceptionalCase(new IllegalArgumentCase() {
0742: @Override
0743: public void exceptionalAction() throws Exception {
0744: node
0745: .pathFromAncestorEnumeration(new DefaultMutableTreeNode());
0746: }
0747: });
0748: testExceptionalCase(new ExceptionalCase() {
0749: @Override
0750: public void exceptionalAction() throws Exception {
0751: Enumeration<?> pathEnum = node
0752: .pathFromAncestorEnumeration(node);
0753: pathEnum.nextElement();
0754: pathEnum.nextElement();
0755: }
0756: });
0757: }
0758:
0759: public void testBreadthFirstEnumeration() throws Exception {
0760: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0761: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
0762: "child");
0763: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode(
0764: "childChild");
0765: DefaultMutableTreeNode nodeChild = new DefaultMutableTreeNode(
0766: "nodeChild");
0767: root.add(child);
0768: child.add(childChild);
0769: root.add(node);
0770: node.add(nodeChild);
0771: Enumeration<?> breadthEnum = node.breadthFirstEnumeration();
0772: assertSame(node, breadthEnum.nextElement());
0773: assertSame(nodeChild, breadthEnum.nextElement());
0774: assertFalse(breadthEnum.hasMoreElements());
0775: breadthEnum = nodeChild.breadthFirstEnumeration();
0776: assertSame(nodeChild, breadthEnum.nextElement());
0777: assertFalse(breadthEnum.hasMoreElements());
0778: breadthEnum = childChild.breadthFirstEnumeration();
0779: assertSame(childChild, breadthEnum.nextElement());
0780: assertFalse(breadthEnum.hasMoreElements());
0781: breadthEnum = child.breadthFirstEnumeration();
0782: assertSame(child, breadthEnum.nextElement());
0783: assertSame(childChild, breadthEnum.nextElement());
0784: assertFalse(breadthEnum.hasMoreElements());
0785: breadthEnum = root.breadthFirstEnumeration();
0786: assertSame(root, breadthEnum.nextElement());
0787: assertSame(child, breadthEnum.nextElement());
0788: assertSame(node, breadthEnum.nextElement());
0789: assertSame(childChild, breadthEnum.nextElement());
0790: assertSame(nodeChild, breadthEnum.nextElement());
0791: assertFalse(breadthEnum.hasMoreElements());
0792: final DefaultMutableTreeNode childChildChild = new DefaultMutableTreeNode(
0793: "childChildChild");
0794: childChild.add(childChildChild);
0795: breadthEnum = root.breadthFirstEnumeration();
0796: assertSame(root, breadthEnum.nextElement());
0797: assertSame(child, breadthEnum.nextElement());
0798: assertSame(node, breadthEnum.nextElement());
0799: assertSame(childChild, breadthEnum.nextElement());
0800: assertSame(nodeChild, breadthEnum.nextElement());
0801: assertSame(childChildChild, breadthEnum.nextElement());
0802: assertFalse(breadthEnum.hasMoreElements());
0803: DefaultMutableTreeNode childChild2 = new DefaultMutableTreeNode(
0804: "childChild2");
0805: child.add(childChild2);
0806: breadthEnum = root.breadthFirstEnumeration();
0807: assertSame(root, breadthEnum.nextElement());
0808: assertSame(child, breadthEnum.nextElement());
0809: assertSame(node, breadthEnum.nextElement());
0810: assertSame(childChild, breadthEnum.nextElement());
0811: assertSame(childChild2, breadthEnum.nextElement());
0812: assertSame(nodeChild, breadthEnum.nextElement());
0813: assertSame(childChildChild, breadthEnum.nextElement());
0814: assertFalse(breadthEnum.hasMoreElements());
0815: testExceptionalCase(new ExceptionalCase() {
0816: @Override
0817: public void exceptionalAction() throws Exception {
0818: Enumeration<?> breadthEnum = childChildChild
0819: .breadthFirstEnumeration();
0820: breadthEnum.nextElement();
0821: breadthEnum.nextElement();
0822: }
0823: });
0824: }
0825:
0826: public void testIsNodeChild() throws Exception {
0827: assertFalse(node.isNodeChild(null));
0828: assertFalse(node.isNodeChild(node));
0829: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0830: child.setParent(node);
0831: assertFalse(node.isNodeChild(child));
0832: child.setParent(null);
0833: node.add(child);
0834: assertTrue(node.isNodeChild(child));
0835: }
0836:
0837: public void testGetFirstChild() throws Exception {
0838: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0839: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0840: root.add(node);
0841: root.add(child);
0842: assertSame(node, root.getFirstChild());
0843: testExceptionalCase(new NoSuchElementCase() {
0844: @Override
0845: public void exceptionalAction() throws Exception {
0846: node.getFirstChild();
0847: }
0848: });
0849: }
0850:
0851: public void testGetLastChild() throws Exception {
0852: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0853: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0854: root.add(child);
0855: root.add(node);
0856: assertSame(node, root.getLastChild());
0857: testExceptionalCase(new NoSuchElementCase() {
0858: @Override
0859: public void exceptionalAction() throws Exception {
0860: node.getLastChild();
0861: }
0862: });
0863: }
0864:
0865: public void testGetChildAfter() throws Exception {
0866: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0867: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode();
0868: DefaultMutableTreeNode child3 = new DefaultMutableTreeNode();
0869: root.add(child1);
0870: root.add(node);
0871: root.add(child3);
0872: assertEquals(node, root.getChildAfter(child1));
0873: assertEquals(child3, root.getChildAfter(node));
0874: assertNull(root.getChildAfter(child3));
0875: testExceptionalCase(new IllegalArgumentCase() {
0876: @Override
0877: public void exceptionalAction() throws Exception {
0878: node.getChildAfter(null);
0879: }
0880: });
0881: testExceptionalCase(new IllegalArgumentCase() {
0882: @Override
0883: public void exceptionalAction() throws Exception {
0884: node.getChildAfter(new DefaultMutableTreeNode());
0885: }
0886: });
0887: }
0888:
0889: public void testGetChildBefore() throws Exception {
0890: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0891: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode();
0892: DefaultMutableTreeNode child3 = new DefaultMutableTreeNode();
0893: root.add(child1);
0894: root.add(node);
0895: root.add(child3);
0896: assertNull(root.getChildBefore(child1));
0897: assertEquals(child1, root.getChildBefore(node));
0898: assertEquals(node, root.getChildBefore(child3));
0899: testExceptionalCase(new IllegalArgumentCase() {
0900: @Override
0901: public void exceptionalAction() throws Exception {
0902: node.getChildBefore(null);
0903: }
0904: });
0905: testExceptionalCase(new IllegalArgumentCase() {
0906: @Override
0907: public void exceptionalAction() throws Exception {
0908: node.getChildBefore(new DefaultMutableTreeNode());
0909: }
0910: });
0911: }
0912:
0913: public void testIsNodeSibling() throws Exception {
0914: assertFalse(node.isNodeSibling(null));
0915: assertTrue(node.isNodeSibling(node));
0916: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0917: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0918: root.add(child);
0919: root.add(node);
0920: assertTrue(node.isNodeSibling(child));
0921: assertTrue(child.isNodeSibling(node));
0922: node.setParent(null);
0923: assertFalse(child.isNodeSibling(node));
0924: }
0925:
0926: public void testGetSiblingCount() throws Exception {
0927: assertEquals(1, node.getSiblingCount());
0928: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0929: root.add(node);
0930: assertEquals(1, node.getSiblingCount());
0931: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0932: root.add(child);
0933: assertEquals(2, node.getSiblingCount());
0934: assertEquals(2, child.getSiblingCount());
0935: }
0936:
0937: public void testGetNextSibling() throws Exception {
0938: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0939: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0940: root.add(child);
0941: root.add(node);
0942: assertNull(root.getNextSibling());
0943: assertSame(node, child.getNextSibling());
0944: assertNull(node.getNextSibling());
0945: }
0946:
0947: public void testGetPreviousSibling() throws Exception {
0948: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0949: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0950: root.add(child);
0951: root.add(node);
0952: assertNull(root.getPreviousSibling());
0953: assertSame(child, node.getPreviousSibling());
0954: assertNull(child.getPreviousSibling());
0955: node.setParent(null);
0956: assertNull(node.getPreviousSibling());
0957: }
0958:
0959: public void testIsLeaf() throws Exception {
0960: assertTrue(node.isLeaf());
0961: node.add(new DefaultMutableTreeNode());
0962: assertFalse(node.isLeaf());
0963: }
0964:
0965: public void testGetFirstLeaf() throws Exception {
0966: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0967: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0968: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0969: root.add(child);
0970: root.add(node);
0971: child.add(childChild);
0972: assertSame(childChild, root.getFirstLeaf());
0973: assertSame(childChild, child.getFirstLeaf());
0974: assertSame(node, node.getFirstLeaf());
0975: }
0976:
0977: public void testGetLastLeaf() throws Exception {
0978: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0979: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
0980: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
0981: root.add(node);
0982: root.add(child);
0983: child.add(childChild);
0984: assertSame(node, node.getLastLeaf());
0985: assertSame(childChild, root.getLastLeaf());
0986: assertSame(childChild, child.getLastLeaf());
0987: }
0988:
0989: public void testGetNextLeaf() throws Exception {
0990: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0991: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
0992: "child");
0993: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode(
0994: "childChild");
0995: DefaultMutableTreeNode nodeChild = new DefaultMutableTreeNode(
0996: "nodeChild");
0997: root.add(node);
0998: root.add(child);
0999: child.add(childChild);
1000: node.add(nodeChild);
1001: assertNull(root.getNextLeaf());
1002: assertSame(childChild, node.getNextLeaf());
1003: assertNull(child.getNextLeaf());
1004: assertNull(childChild.getNextLeaf());
1005: assertSame(childChild, nodeChild.getNextLeaf());
1006: }
1007:
1008: public void testGetPreviousLeaf() throws Exception {
1009: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1010: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
1011: "child");
1012: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode(
1013: "childChild");
1014: DefaultMutableTreeNode nodeChild = new DefaultMutableTreeNode(
1015: "nodeChild");
1016: root.add(node);
1017: root.add(child);
1018: child.add(childChild);
1019: node.add(nodeChild);
1020: assertNull(node.getPreviousLeaf());
1021: assertNull(root.getPreviousLeaf());
1022: assertSame(nodeChild, child.getPreviousLeaf());
1023: assertSame(nodeChild, childChild.getPreviousLeaf());
1024: assertNull(nodeChild.getPreviousLeaf());
1025: }
1026:
1027: public void testGetLeafCount() throws Exception {
1028: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
1029: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
1030: DefaultMutableTreeNode childChild = new DefaultMutableTreeNode();
1031: DefaultMutableTreeNode nodeChild = new DefaultMutableTreeNode();
1032: root.add(node);
1033: root.add(child);
1034: child.add(childChild);
1035: node.add(nodeChild);
1036: assertEquals(2, root.getLeafCount());
1037: assertEquals(1, child.getLeafCount());
1038: assertEquals(1, nodeChild.getLeafCount());
1039: }
1040:
1041: public void testToString() throws Exception {
1042: assertNull(node.toString());
1043: node.setUserObject("user object");
1044: assertEquals("user object", node.toString());
1045: }
1046:
1047: public void testClone() throws Exception {
1048: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
1049: DefaultMutableTreeNode child = new DefaultMutableTreeNode();
1050: root.add(node);
1051: node.add(child);
1052: Object userObject = new Object();
1053: node.setUserObject(userObject);
1054: DefaultMutableTreeNode clone = (DefaultMutableTreeNode) node
1055: .clone();
1056: assertNull(clone.getParent());
1057: assertEquals(0, clone.getChildCount());
1058: assertSame(userObject, clone.getUserObject());
1059: }
1060:
1061: private abstract class ArrayIndexOutOfBoundsCase extends
1062: ExceptionalCase {
1063: @SuppressWarnings("unchecked")
1064: @Override
1065: public Class<?> expectedExceptionClass() {
1066: return ArrayIndexOutOfBoundsException.class;
1067: }
1068: }
1069:
1070: private abstract class IllegalStateCase extends ExceptionalCase {
1071: @SuppressWarnings("unchecked")
1072: @Override
1073: public Class<?> expectedExceptionClass() {
1074: return IllegalStateException.class;
1075: }
1076: }
1077:
1078: private abstract class NoSuchElementCase extends ExceptionalCase {
1079: @SuppressWarnings("unchecked")
1080: @Override
1081: public Class<?> expectedExceptionClass() {
1082: return NoSuchElementException.class;
1083: }
1084: }
1085: }
|