openide.util/test/unit/src/org/openide/util/lookup/SimpleProxyLookupIssue42244Test.java
author Jesse Glick <jglick@netbeans.org>
Fri, 09 Oct 2009 15:11:13 -0400
changeset 833 0e00857c5827
parent 829 3b2ed3e1f01b
permissions -rw-r--r--
Warnings.
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@9
    44
import java.lang.ref.WeakReference;
jtulach@9
    45
import java.util.*;
jtulach@9
    46
import junit.framework.*;
jtulach@9
    47
import org.netbeans.junit.*;
jtulach@550
    48
import org.openide.util.Lookup;
jtulach@9
    49
jtulach@9
    50
/** To simulate issue 42244.
jtulach@9
    51
 */
jglick@833
    52
@SuppressWarnings("unchecked") // XXX ought to be corrected, just a lot of them
jtulach@9
    53
public class SimpleProxyLookupIssue42244Test extends AbstractLookupBaseHid implements AbstractLookupBaseHid.Impl {
jtulach@9
    54
    public SimpleProxyLookupIssue42244Test (java.lang.String testName) {
jtulach@9
    55
        super(testName, null);
jtulach@9
    56
    }
jtulach@189
    57
jtulach@338
    58
    public static Test suite() {
jtulach@338
    59
        // return new SimpleProxyLookupIssue42244Test("testGarbageCollect");
jtulach@338
    60
        return new NbTestSuite(SimpleProxyLookupIssue42244Test.class);
jtulach@9
    61
    }
jtulach@9
    62
    
jtulach@9
    63
    /** Creates an lookup for given lookup. This class just returns 
jtulach@9
    64
     * the object passed in, but subclasses can be different.
jtulach@9
    65
     * @param lookup in lookup
jtulach@9
    66
     * @return a lookup to use
jtulach@9
    67
     */
jtulach@9
    68
    public Lookup createLookup (final Lookup lookup) {
jtulach@9
    69
        class C implements Lookup.Provider {
jtulach@9
    70
            public Lookup getLookup () {
jtulach@9
    71
                return lookup;
jtulach@9
    72
            }
jtulach@9
    73
        }
jtulach@9
    74
        return Lookups.proxy (new C ());
jtulach@9
    75
    }
jtulach@9
    76
    
jtulach@9
    77
    public Lookup createInstancesLookup (InstanceContent ic) {
jtulach@9
    78
        return new KeepResultsProxyLookup (new AbstractLookup (ic));
jtulach@9
    79
    }
jtulach@9
    80
    
jtulach@9
    81
    public void clearCaches () {
jtulach@9
    82
        KeepResultsProxyLookup k = (KeepResultsProxyLookup)this.instanceLookup;
jtulach@9
    83
        
jtulach@9
    84
        ArrayList toGC = new ArrayList ();
jtulach@9
    85
        Iterator it = k.allQueries.iterator ();
jtulach@9
    86
        while (it.hasNext ()) {
jtulach@9
    87
            Lookup.Result r = (Lookup.Result)it.next ();
jtulach@9
    88
            toGC.add (new WeakReference (r));
jtulach@9
    89
        }
jtulach@9
    90
        
jtulach@9
    91
        k.allQueries = null;
jtulach@9
    92
        
jtulach@9
    93
        it = toGC.iterator ();
jtulach@9
    94
        while (it.hasNext ()) {
jtulach@9
    95
            WeakReference r = (WeakReference)it.next ();
jtulach@9
    96
            assertGC ("Trying to release all results from memory", r);
jtulach@9
    97
        }
jtulach@9
    98
    }
jtulach@9
    99
    
jtulach@9
   100
    class KeepResultsProxyLookup extends ProxyLookup {
jtulach@9
   101
        private ArrayList allQueries = new ArrayList ();
jtulach@9
   102
        private ThreadLocal in = new ThreadLocal ();
jtulach@9
   103
        
jtulach@9
   104
        public KeepResultsProxyLookup (Lookup delegate) {
jtulach@9
   105
            super (new Lookup[] { delegate });
jtulach@9
   106
        }
jtulach@9
   107
        
jtulach@550
   108
        @Override
jtulach@9
   109
        protected void beforeLookup (org.openide.util.Lookup.Template template) {
jtulach@9
   110
            super.beforeLookup (template);
jtulach@9
   111
            if (allQueries != null && in.get () == null) {
jtulach@9
   112
                in.set (this);
jtulach@9
   113
                Lookup.Result res = lookup (template);
jtulach@9
   114
                allQueries.add (res);
jtulach@9
   115
                in.set (null);
jtulach@9
   116
            }
jtulach@9
   117
        }
jtulach@9
   118
        
jtulach@9
   119
    }
jtulach@9
   120
}