Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.openide.util.lookup; |
15 |
|
16 |
import org.openide.filesystems.*; |
17 |
import org.openide.loaders.*; |
18 |
import org.openide.util.*; |
19 |
|
20 |
import java.lang.ref.WeakReference; |
21 |
import java.util.*; |
22 |
import junit.framework.*; |
23 |
import org.netbeans.junit.*; |
24 |
|
25 |
/** Runs all NbLookupTest tests on ProxyLookup and adds few additional. |
26 |
*/ |
27 |
public class ExcludingLookupTest extends AbstractLookupBaseHid |
28 |
implements AbstractLookupBaseHid.Impl { |
29 |
public ExcludingLookupTest(java.lang.String testName) { |
30 |
super(testName, null); |
31 |
} |
32 |
|
33 |
public Lookup createLookup (final Lookup lookup) { |
34 |
return Lookups.exclude (lookup, new Class[0]); |
35 |
} |
36 |
|
37 |
public Lookup createInstancesLookup (InstanceContent ic) { |
38 |
return new AbstractLookup (ic); |
39 |
} |
40 |
|
41 |
public void clearCaches () { |
42 |
} |
43 |
|
44 |
public void testWeCanRemoveInteger () throws Exception { |
45 |
doBasicFilteringTest (Integer.class, Integer.class, 0); |
46 |
} |
47 |
|
48 |
public void testWeCanRemoveIntegersEvenByAskingForRemoveOfAllNumbers () throws Exception { |
49 |
doBasicFilteringTest (Number.class, Integer.class, 0); |
50 |
} |
51 |
public void testFunWithInterfaces () throws Exception { |
52 |
doBasicFilteringTest (java.io.Serializable.class, Integer.class, 0); |
53 |
} |
54 |
public void testFunWithInterfaces2 () throws Exception { |
55 |
doBasicFilteringTest (java.io.Serializable.class, Object.class, 1); |
56 |
} |
57 |
|
58 |
public void testIntegersQueriedThruObject () throws Exception { |
59 |
doBasicFilteringTest (Number.class, Object.class, 1); |
60 |
} |
61 |
|
62 |
private void doBasicFilteringTest (Class theFilter, Class theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception { |
63 |
Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { theFilter }); |
64 |
Lookup.Template t = new Lookup.Template (theQuery); |
65 |
Lookup.Result res = lookup.lookup (t); |
66 |
|
67 |
LL ll = new LL (); |
68 |
res.addLookupListener (ll); |
69 |
assertEquals ("Nothing is there", 0, res.allItems ().size ()); |
70 |
|
71 |
Object inst = new Integer (3); |
72 |
ic.add (inst); |
73 |
|
74 |
assertEquals ("Filtered out", null, lookup.lookup (theQuery)); |
75 |
assertEquals ("Filtered out2", null, lookup.lookupItem (t)); |
76 |
assertEquals ("Nothing is there - 2", 0, res.allItems ().size ()); |
77 |
assertEquals ("Nothing is there - 2a", 0, res.allInstances ().size ()); |
78 |
assertEquals ("Nothing is there - 2b", 0, res.allClasses ().size ()); |
79 |
assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ()); |
80 |
|
81 |
ic.remove (inst); |
82 |
assertEquals ("Filtered out3", null, lookup.lookupItem (t)); |
83 |
assertEquals ("Nothing is there - 3", 0, res.allItems ().size ()); |
84 |
assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ()); |
85 |
assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ()); |
86 |
assertEquals ("Of course it is not there", null, lookup.lookup (theQuery)); |
87 |
assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ()); |
88 |
|
89 |
} |
90 |
|
91 |
public void testSizeOfTheLookup () throws Exception { |
92 |
Class exclude = String.class; |
93 |
|
94 |
Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { exclude }); |
95 |
|
96 |
assertSize ("Should be pretty lightweight", Collections.singleton (lookup), 16, |
97 |
new Object[] { this.instanceLookup, exclude }); |
98 |
} |
99 |
public void testSizeOfTheLookupForMultipleFiltersIsHigher () throws Exception { |
100 |
Class exclude = String.class; |
101 |
Class exclude2 = Integer.class; |
102 |
Class[] arr = new Class[] { exclude, exclude2 }; |
103 |
|
104 |
Lookup lookup = Lookups.exclude (this.instanceLookup, arr); |
105 |
|
106 |
assertSize ("Is fatter", Collections.singleton (lookup), 40, |
107 |
new Object[] { this.instanceLookup, exclude, exclude2 }); |
108 |
assertSize ("But only due to the array", Collections.singleton (lookup), 16, |
109 |
new Object[] { this.instanceLookup, exclude, exclude2, arr }); |
110 |
} |
111 |
|
112 |
public void testFilteringOfSomething () throws Exception { |
113 |
doFilteringOfSomething (Runnable.class, java.io.Serializable.class, 1); |
114 |
} |
115 |
|
116 |
private void doFilteringOfSomething (Class theFilter, Class theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception { |
117 |
Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { theFilter }); |
118 |
Lookup.Template t = new Lookup.Template (theQuery); |
119 |
Lookup.Result res = lookup.lookup (t); |
120 |
|
121 |
LL ll = new LL (); |
122 |
res.addLookupListener (ll); |
123 |
assertEquals ("Nothing is there", 0, res.allItems ().size ()); |
124 |
|
125 |
Object inst = new Integer (3); |
126 |
ic.add (inst); |
127 |
|
128 |
assertEquals ("Accepted", inst, lookup.lookup (theQuery)); |
129 |
assertNotNull ("Accepted too", lookup.lookupItem (t)); |
130 |
assertEquals ("One is there - 2", 1, res.allItems ().size ()); |
131 |
assertEquals ("One is there - 2a", 1, res.allInstances ().size ()); |
132 |
assertEquals ("One is there - 2b", 1, res.allClasses ().size ()); |
133 |
assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ()); |
134 |
|
135 |
Object inst2 = new Thread (); // implements Runnable |
136 |
ic.add (inst2); |
137 |
assertEquals ("Accepted - 2", inst, lookup.lookup (theQuery)); |
138 |
assertNotNull ("Accepted too -2", lookup.lookupItem (t)); |
139 |
assertEquals ("One is there - 3", 1, res.allItems ().size ()); |
140 |
assertEquals ("One is there - 3a", 1, res.allInstances ().size ()); |
141 |
assertEquals ("One is there - 3b", 1, res.allClasses ().size ()); |
142 |
assertEquals ("Right # of events", 0, ll.getCount ()); |
143 |
|
144 |
|
145 |
ic.remove (inst); |
146 |
assertEquals ("Filtered out3", null, lookup.lookupItem (t)); |
147 |
assertEquals ("Nothing is there - 3", 0, res.allItems ().size ()); |
148 |
assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ()); |
149 |
assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ()); |
150 |
assertEquals ("Of course it is not there", null, lookup.lookup (theQuery)); |
151 |
assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ()); |
152 |
} |
153 |
|
154 |
} |