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