KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > WeavedTestCase


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.lang.reflect.Constructor JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.net.URLClassLoader JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 /**
20  * Transparently runs TestCase with an embedded online mode Write a JUnit test case and extends WeaverTestCase.
21  *
22  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur </a>
23  */

24 public class WeavedTestCase extends TestCase {
25   /**
26    * the test runner that runs the test thru reflection in a weaving ClassLoader
27    */

28   private static WeaverTestRunner s_runner = new WeaverTestRunner();
29
30   public WeavedTestCase() {
31     super();
32   }
33
34   public WeavedTestCase(String JavaDoc name) {
35     super(name);
36   }
37
38   /**
39    * Overrides JUnit runBare() to run thru the weaverTestRunner This allow WeaverTestCase to be regular TestCase
40    *
41    * @throws java.lang.Throwable
42    */

43   public void runBare() throws Throwable JavaDoc {
44     s_runner.runTest(this.getClass().getName(), getName());
45   }
46
47   /**
48    * Callback the regulare JUnit runBare()
49    *
50    * @throws java.lang.Throwable
51    */

52   public void runBareAfterWeaving() throws Throwable JavaDoc {
53     super.runBare();
54   }
55
56   /**
57    * Allow to run WeaverTestCase thru a weaving ClassLoader
58    */

59   public static class WeaverTestRunner {
60     /**
61      * Weaving classloader
62      */

63     private ClassLoader JavaDoc cl;
64
65     /**
66      * Build weavin classloader with system class path and ext. classloader as parent
67      */

68     public WeaverTestRunner() {
69       try {
70         String JavaDoc path = System.getProperty("java.class.path");
71         ArrayList JavaDoc paths = new ArrayList JavaDoc();
72         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, File.pathSeparator);
73         while (st.hasMoreTokens()) {
74           paths.add((new File JavaDoc(st.nextToken())).getCanonicalFile().toURL());
75         }
76         cl = new URLClassLoader JavaDoc(
77                 (URL JavaDoc[]) paths.toArray(new URL JavaDoc[]{}),
78                 ClassLoader.getSystemClassLoader().getParent(),
79                 null
80         );
81       } catch (IOException JavaDoc e) {
82         throw new WrappedRuntimeException(e);
83       }
84     }
85
86     /**
87      * Runs a single test (testXX) Takes care of not using the weaving class loader is online mode or
88      * weavingClassLoader.main() is already used (might fail under JRockit MAPI)
89      *
90      * @param testClassName test class
91      * @param testMethodName test method
92      * @throws java.lang.Throwable
93      */

94     public void runTest(String JavaDoc testClassName, String JavaDoc testMethodName) throws Throwable JavaDoc {
95       // skip test embedded weaving if online mode / weavingClassLoader.main() is already used
96
if ((cl.getClass().getClassLoader() == null)
97               || (cl.getClass().getClassLoader().getClass().getName().indexOf("hook.impl.Weaving") > 0)) {
98         ;
99       } else {
100         Thread.currentThread().setContextClassLoader(cl); // needed for Aspect loading
101
}
102       Class JavaDoc testClass = Class.forName(testClassName, true, Thread.currentThread().getContextClassLoader());
103
104       //)cl.loadClass(testClassName);
105
Constructor JavaDoc ctor = null;
106       Object JavaDoc testInstance = null;
107       try {
108         // new junit style
109
ctor = testClass.getConstructor(new Class JavaDoc[]{});
110         testInstance = ctor.newInstance(new Object JavaDoc[]{});
111         Method JavaDoc setNameMethod = testClass.getMethod(
112                 "setExpression", new Class JavaDoc[]{
113                 String JavaDoc.class
114         }
115         );
116         setNameMethod.invoke(
117                 testInstance, new Object JavaDoc[]{
118                 testMethodName
119         }
120         );
121       } catch (NoSuchMethodException JavaDoc e) {
122         ctor = testClass.getConstructor(
123                 new Class JavaDoc[]{
124                         String JavaDoc.class
125                 }
126         );
127         testInstance = ctor.newInstance(
128                 new Object JavaDoc[]{
129                         testMethodName
130                 }
131         );
132       }
133       Method JavaDoc runAfterWeavingMethod = testClass.getMethod("runBareAfterWeaving", new Class JavaDoc[]{});
134       runAfterWeavingMethod.invoke(testInstance, new Object JavaDoc[]{});
135     }
136   }
137 }
Popular Tags