1 19 20 package org.openide.nodes; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.*; 24 import junit.textui.TestRunner; 25 import java.util.Enumeration ; 26 import org.openide.nodes.Node; 27 import org.netbeans.junit.*; 28 29 32 public class ChildrenArrayNodeAtShouldNotBeSlowTest extends NbTestCase { 33 34 private long time; 35 36 private static HashMap times = new HashMap (); 37 38 private Node node; 39 40 41 public ChildrenArrayNodeAtShouldNotBeSlowTest (String name) { 42 super(name); 43 } 44 45 protected void setUp() throws Exception { 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 for (int i = 0; i < 5; i++) { 64 createChildren (); 65 } 66 67 time = System.currentTimeMillis (); 68 } 69 70 71 private Integer getNumber () { 72 try { 73 java.util.regex.Matcher 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 ex) { 77 ex.printStackTrace(); 78 fail ("Name: " + getName () + " does not represent number"); 79 return null; 80 } 81 } 82 83 protected void tearDown() throws Exception { 84 node = null; 85 86 long now = System.currentTimeMillis (); 87 88 times.put (getNumber (), new Long (now - time)); 89 90 assertNumbersAreSane (); 92 93 } 94 95 private void createChildren () { 96 int middle = node.getChildren ().getNodesCount () / 2; 97 String 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 { 110 createChildren (); 111 } 112 113 public void test140 () throws java.io.IOException { 114 createChildren (); 115 } 116 117 public void test599 () throws java.io.IOException { 118 createChildren (); 119 } 120 121 public void test1245 () throws java.io.IOException { 122 createChildren (); 123 } 124 125 public void test3553 () throws java.io.IOException { 126 createChildren (); 127 } 128 129 public void test10746 () throws Exception { 130 createChildren (); 131 } 132 133 134 private void assertNumbersAreSane () { 135 StringBuffer error = new StringBuffer (); 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 l = (Long )en.getValue (); 148 if (l.longValue () > max) { 149 max = l.longValue (); 150 maxIndex = ((Integer )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 |