KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** To simulate issue 42244.
30  */

31 public class SimpleProxyLookupIssue42244Test extends AbstractLookupBaseHid implements AbstractLookupBaseHid.Impl {
32     public SimpleProxyLookupIssue42244Test (java.lang.String JavaDoc testName) {
33         super(testName, null);
34     }
35
36     public static void main(java.lang.String JavaDoc[] args) {
37         junit.textui.TestRunner.run(new NbTestSuite (SimpleProxyLookupIssue42244Test.class));
38     }
39     
40     /** Creates an lookup for given lookup. This class just returns
41      * the object passed in, but subclasses can be different.
42      * @param lookup in lookup
43      * @return a lookup to use
44      */

45     public Lookup createLookup (final Lookup lookup) {
46         class C implements Lookup.Provider {
47             public Lookup getLookup () {
48                 return lookup;
49             }
50         }
51         return Lookups.proxy (new C ());
52     }
53     
54     public Lookup createInstancesLookup (InstanceContent ic) {
55         return new KeepResultsProxyLookup (new AbstractLookup (ic));
56     }
57     
58     public void clearCaches () {
59         KeepResultsProxyLookup k = (KeepResultsProxyLookup)this.instanceLookup;
60         
61         ArrayList toGC = new ArrayList ();
62         Iterator it = k.allQueries.iterator ();
63         while (it.hasNext ()) {
64             Lookup.Result r = (Lookup.Result)it.next ();
65             toGC.add (new WeakReference JavaDoc (r));
66         }
67         
68         k.allQueries = null;
69         
70         it = toGC.iterator ();
71         while (it.hasNext ()) {
72             WeakReference JavaDoc r = (WeakReference JavaDoc)it.next ();
73             assertGC ("Trying to release all results from memory", r);
74         }
75     }
76     
77     class KeepResultsProxyLookup extends ProxyLookup {
78         private ArrayList allQueries = new ArrayList ();
79         private ThreadLocal JavaDoc in = new ThreadLocal JavaDoc ();
80         
81         public KeepResultsProxyLookup (Lookup delegate) {
82             super (new Lookup[] { delegate });
83         }
84         
85         protected void beforeLookup (org.openide.util.Lookup.Template template) {
86             super.beforeLookup (template);
87             if (allQueries != null && in.get () == null) {
88                 in.set (this);
89                 Lookup.Result res = lookup (template);
90                 allQueries.add (res);
91                 in.set (null);
92             }
93         }
94         
95     }
96 }
97
Popular Tags