From a526454aa35344bd8b1ff90222b9ccabd6f615e9 Mon Sep 17 00:00:00 2001 From: jvanzyl Date: Sat, 10 Jan 2004 08:23:42 +0000 Subject: [PATCH] o remove the hardwiring to my setup o remove dep on plexus-utils in the test runner git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@114581 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/maven/test/TestRunnerBooter.java | 77 +++++++++++------- surefire/src/surefire-runner/project.xml | 2 +- .../org/apache/maven/test/TestRunner.java | 80 ++----------------- 3 files changed, 57 insertions(+), 102 deletions(-) diff --git a/surefire/src/main/org/apache/maven/test/TestRunnerBooter.java b/surefire/src/main/org/apache/maven/test/TestRunnerBooter.java index aa66d1b8..21dc8093 100644 --- a/surefire/src/main/org/apache/maven/test/TestRunnerBooter.java +++ b/surefire/src/main/org/apache/maven/test/TestRunnerBooter.java @@ -3,29 +3,15 @@ package org.apache.maven.test; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Dependency; import org.apache.maven.project.Project; +import org.codehaus.plexus.util.DirectoryScanner; +import org.codehaus.plexus.util.StringUtils; import java.io.File; +import java.lang.reflect.Method; import java.net.MalformedURLException; import java.util.Iterator; import java.util.List; -import java.lang.reflect.Method; -/** - * A command line based tool to run tests. - *
- * java junit.textui.TestRunner [-wait] TestCaseClass
- * 
- * TestRunner expects the name of a TestCase class as argument. - * If this class defines a static suite method it - * will be invoked and the returned test is run. Otherwise all - * the methods starting with "test" having no arguments are run. - *

- * When the wait command line argument is given TestRunner - * waits until the users types RETURN. - *

- * TestRunner prints a trace as the tests are executed followed by a - * summary at the end. - */ public class TestRunnerBooter { private Project project; @@ -40,8 +26,6 @@ public class TestRunnerBooter { // maven.build.dest // maven.test.dest - // maven.build.dest - // maven.test.dest IsolatedClassLoader classLoader = new IsolatedClassLoader(); @@ -51,9 +35,11 @@ public class TestRunnerBooter Thread.currentThread().setContextClassLoader( classLoader ); - classLoader.addURL( new File ( "/home/jvanzyl/maven-repo-local/junit/jars/junit-3.8.1.jar" ).toURL() ); + String mavenRepoLocal = project.getProperty( "maven.repo.local" ); - classLoader.addURL( new File ( "/tmp/test-runner-1.4.jar" ).toURL() ); + classLoader.addURL( new File ( mavenRepoLocal, "junit/jars/junit-3.8.1.jar" ).toURL() ); + + classLoader.addURL( new File ( mavenRepoLocal, "maven/jars/surefire-runner-1.0.jar" ).toURL() ); classLoader.addURL( new File( basedir, "target/classes/" ).toURL() ); @@ -61,21 +47,22 @@ public class TestRunnerBooter processDependencies( project, classLoader ); + String[] tests = collectTests( basedir, + project.getBuild().getUnitTest().getIncludes(), + project.getBuild().getUnitTest().getExcludes() ); + Class testRunnerClass = classLoader.loadClass( "org.apache.maven.test.TestRunner" ); Object testRunner = testRunnerClass.newInstance(); Method m = testRunnerClass.getMethod( "runTestClasses", new Class[] { - File.class, - List.class, - List.class, - ClassLoader.class } ); + ClassLoader.class, + String[].class + } ); m.invoke( testRunner, new Object[]{ - basedir, - project.getBuild().getUnitTest().getIncludes(), - project.getBuild().getUnitTest().getExcludes(), classLoader, + tests } ); } @@ -106,4 +93,38 @@ public class TestRunnerBooter } } } + + public String[] collectTests( File basedir, List includes, List excludes ) + throws Exception + { + DirectoryScanner scanner = new DirectoryScanner(); + + scanner.setBasedir( new File( basedir, "target/test-classes" ) ); + + String[] incs = new String[includes.size()]; + + for ( int i = 0; i < incs.length; i++ ) + { + incs[i] = StringUtils.replace( (String) includes.get( i ), "java", "class" ); + } + + scanner.setIncludes( incs ); + + String[] excls = new String[excludes.size() + 1]; + + for ( int i = 0; i < excls.length - 1; i++ ) + { + excls[i] = StringUtils.replace( (String) excludes.get( i ), "java", "class" ); + } + + // Exclude inner classes + + excls[excludes.size()] = "**/*$*"; + + scanner.setExcludes( excls ); + + scanner.scan(); + + return scanner.getIncludedFiles(); + } } diff --git a/surefire/src/surefire-runner/project.xml b/surefire/src/surefire-runner/project.xml index 991bc4f6..1269664d 100644 --- a/surefire/src/surefire-runner/project.xml +++ b/surefire/src/surefire-runner/project.xml @@ -6,7 +6,7 @@ surefire-runner surefire-runner Surefire Test Runner - 1.0-SNAPSHOT + 1.0 Run JUnit tests http://maven.apache.org/reference/plugins/surefire-runner diff --git a/surefire/src/surefire-runner/src/main/org/apache/maven/test/TestRunner.java b/surefire/src/surefire-runner/src/main/org/apache/maven/test/TestRunner.java index 08e48156..13c0d867 100644 --- a/surefire/src/surefire-runner/src/main/org/apache/maven/test/TestRunner.java +++ b/surefire/src/surefire-runner/src/main/org/apache/maven/test/TestRunner.java @@ -5,33 +5,9 @@ import junit.framework.TestResult; import junit.framework.TestSuite; import junit.runner.BaseTestRunner; import junit.runner.TestSuiteLoader; -import org.codehaus.plexus.util.DirectoryScanner; -import org.codehaus.plexus.util.StringUtils; -import java.io.File; import java.io.PrintStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Iterator; -import java.util.List; -/** - * A command line based tool to run tests. - *

- * java junit.textui.TestRunner [-wait] TestCaseClass
- * 
- * TestRunner expects the name of a TestCase class as argument. - * If this class defines a static suite method it - * will be invoked and the returned test is run. Otherwise all - * the methods starting with "test" having no arguments are run. - *

- * When the wait command line argument is given TestRunner - * waits until the users types RETURN. - *

- * TestRunner prints a trace as the tests are executed followed by a - * summary at the end. - */ public class TestRunner extends BaseTestRunner { @@ -61,57 +37,21 @@ public class TestRunner } private ClassLoader classLoader; - private List includes; - private List excludes; - private File basedir; - - public void runTestClasses( File basedir, List includes, List excludes, ClassLoader classLoader ) + + public void runTestClasses( ClassLoader classLoader, String[] tests ) throws Exception { - this.basedir = basedir; - this.includes = includes; - this.excludes = excludes; this.classLoader = classLoader; - - DirectoryScanner scanner = new DirectoryScanner(); - - scanner.setBasedir( new File( basedir, "target/test-classes" ) ); - - String[] incs = new String[includes.size()]; - - for ( int i = 0; i < incs.length; i++ ) - { - incs[i] = StringUtils.replace( (String) includes.get( i ), "java", "class" ); - } - - scanner.setIncludes( incs ); - - String[] excls = new String[excludes.size() + 1]; - - for ( int i = 0; i < excls.length - 1; i++ ) - { - excls[i] = StringUtils.replace( (String) excludes.get( i ), "java", "class" ); - } - - // Exclude inner classes - - excls[excludes.size()] = "**/*$*"; - - scanner.setExcludes( excls ); - - scanner.scan(); - - String[] files = scanner.getIncludedFiles(); String s; - for ( int i = 0; i < files.length; i++ ) + for ( int i = 0; i < tests.length; i++ ) { - s = files[i]; + s = tests[i]; s = s.substring( 0, s.indexOf( "." ) ); - s = StringUtils.replace( s, FS, "." ); + s = s.replace( FS.charAt( 0 ), ".".charAt( 0 ) ); Class clazz = null; @@ -119,7 +59,7 @@ public class TestRunner { clazz = classLoader.loadClass( s ); } - catch( ClassNotFoundException e ) + catch ( ClassNotFoundException e ) { System.out.println( "Can't find className = " + s ); @@ -142,14 +82,10 @@ public class TestRunner return failures; } - // ---------------------------------------------------------------------- - // - // ---------------------------------------------------------------------- - public TestResult run( Class testClass, String testName ) { return run( new TestSuite( testClass ), testName ); - + //return run( new MavenJUnitTestSuite( testClass, classLoader ), testName ); } @@ -210,6 +146,4 @@ public class TestRunner { resultPrinter = printer; } - - }