KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > lookup > LookupsProxyTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util.lookup;
21
22 import java.io.Serializable JavaDoc;
23 import org.openide.util.*;
24
25 import java.util.*;
26 import junit.framework.*;
27 import org.netbeans.junit.*;
28
29 /** Runs all NbLookupTest tests on ProxyLookup and adds few additional.
30  */

31 public class LookupsProxyTest extends AbstractLookupBaseHid
32 implements AbstractLookupBaseHid.Impl {
33     public LookupsProxyTest(java.lang.String JavaDoc testName) {
34         super(testName, null);
35     }
36
37     public static void main(java.lang.String JavaDoc[] args) {
38         junit.textui.TestRunner.run(new NbTestSuite (LookupsProxyTest.class));
39     }
40     
41     /** Creates an lookup for given lookup. This class just returns
42      * the object passed in, but subclasses can be different.
43      * @param lookup in lookup
44      * @return a lookup to use
45      */

46     public Lookup createLookup (final Lookup lookup) {
47         return org.openide.util.lookup.Lookups.proxy (
48             new Lookup.Provider () {
49                 public Lookup getLookup () {
50                     return lookup;
51                 }
52             }
53         );
54     }
55     
56     public Lookup createInstancesLookup (InstanceContent ic) {
57         return new AbstractLookup (ic);
58     }
59
60     public void clearCaches () {
61     }
62     
63     
64    
65     /** Check whether setLookups method does not fire when there is no
66      * change in the lookups.
67      */

68     public void testProxyListener () {
69         Changer ch = new Changer (Lookup.EMPTY);
70         
71         Lookup lookup = Lookups.proxy(ch);
72         Lookup.Result res = lookup.lookup (new Lookup.Template (Object JavaDoc.class));
73         
74         LL ll = new LL ();
75         res.addLookupListener (ll);
76         Collection allRes = res.allInstances ();
77
78         ch.setLookup (new AbstractLookup (new InstanceContent ())); // another empty lookup
79
lookup.lookup (Object JavaDoc.class); // does the refresh
80

81         assertEquals("Replacing an empty by empty does not generate an event", 0, ll.getCount());
82         
83         InstanceContent content = new InstanceContent ();
84         AbstractLookup del = new AbstractLookup (content);
85         content.add (this);
86         ch.setLookup (del);
87         lookup.lookup (Object JavaDoc.class);
88         
89         if (ll.getCount () != 1) {
90             fail ("Changing lookups with different content generates an event");
91         }
92         
93         ch.setLookup (del);
94         lookup.lookup (Object JavaDoc.class);
95         
96         if (ll.getCount () != 0) {
97            fail ("Not changing the lookups does not generate any event");
98         }
99     }
100
101     
102     public void testListeningAndQueryingByTwoListenersInstancesSetLookups() {
103         doListeningAndQueryingByTwoListenersSetLookups(0, 1, false);
104     }
105     public void testListeningAndQueryingByTwoListenersClassesSetLookups() {
106         doListeningAndQueryingByTwoListenersSetLookups(1, 1, false);
107     }
108     public void testListeningAndQueryingByTwoListenersItemsSetLookups() {
109         doListeningAndQueryingByTwoListenersSetLookups(2, 1, false);
110     }
111     
112     public void testListeningAndQueryingByTwoListenersInstancesSetLookups2() {
113         doListeningAndQueryingByTwoListenersSetLookups(0, 2, false);
114     }
115     public void testListeningAndQueryingByTwoListenersClassesSetLookups2() {
116         doListeningAndQueryingByTwoListenersSetLookups(1, 2, false);
117     }
118     public void testListeningAndQueryingByTwoListenersItemsSetLookups2() {
119         doListeningAndQueryingByTwoListenersSetLookups(2, 2, false);
120     }
121
122     public void testListeningAndQueryingByTwoListenersInstancesSetLookupsWithProxy() {
123         doListeningAndQueryingByTwoListenersSetLookups(0, 1, true);
124     }
125     public void testListeningAndQueryingByTwoListenersClassesSetLookupsWithProxy() {
126         doListeningAndQueryingByTwoListenersSetLookups(1, 1, true);
127     }
128     public void testListeningAndQueryingByTwoListenersItemsSetLookupsWithProxy() {
129         doListeningAndQueryingByTwoListenersSetLookups(2, 1, true);
130     }
131     
132     public void testListeningAndQueryingByTwoListenersInstancesSetLookups2WithProxy() {
133         doListeningAndQueryingByTwoListenersSetLookups(0, 2, true);
134     }
135     public void testListeningAndQueryingByTwoListenersClassesSetLookups2WithProxy() {
136         doListeningAndQueryingByTwoListenersSetLookups(1, 2, true);
137     }
138     public void testListeningAndQueryingByTwoListenersItemsSetLookups2WithProxy() {
139         doListeningAndQueryingByTwoListenersSetLookups(2, 2, true);
140     }
141     
142     /* XXX: these are pretty slow, seems there is a performance problem 2^22
143     public void testListeningAndQueryingByTwoListenersInstancesSetLookups22() {
144         doListeningAndQueryingByTwoListenersSetLookups(0, 22);
145     }
146     public void testListeningAndQueryingByTwoListenersClassesSetLookups22() {
147         doListeningAndQueryingByTwoListenersSetLookups(1, 22);
148     }
149     public void testListeningAndQueryingByTwoListenersItemsSetLookups22() {
150         doListeningAndQueryingByTwoListenersSetLookups(2, 22);
151     }
152      */

153     
154     private void doListeningAndQueryingByTwoListenersSetLookups(final int type, int depth, boolean cacheOnTop) {
155         Changer orig = new Changer(Lookup.EMPTY);
156         Lookup on = Lookups.proxy(orig);
157         Lookup first = on;
158         
159         while (--depth > 0) {
160             Changer next = new Changer(on);
161             on = Lookups.proxy(next);
162         }
163         
164         
165         final Lookup lookup = cacheOnTop ? new ProxyLookup(new Lookup[] { on }) : on;
166         
167         class L implements LookupListener {
168             Lookup.Result integer = lookup.lookup(new Lookup.Template(Integer JavaDoc.class));
169             Lookup.Result number = lookup.lookup(new Lookup.Template(Number JavaDoc.class));
170             Lookup.Result serial = lookup.lookup(new Lookup.Template(Serializable JavaDoc.class));
171             
172             {
173                 integer.addLookupListener(this);
174                 number.addLookupListener(this);
175                 serial.addLookupListener(this);
176             }
177             
178             int round;
179             
180             public void resultChanged(LookupEvent ev) {
181                 Collection c1 = get(type, integer);
182                 Collection c2 = get(type, number);
183                 Collection c3 = get(type, serial);
184                 
185                 assertEquals("round " + round + " c1 vs. c2", c1, c2);
186                 assertEquals("round " + round + " c1 vs. c3", c1, c3);
187                 assertEquals("round " + round + " c2 vs. c3", c2, c3);
188                 
189                 round++;
190             }
191
192             private Collection get(int type, Lookup.Result res) {
193                 Collection c;
194                 switch(type) {
195                     case 0: c = res.allInstances(); break;
196                     case 1: c = res.allClasses(); break;
197                     case 2: c = res.allItems(); break;
198                     default: c = null; fail("Type: " + type); break;
199                 }
200                 
201                 assertNotNull(c);
202                 return new ArrayList(c);
203             }
204         }
205         
206         L listener = new L();
207         listener.resultChanged(null);
208         ArrayList arr = new ArrayList();
209         for(int i = 0; i < 100; i++) {
210             arr.add(new Integer JavaDoc(i));
211             
212             orig.lookup = Lookups.fixed(arr.toArray());
213             // do the refresh
214
first.lookup((Class JavaDoc)null);
215         }
216         
217         assertEquals("3x100+1 checks", 301, listener.round);
218     }
219     
220     private static final class Changer implements Lookup.Provider {
221         private Lookup lookup;
222         
223         public Changer (Lookup lookup) {
224             setLookup (lookup);
225         }
226         
227         public void setLookup (Lookup lookup) {
228             this.lookup = lookup;
229         }
230         
231         public Lookup getLookup() {
232             return lookup;
233         }
234     }
235 }
236
Popular Tags