1 19 20 package org.openide.explorer.view; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import javax.swing.JPanel ; 28 import javax.swing.event.TreeModelEvent ; 29 import javax.swing.event.TreeModelListener ; 30 import javax.swing.tree.TreeNode ; 31 import org.netbeans.junit.NbTestCase; 32 import org.openide.explorer.ExplorerManager; 33 import org.openide.nodes.AbstractNode; 34 import org.openide.nodes.Children; 35 import org.openide.nodes.Node; 36 37 40 public class ContextTreeViewModelTest extends NbTestCase { 41 42 private static final int NO_OF_NODES = 3; 43 44 45 private Logger LOG = Logger.getLogger("TEST-" + getName()); 46 47 public ContextTreeViewModelTest(String name) { 48 super(name); 49 } 50 51 protected boolean runInEQ() { 52 return true; 53 } 54 55 protected Level logLevel() { 56 return Level.ALL; 57 } 58 59 public void testCheckThatTheModelFiresChangesAboutOnlyVisibleNodes() throws Throwable { 60 final AbstractNode root = new AbstractNode(new Children.Array()); 61 root.setName("test root"); 62 63 LOG.info("node created"); 64 65 root.getChildren().add(new Node [] { 66 createNode("kuk", true), 67 createNode("huk", true), 68 }); 69 70 LOG.info("two subnodes added"); 71 72 73 NodeTreeModel m = new ContextTreeView.NodeContextModel(); 74 75 LOG.info("model created"); 76 77 m.setNode(root); 78 79 LOG.info("its root node set"); 80 81 TreeNode visual = (TreeNode )m.getRoot(); 82 83 LOG.info("getting the root: " + visual); 84 85 waitEQ(); 86 87 88 assertEquals("Leaf nodes are not counted", 0, m.getChildCount(visual)); 89 90 91 Listener listener = new Listener (visual); 92 m.addTreeModelListener(listener); 93 94 LOG.info("listener added"); 95 96 Node n = createNode("blik", true); 97 98 LOG.info("node created"); 99 100 root.getChildren().add(new Node [] { n }); 101 102 LOG.info("node added"); 103 104 assertEquals("Leaf nodes are not counted even when added", 0, m.getChildCount(visual)); 105 assertEquals("Really added", n.getParentNode(), root); 106 listener.assertEvents("No events", 0); 107 108 LOG.info("events ok"); 109 110 root.getChildren().remove(new Node [] { n }); 111 listener.assertEvents("Still no events", 0); 112 assertNull("Removed", n.getParentNode()); 113 114 LOG.info("node removed"); 115 116 Node nonLeaf = createNode("nonleaf", false); 117 root.getChildren().add(new Node [] { nonLeaf }); 118 119 LOG.info("non-leaf added"); 120 121 assertEquals("One child is there", 1, m.getChildCount(visual)); 122 listener.assertEvents("This node is visible there", 1); 123 listener.assertIndexes("Added at position zero", new int[] { 0 }); 124 assertEquals("Really added", nonLeaf.getParentNode(), root); 125 126 LOG.info("all checked"); 127 128 assertAgressiveGC(); 129 130 root.getChildren().remove(new Node [] { nonLeaf }); 131 132 LOG.info("non-leaf removed"); 133 134 assertEquals("One child is away", 0, m.getChildCount(visual)); 135 assertNull("Removed", nonLeaf.getParentNode()); 136 listener.assertEvents("And it has been removed", 1); 137 listener.assertIndexes("Removed from position zero", new int[] { 0 }); 138 139 LOG.info("all ok"); 140 } 141 142 public void testABitMoreComplexAddAndRemoveEventCheck() throws Throwable { 143 final AbstractNode root = new AbstractNode(new Children.Array()); 144 root.setName("test root"); 145 146 LOG.info("root created"); 147 148 root.getChildren().add(new Node [] { 149 createNode("kuk", false), 150 createNode("huk", false), 151 }); 152 153 154 LOG.info("two nodes added"); 155 156 NodeTreeModel m = new ContextTreeView.NodeContextModel(); 157 m.setNode(root); 158 159 LOG.info("root set"); 160 161 TreeNode visual = (TreeNode )m.getRoot(); 162 waitEQ(); 163 164 LOG.info("visual is here: " + visual); 165 166 167 assertEquals("Initial size is two", 2, m.getChildCount(visual)); 168 169 Listener listener = new Listener (visual); 170 m.addTreeModelListener(listener); 171 172 LOG.info("listener added"); 173 174 Node [] arr = { 175 createNode("add1", false), createNode("add2", false) 176 }; 177 178 assertAgressiveGC(); 179 180 LOG.info("adding children"); 181 root.getChildren().add(arr); 182 183 LOG.info("children added"); 184 185 listener.assertEvents("One addition", 1); 186 listener.assertIndexes("after the two first", new int[] { 2, 3 }); 187 188 LOG.info("removing children"); 189 root.getChildren().remove(arr); 190 LOG.info("children removed"); 191 192 listener.assertEvents("One removal", 1); 193 listener.assertIndexes("from end", new int[] { 2, 3 }); 194 195 LOG.info("all well"); 196 } 197 198 public void testRemoveInMiddle() throws Throwable { 199 final AbstractNode root = new AbstractNode(new Children.Array()); 200 root.setName("test root"); 201 202 203 LOG.info("root added"); 204 205 root.getChildren().add(new Node [] { createNode("Ahoj", false) }); 206 207 LOG.info("a node added"); 208 209 Node [] first = { 210 createNode("kuk", false), 211 createNode("huk", false), 212 }; 213 214 215 LOG.info("adding more nodes"); 216 217 root.getChildren().add(first); 218 219 LOG.info("nodes added"); 220 221 222 NodeTreeModel m = new ContextTreeView.NodeContextModel(); 223 m.setNode(root); 224 225 LOG.info("root set"); 226 TreeNode visual = (TreeNode )m.getRoot(); 227 228 LOG.info("visual is here: " + visual); 229 230 waitEQ(); 231 232 233 assertEquals("Initial size is two", 3, m.getChildCount(visual)); 234 235 Listener listener = new Listener (visual); 236 m.addTreeModelListener(listener); 237 238 239 LOG.info("listener added"); 240 241 Node [] arr = { 242 createNode("add1", false), createNode("add2", false) 243 }; 244 245 assertAgressiveGC(); 246 247 root.getChildren().add(arr); 248 249 LOG.info("arr added"); 250 251 listener.assertEvents("One addition", 1); 252 listener.assertIndexes("after the three first", new int[] { 3, 4 }); 253 254 LOG.info("arr ok"); 255 256 root.getChildren().remove(first); 257 258 259 LOG.info("arr removed"); 260 261 listener.assertEvents("One removal", 1); 262 listener.assertIndexes("from end", new int[] { 1, 2 }); 263 264 LOG.info("all ok"); 265 } 266 267 private static Node createNode(String name, boolean leaf) { 268 AbstractNode n = new AbstractNode(leaf ? Children.LEAF : new Children.Array()); 269 n.setName(name); 270 return n; 271 } 272 273 private static void assertAgressiveGC() { 274 for (int i = 0; i < 10; i++) { 275 System.gc(); 276 } 277 } 278 279 private void waitEQ() throws Throwable { 280 287 } 288 289 private static class Panel extends JPanel 290 implements ExplorerManager.Provider { 291 private ExplorerManager em = new ExplorerManager(); 292 293 public ExplorerManager getExplorerManager() { 294 return em; 295 } 296 } 297 298 private class Listener implements TreeModelListener { 299 private int cnt; 300 private int[] indexes; 301 302 303 private TreeNode keep; 304 private List all = new ArrayList (); 305 306 public Listener(TreeNode keep) { 307 this.keep = keep; 308 addChildren(); 309 } 310 311 public void assertEvents(String msg, int cnt) throws Throwable { 312 waitEQ(); 313 assertEquals(msg, cnt, this.cnt); 314 this.cnt = 0; 315 } 316 public void assertIndexes(String msg, int[] arr) throws Throwable { 317 waitEQ(); 318 assertNotNull(msg + " there has to be some", indexes); 319 boolean bad = false; 320 if (arr.length != indexes.length) { 321 bad = true; 322 } else { 323 for (int i = 0; i < arr.length; i++) { 324 if (arr[i] != indexes[i]) { 325 bad = true; 326 } 327 } 328 } 329 if (bad) { 330 fail(msg + " expected: " + toStr(arr) + " was: " + toStr(indexes)); 331 } 332 333 this.indexes = null; 334 } 335 336 private String toStr(int[] arr) { 337 StringBuffer sb = new StringBuffer (); 338 String sep = "["; 339 for (int i = 0; i < arr.length; i++) { 340 sb.append(sep); 341 sb.append(arr[i]); 342 sep = ", "; 343 } 344 sb.append(']'); 345 return sb.toString(); 346 } 347 348 public void treeNodesChanged(TreeModelEvent treeModelEvent) { 349 cnt++; 350 } 351 352 public void treeNodesInserted(TreeModelEvent treeModelEvent) { 353 cnt++; 354 indexes = treeModelEvent.getChildIndices(); 355 } 356 357 public void treeNodesRemoved(TreeModelEvent treeModelEvent) { 358 cnt++; 359 indexes = treeModelEvent.getChildIndices(); 360 } 361 362 public void treeStructureChanged(TreeModelEvent treeModelEvent) { 363 cnt++; 364 } 365 366 private void addChildren() { 367 int cnt = keep.getChildCount(); 368 for (int i = 0; i < cnt; i++) { 369 all.add(keep.getChildAt(i)); 370 } 371 } 372 } 373 } 374 | Popular Tags |