1 17 18 package org.apache.geronimo.jetty6; 19 20 import java.io.File ; 21 import java.net.URL ; 22 23 import org.apache.geronimo.testsupport.TestSupport; 24 25 import org.apache.geronimo.kernel.config.MultiParentClassLoader; 26 import org.apache.geronimo.kernel.repository.Artifact; 27 28 35 public class ClassLoaderTest extends TestSupport { 36 Artifact configId = new Artifact("foo", "bar", "1", "car"); 37 ClassLoader cl; 38 URL [] urls; 39 private static final String [] HIDDEN = {"org.apache.geronimo", "org.mortbay", "org.xml", "org.w3c"}; 40 private static final String [] NON_OVERRIDABLE = {"java.", "javax."}; 41 42 public void setUp() throws Exception { 43 super.setUp(); 44 URL url = new File (BASEDIR, "src/test/resources/deployables/cltest/").toURL(); 45 urls = new URL []{url}; 46 } 47 48 50 54 public void testFalseNonexistantJavaxClass() { 55 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE); 56 try { 57 cl.loadClass("javax.foo.Foo"); 58 } catch(ClassNotFoundException e) { 59 fail("Should be able to load a javax.* class that is not defined by my parent CL"); 60 } 61 } 62 63 67 public void testTrueNonexistantJavaxClass() { 68 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE); 69 try { 70 cl.loadClass("javax.foo.Foo"); 71 } catch(ClassNotFoundException e) { 72 fail("Should be able to load a javax.* class that is not defined by my parent CL"); 73 } 74 } 75 76 81 public void testFalseExistantJavaxClass() { 82 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE); 83 try { 84 Class cls = cl.loadClass("javax.servlet.Servlet"); 85 assertTrue("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",cls.getDeclaredMethods().length > 0); 86 } catch(ClassNotFoundException e) { 87 fail("Problem with test; expecting to have javax.servlet.* on the ClassPath"); 88 } 89 } 90 91 96 public void testTrueExistantJavaxClass() { 97 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE); 98 try { 99 Class cls = cl.loadClass("javax.servlet.Servlet"); 100 assertTrue("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",cls.getDeclaredMethods().length > 0); 101 } catch(ClassNotFoundException e) { 102 fail("Problem with test; expecting to have javax.servlet.* on the ClassPath"); 103 } 104 } 105 106 113 public void testFalseExistantNonJavaxClass() { 114 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE); 115 try { 116 Class cls = cl.loadClass("mx4j.MBeanDescription"); 117 assertTrue("Should not have overriden parent CL definition of class mx4j.MBeanDescription", cls.getDeclaredMethods().length > 0); 118 } catch(ClassNotFoundException e) { 119 fail("Problem with test; expecting to have mx4j.* on the ClassPath"); 120 } 121 } 122 123 130 public void testTrueExistantNonJavaxClass() { 131 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE); 132 try { 133 Class cls = cl.loadClass("mx4j.MBeanDescription"); 134 assertTrue("Should be able to override a class that is not in java.*, javax.*, etc.", cls.getDeclaredMethods().length == 0); 135 } catch(ClassNotFoundException e) { 136 fail("Problem with test; expecting to have mx4j.* on the ClassPath"); 137 } 138 } 139 140 144 public void testFalseNonexistantJavaxResource() { 145 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE); 146 URL url = cl.getResource("javax/foo/Foo.class"); 147 if(url == null) { 148 fail("Should be able to load a javax.* class that is not defined by my parent CL"); 149 } 150 assertEquals(url.getProtocol(), "file"); 151 } 152 153 157 public void testTrueNonexistantJavaxResource() { 158 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE); 159 URL url = cl.getResource("javax/foo/Foo.class"); 160 if(url == null) { 161 fail("Should be able to load a javax.* class that is not defined by my parent CL"); 162 } 163 assertEquals(url.getProtocol(), "file"); 164 } 165 166 171 public void testFalseExistantJavaxResource() { 172 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE); 173 URL url = cl.getResource("javax/servlet/Servlet.class"); 174 if(url == null) { 175 fail("Problem with test; expecting to have javax.servlet.* on the ClassPath"); 176 } 177 assertEquals("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet", url.getProtocol(), "jar"); 178 } 179 180 185 public void testTrueExistantJavaxResource() { 186 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE); 187 URL url = cl.getResource("javax/servlet/Servlet.class"); 188 if(url == null) { 189 fail("Problem with test; expecting to have javax.servlet.* on the ClassPath"); 190 } 191 assertEquals("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",url.getProtocol(),"jar"); 192 } 193 194 201 public void testFalseExistantNonJavaxResource() { 202 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE); 203 URL url = cl.getResource("mx4j/MBeanDescription.class"); 204 if(url == null) { 205 fail("Problem with test; expecting to have mx4j.* on the ClassPath"); 206 } 207 assertEquals("Should not have overriden parent CL definition of class mx4j.MBeanDescription", url.getProtocol(), "jar"); 208 } 209 210 217 public void testTrueExistantNonJavaxResource() { 218 cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, new String [] {}, NON_OVERRIDABLE); 219 URL url = cl.getResource("mx4j/MBeanDescription.class"); 220 if(url == null) { 221 fail("Problem with test; expecting to have mx4j.* on the ClassPath"); 222 } 223 assertEquals("Should be able to override a class that is not in java.*, javax.*, etc.", url.getProtocol(), "file"); 224 } 225 } 226 | Popular Tags |