KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.util.Lookup;
29
30 /**
31  * Tests for class SimpleLookup.
32  * @author David Strupl
33  */

34 public class SimpleLookupTest extends NbTestCase {
35
36     public SimpleLookupTest(String JavaDoc testName) {
37         super(testName);
38     }
39
40     /**
41      * Simple tests testing singleton lookup.
42      */

43     public void testSingleton() {
44         //
45
Object JavaDoc orig = new Object JavaDoc();
46         Lookup p1 = Lookups.singleton(orig);
47         Object JavaDoc obj = p1.lookup(Object JavaDoc.class);
48         assertTrue(obj == orig);
49         assertNull(p1.lookup(String JavaDoc.class));
50         assertTrue(orig == p1.lookup(Object JavaDoc.class)); // 2nd time, still the same?
51
//
52
Lookup p2 = Lookups.singleton("test");
53         assertNotNull(p2.lookup(Object JavaDoc.class));
54         assertNotNull(p2.lookup(String JavaDoc.class));
55         assertNotNull(p2.lookup(java.io.Serializable JavaDoc.class));
56     }
57     
58     /**
59      * Simple tests testing fixed lookup.
60      */

61     public void testFixed() {
62         //
63
Object JavaDoc[] orig = new Object JavaDoc[] { new Object JavaDoc(), new Object JavaDoc() };
64         Lookup p1 = Lookups.fixed(orig);
65         Object JavaDoc obj = p1.lookup(Object JavaDoc.class);
66         assertTrue(obj == orig[0] || obj == orig[1]);
67         assertNull(p1.lookup(String JavaDoc.class));
68         //
69
String JavaDoc[] s = new String JavaDoc[] { "test1", "test2" };
70         Lookup p2 = Lookups.fixed(s);
71         Object JavaDoc obj2 = p2.lookup(Object JavaDoc.class);
72         assertNotNull(obj2);
73         if (obj2 != s[0] && obj2 != s[1]) {
74             fail("Returned objects are not the originals");
75         }
76         assertNotNull(p2.lookup(String JavaDoc.class));
77         assertNotNull(p2.lookup(java.io.Serializable JavaDoc.class));
78         Lookup.Template<String JavaDoc> t = new Lookup.Template<String JavaDoc>(String JavaDoc.class);
79         Lookup.Result<String JavaDoc> r = p2.lookup(t);
80         Collection JavaDoc<? extends String JavaDoc> all = r.allInstances();
81         assertTrue(all.size() == 2);
82         for (String JavaDoc o : all) {
83             assertTrue("allInstances contains wrong objects", o == s[0] || o == s[1]);
84         }
85         
86         try {
87             Lookups.fixed(new Object JavaDoc[] {null});
88             fail("No nulls are allowed");
89         } catch (NullPointerException JavaDoc ex) {
90             // ok, NPE is what we want
91
}
92     }
93     
94     /**
95      * Simple tests testing converting lookup.
96      */

97     public void testConverting() {
98         //
99
String JavaDoc[] orig = new String JavaDoc[] { TestConvertor.TEST1, TestConvertor.TEST2 };
100         TestConvertor convertor = new TestConvertor();
101         Lookup p1 = Lookups.fixed(orig, convertor);
102         assertNull("Converting from String to Integer - it should not find String in result", p1.lookup(String JavaDoc.class));
103         assertNotNull(p1.lookup(Integer JavaDoc.class));
104         assertNotNull(p1.lookup(Integer JavaDoc.class));
105         assertTrue("Convertor should be called only once.", convertor.getNumberOfConvertCalls() == 1);
106         Lookup.Template<Integer JavaDoc> t = new Lookup.Template<Integer JavaDoc>(Integer JavaDoc.class);
107         Lookup.Result<Integer JavaDoc> r = p1.lookup(t);
108         Collection JavaDoc<? extends Integer JavaDoc> all = r.allInstances();
109         assertTrue(all.size() == 2);
110         for (int i : all) {
111             assertTrue("allInstances contains wrong objects", i == TestConvertor.t1 || i == TestConvertor.t2);
112         }
113     }
114     
115     private static class TestConvertor implements InstanceContent.Convertor<String JavaDoc,Integer JavaDoc> {
116         static final String JavaDoc TEST1 = "test1";
117         static final int t1 = 1;
118         static final String JavaDoc TEST2 = "test2";
119         static final int t2 = 2;
120         
121         private int numberOfConvertCalls = 0;
122         
123         public Integer JavaDoc convert(String JavaDoc obj) {
124             numberOfConvertCalls++;
125             if (obj.equals(TEST1)) {
126                 return t1;
127             }
128             if (obj.equals(TEST2)) {
129                 return t2;
130             }
131             throw new IllegalArgumentException JavaDoc();
132         }
133         
134         public String JavaDoc displayName(String JavaDoc obj) {
135             return obj;
136         }
137         
138         public String JavaDoc id(String JavaDoc obj) {
139             if (obj.equals(TEST1)) {
140                 return TEST1;
141             }
142             if (obj.equals(TEST2)) {
143                 return TEST2;
144             }
145             return null;
146         }
147         
148         public Class JavaDoc<? extends Integer JavaDoc> type(String JavaDoc obj) {
149             return Integer JavaDoc.class;
150         }
151         
152         int getNumberOfConvertCalls() {
153             return numberOfConvertCalls;
154         }
155     }
156     
157     public void testLookupItem() {
158         SomeInst inst = new SomeInst();
159         Lookup.Item item = Lookups.lookupItem(inst, "XYZ");
160         
161         assertTrue("Wrong instance", item.getInstance() == inst);
162         assertTrue("Wrong instance class", item.getType() == inst.getClass());
163         assertEquals("Wrong id", "XYZ", item.getId());
164
165         item = Lookups.lookupItem(inst, null);
166         assertNotNull("Id must never be null", item.getId());
167     }
168
169     public void testLookupItemEquals() {
170         SomeInst instA = new SomeInst();
171         SomeInst instB = new SomeInst();
172         Lookup.Item itemA = Lookups.lookupItem(instA, null);
173         Lookup.Item itemB = Lookups.lookupItem(instB, null);
174         
175         assertTrue("Lookup items shouldn't be equal", !itemA.equals(itemB) && !itemB.equals(itemA));
176
177         itemA = Lookups.lookupItem(instA, null);
178         itemB = Lookups.lookupItem(instA, null); // same instance
179

180         assertTrue("Lookup items should be equal", itemA.equals(itemB) && itemB.equals(itemA));
181         assertTrue("Lookup items hashcode should be same", itemA.hashCode() == itemB.hashCode());
182
183         itemA = Lookups.lookupItem(new String JavaDoc("VOKURKA"), null);
184         itemB = Lookups.lookupItem(new String JavaDoc("VOKURKA"), null);
185
186         assertTrue("Lookup items shouldn't be equal (2)", !itemA.equals(itemB) && !itemB.equals(itemA));
187     }
188     
189     public void testAllClassesIssue42399 () throws Exception JavaDoc {
190         Object JavaDoc[] arr = { "Ahoj", new Object JavaDoc () };
191         
192         Lookup l = Lookups.fixed (arr);
193         
194         Set JavaDoc<Class JavaDoc<? extends Object JavaDoc>> s = l.lookup(new Lookup.Template<Object JavaDoc>(Object JavaDoc.class)).allClasses();
195         
196         assertEquals ("Two there", 2, s.size ());
197         assertTrue ("Contains Object.class", s.contains (Object JavaDoc.class));
198         assertTrue ("Contains string", s.contains (String JavaDoc.class));
199         
200     }
201
202     public void testLookupItemEarlyInitializationProblem() {
203         InstanceContent ic = new InstanceContent();
204         AbstractLookup al = new AbstractLookup(ic);
205         LI item = new LI();
206         List JavaDoc<AbstractLookup.Pair> pairs1 = new ArrayList JavaDoc<AbstractLookup.Pair>();
207         List JavaDoc<AbstractLookup.Pair> pairs2 = new ArrayList JavaDoc<AbstractLookup.Pair>();
208         
209         assertEquals("Item's instance shouldn't be requested", 0, item.cnt);
210
211         pairs1.add(new ItemPair<Object JavaDoc>(Lookups.<Object JavaDoc>lookupItem(new SomeInst(), null)));
212         pairs1.add(new ItemPair<Object JavaDoc>(item));
213         pairs1.add(new ItemPair<Object JavaDoc>(Lookups.lookupItem(new Object JavaDoc(), null)));
214
215         pairs2.add(new ItemPair<Object JavaDoc>(item));
216         pairs2.add(new ItemPair<Object JavaDoc>(Lookups.lookupItem(new Object JavaDoc(), null)));
217
218         ic.setPairs(pairs1);
219         ic.setPairs(pairs2);
220
221         assertEquals("Item's instance shouldn't be requested when added to lookup", 0, item.cnt);
222         
223         LI item2 = al.lookup(LI.class);
224         assertEquals("Item's instance should be requested", 1, item.cnt);
225     }
226
227     public void testConvenienceMethods() throws Exception JavaDoc {
228         // Just check signatures and basic behavior of #73848.
229
Lookup l = Lookups.fixed(new Object JavaDoc[] {1, "hello", 2, "goodbye"});
230         Collection JavaDoc<? extends Integer JavaDoc> ints = l.lookupAll(Integer JavaDoc.class);
231         assertEquals(Arrays.asList(new Integer JavaDoc[] {1, 2}), new ArrayList JavaDoc<Integer JavaDoc>(ints));
232         Lookup.Result<Integer JavaDoc> r = l.lookupResult(Integer JavaDoc.class);
233         ints = r.allInstances();
234         assertEquals(Arrays.asList(new Integer JavaDoc[] {1, 2}), new ArrayList JavaDoc<Integer JavaDoc>(ints));
235     }
236     
237     private static class SomeInst { }
238     
239     private static class LI extends Lookup.Item<Object JavaDoc> {
240
241         public long cnt = 0;
242         
243         public String JavaDoc getDisplayName() {
244             return getId();
245         }
246
247         public String JavaDoc getId() {
248             return getClass() + "@" + hashCode();
249         }
250
251         public Object JavaDoc getInstance() {
252             cnt++;
253             return this;
254         }
255
256         public Class JavaDoc<? extends Object JavaDoc> getType() {
257             return getClass();
258         }
259     } // End of LI class
260

261     private static class ItemPair<T> extends AbstractLookup.Pair<T> {
262         
263         private AbstractLookup.Item<T> item;
264         
265         public ItemPair(Lookup.Item<T> i) {
266             this.item = i;
267         }
268
269         protected boolean creatorOf(Object JavaDoc obj) {
270             return item.getInstance() == obj;
271         }
272
273         public String JavaDoc getDisplayName() {
274             return item.getDisplayName ();
275         }
276
277         public String JavaDoc getId() {
278             return item.getId ();
279         }
280
281         public T getInstance() {
282             return item.getInstance ();
283         }
284
285         public Class JavaDoc<? extends T> getType() {
286             return item.getType ();
287         }
288
289         protected boolean instanceOf(Class JavaDoc<?> c) {
290             return c.isAssignableFrom(getType());
291         }
292
293         public boolean equals (Object JavaDoc o) {
294             if (o instanceof ItemPair) {
295                 ItemPair p = (ItemPair)o;
296                 return item.equals (p.item);
297             }
298             return false;
299         }
300
301         public int hashCode () {
302             return item.hashCode ();
303         }
304     } // end of ItemPair
305
}
306
Popular Tags