1 19 package org.netbeans; 20 21 import junit.framework.*; 22 import java.net.URL ; 23 import java.util.jar.JarFile ; 24 import java.util.jar.Manifest ; 25 import java.util.jar.Attributes ; 26 import java.util.jar.Attributes.Name; 27 import java.util.zip.ZipEntry ; 28 import java.io.*; 29 import java.net.MalformedURLException ; 30 import java.security.*; 31 import java.security.cert.Certificate ; 32 import java.util.*; 33 34 38 public class AskDirectlyTest extends TestCase { 39 String c; 40 String g; 41 42 CountingClassLoader cancel; 43 44 CountingClassLoader global; 45 46 TopCountingClassLoader top; 47 48 ProxyClassLoader all; 49 50 public AskDirectlyTest(String testName) { 51 super(testName); 52 } 53 54 protected void setUp() throws Exception { 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 [] { cancel, global }); 69 } 70 71 public void testLoadingJDKClassDoesNotAsk() throws Exception { 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 99 public void testLoadingClassFromFirstCLDoesNotAsk() throws Exception { 100 Class 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 157 158 160 public static class CountingClassLoader extends JarClassLoaderTest.OneClassLoader { 161 public int query; 162 163 public void assertQuery(String msg, int cnt) { 164 assertEquals(msg, cnt, query); 165 query = 0; 166 } 167 168 public CountingClassLoader(String name, ClassLoader l, String classname) { 169 this(name, l, new ClassLoader [] { l }, classname); 170 } 171 172 public CountingClassLoader(String name, ClassLoader l, ClassLoader l2, String classname) { 173 this(name, l, new ClassLoader [] { l2 }, classname); 174 } 175 176 public CountingClassLoader(String name, ClassLoader l, ClassLoader l2, Set names) { 177 this(name, l, new ClassLoader [] { l2 }, names); 178 } 179 180 public CountingClassLoader(String name, ClassLoader lc, ClassLoader [] arr, String classname) { 181 this(name, lc, arr, java.util.Collections.singleton(classname)); 182 } 183 public CountingClassLoader(String name, ClassLoader lc, ClassLoader [] arr, java.util.Set names) { 184 super(name, lc, arr, names); 185 } 186 187 188 protected Class simpleFindClass(String name, String path, String pkgnameSlashes) { 189 query++; 190 return super.simpleFindClass(name, path, pkgnameSlashes); 191 } 192 protected URL findResource(String name) { 193 query++; 194 return super.findResource(name); 195 } 196 } 197 199 public static class TopCountingClassLoader extends ClassLoader { 200 public int query; 201 202 public void assertQuery(String msg, int cnt) { 203 assertEquals(msg, cnt, query); 204 query = 0; 205 } 206 207 public TopCountingClassLoader(ClassLoader parent) { 208 super(parent); 209 } 210 211 public URL getResource(String name) { 212 query++; 213 214 URL retValue; 215 216 retValue = super.getResource(name); 217 return retValue; 218 } 219 220 public InputStream getResourceAsStream(String name) { 221 query++; 222 223 InputStream retValue; 224 225 retValue = super.getResourceAsStream(name); 226 return retValue; 227 } 228 229 public Class loadClass(String name) throws ClassNotFoundException { 230 if (!name.startsWith("java.")) { 231 query++; 232 } 233 234 235 Class retValue; 236 237 retValue = super.loadClass(name); 238 return retValue; 239 } 240 241 242 } 243 } 244 | Popular Tags |