KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > lookup > ExcludingLookupTest


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.util.lookup;
21
22 import org.openide.util.*;
23
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.*;
26 import junit.framework.*;
27 import org.netbeans.junit.*;
28
29 /** Runs all NbLookupTest tests on ProxyLookup and adds few additional.
30  */

31 public class ExcludingLookupTest extends AbstractLookupBaseHid
32 implements AbstractLookupBaseHid.Impl {
33     public ExcludingLookupTest(java.lang.String JavaDoc testName) {
34         super(testName, null);
35     }
36
37     public Lookup createLookup (final Lookup lookup) {
38         return Lookups.exclude (lookup, new Class JavaDoc[0]);
39     }
40     
41     public Lookup createInstancesLookup (InstanceContent ic) {
42         return new AbstractLookup (ic);
43     }
44
45     public void clearCaches () {
46     }
47     
48     public void testWeCanRemoveInteger () throws Exception JavaDoc {
49         doBasicFilteringTest (Integer JavaDoc.class, Integer JavaDoc.class, 0);
50     }
51     
52     public void testWeCanRemoveIntegersEvenByAskingForRemoveOfAllNumbers () throws Exception JavaDoc {
53         doBasicFilteringTest (Number JavaDoc.class, Integer JavaDoc.class, 0);
54     }
55     public void testFunWithInterfaces () throws Exception JavaDoc {
56         doBasicFilteringTest (java.io.Serializable JavaDoc.class, Integer JavaDoc.class, 0);
57     }
58     
59     public void testWeCanGetInstanceOfSerializableEvenItIsExcludedIfWeAskForClassNotExtendingIt () throws Exception JavaDoc {
60         Lookup lookup = Lookups.exclude (this.instanceLookup, new Class JavaDoc[] { java.io.Serializable JavaDoc.class });
61         Lookup.Template t = new Lookup.Template (Object JavaDoc.class);
62         Lookup.Result res = lookup.lookup (t);
63         
64         LL ll = new LL ();
65         res.addLookupListener (ll);
66         assertEquals ("Nothing is there", 0, res.allItems ().size ());
67         
68         Object JavaDoc inst = new Integer JavaDoc (3);
69         ic.add (inst);
70         
71         assertEquals ("Not Filtered out", inst, lookup.lookup (Object JavaDoc.class));
72         assertEquals ("Not Filtered out2", inst, lookup.lookupItem (t).getInstance ());
73         assertEquals ("One is there - 2", 1, res.allItems ().size ());
74         assertEquals ("One is there - 2a", 1, res.allInstances ().size ());
75         assertEquals ("One is there - 2b", 1, res.allClasses ().size ());
76         assertEquals ("Right # of events", 1, ll.getCount ());
77         
78         ic.remove (inst);
79         assertEquals ("Filtered out3", null, lookup.lookupItem (t));
80         assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
81         assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
82         assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
83         assertEquals ("Of course it is not there", null, lookup.lookup (Object JavaDoc.class));
84         assertEquals ("Right # of events", 1, ll.getCount ());
85     }
86     
87     public void testIntegersQueriedThruObject () throws Exception JavaDoc {
88         doBasicFilteringTest (Number JavaDoc.class, Object JavaDoc.class, 1);
89     }
90     
91     private void doBasicFilteringTest (Class JavaDoc theFilter, Class JavaDoc theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception JavaDoc {
92         Lookup lookup = Lookups.exclude (this.instanceLookup, new Class JavaDoc[] { theFilter });
93         Lookup.Template t = new Lookup.Template (theQuery);
94         Lookup.Result res = lookup.lookup (t);
95         
96         LL ll = new LL ();
97         res.addLookupListener (ll);
98         assertEquals ("Nothing is there", 0, res.allItems ().size ());
99         
100         Object JavaDoc inst = new Integer JavaDoc (3);
101         ic.add (inst);
102         
103         assertEquals ("Filtered out", null, lookup.lookup (theQuery));
104         assertEquals ("Filtered out2", null, lookup.lookupItem (t));
105         assertEquals ("Nothing is there - 2", 0, res.allItems ().size ());
106         assertEquals ("Nothing is there - 2a", 0, res.allInstances ().size ());
107         assertEquals ("Nothing is there - 2b", 0, res.allClasses ().size ());
108         assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
109         
110         ic.remove (inst);
111         assertEquals ("Filtered out3", null, lookup.lookupItem (t));
112         assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
113         assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
114         assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
115         assertEquals ("Of course it is not there", null, lookup.lookup (theQuery));
116         assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
117         
118     }
119     
120     public void testSizeOfTheLookup () throws Exception JavaDoc {
121         Class JavaDoc exclude = String JavaDoc.class;
122         
123         Lookup lookup = Lookups.exclude (this.instanceLookup, new Class JavaDoc[] { exclude });
124
125         assertSize ("Should be pretty lightweight", Collections.singleton (lookup), 16,
126                 new Object JavaDoc[] { this.instanceLookup, exclude });
127     }
128     public void testSizeOfTheLookupForMultipleFiltersIsHigher () throws Exception JavaDoc {
129         Class JavaDoc exclude = String JavaDoc.class;
130         Class JavaDoc exclude2 = Integer JavaDoc.class;
131         Class JavaDoc[] arr = new Class JavaDoc[] { exclude, exclude2 };
132         
133         Lookup lookup = Lookups.exclude (this.instanceLookup, arr);
134
135         assertSize ("Is fatter", Collections.singleton (lookup), 40,
136                 new Object JavaDoc[] { this.instanceLookup, exclude, exclude2 });
137         assertSize ("But only due to the array", Collections.singleton (lookup), 16,
138                 new Object JavaDoc[] { this.instanceLookup, exclude, exclude2, arr });
139     }
140     
141     public void testFilteringOfSomething () throws Exception JavaDoc {
142         doFilteringOfSomething (Runnable JavaDoc.class, java.io.Serializable JavaDoc.class, 1);
143     }
144     
145     private void doFilteringOfSomething (Class JavaDoc theFilter, Class JavaDoc theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception JavaDoc {
146         Lookup lookup = Lookups.exclude (this.instanceLookup, new Class JavaDoc[] { theFilter });
147         Lookup.Template t = new Lookup.Template (theQuery);
148         Lookup.Result res = lookup.lookup (t);
149         
150         LL ll = new LL ();
151         res.addLookupListener (ll);
152         assertEquals ("Nothing is there", 0, res.allItems ().size ());
153         
154         Object JavaDoc inst = new Integer JavaDoc (3);
155         ic.add (inst);
156         
157         assertEquals ("Accepted", inst, lookup.lookup (theQuery));
158         assertNotNull ("Accepted too", lookup.lookupItem (t));
159         assertEquals ("One is there - 2", 1, res.allItems ().size ());
160         assertEquals ("One is there - 2a", 1, res.allInstances ().size ());
161         assertEquals ("One is there - 2b", 1, res.allClasses ().size ());
162         assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
163
164         Object JavaDoc inst2 = new Thread JavaDoc (); // implements Runnable
165
ic.add (inst2);
166         assertEquals ("Accepted - 2", inst, lookup.lookup (theQuery));
167         assertNotNull ("Accepted too -2", lookup.lookupItem (t));
168         assertEquals ("One is there - 3", 1, res.allItems ().size ());
169         assertEquals ("One is there - 3a", 1, res.allInstances ().size ());
170         assertEquals ("One is there - 3b", 1, res.allClasses ().size ());
171         assertEquals ("Right # of events", 0, ll.getCount ());
172         
173         
174         ic.remove (inst);
175         assertEquals ("Filtered out3", null, lookup.lookupItem (t));
176         assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
177         assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
178         assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
179         assertEquals ("Of course it is not there", null, lookup.lookup (theQuery));
180         assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
181     }
182
183     public void testTheBehaviourAsRequestedByDavidAndDescribedByJesse () throws Exception JavaDoc {
184         class C implements Runnable JavaDoc, java.io.Serializable JavaDoc {
185             public void run () {}
186         }
187         Object JavaDoc c = new C();
188         Lookup l1 = Lookups.singleton(c);
189         Lookup l2 = Lookups.exclude(l1, new Class JavaDoc[] {Runnable JavaDoc.class});
190         assertNull(l2.lookup(Runnable JavaDoc.class));
191         assertEquals(c, l2.lookup(java.io.Serializable JavaDoc.class));
192     }
193     
194     public void testTheBehaviourAsRequestedByDavidAndDescribedByJesseWithUsageOfResult () throws Exception JavaDoc {
195         class C implements Runnable JavaDoc, java.io.Serializable JavaDoc {
196             public void run () {}
197         }
198         Object JavaDoc c = new C();
199         Lookup l1 = Lookups.singleton(c);
200         Lookup l2 = Lookups.exclude(l1, new Class JavaDoc[] {Runnable JavaDoc.class});
201         
202         Lookup.Result run = l2.lookup (new Lookup.Template (Runnable JavaDoc.class));
203         Lookup.Result ser = l2.lookup (new Lookup.Template (java.io.Serializable JavaDoc.class));
204         
205         assertEquals ("Runnables filtered out", 0, run.allItems ().size ());
206         assertEquals ("One serialiazble", 1, ser.allItems ().size ());
207         assertEquals ("And it is c", c, ser.allInstances ().iterator ().next ());
208     }
209 }
210
Popular Tags