jtulach@9
|
1 |
/*
|
jtulach@302
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@189
|
3 |
*
|
mzlamal@829
|
4 |
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
|
jtulach@189
|
5 |
*
|
jtulach@302
|
6 |
* The contents of this file are subject to the terms of either the GNU
|
jtulach@302
|
7 |
* General Public License Version 2 only ("GPL") or the Common
|
jtulach@302
|
8 |
* Development and Distribution License("CDDL") (collectively, the
|
jtulach@302
|
9 |
* "License"). You may not use this file except in compliance with the
|
jtulach@302
|
10 |
* License. You can obtain a copy of the License at
|
jtulach@302
|
11 |
* http://www.netbeans.org/cddl-gplv2.html
|
jtulach@302
|
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jtulach@302
|
13 |
* specific language governing permissions and limitations under the
|
jtulach@302
|
14 |
* License. When distributing the software, include this License Header
|
jtulach@302
|
15 |
* Notice in each file and include the License file at
|
jtulach@302
|
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
jtulach@302
|
17 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@302
|
18 |
* by Sun in the GPL Version 2 section of the License file that
|
jtulach@302
|
19 |
* accompanied this code. If applicable, add the following below the
|
jtulach@302
|
20 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jtulach@302
|
21 |
* your own identifying information:
|
jtulach@189
|
22 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@189
|
23 |
*
|
jtulach@302
|
24 |
* Contributor(s):
|
jtulach@302
|
25 |
*
|
jtulach@189
|
26 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jtulach@189
|
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
|
jtulach@9
|
28 |
* Microsystems, Inc. All Rights Reserved.
|
jtulach@302
|
29 |
*
|
jtulach@302
|
30 |
* If you wish your version of this file to be governed by only the CDDL
|
jtulach@302
|
31 |
* or only the GPL Version 2, indicate your decision by adding
|
jtulach@302
|
32 |
* "[Contributor] elects to include this software in this distribution
|
jtulach@302
|
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jtulach@302
|
34 |
* single choice of license, a recipient has the option to distribute
|
jtulach@302
|
35 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jtulach@302
|
36 |
* to extend the choice of license to its licensees as provided above.
|
jtulach@302
|
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jtulach@302
|
38 |
* Version 2 license, then the option applies only if the new code is
|
jtulach@302
|
39 |
* made subject to such option by the copyright holder.
|
jtulach@9
|
40 |
*/
|
jtulach@9
|
41 |
|
jtulach@9
|
42 |
package org.openide.util.lookup;
|
jtulach@9
|
43 |
|
jtulach@94
|
44 |
import java.io.Serializable;
|
jtulach@9
|
45 |
|
jtulach@9
|
46 |
import java.util.*;
|
jtulach@9
|
47 |
import org.netbeans.junit.*;
|
jtulach@550
|
48 |
import org.openide.util.Lookup;
|
jtulach@550
|
49 |
import org.openide.util.LookupEvent;
|
jtulach@550
|
50 |
import org.openide.util.LookupListener;
|
jtulach@9
|
51 |
|
jtulach@9
|
52 |
/** Runs all NbLookupTest tests on ProxyLookup and adds few additional.
|
jtulach@9
|
53 |
*/
|
jglick@833
|
54 |
@SuppressWarnings("unchecked") // XXX ought to be corrected, just a lot of them
|
jtulach@9
|
55 |
public class LookupsProxyTest extends AbstractLookupBaseHid
|
jtulach@9
|
56 |
implements AbstractLookupBaseHid.Impl {
|
jtulach@9
|
57 |
public LookupsProxyTest(java.lang.String testName) {
|
jtulach@9
|
58 |
super(testName, null);
|
jtulach@9
|
59 |
}
|
jtulach@189
|
60 |
|
jtulach@9
|
61 |
public static void main(java.lang.String[] args) {
|
jtulach@9
|
62 |
junit.textui.TestRunner.run(new NbTestSuite (LookupsProxyTest.class));
|
jtulach@9
|
63 |
}
|
jtulach@9
|
64 |
|
jtulach@9
|
65 |
/** Creates an lookup for given lookup. This class just returns
|
jtulach@9
|
66 |
* the object passed in, but subclasses can be different.
|
jtulach@9
|
67 |
* @param lookup in lookup
|
jtulach@9
|
68 |
* @return a lookup to use
|
jtulach@9
|
69 |
*/
|
jtulach@9
|
70 |
public Lookup createLookup (final Lookup lookup) {
|
jtulach@9
|
71 |
return org.openide.util.lookup.Lookups.proxy (
|
jtulach@9
|
72 |
new Lookup.Provider () {
|
jtulach@9
|
73 |
public Lookup getLookup () {
|
jtulach@9
|
74 |
return lookup;
|
jtulach@9
|
75 |
}
|
jtulach@9
|
76 |
}
|
jtulach@9
|
77 |
);
|
jtulach@9
|
78 |
}
|
jtulach@9
|
79 |
|
jtulach@9
|
80 |
public Lookup createInstancesLookup (InstanceContent ic) {
|
jtulach@9
|
81 |
return new AbstractLookup (ic);
|
jtulach@9
|
82 |
}
|
jtulach@9
|
83 |
|
jtulach@9
|
84 |
public void clearCaches () {
|
jtulach@9
|
85 |
}
|
jtulach@9
|
86 |
|
jtulach@9
|
87 |
|
jtulach@9
|
88 |
|
jtulach@9
|
89 |
/** Check whether setLookups method does not fire when there is no
|
jtulach@9
|
90 |
* change in the lookups.
|
jtulach@9
|
91 |
*/
|
jtulach@9
|
92 |
public void testProxyListener () {
|
jtulach@9
|
93 |
Changer ch = new Changer (Lookup.EMPTY);
|
jtulach@9
|
94 |
|
jtulach@9
|
95 |
Lookup lookup = Lookups.proxy(ch);
|
jtulach@9
|
96 |
Lookup.Result res = lookup.lookup (new Lookup.Template (Object.class));
|
jtulach@9
|
97 |
|
jtulach@9
|
98 |
LL ll = new LL ();
|
jtulach@9
|
99 |
res.addLookupListener (ll);
|
jtulach@9
|
100 |
Collection allRes = res.allInstances ();
|
jtulach@9
|
101 |
|
jtulach@9
|
102 |
ch.setLookup (new AbstractLookup (new InstanceContent ())); // another empty lookup
|
jtulach@9
|
103 |
lookup.lookup (Object.class); // does the refresh
|
jtulach@9
|
104 |
|
jtulach@9
|
105 |
assertEquals("Replacing an empty by empty does not generate an event", 0, ll.getCount());
|
jtulach@9
|
106 |
|
jtulach@9
|
107 |
InstanceContent content = new InstanceContent ();
|
jtulach@9
|
108 |
AbstractLookup del = new AbstractLookup (content);
|
jtulach@9
|
109 |
content.add (this);
|
jtulach@9
|
110 |
ch.setLookup (del);
|
jtulach@9
|
111 |
lookup.lookup (Object.class);
|
jtulach@9
|
112 |
|
jtulach@9
|
113 |
if (ll.getCount () != 1) {
|
jtulach@9
|
114 |
fail ("Changing lookups with different content generates an event");
|
jtulach@9
|
115 |
}
|
jtulach@9
|
116 |
|
jtulach@9
|
117 |
ch.setLookup (del);
|
jtulach@9
|
118 |
lookup.lookup (Object.class);
|
jtulach@9
|
119 |
|
jtulach@9
|
120 |
if (ll.getCount () != 0) {
|
jtulach@9
|
121 |
fail ("Not changing the lookups does not generate any event");
|
jtulach@9
|
122 |
}
|
jtulach@9
|
123 |
}
|
jtulach@94
|
124 |
|
jtulach@94
|
125 |
|
jtulach@94
|
126 |
public void testListeningAndQueryingByTwoListenersInstancesSetLookups() {
|
jtulach@94
|
127 |
doListeningAndQueryingByTwoListenersSetLookups(0, 1, false);
|
jtulach@94
|
128 |
}
|
jtulach@94
|
129 |
public void testListeningAndQueryingByTwoListenersClassesSetLookups() {
|
jtulach@94
|
130 |
doListeningAndQueryingByTwoListenersSetLookups(1, 1, false);
|
jtulach@94
|
131 |
}
|
jtulach@94
|
132 |
public void testListeningAndQueryingByTwoListenersItemsSetLookups() {
|
jtulach@94
|
133 |
doListeningAndQueryingByTwoListenersSetLookups(2, 1, false);
|
jtulach@94
|
134 |
}
|
jtulach@94
|
135 |
|
jtulach@94
|
136 |
public void testListeningAndQueryingByTwoListenersInstancesSetLookups2() {
|
jtulach@94
|
137 |
doListeningAndQueryingByTwoListenersSetLookups(0, 2, false);
|
jtulach@94
|
138 |
}
|
jtulach@94
|
139 |
public void testListeningAndQueryingByTwoListenersClassesSetLookups2() {
|
jtulach@94
|
140 |
doListeningAndQueryingByTwoListenersSetLookups(1, 2, false);
|
jtulach@94
|
141 |
}
|
jtulach@94
|
142 |
public void testListeningAndQueryingByTwoListenersItemsSetLookups2() {
|
jtulach@94
|
143 |
doListeningAndQueryingByTwoListenersSetLookups(2, 2, false);
|
jtulach@94
|
144 |
}
|
jtulach@94
|
145 |
|
jtulach@94
|
146 |
public void testListeningAndQueryingByTwoListenersInstancesSetLookupsWithProxy() {
|
jtulach@94
|
147 |
doListeningAndQueryingByTwoListenersSetLookups(0, 1, true);
|
jtulach@94
|
148 |
}
|
jtulach@94
|
149 |
public void testListeningAndQueryingByTwoListenersClassesSetLookupsWithProxy() {
|
jtulach@94
|
150 |
doListeningAndQueryingByTwoListenersSetLookups(1, 1, true);
|
jtulach@94
|
151 |
}
|
jtulach@94
|
152 |
public void testListeningAndQueryingByTwoListenersItemsSetLookupsWithProxy() {
|
jtulach@94
|
153 |
doListeningAndQueryingByTwoListenersSetLookups(2, 1, true);
|
jtulach@94
|
154 |
}
|
jtulach@94
|
155 |
|
jtulach@94
|
156 |
public void testListeningAndQueryingByTwoListenersInstancesSetLookups2WithProxy() {
|
jtulach@94
|
157 |
doListeningAndQueryingByTwoListenersSetLookups(0, 2, true);
|
jtulach@94
|
158 |
}
|
jtulach@94
|
159 |
public void testListeningAndQueryingByTwoListenersClassesSetLookups2WithProxy() {
|
jtulach@94
|
160 |
doListeningAndQueryingByTwoListenersSetLookups(1, 2, true);
|
jtulach@94
|
161 |
}
|
jtulach@94
|
162 |
public void testListeningAndQueryingByTwoListenersItemsSetLookups2WithProxy() {
|
jtulach@94
|
163 |
doListeningAndQueryingByTwoListenersSetLookups(2, 2, true);
|
jtulach@94
|
164 |
}
|
jtulach@94
|
165 |
|
jtulach@94
|
166 |
/* XXX: these are pretty slow, seems there is a performance problem 2^22
|
jtulach@94
|
167 |
public void testListeningAndQueryingByTwoListenersInstancesSetLookups22() {
|
jtulach@94
|
168 |
doListeningAndQueryingByTwoListenersSetLookups(0, 22);
|
jtulach@94
|
169 |
}
|
jtulach@94
|
170 |
public void testListeningAndQueryingByTwoListenersClassesSetLookups22() {
|
jtulach@94
|
171 |
doListeningAndQueryingByTwoListenersSetLookups(1, 22);
|
jtulach@94
|
172 |
}
|
jtulach@94
|
173 |
public void testListeningAndQueryingByTwoListenersItemsSetLookups22() {
|
jtulach@94
|
174 |
doListeningAndQueryingByTwoListenersSetLookups(2, 22);
|
jtulach@94
|
175 |
}
|
jtulach@94
|
176 |
*/
|
jtulach@94
|
177 |
|
jtulach@94
|
178 |
private void doListeningAndQueryingByTwoListenersSetLookups(final int type, int depth, boolean cacheOnTop) {
|
jtulach@94
|
179 |
Changer orig = new Changer(Lookup.EMPTY);
|
jtulach@94
|
180 |
Lookup on = Lookups.proxy(orig);
|
jtulach@94
|
181 |
Lookup first = on;
|
jtulach@94
|
182 |
|
jtulach@94
|
183 |
while (--depth > 0) {
|
jtulach@94
|
184 |
Changer next = new Changer(on);
|
jtulach@94
|
185 |
on = Lookups.proxy(next);
|
jtulach@94
|
186 |
}
|
jtulach@94
|
187 |
|
jtulach@94
|
188 |
|
jtulach@94
|
189 |
final Lookup lookup = cacheOnTop ? new ProxyLookup(new Lookup[] { on }) : on;
|
jtulach@94
|
190 |
|
jtulach@94
|
191 |
class L implements LookupListener {
|
jtulach@94
|
192 |
Lookup.Result integer = lookup.lookup(new Lookup.Template(Integer.class));
|
jtulach@94
|
193 |
Lookup.Result number = lookup.lookup(new Lookup.Template(Number.class));
|
jtulach@94
|
194 |
Lookup.Result serial = lookup.lookup(new Lookup.Template(Serializable.class));
|
jtulach@94
|
195 |
|
jtulach@94
|
196 |
{
|
jtulach@94
|
197 |
integer.addLookupListener(this);
|
jtulach@94
|
198 |
number.addLookupListener(this);
|
jtulach@94
|
199 |
serial.addLookupListener(this);
|
jtulach@94
|
200 |
}
|
jtulach@94
|
201 |
|
jtulach@94
|
202 |
int round;
|
jtulach@94
|
203 |
|
jtulach@94
|
204 |
public void resultChanged(LookupEvent ev) {
|
jtulach@94
|
205 |
Collection c1 = get(type, integer);
|
jtulach@94
|
206 |
Collection c2 = get(type, number);
|
jtulach@94
|
207 |
Collection c3 = get(type, serial);
|
jtulach@94
|
208 |
|
jtulach@94
|
209 |
assertEquals("round " + round + " c1 vs. c2", c1, c2);
|
jtulach@94
|
210 |
assertEquals("round " + round + " c1 vs. c3", c1, c3);
|
jtulach@94
|
211 |
assertEquals("round " + round + " c2 vs. c3", c2, c3);
|
jtulach@94
|
212 |
|
jtulach@94
|
213 |
round++;
|
jtulach@94
|
214 |
}
|
jtulach@94
|
215 |
|
jtulach@94
|
216 |
private Collection get(int type, Lookup.Result res) {
|
jtulach@94
|
217 |
Collection c;
|
jtulach@94
|
218 |
switch(type) {
|
jtulach@94
|
219 |
case 0: c = res.allInstances(); break;
|
jtulach@94
|
220 |
case 1: c = res.allClasses(); break;
|
jtulach@94
|
221 |
case 2: c = res.allItems(); break;
|
jtulach@94
|
222 |
default: c = null; fail("Type: " + type); break;
|
jtulach@94
|
223 |
}
|
jtulach@94
|
224 |
|
jtulach@94
|
225 |
assertNotNull(c);
|
jtulach@94
|
226 |
return new ArrayList(c);
|
jtulach@94
|
227 |
}
|
jtulach@94
|
228 |
}
|
jtulach@94
|
229 |
|
jtulach@94
|
230 |
L listener = new L();
|
jtulach@94
|
231 |
listener.resultChanged(null);
|
jtulach@94
|
232 |
ArrayList arr = new ArrayList();
|
jtulach@94
|
233 |
for(int i = 0; i < 100; i++) {
|
jtulach@94
|
234 |
arr.add(new Integer(i));
|
jtulach@94
|
235 |
|
jtulach@94
|
236 |
orig.lookup = Lookups.fixed(arr.toArray());
|
jtulach@94
|
237 |
// do the refresh
|
jtulach@94
|
238 |
first.lookup((Class)null);
|
jtulach@94
|
239 |
}
|
jtulach@94
|
240 |
|
jtulach@94
|
241 |
assertEquals("3x100+1 checks", 301, listener.round);
|
jtulach@94
|
242 |
}
|
jtulach@9
|
243 |
|
jtulach@280
|
244 |
|
jtulach@280
|
245 |
public void testRefreshWithoutAllInstances103300 () {
|
jtulach@280
|
246 |
Changer ch = new Changer (Lookup.EMPTY);
|
jtulach@280
|
247 |
|
jtulach@280
|
248 |
Lookup lookup = Lookups.proxy(ch);
|
jtulach@280
|
249 |
|
jtulach@280
|
250 |
ch.setLookup (new AbstractLookup (new InstanceContent ())); // another empty lookup
|
jtulach@280
|
251 |
assertNull("Nothing there", lookup.lookup (Object.class)); // does the refresh
|
jtulach@280
|
252 |
|
jtulach@280
|
253 |
InstanceContent content = new InstanceContent ();
|
jtulach@280
|
254 |
AbstractLookup del = new AbstractLookup (content);
|
jtulach@280
|
255 |
content.add (this);
|
jtulach@280
|
256 |
ch.setLookup (del);
|
jtulach@280
|
257 |
assertEquals("Can see me", this, lookup.lookup (Object.class));
|
jtulach@280
|
258 |
|
jtulach@280
|
259 |
ch.setLookup (del);
|
jtulach@280
|
260 |
assertEquals("Still can see me", this, lookup.lookup (Object.class));
|
jtulach@280
|
261 |
|
jtulach@280
|
262 |
assertEquals("I am visible", this, lookup.lookup(LookupsProxyTest.class));
|
jtulach@280
|
263 |
}
|
jtulach@280
|
264 |
|
jtulach@280
|
265 |
|
jtulach@9
|
266 |
private static final class Changer implements Lookup.Provider {
|
jtulach@9
|
267 |
private Lookup lookup;
|
jtulach@9
|
268 |
|
jtulach@9
|
269 |
public Changer (Lookup lookup) {
|
jtulach@9
|
270 |
setLookup (lookup);
|
jtulach@9
|
271 |
}
|
jtulach@9
|
272 |
|
jtulach@9
|
273 |
public void setLookup (Lookup lookup) {
|
jtulach@9
|
274 |
this.lookup = lookup;
|
jtulach@9
|
275 |
}
|
jtulach@9
|
276 |
|
jtulach@9
|
277 |
public Lookup getLookup() {
|
jtulach@9
|
278 |
return lookup;
|
jtulach@9
|
279 |
}
|
jtulach@9
|
280 |
}
|
jtulach@280
|
281 |
|
jtulach@9
|
282 |
}
|