1.1 --- a/lookup/src/test/java/org/openide/util/lookup/AbstractLookupBaseHid.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,2088 +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.ByteArrayInputStream;
1.48 -import java.io.ByteArrayOutputStream;
1.49 -import java.io.ObjectInputStream;
1.50 -import java.io.ObjectOutputStream;
1.51 -import java.io.Serializable;
1.52 -import java.lang.ref.WeakReference;
1.53 -import java.lang.ref.Reference;
1.54 -import java.util.ArrayList;
1.55 -import java.util.Arrays;
1.56 -import java.util.Collection;
1.57 -import java.util.Collections;
1.58 -import java.util.Iterator;
1.59 -import java.util.LinkedList;
1.60 -import java.util.List;
1.61 -import java.util.concurrent.Executors;
1.62 -import java.util.concurrent.TimeUnit;
1.63 -import javax.swing.ActionMap;
1.64 -import javax.swing.InputMap;
1.65 -import org.netbeans.junit.NbTestCase;
1.66 -import org.openide.util.Lookup;
1.67 -import org.openide.util.Lookup.Template;
1.68 -import org.openide.util.LookupEvent;
1.69 -import org.openide.util.LookupListener;
1.70 -
1.71 -@SuppressWarnings("unchecked") // XXX ought to be corrected, just a lot of them
1.72 -public class AbstractLookupBaseHid extends NbTestCase {
1.73 - private static AbstractLookupBaseHid running;
1.74 -
1.75 - /** instance content to work with */
1.76 - InstanceContent ic;
1.77 - /** the lookup to work on */
1.78 - protected Lookup instanceLookup;
1.79 - /** the lookup created to work with */
1.80 - private Lookup lookup;
1.81 - /** implementation of methods that can influence the behaviour */
1.82 - Impl impl;
1.83 -
1.84 - protected AbstractLookupBaseHid(String testName, Impl impl) {
1.85 - super(testName);
1.86 - if (impl == null && (this instanceof Impl)) {
1.87 - impl = (Impl)this;
1.88 - }
1.89 - this.impl = impl;
1.90 - }
1.91 -
1.92 - protected @Override void setUp() {
1.93 - this.ic = new InstanceContent ();
1.94 -
1.95 - beforeActualTest(getName());
1.96 -
1.97 - this.instanceLookup = createInstancesLookup (ic);
1.98 - this.lookup = createLookup (instanceLookup);
1.99 - running = this;
1.100 - }
1.101 -
1.102 - protected @Override void tearDown() {
1.103 - running = null;
1.104 - }
1.105 -
1.106 - /** The methods to influence test behaviour */
1.107 - public static interface Impl {
1.108 - /** Creates the initial abstract lookup.
1.109 - */
1.110 - public Lookup createInstancesLookup (InstanceContent ic);
1.111 - /** Creates an lookup for given lookup. This class just returns
1.112 - * the object passed in, but subclasses can be different.
1.113 - * @param lookup in lookup
1.114 - * @return a lookup to use
1.115 - */
1.116 - public Lookup createLookup (Lookup lookup);
1.117 -
1.118 - /** If the impl has any caches that would prevent the system
1.119 - * to not garbage collect correctly, then clear them now.
1.120 - */
1.121 - public void clearCaches ();
1.122 - }
1.123 -
1.124 - private Lookup createInstancesLookup (InstanceContent ic) {
1.125 - return impl.createInstancesLookup (ic);
1.126 - }
1.127 -
1.128 - private Lookup createLookup (Lookup lookup) {
1.129 - return impl.createLookup (lookup);
1.130 - }
1.131 -
1.132 - /** instances that we register */
1.133 - private static Object[] INSTANCES = new Object[] {
1.134 - new Integer (10),
1.135 - new Object ()
1.136 - };
1.137 -
1.138 - /** Test if first is really first.
1.139 - */
1.140 - public void testFirst () {
1.141 - Integer i1 = 1;
1.142 - Integer i2 = 2;
1.143 -
1.144 - ic.add (i1);
1.145 - ic.add (i2);
1.146 -
1.147 - Integer found = lookup.lookup(Integer.class);
1.148 - if (found != i1) {
1.149 - fail ("First object is not first: " + found + " != " + i1);
1.150 - }
1.151 -
1.152 - List<Integer> list = new ArrayList<Integer>();
1.153 - list.add (i2);
1.154 - list.add (i1);
1.155 - ic.set (list, null);
1.156 -
1.157 - found = lookup.lookup (Integer.class);
1.158 - if (found != i2) {
1.159 - fail ("Second object is not first after reorder: " + found + " != " + i2);
1.160 - }
1.161 -
1.162 - }
1.163 -
1.164 - public void testToString() {
1.165 - String txt = lookup.toString();
1.166 - assertNotNull("Something is there", txt);
1.167 - assertTrue("Something2: " + txt, txt.length() > 0);
1.168 - }
1.169 -
1.170 -
1.171 - /** Tests ordering of items in the lookup.
1.172 - */
1.173 - public void testOrder () {
1.174 - addInstances (INSTANCES);
1.175 -
1.176 - if (INSTANCES[0] != lookup.lookup (INSTANCES[0].getClass ())) {
1.177 - fail ("First object in intances not found");
1.178 - }
1.179 -
1.180 - Iterator<?> all = lookup.lookupAll(Object.class).iterator();
1.181 - checkIterator ("Difference between instances added and found", all, Arrays.asList (INSTANCES));
1.182 - }
1.183 -
1.184 - /** Checks the reorder of items in lookup reflects the result.
1.185 - * Testing both classes and interfaces, because they are often treated
1.186 - * especially.
1.187 - */
1.188 - public void testReorder () {
1.189 - String s1 = "s2";
1.190 - String s2 = "s1";
1.191 - Runnable r1 = new Runnable () {
1.192 - public void run () {}
1.193 - };
1.194 - Runnable r2 = new Runnable () {
1.195 - public void run () {}
1.196 - };
1.197 - List<Object> l = new ArrayList<Object>();
1.198 -
1.199 - l.add (s1);
1.200 - l.add (s2);
1.201 - l.add (r1);
1.202 - l.add (r2);
1.203 - ic.set (l, null);
1.204 -
1.205 - assertEquals ("s1 is found", s1, lookup.lookup (String.class));
1.206 - assertEquals ("r1 is found", r1, lookup.lookup (Runnable.class));
1.207 -
1.208 - Collections.reverse (l);
1.209 -
1.210 - ic.set (l, null);
1.211 -
1.212 - assertEquals ("s2 is found", s2, lookup.lookup (String.class));
1.213 - assertEquals ("r2 is found", r2, lookup.lookup (Runnable.class));
1.214 - }
1.215 -
1.216 - /** Tries to set empty collection to the lookup.
1.217 - */
1.218 - public void testSetEmpty () {
1.219 - ic.add ("A serializable string");
1.220 - lookup.lookup (Serializable.class);
1.221 -
1.222 - ic.set (Collections.emptyList(), null);
1.223 - }
1.224 -
1.225 - /** Tests a more complex reorder on nodes.
1.226 - */
1.227 - public void testComplexReorder () {
1.228 - Integer i1 = 1;
1.229 - Long i2 = 2L;
1.230 -
1.231 - List<Object> l = new ArrayList<Object>();
1.232 - l.add (i1);
1.233 - l.add (i2);
1.234 - ic.set (l, null);
1.235 -
1.236 - assertEquals ("Find integer", i1, lookup.lookup (Integer.class));
1.237 - assertEquals ("Find long", i2, lookup.lookup (Long.class));
1.238 - assertEquals ("Find number", i1, lookup.lookup (Number.class));
1.239 -
1.240 - Collections.reverse (l);
1.241 -
1.242 - ic.set (l, null);
1.243 -
1.244 - assertEquals ("Find integer", i1, lookup.lookup (Integer.class));
1.245 - assertEquals ("Find long", i2, lookup.lookup (Long.class));
1.246 - assertEquals ("Find number", i2, lookup.lookup (Number.class));
1.247 - }
1.248 -
1.249 - /** Checks whether setPairs keeps the order.
1.250 - */
1.251 - public void testSetPairs () {
1.252 - // test setPairs method
1.253 - List<Object> li = new ArrayList<Object>();
1.254 - li.addAll (Arrays.asList (INSTANCES));
1.255 - ic.set (li, null);
1.256 -
1.257 - Lookup.Result<Object> res = lookup.lookupResult(Object.class);
1.258 - Iterator<?> all = res.allInstances().iterator();
1.259 - checkIterator ("Original order not kept", all, li);
1.260 -
1.261 - // reverse the order
1.262 - Collections.reverse (li);
1.263 -
1.264 - // change the pairs
1.265 - LL listener = new LL (res);
1.266 - res.addLookupListener (listener);
1.267 - ic.set (li, null);
1.268 - if (listener.getCount () != 1) {
1.269 - fail ("Result has not changed even we set reversed order");
1.270 - }
1.271 -
1.272 - all = res.allInstances ().iterator ();
1.273 - checkIterator ("Reversed order not kept", all, li);
1.274 - }
1.275 -
1.276 - /** Checks whether setPairs fires correct events.
1.277 - */
1.278 - public void testSetPairsFire () {
1.279 - // test setPairs method
1.280 - List<Object> li = new ArrayList<Object>();
1.281 - li.addAll (Arrays.asList (INSTANCES));
1.282 - ic.set (li, null);
1.283 -
1.284 - Lookup.Result<Integer> res = lookup.lookupResult(Integer.class);
1.285 - Iterator<?> all = res.allInstances().iterator();
1.286 - checkIterator ("Integer is not there", all, Collections.nCopies (1, INSTANCES[0]));
1.287 -
1.288 - // change the pairs
1.289 - LL listener = new LL (res);
1.290 - res.addLookupListener (listener);
1.291 -
1.292 - List<Object> l2 = new ArrayList<Object>(li);
1.293 - l2.remove (INSTANCES[0]);
1.294 - ic.set (l2, null);
1.295 -
1.296 - all = lookup.lookupAll(Object.class).iterator();
1.297 - checkIterator ("The removed integer is not noticed", all, l2);
1.298 -
1.299 - if (listener.getCount () != 1) {
1.300 - fail ("Nothing has not been fired");
1.301 - }
1.302 - }
1.303 -
1.304 - /** Checks whether set pairs does not fire when they should not.
1.305 - */
1.306 - public void testSetPairsDoesNotFire () {
1.307 - Object tmp = new Object ();
1.308 -
1.309 - List<Object> li = new ArrayList<Object>();
1.310 - li.add (tmp);
1.311 - li.addAll (Arrays.asList (INSTANCES));
1.312 - ic.set (li, null);
1.313 -
1.314 - Lookup.Result<Integer> res = lookup.lookupResult(Integer.class);
1.315 - Iterator<?> all = res.allInstances ().iterator ();
1.316 - checkIterator ("Integer is not there", all, Collections.nCopies (1, INSTANCES[0]));
1.317 -
1.318 - // change the pairs
1.319 - LL listener = new LL (res);
1.320 - res.addLookupListener (listener);
1.321 -
1.322 - List<Object> l2 = new ArrayList<Object>(li);
1.323 - l2.remove (tmp);
1.324 - ic.set (l2, null);
1.325 -
1.326 - all = lookup.lookupAll(Object.class).iterator();
1.327 - checkIterator ("The removed integer is not noticed", all, l2);
1.328 -
1.329 - if (listener.getCount () != 0) {
1.330 - fail ("Something has been fired");
1.331 - }
1.332 - }
1.333 -
1.334 - /** Test whether after registration it is possible to find registered objects
1.335 - *
1.336 - */
1.337 - public void testLookupAndAdd () throws Exception {
1.338 - addInstances (INSTANCES);
1.339 -
1.340 - for (int i = 0; i < INSTANCES.length; i++) {
1.341 - Object obj = INSTANCES[i];
1.342 - findAll (lookup, obj.getClass (), true);
1.343 - }
1.344 - }
1.345 -
1.346 - /** Tries to find all classes and superclasses in the lookup.
1.347 - */
1.348 - private void findAll(Lookup lookup, Class<?> clazz, boolean shouldBeThere) {
1.349 - if (clazz == null) return;
1.350 -
1.351 - Object found = lookup.lookup (clazz);
1.352 - if (found == null) {
1.353 - if (shouldBeThere) {
1.354 - // should find at either instance or something else, but must
1.355 - // find at least something
1.356 - fail ("Lookup (" + clazz.getName () + ") found nothing");
1.357 - }
1.358 - } else {
1.359 - if (!shouldBeThere) {
1.360 - // should find at either instance or something else, but must
1.361 - // find at least something
1.362 - fail ("Lookup (" + clazz.getName () + ") found " + found);
1.363 - }
1.364 - }
1.365 -
1.366 - Lookup.Result<?> res = lookup.lookupResult(clazz);
1.367 - Collection<?> collection = res.allInstances();
1.368 -
1.369 - for (int i = 0; i < INSTANCES.length; i++) {
1.370 - boolean isSubclass = clazz.isInstance (INSTANCES[i]);
1.371 - boolean isThere = collection.contains (INSTANCES[i]);
1.372 -
1.373 - if (isSubclass != isThere) {
1.374 - // a problem found
1.375 - // should find at either instance or something else, but must
1.376 - // find at least something
1.377 - fail ("Lookup.Result (" + clazz.getName () + ") for " + INSTANCES[i] + " is subclass: " + isSubclass + " isThere: " + isThere);
1.378 - }
1.379 - }
1.380 -
1.381 - // go on for superclasses
1.382 -
1.383 - findAll (lookup, clazz.getSuperclass (), shouldBeThere);
1.384 -
1.385 - Class[] ies = clazz.getInterfaces ();
1.386 - for (int i = 0; i < ies.length; i++) {
1.387 - findAll (lookup, ies[i], shouldBeThere);
1.388 - }
1.389 - }
1.390 -
1.391 - /** Test if it is possible to remove a registered object. */
1.392 - public void testRemoveRegisteredObject() {
1.393 - Integer inst = new Integer(10);
1.394 -
1.395 - ic.add(inst);
1.396 - if (lookup.lookup(inst.getClass()) == null) {
1.397 - // should find an instance
1.398 - fail("Lookup (" + inst.getClass().getName () + ") found nothing");
1.399 - }
1.400 -
1.401 - ic.remove(inst);
1.402 - if (lookup.lookup(inst.getClass()) != null) {
1.403 - // should NOT find an instance
1.404 - fail("Lookup (" + inst.getClass().getName () +
1.405 - ") found an instance after remove operation");
1.406 - }
1.407 - }
1.408 -
1.409 - public void testCanReturnReallyStrangeResults () throws Exception {
1.410 - class QueryingPair extends AbstractLookup.Pair<Object> {
1.411 - private Integer i = 434;
1.412 -
1.413 - //
1.414 - // do the test
1.415 - //
1.416 -
1.417 - public void doTest () throws Exception {
1.418 - ic.add (i);
1.419 - ic.addPair (this);
1.420 -
1.421 - Object found = lookup.lookup (QueryingPair.class);
1.422 - assertEquals ("This object is found", this, found);
1.423 - }
1.424 -
1.425 -
1.426 - //
1.427 - // Implementation of pair
1.428 - //
1.429 -
1.430 - public String getId() {
1.431 - return getType ().toString();
1.432 - }
1.433 -
1.434 - public String getDisplayName() {
1.435 - return getId ();
1.436 - }
1.437 -
1.438 - public Class<?> getType() {
1.439 - return getClass ();
1.440 - }
1.441 -
1.442 - protected boolean creatorOf(Object obj) {
1.443 - return obj == this;
1.444 - }
1.445 -
1.446 - protected boolean instanceOf(Class<?> c) {
1.447 - assertEquals ("Integer found or exception is thrown", i, lookup.lookup (Integer.class));
1.448 - return c.isAssignableFrom(getType ());
1.449 - }
1.450 -
1.451 - public Object getInstance() {
1.452 - return this;
1.453 - }
1.454 -
1.455 -
1.456 - }
1.457 -
1.458 -
1.459 - QueryingPair qp = new QueryingPair ();
1.460 - qp.doTest ();
1.461 - }
1.462 -
1.463 - /** Test of firing events. */
1.464 - public void testLookupListener() {
1.465 - Object inst = 10;
1.466 - Lookup.Result<?> res = lookup.lookupResult(inst.getClass());
1.467 - res.allInstances ();
1.468 -
1.469 - LL listener = new LL(res);
1.470 - res.addLookupListener(listener);
1.471 -
1.472 - ic.add(inst);
1.473 - if (listener.getCount() == 0) {
1.474 - fail("None event fired during NbLookup.addPair()");
1.475 - }
1.476 -
1.477 - ic.remove(inst);
1.478 - if (listener.getCount() == 0) {
1.479 - fail("None event fired during NbLookup.removePair()");
1.480 - }
1.481 -
1.482 - ic.add(inst);
1.483 - if (listener.getCount() == 0) {
1.484 - fail("None event fired during second NbLookup.addPair()");
1.485 - }
1.486 -
1.487 - ic.remove(inst);
1.488 - if (listener.getCount() == 0) {
1.489 - fail("None event fired during second NbLookup.removePair()");
1.490 - }
1.491 - }
1.492 -
1.493 - /** Testing identity of the lookup.
1.494 - */
1.495 - public void testId () {
1.496 - Lookup.Template<?> templ;
1.497 - int cnt;
1.498 -
1.499 - addInstances (INSTANCES);
1.500 -
1.501 - Lookup.Result<?> res = lookup.lookupResult(Object.class);
1.502 - for (AbstractLookup.Item<?> item : res.allItems()) {
1.503 -
1.504 - templ = new Lookup.Template<Object>(null, item.getId(), null);
1.505 - cnt = lookup.lookup (templ).allInstances ().size ();
1.506 - if (cnt != 1) {
1.507 - fail ("Identity lookup failed. Instances = " + cnt);
1.508 - }
1.509 -
1.510 - templ = makeTemplate(item.getType(), item.getId());
1.511 - cnt = lookup.lookup (templ).allInstances ().size ();
1.512 - if (cnt != 1) {
1.513 - fail ("Identity lookup with type failed. Instances = " + cnt);
1.514 - }
1.515 -
1.516 - templ = makeTemplate(this.getClass(), item.getId());
1.517 - cnt = lookup.lookup (templ).allInstances ().size ();
1.518 - if (cnt != 0) {
1.519 - fail ("Identity lookup with wrong type failed. Instances = " + cnt);
1.520 - }
1.521 -
1.522 - templ = new Lookup.Template<Object>(null, null, item.getInstance());
1.523 - cnt = lookup.lookup (templ).allInstances ().size ();
1.524 - if (cnt != 1) {
1.525 - fail ("Instance lookup failed. Instances = " + cnt);
1.526 - }
1.527 -
1.528 - templ = new Lookup.Template<Object>(null, item.getId(), item.getInstance());
1.529 - cnt = lookup.lookup (templ).allInstances ().size ();
1.530 - if (cnt != 1) {
1.531 - fail ("Instance & identity lookup failed. Instances = " + cnt);
1.532 - }
1.533 -
1.534 - }
1.535 - }
1.536 - private static <T> Lookup.Template<T> makeTemplate(Class<T> clazz, String id) { // captures type parameter
1.537 - return new Lookup.Template<T>(clazz, id, null);
1.538 - }
1.539 -
1.540 - /** Tests adding and removing.
1.541 - */
1.542 - public void testAddAndRemove () throws Exception {
1.543 - Object map = new javax.swing.ActionMap ();
1.544 - LL ll = new LL ();
1.545 -
1.546 - Lookup.Result<?> res = lookup.lookupResult(map.getClass());
1.547 - res.allItems();
1.548 - res.addLookupListener (ll);
1.549 - ll.source = res;
1.550 -
1.551 - ic.add (map);
1.552 -
1.553 - assertEquals ("First change when adding", ll.getCount (), 1);
1.554 -
1.555 - ic.remove (map);
1.556 -
1.557 - assertEquals ("Second when removing", ll.getCount (), 1);
1.558 -
1.559 - ic.add (map);
1.560 -
1.561 - assertEquals ("Third when readding", ll.getCount (), 1);
1.562 -
1.563 - ic.remove (map);
1.564 -
1.565 - assertEquals ("Forth when reremoving", ll.getCount (), 1);
1.566 -
1.567 - }
1.568 -
1.569 - /** Will a class garbage collect even it is registered in lookup.
1.570 - */
1.571 - public void testGarbageCollect () throws Exception {
1.572 - ClassLoader l = new CL ();
1.573 - Class<?> c = l.loadClass(Garbage.class.getName());
1.574 - Reference<?> ref = new WeakReference<Object>(c);
1.575 -
1.576 - lookup.lookup (c);
1.577 -
1.578 - // now test garbage collection
1.579 - c = null;
1.580 - l = null;
1.581 - impl.clearCaches ();
1.582 - assertGC ("The classloader has not been garbage collected!", ref);
1.583 - }
1.584 -
1.585 - /** Items are the same as results.
1.586 - */
1.587 - public void testItemsAndIntances () {
1.588 - addInstances (INSTANCES);
1.589 -
1.590 - Lookup.Result<Object> r = lookup.lookupResult(Object.class);
1.591 - Collection<? extends Lookup.Item<?>> items = r.allItems();
1.592 - Collection<?> insts = r.allInstances();
1.593 -
1.594 - if (items.size () != insts.size ()) {
1.595 - fail ("Different size of sets");
1.596 - }
1.597 -
1.598 - for (Lookup.Item<?> item : items) {
1.599 - if (!insts.contains (item.getInstance ())) {
1.600 - fail ("Intance " + item.getInstance () + " is missing in " + insts);
1.601 - }
1.602 - }
1.603 - }
1.604 -
1.605 - /** Checks search for interface.
1.606 - */
1.607 - public void testSearchForInterface () {
1.608 - Lookup.Template<Serializable> t = new Lookup.Template<Serializable>(Serializable.class, null, null);
1.609 -
1.610 - assertNull("Nothing to find", lookup.lookupItem (t));
1.611 -
1.612 - Serializable s = new Serializable () {};
1.613 - ic.add (s);
1.614 -
1.615 - Lookup.Item item = lookup.lookupItem (t);
1.616 - assertNotNull ("Something found", item);
1.617 - }
1.618 -
1.619 - /** Test to add broken item if it incorrectly answers instanceOf questions.
1.620 - */
1.621 - public void testIncorectInstanceOf40364 () {
1.622 - final Long sharedLong = new Long (0);
1.623 -
1.624 - class P extends AbstractLookup.Pair<Object> {
1.625 - public boolean isLong;
1.626 -
1.627 - P (boolean b) {
1.628 - isLong = b;
1.629 - }
1.630 -
1.631 - protected boolean creatorOf (Object obj) {
1.632 - return obj == sharedLong;
1.633 - }
1.634 -
1.635 - public String getDisplayName () {
1.636 - return "";
1.637 - }
1.638 -
1.639 - public String getId () {
1.640 - return "";
1.641 - }
1.642 -
1.643 - public Object getInstance () {
1.644 - return sharedLong;
1.645 - }
1.646 -
1.647 - public Class<?> getType() {
1.648 - return isLong ? Long.class : Number.class;
1.649 - }
1.650 -
1.651 - protected boolean instanceOf(Class<?> c) {
1.652 - return c.isAssignableFrom (getType ());
1.653 - }
1.654 -
1.655 - public @Override int hashCode() {
1.656 - return getClass ().hashCode ();
1.657 - }
1.658 -
1.659 - public @Override boolean equals(Object obj) {
1.660 - return obj != null && getClass ().equals (obj.getClass ());
1.661 - }
1.662 - }
1.663 -
1.664 - // to create the right structure in the lookup
1.665 - lookup.lookup (Object.class);
1.666 - lookup.lookup (Long.class);
1.667 - lookup.lookup (Number.class);
1.668 -
1.669 - P lng1 = new P (true);
1.670 - ic.addPair (lng1);
1.671 -
1.672 - P lng2 = new P (false);
1.673 - ic.setPairs (Collections.singleton (lng2));
1.674 -
1.675 - Collection<? extends Lookup.Item<?>> res = lookup.lookupResult(Object.class).allItems();
1.676 - assertEquals ("Just one pair", 1, res.size ());
1.677 - }
1.678 -
1.679 - public void testAbsolutelyCrazyWayToSimulateIssue48590ByChangingTheBehaviourOfEqualOnTheFly () throws Exception {
1.680 - class X implements TestInterfaceInheritanceA, TestInterfaceInheritanceB {
1.681 - }
1.682 - final X shared = new X ();
1.683 -
1.684 - class P extends AbstractLookup.Pair<Object> {
1.685 - public int howLong;
1.686 -
1.687 - P (int b) {
1.688 - howLong = b;
1.689 - }
1.690 -
1.691 - protected boolean creatorOf (Object obj) {
1.692 - return obj == shared;
1.693 - }
1.694 -
1.695 - public String getDisplayName () {
1.696 - return "";
1.697 - }
1.698 -
1.699 - public String getId () {
1.700 - return "";
1.701 - }
1.702 -
1.703 - public Object getInstance () {
1.704 - return shared;
1.705 - }
1.706 -
1.707 - public Class<?> getType() {
1.708 - return howLong == 0 ? TestInterfaceInheritanceB.class : TestInterfaceInheritanceA.class;
1.709 - }
1.710 -
1.711 - protected boolean instanceOf(Class<?> c) {
1.712 - return c.isAssignableFrom (getType ());
1.713 - }
1.714 -
1.715 - public @Override int hashCode() {
1.716 - return getClass ().hashCode ();
1.717 - }
1.718 -
1.719 - public @Override boolean equals(Object obj) {
1.720 - if (obj instanceof P) {
1.721 - P p = (P)obj;
1.722 - if (this.howLong > 0) {
1.723 - this.howLong--;
1.724 - return false;
1.725 - }
1.726 - if (p.howLong > 0) {
1.727 - p.howLong--;
1.728 - return false;
1.729 - }
1.730 - return getClass ().equals (p.getClass ());
1.731 - }
1.732 - return false;
1.733 - }
1.734 - }
1.735 -
1.736 - // to create the right structure in the lookup
1.737 - Lookup.Result<?> a = lookup.lookupResult(TestInterfaceInheritanceA.class);
1.738 - Lookup.Result<?> b = lookup.lookupResult(TestInterfaceInheritanceB.class);
1.739 -
1.740 - P lng1 = new P (0);
1.741 - ic.addPair (lng1);
1.742 -
1.743 - assertEquals ("One in a", 1, a.allItems ().size ());
1.744 - assertEquals ("One in b", 1, b.allItems ().size ());
1.745 -
1.746 - P lng2 = new P (1);
1.747 -
1.748 -
1.749 - /* Following call used to generate this exception:
1.750 - java.lang.IllegalStateException: Duplicate pair in treePair1: pair2: index1: 0 index2: 0 item1: org.openide.util.lookup.AbstractLookupBaseHid$1X@1a457b6 item2: org.openide.util.lookup.AbstractLookupBaseHid$1X@1a457b6 id1: 7a78d3 id2: 929206
1.751 - at org.openide.util.lookup.ALPairComparator.compare(ALPairComparator.java:52)
1.752 - at java.util.Arrays.mergeSort(Arrays.java:1284)
1.753 - at java.util.Arrays.sort(Arrays.java:1223)
1.754 - at java.util.Collections.sort(Collections.java:159)
1.755 - at org.openide.util.lookup.InheritanceTree.retainAllInterface(InheritanceTree.java:753)
1.756 - at org.openide.util.lookup.InheritanceTree.retainAll(InheritanceTree.java:183)
1.757 - at org.openide.util.lookup.DelegatingStorage.retainAll(DelegatingStorage.java:83)
1.758 - at org.openide.util.lookup.AbstractLookup.setPairsAndCollectListeners(AbstractLookup.java:238)
1.759 - at org.openide.util.lookup.AbstractLookup.setPairs(AbstractLookup.java:203)
1.760 - at org.openide.util.lookup.AbstractLookup$Content.setPairs(AbstractLookup.java:885)
1.761 - at org.openide.util.lookup.AbstractLookupBaseHid.testAbsolutelyCrazyWayToSimulateIssue48590ByChangingTheBehaviourOfEqualOnTheFly(AbstractLookupBaseHid.java:696)
1.762 - at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
1.763 - at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
1.764 - at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
1.765 - at org.netbeans.junit.NbTestCase.run(NbTestCase.java:119)
1.766 - */
1.767 - ic.setPairs (Collections.singleton (lng2));
1.768 -
1.769 -
1.770 - }
1.771 -
1.772 - public void testInstancesArePreservedFoundWhenFixing48590 () throws Exception {
1.773 - class X implements Runnable, Serializable {
1.774 - public void run () {
1.775 -
1.776 - }
1.777 -
1.778 - public void assertOnlyMe (String msg, Lookup.Result<?> res) {
1.779 - Collection<?> col = res.allInstances();
1.780 - assertEquals (msg + " just one", 1, col.size ());
1.781 - assertSame (msg + " and it is me", this, col.iterator ().next ());
1.782 - }
1.783 - }
1.784 -
1.785 - Lookup.Result<?> runnable = lookup.lookupResult(Runnable.class);
1.786 - Lookup.Result<?> serial = lookup.lookupResult(Serializable.class);
1.787 -
1.788 -
1.789 - X x = new X ();
1.790 - ic.add (x);
1.791 -
1.792 -
1.793 - x.assertOnlyMe ("x implements it (1)", runnable);
1.794 - x.assertOnlyMe ("x implements it (2)", serial);
1.795 -
1.796 - ic.set (Collections.singleton (x), null);
1.797 -
1.798 - x.assertOnlyMe ("x implements it (3)", runnable);
1.799 - x.assertOnlyMe ("x implements it (4)", serial);
1.800 - }
1.801 -
1.802 - /** Testing lookup of inherited classes. */
1.803 - public void testInheritance() {
1.804 - class A {}
1.805 - class B extends A implements java.rmi.Remote {}
1.806 - class BB extends B {}
1.807 - class C extends A implements java.rmi.Remote {}
1.808 - class D extends A {}
1.809 -
1.810 - A[] types = {new B(), new BB(), new C(), new D()};
1.811 -
1.812 - for (int i = 0; i < types.length; i++) {
1.813 - ic.add(types[i]);
1.814 - if (lookup.lookup(types[i].getClass()) == null) {
1.815 - // should find an instance
1.816 - fail("Lookup (" + types[i].getClass().getName () + ") found nothing");
1.817 - }
1.818 - }
1.819 -
1.820 - int size1, size2;
1.821 -
1.822 - //interface query
1.823 - size1 = lookup.lookupAll(java.rmi.Remote.class).size();
1.824 - size2 = countInstances(types, java.rmi.Remote.class);
1.825 -
1.826 - if (size1 != size2) fail("Lookup with interface failed: " + size1 + " != " + size2);
1.827 -
1.828 - // superclass query
1.829 - size1 = lookup.lookupAll(A.class).size();
1.830 - size2 = countInstances(types, A.class);
1.831 -
1.832 - if (size1 != size2) fail("Lookup with superclass failed: " + size1 + " != " + size2);
1.833 - }
1.834 -
1.835 - /** Test interface inheritance.
1.836 - */
1.837 - public void testInterfaceInheritance() {
1.838 - TestInterfaceInheritanceA[] types = {
1.839 - new TestInterfaceInheritanceB() {},
1.840 - new TestInterfaceInheritanceBB() {},
1.841 - new TestInterfaceInheritanceC() {},
1.842 - new TestInterfaceInheritanceD() {}
1.843 - };
1.844 -
1.845 - for (int i = 0; i < types.length; i++) {
1.846 - ic.add(types[i]);
1.847 - if (lookup.lookup(types[i].getClass()) == null) {
1.848 - // should find an instance
1.849 - fail("Lookup (" + types[i].getClass().getName () + ") found nothing");
1.850 - }
1.851 - }
1.852 -
1.853 - int size1, size2;
1.854 -
1.855 - //interface query
1.856 - LL l = new LL ();
1.857 - Lookup.Result<?> res = lookup.lookupResult(java.rmi.Remote.class);
1.858 - l.source = res;
1.859 - size1 = res.allInstances().size();
1.860 - size2 = countInstances(types, java.rmi.Remote.class);
1.861 -
1.862 - if (size1 != size2) fail("Lookup with interface failed: " + size1 + " != " + size2);
1.863 -
1.864 - // superclass query
1.865 - size1 = lookup.lookupAll(TestInterfaceInheritanceA.class).size();
1.866 - size2 = countInstances(types, TestInterfaceInheritanceA.class);
1.867 -
1.868 - if (size1 != size2) fail("Lookup with superclass failed: " + size1 + " != " + size2);
1.869 -
1.870 - res.addLookupListener (l);
1.871 - ic.remove (types[0]);
1.872 -
1.873 - if (l.getCount () != 1) {
1.874 - fail ("No notification that a Remote is removed");
1.875 - }
1.876 - }
1.877 -
1.878 - /** Checks whether the AbstractLookup is guarded against modifications
1.879 - * while doing some kind of modification.
1.880 - */
1.881 - public void testModificationArePreventedWhenDoingModifications () throws Exception {
1.882 - BrokenPair broken = new BrokenPair (true, false);
1.883 - ic.addPair (broken);
1.884 -
1.885 - Lookup.Template<BrokenPair> templ = new Lookup.Template<BrokenPair>(BrokenPair.class);
1.886 - Lookup.Item<BrokenPair> item = lookup.lookupItem (templ);
1.887 - assertEquals ("Broken is found", broken, item);
1.888 - }
1.889 -
1.890 - public void testModificationArePreventedWhenDoingModificationsResult () throws Exception {
1.891 - BrokenPair broken = new BrokenPair (false, true);
1.892 - ic.addPair (broken);
1.893 -
1.894 - Lookup.Template<BrokenPair> templ = new Lookup.Template<BrokenPair>(BrokenPair.class);
1.895 -
1.896 - Collection<? extends BrokenPair> c = lookup.lookup (templ).allInstances();
1.897 - assertEquals ("One item", 1, c.size ());
1.898 - assertEquals ("Broken is found again", broken, c.iterator().next ());
1.899 - }
1.900 -
1.901 - public void testModificationArePreventedWhenDoingModificationsItemAndResult () throws Exception {
1.902 - BrokenPair broken = new BrokenPair (false, true);
1.903 - ic.addPair (broken);
1.904 -
1.905 - Lookup.Template<BrokenPair> templ = new Lookup.Template<BrokenPair>(BrokenPair.class);
1.906 - Lookup.Item<BrokenPair> item = lookup.lookupItem (templ);
1.907 - assertEquals ("Broken is found", broken, item);
1.908 -
1.909 - Collection<? extends BrokenPair> c = lookup.lookup(templ).allInstances();
1.910 - assertEquals ("One item", 1, c.size ());
1.911 - assertEquals ("Broken is found again", broken, c.iterator().next ());
1.912 - }
1.913 -
1.914 - public void testModificationArePreventedWhenDoingModificationsResultAndItem () throws Exception {
1.915 - BrokenPair broken = new BrokenPair (false, true);
1.916 - ic.addPair (broken);
1.917 -
1.918 - Lookup.Template<BrokenPair> templ = new Lookup.Template<BrokenPair>(BrokenPair.class);
1.919 - Collection<? extends BrokenPair> c = lookup.lookup(templ).allInstances();
1.920 - assertEquals ("One item", 1, c.size ());
1.921 - assertEquals ("Broken is found again", broken, c.iterator().next ());
1.922 -
1.923 - Object item = lookup.lookupItem (templ);
1.924 - assertEquals ("Broken is found", broken, item);
1.925 - }
1.926 -
1.927 - public void testAddALotOfPairsIntoTheLookupOneByOne () throws Exception {
1.928 - Lookup.Result<Integer> res = lookup.lookupResult(Integer.class);
1.929 - for (int i = 0; i < 1000; i++) {
1.930 - ic.add(i);
1.931 - }
1.932 - assertEquals (
1.933 - "there is the right count",
1.934 - 1000,
1.935 - res.allItems().size ()
1.936 - );
1.937 - }
1.938 -
1.939 - public void testAddALotOfPairsIntoTheLookup () throws Exception {
1.940 - List<Integer> arr = new ArrayList<Integer>();
1.941 - for (int i = 0; i < 1000; i++) {
1.942 - arr.add(i);
1.943 - }
1.944 - ic.set (arr, null);
1.945 -
1.946 - assertEquals (
1.947 - "there is the right count",
1.948 - 1000,
1.949 - lookup.lookupResult(Integer.class).allItems().size()
1.950 - );
1.951 - }
1.952 -
1.953 -
1.954 - public void testDoubleAddIssue35274 () throws Exception {
1.955 - class P extends AbstractLookup.Pair<Object> {
1.956 - protected boolean creatorOf(Object obj) { return false; }
1.957 - public String getDisplayName() { return ""; }
1.958 - public String getId() { return ""; }
1.959 - public Object getInstance() { return null; }
1.960 - public Class<?> getType() { return Object.class; }
1.961 - protected boolean instanceOf(Class<?> c) { return c.isAssignableFrom(getType ()); }
1.962 - public @Override int hashCode() {return getClass().hashCode();}
1.963 - public @Override boolean equals(Object obj) {return getClass() == obj.getClass();}
1.964 - }
1.965 -
1.966 - P p = new P ();
1.967 -
1.968 - ic.addPair (p);
1.969 - ic.addPair (p);
1.970 -
1.971 - Lookup.Result<Object> result = lookup.lookupResult(Object.class);
1.972 - Collection res = result.allItems ();
1.973 - assertEquals ("One item there", 1, res.size ());
1.974 - assertTrue ("It is the p", p == res.iterator ().next ());
1.975 -
1.976 - P p2 = new P ();
1.977 - ic.addPair (p2);
1.978 -
1.979 - Reference<?> ref = new WeakReference<Object>(result);
1.980 - result = null;
1.981 - assertGC ("The result can disappear", ref);
1.982 -
1.983 - impl.clearCaches ();
1.984 -
1.985 - result = lookup.lookupResult(Object.class);
1.986 - res = result.allItems ();
1.987 - assertEquals ("One item is still there", 1, res.size ());
1.988 - assertTrue ("But the p2 replaced p", p2 == res.iterator ().next ());
1.989 -
1.990 - }
1.991 -
1.992 - /** Test for proper serialization.
1.993 - */
1.994 - public void testSerializationSupport () throws Exception {
1.995 - doSerializationSupport (1);
1.996 - }
1.997 - public void testDoubleSerializationSupport () throws Exception {
1.998 - doSerializationSupport (2);
1.999 - }
1.1000 -
1.1001 - private void doSerializationSupport (int count) throws Exception {
1.1002 - if (lookup instanceof Serializable) {
1.1003 - ic.addPair (new SerialPair ("1"));
1.1004 - ic.addPair (new SerialPair ("2"));
1.1005 - ic.addPair (new SerialPair ("3"));
1.1006 -
1.1007 - Lookup l = (Lookup)reserialize(lookup);
1.1008 -
1.1009 - assertEquals ("Able to answer simple query", "1", l.lookup (String.class));
1.1010 -
1.1011 - assertEquals ("Three objects there", 3, l.lookup (new Lookup.Template (String.class)).allInstances().size ());
1.1012 -
1.1013 - while (count-- > 0) {
1.1014 - l = (Lookup)reserialize(l);
1.1015 - }
1.1016 -
1.1017 - assertEquals ("Able to answer simple query", "1", l.lookup (String.class));
1.1018 -
1.1019 - assertEquals ("Three objects there", 3, l.lookup (new Lookup.Template (String.class)).allInstances().size ());
1.1020 - }
1.1021 - }
1.1022 -
1.1023 - /** When a lookup with two different versions of the same class
1.1024 - * get's serialized, the results may be very bad.
1.1025 - */
1.1026 - public void testSerializationOfTwoClassesWithTheSameName () throws Exception {
1.1027 - if (lookup instanceof Serializable) {
1.1028 - doTwoSerializedClasses (false, false);
1.1029 - }
1.1030 - }
1.1031 - public void testSerializationOfTwoClassesWithTheSameNameButQueryBeforeSave () throws Exception {
1.1032 - if (lookup instanceof Serializable) {
1.1033 - doTwoSerializedClasses (true, false);
1.1034 - }
1.1035 - }
1.1036 - public void testSerializationOfTwoClassesWithTheSameNameWithBroken () throws Exception {
1.1037 - if (lookup instanceof Serializable) {
1.1038 - doTwoSerializedClasses (false, true);
1.1039 - }
1.1040 - }
1.1041 - public void testSerializationOfTwoClassesWithTheSameNameButQueryBeforeSaveWithBroken () throws Exception {
1.1042 - if (lookup instanceof Serializable) {
1.1043 - doTwoSerializedClasses (true, true);
1.1044 - }
1.1045 - }
1.1046 -
1.1047 - private void doTwoSerializedClasses (boolean queryBeforeSerialization, boolean useBroken) throws Exception {
1.1048 - ClassLoader loader = new CL ();
1.1049 - Class c = loader.loadClass (Garbage.class.getName ());
1.1050 -
1.1051 - // in case of InheritanceTree it creates a slot for class Garbage
1.1052 - lookup.lookup(c);
1.1053 -
1.1054 - // but creates new instance and adds it into the lookup
1.1055 - // without querying for it
1.1056 - loader = new CL ();
1.1057 - c = loader.loadClass (Garbage.class.getName ());
1.1058 -
1.1059 - Object theInstance = c.newInstance ();
1.1060 -
1.1061 - ic.addPair (new SerialPair (theInstance));
1.1062 -
1.1063 - Broken2Pair broken = null;
1.1064 - if (useBroken) {
1.1065 - broken = new Broken2Pair ();
1.1066 - ic.addPair (broken);
1.1067 -
1.1068 - assertNull (
1.1069 - "We need to create the slot for the List as " +
1.1070 - "the Broken2Pair will ask for it after deserialization",
1.1071 - lookup.lookup (java.awt.List.class)
1.1072 - );
1.1073 - }
1.1074 -
1.1075 - if (queryBeforeSerialization) {
1.1076 - assertEquals ("Instance is found", theInstance, lookup.lookup (c));
1.1077 - }
1.1078 -
1.1079 - // replace the old lookup with new one
1.1080 - lookup = (Lookup)reserialize(lookup);
1.1081 -
1.1082 - Lookup.Result result = lookup.lookup (new Lookup.Template (Garbage.class));
1.1083 - assertEquals ("One item is the result", 1, result.allInstances ().size ());
1.1084 - Object r = result.allInstances ().iterator ().next ();
1.1085 - assertNotNull("A value is found", r);
1.1086 - assertEquals ("It is of the right class", Garbage.class, r.getClass());
1.1087 - }
1.1088 -
1.1089 - /** Test of reorder and item change which used to fail on interfaces.
1.1090 - */
1.1091 - public void testReoderingIssue13779 () throws Exception {
1.1092 - LinkedList arr = new LinkedList ();
1.1093 -
1.1094 - class R extends Exception implements Cloneable {
1.1095 - }
1.1096 - Object o1 = new R ();
1.1097 - Object o2 = new R ();
1.1098 - Object o3 = new R ();
1.1099 -
1.1100 - arr.add (o1);
1.1101 - arr.add (o2);
1.1102 -
1.1103 - ic.set (arr, null);
1.1104 -
1.1105 - Lookup.Result objectResult = lookup.lookup (new Lookup.Template (Exception.class));
1.1106 - Lookup.Result interfaceResult = lookup.lookup (new Lookup.Template (Cloneable.class));
1.1107 - objectResult.allItems ();
1.1108 - interfaceResult.allItems ();
1.1109 -
1.1110 - LL l1 = new LL (objectResult);
1.1111 - LL l2 = new LL (interfaceResult);
1.1112 -
1.1113 - objectResult.addLookupListener(l1);
1.1114 - interfaceResult.addLookupListener(l2);
1.1115 -
1.1116 - arr.addFirst (o3);
1.1117 -
1.1118 - ic.set (arr, null);
1.1119 -
1.1120 - assertEquals ("One change on objects", 1, l1.getCount ());
1.1121 - assertEquals ("One change on interfaces", 1, l2.getCount ());
1.1122 -
1.1123 - arr.addFirst (new Cloneable () { });
1.1124 - ic.set (arr, null);
1.1125 -
1.1126 - assertEquals ("No change on objects", 0, l1.getCount ());
1.1127 - assertEquals ("But one change on interfaces", 1, l2.getCount ());
1.1128 -
1.1129 - }
1.1130 -
1.1131 - public void testDeadlockBetweenProxyResultAndLookupIssue47772 () throws Exception {
1.1132 - final String myModule = "My Module";
1.1133 - ic.add (myModule);
1.1134 -
1.1135 - class MyProxy extends ProxyLookup {
1.1136 - public MyProxy () {
1.1137 - super (new Lookup[] { lookup });
1.1138 - }
1.1139 - }
1.1140 - final MyProxy my = new MyProxy ();
1.1141 -
1.1142 - final Lookup.Result allModules = my.lookup (new Lookup.Template (String.class));
1.1143 -
1.1144 - class PairThatNeedsInfoAboutModules extends AbstractLookup.Pair {
1.1145 - public String getDisplayName () {
1.1146 - return "Need a module";
1.1147 - }
1.1148 - public String getId () {
1.1149 - return getDisplayName ();
1.1150 - }
1.1151 - public Class getType () {
1.1152 - return Integer.class;
1.1153 - }
1.1154 - protected boolean instanceOf (Class c) {
1.1155 - if (c == Integer.class) {
1.1156 - synchronized (this) {
1.1157 - notifyAll ();
1.1158 - try {
1.1159 - wait (1000);
1.1160 - } catch (InterruptedException ex) {
1.1161 - fail (ex.getMessage ());
1.1162 - }
1.1163 - }
1.1164 - java.util.Collection coll = allModules.allInstances ();
1.1165 - assertEquals ("Size is 1", 1, coll.size ());
1.1166 - assertEquals ("My module is there", myModule, coll.iterator ().next ());
1.1167 - }
1.1168 - return c.isAssignableFrom (Integer.class);
1.1169 - }
1.1170 -
1.1171 - public Object getInstance () {
1.1172 - return new Integer (10);
1.1173 - }
1.1174 -
1.1175 - protected boolean creatorOf (Object obj) {
1.1176 - return new Integer (10).equals (obj);
1.1177 - }
1.1178 - }
1.1179 -
1.1180 - PairThatNeedsInfoAboutModules pair = new PairThatNeedsInfoAboutModules ();
1.1181 - ic.addPair (pair);
1.1182 -
1.1183 - synchronized (pair) {
1.1184 - class BlockInInstanceOf implements Runnable {
1.1185 - public void run () {
1.1186 - Integer i = my.lookup(Integer.class);
1.1187 - assertEquals (new Integer (10), i);
1.1188 - }
1.1189 - }
1.1190 - BlockInInstanceOf blk = new BlockInInstanceOf ();
1.1191 - Executors.newSingleThreadScheduledExecutor().schedule(blk, 0, TimeUnit.MICROSECONDS);
1.1192 - pair.wait ();
1.1193 - }
1.1194 -
1.1195 - java.util.Collection coll = allModules.allInstances ();
1.1196 - assertEquals ("Size is 1", 1, coll.size ());
1.1197 - assertEquals ("My module is there", myModule, coll.iterator ().next ());
1.1198 - }
1.1199 -
1.1200 - public void testAWayToGenerateProblem13779 () {
1.1201 - ic.add (new Integer (1));
1.1202 - ic.add (new Integer (2));
1.1203 - ic.add (new Integer (1));
1.1204 - ic.add (new Integer (2));
1.1205 -
1.1206 - Collection c = lookup.lookup (new Lookup.Template (Integer.class)).allInstances ();
1.1207 - assertEquals ("There are two objects", 2, c.size ());
1.1208 -
1.1209 - }
1.1210 -
1.1211 - /** Replacing items with different objects.
1.1212 - */
1.1213 - public void testReplacingObjectsDoesNotGenerateException () throws Exception {
1.1214 - LinkedList arr = new LinkedList ();
1.1215 -
1.1216 - class R extends Exception implements Cloneable {
1.1217 - }
1.1218 - arr.add (new R ());
1.1219 - arr.add (new R ());
1.1220 -
1.1221 - ic.set (arr, null);
1.1222 -
1.1223 - arr.clear();
1.1224 -
1.1225 - arr.add (new R ());
1.1226 - arr.add (new R ());
1.1227 -
1.1228 - ic.set (arr, null);
1.1229 - }
1.1230 -
1.1231 - public void testAfterDeserializationNoQueryIsPeformedOnAlreadyQueriedObjects() throws Exception {
1.1232 - if (! (lookup instanceof Serializable)) {
1.1233 - // well this test works only for serializable lookups
1.1234 - return;
1.1235 - }
1.1236 -
1.1237 - SerialPair my = new SerialPair ("no");
1.1238 - ic.addPair (my);
1.1239 -
1.1240 - Lookup.Result res = lookup.lookup (new Lookup.Template (String.class));
1.1241 - assertEquals ("One instance", 1, res.allInstances().size ());
1.1242 - assertEquals ("my.instanceOf called once", 1, my.countInstanceOf);
1.1243 -
1.1244 - Lookup serial = (Lookup)reserialize(lookup);
1.1245 -
1.1246 - Lookup.Result r2 = serial.lookup(new Lookup.Template(String.class));
1.1247 -
1.1248 - assertEquals ("One item", 1, r2.allItems ().size ());
1.1249 - Object one = r2.allItems().iterator().next ();
1.1250 - assertEquals ("The right class", SerialPair.class, one.getClass());
1.1251 - SerialPair p = (SerialPair)one;
1.1252 -
1.1253 - assertEquals ("p.instanceOf has not been queried", 0, p.countInstanceOf);
1.1254 - }
1.1255 -
1.1256 - /** Checks the iterator */
1.1257 - private <T> void checkIterator(String msg, Iterator<? extends T> it1, List<? extends T> list) {
1.1258 - int cnt = 0;
1.1259 - Iterator<? extends T> it2 = list.iterator();
1.1260 - while (it1.hasNext () && it2.hasNext ()) {
1.1261 - T n1 = it1.next();
1.1262 - T n2 = it2.next();
1.1263 -
1.1264 - if (n1 != n2) {
1.1265 - fail (msg + " iterator[" + cnt + "] = " + n1 + " but list[" + cnt + "] = " + n2);
1.1266 - }
1.1267 -
1.1268 - cnt++;
1.1269 - }
1.1270 -
1.1271 - if (it1.hasNext ()) {
1.1272 - fail ("Iterator has more elements than list");
1.1273 - }
1.1274 -
1.1275 - if (it2.hasNext ()) {
1.1276 - fail ("List has more elements than iterator");
1.1277 - }
1.1278 - }
1.1279 -
1.1280 -
1.1281 - public void testResultsAreUnmodifyableOrAtLeastTheyDoNotPropagateToCache() throws Exception {
1.1282 - String s = "Ahoj";
1.1283 -
1.1284 - ic.add(s);
1.1285 -
1.1286 - Lookup.Result res = lookup.lookup(new Template(String.class));
1.1287 -
1.1288 - for (int i = 1; i < 5; i++) {
1.1289 - Collection c1 = res.allInstances();
1.1290 - Collection c2 = res.allClasses();
1.1291 - Collection c3 = res.allItems();
1.1292 -
1.1293 - assertTrue(i + ": c1 has it", c1.contains(s));
1.1294 - assertTrue(i + ": c2 has it", c2.contains(s.getClass()));
1.1295 - assertEquals(i + ": c3 has one", 1, c3.size());
1.1296 - Lookup.Item item = (Lookup.Item) c3.iterator().next();
1.1297 - assertEquals(i + ": c3 has it", s, item.getInstance());
1.1298 -
1.1299 - try {
1.1300 - c1.remove(s);
1.1301 - assertEquals("No elements now", 0, c1.size());
1.1302 - } catch (UnsupportedOperationException ex) {
1.1303 - // ok, this need not be supported
1.1304 - }
1.1305 - try {
1.1306 - c2.remove(s.getClass());
1.1307 - assertEquals("No elements now", 0, c2.size());
1.1308 - } catch (UnsupportedOperationException ex) {
1.1309 - // ok, this need not be supported
1.1310 - }
1.1311 - try {
1.1312 - c3.remove(item);
1.1313 - assertEquals("No elements now", 0, c3.size());
1.1314 - } catch (UnsupportedOperationException ex) {
1.1315 - // ok, this need not be supported
1.1316 - }
1.1317 - }
1.1318 - }
1.1319 -
1.1320 - public void testSomeProblemWithDVBFrameworkSeemsToBeInLookup() {
1.1321 - for (int i = 0; i < 5; i++) {
1.1322 - ic.add(lookup);
1.1323 - assertEquals("Can be found", lookup, lookup.lookup(lookup.getClass()));
1.1324 - ic.set(Collections.EMPTY_LIST, null);
1.1325 - }
1.1326 - }
1.1327 -
1.1328 - public void testListeningAndQueryingByTwoListenersInstances() {
1.1329 - doListeningAndQueryingByTwoListeners(0);
1.1330 - }
1.1331 - public void testListeningAndQueryingByTwoListenersClasses() {
1.1332 - doListeningAndQueryingByTwoListeners(1);
1.1333 - }
1.1334 - public void testListeningAndQueryingByTwoListenersItems() {
1.1335 - doListeningAndQueryingByTwoListeners(2);
1.1336 - }
1.1337 -
1.1338 -
1.1339 - private void doListeningAndQueryingByTwoListeners(final int type) {
1.1340 - class L implements LookupListener {
1.1341 - Lookup.Result integer = lookup.lookup(new Template(Integer.class));
1.1342 - Lookup.Result number = lookup.lookup(new Template(Number.class));
1.1343 - Lookup.Result serial = lookup.lookup(new Template(Serializable.class));
1.1344 -
1.1345 - {
1.1346 - integer.addLookupListener(this);
1.1347 - number.addLookupListener(this);
1.1348 - serial.addLookupListener(this);
1.1349 - }
1.1350 -
1.1351 - int round;
1.1352 -
1.1353 - public void resultChanged(LookupEvent ev) {
1.1354 - Collection c1 = get(type, integer);
1.1355 - Collection c2 = get(type, number);
1.1356 - Collection c3 = get(type, serial);
1.1357 -
1.1358 - assertEquals("round " + round + " c1 vs. c2", c1, c2);
1.1359 - assertEquals("round " + round + " c1 vs. c3", c1, c3);
1.1360 - assertEquals("round " + round + " c2 vs. c3", c2, c3);
1.1361 -
1.1362 - round++;
1.1363 - }
1.1364 -
1.1365 - private Collection get(int type, Lookup.Result res) {
1.1366 - Collection c;
1.1367 - switch(type) {
1.1368 - case 0: c = res.allInstances(); break;
1.1369 - case 1: c = res.allClasses(); break;
1.1370 - case 2: c = res.allItems(); break;
1.1371 - default: c = null; fail("Type: " + type); break;
1.1372 - }
1.1373 -
1.1374 - assertNotNull(c);
1.1375 - return new ArrayList(c);
1.1376 - }
1.1377 - }
1.1378 -
1.1379 - L listener = new L();
1.1380 - listener.resultChanged(null);
1.1381 -
1.1382 - for(int i = 0; i < 100; i++) {
1.1383 - ic.add(new Integer(i));
1.1384 - }
1.1385 -
1.1386 - assertEquals("3x100+1 checks", 301, listener.round);
1.1387 - }
1.1388 -
1.1389 - public void testChangeOfNodeDoesNotFireChangeInActionMap() {
1.1390 - ActionMap am = new ActionMap();
1.1391 - Lookup s = Lookups.singleton(am);
1.1392 - doChangeOfNodeDoesNotFireChangeInActionMap(am, s, false, 0);
1.1393 - }
1.1394 - public void testChangeOfNodeDoesNotFireChangeInActionMapSimple() {
1.1395 - ActionMap am = new ActionMap();
1.1396 - Lookup s = Lookups.singleton(am);
1.1397 - doChangeOfNodeDoesNotFireChangeInActionMap(am, s, true, 0);
1.1398 - }
1.1399 -
1.1400 - public void testChangeOfNodeDoesNotFireChangeInActionMapWithBeforeLookupSimple() {
1.1401 - doChangeOfNodeDoesNotFireChangeInActionMapWithBeforeLookup(true);
1.1402 - }
1.1403 -
1.1404 - public void testChangeOfNodeDoesNotFireChangeInActionMapWithBeforeLookup() {
1.1405 - doChangeOfNodeDoesNotFireChangeInActionMapWithBeforeLookup(false);
1.1406 - }
1.1407 - private void doChangeOfNodeDoesNotFireChangeInActionMapWithBeforeLookup(boolean wrapBySimple) {
1.1408 - final ActionMap am = new ActionMap();
1.1409 -
1.1410 - class Before extends AbstractLookup {
1.1411 - public InstanceContent ic;
1.1412 - public Before() {
1.1413 - this(new InstanceContent());
1.1414 - }
1.1415 -
1.1416 - private Before(InstanceContent ic) {
1.1417 - super(ic);
1.1418 - this.ic = ic;
1.1419 - }
1.1420 -
1.1421 - protected @Override void beforeLookup(Template template) {
1.1422 - if (ic != null) {
1.1423 - ic.add(am);
1.1424 - ic = null;
1.1425 - }
1.1426 - }
1.1427 - }
1.1428 -
1.1429 - Before s = new Before();
1.1430 - doChangeOfNodeDoesNotFireChangeInActionMap(am, s, wrapBySimple, 1);
1.1431 -
1.1432 - assertNull("beforeLookup called once", s.ic);
1.1433 - }
1.1434 -
1.1435 - private void doChangeOfNodeDoesNotFireChangeInActionMap(final ActionMap am, Lookup actionMapLookup, final boolean wrapBySimple, int firstChange) {
1.1436 - Lookup[] lookups = { lookup, actionMapLookup };
1.1437 -
1.1438 - class Provider implements Lookup.Provider {
1.1439 - ProxyLookup delegate;
1.1440 - Lookup query;
1.1441 -
1.1442 - public Provider(Lookup[] arr) {
1.1443 - if (wrapBySimple) {
1.1444 - delegate = new ProxyLookup(arr);
1.1445 - query = Lookups.proxy(this);
1.1446 - } else {
1.1447 - query = delegate = new ProxyLookup(arr);
1.1448 - }
1.1449 - }
1.1450 -
1.1451 - public Lookup getLookup() {
1.1452 - return delegate;
1.1453 - }
1.1454 -
1.1455 - public void setLookups(Lookup... arr) {
1.1456 - if (wrapBySimple) {
1.1457 - delegate = new ProxyLookup(arr);
1.1458 - } else {
1.1459 - delegate.setLookups(arr);
1.1460 - }
1.1461 - }
1.1462 - }
1.1463 -
1.1464 - Provider p = new Provider(lookups);
1.1465 -
1.1466 - Lookup.Result res = p.query.lookup(new Lookup.Template(ActionMap.class));
1.1467 - LL ll = new LL();
1.1468 - res.addLookupListener(ll);
1.1469 -
1.1470 - Collection c = res.allInstances();
1.1471 - assertFalse("Has next", c.isEmpty());
1.1472 -
1.1473 - ActionMap am1 = (ActionMap)c.iterator().next();
1.1474 - assertEquals("Am is there", am, am1);
1.1475 -
1.1476 - assertEquals("Correct # of changes in first get", firstChange, ll.getCount());
1.1477 -
1.1478 - Object m1 = new InputMap();
1.1479 - Object m2 = new InputMap();
1.1480 -
1.1481 - ic.add(m1);
1.1482 - assertEquals("No change in ActionMap 1", 0, ll.getCount());
1.1483 - ic.set(Collections.singletonList(m2), null);
1.1484 - assertEquals("No change in ActionMap 2", 0, ll.getCount());
1.1485 - ic.add(m2);
1.1486 - assertEquals("No change in ActionMap 3", 0, ll.getCount());
1.1487 - p.setLookups(lookup, actionMapLookup, Lookup.EMPTY);
1.1488 - assertEquals("No change in ActionMap 4", 0, ll.getCount());
1.1489 -
1.1490 - ActionMap am2 = p.query.lookup(ActionMap.class);
1.1491 - assertEquals("Still the same action map", am, am2);
1.1492 -
1.1493 -
1.1494 - class Before extends AbstractLookup {
1.1495 - public InstanceContent ic;
1.1496 - public Before() {
1.1497 - this(new InstanceContent());
1.1498 - }
1.1499 -
1.1500 - private Before(InstanceContent ic) {
1.1501 - super(ic);
1.1502 - this.ic = ic;
1.1503 - }
1.1504 -
1.1505 - protected @Override void beforeLookup(Template template) {
1.1506 - if (ic != null) {
1.1507 - ic.add(am);
1.1508 - ic = null;
1.1509 - }
1.1510 - }
1.1511 - }
1.1512 -
1.1513 - Before s = new Before();
1.1514 -
1.1515 - // adding different Before, but returning the same instance
1.1516 - // this happens with metaInfServices lookup often, moreover
1.1517 - // it adds the instance in beforeLookup, which confuses a lot
1.1518 - p.setLookups(new Lookup[]{ lookup, new Before() });
1.1519 - assertEquals("No change in ActionMap 5", 0, ll.getCount());
1.1520 -
1.1521 -
1.1522 - }
1.1523 -
1.1524 - public void testTasklistsCase() throws Exception {
1.1525 - ic.remove(new Object());
1.1526 - }
1.1527 -
1.1528 -
1.1529 -
1.1530 - public void testMultipleListeners() {
1.1531 - Object object = new ImplementationObject();
1.1532 - ic.add(object);
1.1533 -
1.1534 - Listener[] listeners = new Listener[4];
1.1535 - Lookup.Result result = lookup.lookup(new Lookup.Template(LookupObject.class));
1.1536 - for(int i = 0; i < listeners.length; ++i) {
1.1537 - listeners[i] = new Listener();
1.1538 - result.addLookupListener(listeners[i]);
1.1539 - }
1.1540 - // initialize listening
1.1541 - result.allItems();
1.1542 -
1.1543 - ic.remove(object);
1.1544 -
1.1545 - // Apparently, only odd-numbered listeners get called when there are multiple LookupListeners on a result
1.1546 - //for(int i = 0; i < listeners.length; ++i) {
1.1547 - // System.out.println("Listener " + i + ": " + listeners[i].wasCalled());
1.1548 - //}
1.1549 - for(int i = 0; i < listeners.length; ++i) {
1.1550 - assertTrue("Listener " + i + " called", listeners[i].wasCalled());
1.1551 - }
1.1552 - }
1.1553 -
1.1554 - static Object reserialize(Object o) throws Exception {
1.1555 - ByteArrayOutputStream os = new ByteArrayOutputStream();
1.1556 - ObjectOutputStream oos = new ObjectOutputStream(os);
1.1557 - oos.writeObject(o);
1.1558 - oos.close();
1.1559 -
1.1560 - ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
1.1561 - ObjectInputStream ois = new ObjectInputStream(is);
1.1562 - return ois.readObject();
1.1563 - }
1.1564 -
1.1565 - private class Listener implements LookupListener {
1.1566 - private boolean listenerCalled = false;
1.1567 -
1.1568 - public void resultChanged(LookupEvent ev) {
1.1569 - listenerCalled = true;
1.1570 - }
1.1571 -
1.1572 - public boolean wasCalled() {
1.1573 - return listenerCalled;
1.1574 - }
1.1575 -
1.1576 - public void reset() {
1.1577 - listenerCalled = false;
1.1578 - }
1.1579 - }
1.1580 -
1.1581 - private interface LookupObject {}
1.1582 - private class ImplementationObject implements LookupObject {}
1.1583 - private class NullObject implements LookupObject {}
1.1584 -
1.1585 -
1.1586 - public void testReturnSomethingElseThenYouClaimYouWillReturn() {
1.1587 - class Liar extends AbstractLookup.Pair {
1.1588 - public Object obj;
1.1589 -
1.1590 - protected boolean instanceOf(Class c) {
1.1591 - return c.isAssignableFrom(String.class);
1.1592 - }
1.1593 -
1.1594 - protected boolean creatorOf(Object obj) {
1.1595 - return this.obj == obj;
1.1596 - }
1.1597 -
1.1598 - public Object getInstance() {
1.1599 - return this.obj;
1.1600 - }
1.1601 -
1.1602 - public Class getType() {
1.1603 - return String.class;
1.1604 - }
1.1605 -
1.1606 - public String getId() {
1.1607 - return String.class.getName();
1.1608 - }
1.1609 -
1.1610 - public String getDisplayName() {
1.1611 - return getId();
1.1612 - }
1.1613 - }
1.1614 -
1.1615 -
1.1616 - Liar l = new Liar();
1.1617 - l.obj = new Integer(5);
1.1618 -
1.1619 - this.ic.addPair(l);
1.1620 -
1.1621 - Collection c = lookup.lookup(new Lookup.Template(String.class)).allInstances();
1.1622 - assertTrue("It is empty: " + c, c.isEmpty());
1.1623 - }
1.1624 -
1.1625 - public void testCanProxyLookupHaveWrongResults() {
1.1626 - class L implements LookupListener {
1.1627 - ProxyLookup pl;
1.1628 - Lookup.Result<String> original;
1.1629 - Lookup.Result<String> wrapped;
1.1630 - boolean ok;
1.1631 -
1.1632 - public void test() {
1.1633 - pl = new ProxyLookup(lookup);
1.1634 - original = lookup.lookupResult(String.class);
1.1635 -
1.1636 - original.addLookupListener(this);
1.1637 -
1.1638 - wrapped = pl.lookupResult(String.class);
1.1639 -
1.1640 - assertEquals("Original empty", 0, original.allInstances().size());
1.1641 - assertEquals("Wrapped empty", 0, wrapped.allInstances().size());
1.1642 -
1.1643 - ic.add("Hello!");
1.1644 - }
1.1645 -
1.1646 - public void resultChanged(LookupEvent ev) {
1.1647 - ok = true;
1.1648 -
1.1649 - assertEquals("Original has hello", 1, original.allInstances().size());
1.1650 - assertEquals("Wrapped has hello", 1, wrapped.allInstances().size());
1.1651 - }
1.1652 -
1.1653 - }
1.1654 - L listener = new L();
1.1655 - listener.test();
1.1656 - assertTrue("Listener called", listener.ok);
1.1657 - }
1.1658 -
1.1659 - public void testObjectFromInstanceContentConverterDisappearsIfNotReferenced() {
1.1660 - Conv converter = new Conv("foo");
1.1661 - ic.add (converter, converter);
1.1662 - Lookup lkp = instanceLookup;
1.1663 - StringBuilder sb = lookup.lookup (StringBuilder.class);
1.1664 - assertNotNull (sb);
1.1665 - int hash = System.identityHashCode(sb);
1.1666 - assertEquals ("foo", sb.toString());
1.1667 - Reference<StringBuilder> r = new WeakReference<StringBuilder>(sb);
1.1668 - sb = null;
1.1669 - assertGC("Lookup held onto object", r);
1.1670 - sb = lookup.lookup (StringBuilder.class);
1.1671 - assertNotSame(hash, System.identityHashCode(sb));
1.1672 - r = new WeakReference<StringBuilder>(sb);
1.1673 - sb = null;
1.1674 - assertGC("Lookup held onto object", r);
1.1675 - ic.remove (converter, converter);
1.1676 - Reference <InstanceContent.Convertor> cref = new WeakReference<InstanceContent.Convertor>(converter);
1.1677 - converter = null;
1.1678 - assertGC("Converter still referenced", cref);
1.1679 -
1.1680 - sb = lkp.lookup(StringBuilder.class);
1.1681 - assertNull ("Converter removed from lookup, but object it " +
1.1682 - "created still present:'" + sb +"'", sb);
1.1683 - converter = new Conv("bar");
1.1684 - ic.add (converter, converter);
1.1685 - assertNotNull (lkp.lookup(StringBuilder.class));
1.1686 - assertEquals ("bar", lkp.lookup(StringBuilder.class).toString());
1.1687 - }
1.1688 -
1.1689 - private static class Conv implements InstanceContent.Convertor<Conv, StringBuilder> {
1.1690 - private final String str;
1.1691 - private Conv (String str) {
1.1692 - this.str = str;
1.1693 - }
1.1694 -
1.1695 - public StringBuilder convert(Conv obj) {
1.1696 - return new StringBuilder (str);
1.1697 - }
1.1698 -
1.1699 - public Class<? extends StringBuilder> type(Conv obj) {
1.1700 - return StringBuilder.class;
1.1701 - }
1.1702 -
1.1703 - public String id(Conv obj) {
1.1704 - return "Foo";
1.1705 - }
1.1706 -
1.1707 - public String displayName(Conv obj) {
1.1708 - return "Foo";
1.1709 - }
1.1710 - } // end of Conv
1.1711 -
1.1712 - public void testCanGCResults() throws Exception {
1.1713 - class L implements LookupListener {
1.1714 - int cnt;
1.1715 -
1.1716 - public void resultChanged(LookupEvent ev) {
1.1717 - cnt++;
1.1718 - }
1.1719 -
1.1720 - }
1.1721 - L listener1 = new L();
1.1722 - L listener2 = new L();
1.1723 -
1.1724 - Lookup.Result<String> res1 = this.instanceLookup.lookupResult(String.class);
1.1725 - Lookup.Result<String> res2 = this.lookup.lookupResult(String.class);
1.1726 -
1.1727 - assertEquals("Empty1", 0, res1.allItems().size());
1.1728 - assertEquals("Empty2", 0, res2.allItems().size());
1.1729 -
1.1730 - res1.addLookupListener(listener1);
1.1731 - res2.addLookupListener(listener2);
1.1732 -
1.1733 - addInstances(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
1.1734 - this.ic.add("Ahoj");
1.1735 -
1.1736 - assertEquals("Change1", 1, listener1.cnt);
1.1737 - assertEquals("Change2", 1, listener2.cnt);
1.1738 -
1.1739 - assertEquals("Full1", 1, res1.allItems().size());
1.1740 - assertEquals("Full2", 1, res2.allItems().size());
1.1741 -
1.1742 -
1.1743 - Reference<Object> ref2 = new WeakReference<Object>(res2);
1.1744 - res2 = null;
1.1745 - assertGC("Result can disappear", ref2);
1.1746 - }
1.1747 -
1.1748 - void beforeActualTest(String n) {
1.1749 - if (n.equals("testEqualsIsNotCalledTooMuch")) {
1.1750 - CntPair.cnt = 0;
1.1751 - CntPair.hashCnt = 0;
1.1752 - CntPair.instances = 0;
1.1753 - int how = 1000;
1.1754 -
1.1755 - for(int i = 0; i < how; i++) {
1.1756 - this.ic.addPair(new CntPair("x" + i));
1.1757 - }
1.1758 -
1.1759 - assertEquals("No equals called", 0, CntPair.cnt);
1.1760 - assertEquals("1000 instances ", how, CntPair.instances);
1.1761 - }
1.1762 - }
1.1763 -
1.1764 - public void testEqualsIsNotCalledTooMuch() throws Exception {
1.1765 - // most of the work done in beforeActualTest
1.1766 -
1.1767 - // desirable: assertEquals("no comparitions", 0, CntPair.cnt);
1.1768 - // works for InheritanceTree, but not for ArrayStorage, but array
1.1769 - // storages are generally small
1.1770 -
1.1771 - if (CntPair.cnt > 12000) {
1.1772 - fail("Too much comparitions " + CntPair.cnt);
1.1773 - }
1.1774 - if (CntPair.hashCnt > 40000) {
1.1775 - fail("Too much hashes: " + CntPair.hashCnt);
1.1776 - }
1.1777 -
1.1778 - assertEquals("instaces is enough", 1000, CntPair.instances);
1.1779 - }
1.1780 -
1.1781 - /** Adds instances to the instance lookup.
1.1782 - */
1.1783 - private void addInstances (Object... instances) {
1.1784 - for (int i = 0; i < instances.length; i++) {
1.1785 - ic.add(instances[i]);
1.1786 - }
1.1787 - }
1.1788 -
1.1789 - /** Count instances of clazz in an array. */
1.1790 - private int countInstances (Object[] objs, Class clazz) {
1.1791 - int count = 0;
1.1792 - for (int i = 0; i < objs.length; i++) {
1.1793 - if (clazz.isInstance(objs[i])) count++;
1.1794 - }
1.1795 - return count;
1.1796 - }
1.1797 -
1.1798 - /** Counting listener */
1.1799 - protected static class LL implements LookupListener {
1.1800 - private int count = 0;
1.1801 - public Object source;
1.1802 - public Thread changesIn;
1.1803 -
1.1804 - public LL () {
1.1805 - this (null);
1.1806 - }
1.1807 -
1.1808 - public LL (Object source) {
1.1809 - this.source = source;
1.1810 - }
1.1811 -
1.1812 - public void resultChanged(LookupEvent ev) {
1.1813 - if (changesIn != null) {
1.1814 - assertEquals("Changes in the same thread", changesIn, Thread.currentThread());
1.1815 - } else {
1.1816 - changesIn = Thread.currentThread();
1.1817 - }
1.1818 - ++count;
1.1819 - if (source != null) {
1.1820 - assertSame ("Source is the same", source, ev.getSource ());
1.1821 -// assertSame ("Result is the same", source, ev.getResult ());
1.1822 - }
1.1823 - }
1.1824 -
1.1825 - public int getCount() {
1.1826 - int i = count;
1.1827 - count = 0;
1.1828 - return i;
1.1829 - }
1.1830 - };
1.1831 -
1.1832 - /** A set of interfaces for testInterfaceInheritance
1.1833 - */
1.1834 - interface TestInterfaceInheritanceA {}
1.1835 - interface TestInterfaceInheritanceB extends TestInterfaceInheritanceA, java.rmi.Remote {}
1.1836 - interface TestInterfaceInheritanceBB extends TestInterfaceInheritanceB {}
1.1837 - interface TestInterfaceInheritanceC extends TestInterfaceInheritanceA, java.rmi.Remote {}
1.1838 - interface TestInterfaceInheritanceD extends TestInterfaceInheritanceA {}
1.1839 -
1.1840 - /** A special class for garbage test */
1.1841 - public static final class Garbage extends Object implements Serializable {
1.1842 - static final long serialVersionUID = 435340912534L;
1.1843 - }
1.1844 -
1.1845 -
1.1846 - /* A classloader that can load one class in a special way */
1.1847 - private static class CL extends ClassLoader {
1.1848 - public CL () {
1.1849 - super (null);
1.1850 - }
1.1851 -
1.1852 - public @Override Class findClass(String name) throws ClassNotFoundException {
1.1853 - if (name.equals (Garbage.class.getName ())) {
1.1854 - String n = name.replace ('.', '/');
1.1855 - java.io.InputStream is = getClass ().getResourceAsStream ("/" + n + ".class");
1.1856 - byte[] arr = new byte[8096];
1.1857 - try {
1.1858 - int cnt = is.read (arr);
1.1859 - if (cnt == arr.length) {
1.1860 - fail ("Buffer to load the class is not big enough");
1.1861 - }
1.1862 -
1.1863 - return defineClass (name, arr, 0, cnt);
1.1864 - } catch (java.io.IOException ex) {
1.1865 - ex.printStackTrace();
1.1866 - fail ("IO Exception");
1.1867 - return null;
1.1868 - }
1.1869 - } else {
1.1870 - return null;
1.1871 - }
1.1872 - }
1.1873 -
1.1874 - /** Convert obj to other object. There is no need to implement
1.1875 - * cache mechanism. It is provided by AbstractLookup.Item.getInstance().
1.1876 - * Method should be called more than once because Lookup holds
1.1877 - * just weak reference.
1.1878 - */
1.1879 - public Object convert(Object obj) {
1.1880 - return null;
1.1881 - }
1.1882 -
1.1883 - /** Return type of converted object. */
1.1884 - public Class type(Object obj) {
1.1885 - try {
1.1886 - return loadClass (Garbage.class.getName ());
1.1887 - } catch (ClassNotFoundException ex) {
1.1888 - fail ("Class not found");
1.1889 - throw new InternalError ();
1.1890 - }
1.1891 - }
1.1892 - }
1.1893 -
1.1894 - private static final class CntPair extends AbstractLookup.Pair {
1.1895 - private static int instances;
1.1896 - private String txt;
1.1897 -
1.1898 - public CntPair(String txt) {
1.1899 - this.txt = txt;
1.1900 - instances++;
1.1901 - }
1.1902 -
1.1903 - public static int hashCnt;
1.1904 - @Override
1.1905 - public int hashCode() {
1.1906 - hashCnt++;
1.1907 - return txt.hashCode() + 3777;
1.1908 - }
1.1909 -
1.1910 - public static int cnt;
1.1911 - @Override
1.1912 - public boolean equals(Object obj) {
1.1913 - cnt++;
1.1914 -
1.1915 - if (obj == null) {
1.1916 - return false;
1.1917 - }
1.1918 - if (getClass() != obj.getClass()) {
1.1919 - return false;
1.1920 - }
1.1921 - final CntPair other = (CntPair) obj;
1.1922 - if (this.txt != other.txt && (this.txt == null || !this.txt.equals(other.txt))) {
1.1923 - return false;
1.1924 - }
1.1925 - return true;
1.1926 - }
1.1927 -
1.1928 - protected boolean instanceOf(Class c) {
1.1929 - return c.isAssignableFrom(String.class);
1.1930 - }
1.1931 -
1.1932 - protected boolean creatorOf(Object obj) {
1.1933 - return obj == txt;
1.1934 - }
1.1935 -
1.1936 - public Object getInstance() {
1.1937 - return txt;
1.1938 - }
1.1939 -
1.1940 - public Class getType() {
1.1941 - return String.class;
1.1942 - }
1.1943 -
1.1944 - public String getId() {
1.1945 - return txt;
1.1946 - }
1.1947 -
1.1948 - public String getDisplayName() {
1.1949 - return txt;
1.1950 - }
1.1951 -
1.1952 - }
1.1953 -
1.1954 - public static final class SerialPair extends AbstractLookup.Pair
1.1955 - implements java.io.Serializable {
1.1956 - static final long serialVersionUID = 54305834L;
1.1957 - private Object value;
1.1958 - public transient int countInstanceOf;
1.1959 -
1.1960 - public SerialPair (Object value) {
1.1961 - this.value = value;
1.1962 - }
1.1963 -
1.1964 - protected boolean creatorOf(Object obj) {
1.1965 - return obj == value;
1.1966 - }
1.1967 -
1.1968 - public String getDisplayName() {
1.1969 - return getId ();
1.1970 - }
1.1971 -
1.1972 - public String getId() {
1.1973 - return value.toString();
1.1974 - }
1.1975 -
1.1976 - public Object getInstance() {
1.1977 - return value;
1.1978 - }
1.1979 -
1.1980 - public Class getType() {
1.1981 - return value.getClass ();
1.1982 - }
1.1983 -
1.1984 - protected boolean instanceOf(Class c) {
1.1985 - countInstanceOf++;
1.1986 - return c.isInstance(value);
1.1987 - }
1.1988 - } // end of SerialPair
1.1989 -
1.1990 - private static class BrokenPair extends AbstractLookup.Pair {
1.1991 - private transient ThreadLocal IN = new ThreadLocal ();
1.1992 - private boolean checkModify;
1.1993 - private boolean checkQuery;
1.1994 -
1.1995 - public BrokenPair (boolean checkModify, boolean checkQuery) {
1.1996 - this.checkModify = checkModify;
1.1997 - this.checkQuery = checkQuery;
1.1998 - }
1.1999 -
1.2000 - protected boolean creatorOf(Object obj) { return this == obj; }
1.2001 - public String getDisplayName() { return "Broken"; }
1.2002 - public String getId() { return "broken"; }
1.2003 - public Object getInstance() { return this; }
1.2004 - public Class getType() { return getClass (); }
1.2005 - protected boolean instanceOf(Class c) {
1.2006 -
1.2007 - if (checkQuery) {
1.2008 - if (IN.get () == null) {
1.2009 - try {
1.2010 - IN.set (this);
1.2011 - // broken behaviour, tries to modify the lookup
1.2012 - // queries have to survive
1.2013 -
1.2014 - running.lookup.lookup (java.awt.List.class);
1.2015 -
1.2016 - //
1.2017 - // creation of new result has to survive as well
1.2018 - Lookup.Result myQuery = running.lookup.lookup (new Lookup.Template (java.awt.Button.class));
1.2019 - Collection all = myQuery.allItems ();
1.2020 - } finally {
1.2021 - IN.set (null);
1.2022 - }
1.2023 - }
1.2024 - }
1.2025 -
1.2026 -
1.2027 - if (checkModify) {
1.2028 - //
1.2029 - // modifications should fail
1.2030 - //
1.2031 -
1.2032 - try {
1.2033 - running.ic.addPair (new SerialPair (""));
1.2034 - fail ("Modification from a query should be prohibited");
1.2035 - } catch (IllegalStateException ex) {
1.2036 - }
1.2037 -
1.2038 - try {
1.2039 - running.ic.removePair (this);
1.2040 - fail ("This has to throw the exception");
1.2041 - } catch (IllegalStateException ex) {
1.2042 - }
1.2043 - try {
1.2044 - running.ic.setPairs (Collections.EMPTY_SET);
1.2045 - fail ("This has to throw the exception as well");
1.2046 - } catch (IllegalStateException ex) {
1.2047 - }
1.2048 - }
1.2049 -
1.2050 - return c.isAssignableFrom(getType ());
1.2051 - }
1.2052 - } // end of BrokenPair
1.2053 -
1.2054 - private static class Broken2Pair extends AbstractLookup.Pair {
1.2055 - static final long serialVersionUID = 4532587018501L;
1.2056 - public transient ThreadLocal IN;
1.2057 -
1.2058 - public Broken2Pair () {
1.2059 - }
1.2060 -
1.2061 - private void writeObject (java.io.ObjectOutputStream oos) throws java.io.IOException {
1.2062 - }
1.2063 -
1.2064 - private void readObject (java.io.ObjectInputStream ois) throws java.io.IOException, ClassNotFoundException {
1.2065 - IN = new ThreadLocal ();
1.2066 - }
1.2067 -
1.2068 - protected boolean creatorOf(Object obj) { return this == obj; }
1.2069 - public String getDisplayName() { return "Broken"; }
1.2070 - public String getId() { return "broken"; }
1.2071 - public Object getInstance() { return this; }
1.2072 - public Class getType() { return getClass (); }
1.2073 - protected boolean instanceOf(Class c) {
1.2074 -
1.2075 - // behaviour gets broken only after deserialization
1.2076 - if (IN != null && IN.get () == null) {
1.2077 - try {
1.2078 - IN.set (this);
1.2079 -
1.2080 - // creation of new result has to survive as well
1.2081 - Lookup.Result myQuery = running.lookup.lookup (new Lookup.Template (java.awt.List.class));
1.2082 - Collection all = myQuery.allItems ();
1.2083 - } finally {
1.2084 - IN.set (null);
1.2085 - }
1.2086 - }
1.2087 -
1.2088 - return c.isAssignableFrom(getType ());
1.2089 - }
1.2090 - } // end of Broken2Pair
1.2091 -}