KickJava   Java API By Example, From Geeks To Geeks.

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


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
30 public class InitializationBug44134Test extends NbTestCase {
31     public InitializationBug44134Test (java.lang.String JavaDoc testName) {
32         super(testName);
33     }
34
35     public static void main(java.lang.String JavaDoc[] args) {
36         junit.textui.TestRunner.run(new NbTestSuite(InitializationBug44134Test.class));
37     }
38
39     public void testThereShouldBe18Integers () throws Exception JavaDoc {
40         FooManifestLookup foo = new FooManifestLookup ();
41         
42         Collection items = foo.lookup (new Lookup.Template (Integer JavaDoc.class)).allItems ();
43         
44         assertEquals ("18 of them", 18, items.size ());
45         
46         Iterator it = items.iterator ();
47         while (it.hasNext()) {
48             Lookup.Item t = (Lookup.Item)it.next ();
49             assertEquals ("Is Integer", Integer JavaDoc.class, t.getInstance ().getClass ());
50         }
51     }
52
53     
54     public class FooManifestLookup extends AbstractLookup {
55         public FooManifestLookup() {
56             super();
57         }
58         
59         protected void initialize() {
60             for (int i=0; i<18; i++) {
61                 try {
62                     String JavaDoc id= "__" + i;
63                     
64                     addPair(new FooLookupItem(new Integer JavaDoc(i),id));
65                 }
66                 catch (Exception JavaDoc e) {
67                 }
68             }
69         }
70         
71         public class FooLookupItem extends AbstractLookup.Pair {
72             public FooLookupItem(Integer JavaDoc data, String JavaDoc id) {
73                 super();
74                 this.data=data;
75                 this.id=id;
76             }
77             
78             protected boolean creatorOf(Object JavaDoc obj) {
79                 return obj == data;
80             }
81             
82             public String JavaDoc getDisplayName() {
83                 return data.toString();
84             }
85             
86             public Class JavaDoc getType () {
87                 return Integer JavaDoc.class;
88             }
89             
90             protected boolean instanceOf (Class JavaDoc c) {
91                 return c.isInstance(data);
92             }
93             
94             public Object JavaDoc getInstance() {
95                 return data;
96             }
97             
98             public String JavaDoc getId() {
99                 return id;
100             }
101             
102             private Integer JavaDoc data;
103             private String JavaDoc id;
104         }
105     }
106     
107 }
108
Popular Tags