KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > ChildrenArrayNodeAtShouldNotBeSlowTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.nodes;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.*;
24 import junit.textui.TestRunner;
25 import java.util.Enumeration JavaDoc;
26 import org.openide.nodes.Node;
27 import org.netbeans.junit.*;
28
29 /** If using Children.Array the node.getChildren().getNodeAt(int) used to iterate slowly.
30  * @author Jaroslav Tulach
31  */

32 public class ChildrenArrayNodeAtShouldNotBeSlowTest extends NbTestCase {
33     /** start time of the test */
34     private long time;
35     /** table with test resutls Integer -> Long */
36     private static HashMap times = new HashMap ();
37     /** node to work on */
38     private Node node;
39     
40     
41     public ChildrenArrayNodeAtShouldNotBeSlowTest (String JavaDoc name) {
42         super(name);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46         int count = getNumber ().intValue ();
47         
48         final Node[] arr = new Node[count];
49         for (int i = 0; i < count; i++) {
50             AbstractNode n = new AbstractNode (Children.LEAF);
51             n.setName (String.valueOf (i));
52             arr[i] = n;
53         }
54
55         Children.Array ch = new Children.Array ();
56         ch.add (arr);
57         node = new AbstractNode (ch);
58         
59         assertEquals (count, node.getChildren ().getNodesCount ());
60         assertEquals (String.valueOf (count - 1), node.getChildren ().getNodeAt (count - 1).getName ());
61
62         // warmup a bit
63
for (int i = 0; i < 5; i++) {
64             createChildren ();
65         }
66         
67         time = System.currentTimeMillis ();
68     }
69     
70     /** @return the size of this test */
71     private Integer JavaDoc getNumber () {
72         try {
73             java.util.regex.Matcher JavaDoc m = java.util.regex.Pattern.compile ("test[a-zA-Z]*([0-9]+)").matcher (getName ());
74             assertTrue ("Name does not contain numbers: " + getName (), m.find ());
75             return Integer.valueOf (m.group (1));
76         } catch (Exception JavaDoc ex) {
77             ex.printStackTrace();
78             fail ("Name: " + getName () + " does not represent number");
79             return null;
80         }
81     }
82     
83     protected void tearDown() throws Exception JavaDoc {
84         node = null;
85         
86         long now = System.currentTimeMillis ();
87         
88         times.put (getNumber (), new Long JavaDoc (now - time));
89
90         // and verify
91
assertNumbersAreSane ();
92         
93     }
94     
95     private void createChildren () {
96         int middle = node.getChildren ().getNodesCount () / 2;
97         String JavaDoc middleName = String.valueOf (middle);
98         Node prev = null;
99         for (int i = 0; i < 100000; i++) {
100             Node n = node.getChildren ().getNodeAt (middle);
101             if (prev != null) {
102                 assertSame ("The node is still the same", prev, n);
103             }
104             prev = n;
105             assertEquals (middleName, n.getName ());
106         }
107     }
108     
109     public void test10 () throws Exception JavaDoc {
110         createChildren ();
111     }
112     
113     public void test140 () throws java.io.IOException JavaDoc {
114         createChildren ();
115     }
116     
117     public void test599 () throws java.io.IOException JavaDoc {
118         createChildren ();
119     }
120
121     public void test1245 () throws java.io.IOException JavaDoc {
122         createChildren ();
123     }
124     
125     public void test3553 () throws java.io.IOException JavaDoc {
126         createChildren ();
127     }
128     
129     public void test10746 () throws Exception JavaDoc {
130         createChildren ();
131     }
132     
133     /** Compares that the numbers are in sane bounds */
134     private void assertNumbersAreSane () {
135         StringBuffer JavaDoc error = new StringBuffer JavaDoc ();
136         long min = Long.MAX_VALUE;
137         long max = Long.MIN_VALUE;
138         int maxIndex = -1;
139         {
140             Iterator it = times.entrySet ().iterator ();
141             int cnt = 0;
142             while (it.hasNext ()) {
143                 Map.Entry en = (Map.Entry)it.next ();
144                 error.append ("Test "); error.append (en.getKey ());
145                 error.append (" took "); error.append (en.getValue ());
146                 
147                 Long JavaDoc l = (Long JavaDoc)en.getValue ();
148                 if (l.longValue () > max) {
149                     max = l.longValue ();
150                     maxIndex = ((Integer JavaDoc)en.getKey ()).intValue ();
151                 }
152                 if (l.longValue () < min) min = l.longValue ();
153                 error.append (" ms\n");
154                 
155                 cnt++;
156             }
157         }
158         
159         
160         if (min * 10 < max && maxIndex > 3) {
161             fail ("Too big differences when various number of shadows is used:\n" + error.toString ());
162         }
163         
164         System.err.println(error.toString ());
165     }
166     
167 }
168
169
Popular Tags