1 22 package org.jboss.test.classloader.circularity.test; 23 24 import java.net.URL ; 25 import java.lang.reflect.Constructor ; 26 27 import org.jboss.logging.Logger; 28 29 import org.jboss.test.classloader.circularity.support.Support; 30 import org.jboss.mx.loading.LoaderRepository; 31 import org.jboss.mx.loading.UnifiedClassLoader; 32 import org.jboss.mx.loading.UnifiedClassLoader3; 33 import org.jboss.mx.loading.UnifiedLoaderRepository3; 34 35 40 public class CircularityErrorTests 41 { 42 private static Logger log = Logger.getLogger(CircularityErrorTests.class); 43 44 private Object lock = new Object (); 45 private boolean sawCircularity; 46 private boolean sawClassNotFound; 47 48 public CircularityErrorTests() 49 { 50 } 51 52 public void testClassCircularityError() throws Exception 53 { 54 61 UnifiedLoaderRepository3 repository = new UnifiedLoaderRepository3(); 62 URL url = getClass().getProtectionDomain().getCodeSource().getLocation(); 63 final UnifiedLoader ucl = new UnifiedLoader(url, repository); 64 repository.addClassLoader(ucl); 65 log.debug("Unified ClassLoader created"); 66 67 Class cls = ucl.loadClass("org.jboss.test.classloader.circularity.support.Support"); 68 69 Thread thread1 = new Thread (new Runnable () 70 { 71 public void run() 72 { 73 try 74 { 75 try {Thread.sleep(1000);} 77 catch (InterruptedException x) {} 78 79 try 80 { 81 log.debug("Thread " + Thread.currentThread() + " loading..."); 83 ucl.loadClass("org.jboss.test.classloader.circularity.support.Derived"); 84 log.debug("Thread " + Thread.currentThread() + " loading done !"); 85 } 86 catch (ClassCircularityError x) 87 { 88 log.error("Saw ClassCircularityError", x); 89 sawCircularity = true; 90 } 91 } 92 catch (ClassNotFoundException x) 93 { 94 log.error("Bug in the test: ", x); 95 sawClassNotFound = true; 96 } 97 } 98 }, "CircularityError Thread"); 99 thread1.start(); 100 101 { 103 log.debug("Thread " + Thread.currentThread() + " waiting..."); 104 log.debug("Thread " + Thread.currentThread() + " woken up !"); 106 } 107 108 cls.newInstance(); 112 113 if( sawCircularity ) 115 throw new ClassCircularityError ("Got ClassCircularityError, UnifiedLoaderRepository is buggy"); 116 if( sawClassNotFound ) 117 throw new ClassNotFoundException ("Got ClassNotFoundException, UnifiedLoaderRepository is buggy"); 118 } 119 120 public class UnifiedLoader extends UnifiedClassLoader3 121 { 122 public UnifiedLoader(URL url, LoaderRepository repository) 123 { 124 super(url, url, repository); 125 } 126 127 public Class loadClass(String name) throws ClassNotFoundException 128 { 129 log.debug(Thread.currentThread() + " loadClass: " + name); 130 return super.loadClass(name); 131 } 132 133 public Class loadClassLocally(String name, boolean resolve) throws ClassNotFoundException 134 { 135 log.debug(Thread.currentThread() + " is now asked to load class: " + name); 136 137 if (name.equals("org.jboss.test.classloader.circularity.support.Derived")) 138 { 139 synchronized (lock) 140 { 141 lock.notifyAll(); 142 } 143 144 try 147 { 148 log.debug("Loading " + name + ", waiting..."); 149 Thread.sleep(2000); 150 log.debug("Loading " + name + " end wait"); 151 } 152 catch (InterruptedException x) 153 { 154 log.debug("Sleep was interrupted", x); 155 } 156 } 157 158 return super.loadClassLocally(name, resolve); 159 } 160 } 161 162 } 163 164 | Popular Tags |