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