1 17 package org.apache.servicemix.jbi.loaders; 18 19 import java.io.File ; 20 import java.net.URL ; 21 import java.net.URLClassLoader ; 22 23 import org.apache.xbean.classloader.JarFileClassLoader; 24 25 import junit.framework.TestCase; 26 27 30 public class ClassLoaderTest extends TestCase { 31 32 public static class TestClass { 33 } 34 35 public void testParentFirstClassLoader() throws Exception { 36 URLClassLoader pcl = (URLClassLoader ) getClass().getClassLoader(); 37 ClassLoader clsLoader = new JarFileClassLoader("", pcl.getURLs(), pcl, false, new String [0], new String [0]); 38 Class clazz = clsLoader.loadClass(TestClass.class.getName()); 39 assertSame(TestClass.class, clazz); 40 } 41 42 public void testSelfFirstClassLoader() throws Exception { 43 URLClassLoader pcl = (URLClassLoader ) getClass().getClassLoader(); 44 ClassLoader clsLoader = new JarFileClassLoader("", pcl.getURLs(), pcl, true, new String [0], new String [0]); 45 Class clazz = clsLoader.loadClass(TestClass.class.getName()); 46 assertNotSame(TestClass.class, clazz); 47 } 48 49 public void testParentFirstResource() throws Exception { 50 URLClassLoader pcl = (URLClassLoader ) getClass().getClassLoader(); 51 URL url = getClass().getResource("jndi.properties"); 52 url = new File (url.toURI()).getParentFile().toURL(); 53 ClassLoader clsLoader = new JarFileClassLoader("", new URL [] { url }, pcl, false, new String [0], new String [0]); 54 URL res1 = clsLoader.getResource("jndi.properties"); 55 URL res2 = pcl.getResource("jndi.properties"); 56 assertEquals(res2, res1); 57 } 58 59 public void testSelfFirstResource() throws Exception { 60 URLClassLoader pcl = (URLClassLoader ) getClass().getClassLoader(); 61 URL url = getClass().getResource("jndi.properties"); 62 url = new File (url.toURI()).getParentFile().toURL(); 63 ClassLoader clsLoader = new JarFileClassLoader("", new URL [] { url }, pcl, true, new String [0], new String [0]); 64 URL res1 = clsLoader.getResource("jndi.properties"); 65 URL res2 = pcl.getResource("jndi.properties"); 66 assertFalse(res2.equals(res1)); 67 } 68 69 } 70 | Popular Tags |