001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
011: import org.codehaus.aspectwerkz.hook.impl.WeavingClassLoader;
012:
013: import java.io.File;
014: import java.io.IOException;
015: import java.lang.reflect.Constructor;
016: import java.lang.reflect.Method;
017: import java.net.URL;
018: import java.util.ArrayList;
019: import java.util.StringTokenizer;
020:
021: /**
022: * Transparently runs TestCase with an embedded online mode Write a JUnit test case and extends
023: * WeaverTestCase.
024: *
025: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
026: */
027: public class WeavedTestCase extends TestCase {
028: /**
029: * the test runner that runs the test thru reflection in a weaving ClassLoader
030: */
031: private static WeaverTestRunner s_runner = new WeaverTestRunner();
032:
033: public WeavedTestCase() {
034: super ();
035: }
036:
037: public WeavedTestCase(String name) {
038: super (name);
039: }
040:
041: /**
042: * Overrides JUnit runBare() to run thru the weaverTestRunner This allow WeaverTestCase to be
043: * regular TestCase
044: *
045: * @throws Throwable
046: */
047: public void runBare() throws Throwable {
048: s_runner.runTest(this .getClass().getName(), getName());
049: }
050:
051: /**
052: * Callback the regulare JUnit runBare()
053: *
054: * @throws Throwable
055: */
056: public void runBareAfterWeaving() throws Throwable {
057: super .runBare();
058: }
059:
060: /**
061: * Allow to run WeaverTestCase thru a weaving ClassLoader
062: */
063: public static class WeaverTestRunner {
064: /**
065: * Weaving classloader
066: */
067: private WeavingClassLoader cl;
068:
069: /**
070: * Build weavin classloader with system class path and ext. classloader as parent
071: */
072: public WeaverTestRunner() {
073: try {
074: String path = System.getProperty("java.class.path");
075: ArrayList paths = new ArrayList();
076: StringTokenizer st = new StringTokenizer(path,
077: File.pathSeparator);
078: while (st.hasMoreTokens()) {
079: paths.add((new File(st.nextToken()))
080: .getCanonicalFile().toURL());
081: }
082: cl = new WeavingClassLoader((URL[]) paths
083: .toArray(new URL[] {}), ClassLoader
084: .getSystemClassLoader().getParent());
085: } catch (IOException e) {
086: throw new WrappedRuntimeException(e);
087: }
088: }
089:
090: /**
091: * Runs a single test (testXX) Takes care of not using the weaving class loader is online
092: * mode or weavingClassLoader.main() is already used (might fail under JRockit MAPI)
093: *
094: * @param testClassName test class
095: * @param testMethodName test method
096: * @throws Throwable
097: */
098: public void runTest(String testClassName, String testMethodName)
099: throws Throwable {
100: // skip test embedded weaving if online mode / weavingClassLoader.main() is already used
101: if ((cl.getClass().getClassLoader() == null)
102: || (cl.getClass().getClassLoader().getClass()
103: .getName().indexOf("hook.impl.Weaving") > 0)) {
104: ;
105: } else {
106: Thread.currentThread().setContextClassLoader(cl); // needed for Aspect loading
107: }
108: Class testClass = Class.forName(testClassName, true, Thread
109: .currentThread().getContextClassLoader());
110:
111: //)cl.loadClass(testClassName);
112: Constructor ctor = null;
113: Object testInstance = null;
114: try {
115: // new junit style
116: ctor = testClass.getConstructor(new Class[] {});
117: testInstance = ctor.newInstance(new Object[] {});
118: Method setNameMethod = testClass.getMethod(
119: "setExpression", new Class[] { String.class });
120: setNameMethod.invoke(testInstance,
121: new Object[] { testMethodName });
122: } catch (NoSuchMethodException e) {
123: ctor = testClass
124: .getConstructor(new Class[] { String.class });
125: testInstance = ctor
126: .newInstance(new Object[] { testMethodName });
127: }
128: Method runAfterWeavingMethod = testClass.getMethod(
129: "runBareAfterWeaving", new Class[] {});
130: runAfterWeavingMethod.invoke(testInstance, new Object[] {});
131: }
132: }
133: }
|