lookup/src/test/java/org/openide/util/lookup/AbstractLookupTest.java
changeset 972 a2947558c966
parent 971 b3ae88304dd0
child 973 5653a70ebb56
     1.1 --- a/lookup/src/test/java/org/openide/util/lookup/AbstractLookupTest.java	Wed Jan 27 17:46:23 2010 -0500
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,353 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
     1.8 - *
     1.9 - * The contents of this file are subject to the terms of either the GNU
    1.10 - * General Public License Version 2 only ("GPL") or the Common
    1.11 - * Development and Distribution License("CDDL") (collectively, the
    1.12 - * "License"). You may not use this file except in compliance with the
    1.13 - * License. You can obtain a copy of the License at
    1.14 - * http://www.netbeans.org/cddl-gplv2.html
    1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.16 - * specific language governing permissions and limitations under the
    1.17 - * License.  When distributing the software, include this License Header
    1.18 - * Notice in each file and include the License file at
    1.19 - * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.20 - * particular file as subject to the "Classpath" exception as provided
    1.21 - * by Sun in the GPL Version 2 section of the License file that
    1.22 - * accompanied this code. If applicable, add the following below the
    1.23 - * License Header, with the fields enclosed by brackets [] replaced by
    1.24 - * your own identifying information:
    1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
    1.26 - *
    1.27 - * Contributor(s):
    1.28 - *
    1.29 - * The Original Software is NetBeans. The Initial Developer of the Original
    1.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    1.31 - * Microsystems, Inc. All Rights Reserved.
    1.32 - *
    1.33 - * If you wish your version of this file to be governed by only the CDDL
    1.34 - * or only the GPL Version 2, indicate your decision by adding
    1.35 - * "[Contributor] elects to include this software in this distribution
    1.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.37 - * single choice of license, a recipient has the option to distribute
    1.38 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.39 - * to extend the choice of license to its licensees as provided above.
    1.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.41 - * Version 2 license, then the option applies only if the new code is
    1.42 - * made subject to such option by the copyright holder.
    1.43 - */
    1.44 -
    1.45 -package org.openide.util.lookup;
    1.46 -
    1.47 -import java.util.concurrent.ExecutionException;
    1.48 -
    1.49 -import java.lang.ref.WeakReference;
    1.50 -import java.util.*;
    1.51 -import java.util.concurrent.Executors;
    1.52 -import java.util.concurrent.TimeUnit;
    1.53 -import junit.framework.Test;
    1.54 -import org.netbeans.junit.*;
    1.55 -import org.openide.util.Lookup;
    1.56 -import org.openide.util.lookup.AbstractLookup.Pair;
    1.57 -
    1.58 -@SuppressWarnings("unchecked") // XXX ought to be corrected, just a lot of them
    1.59 -public class AbstractLookupTest extends AbstractLookupBaseHid implements AbstractLookupBaseHid.Impl {
    1.60 -    public AbstractLookupTest(java.lang.String testName) {
    1.61 -        super(testName, null);
    1.62 -    }
    1.63 -
    1.64 -    //
    1.65 -    // Impl of AbstractLookupBaseHid.Impl
    1.66 -    //
    1.67 -
    1.68 -    /** Creates the initial abstract lookup.
    1.69 -     */
    1.70 -    public Lookup createInstancesLookup (InstanceContent ic) {
    1.71 -        return new AbstractLookup (ic, new InheritanceTree ());
    1.72 -    }
    1.73 -    
    1.74 -    /** Creates an lookup for given lookup. This class just returns 
    1.75 -     * the object passed in, but subclasses can be different.
    1.76 -     * @param lookup in lookup
    1.77 -     * @return a lookup to use
    1.78 -     */
    1.79 -    public Lookup createLookup (Lookup lookup) {
    1.80 -        return lookup;
    1.81 -    }
    1.82 -
    1.83 -    public void clearCaches () {
    1.84 -    }    
    1.85 -
    1.86 -    static class LkpResultCanBeGargageCollectedAndClearsTheResult extends AbstractLookup {
    1.87 -        public int cleared;
    1.88 -        public int dirty;
    1.89 -
    1.90 -        synchronized @Override boolean cleanUpResult(Template t) {
    1.91 -            boolean res = super.cleanUpResult (t);
    1.92 -            if (res) {
    1.93 -                cleared++;
    1.94 -            } else {
    1.95 -                dirty++;
    1.96 -            }
    1.97 -
    1.98 -            notifyAll ();
    1.99 -
   1.100 -            return res;
   1.101 -        }
   1.102 -    }
   1.103 -    public void testResultCanBeGargageCollectedAndClearsTheResult () throws Exception {
   1.104 -        LkpResultCanBeGargageCollectedAndClearsTheResult lkp = new LkpResultCanBeGargageCollectedAndClearsTheResult ();
   1.105 -        assertSize ("24 for AbstractLookup, 8 for two ints", 32, lkp);
   1.106 -        synchronized (lkp) {
   1.107 -            Lookup.Result res = lkp.lookup (new Lookup.Template (getClass ()));
   1.108 -            res.allItems();
   1.109 -            
   1.110 -            WeakReference ref = new WeakReference (res);
   1.111 -            res = null;
   1.112 -            assertGC ("Reference can get cleared", ref);
   1.113 -         
   1.114 -            // wait till we 
   1.115 -            while (lkp.cleared == 0 && lkp.dirty == 0) {
   1.116 -                lkp.wait ();
   1.117 -            }
   1.118 -            
   1.119 -            assertEquals ("No dirty cleanups", 0, lkp.dirty);
   1.120 -            assertEquals ("One final cleanup", 1, lkp.cleared);
   1.121 -        }
   1.122 -        //assertSize ("Everything has been cleaned to original size", 32, lkp);
   1.123 -        
   1.124 -    }
   1.125 -    
   1.126 -    public void testPairCannotBeUsedInMoreThanOneLookupAtOnce () throws Exception {
   1.127 -        /** Simple pair with no data */
   1.128 -        class EmptyPair extends AbstractLookup.Pair {
   1.129 -            protected boolean creatorOf(Object obj) { return false; }
   1.130 -            public String getDisplayName() { return "Empty"; }
   1.131 -            public String getId() { return "empty"; }
   1.132 -            public Object getInstance() { return null; }
   1.133 -            public Class getType() { return Object.class; }
   1.134 -            protected boolean instanceOf(Class c) { return c == getType (); }
   1.135 -        } // end of EmptyPair
   1.136 -        
   1.137 -        AbstractLookup.Content c1 = new AbstractLookup.Content ();
   1.138 -        AbstractLookup.Content c2 = new AbstractLookup.Content ();
   1.139 -        AbstractLookup l1 = new AbstractLookup (c1);
   1.140 -        AbstractLookup l2 = new AbstractLookup (c2);
   1.141 -        
   1.142 -        EmptyPair empty = new EmptyPair ();
   1.143 -        c1.addPair (empty);
   1.144 -        Lookup.Result res = l1.lookup (new Lookup.Template (Object.class));
   1.145 -        assertEquals (
   1.146 -            "Pair is really found", empty, 
   1.147 -            res.allItems ().iterator().next ()
   1.148 -        );
   1.149 -        try {
   1.150 -            c2.addPair (empty);
   1.151 -            fail ("It should not be possible to add pair to two lookups");
   1.152 -        } catch (IllegalStateException ex) {
   1.153 -            // ok, exception is fine
   1.154 -        }
   1.155 -        assertEquals (
   1.156 -            "L2 is still empty", Collections.EMPTY_LIST, 
   1.157 -            new ArrayList (l2.lookup (new Lookup.Template (Object.class)).allItems ())
   1.158 -        );
   1.159 -    }
   1.160 -    
   1.161 -    public void testInitializationCanBeDoneFromAnotherThread () {
   1.162 -        class MyLkp extends AbstractLookup implements Runnable {
   1.163 -            private InstanceContent ic;
   1.164 -            private boolean direct;
   1.165 -            
   1.166 -            public MyLkp (boolean direct) {
   1.167 -                this (direct, new InstanceContent ());
   1.168 -            }
   1.169 -                
   1.170 -            private MyLkp (boolean direct, InstanceContent ic) {
   1.171 -                super (ic);
   1.172 -                this.direct = direct;
   1.173 -                this.ic = ic;
   1.174 -            }
   1.175 -            
   1.176 -            protected @Override void initialize() {
   1.177 -                if (direct) {
   1.178 -                    run ();
   1.179 -                } else {
   1.180 -                    try {
   1.181 -                        Executors.newSingleThreadScheduledExecutor().schedule(this, 0, TimeUnit.MICROSECONDS).get();
   1.182 -                    } catch (InterruptedException ex) {
   1.183 -                        ex.printStackTrace();
   1.184 -                    } catch (ExecutionException ex) {
   1.185 -                        ex.printStackTrace();
   1.186 -                    }
   1.187 -                }
   1.188 -            }
   1.189 -            
   1.190 -            public void run () {
   1.191 -                ic.add (this);
   1.192 -                ic.remove (this);
   1.193 -                ic.set (Collections.nCopies(10, this), null);
   1.194 -                ic.set (Collections.EMPTY_LIST, null);
   1.195 -                ic.add (AbstractLookupTest.this);
   1.196 -            }
   1.197 -        }
   1.198 -        
   1.199 -        assertEquals ("The test should be there", this, new MyLkp (true).lookup (Object.class));
   1.200 -        assertEquals ("and in async mode as well", this, new MyLkp (false).lookup (Object.class));
   1.201 -    }
   1.202 -    
   1.203 -    public void testBeforeLookupIsCalled () {
   1.204 -        class BeforeL extends AbstractLookup {
   1.205 -            public ArrayList list = new ArrayList ();
   1.206 -            public String toAdd;
   1.207 -            public InstanceContent ic;
   1.208 -            
   1.209 -            public BeforeL () {
   1.210 -                this (new InstanceContent ());
   1.211 -            }
   1.212 -            
   1.213 -            private BeforeL (InstanceContent c) {
   1.214 -                super (c);
   1.215 -                this.ic = c;
   1.216 -            }
   1.217 -        
   1.218 -            protected @Override void beforeLookup(Template t) {
   1.219 -                if (toAdd != null) {
   1.220 -                    list.add (0, new SerialPair (toAdd));
   1.221 -                    setPairs (list);
   1.222 -                } else {
   1.223 -                    ic.add (new Integer (1));
   1.224 -                }
   1.225 -            }
   1.226 -        }
   1.227 -        
   1.228 -        BeforeL lookup = new BeforeL ();
   1.229 -        
   1.230 -        lookup.toAdd = "First";
   1.231 -        assertEquals ("First if found", "First", lookup.lookup (String.class));
   1.232 -        
   1.233 -        lookup.toAdd = "2";
   1.234 -        assertEquals ("2 is not first", "2", lookup.lookup (String.class));
   1.235 -        
   1.236 -        Lookup.Result res = lookup.lookup (new Lookup.Template (Object.class));
   1.237 -        for (int i = 3; i < 20; i++) {
   1.238 -            lookup.toAdd = String.valueOf (i);
   1.239 -            assertEquals (i + " items are now there", i, res.allInstances ().size ());
   1.240 -        }
   1.241 -        for (int i = 20; i < 35; i++) {
   1.242 -            lookup.toAdd = String.valueOf (i);
   1.243 -            assertEquals (i + " items are now there", i, res.allItems ().size ());
   1.244 -        }
   1.245 -        
   1.246 -        assertEquals ("Just strings are there now", 1, res.allClasses ().size ());
   1.247 -        lookup.toAdd = null; // this will add integer
   1.248 -        assertEquals ("Two classes now", 2, res.allClasses ().size ());
   1.249 -    }
   1.250 -
   1.251 -    public void testInconsistentAfterDeserIssue71744() throws Exception {
   1.252 -        InheritanceTree inhTree = new InheritanceTree();
   1.253 -
   1.254 -        AbstractLookup al = new AbstractLookup(new AbstractLookup.Content(), inhTree);
   1.255 -        {
   1.256 -
   1.257 -            Collection r = al.lookup(new Lookup.Template(Integer.class)).allInstances();
   1.258 -            assertEquals("None", 0, r.size());
   1.259 -        }
   1.260 -
   1.261 -        ICP item = new ICP(new Integer(10));
   1.262 -        al.addPair(item);
   1.263 -        al.removePair(item);
   1.264 -
   1.265 -        AbstractLookup newLookup = (AbstractLookup)reserialize(al);
   1.266 -
   1.267 -        newLookup.lookup(Number.class);
   1.268 -
   1.269 -
   1.270 -        newLookup.addPair(new ICP(new Long(20)));
   1.271 -
   1.272 -        {
   1.273 -
   1.274 -            Collection r = newLookup.lookup(new Lookup.Template(Number.class)).allInstances();
   1.275 -            assertEquals("one", 1, r.size());
   1.276 -/*
   1.277 -            Iterator it = r.iterator();
   1.278 -            assertEquals(new Integer(10), it.next());
   1.279 -            assertEquals(new Long(20), it.next());*/
   1.280 -        }
   1.281 -    }
   1.282 -
   1.283 -    public void testMatchesIssue130673() {
   1.284 -        class BrokenPairReturningNullID extends Pair<Object> {
   1.285 -            @Override
   1.286 -            protected boolean instanceOf(Class<?> c) {
   1.287 -                return false;
   1.288 -            }
   1.289 -
   1.290 -            @Override
   1.291 -            protected boolean creatorOf(Object obj) {
   1.292 -                return false;
   1.293 -            }
   1.294 -
   1.295 -            @Override
   1.296 -            public Object getInstance() {
   1.297 -                return null;
   1.298 -            }
   1.299 -
   1.300 -            @Override
   1.301 -            public Class<? extends Object> getType() {
   1.302 -                return null;
   1.303 -            }
   1.304 -
   1.305 -            @Override
   1.306 -            public String getId() {
   1.307 -                return null;
   1.308 -            }
   1.309 -
   1.310 -            @Override
   1.311 -            public String getDisplayName() {
   1.312 -                return null;
   1.313 -            }
   1.314 -        }
   1.315 -        BrokenPairReturningNullID broken = new BrokenPairReturningNullID();
   1.316 -        
   1.317 -        
   1.318 -        Lookup.Template<String> t = new Lookup.Template<String>(String.class, "ID", null);
   1.319 -        boolean not = AbstractLookup.matches(t, broken, true);
   1.320 -        assertFalse("Does not match the template, but throws no exception", not);
   1.321 -    }
   1.322 -    
   1.323 -    private static final class ICP extends AbstractLookup.Pair {
   1.324 -        private Number s;
   1.325 -
   1.326 -        public ICP (Number s) {
   1.327 -            this.s = s;
   1.328 -        }
   1.329 -
   1.330 -
   1.331 -        protected boolean instanceOf(Class c) {
   1.332 -            return c.isInstance(s);
   1.333 -        }
   1.334 -
   1.335 -        protected boolean creatorOf(Object obj) {
   1.336 -            return s == obj;
   1.337 -        }
   1.338 -
   1.339 -        public Object getInstance() {
   1.340 -            return s;
   1.341 -        }
   1.342 -
   1.343 -        public Class getType() {
   1.344 -            return s.getClass();
   1.345 -        }
   1.346 -
   1.347 -        public String getId() {
   1.348 -            return s.toString();
   1.349 -        }
   1.350 -
   1.351 -        public String getDisplayName() {
   1.352 -            return getId();
   1.353 -        }
   1.354 -
   1.355 -    }
   1.356 -}