KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.util.*;
23
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.*;
26 import junit.framework.*;
27 import org.netbeans.junit.*;
28 import java.io.Serializable JavaDoc;
29 import org.openide.util.io.NbMarshalledObject;
30
31 public class AbstractLookupTest extends AbstractLookupBaseHid implements AbstractLookupBaseHid.Impl {
32     public AbstractLookupTest(java.lang.String JavaDoc testName) {
33         super(testName, null);
34     }
35
36     //
37
// Impl of AbstractLookupBaseHid.Impl
38
//
39

40     /** Creates the initial abstract lookup.
41      */

42     public Lookup createInstancesLookup (InstanceContent ic) {
43         return new AbstractLookup (ic, new InheritanceTree ());
44     }
45     
46     /** Creates an lookup for given lookup. This class just returns
47      * the object passed in, but subclasses can be different.
48      * @param lookup in lookup
49      * @return a lookup to use
50      */

51     public Lookup createLookup (Lookup lookup) {
52         return lookup;
53     }
54
55     public void clearCaches () {
56     }
57     
58     public static void main(java.lang.String JavaDoc[] args) {
59         junit.textui.TestRunner.run(new NbTestSuite(AbstractLookupTest.class));
60     }
61     
62     static class LkpResultCanBeGargageCollectedAndClearsTheResult extends AbstractLookup {
63         public int cleared;
64         public int dirty;
65
66         synchronized boolean cleanUpResult (Template t) {
67             boolean res = super.cleanUpResult (t);
68             if (res) {
69                 cleared++;
70             } else {
71                 dirty++;
72             }
73
74             notifyAll ();
75
76             return res;
77         }
78     }
79     public void testResultCanBeGargageCollectedAndClearsTheResult () throws Exception JavaDoc {
80         LkpResultCanBeGargageCollectedAndClearsTheResult lkp = new LkpResultCanBeGargageCollectedAndClearsTheResult ();
81         assertSize ("24 for AbstractLookup, 8 for two ints", 32, lkp);
82         synchronized (lkp) {
83             Lookup.Result res = lkp.lookup (new Lookup.Template (getClass ()));
84             res.allItems();
85             
86             WeakReference JavaDoc ref = new WeakReference JavaDoc (res);
87             res = null;
88             assertGC ("Reference can get cleared", ref);
89          
90             // wait till we
91
while (lkp.cleared == 0 && lkp.dirty == 0) {
92                 lkp.wait ();
93             }
94             
95             assertEquals ("No dirty cleanups", 0, lkp.dirty);
96             assertEquals ("One final cleanup", 1, lkp.cleared);
97         }
98         //assertSize ("Everything has been cleaned to original size", 32, lkp);
99

100     }
101     
102     public void testPairCannotBeUsedInMoreThanOneLookupAtOnce () throws Exception JavaDoc {
103         /** Simple pair with no data */
104         class EmptyPair extends AbstractLookup.Pair {
105             protected boolean creatorOf(Object JavaDoc obj) { return false; }
106             public String JavaDoc getDisplayName() { return "Empty"; }
107             public String JavaDoc getId() { return "empty"; }
108             public Object JavaDoc getInstance() { return null; }
109             public Class JavaDoc getType() { return Object JavaDoc.class; }
110             protected boolean instanceOf(Class JavaDoc c) { return c == getType (); }
111         } // end of EmptyPair
112

113         AbstractLookup.Content c1 = new AbstractLookup.Content ();
114         AbstractLookup.Content c2 = new AbstractLookup.Content ();
115         AbstractLookup l1 = new AbstractLookup (c1);
116         AbstractLookup l2 = new AbstractLookup (c2);
117         
118         EmptyPair empty = new EmptyPair ();
119         c1.addPair (empty);
120         Lookup.Result res = l1.lookup (new Lookup.Template (Object JavaDoc.class));
121         assertEquals (
122             "Pair is really found", empty,
123             res.allItems ().iterator().next ()
124         );
125         try {
126             c2.addPair (empty);
127             fail ("It should not be possible to add pair to two lookups");
128         } catch (IllegalStateException JavaDoc ex) {
129             // ok, exception is fine
130
}
131         assertEquals (
132             "L2 is still empty", Collections.EMPTY_LIST,
133             new ArrayList (l2.lookup (new Lookup.Template (Object JavaDoc.class)).allItems ())
134         );
135     }
136     
137     public void testInitializationCanBeDoneFromAnotherThread () {
138         class MyLkp extends AbstractLookup implements Runnable JavaDoc {
139             private InstanceContent ic;
140             private boolean direct;
141             
142             public MyLkp (boolean direct) {
143                 this (direct, new InstanceContent ());
144             }
145                 
146             private MyLkp (boolean direct, InstanceContent ic) {
147                 super (ic);
148                 this.direct = direct;
149                 this.ic = ic;
150             }
151             
152             protected void initialize () {
153                 if (direct) {
154                     run ();
155                 } else {
156                     RequestProcessor.getDefault().post (this).waitFinished ();
157                 }
158             }
159             
160             public void run () {
161                 ic.add (this);
162                 ic.remove (this);
163                 ic.set (Collections.nCopies(10, this), null);
164                 ic.set (Collections.EMPTY_LIST, null);
165                 ic.add (AbstractLookupTest.this);
166             }
167         }
168         
169         assertEquals ("The test should be there", this, new MyLkp (true).lookup (Object JavaDoc.class));
170         assertEquals ("and in async mode as well", this, new MyLkp (false).lookup (Object JavaDoc.class));
171     }
172     
173     public void testBeforeLookupIsCalled () {
174         class BeforeL extends AbstractLookup {
175             public ArrayList list = new ArrayList ();
176             public String JavaDoc toAdd;
177             public InstanceContent ic;
178             
179             public BeforeL () {
180                 this (new InstanceContent ());
181             }
182             
183             private BeforeL (InstanceContent c) {
184                 super (c);
185                 this.ic = c;
186             }
187         
188             protected void beforeLookup (Template t) {
189                 if (toAdd != null) {
190                     list.add (0, new SerialPair (toAdd));
191                     setPairs (list);
192                 } else {
193                     ic.add (new Integer JavaDoc (1));
194                 }
195             }
196         }
197         
198         BeforeL lookup = new BeforeL ();
199         
200         lookup.toAdd = "First";
201         assertEquals ("First if found", "First", lookup.lookup (String JavaDoc.class));
202         
203         lookup.toAdd = "2";
204         assertEquals ("2 is not first", "2", lookup.lookup (String JavaDoc.class));
205         
206         Lookup.Result res = lookup.lookup (new Lookup.Template (Object JavaDoc.class));
207         for (int i = 3; i < 20; i++) {
208             lookup.toAdd = String.valueOf (i);
209             assertEquals (i + " items are now there", i, res.allInstances ().size ());
210         }
211         for (int i = 20; i < 35; i++) {
212             lookup.toAdd = String.valueOf (i);
213             assertEquals (i + " items are now there", i, res.allItems ().size ());
214         }
215         
216         assertEquals ("Just strings are there now", 1, res.allClasses ().size ());
217         lookup.toAdd = null; // this will add integer
218
assertEquals ("Two classes now", 2, res.allClasses ().size ());
219     }
220
221     public void testInconsistentAfterDeserIssue71744() throws Exception JavaDoc {
222         InheritanceTree inhTree = new InheritanceTree();
223
224         AbstractLookup al = new AbstractLookup(new AbstractLookup.Content(), inhTree);
225         {
226
227             Collection r = al.lookup(new Lookup.Template(Integer JavaDoc.class)).allInstances();
228             assertEquals("None", 0, r.size());
229         }
230
231         ICP item = new ICP(new Integer JavaDoc(10));
232         al.addPair(item);
233         al.removePair(item);
234
235         NbMarshalledObject mar = new NbMarshalledObject(al);
236
237         AbstractLookup newLookup = (AbstractLookup)mar.get();
238
239         newLookup.lookup(Number JavaDoc.class);
240
241
242         newLookup.addPair(new ICP(new Long JavaDoc(20)));
243
244         {
245
246             Collection r = newLookup.lookup(new Lookup.Template(Number JavaDoc.class)).allInstances();
247             assertEquals("one", 1, r.size());
248 /*
249             Iterator it = r.iterator();
250             assertEquals(new Integer(10), it.next());
251             assertEquals(new Long(20), it.next());*/

252         }
253     }
254
255     private static final class ICP extends AbstractLookup.Pair {
256         private Number JavaDoc s;
257
258         public ICP (Number JavaDoc s) {
259             this.s = s;
260         }
261
262
263         protected boolean instanceOf(Class JavaDoc c) {
264             return c.isInstance(s);
265         }
266
267         protected boolean creatorOf(Object JavaDoc obj) {
268             return s == obj;
269         }
270
271         public Object JavaDoc getInstance() {
272             return s;
273         }
274
275         public Class JavaDoc getType() {
276             return s.getClass();
277         }
278
279         public String JavaDoc getId() {
280             return s.toString();
281         }
282
283         public String JavaDoc getDisplayName() {
284             return getId();
285         }
286
287     }
288 }
289
Popular Tags