openide.util.lookup/test/unit/src/org/openide/util/lookup/ProxyLookupTest.java
changeset 972 a2947558c966
parent 971 b3ae88304dd0
child 973 5653a70ebb56
     1.1 --- a/openide.util.lookup/test/unit/src/org/openide/util/lookup/ProxyLookupTest.java	Wed Jan 27 17:46:23 2010 -0500
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,657 +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.io.Serializable;
    1.48 -
    1.49 -import java.lang.ref.Reference;
    1.50 -import java.lang.ref.WeakReference;
    1.51 -import java.util.ArrayList;
    1.52 -import java.util.Collection;
    1.53 -import java.util.Collections;
    1.54 -import java.util.concurrent.Executor;
    1.55 -import junit.framework.Test;
    1.56 -import org.netbeans.junit.NbTestSuite;
    1.57 -import org.openide.util.Lookup;
    1.58 -import org.openide.util.Lookup.Result;
    1.59 -import org.openide.util.LookupEvent;
    1.60 -import org.openide.util.LookupListener;
    1.61 -import org.openide.util.lookup.implspi.ActiveQueue;
    1.62 -
    1.63 -/** Runs all NbLookupTest tests on ProxyLookup and adds few additional.
    1.64 - */
    1.65 -@SuppressWarnings("unchecked") // XXX ought to be corrected, just a lot of them
    1.66 -public class ProxyLookupTest extends AbstractLookupBaseHid
    1.67 -implements AbstractLookupBaseHid.Impl {
    1.68 -    public ProxyLookupTest(java.lang.String testName) {
    1.69 -        super(testName, null);
    1.70 -    }
    1.71 -
    1.72 -    public static Test suite() {
    1.73 -        return new NbTestSuite (ProxyLookupTest.class);
    1.74 -//        return new ProxyLookupTest("testDuplicatedLookupArrayIndexWithSetLookupAsInIssue123679");
    1.75 -    }
    1.76 -    
    1.77 -    /** Creates an lookup for given lookup. This class just returns 
    1.78 -     * the object passed in, but subclasses can be different.
    1.79 -     * @param lookup in lookup
    1.80 -     * @return a lookup to use
    1.81 -     */
    1.82 -    public Lookup createLookup (Lookup lookup) {
    1.83 -        return new ProxyLookup (new Lookup[] { lookup });
    1.84 -    }
    1.85 -    
    1.86 -    public Lookup createInstancesLookup (InstanceContent ic) {
    1.87 -        return new AbstractLookup (ic);
    1.88 -    }
    1.89 -    
    1.90 -
    1.91 -    public void clearCaches () {
    1.92 -    }    
    1.93 -    
    1.94 -    
    1.95 -    /** Check whether setLookups method does not fire when there is no
    1.96 -     * change in the lookups.
    1.97 -     */
    1.98 -    public void testProxyListener () {
    1.99 -        ProxyLookup lookup = new ProxyLookup (new Lookup[0]);
   1.100 -
   1.101 -        final Lookup.Template<Object> template = new Lookup.Template<Object>(Object.class);
   1.102 -        final Object[] IGNORE = {
   1.103 -            ProxyLookup.ImmutableInternalData.EMPTY,
   1.104 -            ProxyLookup.ImmutableInternalData.EMPTY_ARR,
   1.105 -            ActiveQueue.queue(),
   1.106 -            Collections.emptyMap(),
   1.107 -            Collections.emptyList(),
   1.108 -            Collections.emptySet()
   1.109 -        };
   1.110 -        
   1.111 -        assertSize("Pretty small", Collections.singleton(lookup), 16, IGNORE);
   1.112 -        
   1.113 -        Lookup.Result<Object> res = lookup.lookup (template);
   1.114 -
   1.115 -        assertSize("Bigger", Collections.singleton(lookup), 216, IGNORE);
   1.116 -        
   1.117 -        LL ll = new LL ();
   1.118 -        res.addLookupListener (ll);
   1.119 -        Collection allRes = res.allInstances ();
   1.120 -        
   1.121 -        lookup.setLookups (new Lookup[0]);
   1.122 -        
   1.123 -        if (ll.getCount () != 0) {
   1.124 -           fail ("Calling setLookups (emptyarray) fired a change");
   1.125 -        }
   1.126 -        
   1.127 -        InstanceContent t = new InstanceContent();
   1.128 -        Lookup del = new AbstractLookup (t);
   1.129 -        t.add("Ahoj");
   1.130 -        lookup.setLookups (new Lookup[] { del });
   1.131 -        
   1.132 -        if (ll.getCount () != 1) {
   1.133 -            fail ("Changing lookups did not generate an event");
   1.134 -        }
   1.135 -        
   1.136 -        lookup.setLookups (new Lookup[] { del });
   1.137 -        
   1.138 -        if (ll.getCount () != 0) {
   1.139 -           fail ("Calling setLookups (thesamearray) fired a change");
   1.140 -        }
   1.141 -    }
   1.142 -    
   1.143 -    public void testNoListenersProxyListener () {
   1.144 -        ProxyLookup lookup = new ProxyLookup (new Lookup[0]);
   1.145 -        class E implements Executor {
   1.146 -            Runnable r;
   1.147 -            public void execute(Runnable command) {
   1.148 -                assertNull("NO previous", r);
   1.149 -                r = command;
   1.150 -            }
   1.151 -            public void perform() {
   1.152 -                assertNotNull("We shall have a runnable", r);
   1.153 -                r.run();
   1.154 -                r = null;
   1.155 -            }
   1.156 -        }
   1.157 -        E executor = new E();
   1.158 -                
   1.159 -
   1.160 -        final Lookup.Template<Object> template = new Lookup.Template<Object>(Object.class);
   1.161 -        final Object[] IGNORE = {
   1.162 -            ProxyLookup.ImmutableInternalData.EMPTY,
   1.163 -            ProxyLookup.ImmutableInternalData.EMPTY_ARR,
   1.164 -            ActiveQueue.queue(),
   1.165 -            Collections.emptyMap(),
   1.166 -            Collections.emptyList(),
   1.167 -            Collections.emptySet()
   1.168 -        };
   1.169 -        
   1.170 -        assertSize("Pretty small", Collections.singleton(lookup), 16, IGNORE);
   1.171 -        
   1.172 -        Lookup.Result<Object> res = lookup.lookup (template);
   1.173 -
   1.174 -        assertSize("Bigger", Collections.singleton(lookup), 216, IGNORE);
   1.175 -        
   1.176 -        LL ll = new LL ();
   1.177 -        res.addLookupListener (ll);
   1.178 -        Collection allRes = res.allInstances ();
   1.179 -        
   1.180 -        lookup.setLookups (executor, new Lookup[0]);
   1.181 -        if (ll.getCount () != 0) {
   1.182 -           fail ("Calling setLookups (emptyarray) fired a change");
   1.183 -        }
   1.184 -        
   1.185 -        InstanceContent t = new InstanceContent();
   1.186 -        Lookup del = new AbstractLookup (t);
   1.187 -        t.add("Ahoj");
   1.188 -        lookup.setLookups (executor, new Lookup[] { del });
   1.189 -        assertEquals("No change yet", 0, ll.getCount());
   1.190 -        executor.perform();
   1.191 -        if (ll.getCount () != 1) {
   1.192 -            fail ("Changing lookups did not generate an event");
   1.193 -        }
   1.194 -        
   1.195 -        lookup.setLookups (executor, new Lookup[] { del });
   1.196 -        if (ll.getCount () != 0) {
   1.197 -           fail ("Calling setLookups (thesamearray) fired a change");
   1.198 -        }
   1.199 -    }
   1.200 -
   1.201 -    public void testSetLookups () throws Exception {
   1.202 -        AbstractLookup a1 = new AbstractLookup (new InstanceContent ());
   1.203 -        AbstractLookup a2 = new AbstractLookup (new InstanceContent ());
   1.204 -        
   1.205 -        InstanceContent i3 = new InstanceContent ();
   1.206 -        i3.add (i3);
   1.207 -        AbstractLookup a3 = new AbstractLookup (i3);
   1.208 -
   1.209 -        final ProxyLookup p = new ProxyLookup (new Lookup[] { a1, a2 });
   1.210 -        final Lookup.Result res1 = p.lookup (new Lookup.Template (Object.class));
   1.211 -        Collection c1 = res1.allInstances();
   1.212 -        
   1.213 -        Lookup.Result res2 = p.lookup (new Lookup.Template (String.class));
   1.214 -        Collection c2 = res2.allInstances ();
   1.215 -        
   1.216 -        
   1.217 -        assertTrue ("We need two results", res1 != res2);
   1.218 -
   1.219 -        final Object blocked = new Object ();
   1.220 -
   1.221 -        class L extends Object implements LookupListener {
   1.222 -            public void resultChanged (LookupEvent ev) {
   1.223 -                try {
   1.224 -                    res1.removeLookupListener(this);
   1.225 -                    
   1.226 -                    // waiting for second thread to start #111#
   1.227 -                    blocked.wait ();
   1.228 -
   1.229 -                } catch (Exception ex) {
   1.230 -                    ex.printStackTrace();
   1.231 -                    fail ("An exception occured ");
   1.232 -                }
   1.233 -            }
   1.234 -        }
   1.235 -        
   1.236 -        final L listener1 = new L ();
   1.237 -        res1.addLookupListener (listener1);
   1.238 -        
   1.239 -
   1.240 -        Runnable newLookupSetter = new Runnable() {
   1.241 -            public void run () {
   1.242 -                synchronized (blocked) {
   1.243 -                    try {
   1.244 -                        p.setLookups (new Lookup[0]);
   1.245 -                    } catch (Exception ex) {
   1.246 -                        ex.printStackTrace();
   1.247 -                        fail ("setLookups failed.");
   1.248 -                    } finally {
   1.249 -                        // starts the main thread #111#
   1.250 -                        blocked.notify ();
   1.251 -                    }
   1.252 -                }
   1.253 -            }
   1.254 -        };
   1.255 -        
   1.256 -        synchronized (blocked) {
   1.257 -            new Thread (newLookupSetter).start ();
   1.258 -            
   1.259 -            p.setLookups (new Lookup[] { a1, a2, a3 });
   1.260 -        }
   1.261 -    }
   1.262 -    
   1.263 -    public void testProxyLookupTemplateCaching(){
   1.264 -        Lookup lookups[] = new Lookup[1];
   1.265 -        doProxyLookupTemplateCaching(lookups, false);
   1.266 -    }
   1.267 -    
   1.268 -    public void testProxyLookupTemplateCachingOnSizeTwoArray() {
   1.269 -        Lookup lookups[] = new Lookup[2];
   1.270 -        lookups[1] = Lookup.EMPTY;
   1.271 -        doProxyLookupTemplateCaching(lookups, false);
   1.272 -    }
   1.273 -    public void testProxyLookupShallNotAllowModificationOfGetLookups(){
   1.274 -        Lookup lookups[] = new Lookup[1];
   1.275 -        doProxyLookupTemplateCaching(lookups, true);
   1.276 -    }
   1.277 -    
   1.278 -    public void testProxyLookupShallNotAllowModificationOfGetLookupsOnSizeTwoArray() {
   1.279 -        Lookup lookups[] = new Lookup[2];
   1.280 -        lookups[1] = Lookup.EMPTY;
   1.281 -        doProxyLookupTemplateCaching(lookups, true);
   1.282 -    }
   1.283 -    
   1.284 -    /** Index 0 of lookups will be modified, the rest is up to the 
   1.285 -     * setup code.
   1.286 -     */
   1.287 -    private void doProxyLookupTemplateCaching(Lookup[] lookups, boolean reget) {
   1.288 -        // Create MyProxyLookup with one lookup containing the String object
   1.289 -        InstanceContent inst = new InstanceContent();
   1.290 -        inst.add(new String("Hello World")); //NOI18N
   1.291 -        lookups[0] = new AbstractLookup(inst);
   1.292 -        ProxyLookup proxy = new ProxyLookup(lookups);
   1.293 -        if (reget) {
   1.294 -            lookups = proxy.getLookups();
   1.295 -        }
   1.296 -        
   1.297 -        // Performing template lookup for String object
   1.298 -        Lookup.Result result = proxy.lookup(new Lookup.Template(String.class, null, null));
   1.299 -        int stringTemplateResultSize = result.allInstances().size();
   1.300 -        assertEquals ("Ensure, there is only one instance of String.class in proxyLookup:", //NOI18N
   1.301 -                1, stringTemplateResultSize);
   1.302 -        
   1.303 -        // Changing lookup in proxy lookup, now it will contain 
   1.304 -        // StringBuffer Object instead of String
   1.305 -        InstanceContent ic2 = new InstanceContent();
   1.306 -        ic2.add(new Integer(1234567890));
   1.307 -        lookups[0] = new AbstractLookup(ic2);
   1.308 -        proxy.setLookups(lookups);
   1.309 -        
   1.310 -        assertEquals ("the old result is updated", 0, result.allInstances().size());
   1.311 -
   1.312 -        // Instance of String.class should not appear in proxyLookup
   1.313 -        Lookup.Result r2 = proxy.lookup(new Lookup.Template(String.class, null, null));
   1.314 -        assertEquals ("Instance of String.class should not appear in proxyLookup:", //NOI18N
   1.315 -                0, r2.allInstances().size());
   1.316 -
   1.317 -        Lookup.Result r3 = proxy.lookup(new Lookup.Template(Integer.class, null, null));
   1.318 -        assertEquals ("There is only one instance of Integer.class in proxyLookup:", //NOI18N
   1.319 -                1, r3.allInstances().size());
   1.320 -    }
   1.321 -    
   1.322 -    public void testListeningAndQueryingByTwoListenersInstancesSetLookups() {
   1.323 -        doListeningAndQueryingByTwoListenersSetLookups(0, 1);
   1.324 -    }
   1.325 -    public void testListeningAndQueryingByTwoListenersClassesSetLookups() {
   1.326 -        doListeningAndQueryingByTwoListenersSetLookups(1, 1);        
   1.327 -    }
   1.328 -    public void testListeningAndQueryingByTwoListenersItemsSetLookups() {
   1.329 -        doListeningAndQueryingByTwoListenersSetLookups(2, 1);
   1.330 -    }
   1.331 -    
   1.332 -    public void testListeningAndQueryingByTwoListenersInstancesSetLookups2() {
   1.333 -        doListeningAndQueryingByTwoListenersSetLookups(0, 2);
   1.334 -    }
   1.335 -    public void testListeningAndQueryingByTwoListenersClassesSetLookups2() {
   1.336 -        doListeningAndQueryingByTwoListenersSetLookups(1, 2);        
   1.337 -    }
   1.338 -    public void testListeningAndQueryingByTwoListenersItemsSetLookups2() {
   1.339 -        doListeningAndQueryingByTwoListenersSetLookups(2, 2);
   1.340 -    }
   1.341 -    public void testListeningAndQueryingByTwoListenersInstancesSetLookups22() {
   1.342 -        doListeningAndQueryingByTwoListenersSetLookups(0, 22);
   1.343 -    }
   1.344 -    public void testListeningAndQueryingByTwoListenersClassesSetLookups22() {
   1.345 -        doListeningAndQueryingByTwoListenersSetLookups(1, 22);        
   1.346 -    }
   1.347 -    public void testListeningAndQueryingByTwoListenersItemsSetLookups22() {
   1.348 -        doListeningAndQueryingByTwoListenersSetLookups(2, 22);
   1.349 -    }
   1.350 -    
   1.351 -    private void doListeningAndQueryingByTwoListenersSetLookups(final int type, int depth) {
   1.352 -        ProxyLookup orig = new ProxyLookup();
   1.353 -        ProxyLookup on = orig;
   1.354 -        
   1.355 -        while (--depth > 0) {
   1.356 -            on = new ProxyLookup(new Lookup[] { on });
   1.357 -        }
   1.358 -        
   1.359 -        
   1.360 -        final ProxyLookup lookup = on;
   1.361 -        
   1.362 -        class L implements LookupListener {
   1.363 -            Lookup.Result integer = lookup.lookup(new Lookup.Template(Integer.class));
   1.364 -            Lookup.Result number = lookup.lookup(new Lookup.Template(Number.class));
   1.365 -            Lookup.Result serial = lookup.lookup(new Lookup.Template(Serializable.class));
   1.366 -            
   1.367 -            {
   1.368 -                integer.addLookupListener(this);
   1.369 -                number.addLookupListener(this);
   1.370 -                serial.addLookupListener(this);
   1.371 -            }
   1.372 -            
   1.373 -            int round;
   1.374 -            
   1.375 -            public void resultChanged(LookupEvent ev) {
   1.376 -                Collection c1 = get(type, integer);
   1.377 -                Collection c2 = get(type, number);
   1.378 -                Collection c3 = get(type, serial);
   1.379 -                
   1.380 -                assertEquals("round " + round + " c1 vs. c2", c1, c2);
   1.381 -                assertEquals("round " + round + " c1 vs. c3", c1, c3);
   1.382 -                assertEquals("round " + round + " c2 vs. c3", c2, c3);
   1.383 -                
   1.384 -                round++;
   1.385 -            }            
   1.386 -
   1.387 -            private Collection get(int type, Lookup.Result res) {
   1.388 -                Collection c;
   1.389 -                switch(type) {
   1.390 -                    case 0: c = res.allInstances(); break;
   1.391 -                    case 1: c = res.allClasses(); break;
   1.392 -                    case 2: c = res.allItems(); break;
   1.393 -                    default: c = null; fail("Type: " + type); break;
   1.394 -                }
   1.395 -                
   1.396 -                assertNotNull(c);
   1.397 -                return new ArrayList(c);
   1.398 -            }
   1.399 -        }
   1.400 -        
   1.401 -        L listener = new L();
   1.402 -        listener.resultChanged(null);
   1.403 -        ArrayList arr = new ArrayList();
   1.404 -        for(int i = 0; i < 100; i++) {
   1.405 -            arr.add(new Integer(i));
   1.406 -            
   1.407 -            orig.setLookups(new Lookup[] { Lookups.fixed(arr.toArray()) });
   1.408 -        }
   1.409 -        
   1.410 -        assertEquals("3x100+1 checks", 301, listener.round);
   1.411 -    }
   1.412 -
   1.413 -    static Object holder;
   1.414 -    
   1.415 -    public void testProxyWithLiveResultCanBeCollected() {
   1.416 -        Lookup layer0 = Lookups.singleton("Hello");
   1.417 -        Lookup layer1 = new ProxyLookup(new Lookup[] { layer0 });
   1.418 -        Lookup layer2 = new ProxyLookup(new Lookup[] { layer1 });
   1.419 -        Lookup.Result result1 = layer1.lookup(new Lookup.Template(String.class));
   1.420 -
   1.421 -        assertEquals("One instance", 1, result1.allInstances().size());
   1.422 -
   1.423 -        // this will create ProxyLookup$R which listens on origResult
   1.424 -        Lookup.Result result2 = layer2.lookup(new Lookup.Template(String.class));
   1.425 -        
   1.426 -        // this line is necessary. W/o actually querying the result,
   1.427 -        // it will nether compute it nor attach the listener.
   1.428 -        assertEquals("One instance", 1, result2.allInstances().size());
   1.429 -        
   1.430 -        result2.addLookupListener(new LookupListener() {
   1.431 -            public void resultChanged(LookupEvent ev) {}
   1.432 -        });
   1.433 -        
   1.434 -        Reference ref = new WeakReference(layer2);
   1.435 -        layer2 = null;
   1.436 -        result2 = null;
   1.437 -        try {
   1.438 -            holder = result1;
   1.439 -            assertGC ("The proxy lookup not been garbage collected!", ref);
   1.440 -        } finally {
   1.441 -            holder = null;
   1.442 -        }
   1.443 -    }
   1.444 -    
   1.445 -    public void testArrayIndexAsInIssue119292() throws Exception {
   1.446 -        final ProxyLookup pl = new ProxyLookup();
   1.447 -        final int[] cnt = { 0 };
   1.448 -        
   1.449 -        class L extends Lookup {
   1.450 -            L[] set;
   1.451 -            Lookup l;
   1.452 -            
   1.453 -            public L(String s) {
   1.454 -                l = Lookups.singleton(s);
   1.455 -            }
   1.456 -            
   1.457 -            @Override
   1.458 -            public <T> T lookup(Class<T> clazz) {
   1.459 -                return l.lookup(clazz);
   1.460 -            }
   1.461 -
   1.462 -            @Override
   1.463 -            public <T> Result<T> lookup(Template<T> template) {
   1.464 -                return l.lookup(template);
   1.465 -            }
   1.466 -
   1.467 -            @Override
   1.468 -            @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
   1.469 -            public boolean equals(Object obj) {
   1.470 -                if (set != null) {
   1.471 -                    cnt[0]++;
   1.472 -                    pl.setLookups(set);
   1.473 -                }
   1.474 -                return super.equals(obj);
   1.475 -            }
   1.476 -
   1.477 -            @Override
   1.478 -            public int hashCode() {
   1.479 -                int hash = 3;
   1.480 -                return hash;
   1.481 -            }
   1.482 -        }
   1.483 -
   1.484 -        Result<String> res = pl.lookupResult(String.class);
   1.485 -        assertEquals(Collections.EMPTY_LIST, res.allItems());
   1.486 -        
   1.487 -        L[] old = { new L("A"), new L("B") };
   1.488 -        L[] now = { new L("C") };
   1.489 -        
   1.490 -        pl.setLookups(old);
   1.491 -        cnt[0] = 0;
   1.492 -        
   1.493 -        old[0].set = new L[0];
   1.494 -        pl.setLookups(now);
   1.495 -
   1.496 -        assertEquals("No call to equals", 0, cnt[0]);
   1.497 -        
   1.498 -        assertEquals("Still assigned to C", Collections.singletonList("C"), res.allInstances());
   1.499 -    }
   1.500 -    
   1.501 -    public void testArrayIndexWithAddRemoveListenerAsInIssue119292() throws Exception {
   1.502 -        final ProxyLookup pl = new ProxyLookup();
   1.503 -        final int[] cnt = { 0 };
   1.504 -        
   1.505 -        class L extends Lookup {
   1.506 -            L[] set;
   1.507 -            Lookup l;
   1.508 -            
   1.509 -            public L(String s) {
   1.510 -                l = Lookups.singleton(s);
   1.511 -            }
   1.512 -            
   1.513 -            @Override
   1.514 -            public <T> T lookup(Class<T> clazz) {
   1.515 -                return l.lookup(clazz);
   1.516 -            }
   1.517 -
   1.518 -            @Override
   1.519 -            public <T> Result<T> lookup(Template<T> template) {
   1.520 -                Result<T> r = l.lookup(template);
   1.521 -                return new R<T>(r);
   1.522 -            }
   1.523 -
   1.524 -            final class R<T> extends Result<T> {
   1.525 -                private Result<T> delegate;
   1.526 -
   1.527 -                public R(Result<T> delegate) {
   1.528 -                    this.delegate = delegate;
   1.529 -                }
   1.530 -                
   1.531 -                @Override
   1.532 -                public void addLookupListener(LookupListener l) {
   1.533 -                    cnt[0]++;
   1.534 -                    if (set != null) {
   1.535 -                        pl.setLookups(set);
   1.536 -                    }
   1.537 -                    delegate.addLookupListener(l);
   1.538 -                }
   1.539 -
   1.540 -                @Override
   1.541 -                public void removeLookupListener(LookupListener l) {
   1.542 -                    cnt[0]++;
   1.543 -                    if (set != null) {
   1.544 -                        pl.setLookups(set);
   1.545 -                    }
   1.546 -                    delegate.removeLookupListener(l);
   1.547 -                }
   1.548 -
   1.549 -                @Override
   1.550 -                public Collection<? extends T> allInstances() {
   1.551 -                    return delegate.allInstances();
   1.552 -                }
   1.553 -            }
   1.554 -        }
   1.555 -
   1.556 -        Result<String> res = pl.lookupResult(String.class);
   1.557 -        assertEquals(Collections.EMPTY_LIST, res.allItems());
   1.558 -        
   1.559 -        L[] old = { new L("A"), new L("B") };
   1.560 -        L[] now = { new L("C") };
   1.561 -        
   1.562 -        pl.setLookups(old);
   1.563 -        cnt[0] = 0;
   1.564 -        
   1.565 -        old[0].set = new L[0];
   1.566 -        pl.setLookups(now);
   1.567 -
   1.568 -        if (cnt[0] == 0) {
   1.569 -            fail("There should be calls to listeners");
   1.570 -        }
   1.571 -        
   1.572 -        assertEquals("C is overriden from removeLookupListener", Collections.emptyList(), res.allInstances());
   1.573 -    }
   1.574 -    
   1.575 -    
   1.576 -    public void testArrayIndexWithSetLookupAsInIssue123679() throws Exception {
   1.577 -        final ProxyLookup pl = new ProxyLookup();
   1.578 -        final int[] cnt = { 0 };
   1.579 -        
   1.580 -        class L extends Lookup {
   1.581 -            L[] set;
   1.582 -            Lookup l;
   1.583 -            Collection<? extends Serializable> res;
   1.584 -            
   1.585 -            public L(String s) {
   1.586 -                l = Lookups.singleton(s);
   1.587 -            }
   1.588 -            
   1.589 -            @Override
   1.590 -            public <T> T lookup(Class<T> clazz) {
   1.591 -                return l.lookup(clazz);
   1.592 -            }
   1.593 -
   1.594 -            @Override
   1.595 -            public <T> Result<T> lookup(Template<T> template) {
   1.596 -                cnt[0]++;
   1.597 -                if (set != null) {
   1.598 -                    pl.setLookups(set);
   1.599 -                    res = pl.lookupAll(Serializable.class);
   1.600 -                }
   1.601 -                Result<T> r = l.lookup(template);
   1.602 -                return r;
   1.603 -            }
   1.604 -        }
   1.605 -
   1.606 -        L[] now = { new L("A"), new L("B") };
   1.607 -        L[] old = { new L("C") };
   1.608 -        pl.setLookups(old);
   1.609 -        old[0].set = now;
   1.610 -        
   1.611 -        Result<String> res = pl.lookupResult(String.class);
   1.612 -        assertEquals("New items visible", 2, res.allItems().size());
   1.613 -        
   1.614 -        
   1.615 -        pl.setLookups(new L("X"), new L("Y"), new L("Z"));
   1.616 -    }
   1.617 -    
   1.618 -    public void testDuplicatedLookupArrayIndexWithSetLookupAsInIssue123679() throws Exception {
   1.619 -        final ProxyLookup pl = new ProxyLookup();
   1.620 -        final int[] cnt = { 0 };
   1.621 -        
   1.622 -        class L extends Lookup {
   1.623 -            L[] set;
   1.624 -            Lookup l;
   1.625 -            Collection<? extends Serializable> res;
   1.626 -            
   1.627 -            public L(String s) {
   1.628 -                l = Lookups.singleton(s);
   1.629 -            }
   1.630 -            
   1.631 -            @Override
   1.632 -            public <T> T lookup(Class<T> clazz) {
   1.633 -                return l.lookup(clazz);
   1.634 -            }
   1.635 -
   1.636 -            @Override
   1.637 -            public <T> Result<T> lookup(Template<T> template) {
   1.638 -                cnt[0]++;
   1.639 -                if (set != null) {
   1.640 -                    pl.setLookups(set);
   1.641 -                    res = pl.lookupAll(Serializable.class);
   1.642 -                }
   1.643 -                Result<T> r = l.lookup(template);
   1.644 -                return r;
   1.645 -            }
   1.646 -        }
   1.647 -
   1.648 -        L dupl = new L("A");
   1.649 -        L[] now = { dupl };
   1.650 -        L[] old = { new L("C") };
   1.651 -        pl.setLookups(old);
   1.652 -        old[0].set = now;
   1.653 -        
   1.654 -        Result<String> res = pl.lookupResult(String.class);
   1.655 -        assertEquals("New items visible", 1, res.allItems().size());
   1.656 -        
   1.657 -        
   1.658 -        pl.setLookups(old);
   1.659 -    }
   1.660 -}