KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.BorderLayout JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.beans.PropertyVetoException JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import javax.swing.JFrame JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import junit.framework.AssertionFailedError;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.explorer.ExplorerManager;
32 import org.openide.nodes.AbstractNode;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.Node;
35
36 /**
37  * Tests for inconsistnecy in tree structures when getters change structure
38  */

39 public class TreeView48993Test extends NbTestCase {
40     
41     private static final int NO_OF_NODES = 3;
42     /** exception thrown in AWT thread */
43     private static Throwable JavaDoc exc;
44     
45     JFrame JavaDoc f;
46     
47     static {
48         System.setProperty("sun.awt.exception.handler", TreeView48993Test.class.getName());
49     }
50     
51     /** Needed because we implement the handler.
52      */

53     public TreeView48993Test(){
54         super("");
55     }
56     
57     public TreeView48993Test(String JavaDoc name) {
58         super(name);
59     }
60     
61     protected void setUp() {
62         exc = null;
63     }
64     
65     protected void tearDown() {
66         if (f != null) {
67             f.setVisible(false);
68             f.dispose();
69         }
70         
71         if (exc != null) {
72             AssertionFailedError ass = new AssertionFailedError("There should be no exception in AWT");
73             ass.initCause(exc);
74             throw ass;
75         }
76     }
77     
78     protected int timeOut() {
79         return 15000; // 15s
80
}
81
82     public void testNotReentrantWhenDoingUpdate() throws Throwable JavaDoc {
83         f = new JFrame JavaDoc();
84         final MyChildren ch = new MyChildren();
85         final AbstractNode root = new AbstractNode(ch);
86         root.setName("test root");
87         
88         ch.keys(new String JavaDoc[] {"A", "B", "C", "D" } );
89         
90         final Node[] all = ch.getNodes();
91         final UglyNode u = (UglyNode)all[2];
92         
93         SwingUtilities.invokeLater(new Runnable JavaDoc() { public void run() {
94             Panel JavaDoc p = new Panel JavaDoc();
95             p.getExplorerManager().setRootContext(root);
96             try {
97                 p.getExplorerManager().setSelectedNodes(new Node[] {all[0]});
98             } catch (PropertyVetoException JavaDoc pve) {
99                 fail(pve.toString());
100             }
101             
102             BeanTreeView btv = new BeanTreeView();
103             p.add(BorderLayout.CENTER, btv);
104             
105             f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
106             f.getContentPane().add(BorderLayout.CENTER, p);
107             f.setBounds(300, 300, 200, 200);
108             f.setVisible(true);
109         }});
110         
111         u.waitOn(10000);
112         
113         waitForAWT(); // Thread.sleep(5000); // wait for the window to finish opening
114

115         assertTrue("Somebody asked for the icon", u.flag);
116         assertEquals("Still four children", 4, ch.getNodesCount());
117         
118         ((VisualizerNode)Visualizer.findVisualizer(u)).getIcon(true, true); // this clears the internal cache
119

120         synchronized (u) {
121             u.flag = false;
122             u.keys = new String JavaDoc[] {"C" };
123             ch.keys(new String JavaDoc[] { "B", "C", "D", "A" });
124             u.fire();
125             u.waitOn(10000);
126         }
127         Thread.sleep(1000);
128         
129         assertTrue("Somebody asked for the display name again", u.flag);
130         assertEquals("Just one child", 1, ch.getNodesCount());
131     }
132     
133     public void testSomeChildrenDisappearDuringFirstPaint() throws Throwable JavaDoc {
134         assertFalse("Won't work from AWT thread", SwingUtilities.isEventDispatchThread());
135         
136         f = new JFrame JavaDoc();
137         final MyChildren ch = new MyChildren();
138         final AbstractNode root = new AbstractNode(ch);
139         root.setName("test root");
140         
141         ch.keys(new String JavaDoc[] {"A", "B", "C", "D" } );
142         
143         Node[] all = ch.getNodes();
144         final UglyNode u = (UglyNode)all[1];
145         
146         synchronized (u) {
147             Panel JavaDoc p = new Panel JavaDoc();
148             p.getExplorerManager().setRootContext(root);
149             p.getExplorerManager().setSelectedNodes(new Node[] {all[0]});
150             
151             BeanTreeView btv = new BeanTreeView();
152             p.add(BorderLayout.CENTER, btv);
153             
154             
155             f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
156             f.getContentPane().add(BorderLayout.CENTER, p);
157             f.pack();
158             f.setVisible(true);
159             u.waitOn(10000000);
160             
161             Thread.sleep(2000);
162             ((VisualizerNode)Visualizer.findVisualizer(u)).getIcon(true, true); // this clears the internal cache
163

164             u.flag = false;
165             u.keys = new String JavaDoc[] { "B", "C", "D" };
166         }
167         
168         f.setBounds(300, 300, 200, 200);
169         f.setVisible(false);
170         f.invalidate();
171         f.validate();
172         f.repaint();
173         f.setVisible(true);
174         
175         u.waitOn(10000000); // Waits for getIcon to run
176

177         waitForAWT();
178         
179         assertTrue("Somebody asked for the icon", u.flag);
180         assertEquals("And now we 3 ", 3, ch.getNodesCount());
181     }
182     
183     /**
184      * Waits for AWT queue to finish queued processing.
185      */

186     private static void waitForAWT() throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
187         for (int i=0; i<5; i++) {
188             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {public void run() {}});;
189         }
190     }
191     
192     private static class UglyNode extends AbstractNode {
193         private boolean flag;
194         private Object JavaDoc[] keys;
195         boolean ticked;
196         
197         public UglyNode(String JavaDoc name) {
198             super(Children.LEAF);
199             setName(name);
200         }
201         
202         public String JavaDoc toString() {
203             return getClass().getName() + "@" + Integer.toHexString(hashCode());
204             
205         }
206         
207         public Image JavaDoc getIcon(int type) {
208             synchronized(this) {
209                 tick();
210                 flag = true;
211                 if (keys != null) {
212                     ((MyChildren)getParentNode().getChildren()).keys(keys);
213                 }
214                 return super.getIcon(type);
215             }
216         }
217         
218         public void fire() {
219             fireIconChange();
220         }
221         
222         /**
223          * @return true if returning due to timeout
224          */

225         public boolean waitOn(int timeout) {
226             synchronized(this) {
227                 boolean tck;
228                 if(!ticked) { //while (!ticked) {
229
try {
230                         wait(timeout);
231                     } catch (InterruptedException JavaDoc e) {
232                         throw new InternalError JavaDoc();
233                     }
234                 }
235                 tck = ticked;
236                 ticked = false; // reusable
237
return !tck;
238             }
239         }
240         
241         public void tick() {
242             synchronized(this) {
243                 ticked = true;
244                 notifyAll();
245             }
246         }
247         
248     }
249     
250     public void testRemoveOneOfEqualsChildren() throws Throwable JavaDoc {
251         class EqualsNode extends AbstractNode {
252             private Object JavaDoc token;
253             
254             public EqualsNode(String JavaDoc name, Object JavaDoc token) {
255                 super(Children.LEAF);
256                 this.token = token;
257                 setName(name);
258             }
259             
260             public boolean equals(Object JavaDoc obj) {
261                 if (obj instanceof EqualsNode) {
262                     return ((EqualsNode)obj).token.equals(token);
263                 }
264                 return super.equals(obj);
265             }
266         }
267         
268         class EqualsChildren extends Children.Keys {
269             Object JavaDoc token;
270             EqualsChildren(Object JavaDoc token) {
271                 this.token = token;
272             }
273             
274             protected Node[] createNodes(Object JavaDoc key) {
275                 return new Node[] {new EqualsNode(key.toString(), token)};
276             }
277             
278             public void keys(Object JavaDoc[] keys) {
279                 setKeys(keys);
280             }
281         }
282         
283         Object JavaDoc token = new Object JavaDoc();
284         assertFalse("Won't work from AWT thread", SwingUtilities.isEventDispatchThread());
285         
286         EqualsChildren ch = new EqualsChildren(token);
287         AbstractNode root = new AbstractNode(ch);
288         root.setName("test root");
289         
290         Panel JavaDoc p = new Panel JavaDoc();
291         p.getExplorerManager().setRootContext(root);
292         BeanTreeView btv = new BeanTreeView();
293         p.add(BorderLayout.CENTER, btv);
294         
295         f = new JFrame JavaDoc();
296         f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
297         f.getContentPane().add(BorderLayout.CENTER, p);
298         f.pack();
299         f.setVisible(true);
300         waitForAWT();
301         
302         ch.keys(new String JavaDoc[] {"A", "B"} );
303         
304         waitForAWT();
305         
306         ch.keys(new String JavaDoc[] {"a", "B"} );
307         
308         waitForAWT();
309     }
310     
311     
312     
313     
314     private static class MyChildren extends Children.Keys {
315         protected Node[] createNodes(Object JavaDoc key) {
316             return new Node[] {new UglyNode(key.toString())};
317         }
318         
319         public void keys(Object JavaDoc[] keys) {
320             setKeys(keys);
321         }
322     }
323     
324     /** The name MUST be handle and MUST be public */
325     public static void handle(Throwable JavaDoc t) {
326         exc = t;
327     }
328     
329     private static class Panel extends JPanel JavaDoc
330             implements ExplorerManager.Provider {
331         private ExplorerManager em = new ExplorerManager();
332         
333         public ExplorerManager getExplorerManager() {
334             return em;
335         }
336     }
337 }
338
Popular Tags