Warnings.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
30 * If you wish your version of this file to be governed by only the CDDL
31 * or only the GPL Version 2, indicate your decision by adding
32 * "[Contributor] elects to include this software in this distribution
33 * under the [CDDL or GPL Version 2] license." If you do not indicate a
34 * single choice of license, a recipient has the option to distribute
35 * your version of this file under either the CDDL, the GPL Version 2 or
36 * to extend the choice of license to its licensees as provided above.
37 * However, if you add GPL Version 2 code and therefore, elected the GPL
38 * Version 2 license, then the option applies only if the new code is
39 * made subject to such option by the copyright holder.
42 package org.openide.util.lookup;
44 import java.io.Serializable;
47 import org.netbeans.junit.*;
48 import org.openide.util.Lookup;
49 import org.openide.util.LookupEvent;
50 import org.openide.util.LookupListener;
52 /** Runs all NbLookupTest tests on ProxyLookup and adds few additional.
54 @SuppressWarnings("unchecked") // XXX ought to be corrected, just a lot of them
55 public class LookupsProxyTest extends AbstractLookupBaseHid
56 implements AbstractLookupBaseHid.Impl {
57 public LookupsProxyTest(java.lang.String testName) {
58 super(testName, null);
61 public static void main(java.lang.String[] args) {
62 junit.textui.TestRunner.run(new NbTestSuite (LookupsProxyTest.class));
65 /** Creates an lookup for given lookup. This class just returns
66 * the object passed in, but subclasses can be different.
67 * @param lookup in lookup
68 * @return a lookup to use
70 public Lookup createLookup (final Lookup lookup) {
71 return org.openide.util.lookup.Lookups.proxy (
72 new Lookup.Provider () {
73 public Lookup getLookup () {
80 public Lookup createInstancesLookup (InstanceContent ic) {
81 return new AbstractLookup (ic);
84 public void clearCaches () {
89 /** Check whether setLookups method does not fire when there is no
90 * change in the lookups.
92 public void testProxyListener () {
93 Changer ch = new Changer (Lookup.EMPTY);
95 Lookup lookup = Lookups.proxy(ch);
96 Lookup.Result res = lookup.lookup (new Lookup.Template (Object.class));
99 res.addLookupListener (ll);
100 Collection allRes = res.allInstances ();
102 ch.setLookup (new AbstractLookup (new InstanceContent ())); // another empty lookup
103 lookup.lookup (Object.class); // does the refresh
105 assertEquals("Replacing an empty by empty does not generate an event", 0, ll.getCount());
107 InstanceContent content = new InstanceContent ();
108 AbstractLookup del = new AbstractLookup (content);
111 lookup.lookup (Object.class);
113 if (ll.getCount () != 1) {
114 fail ("Changing lookups with different content generates an event");
118 lookup.lookup (Object.class);
120 if (ll.getCount () != 0) {
121 fail ("Not changing the lookups does not generate any event");
126 public void testListeningAndQueryingByTwoListenersInstancesSetLookups() {
127 doListeningAndQueryingByTwoListenersSetLookups(0, 1, false);
129 public void testListeningAndQueryingByTwoListenersClassesSetLookups() {
130 doListeningAndQueryingByTwoListenersSetLookups(1, 1, false);
132 public void testListeningAndQueryingByTwoListenersItemsSetLookups() {
133 doListeningAndQueryingByTwoListenersSetLookups(2, 1, false);
136 public void testListeningAndQueryingByTwoListenersInstancesSetLookups2() {
137 doListeningAndQueryingByTwoListenersSetLookups(0, 2, false);
139 public void testListeningAndQueryingByTwoListenersClassesSetLookups2() {
140 doListeningAndQueryingByTwoListenersSetLookups(1, 2, false);
142 public void testListeningAndQueryingByTwoListenersItemsSetLookups2() {
143 doListeningAndQueryingByTwoListenersSetLookups(2, 2, false);
146 public void testListeningAndQueryingByTwoListenersInstancesSetLookupsWithProxy() {
147 doListeningAndQueryingByTwoListenersSetLookups(0, 1, true);
149 public void testListeningAndQueryingByTwoListenersClassesSetLookupsWithProxy() {
150 doListeningAndQueryingByTwoListenersSetLookups(1, 1, true);
152 public void testListeningAndQueryingByTwoListenersItemsSetLookupsWithProxy() {
153 doListeningAndQueryingByTwoListenersSetLookups(2, 1, true);
156 public void testListeningAndQueryingByTwoListenersInstancesSetLookups2WithProxy() {
157 doListeningAndQueryingByTwoListenersSetLookups(0, 2, true);
159 public void testListeningAndQueryingByTwoListenersClassesSetLookups2WithProxy() {
160 doListeningAndQueryingByTwoListenersSetLookups(1, 2, true);
162 public void testListeningAndQueryingByTwoListenersItemsSetLookups2WithProxy() {
163 doListeningAndQueryingByTwoListenersSetLookups(2, 2, true);
166 /* XXX: these are pretty slow, seems there is a performance problem 2^22
167 public void testListeningAndQueryingByTwoListenersInstancesSetLookups22() {
168 doListeningAndQueryingByTwoListenersSetLookups(0, 22);
170 public void testListeningAndQueryingByTwoListenersClassesSetLookups22() {
171 doListeningAndQueryingByTwoListenersSetLookups(1, 22);
173 public void testListeningAndQueryingByTwoListenersItemsSetLookups22() {
174 doListeningAndQueryingByTwoListenersSetLookups(2, 22);
178 private void doListeningAndQueryingByTwoListenersSetLookups(final int type, int depth, boolean cacheOnTop) {
179 Changer orig = new Changer(Lookup.EMPTY);
180 Lookup on = Lookups.proxy(orig);
183 while (--depth > 0) {
184 Changer next = new Changer(on);
185 on = Lookups.proxy(next);
189 final Lookup lookup = cacheOnTop ? new ProxyLookup(new Lookup[] { on }) : on;
191 class L implements LookupListener {
192 Lookup.Result integer = lookup.lookup(new Lookup.Template(Integer.class));
193 Lookup.Result number = lookup.lookup(new Lookup.Template(Number.class));
194 Lookup.Result serial = lookup.lookup(new Lookup.Template(Serializable.class));
197 integer.addLookupListener(this);
198 number.addLookupListener(this);
199 serial.addLookupListener(this);
204 public void resultChanged(LookupEvent ev) {
205 Collection c1 = get(type, integer);
206 Collection c2 = get(type, number);
207 Collection c3 = get(type, serial);
209 assertEquals("round " + round + " c1 vs. c2", c1, c2);
210 assertEquals("round " + round + " c1 vs. c3", c1, c3);
211 assertEquals("round " + round + " c2 vs. c3", c2, c3);
216 private Collection get(int type, Lookup.Result res) {
219 case 0: c = res.allInstances(); break;
220 case 1: c = res.allClasses(); break;
221 case 2: c = res.allItems(); break;
222 default: c = null; fail("Type: " + type); break;
226 return new ArrayList(c);
230 L listener = new L();
231 listener.resultChanged(null);
232 ArrayList arr = new ArrayList();
233 for(int i = 0; i < 100; i++) {
234 arr.add(new Integer(i));
236 orig.lookup = Lookups.fixed(arr.toArray());
238 first.lookup((Class)null);
241 assertEquals("3x100+1 checks", 301, listener.round);
245 public void testRefreshWithoutAllInstances103300 () {
246 Changer ch = new Changer (Lookup.EMPTY);
248 Lookup lookup = Lookups.proxy(ch);
250 ch.setLookup (new AbstractLookup (new InstanceContent ())); // another empty lookup
251 assertNull("Nothing there", lookup.lookup (Object.class)); // does the refresh
253 InstanceContent content = new InstanceContent ();
254 AbstractLookup del = new AbstractLookup (content);
257 assertEquals("Can see me", this, lookup.lookup (Object.class));
260 assertEquals("Still can see me", this, lookup.lookup (Object.class));
262 assertEquals("I am visible", this, lookup.lookup(LookupsProxyTest.class));
266 private static final class Changer implements Lookup.Provider {
267 private Lookup lookup;
269 public Changer (Lookup lookup) {
273 public void setLookup (Lookup lookup) {
274 this.lookup = lookup;
277 public Lookup getLookup() {