001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.modules.profiler.utilities.trees;
042:
043: import junit.framework.TestCase;
044: import org.netbeans.modules.profiler.utilities.trees.TreeDecimator.NodeFilter;
045: import java.util.ArrayList;
046: import java.util.Enumeration;
047: import java.util.List;
048: import javax.swing.tree.DefaultMutableTreeNode;
049: import javax.swing.tree.MutableTreeNode;
050: import javax.swing.tree.TreeNode;
051:
052: /**
053: *
054: * @author Jaroslav Bachorik
055: */
056: public class TreeDecimatorTest extends TestCase {
057: //~ Instance fields ----------------------------------------------------------------------------------------------------------
058:
059: private DefaultMutableTreeNode treeRoot;
060: private TreeDecimator<DefaultMutableTreeNode> decimator = new TreeDecimator<DefaultMutableTreeNode>() {
061: @Override
062: protected List<DefaultMutableTreeNode> getChildren(
063: DefaultMutableTreeNode aNode) {
064: List<DefaultMutableTreeNode> children = new ArrayList<DefaultMutableTreeNode>();
065: Enumeration<MutableTreeNode> childrenEnmu = aNode
066: .children();
067:
068: while (childrenEnmu.hasMoreElements()) {
069: children.add((DefaultMutableTreeNode) childrenEnmu
070: .nextElement());
071: }
072:
073: return children;
074: }
075:
076: @Override
077: protected void attachChildren(DefaultMutableTreeNode aNode,
078: List<DefaultMutableTreeNode> children) {
079: for (DefaultMutableTreeNode child : children) {
080: aNode.setParent(aNode);
081: }
082: }
083:
084: @Override
085: protected void detachChild(DefaultMutableTreeNode aNode,
086: DefaultMutableTreeNode child) {
087: aNode.remove(child);
088: }
089:
090: @Override
091: protected void detachChildren(DefaultMutableTreeNode aNode) {
092: aNode.removeAllChildren();
093: }
094: };
095:
096: //~ Constructors -------------------------------------------------------------------------------------------------------------
097:
098: public TreeDecimatorTest(String testName) {
099: super (testName);
100: }
101:
102: //~ Methods ------------------------------------------------------------------------------------------------------------------
103:
104: /**
105: * Test of decimate method, of class TreeDecimator.
106: */
107: public void testDecimateRoot() {
108: System.out.println("decimate Root");
109:
110: TreeDecimator.NodeFilter<DefaultMutableTreeNode> filter = new TreeDecimator.NodeFilter() {
111: public boolean match(Object node) {
112: return node.toString().equals("A");
113: }
114:
115: public boolean maymatch(Object node) {
116: return "A".startsWith(node.toString());
117: }
118: };
119:
120: DefaultMutableTreeNode result = decimator.decimate(treeRoot,
121: filter);
122: assertEquals("A", result.toString());
123: assertEquals(2, result.getChildCount());
124: }
125:
126: public void testDecimateAB() {
127: System.out.println("decimate AB");
128:
129: TreeDecimator.NodeFilter<DefaultMutableTreeNode> filter = new TreeDecimator.NodeFilter() {
130: public boolean match(Object node) {
131: return node.toString().equals("AB");
132: }
133:
134: public boolean maymatch(Object node) {
135: return "AB".startsWith(node.toString());
136: }
137: };
138:
139: DefaultMutableTreeNode result = decimator.decimate(treeRoot,
140: filter);
141: assertEquals("AB", result.toString());
142: assertEquals(0, result.getChildCount());
143: }
144:
145: public void testDecimateAC() {
146: System.out.println("decimate AC");
147:
148: TreeDecimator.NodeFilter<DefaultMutableTreeNode> filter = new TreeDecimator.NodeFilter() {
149: public boolean match(Object node) {
150: return node.toString().equals("AC");
151: }
152:
153: public boolean maymatch(Object node) {
154: return "AC".startsWith(node.toString());
155: }
156: };
157:
158: DefaultMutableTreeNode result = decimator.decimate(treeRoot,
159: filter);
160: assertEquals("AC", result.toString());
161: assertEquals(2, result.getChildCount());
162: }
163:
164: public void testDecimateACA() {
165: System.out.println("decimate ACA");
166:
167: TreeDecimator.NodeFilter<DefaultMutableTreeNode> filter = new TreeDecimator.NodeFilter() {
168: public boolean match(Object node) {
169: return node.toString().equals("ACA");
170: }
171:
172: public boolean maymatch(Object node) {
173: return "ACA".startsWith(node.toString());
174: }
175: };
176:
177: DefaultMutableTreeNode result = decimator.decimate(treeRoot,
178: filter);
179: assertEquals("ACA", result.toString());
180: assertEquals(0, result.getChildCount());
181: }
182:
183: public void testDecimateACAB() {
184: System.out.println("decimate ACAB");
185:
186: TreeDecimator.NodeFilter<DefaultMutableTreeNode> filter = new TreeDecimator.NodeFilter() {
187: public boolean match(Object node) {
188: return node.toString().equals("ACAB");
189: }
190:
191: public boolean maymatch(Object node) {
192: return "ACAB".startsWith(node.toString());
193: }
194: };
195:
196: DefaultMutableTreeNode result = decimator.decimate(treeRoot,
197: filter);
198: assertEquals("ACAB", result.toString());
199: assertEquals(0, result.getChildCount());
200: }
201:
202: @Override
203: protected void setUp() throws Exception {
204: treeRoot = new DefaultMutableTreeNode("A");
205:
206: DefaultMutableTreeNode nodeB = new DefaultMutableTreeNode("AB");
207: DefaultMutableTreeNode nodeC = new DefaultMutableTreeNode("AC");
208: DefaultMutableTreeNode nodeCA = new DefaultMutableTreeNode(
209: "ACA");
210: DefaultMutableTreeNode nodeCB = new DefaultMutableTreeNode(
211: "ACAB");
212:
213: treeRoot.add(nodeB);
214: treeRoot.add(nodeC);
215: nodeC.add(nodeCA);
216: nodeC.add(nodeCB);
217: }
218:
219: @Override
220: protected void tearDown() throws Exception {
221: treeRoot = null;
222: }
223: }
|