KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > AskDirectlyTest


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 package org.netbeans;
20
21 import junit.framework.*;
22 import java.net.URL JavaDoc;
23 import java.util.jar.JarFile JavaDoc;
24 import java.util.jar.Manifest JavaDoc;
25 import java.util.jar.Attributes JavaDoc;
26 import java.util.jar.Attributes.Name;
27 import java.util.zip.ZipEntry JavaDoc;
28 import java.io.*;
29 import java.net.MalformedURLException JavaDoc;
30 import java.security.*;
31 import java.security.cert.Certificate JavaDoc;
32 import java.util.*;
33
34 /**
35  *
36  * @author Jaroslav Tulach
37  */

38 public class AskDirectlyTest extends TestCase {
39     String JavaDoc c;
40     String JavaDoc g;
41
42     CountingClassLoader cancel;
43
44     CountingClassLoader global;
45     
46     TopCountingClassLoader top;
47
48     ProxyClassLoader all;
49     
50     public AskDirectlyTest(String JavaDoc testName) {
51         super(testName);
52     }
53
54     protected void setUp() throws Exception JavaDoc {
55         c = "org.openide.util.Cancellable";
56         g = "org.openide.util.datatransfer.TransferListener";
57         
58         top = new TopCountingClassLoader(getClass().getClassLoader().getParent());
59         
60         cancel = new CountingClassLoader (
61             "cancel loader", getClass().getClassLoader(), top, c
62         );
63         
64         global = new CountingClassLoader (
65             "global that delegates to cancel", getClass().getClassLoader(), cancel, g
66         );
67
68        all = new ProxyClassLoader(new ClassLoader JavaDoc[] { cancel, global });
69     }
70
71     public void testLoadingJDKClassDoesNotAsk() throws Exception JavaDoc {
72         
73         assertNotNull ("We can loader JDK class", all.loadClass("java.util.HashSet", true));
74         
75         cancel.assertQuery("No query to our loaders1", 0);
76         global.assertQuery("No query to our loaders2", 0);
77     }
78 /*
79     public void testLoadingNonExistingClassDoesNotAsk() throws Exception {
80         try {
81             all.loadClass("does.not.Exists", true);
82             fail ("This is supposed to fail, as the class does not exists");
83         } catch (ClassNotFoundException ex) {
84             // ok
85         }
86         
87         cancel.assertQuery("No query to our loaders1", 0);
88         global.assertQuery("No query to our loaders2", 0);
89     }
90
91     public void testLoadingClassFromSecondCLDoesNotAsk() throws Exception {
92         Class c = all.loadClass(g, true);
93         assertEquals("Loaded by global loader", global, c.getClassLoader());
94                 
95         cancel.assertQuery("No query to our loader", 0);
96         global.assertQuery("Global asked once", 1);
97     }
98 */

99     public void testLoadingClassFromFirstCLDoesNotAsk() throws Exception JavaDoc {
100         Class JavaDoc c = all.loadClass(this.c, true);
101         assertEquals("Loaded by cancel loader", cancel, c.getClassLoader());
102                 
103         global.assertQuery("No query to our loader", 0);
104         cancel.assertQuery("Cancel asked once", 1);
105     }
106 /*
107     public void testLoadingAllResourcesFromSecondCLDoesNotAsk() throws Exception {
108         Enumeration en = all.getResources(g.replace('.', '/') + ".class");
109         assertTrue("At least one is there", en.hasMoreElements());
110         URL u;
111         while (en.hasMoreElements()) {
112             u = (URL)en.nextElement();
113         }
114                 
115         cancel.assertQuery("No query to our loader", 0);
116         global.assertQuery("Global asked once", 1);
117     }
118
119     public void testLoadingAllResourcesFromFirstCLDoesNotAsk() throws Exception {
120         Enumeration en = all.getResources(c.replace('.', '/') + ".class");
121         assertTrue("At least one is there", en.hasMoreElements());
122         URL u;
123         while (en.hasMoreElements()) {
124             u = (URL)en.nextElement();
125         }
126                 
127         global.assertQuery("No query to our loader", 0);
128         cancel.assertQuery("Cancel asked once", 1);
129     }
130     
131     public void testLoadingResourceFromSecondCLDoesNotAsk() throws Exception {
132         URL u = all.getResource(g.replace('.', '/') + ".class");
133         assertNotNull ("Really found", u);
134                 
135         cancel.assertQuery("No query to our loader", 0);
136         global.assertQuery("Global asked once", 1);
137     }
138
139     public void testLoadingResourceFromFirstCLDoesNotAsk() throws Exception {
140         URL u = all.getResource(c.replace('.', '/') + ".class");
141         assertNotNull ("Really found", u);
142                 
143         global.assertQuery("No query to our loader", 0);
144         cancel.assertQuery("Cancel asked once", 1);
145     }
146     
147     public void testLoadingFromOwnClassLoaderIsOptimizedAsWell() throws Exception {
148         Class clazz = global.loadClass(g);
149         assertNotNull("Class found", clazz);
150         assertSame("Classloader is the right one", global, clazz.getClassLoader());
151         
152         global.assertQuery("One query to our loader", 1);
153         cancel.assertQuery("Cancel has not been asked at all", 0);
154         top.assertQuery("The VM classloader was not asked at all", 0);
155     }
156 */

157         
158     /** Counts number of queiries.
159      */

160     public static class CountingClassLoader extends JarClassLoaderTest.OneClassLoader {
161         public int query;
162         
163         public void assertQuery(String JavaDoc msg, int cnt) {
164             assertEquals(msg, cnt, query);
165             query = 0;
166         }
167         
168         public CountingClassLoader(String JavaDoc name, ClassLoader JavaDoc l, String JavaDoc classname) {
169             this(name, l, new ClassLoader JavaDoc[] { l }, classname);
170         }
171         
172         public CountingClassLoader(String JavaDoc name, ClassLoader JavaDoc l, ClassLoader JavaDoc l2, String JavaDoc classname) {
173             this(name, l, new ClassLoader JavaDoc[] { l2 }, classname);
174         }
175         
176         public CountingClassLoader(String JavaDoc name, ClassLoader JavaDoc l, ClassLoader JavaDoc l2, Set names) {
177             this(name, l, new ClassLoader JavaDoc[] { l2 }, names);
178         }
179         
180         public CountingClassLoader(String JavaDoc name, ClassLoader JavaDoc lc, ClassLoader JavaDoc[] arr, String JavaDoc classname) {
181             this(name, lc, arr, java.util.Collections.singleton(classname));
182         }
183         public CountingClassLoader(String JavaDoc name, ClassLoader JavaDoc lc, ClassLoader JavaDoc[] arr, java.util.Set JavaDoc names) {
184             super(name, lc, arr, names);
185         }
186     
187
188         protected Class JavaDoc simpleFindClass(String JavaDoc name, String JavaDoc path, String JavaDoc pkgnameSlashes) {
189             query++;
190             return super.simpleFindClass(name, path, pkgnameSlashes);
191         }
192         protected URL JavaDoc findResource(String JavaDoc name) {
193             query++;
194             return super.findResource(name);
195         }
196     }
197     /** Counts number of queiries.
198      */

199     public static class TopCountingClassLoader extends ClassLoader JavaDoc {
200         public int query;
201         
202         public void assertQuery(String JavaDoc msg, int cnt) {
203             assertEquals(msg, cnt, query);
204             query = 0;
205         }
206         
207         public TopCountingClassLoader(ClassLoader JavaDoc parent) {
208             super(parent);
209         }
210
211         public URL JavaDoc getResource(String JavaDoc name) {
212             query++;
213
214             URL JavaDoc retValue;
215             
216             retValue = super.getResource(name);
217             return retValue;
218         }
219
220         public InputStream getResourceAsStream(String JavaDoc name) {
221             query++;
222
223             InputStream retValue;
224             
225             retValue = super.getResourceAsStream(name);
226             return retValue;
227         }
228
229         public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
230             if (!name.startsWith("java.")) {
231                 query++;
232             }
233             
234
235             Class JavaDoc retValue;
236             
237             retValue = super.loadClass(name);
238             return retValue;
239         }
240         
241
242     }
243 }
244
Popular Tags