1 17 18 package org.apache.geronimo.jetty; 19 20 import java.net.URL ; 21 import java.net.MalformedURLException ; 22 import java.io.File ; 23 24 import junit.framework.TestCase; 25 26 33 public class ClassLoaderTest extends TestCase { 34 JettyClassLoader cl; 35 URL [] urls; 36 37 public void setUp() throws MalformedURLException { 38 URL url = new File ("src/test-resources/deployables/cltest/").toURL(); 39 System.err.println("URL: "+url); 41 urls = new URL []{url}; 42 } 43 44 46 50 public void testFalseNonexistantJavaxClass() { 51 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), false); 52 try { 53 cl.loadClass("javax.foo.Foo"); 54 } catch(ClassNotFoundException e) { 55 fail("Should be able to load a javax.* class that is not defined by my parent CL"); 56 } 57 } 58 59 63 public void testTrueNonexistantJavaxClass() { 64 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), true); 65 try { 66 cl.loadClass("javax.foo.Foo"); 67 } catch(ClassNotFoundException e) { 68 fail("Should be able to load a javax.* class that is not defined by my parent CL"); 69 } 70 } 71 72 77 public void testFalseExistantJavaxClass() { 78 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), false); 79 try { 80 Class cls = cl.loadClass("javax.servlet.Servlet"); 81 assertTrue("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",cls.getDeclaredMethods().length > 0); 82 } catch(ClassNotFoundException e) { 83 fail("Problem with test; expecting to have javax.servlet.* on the ClassPath"); 84 } 85 } 86 87 92 public void testTrueExistantJavaxClass() { 93 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), true); 94 try { 95 Class cls = cl.loadClass("javax.servlet.Servlet"); 96 assertTrue("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",cls.getDeclaredMethods().length > 0); 97 } catch(ClassNotFoundException e) { 98 fail("Problem with test; expecting to have javax.servlet.* on the ClassPath"); 99 } 100 } 101 102 109 public void testFalseExistantNonJavaxClass() { 110 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), false); 111 try { 112 Class cls = cl.loadClass("mx4j.MBeanDescription"); 113 assertTrue("Should not have overriden parent CL definition of class mx4j.MBeanDescription", cls.getDeclaredMethods().length > 0); 114 } catch(ClassNotFoundException e) { 115 fail("Problem with test; expecting to have mx4j.* on the ClassPath"); 116 } 117 } 118 119 126 public void testTrueExistantNonJavaxClass() { 127 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), true); 128 try { 129 Class cls = cl.loadClass("mx4j.MBeanDescription"); 130 assertTrue("Should be able to override a class that is not in java.*, javax.*, etc.", cls.getDeclaredMethods().length == 0); 131 } catch(ClassNotFoundException e) { 132 fail("Problem with test; expecting to have mx4j.* on the ClassPath"); 133 } 134 } 135 136 140 public void testFalseNonexistantJavaxResource() { 141 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), false); 142 URL url = cl.getResource("javax/foo/Foo.class"); 143 if(url == null) { 144 fail("Should be able to load a javax.* class that is not defined by my parent CL"); 145 } 146 assertEquals(url.getProtocol(), "file"); 147 } 148 149 153 public void testTrueNonexistantJavaxResource() { 154 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), true); 155 URL url = cl.getResource("javax/foo/Foo.class"); 156 if(url == null) { 157 fail("Should be able to load a javax.* class that is not defined by my parent CL"); 158 } 159 assertEquals(url.getProtocol(), "file"); 160 } 161 162 167 public void testFalseExistantJavaxResource() { 168 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), false); 169 URL url = cl.getResource("javax/servlet/Servlet.class"); 170 if(url == null) { 171 fail("Problem with test; expecting to have javax.servlet.* on the ClassPath"); 172 } 173 assertEquals("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet", url.getProtocol(), "jar"); 174 } 175 176 181 public void testTrueExistantJavaxResource() { 182 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), true); 183 URL url = cl.getResource("javax/servlet/Servlet.class"); 184 if(url == null) { 185 fail("Problem with test; expecting to have javax.servlet.* on the ClassPath"); 186 } 187 assertEquals("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",url.getProtocol(),"jar"); 188 } 189 190 197 public void testFalseExistantNonJavaxResource() { 198 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), false); 199 URL url = cl.getResource("mx4j/MBeanDescription.class"); 200 if(url == null) { 201 fail("Problem with test; expecting to have mx4j.* on the ClassPath"); 202 } 203 assertEquals("Should not have overriden parent CL definition of class mx4j.MBeanDescription", url.getProtocol(), "jar"); 204 } 205 206 213 public void testTrueExistantNonJavaxResource() { 214 cl = new JettyClassLoader(urls, null, getClass().getClassLoader(), true); 215 URL url = cl.getResource("mx4j/MBeanDescription.class"); 216 if(url == null) { 217 fail("Problem with test; expecting to have mx4j.* on the ClassPath"); 218 } 219 assertEquals("Should be able to override a class that is not in java.*, javax.*, etc.", url.getProtocol(), "file"); 220 } 221 } 222 | Popular Tags |