1 2 package org.quilt.cl; 3 4 import java.io.*; 5 import java.lang.Class ; 6 import java.lang.reflect.*; 7 import java.net.*; 8 import junit.framework.*; 9 10 16 public class TestQuiltClassLoader extends TestCase { 17 18 private URL [] cp = null; 19 20 private String [] delegating = { 23 "org.quilt" 24 }; 25 private String [] include = { 26 }; 28 private String [] exclude = { 29 "AnonymousClass", "AnonymousClass2Catches", 30 "InnerClass", "NestedTryBlocks", 31 "PrivateClass", "SuperClass", 32 "Wimple" 33 }; 34 private QuiltClassLoader qLoader = null; 35 36 public TestQuiltClassLoader (String name) { 37 super(name); 38 } 39 40 public void setUp () { 41 File sam1 = new File ("target/test-data-classes/"); 42 File sam2 = new File ("target/classes"); 43 File sam3 = new File ("target/test-classes"); 44 45 62 qLoader = new QuiltClassLoader( 63 QuiltClassLoader.cpToURLs( 64 sam1 + ":" + sam2 + ":" + sam3), 65 null, delegating, include, exclude); } 70 public void testDomainToFileName() { 71 assertEquals ("../my.jar", 72 QuiltClassLoader.domainToFileName("../my.jar")); 73 assertEquals ("../../target/my.jar", 74 QuiltClassLoader.domainToFileName("../../target/my.jar")); 75 assertEquals ("../../../target/my.jar", 76 QuiltClassLoader.domainToFileName("../../../target/my.jar")); 77 assertEquals ("ab/cd/ef/gh", 78 QuiltClassLoader.domainToFileName("ab.cd.ef.gh")); 79 assertEquals ("./abc/def", 80 QuiltClassLoader.domainToFileName("./abc.def")); 81 assertEquals ("./.././abc", 82 QuiltClassLoader.domainToFileName("./.././abc")); 83 } 85 public void testConstructor() { 87 assertNotNull("Error creating Quilt class loader", qLoader); 88 URL[] cp2 = qLoader.getClassPath(); 89 assertEquals("wrong size classpath", 3, cp2.length); 90 91 String [] del2 = qLoader.getDelegated(); 92 assertEquals ("wrong number of delegated classes", 93 delegating.length 94 + QuiltClassLoader.DELEGATED.length, 95 del2.length); 96 97 String [] exc2 = qLoader.getExcluded(); 98 assertEquals ("wrong number of excluded classes", 99 exclude.length, exc2.length); 100 101 String [] inc2 = qLoader.getIncluded(); 102 assertEquals ("wrong number of delegated classes", 103 include.length, inc2.length); 104 } 105 public void testLoader() { 106 Class a1 = null; 107 Class a2 = null; 108 try { 109 a1 = qLoader.loadClass("AnonymousClass"); 110 } catch (ClassNotFoundException e) { 111 e.printStackTrace(); 112 fail ("Error loading AnonymousClass using loadClass"); 113 } 114 try { 115 a2 = qLoader.loadClass("AnonymousClass"); 116 } catch (ClassNotFoundException e) { 117 fail ("Error loading AnonymousClass using loadClass"); 118 } 119 assertNotNull("qLoader returned null", a1); 120 assertNotNull("qLoader returned null", a2); 121 assertEquals ("second load returned a different class", 122 (Object )a1, (Object )a2); 123 } 124 public void testParentage() { 125 qLoader.setSynthEnabled(true); 126 Class a1 = null; 127 Class a2 = null; 128 try { 129 a1 = qLoader.loadClass("AnonymousClass"); 130 } catch (ClassNotFoundException e) { 131 e.printStackTrace(); 132 fail ("Error loading AnonymousClass using loadClass"); 133 } 134 try { 135 a2 = qLoader.loadClass("test.data.TestDefault"); 136 } catch (ClassNotFoundException e) { 137 fail ("Error loading test.data.TestDefault using loadClass"); 138 } 139 assertNotNull("qLoader returned null", a1); 140 assertNotNull("qLoader returned null", a2); 141 142 assertEquals ("AnonymousClass has wrong parent", 143 qLoader, a1.getClassLoader()); 144 assertEquals ("testDefault has wrong parent", 145 qLoader, a2.getClassLoader()); 146 assertEquals ("qLoader's parent is not my parent", 148 this.getClass().getClassLoader(), 149 qLoader.getClass().getClassLoader() ); 150 } 151 152 public void testInvokeTestData() { 153 Class a1 = null; 154 Class a2 = null; 155 try { 156 a1 = qLoader.loadClass("AnonymousClass"); 157 } catch (ClassNotFoundException e) { 158 e.printStackTrace(); 159 fail ("Error loading AnonymousClass using loadClass"); 160 } 161 try { 162 a2 = qLoader.loadClass("BasicLoad"); 163 } catch (ClassNotFoundException e) { 164 fail ("Error loading BasicLoad using loadClass"); 165 } 166 RunTest rt = null; 167 try { 169 rt = (RunTest) a1.newInstance(); 170 } catch ( InstantiationException e ) { 171 fail ("error instantiating loaded AnonymousClass"); 172 } catch ( IllegalAccessException e ) { 173 fail ("error instantiating loaded AnonymousClass"); 174 } 175 assertEquals ("AnonymousClass isn't working", 47, rt.runTest(47)); 177 178 try { 180 rt = (RunTest) a2.newInstance(); 181 } catch ( InstantiationException e ) { 182 fail ("error instantiating loaded BasicLoad"); 183 } catch ( IllegalAccessException e ) { 184 fail ("error instantiating loaded BasicLoad"); 185 } 186 assertEquals ("BasicLoad isn't working", 49, rt.runTest(7)); 188 } 189 190 public void testSynth () { 191 assertEquals ("synthesizing isn't disabled in loader", 192 false, qLoader.getSynthEnabled() ); 193 qLoader.setSynthEnabled(true); 194 assertEquals ("enabling synthesizing failed", 195 true, qLoader.getSynthEnabled() ); 196 197 Class a1 = null; 198 Class a2 = null; 199 try { 200 a1 = qLoader.loadClass("test.data.TestDefault"); 201 } catch (ClassNotFoundException e) { 202 e.printStackTrace(); 203 fail ("Error loading Default"); 204 } 205 try { 206 a2 = qLoader.loadClass("test.data.TestIfThen"); 207 } catch (ClassNotFoundException e) { 208 fail ("Error loading testIfThen using loadClass"); 209 } 210 RunTest rt = null; 211 try { 213 rt = (RunTest) a1.newInstance(); 214 } catch ( InstantiationException e ) { 215 fail ("error instantiating loaded testDefault"); 216 } catch ( IllegalAccessException e ) { 217 fail ("error instantiating loaded testDefault"); 218 } 219 assertEquals ("testDefault isn't working", 2, rt.runTest(47)); 221 assertEquals ("testDefault isn't working", 2, rt.runTest(-7)); 222 223 try { 225 rt = (RunTest) a2.newInstance(); 226 } catch ( InstantiationException e ) { 227 fail ("error instantiating loaded testIfThen"); 228 } catch ( IllegalAccessException e ) { 229 fail ("error instantiating loaded testIfThen"); 230 } 231 assertEquals ("testIfThen isn't working", 3, rt.runTest(47)); 233 assertEquals ("testIfThen isn't working", 5, rt.runTest(-7)); 234 235 Class xNoCatch = null; 236 Class xWithCatch = null; 237 Class xSelect = null; 238 Class xWhile = null; 239 try { 240 xNoCatch = qLoader.loadClass("test.data.TestNPENoCatch"); 241 xWithCatch = qLoader.loadClass("test.data.TestNPEWithCatch"); 242 xSelect = qLoader.loadClass("test.data.TestSelect"); 243 xWhile = qLoader.loadClass("test.data.TestWhile"); 244 } catch (ClassNotFoundException e) { 245 e.printStackTrace(); 246 fail ("Error loading synthesized class"); 247 } 248 249 try { 251 rt = (RunTest) xWithCatch.newInstance(); 252 } catch ( InstantiationException e ) { 253 fail ("error instantiating loaded testNPEWithCatch"); 254 } catch ( IllegalAccessException e ) { 255 fail ("error instantiating loaded testNPEWithCatch"); 256 } 257 assertEquals ("testNPEWithCatch isn't working", 3, rt.runTest(47)); 259 assertEquals ("testNPEWithCatch isn't working", 3, rt.runTest(-7)); 260 261 try { 263 rt = (RunTest) xNoCatch.newInstance(); 264 } catch ( InstantiationException e ) { 265 fail ("error instantiating loaded testNPENoCatch"); 266 } catch ( IllegalAccessException e ) { 267 fail ("error instantiating loaded testNPENoCatch"); 268 } 269 int x; 271 try { 272 x = rt.runTest(47); 273 fail ("testNPENoCatch didn't throw exception"); 274 } catch (NullPointerException e) { 275 ; } 277 try { 278 x = rt.runTest(-7); 279 fail ("testNPENoCatch didn't throw exception"); 280 } catch (NullPointerException e) { 281 ; } 283 284 try { 286 rt = (RunTest) xSelect.newInstance(); 287 } catch ( InstantiationException e ) { 288 fail ("error instantiating loaded testSelect"); 289 } catch ( IllegalAccessException e ) { 290 fail ("error instantiating loaded testSelect"); 291 } 292 assertEquals ("testSelect isn't working", 2, rt.runTest(47)); 295 assertEquals ("testSelect isn't working", 2, rt.runTest(-7)); 296 assertEquals ("testSelect isn't working", 1, rt.runTest(1)); 297 assertEquals ("testSelect isn't working", 3, rt.runTest(2)); 298 assertEquals ("testSelect isn't working", 5, rt.runTest(3)); 299 300 try { 302 rt = (RunTest) xWhile.newInstance(); 303 } catch ( InstantiationException e ) { 304 fail ("error instantiating loaded testWhile"); 305 } catch ( IllegalAccessException e ) { 306 fail ("error instantiating loaded testWhile"); 307 } 308 assertEquals ("testWhile isn't working", 0, rt.runTest(47)); 311 assertEquals ("testWhile isn't working",-7, rt.runTest(-7)); 312 313 } 314 public void testParentage2() { 315 qLoader.setSynthEnabled(true); 316 } 317 public void testSetters() { 318 qLoader.setClassPath("abc.jar:def:ghi.a.b.c:froggy.pickle.jar"); 319 URL[] urls = qLoader.getClassPath(); 320 321 assertNotNull("classpath is null", urls); 322 assertEquals ("wrong number of URLs in classpath", 4, urls.length); 323 assertTrue(urls[0].getFile().endsWith("abc.jar")); 324 assertTrue(urls[1].getPath().endsWith("def/")); 325 assertTrue(urls[2].getPath().endsWith("ghi/a/b/c/")); 326 assertTrue(urls[3].getPath().endsWith("froggy/pickle.jar")); 328 assertTrue(urls[3].getFile().endsWith("froggy/pickle.jar")); 329 330 qLoader.setExcluded("this,that,theOther"); 331 String [] exc = qLoader.getExcluded(); 332 assertEquals("wrong excluded count", 3, exc.length); 333 assertEquals("this", exc[0]); 334 assertEquals("theOther", exc[2]); 335 336 qLoader.setIncluded("eenie,meenie,minee,mo"); 337 String [] inc = qLoader.getIncluded(); 338 assertEquals("wrong included count", 4, inc.length); 339 assertEquals("eenie", inc[0]); 340 assertEquals("mo", inc[3]); 341 } 342 } 343 | Popular Tags |