1.1 --- a/lookup/src/test/java/org/openide/util/lookup/ExcludingLookupTest.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,228 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * Contributor(s):
1.28 - *
1.29 - * The Original Software is NetBeans. The Initial Developer of the Original
1.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
1.31 - * Microsystems, Inc. All Rights Reserved.
1.32 - *
1.33 - * If you wish your version of this file to be governed by only the CDDL
1.34 - * or only the GPL Version 2, indicate your decision by adding
1.35 - * "[Contributor] elects to include this software in this distribution
1.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.37 - * single choice of license, a recipient has the option to distribute
1.38 - * your version of this file under either the CDDL, the GPL Version 2 or
1.39 - * to extend the choice of license to its licensees as provided above.
1.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.41 - * Version 2 license, then the option applies only if the new code is
1.42 - * made subject to such option by the copyright holder.
1.43 - */
1.44 -
1.45 -package org.openide.util.lookup;
1.46 -
1.47 -import java.util.*;
1.48 -import org.openide.util.Lookup;
1.49 -
1.50 -/** Runs all NbLookupTest tests on ProxyLookup and adds few additional.
1.51 - */
1.52 -@SuppressWarnings("unchecked") // XXX ought to be corrected, just a lot of them
1.53 -public class ExcludingLookupTest extends AbstractLookupBaseHid
1.54 -implements AbstractLookupBaseHid.Impl {
1.55 - public ExcludingLookupTest(java.lang.String testName) {
1.56 - super(testName, null);
1.57 - }
1.58 -
1.59 - public Lookup createLookup (final Lookup lookup) {
1.60 - return Lookups.exclude (lookup, new Class[0]);
1.61 - }
1.62 -
1.63 - public Lookup createInstancesLookup (InstanceContent ic) {
1.64 - return new AbstractLookup (ic);
1.65 - }
1.66 -
1.67 - public void clearCaches () {
1.68 - }
1.69 -
1.70 - public void testWeCanRemoveInteger () throws Exception {
1.71 - doBasicFilteringTest (Integer.class, Integer.class, 0);
1.72 - }
1.73 -
1.74 - public void testWeCanRemoveIntegersEvenByAskingForRemoveOfAllNumbers () throws Exception {
1.75 - doBasicFilteringTest (Number.class, Integer.class, 0);
1.76 - }
1.77 - public void testFunWithInterfaces () throws Exception {
1.78 - doBasicFilteringTest (java.io.Serializable.class, Integer.class, 0);
1.79 - }
1.80 -
1.81 - public void testWeCanGetInstanceOfSerializableEvenItIsExcludedIfWeAskForClassNotExtendingIt () throws Exception {
1.82 - Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { java.io.Serializable.class });
1.83 - Lookup.Template t = new Lookup.Template (Object.class);
1.84 - Lookup.Result res = lookup.lookup (t);
1.85 -
1.86 - LL ll = new LL ();
1.87 - res.addLookupListener (ll);
1.88 - assertEquals ("Nothing is there", 0, res.allItems ().size ());
1.89 -
1.90 - Object inst = new Integer (3);
1.91 - ic.add (inst);
1.92 -
1.93 - assertEquals ("Not Filtered out", inst, lookup.lookup (Object.class));
1.94 - assertEquals ("Not Filtered out2", inst, lookup.lookupItem (t).getInstance ());
1.95 - assertEquals ("One is there - 2", 1, res.allItems ().size ());
1.96 - assertEquals ("One is there - 2a", 1, res.allInstances ().size ());
1.97 - assertEquals ("One is there - 2b", 1, res.allClasses ().size ());
1.98 - assertEquals ("Right # of events", 1, ll.getCount ());
1.99 -
1.100 - ic.remove (inst);
1.101 - assertEquals ("Filtered out3", null, lookup.lookupItem (t));
1.102 - assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
1.103 - assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
1.104 - assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
1.105 - assertEquals ("Of course it is not there", null, lookup.lookup (Object.class));
1.106 - assertEquals ("Right # of events", 1, ll.getCount ());
1.107 - }
1.108 -
1.109 - public void testIntegersQueriedThruObject () throws Exception {
1.110 - doBasicFilteringTest (Number.class, Object.class, 1);
1.111 - }
1.112 -
1.113 - private void doBasicFilteringTest (Class theFilter, Class theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception {
1.114 - Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { theFilter });
1.115 - Lookup.Template t = new Lookup.Template (theQuery);
1.116 - Lookup.Result res = lookup.lookup (t);
1.117 -
1.118 - LL ll = new LL ();
1.119 - res.addLookupListener (ll);
1.120 - assertEquals ("Nothing is there", 0, res.allItems ().size ());
1.121 -
1.122 - Object inst = new Integer (3);
1.123 - ic.add (inst);
1.124 -
1.125 - assertEquals ("Filtered out", null, lookup.lookup (theQuery));
1.126 - assertEquals ("Filtered out2", null, lookup.lookupItem (t));
1.127 - assertEquals ("Nothing is there - 2", 0, res.allItems ().size ());
1.128 - assertEquals ("Nothing is there - 2a", 0, res.allInstances ().size ());
1.129 - assertEquals ("Nothing is there - 2b", 0, res.allClasses ().size ());
1.130 - assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
1.131 -
1.132 - ic.remove (inst);
1.133 - assertEquals ("Filtered out3", null, lookup.lookupItem (t));
1.134 - assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
1.135 - assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
1.136 - assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
1.137 - assertEquals ("Of course it is not there", null, lookup.lookup (theQuery));
1.138 - assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
1.139 -
1.140 - }
1.141 -
1.142 - public void testSizeOfTheLookup () throws Exception {
1.143 - Class exclude = String.class;
1.144 -
1.145 - Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { exclude });
1.146 -
1.147 - assertSize ("Should be pretty lightweight", Collections.singleton (lookup), 16,
1.148 - new Object[] { this.instanceLookup, exclude });
1.149 - }
1.150 - public void testSizeOfTheLookupForMultipleFiltersIsHigher () throws Exception {
1.151 - Class exclude = String.class;
1.152 - Class exclude2 = Integer.class;
1.153 - Class[] arr = new Class[] { exclude, exclude2 };
1.154 -
1.155 - Lookup lookup = Lookups.exclude (this.instanceLookup, arr);
1.156 -
1.157 - assertSize ("Is fatter", Collections.singleton (lookup), 40,
1.158 - new Object[] { this.instanceLookup, exclude, exclude2 });
1.159 - assertSize ("But only due to the array", Collections.singleton (lookup), 16,
1.160 - new Object[] { this.instanceLookup, exclude, exclude2, arr });
1.161 - }
1.162 -
1.163 - public void testFilteringOfSomething () throws Exception {
1.164 - doFilteringOfSomething (Runnable.class, java.io.Serializable.class, 1);
1.165 - }
1.166 -
1.167 - private void doFilteringOfSomething (Class theFilter, Class theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception {
1.168 - Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { theFilter });
1.169 - Lookup.Template t = new Lookup.Template (theQuery);
1.170 - Lookup.Result res = lookup.lookup (t);
1.171 -
1.172 - LL ll = new LL ();
1.173 - res.addLookupListener (ll);
1.174 - assertEquals ("Nothing is there", 0, res.allItems ().size ());
1.175 -
1.176 - Object inst = new Integer (3);
1.177 - ic.add (inst);
1.178 -
1.179 - assertEquals ("Accepted", inst, lookup.lookup (theQuery));
1.180 - assertNotNull ("Accepted too", lookup.lookupItem (t));
1.181 - assertEquals ("One is there - 2", 1, res.allItems ().size ());
1.182 - assertEquals ("One is there - 2a", 1, res.allInstances ().size ());
1.183 - assertEquals ("One is there - 2b", 1, res.allClasses ().size ());
1.184 - assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
1.185 -
1.186 - Object inst2 = new Thread (); // implements Runnable
1.187 - ic.add (inst2);
1.188 - assertEquals ("Accepted - 2", inst, lookup.lookup (theQuery));
1.189 - assertNotNull ("Accepted too -2", lookup.lookupItem (t));
1.190 - assertEquals ("One is there - 3", 1, res.allItems ().size ());
1.191 - assertEquals ("One is there - 3a", 1, res.allInstances ().size ());
1.192 - assertEquals ("One is there - 3b", 1, res.allClasses ().size ());
1.193 - assertEquals ("Right # of events", 0, ll.getCount ());
1.194 -
1.195 -
1.196 - ic.remove (inst);
1.197 - assertEquals ("Filtered out3", null, lookup.lookupItem (t));
1.198 - assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
1.199 - assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
1.200 - assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
1.201 - assertEquals ("Of course it is not there", null, lookup.lookup (theQuery));
1.202 - assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
1.203 - }
1.204 -
1.205 - public void testTheBehaviourAsRequestedByDavidAndDescribedByJesse () throws Exception {
1.206 - class C implements Runnable, java.io.Serializable {
1.207 - public void run () {}
1.208 - }
1.209 - Object c = new C();
1.210 - Lookup l1 = Lookups.singleton(c);
1.211 - Lookup l2 = Lookups.exclude(l1, new Class[] {Runnable.class});
1.212 - assertNull(l2.lookup(Runnable.class));
1.213 - assertEquals(c, l2.lookup(java.io.Serializable.class));
1.214 - }
1.215 -
1.216 - public void testTheBehaviourAsRequestedByDavidAndDescribedByJesseWithUsageOfResult () throws Exception {
1.217 - class C implements Runnable, java.io.Serializable {
1.218 - public void run () {}
1.219 - }
1.220 - Object c = new C();
1.221 - Lookup l1 = Lookups.singleton(c);
1.222 - Lookup l2 = Lookups.exclude(l1, new Class[] {Runnable.class});
1.223 -
1.224 - Lookup.Result run = l2.lookup (new Lookup.Template (Runnable.class));
1.225 - Lookup.Result ser = l2.lookup (new Lookup.Template (java.io.Serializable.class));
1.226 -
1.227 - assertEquals ("Runnables filtered out", 0, run.allItems ().size ());
1.228 - assertEquals ("One serialiazble", 1, ser.allItems ().size ());
1.229 - assertEquals ("And it is c", c, ser.allInstances ().iterator ().next ());
1.230 - }
1.231 -}