1 4 package com.tc.aspectwerkz; 5 6 import junit.framework.TestCase; 7 8 import com.tc.aspectwerkz.exception.WrappedRuntimeException; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.lang.reflect.Constructor ; 13 import java.lang.reflect.Method ; 14 import java.net.URL ; 15 import java.net.URLClassLoader ; 16 import java.util.ArrayList ; 17 import java.util.StringTokenizer ; 18 19 24 public class WeavedTestCase extends TestCase { 25 28 private static WeaverTestRunner s_runner = new WeaverTestRunner(); 29 30 public WeavedTestCase() { 31 super(); 32 } 33 34 public WeavedTestCase(String name) { 35 super(name); 36 } 37 38 43 public void runBare() throws Throwable { 44 s_runner.runTest(this.getClass().getName(), getName()); 45 } 46 47 52 public void runBareAfterWeaving() throws Throwable { 53 super.runBare(); 54 } 55 56 59 public static class WeaverTestRunner { 60 63 private ClassLoader cl; 64 65 68 public WeaverTestRunner() { 69 try { 70 String path = System.getProperty("java.class.path"); 71 ArrayList paths = new ArrayList (); 72 StringTokenizer st = new StringTokenizer (path, File.pathSeparator); 73 while (st.hasMoreTokens()) { 74 paths.add((new File (st.nextToken())).getCanonicalFile().toURL()); 75 } 76 cl = new URLClassLoader ( 77 (URL []) paths.toArray(new URL []{}), 78 ClassLoader.getSystemClassLoader().getParent(), 79 null 80 ); 81 } catch (IOException e) { 82 throw new WrappedRuntimeException(e); 83 } 84 } 85 86 94 public void runTest(String testClassName, String testMethodName) throws Throwable { 95 if ((cl.getClass().getClassLoader() == null) 97 || (cl.getClass().getClassLoader().getClass().getName().indexOf("hook.impl.Weaving") > 0)) { 98 ; 99 } else { 100 Thread.currentThread().setContextClassLoader(cl); } 102 Class testClass = Class.forName(testClassName, true, Thread.currentThread().getContextClassLoader()); 103 104 Constructor ctor = null; 106 Object testInstance = null; 107 try { 108 ctor = testClass.getConstructor(new Class []{}); 110 testInstance = ctor.newInstance(new Object []{}); 111 Method setNameMethod = testClass.getMethod( 112 "setExpression", new Class []{ 113 String .class 114 } 115 ); 116 setNameMethod.invoke( 117 testInstance, new Object []{ 118 testMethodName 119 } 120 ); 121 } catch (NoSuchMethodException e) { 122 ctor = testClass.getConstructor( 123 new Class []{ 124 String .class 125 } 126 ); 127 testInstance = ctor.newInstance( 128 new Object []{ 129 testMethodName 130 } 131 ); 132 } 133 Method runAfterWeavingMethod = testClass.getMethod("runBareAfterWeaving", new Class []{}); 134 runAfterWeavingMethod.invoke(testInstance, new Object []{}); 135 } 136 } 137 } | Popular Tags |