1 19 20 package org.openide.util.lookup; 21 22 import org.openide.util.*; 23 24 import java.lang.ref.WeakReference ; 25 import java.util.*; 26 import junit.framework.*; 27 import org.netbeans.junit.*; 28 29 31 public class SimpleProxyLookupIssue42244Test extends AbstractLookupBaseHid implements AbstractLookupBaseHid.Impl { 32 public SimpleProxyLookupIssue42244Test (java.lang.String testName) { 33 super(testName, null); 34 } 35 36 public static void main(java.lang.String [] args) { 37 junit.textui.TestRunner.run(new NbTestSuite (SimpleProxyLookupIssue42244Test.class)); 38 } 39 40 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 (r)); 66 } 67 68 k.allQueries = null; 69 70 it = toGC.iterator (); 71 while (it.hasNext ()) { 72 WeakReference r = (WeakReference )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 in = new ThreadLocal (); 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 |