KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > nodes2looks > LookNodeLookupTest


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.netbeans.api.nodes2looks;
21
22 import junit.framework.*;
23 import junit.textui.TestRunner;
24 import java.util.*;
25 import org.netbeans.spi.looks.*;
26 import org.openide.nodes.*;
27 import org.openide.cookies.InstanceCookie;
28
29 import org.netbeans.junit.*;
30 import org.openide.util.Lookup;
31
32 import org.openide.util.LookupListener;
33 import org.openide.util.lookup.*;
34
35 /**
36  * @author Jaroslav Tulach, Jiri Rechtacek
37  */

38 public class LookNodeLookupTest extends NbTestCase {
39     public LookNodeLookupTest(String JavaDoc name) {
40         super(name);
41     }
42
43     public static void main(String JavaDoc[] args) {
44         TestRunner.run(new NbTestSuite(LookNodeLookupTest.class));
45     }
46
47     protected void setUp() throws Exception JavaDoc {
48         super.setUp();
49         TestUtil.setUpRegistryToDefault();
50     }
51     
52     public void testChangesAreFiredFromLookup () {
53         Collection ro = new ArrayList();
54         TestLookupLook look = new TestLookupLook( "TLT" );
55         
56         Node node = Nodes.node (ro, look);
57
58         checkInstanceInGetCookie ( ro, new Node.Cookie () {}, look, node);
59         checkInstanceInGetLookup ( ro, new Node.Cookie () {}, look, node, true);
60         checkInstanceInGetLookup ( ro, new Node.Cookie () {}, look, node, true);
61         checkInstanceInGetLookup ( ro, "Some string", look, node, true);
62         
63     }
64     
65     public void testInstanceCookieOfInLookup () throws Exception JavaDoc {
66                 
67         Object JavaDoc rep = new AbstractNode (Children.LEAF);
68         Look look = Nodes.nodeLook ();
69         Node node = Nodes.node (rep, look);
70         
71         InstanceCookie.Of icOf = (InstanceCookie.Of)node.getLookup ().lookup (InstanceCookie.Of.class);
72         
73         assertNotNull ("Ic cookie should not be null.", icOf);
74         assertNotNull ("Ro from LookNode's lookup.", icOf.instanceCreate());
75         assertEquals ("Sample look ro is same ro lookup.", icOf.instanceCreate(), rep);
76         
77     }
78
79     public void testChangesAreFiredFromLookupThruFilterNode () {
80         Collection ro = new ArrayList();
81         TestLookupLook look = new TestLookupLook( "TLT" );
82         
83         Node node = new FilterNode (Nodes.node (ro, look));
84
85         checkInstanceInGetLookup ( ro, new Node.Cookie () {}, look, node, true);
86         checkInstanceInGetLookup ( ro, "Some string", look, node, true);
87         
88     }
89
90     public void testChangesAreFiredFromLookupThruFilterNodeWithOverWrittenGetCookie () {
91         
92         final Node.Cookie myInstance = new Node.Cookie () { };
93         
94         Collection ro = new ArrayList();
95         TestLookupLook look = new TestLookupLook( "TLT" );
96         
97         Node node = new FilterNode (Nodes.node (ro, look)) {
98             public Node.Cookie getCookie (Class JavaDoc clazz) {
99                 if (clazz == myInstance.getClass ()) {
100                     return myInstance;
101                 }
102                 return super.getCookie (clazz);
103             }
104         };
105
106         checkInstanceInGetCookie (ro, new Node.Cookie () {}, look, node);
107         checkInstanceInGetLookup (ro, new Node.Cookie () {}, look, node, true);
108         // by overwriting the FilterNode.getCookie we disable enhanced support
109
// for non-cookie objects in original lookup
110
checkInstanceInGetLookup (ro, "Some string", look, node, false);
111         
112         assertEquals ("It is possible to get myInstance from getCookie", myInstance, node.getCookie (myInstance.getClass ()));
113         assertEquals ("It also possible to get it from getLookup", myInstance, node.getLookup ().lookup (myInstance.getClass ()));
114        
115     }
116
117     public void testOldEnvInLookup() {
118
119         final String JavaDoc oldCookie = "OLD";
120         final String JavaDoc newCookie = "NEW";
121         
122         Collection ro = new ArrayList();
123         ro.add( oldCookie );
124         TestLookupLook look = new TestLookupLook( "OETLT" );
125         
126         Node n = Nodes.node( ro, look );
127         n.getLookup().lookup( Object JavaDoc.class ); // To initialize
128

129         assertEquals( "Old items should be empty.", 0, look.getValues().size() );
130         
131         ro.remove( oldCookie );
132         ro.add( newCookie );
133         
134         look.lookupChange( ro );
135         
136         Lookup newLookup = n.getLookup();
137         Collection oldValues = look.getValues();
138         
139         assertTrue( "New should conntain newCookie. ", newCookie == newLookup.lookup( String JavaDoc.class ) );
140         assertEquals( "1 string in new Lookup ", 1, newLookup.lookup( new Lookup.Template( String JavaDoc.class ) ).allItems().size() );
141         
142         assertTrue( "Old should conntain oldCookie. ", oldValues.contains( oldCookie ) );
143         assertEquals( "1 string in old Lookup ", 2, oldValues.size() );
144     }
145     
146     public void testLookupNoInit() {
147         
148         Collection ro = new ArrayList();
149         TestLookupLook look = new TestLookupLook( "OETLT" );
150         Node n = Nodes.node( ro, look );
151         
152         n.getIcon( 0 );
153         n.getDisplayName();
154         n.getName();
155         
156         assertEquals( "getLookupItems should not be called", 0, look.getLookupItemsCallCount );
157         
158     }
159     
160     
161     private void checkInstanceInGetCookie (Collection ro, Object JavaDoc obj, TestLookupLook look, Node node) {
162         
163         Listener listener = new Listener ();
164         node.addNodeListener(listener);
165         
166         assertNull ("The object is not there yet", node.getCookie (obj.getClass ()));
167         
168         ro.add (obj);
169         look.lookupChange( ro );
170         listener.assertEvents ("One change in node", 1, -1);
171
172         if (obj instanceof Node.Cookie) {
173             assertEquals ("Can access cookie in the content", obj, node.getCookie (obj.getClass ()));
174         } else {
175             assertNull ("Cannot access noncookie in the content", node.getCookie (obj.getClass ()));
176         }
177
178         ro.remove (obj);
179         look.lookupChange( ro );
180         listener.assertEvents ("One change in node", 1, -1);
181          
182     }
183     
184     private void checkInstanceInGetLookup (Collection ro, Object JavaDoc obj, TestLookupLook look, Node node, boolean shouldBeThere) {
185         
186         Listener listener = new Listener ();
187         Lookup.Result res = node.getLookup ().lookup (new Lookup.Template (obj.getClass ()));
188         Collection ignore = res.allItems ();
189         res.addLookupListener(listener);
190
191         ro.add (obj);
192         look.lookupChange( ro );
193         if (shouldBeThere) {
194             listener.assertEvents ("One change in node's lookup (add)", -1, 1);
195             assertEquals ("Can access object in content via lookup", obj, node.getLookup ().lookup (obj.getClass ()));
196         } else {
197             assertNull ("Cannot access object in content via lookup", node.getLookup ().lookup (obj.getClass ()));
198         }
199             
200         
201         ro.remove (obj);
202         look.lookupChange( ro );
203         if (shouldBeThere) {
204             listener.assertEvents ("One change in node's lookup (remove)", -1, 1);
205         }
206         assertNull ("Cookie is removed", node.getLookup ().lookup (obj.getClass ()));
207         
208     }
209     
210     
211     //
212
// Test to see correct behaviour from getCookie to lookup
213
//
214
public void testNodeIsInItsLookup () {
215         Collection ro = new ArrayList();
216         TestLookupLook look = new TestLookupLook( "TLT" );
217         
218         
219         Node n = Nodes.node (ro, look);
220         ro.add (n);
221         look.lookupChange( ro );
222         assertEquals ("Node is there", n, n.getLookup ().lookup (Node.class));
223         
224     }
225     
226     
227     private void checkInstanceInLookup (Node.Cookie obj, CookieSet ic, Lookup l) {
228         Listener listener = new Listener ();
229         Lookup.Result res = l.lookup (new Lookup.Template (Object JavaDoc.class));
230         Collection justToEnsureChangesToListenerWillBeFired = res.allItems ();
231         res.addLookupListener(listener);
232         
233         ic.add (obj);
234         listener.assertEvents ("One change in lookup", -1, 1);
235
236         assertEquals ("Can access cookie in the content", obj, l.lookup (obj.getClass ()));
237
238         ic.remove (obj);
239         listener.assertEvents ("One change in lookup", -1, 1);
240         
241         ic.add (obj);
242         listener.assertEvents ("One change in lookup", -1, 1);
243
244         assertEquals ("Can access cookie in the content", obj, l.lookup (obj.getClass ()));
245
246         ic.remove (obj);
247         listener.assertEvents ("One change in lookup", -1, 1);
248         
249     }
250     
251     
252     
253     //
254
// Garbage collect
255
//
256

257 // public void testBackwardCompatibleAbstractNodeLookupCanBeGarbageCollected () {
258
// // Note: doesn't work because lookup is holded by substitute too
259
// //AbstractNode n = new AbstractNode (Children.LEAF);
260
//// InstanceContent ic = new InstanceContent ();
261
//// final AbstractLookup lookup = new AbstractLookup (ic);
262
//// Node rep = new AbstractNode (Children.LEAF, lookup);
263
// AbstractNode rep = new AbstractNode (Children.LEAF);
264
// Look sampleLook = new SampleLook( "Sample look" ) {
265
// public String getName( Look.NodeSubstitute ns ) {
266
// return "Node name";
267
// }
268
//// public Look.NodeSubstitute attachTo (java.lang.Object representedObject) {
269
//// return new Look.NodeSubstitute (representedObject, this, lookup);
270
//// }
271
// };
272
//
273
// Node n = new LookNode (rep, sampleLook);
274
//
275
// Lookup l = n.getLookup ();
276
// assertEquals ("Two invocations share the same lookup", l, n.getLookup ());
277
//
278
// java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (l);
279
// l = null;
280
// assertGC ("Lookup can be GCed", ref);
281
// }
282

283     
284     
285     private static class Listener implements LookupListener, NodeListener {
286         private int cookies;
287         private int lookups;
288         
289         public void assertEvents (String JavaDoc txt, int cookies, int lookups) {
290             //org.openide.util.lookup.AbstractLookupTest.waitForFiring();
291
if (cookies != -1)
292                 assertEquals (txt + " cookies", cookies, this.cookies);
293             if (lookups != -1)
294                 assertEquals (txt + " lookups", lookups, this.lookups);
295             
296             this.cookies = 0;
297             this.lookups = 0;
298         }
299         
300         public void childrenAdded(NodeMemberEvent ev) {
301         }
302         
303         public void childrenRemoved(NodeMemberEvent ev) {
304         }
305         
306         public void childrenReordered(NodeReorderEvent ev) {
307         }
308         
309         public void nodeDestroyed(NodeEvent ev) {
310         }
311         
312         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
313             if (Node.PROP_COOKIE == evt.getPropertyName()) {
314                 cookies++;
315             }
316         }
317         
318         public void resultChanged(org.openide.util.LookupEvent ev) {
319             lookups++;
320         }
321         
322     } // end of Listener
323

324
325     private static class TestLookupLook extends Look {
326         
327         private int getLookupItemsCallCount;
328         
329         private Collection values;
330         
331         public TestLookupLook( String JavaDoc name ) {
332             super( name );
333         }
334         
335         public void lookupChange( Object JavaDoc representedObject ) {
336             fireChange( representedObject, Look.GET_LOOKUP_ITEMS );
337         }
338         
339         public Collection getValues() {
340             return values;
341         }
342              
343         public Collection getLookupItems(Object JavaDoc representedObject, Lookup oldEnv) {
344             
345             getLookupItemsCallCount++;
346             
347             this.values = oldEnv.lookup( new Lookup.Template( Object JavaDoc.class )).allInstances() ;
348             
349             Collection items = new ArrayList();
350             Collection c = (Collection)representedObject;
351             
352             for( Iterator it = c.iterator(); it.hasNext(); ) {
353                 items.add( new GoldenValue.TestLookupItem( it.next() ) );
354             }
355             
356             return items;
357         }
358         
359     }
360         
361 }
Popular Tags