1 8 package test; 9 10 import junit.framework.TestCase; 11 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException; 12 import org.codehaus.aspectwerkz.hook.impl.WeavingClassLoader; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.lang.reflect.Constructor ; 17 import java.lang.reflect.Method ; 18 import java.net.URL ; 19 import java.util.ArrayList ; 20 import java.util.StringTokenizer ; 21 22 28 public class WeavedTestCase extends TestCase { 29 32 private static WeaverTestRunner s_runner = new WeaverTestRunner(); 33 34 public WeavedTestCase() { 35 super(); 36 } 37 38 public WeavedTestCase(String name) { 39 super(name); 40 } 41 42 48 public void runBare() throws Throwable { 49 s_runner.runTest(this.getClass().getName(), getName()); 50 } 51 52 57 public void runBareAfterWeaving() throws Throwable { 58 super.runBare(); 59 } 60 61 64 public static class WeaverTestRunner { 65 68 private WeavingClassLoader cl; 69 70 73 public WeaverTestRunner() { 74 try { 75 String path = System.getProperty("java.class.path"); 76 ArrayList paths = new ArrayList (); 77 StringTokenizer st = new StringTokenizer (path, File.pathSeparator); 78 while (st.hasMoreTokens()) { 79 paths.add((new File (st.nextToken())).getCanonicalFile().toURL()); 80 } 81 cl = new WeavingClassLoader( 82 (URL []) paths.toArray(new URL []{}), ClassLoader 83 .getSystemClassLoader().getParent() 84 ); 85 } catch (IOException e) { 86 throw new WrappedRuntimeException(e); 87 } 88 } 89 90 98 public void runTest(String testClassName, String testMethodName) throws Throwable { 99 if ((cl.getClass().getClassLoader() == null) 101 || (cl.getClass().getClassLoader().getClass().getName() 102 .indexOf("hook.impl.Weaving") > 0)) { 103 ; 104 } else { 105 Thread.currentThread().setContextClassLoader(cl); } 107 Class testClass = Class.forName( 108 testClassName, true, Thread.currentThread() 109 .getContextClassLoader() 110 ); 111 112 Constructor ctor = null; 114 Object testInstance = null; 115 try { 116 ctor = testClass.getConstructor(new Class []{}); 118 testInstance = ctor.newInstance(new Object []{}); 119 Method setNameMethod = testClass.getMethod( 120 "setExpression", new Class []{ 121 String .class 122 } 123 ); 124 setNameMethod.invoke( 125 testInstance, new Object []{ 126 testMethodName 127 } 128 ); 129 } catch (NoSuchMethodException e) { 130 ctor = testClass.getConstructor( 131 new Class []{ 132 String .class 133 } 134 ); 135 testInstance = ctor.newInstance( 136 new Object []{ 137 testMethodName 138 } 139 ); 140 } 141 Method runAfterWeavingMethod = testClass.getMethod( 142 "runBareAfterWeaving", 143 new Class []{} 144 ); 145 runAfterWeavingMethod.invoke(testInstance, new Object []{}); 146 } 147 } 148 } | Popular Tags |