KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > windows > TopComponentActivatedNodesTest


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.windows;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyVetoException JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31 import junit.textui.TestRunner;
32
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.junit.NbTestSuite;
35
36 import org.openide.explorer.ExplorerManager;
37 import org.openide.explorer.ExplorerPanel;
38 import org.openide.explorer.view.BeanTreeView;
39 import org.openide.explorer.view.TreeView;
40 import org.openide.nodes.AbstractNode;
41 import org.openide.nodes.Children;
42 import org.openide.nodes.Node;
43 import org.openide.util.Lookup;
44
45
46
47
48 /** Check the synchronization TC.getActivatedNodes and ExplorerManager.getSelectedNodes.
49  * Test should assure the fix of issue 31244.
50  *
51  * @author Jiri Rechtacek
52  */

53 public class TopComponentActivatedNodesTest extends NbTestCase {
54     /** top component we work on */
55     private TopComponent top;
56     
57     public TopComponentActivatedNodesTest(java.lang.String JavaDoc testName) {
58         super(testName);
59     }
60     
61     public static void main(java.lang.String JavaDoc[] args) {
62         TestRunner.run(suite());
63     }
64     
65     public static Test suite() {
66         TestSuite suite = new NbTestSuite(TopComponentActivatedNodesTest.class);
67         
68         return suite;
69     }
70     
71     protected boolean runInEQ() {
72         return true;
73     }
74     
75     private ExplorerPanel p;
76     private ExplorerManager em;
77     private Node[] nodes;
78     private PropertyChangeListener JavaDoc listenerEM, listenerTC;
79     
80     protected void setUp () {
81         System.setProperty("org.openide.util.Lookup", "-"); // no lookup
82

83         
84         p = new ExplorerPanel ();
85         em = p.getExplorerManager ();
86         
87         TreeView tv = new BeanTreeView ();
88         p.add (tv);
89         Children ch = new Children.Array ();
90         nodes = new Node[10];
91         for (int i = 0; i < 10; i++) {
92             nodes[i] = new AbstractNode (Children.LEAF);
93             nodes[i].setName ("Node" + i);
94         }
95         ch.add (nodes);
96         Node root = new AbstractNode (ch);
97         em.setRootContext (root);
98         
99         // check synchronixzation before
100
assertArrays ("INIT: getSelectedNodes equals getActivatedNodes.",
101             em.getSelectedNodes (), p.getActivatedNodes ());
102     }
103     
104     private void initListeners () {
105         listenerTC = new PropertyChangeListener JavaDoc () {
106             public void propertyChange (PropertyChangeEvent JavaDoc ev) {
107                 System.out.println("TC: PROP_ACTIVATED_NODES change!");
108 // try {
109
// Thread.sleep (1000);
110
// } catch (Exception e) {
111
// }
112
assertArrays ("FIRED TC CHANGE: getSelectedNodes equals PROP_ACTIVATED_NODES",
113                     em.getSelectedNodes (), p.getActivatedNodes ());
114             }
115         };
116         
117         p.addPropertyChangeListener (TopComponent.Registry.PROP_ACTIVATED_NODES, listenerTC);
118         
119         listenerEM = new PropertyChangeListener JavaDoc () {
120             public void propertyChange (PropertyChangeEvent JavaDoc ev) {
121                 if (ExplorerManager.PROP_SELECTED_NODES.equals (ev.getPropertyName ())) {
122                     System.out.println("EM: PROP_SELECTED_NODES change!");
123                     assertArrays ("FIRED EM CHANGE: PROP_SELECTED_NODES equals getActivatedNodes",
124                         ((Object JavaDoc[])ev.getNewValue ()), p.getActivatedNodes ());
125                 }
126             }
127         };
128         
129         em.addPropertyChangeListener (listenerEM);
130         
131     }
132     
133     private void removeListeners () {
134         em.removePropertyChangeListener (listenerEM);
135         p.removePropertyChangeListener (TopComponent.Registry.PROP_ACTIVATED_NODES, listenerTC);
136     }
137     
138     public void testOnceChange () {
139         SwingUtilities.invokeLater (new Runnable JavaDoc () {
140             public void run () {
141                 
142                 initListeners ();
143
144                 // select a node
145
try {
146                     em.setSelectedNodes (new Node[] { nodes[3], nodes[5] });
147
148                     Node[] activatedNodes = p.getActivatedNodes ();
149
150                     // check synchronixzation after
151
assertArrays ("ONCE CHANGE: getSelectedNodes equals getActivatedNodes.",
152                         em.getSelectedNodes (), p.getActivatedNodes ());
153
154                     // lookup
155
Lookup.Result result = p.getLookup ().lookup (new Lookup.Template (Node.class));
156                     Collection JavaDoc col = result.allInstances ();
157                     Iterator JavaDoc it = col.iterator ();
158                     Node[] lookupNodes = new Node[col.size ()];
159                     int i = 0;
160                     while (it.hasNext ()) {
161                         lookupNodes[i] = (Node)it.next ();
162                         i++;
163                     }
164
165                     // check nodes in lookup with acivated nodes
166
assertArrays ("LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes",
167                         lookupNodes, activatedNodes);
168
169                 } catch (PropertyVetoException JavaDoc pve) {
170                     fail ("Caught PropertyVetoException. msg:" + pve.getMessage ());
171                 } finally {
172                     removeListeners ();
173                 }
174                 
175             }
176         });
177     }
178     
179     public void testIntensiveChange () {
180         SwingUtilities.invokeLater (new Runnable JavaDoc () {
181             public void run () {
182                 
183                 initListeners ();
184
185                 // select a node
186
try {
187                     for (int i = 3; i < 8; i++)
188                     em.setSelectedNodes (new Node[] { nodes[i] });
189
190                     Node[] activatedNodes = p.getActivatedNodes ();
191
192                     // check synchronixzation after
193
assertArrays ("INTENSIVE CHANGES: getSelectedNodes equals getActivatedNodes.",
194                         em.getSelectedNodes (), activatedNodes);
195
196                     // lookup
197
Lookup.Result result = p.getLookup ().lookup (new Lookup.Template (Node.class));
198                     Collection JavaDoc col = result.allInstances ();
199                     Iterator JavaDoc it = col.iterator ();
200                     Node[] lookupNodes = new Node[col.size ()];
201                     int i = 0;
202                     while (it.hasNext ()) {
203                         lookupNodes[i] = (Node)it.next ();
204                         i++;
205                     }
206
207                     // check nodes in lookup with acivated nodes
208
assertArrays ("LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes",
209                         lookupNodes, em.getSelectedNodes ());
210
211                 } catch (PropertyVetoException JavaDoc pve) {
212                     fail ("Caught PropertyVetoException. msg:" + pve.getMessage ());
213                 } finally {
214                     removeListeners ();
215                 }
216             }
217         });
218     }
219     
220     public void testIntensiveChangeWithLookup () {
221         SwingUtilities.invokeLater (new Runnable JavaDoc () {
222             public void run () {
223                 
224                 initListeners ();
225                 // select a node
226
try {
227                     for (int i = 3; i < 8; i++)
228                     em.setSelectedNodes (new Node[] { nodes[i] });
229                 } catch (PropertyVetoException JavaDoc pve) {
230                     fail ("Caught PropertyVetoException. msg:" + pve.getMessage ());
231                 } finally {
232                     removeListeners ();
233                 }
234
235                 // get nodes from lookup
236
Lookup.Result result = p.getLookup ().lookup (new Lookup.Template (Node.class));
237                 Collection JavaDoc col = result.allInstances ();
238                 Iterator JavaDoc it = col.iterator ();
239                 Node[] lookupNodes = new Node[col.size ()];
240                 int i = 0;
241                 while (it.hasNext ()) {
242                     lookupNodes[i] = (Node)it.next ();
243                     i++;
244                 }
245
246                 // check nodes in lookup with acivated nodes
247
assertArrays ("LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes",
248                     lookupNodes, p.getActivatedNodes ());
249
250                 // check nodes in lookup with selected nodes
251
assertArrays ("LOOKUP AFTER INTENSIVE CHANGES: nodes in lookup == activated nodes",
252                     lookupNodes, em.getSelectedNodes ());
253             }
254         });
255     }
256     
257     
258     public void testInteroperabilityWithTopComponentRegistry () throws Exception JavaDoc {
259         final TopComponent tc = new TopComponent ();
260         final Lookup.Result res = tc.getLookup ().lookup (new Lookup.Template (Node.class));
261         
262         assertNull ("Empty arrays", tc.getActivatedNodes());
263         assertEquals ("Empty list of nodes", 0, res.allInstances().size ());
264         
265         class L implements PropertyChangeListener JavaDoc, org.openide.util.LookupListener {
266             public Object JavaDoc[] expectedArray;
267             public java.util.ArrayList JavaDoc events = new java.util.ArrayList JavaDoc ();
268             
269             public void resultChanged (org.openide.util.LookupEvent ev) {
270                 events.add (ev);
271             }
272             
273             public void propertyChange (PropertyChangeEvent JavaDoc ev) {
274                 if (TopComponent.Registry.PROP_CURRENT_NODES.equals (ev.getPropertyName ())) {
275                     assertArrays ("Should be the same", tc.getActivatedNodes(), expectedArray);
276                     assertArrays (
277                         "Also in lookup. ",
278                         res.allInstances ().toArray (),
279                         expectedArray
280                     );
281                 }
282                 events.add (ev);
283             }
284         }
285         L l = new L ();
286         res.addLookupListener(l);
287
288         tc.requestActive ();
289         assertEquals ("Really activated", tc, TopComponent.getRegistry ().getActivated ());
290         try {
291             TopComponent.getRegistry ().addPropertyChangeListener (l);
292             
293             Node[] arr = { Node.EMPTY };
294             l.expectedArray = arr;
295             tc.setActivatedNodes (arr);
296             
297             Object JavaDoc[] ev = l.events.toArray ();
298             assertEquals ("Three events", 3, ev.length);
299             assertEquals ("First is lookup change", org.openide.util.LookupEvent.class, ev[0].getClass ());
300             assertEquals ("Second is prop change", PropertyChangeEvent JavaDoc.class, ev[1].getClass ());
301             assertEquals ("Third is prop change", PropertyChangeEvent JavaDoc.class, ev[2].getClass ());
302             
303             assertEquals (TopComponent.Registry.PROP_ACTIVATED_NODES, ((PropertyChangeEvent JavaDoc)ev[1]).getPropertyName());
304             assertEquals (TopComponent.Registry.PROP_CURRENT_NODES, ((PropertyChangeEvent JavaDoc)ev[2]).getPropertyName());
305         } finally {
306             TopComponent.getRegistry ().removePropertyChangeListener (l);
307         }
308     }
309     
310     private void assertArrays (String JavaDoc msg, Object JavaDoc[] arr1, Object JavaDoc[] arr2) {
311         // DEBUG MSG log content of arrays
312
// System.out.println("do ["+msg+"]: ");
313
// if (arr1 != null) for (int i = 0; i < arr1.length; i++) System.out.println("Arr1: " + i + ". " + arr1[i]);
314
// if (arr2 != null) for (int i = 0; i < arr2.length; i++) System.out.println("Arr2: " + i + ". " + arr2[i]);
315
// System.out.println("done!");
316
// END OF DEBUG MSG
317
if (arr1 == null && arr2 == null) return ;
318         if (arr1 == null) {
319             if (arr2.length == 0) {
320                 return ;
321             } else {
322                 fail (msg + " BUT: Array1 was null Array2 was " + java.util.Arrays.asList (arr2));
323             }
324         }
325         if (arr2 == null) {
326             if (arr1.length == 0) {
327                 return ;
328             } else {
329                 fail (msg + " BUT: Array2 was null Array1 was " + java.util.Arrays.asList (arr1));
330             }
331         }
332         if (arr1.length != arr2.length) fail (msg + "Arrays have a diferent size. First: " + java.util.Arrays.asList (arr1) + " second: " + java.util.Arrays.asList (arr2));
333         //Arrays.sort (arr1);
334
//Arrays.sort (arr2);
335
for (int i = 0; i < arr1.length; i++) {
336             if (! arr1[i].equals (arr2[i]) ) {
337                 fail (msg + " BUT: excepted: " + arr1[i] + ", was: " + arr2[i]);
338             }
339         }
340     }
341     
342 }
343
Popular Tags