Lines 36-70
Link Here
|
36 |
public static void main(String[] args) { |
36 |
public static void main(String[] args) { |
37 |
TestRunner.run(new NbTestSuite(NodeLookupTest.class)); |
37 |
TestRunner.run(new NbTestSuite(NodeLookupTest.class)); |
38 |
} |
38 |
} |
39 |
|
39 |
|
40 |
public void testChangesAreFiredFromLookup () { |
40 |
public void testChangesAreFiredFromLookup () { |
41 |
InstanceContent ic = new InstanceContent (); |
41 |
CountableLookup lkp = new CountableLookup (); |
42 |
AbstractLookup lookup = new AbstractLookup (ic); |
42 |
Node node = new AbstractNode (createChildren (), lkp); |
43 |
Node node = new AbstractNode (Children.LEAF, lookup); |
|
|
44 |
|
43 |
|
45 |
checkInstanceInGetCookie (new Node.Cookie () {}, ic, node); |
44 |
checkGetNodesDoesNotInitializeLookup (node, lkp.queries); |
46 |
checkInstanceInGetLookup (new Node.Cookie () {}, ic, node, true); |
45 |
checkInstanceInGetCookie (new Node.Cookie () {}, lkp.ic, node); |
47 |
checkInstanceInGetLookup ("Some string", ic, node, true); |
46 |
checkInstanceInGetLookup (new Node.Cookie () {}, lkp.ic, node, true); |
|
|
47 |
checkInstanceInGetLookup ("Some string", lkp.ic, node, true); |
48 |
} |
48 |
} |
49 |
|
49 |
|
50 |
public void testChangesAreFiredFromLookupThruFilterNode () { |
50 |
public void testChangesAreFiredFromLookupThruFilterNode () { |
51 |
InstanceContent ic = new InstanceContent (); |
51 |
CountableLookup lkp = new CountableLookup (); |
52 |
AbstractLookup lookup = new AbstractLookup (ic); |
52 |
Node node = new FilterNode (new AbstractNode (createChildren (), lkp)); |
53 |
Node node = new FilterNode (new AbstractNode (Children.LEAF, lookup)); |
|
|
54 |
|
53 |
|
|
|
54 |
checkGetNodesDoesNotInitializeLookup (node, lkp.queries); |
55 |
//checkInstanceInGetCookie (new Node.Cookie () {}, ic, node); |
55 |
//checkInstanceInGetCookie (new Node.Cookie () {}, ic, node); |
56 |
checkInstanceInGetLookup (new Node.Cookie () {}, ic, node, true); |
56 |
checkInstanceInGetLookup (new Node.Cookie () {}, lkp.ic, node, true); |
57 |
checkInstanceInGetLookup ("Some string", ic, node, true); |
57 |
checkInstanceInGetLookup ("Some string", lkp.ic, node, true); |
58 |
} |
58 |
} |
59 |
|
59 |
|
60 |
public void testChangesAreFiredFromLookupThruFilterNodeWithOverWrittenGetCookie () { |
60 |
public void testChangesAreFiredFromLookupThruFilterNodeWithOverWrittenGetCookie () { |
61 |
final Node.Cookie myInstance = new Node.Cookie () { }; |
61 |
final Node.Cookie myInstance = new Node.Cookie () { }; |
62 |
|
62 |
final ArrayList queries = new ArrayList (); |
63 |
|
63 |
|
64 |
InstanceContent ic = new InstanceContent (); |
64 |
InstanceContent ic = new InstanceContent (); |
65 |
AbstractLookup lookup = new AbstractLookup (ic); |
65 |
AbstractLookup lookup = new AbstractLookup (ic); |
66 |
Node node = new FilterNode (new AbstractNode (Children.LEAF, lookup)) { |
66 |
Node node = new FilterNode (new AbstractNode (createChildren (), lookup)) { |
67 |
public Node.Cookie getCookie (Class clazz) { |
67 |
public Node.Cookie getCookie (Class clazz) { |
|
|
68 |
queries.add (clazz); |
69 |
|
68 |
if (clazz == myInstance.getClass ()) { |
70 |
if (clazz == myInstance.getClass ()) { |
69 |
return myInstance; |
71 |
return myInstance; |
70 |
} |
72 |
} |
Lines 72-77
Link Here
|
72 |
} |
74 |
} |
73 |
}; |
75 |
}; |
74 |
|
76 |
|
|
|
77 |
checkGetNodesDoesNotInitializeLookup (node, queries); |
75 |
checkInstanceInGetCookie (new Node.Cookie () {}, ic, node); |
78 |
checkInstanceInGetCookie (new Node.Cookie () {}, ic, node); |
76 |
checkInstanceInGetLookup (new Node.Cookie () {}, ic, node, true); |
79 |
checkInstanceInGetLookup (new Node.Cookie () {}, ic, node, true); |
77 |
// by overwriting the FilterNode.getCookie we disable enhanced support |
80 |
// by overwriting the FilterNode.getCookie we disable enhanced support |
Lines 133-150
Link Here
|
133 |
|
136 |
|
134 |
public void testFilterNodeWithOverridenGetCookieIsInTheLookup () { |
137 |
public void testFilterNodeWithOverridenGetCookieIsInTheLookup () { |
135 |
CookieNode n = new CookieNode (); |
138 |
CookieNode n = new CookieNode (); |
136 |
|
139 |
|
|
|
140 |
final ArrayList queries = new ArrayList (); |
137 |
class MyFN extends FilterNode { |
141 |
class MyFN extends FilterNode { |
138 |
public MyFN (Node n) { |
142 |
public MyFN (Node n) { |
139 |
super (n); |
143 |
super (n); |
140 |
} |
144 |
} |
141 |
|
145 |
|
142 |
public Node.Cookie getCookie (Class clazz) { |
146 |
public Node.Cookie getCookie (Class clazz) { |
|
|
147 |
queries.add (clazz); |
143 |
return super.getCookie (clazz); |
148 |
return super.getCookie (clazz); |
144 |
} |
149 |
} |
145 |
} |
150 |
} |
146 |
|
151 |
|
147 |
FilterNode fn = new MyFN (n); |
152 |
FilterNode fn = new MyFN (n); |
|
|
153 |
checkGetNodesDoesNotInitializeLookup (fn, queries); |
154 |
checkGetNodesDoesNotInitializeLookup (fn, n.queries); |
155 |
|
148 |
Lookup l = fn.getLookup (); |
156 |
Lookup l = fn.getLookup (); |
149 |
|
157 |
|
150 |
// == must be used instead of equals for nodes!!! |
158 |
// == must be used instead of equals for nodes!!! |
Lines 158-165
Link Here
|
158 |
CookieNode n = new CookieNode (); |
166 |
CookieNode n = new CookieNode (); |
159 |
|
167 |
|
160 |
FilterNode fn = new FilterNode (n); |
168 |
FilterNode fn = new FilterNode (n); |
161 |
Lookup l = fn.getLookup (); |
169 |
checkGetNodesDoesNotInitializeLookup (fn, n.queries); |
162 |
|
170 |
|
|
|
171 |
Lookup l = fn.getLookup (); |
163 |
// == must be used instead of equals for nodes!!! |
172 |
// == must be used instead of equals for nodes!!! |
164 |
assertTrue ("Node is there", fn == l.lookup (Node.class)); |
173 |
assertTrue ("Node is there", fn == l.lookup (Node.class)); |
165 |
Collection c = l.lookup (new Lookup.Template (Node.class)).allInstances (); |
174 |
Collection c = l.lookup (new Lookup.Template (Node.class)).allInstances (); |
Lines 229-234
Link Here
|
229 |
|
238 |
|
230 |
ic.remove (obj); |
239 |
ic.remove (obj); |
231 |
listener.assertEvents ("One change in lookup", -1, 1); |
240 |
listener.assertEvents ("One change in lookup", -1, 1); |
|
|
241 |
} |
242 |
|
243 |
private static void checkGetNodesDoesNotInitializeLookup (final org.openide.nodes.Node n, java.util.List queried) { |
244 |
assertEquals ("No queries before", Collections.EMPTY_LIST, queried); |
245 |
|
246 |
class MyCh extends Children.Keys { |
247 |
protected void addNotify () { |
248 |
setKeys (java.util.Collections.singleton(n)); |
249 |
} |
250 |
|
251 |
public void clear () { |
252 |
setKeys (java.util.Collections.EMPTY_LIST); |
253 |
} |
254 |
|
255 |
protected Node[] createNodes (Object key) { |
256 |
return new Node[] { n }; |
257 |
} |
258 |
}; |
259 |
MyCh ch = new MyCh (); |
260 |
|
261 |
// initialize the node N |
262 |
new AbstractNode (ch).getChildren().getNodes (); |
263 |
|
264 |
assertEquals ("No queries after", Collections.EMPTY_LIST, queried); |
265 |
ch.clear (); |
266 |
assertEquals ("No queries after clean either", Collections.EMPTY_LIST, queried); |
232 |
|
267 |
|
233 |
} |
268 |
} |
234 |
|
269 |
|
Lines 261-267
Link Here
|
261 |
|
296 |
|
262 |
|
297 |
|
263 |
public void testBackwardCompatibleAbstractNodeLookupCanBeGarbageCollected () { |
298 |
public void testBackwardCompatibleAbstractNodeLookupCanBeGarbageCollected () { |
264 |
AbstractNode n = new AbstractNode (Children.LEAF); |
299 |
AbstractNode n = new AbstractNode (createChildren ()); |
265 |
|
300 |
|
266 |
Lookup l = n.getLookup (); |
301 |
Lookup l = n.getLookup (); |
267 |
assertEquals ("Two invocations share the same lookup", l, n.getLookup ()); |
302 |
assertEquals ("Two invocations share the same lookup", l, n.getLookup ()); |
Lines 271-276
Link Here
|
271 |
assertGC ("Lookup can be GCed", ref); |
306 |
assertGC ("Lookup can be GCed", ref); |
272 |
} |
307 |
} |
273 |
|
308 |
|
|
|
309 |
|
274 |
/** Assert GC. |
310 |
/** Assert GC. |
275 |
*/ |
311 |
*/ |
276 |
private static void assertGC (String text, java.lang.ref.Reference ref) { |
312 |
private static void assertGC (String text, java.lang.ref.Reference ref) { |
Lines 284-289
Link Here
|
284 |
fail (text + " " + ref.get ()); |
320 |
fail (text + " " + ref.get ()); |
285 |
} |
321 |
} |
286 |
|
322 |
|
|
|
323 |
private static Children createChildren () { |
324 |
return Children.LEAF; |
325 |
} |
326 |
|
287 |
|
327 |
|
288 |
private static class Listener extends Object |
328 |
private static class Listener extends Object |
289 |
implements LookupListener, NodeListener { |
329 |
implements LookupListener, NodeListener { |
Lines 326-336
Link Here
|
326 |
} // end of Listener |
366 |
} // end of Listener |
327 |
|
367 |
|
328 |
private static class CookieNode extends AbstractNode { |
368 |
private static class CookieNode extends AbstractNode { |
|
|
369 |
public ArrayList queries = new ArrayList (); |
370 |
|
329 |
public CookieNode () { |
371 |
public CookieNode () { |
330 |
super (Children.LEAF); |
372 |
super (createChildren ()); |
331 |
} |
373 |
} |
332 |
public CookieNode (Lookup l) { |
374 |
public CookieNode (Lookup l) { |
333 |
super (Children.LEAF, l); |
375 |
super (createChildren (), l); |
334 |
} |
376 |
} |
335 |
|
377 |
|
336 |
public CookieSet cookieSet () { |
378 |
public CookieSet cookieSet () { |
Lines 339-344
Link Here
|
339 |
public void setSet (CookieSet s) { |
381 |
public void setSet (CookieSet s) { |
340 |
super.setCookieSet (s); |
382 |
super.setCookieSet (s); |
341 |
} |
383 |
} |
|
|
384 |
|
385 |
public Node.Cookie getCookie (Class c) { |
386 |
queries.add (c); |
387 |
return super.getCookie (c); |
388 |
} |
342 |
} // end of CookieNode |
389 |
} // end of CookieNode |
|
|
390 |
|
391 |
|
392 |
private static class CountableLookup extends AbstractLookup { |
393 |
public final InstanceContent ic; |
394 |
public final ArrayList queries; |
395 |
|
396 |
public CountableLookup () { |
397 |
this (new InstanceContent (), new ArrayList ()); |
398 |
} |
399 |
|
400 |
private CountableLookup (InstanceContent ic, ArrayList queries) { |
401 |
super (ic); |
402 |
this.ic = ic; |
403 |
this.queries = queries; |
404 |
} |
405 |
|
406 |
protected void beforeLookup (Lookup.Template t) { |
407 |
super.beforeLookup (t); |
408 |
queries.add (t.getType ()); |
409 |
} |
410 |
} |
343 |
} |
411 |
} |
344 |
|
412 |
|