Index: surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit4TestSet.java =================================================================== --- surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit4TestSet.java (Revision 0) +++ surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit4TestSet.java (Revision 0) @@ -0,0 +1,302 @@ +package org.apache.maven.surefire.junit; + +/* + * Copyright 2001-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import org.apache.maven.surefire.report.ReporterManager; +import org.apache.maven.surefire.testset.AbstractTestSet; +import org.apache.maven.surefire.testset.TestSetFailedException; +import org.junit.internal.runners.TextListener; +import org.junit.runner.JUnitCore; +import org.junit.runner.notification.RunListener; + +public final class JUnit4TestSet + extends AbstractTestSet +{ + public static final String TEST_CASE = "junit.framework.Test"; + + public static final String TEST_RESULT = "junit.framework.TestResult"; + + public static final String TEST_LISTENER = "junit.framework.TestListener"; + + public static final String TEST = "junit.framework.Test"; + + public static final String ADD_LISTENER_METHOD = "addListener"; + +// public static final String RUN_METHOD = "run"; + +// public static final String COUNT_TEST_CASES_METHOD = "countTestCases"; + + public static final String SETUP_METHOD = "setUp"; + + public static final String TEARDOWN_METHOD = "tearDown"; + + private static final String TEST_SUITE = "junit.framework.TestSuite"; + + private Class[] interfacesImplementedByDynamicProxy; + + private Class testResultClass; + + private Method addListenerMethod; + +// private Method countTestCasesMethod; + +// private Method runMethod; + + private static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; + + private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; + + public JUnit4TestSet( Class testClass ) + throws TestSetFailedException + { + super( testClass ); + + processTestClass(); + } + + private void processTestClass() + throws TestSetFailedException + { + try + { + Class testClass = getTestClass(); + ClassLoader loader = testClass.getClassLoader(); + + testResultClass = loader.loadClass( TEST_RESULT ); + + Class testListenerInterface = loader.loadClass( TEST_LISTENER ); + + Class testInterface = loader.loadClass( TEST ); + + // ---------------------------------------------------------------------- + // Strategy for executing JUnit tests + // + // o look for the suite method and if that is present execute that method + // to get the test object. + // + // o look for test classes that are assignable from TestCase + // + // o look for test classes that only implement the Test interface + // ---------------------------------------------------------------------- + + interfacesImplementedByDynamicProxy = new Class[1]; + + interfacesImplementedByDynamicProxy[0] = testListenerInterface; + + // The interface implemented by the dynamic proxy (TestListener), happens to be + // the same as the param types of TestResult.addTestListener + Class[] addListenerParamTypes = interfacesImplementedByDynamicProxy; + + addListenerMethod = testResultClass.getMethod( ADD_LISTENER_METHOD, addListenerParamTypes ); + + if ( testInterface.isAssignableFrom( testClass ) )//testObject.getClass() ) ) + { +// countTestCasesMethod = testInterface.getMethod( COUNT_TEST_CASES_METHOD, EMPTY_CLASS_ARRAY ); + +// runMethod = testInterface.getMethod( RUN_METHOD, new Class[]{testResultClass} ); + + } + else + { +// countTestCasesMethod = testClass.getMethod( COUNT_TEST_CASES_METHOD, EMPTY_CLASS_ARRAY ); + +// runMethod = testClass.getMethod( RUN_METHOD, new Class[]{testResultClass} ); + } + } + catch ( ClassNotFoundException e ) + { + throw new TestSetFailedException( "JUnit classes not available", e ); + } + catch ( NoSuchMethodException e ) + { + throw new TestSetFailedException( "Class is not a JUnit TestCase", e ); + } + } + + private static Object constructTestObject( Class testClass ) + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, + ClassNotFoundException + { + Object testObject = createInstanceFromSuiteMethod( testClass ); + + if ( testObject == null && testClass.getClassLoader().loadClass( TEST_CASE ).isAssignableFrom( testClass ) ) + { + Class[] constructorParamTypes = {Class.class}; + + Constructor constructor = + testClass.getClassLoader().loadClass( TEST_SUITE ).getConstructor( constructorParamTypes ); + + Object[] constructorParams = {testClass}; + + testObject = constructor.newInstance( constructorParams ); + } + + if ( testObject == null ) + { + Constructor testConstructor = getTestConstructor( testClass ); + + if ( testConstructor.getParameterTypes().length == 0 ) + { + testObject = testConstructor.newInstance( EMPTY_OBJECT_ARRAY ); + } + else + { + testObject = testConstructor.newInstance( new Object[]{testClass.getName()} ); + } + } + return testObject; + } + + private static Object createInstanceFromSuiteMethod( Class testClass ) + throws IllegalAccessException, InvocationTargetException + { + Object testObject = null; + try + { + Method suiteMethod = testClass.getMethod( "suite", EMPTY_CLASS_ARRAY ); + + if ( Modifier.isPublic( suiteMethod.getModifiers() ) && Modifier.isStatic( suiteMethod.getModifiers() ) ) + { + testObject = suiteMethod.invoke( null, EMPTY_CLASS_ARRAY ); + } + } + catch ( NoSuchMethodException e ) + { + // No suite method + } + return testObject; + } + + public void execute( ReporterManager reportManager, ClassLoader loader ) + throws TestSetFailedException + { + Class testClass = getTestClass(); + + try + { + + + JUnitCore juc=new JUnitCore(); + RunListener l= new SurefireRunListener(reportManager); + juc.addListener(l); +// juc.addListener(new TextListener()); + + juc.run(Class.forName(testClass.getName())); + +// Object testObject = constructTestObject( testClass ); +// +// Object instanceOfTestResult = testResultClass.newInstance(); +// +// TestListenerInvocationHandler invocationHandler = +// new TestListenerInvocationHandler( reportManager, instanceOfTestResult, loader ); +// +// Object testListener = +// Proxy.newProxyInstance( loader, interfacesImplementedByDynamicProxy, invocationHandler ); +// +// Object[] addTestListenerParams = {testListener}; +// +// addListenerMethod.invoke( instanceOfTestResult, addTestListenerParams ); +// +// Object[] runParams = {instanceOfTestResult}; +// +// runMethod.invoke( testObject, runParams ); + } + catch ( IllegalArgumentException e ) + { + throw new TestSetFailedException( testClass.getName(), e ); + } +// catch ( InstantiationException e ) +// { +// throw new TestSetFailedException( testClass.getName(), e ); +// } +// catch ( IllegalAccessException e ) +// { +// throw new TestSetFailedException( testClass.getName(), e ); +// } +// catch ( InvocationTargetException e ) +// { +// throw new TestSetFailedException( testClass.getName(), e.getTargetException() ); +// } + catch ( ClassNotFoundException e ) + { + throw new TestSetFailedException( "JUnit classes not available", e ); + } +// catch ( NoSuchMethodException e ) +// { +// throw new TestSetFailedException( "Class is not a JUnit TestCase", e ); +// } + } + + public int getTestCount() + throws TestSetFailedException + { + Class testClass = getTestClass(); + try + { + Object testObject = constructTestObject( testClass ); + +// Integer integer = (Integer) countTestCasesMethod.invoke( testObject, EMPTY_CLASS_ARRAY ); + Integer integer = new Integer(42); + + return integer.intValue(); + } + catch ( IllegalAccessException e ) + { + throw new TestSetFailedException( testClass.getName(), e ); + } + catch ( IllegalArgumentException e ) + { + throw new TestSetFailedException( testClass.getName(), e ); + } + catch ( InvocationTargetException e ) + { + throw new TestSetFailedException( testClass.getName(), e.getTargetException() ); + } + catch ( InstantiationException e ) + { + throw new TestSetFailedException( testClass.getName(), e ); + } + catch ( ClassNotFoundException e ) + { + throw new TestSetFailedException( "JUnit classes not available", e ); + } + catch ( NoSuchMethodException e ) + { + throw new TestSetFailedException( "Class is not a JUnit TestCase", e ); + } + } + + private static Constructor getTestConstructor( Class testClass ) + throws NoSuchMethodException + { + Constructor constructor; + try + { + constructor = testClass.getConstructor( new Class[]{String.class} ); + } + catch ( NoSuchMethodException e ) + { + constructor = testClass.getConstructor( EMPTY_CLASS_ARRAY ); + } + return constructor; + } +} Index: surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit4DirectoryTestSuite.java =================================================================== --- surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit4DirectoryTestSuite.java (Revision 0) +++ surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit4DirectoryTestSuite.java (Revision 0) @@ -0,0 +1,48 @@ +package org.apache.maven.surefire.junit; + +/* + * Copyright 2001-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.util.ArrayList; + +import org.apache.maven.surefire.suite.AbstractDirectoryTestSuite; +import org.apache.maven.surefire.testset.SurefireTestSet; +import org.apache.maven.surefire.testset.TestSetFailedException; + +/** + * Test suite for JUnit tests based on a directory of Java test classes. + * + * @author Brett Porter + */ +public class JUnit4DirectoryTestSuite + extends AbstractDirectoryTestSuite +{ + public JUnit4DirectoryTestSuite( File basedir, ArrayList includes, ArrayList excludes ) + { + super( basedir, includes, excludes ); + } + + protected SurefireTestSet createTestSet( Class testClass, ClassLoader classLoader ) + throws TestSetFailedException + { + SurefireTestSet testSet; + + testSet = new JUnit4TestSet( testClass ); + + return testSet; + } +} Index: surefire-providers/surefire-junit/pom.xml =================================================================== --- surefire-providers/surefire-junit/pom.xml (Revision 431214) +++ surefire-providers/surefire-junit/pom.xml (Arbeitskopie) @@ -27,7 +27,7 @@ junit junit - 3.8.1 + 4.0 Index: pom.xml =================================================================== --- pom.xml (Revision 431214) +++ pom.xml (Arbeitskopie) @@ -68,8 +68,8 @@ maven-compiler-plugin - 1.3 - 1.3 + 1.5 + 1.5