KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > ContextTreeViewModelTest


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.explorer.view;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.event.TreeModelEvent JavaDoc;
29 import javax.swing.event.TreeModelListener JavaDoc;
30 import javax.swing.tree.TreeNode JavaDoc;
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 /**
38  * Tests for class ContextTreeViewTest
39  */

40 public class ContextTreeViewModelTest extends NbTestCase {
41     
42     private static final int NO_OF_NODES = 3;
43
44
45     private Logger JavaDoc LOG = Logger.getLogger("TEST-" + getName());
46     
47     public ContextTreeViewModelTest(String JavaDoc name) {
48         super(name);
49     }
50     
51     protected boolean runInEQ() {
52         return true;
53     }
54
55     protected Level JavaDoc logLevel() {
56         return Level.ALL;
57     }
58     
59     public void testCheckThatTheModelFiresChangesAboutOnlyVisibleNodes() throws Throwable JavaDoc {
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 JavaDoc[] {
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 JavaDoc visual = (TreeNode JavaDoc)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 JavaDoc listener = new Listener JavaDoc(visual);
92         m.addTreeModelListener(listener);
93
94         LOG.info("listener added");
95
96         Node JavaDoc n = createNode("blik", true);
97
98         LOG.info("node created");
99         
100         root.getChildren().add(new Node JavaDoc[] { 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 JavaDoc[] { n });
111         listener.assertEvents("Still no events", 0);
112         assertNull("Removed", n.getParentNode());
113
114         LOG.info("node removed");
115         
116         Node JavaDoc nonLeaf = createNode("nonleaf", false);
117         root.getChildren().add(new Node JavaDoc[] { 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 JavaDoc[] { 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 JavaDoc {
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 JavaDoc[] {
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 JavaDoc visual = (TreeNode JavaDoc)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 JavaDoc listener = new Listener JavaDoc(visual);
170         m.addTreeModelListener(listener);
171
172         LOG.info("listener added");
173         
174         Node JavaDoc[] 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 JavaDoc {
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 JavaDoc[] { createNode("Ahoj", false) });
206         
207         LOG.info("a node added");
208
209         Node JavaDoc[] 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 JavaDoc visual = (TreeNode JavaDoc)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 JavaDoc listener = new Listener JavaDoc(visual);
236         m.addTreeModelListener(listener);
237
238
239         LOG.info("listener added");
240         
241         Node JavaDoc[] 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 JavaDoc createNode(String JavaDoc 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 JavaDoc {
280         /*
281         try {
282             javax.swing.SwingUtilities.invokeAndWait (new Runnable () { public void run () { } } );
283         } catch (java.lang.reflect.InvocationTargetException ex) {
284             throw ex.getTargetException ();
285         }
286          */

287     }
288     
289     private static class Panel extends JPanel JavaDoc
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 JavaDoc {
299         private int cnt;
300         private int[] indexes;
301
302
303         private TreeNode JavaDoc keep;
304         private List JavaDoc all = new ArrayList JavaDoc();
305
306         public Listener(TreeNode JavaDoc keep) {
307             this.keep = keep;
308             addChildren();
309         }
310         
311         public void assertEvents(String JavaDoc msg, int cnt) throws Throwable JavaDoc {
312             waitEQ();
313             assertEquals(msg, cnt, this.cnt);
314             this.cnt = 0;
315         }
316         public void assertIndexes(String JavaDoc msg, int[] arr) throws Throwable JavaDoc {
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 JavaDoc toStr(int[] arr) {
337             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
338             String JavaDoc 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 JavaDoc treeModelEvent) {
349             cnt++;
350         }
351         
352         public void treeNodesInserted(TreeModelEvent JavaDoc treeModelEvent) {
353             cnt++;
354             indexes = treeModelEvent.getChildIndices();
355         }
356         
357         public void treeNodesRemoved(TreeModelEvent JavaDoc treeModelEvent) {
358             cnt++;
359             indexes = treeModelEvent.getChildIndices();
360         }
361         
362         public void treeStructureChanged(TreeModelEvent JavaDoc 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