KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > CookieSetTest


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.nodes;
21
22 import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
23 import junit.framework.*;
24 import junit.textui.TestRunner;
25 import java.util.*;
26 import org.openide.cookies.EditCookie;
27 import org.openide.cookies.OpenCookie;
28 import org.openide.cookies.SaveCookie;
29 import org.openide.nodes.*;
30
31 import org.netbeans.junit.*;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.openide.util.Lookup;
34 import org.openide.util.LookupEvent;
35 import org.openide.util.LookupListener;
36 import org.openide.util.lookup.InstanceContent;
37
38 /** Tests behaviour of CookieSet.
39  *
40  * @author Jaroslav Tulach
41  */

42 public class CookieSetTest extends NbTestCase {
43     public CookieSetTest(String JavaDoc name) {
44         super(name);
45     }
46
47     
48     public void testAddRemove () throws Exception JavaDoc {
49         CookieSet set = new CookieSet ();
50         L l = new L ();
51         set.addChangeListener(l);
52         
53         
54         C1 a1 = new C1 ();
55         C1 c1 = new C1 ();
56         C2 c2 = new C2 ();
57         
58         set.add (c1);
59         
60         assertEquals ("One change expected", l.cnt (), 1);
61         assertEquals ("Node.Cookie", c1, set.getCookie (Node.Cookie.class));
62         
63         
64         // replacing the c1 with a1
65
set.add (a1);
66         
67         assertEquals ("One change expected", l.cnt (), 1);
68         assertEquals ("Node.Cookie", a1, set.getCookie (Node.Cookie.class));
69         
70         // removing the cookie a1 leaves the set empty
71
set.remove (a1);
72         assertEquals ("One change expected", l.cnt (), 1);
73         assertNull ("Node.Cookie", set.getCookie (Node.Cookie.class));
74         
75
76         // adding c1 again
77
set.add (c1);
78         assertEquals ("One change expected", l.cnt (), 1);
79         assertEquals ("Node.Cookie", c1, set.getCookie (Node.Cookie.class));
80         
81         // and c2
82
set.add (c2);
83         assertEquals ("One change expected", l.cnt (), 1);
84         assertEquals ("C1 cookie", c1, set.getCookie (Node.Cookie.class));
85         assertEquals ("C2 index", c2, set.getCookie (Index.class));
86         
87         // removing c2 leaves to null on Index cookie
88
set.remove (c2);
89         assertEquals ("One change expected", l.cnt (), 1);
90         assertEquals ("C1 cookie", c1, set.getCookie (Node.Cookie.class));
91         assertNull ("Null index", set.getCookie (Index.class));
92         
93         assertCookieSet(set);
94     }
95         
96     /** Adding smaller and bigger and removing smaller.
97      */

98     public void testAddSBremoveS () {
99         CookieSet set = new CookieSet ();
100         C1 c1 = new C1 ();
101         C2 c2 = new C2 ();
102         
103         // after adding c1
104
set.add (c1);
105         
106         // adding c2 and removing c1
107
set.add (c2);
108         set.remove (c1);
109         
110         assertEquals ("C2 index", c2, set.getCookie (Index.class));
111         assertEquals ("C2 cookie", c2, set.getCookie (Node.Cookie.class));
112         assertCookieSet(set);
113     }
114
115     /** Adding bigger and smaller and removing bigger.
116      */

117     public void testAddBSremoveB () {
118         CookieSet set = new CookieSet ();
119         C1 c1 = new C1 ();
120         C2 c2 = new C2 ();
121         
122         // after adding c1
123
set.add (c2);
124         assertEquals ("Bigger registered", c2, set.getCookie (Node.Cookie.class));
125         
126         // adding c2 and removing c1
127
set.add (c1);
128         
129         assertEquals ("Smaller takes preceedence", c1, set.getCookie (Node.Cookie.class));
130         
131         set.remove (c2);
132         
133         assertEquals ("C1 cookie", c1, set.getCookie (Node.Cookie.class));
134         assertEquals ("Null index", null, set.getCookie (Index.class));
135
136         assertCookieSet(set);
137     }
138     
139     /** Tests behaviour of modifications via factory.
140      */

141     public void testFactoryAddRemove () {
142         L l = new L ();
143         CookieSet set = new CookieSet ();
144         set.addChangeListener(l);
145         
146         set.add (C1.class, l);
147         
148         assertEquals ("One change", l.cnt (), 1);
149         Node.Cookie obj = set.getCookie (C1.class);
150         if (! (obj instanceof C1)) {
151             fail ("Instance created by factory is wrong");
152         }
153         
154         if (obj != set.getCookie (C1.class)) {
155             fail ("New cookie created even it should not");
156         }
157         
158         if (obj != set.getCookie (Node.Cookie.class)) {
159             fail ("C1 is not registered as cookie");
160         }
161         
162         // replace
163
set.add (C1.class, l);
164         assertEquals ("One change", l.cnt (), 1);
165         
166         if (obj == set.getCookie (C1.class)) {
167             fail ("Factory changed, but cookie remains");
168         }
169         
170         obj = set.getCookie (C1.class);
171
172         // remove
173
set.remove (obj);
174         assertNotNull("Factory cookies cannot directly be removed", set.getCookie (C1.class));
175         
176         // remove of a factory
177
set.remove(C1.class, l);
178         assertNull("Removed factory still returns a cookie", set.getCookie (C1.class));
179
180         assertCookieSet(set);
181     }
182
183     /** Tests behaviour of modifications via factory.
184      */

185     public void testFactoryAddRemoveInherit () {
186         L l = new L ();
187         CookieSet set = new CookieSet ();
188         set.addChangeListener(l);
189         
190         set.add (C1.class, l);
191         set.add (Node.Cookie.class, l);
192
193         assertNull ("Nobody registered as C2", set.getCookie (C2.class));
194
195         {
196             Node.Cookie cookie = set.getCookie (Node.Cookie.class);
197             if (! (cookie instanceof C2)) {
198                 fail ("factory provides cookie C2 for Node.Cookie");
199             }
200         }
201
202         {
203             Node.Cookie c1 = set.getCookie (C1.class);
204             assertNotNull (c1);
205             assertEquals ("Factory provides C1 for C1 class", c1.getClass (), C1.class);
206         }
207         
208         assertNull ("Still nobody registered as C2", set.getCookie (C2.class));
209         
210         assertCookieSet(set);
211     }
212     
213     public void testCookieSetThruLookupReturnsTheSame () throws Exception JavaDoc {
214         doCookieSetTestsToSimulateIssue47411 (Node.Cookie.class, false);
215     }
216
217     public void testCookieSetThruLookupReturnsTheSameEvenWhenQueriedForLarger () throws Exception JavaDoc {
218         doCookieSetTestsToSimulateIssue47411 (Index.class, false);
219     }
220     
221     public void testCookieSetThruLookupReturnsTheSameWithFilter () throws Exception JavaDoc {
222         doCookieSetTestsToSimulateIssue47411 (Node.Cookie.class, true);
223     }
224
225     public void testCookieSetThruLookupReturnsTheSameEvenWhenQueriedForLargerWithFilter () throws Exception JavaDoc {
226         doCookieSetTestsToSimulateIssue47411 (Index.class, true);
227     }
228     
229     private void doCookieSetTestsToSimulateIssue47411 (Class JavaDoc firstQuery, boolean filter) throws Exception JavaDoc {
230         AbstractNode an = new AbstractNode (Children.LEAF);
231         CookieSet set = new CookieSet ();
232         an.setCookieSet (set);
233         
234         Node n = filter ? new FilterNode(an) : an;
235         
236         C1 c1 = new C1 ();
237         C2 c2 = new C2 ();
238         
239         // after adding c1
240
set.add (c2);
241         assertEquals ("Bigger registered", c2, set.getCookie (firstQuery));
242         assertEquals ("Bigger in lookup", c2, n.getLookup ().lookup (firstQuery));
243         
244         // adding c2 and removing c1
245
set.add (c1);
246         assertEquals ("Smaller takes preceedence", c1, set.getCookie (Node.Cookie.class));
247         assertEquals ("Smaller even in lookup", c1, n.getLookup ().lookup (Node.Cookie.class));
248         assertCookieSet(set);
249     }
250     
251     public void testCookieSetThruLookupImprovedVersionIssue47411 () throws Exception JavaDoc {
252         doCookieSetThruLookupImprovedVersionIssue47411 (false);
253     }
254         
255     public void testCookieSetThruLookupImprovedVersionWithFitlerIssue47411 () throws Exception JavaDoc {
256         doCookieSetThruLookupImprovedVersionIssue47411 (true);
257     }
258     
259     private void doCookieSetThruLookupImprovedVersionIssue47411 (boolean filter) throws Exception JavaDoc {
260         AbstractNode node = new AbstractNode (Children.LEAF);
261         CookieSet set = new CookieSet ();
262         node.setCookieSet (set);
263         
264         Node an = filter ? new FilterNode(node) : node;
265         
266
267         class X implements org.openide.cookies.OpenCookie, org.openide.cookies.EditCookie {
268             public void open () {
269             }
270             public void edit () {
271             }
272         }
273         X x = new X ();
274         
275         class A implements org.openide.cookies.OpenCookie {
276             public void open () { }
277         }
278         A a = new A ();
279         
280         set.add (a);
281         set.add (x);
282         
283         Object JavaDoc edit = an.getLookup ().lookup (org.openide.cookies.EditCookie.class);
284         assertEquals ("X has edit", x, edit);
285         
286         Object JavaDoc open = an.getLookup ().lookup (org.openide.cookies.OpenCookie.class);
287         
288         assertEquals ("Just verify that CookieSet returns A", a, set.getCookie (org.openide.cookies.OpenCookie.class));
289         assertEquals ("A has open", a, open);
290         
291         assertEquals (null, an.getLookup ().lookup (SaveCookie.class));
292         assertEquals (a, an.getLookup ().lookup (OpenCookie.class));
293         assertEquals (x, an.getLookup ().lookup (EditCookie.class));
294         assertCookieSet(set);
295     }
296     
297     private static void assertCookieSet(CookieSet en) {
298         if (en.getCookie(Node.Cookie.class) == null) {
299             assertEquals("Should be empty", 0, en.getLookup().lookupAll(Node.Cookie.class).size());
300             return;
301         }
302     
303         Lookup.Result<Node.Cookie> all = en.getLookup().lookupResult(Node.Cookie.class);
304         int cnt = 0;
305         for (Class JavaDoc<? extends Node.Cookie> c : all.allClasses()) {
306             Object JavaDoc o = en.getLookup().lookup(c);
307             assertEquals("Query for " + c, o, en.getCookie(c));
308             cnt++;
309         }
310         
311         if (cnt == 0) {
312             fail("There shall be at least one object in lookup: " + cnt);
313         }
314     }
315
316     public void testOneChangeInLookupWhenAddingMultipleElements() throws Exception JavaDoc {
317         CookieSet general = CookieSet.createGeneric(null);
318         
319         L listener = new L();
320         Lookup.Result<String JavaDoc> res = general.getLookup().lookupResult(String JavaDoc.class);
321         res.addLookupListener(listener);
322         res.allItems();
323         
324         assertEquals("No change", 0, listener.cnt());
325         
326         general.assign(String JavaDoc.class, "Ahoj", "Jardo");
327         
328         assertEquals("One change", 1, listener.cnt());
329         assertEquals("Two items", 2, res.allItems().size());
330
331         general.assign(String JavaDoc.class, "Ahoj", "Jardo");
332         assertEquals("No change", 0, listener.cnt());
333         assertEquals("Still two items", 2, res.allItems().size());
334         
335         general.assign(String JavaDoc.class, "Ahoj");
336         assertEquals("Yet one change", 1, listener.cnt());
337         assertEquals("One item", 1, res.allItems().size());
338     }
339
340     public void testOneChangeInLookupWhenAddingMultipleElementsWithBefore() throws Exception JavaDoc {
341         class B implements CookieSet.Before {
342             CookieSet general;
343             private String JavaDoc[] asg;
344             public void assign(String JavaDoc... arr) {
345                 asg = arr;
346             }
347             
348             public void beforeLookup(Class JavaDoc<?> c) {
349                 if (asg != null) {
350                     general.assign(String JavaDoc.class, asg);
351                     asg = null;
352                 }
353             }
354         }
355         B b = new B();
356         
357         b.general = CookieSet.createGeneric(b);
358         
359         L listener = new L();
360         Lookup.Result<String JavaDoc> res = b.general.getLookup().lookupResult(String JavaDoc.class);
361         res.addLookupListener(listener);
362         res.allItems();
363         
364         assertEquals("No change", 0, listener.cnt());
365         
366         b.assign("Ahoj", "Jardo");
367         
368         assertEquals("Two items", 2, res.allItems().size());
369         assertEquals("One change", 1, listener.cnt());
370
371         b.assign("Ahoj", "Jardo");
372         assertEquals("Still two items", 2, res.allItems().size());
373         assertEquals("No change", 0, listener.cnt());
374         
375         b.assign("Ahoj");
376         assertEquals("One item", 1, res.allItems().size());
377         assertEquals("Yet one change", 1, listener.cnt());
378     }
379     
380     /** Change listener.
381      */

382     private static final class L extends Object JavaDoc
383     implements LookupListener, ChangeListener JavaDoc, CookieSet.Factory {
384         private int count;
385         
386         public void stateChanged(javax.swing.event.ChangeEvent JavaDoc changeEvent) {
387             count++;
388         }
389         
390         /** Gets and clears the count.
391          */

392         public int cnt () {
393             int c = count;
394             count = 0;
395             return c;
396         }
397         
398         /** Creates a Node.Cookie of given class. The method
399          * may be called more than once.
400          */

401         public Node.Cookie createCookie(Class JavaDoc klass) {
402             if (klass == C1.class) {
403                 return new C1 ();
404             }
405             return new C2 ();
406         }
407
408         public void resultChanged(LookupEvent ev) {
409             count++;
410         }
411         
412     }
413     
414     /** A simple cookie.
415      */

416     private static class C1 implements Node.Cookie {
417     }
418     
419     /** A complicated cookie.
420      */

421     private static final class C2 extends C1 implements Index {
422         
423         /** Get the index of a given node.
424          * @param node node to find index of
425          * @return index of the node, or <code>-1</code> if no such node was found
426          */

427         public int indexOf(Node node) {
428             return 0;
429         }
430         
431         /** Move an element up.
432          * @param x index of element to move up
433          * @exception IndexOutOfBoundsException if an index is out of bounds
434          */

435         public void moveUp(int x) {
436         }
437         
438         /** Get the child nodes.
439          * @return array of nodes that can be sorted by this index
440          */

441         public Node[] getNodes() {
442             return null;
443         }
444         
445         /** Move an element down.
446          * @param x index of element to move down
447          * @exception IndexOutOfBoundsException if an index is out of bounds
448          */

449         public void moveDown(int x) {
450         }
451         
452         /** Invoke a dialog for reordering the children.
453          */

454         public void reorder() {
455         }
456         
457         /** Exchange two elements.
458          * @param x position of the first element
459          * @param y position of the second element
460          * @exception IndexOutOfBoundsException if an index is out of bounds
461          */

462         public void exchange(int x, int y) {
463         }
464         
465         /** Remove a listener from the listener list.
466          *
467          * @param chl listener to remove
468          */

469         public void removeChangeListener(ChangeListener JavaDoc chl) {
470         }
471         
472         /** Get the number of nodes.
473          * @return the count
474          */

475         public int getNodesCount() {
476             return 0;
477         }
478         
479         /** Add a new listener to the listener list. The listener will be notified of
480          * any change in the order of the nodes.
481          *
482          * @param chl new listener
483          */

484         public void addChangeListener(ChangeListener JavaDoc chl) {
485         }
486         
487         /** Reorder all children with a given permutation.
488          * @param perm permutation with the length of current nodes
489          * @exception IllegalArgumentException if the permutation is not valid
490          */

491         public void reorder(int[] perm) {
492         }
493         
494         /** Move the element at the <code>x</code>-th position to the <code>y</code>-th position. All
495          * elements after the <code>y</code>-th position are moved down.
496          *
497          * @param x the position to remove the element from
498          * @param y the position to insert the element to
499          * @exception IndexOutOfBoundsException if an index is out of bounds
500          */

501         public void move(int x, int y) {
502         }
503     };
504 }
505
Popular Tags