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
This commit is contained in:
jvanzyl
2004-01-10 08:23:42 +00:00
parent 8695eb3c27
commit a526454aa3
3 changed files with 57 additions and 102 deletions

View File

@@ -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.
* <pre>
* java junit.textui.TestRunner [-wait] TestCaseClass
* </pre>
* TestRunner expects the name of a TestCase class as argument.
* If this class defines a static <code>suite</code> method it
* will be invoked and the returned test is run. Otherwise all
* the methods starting with "test" having no arguments are run.
* <p>
* When the wait command line argument is given TestRunner
* waits until the users types RETURN.
* <p>
* 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();
}
}

View File

@@ -6,7 +6,7 @@
<id>surefire-runner</id>
<artifactId>surefire-runner</artifactId>
<name>Surefire Test Runner</name>
<currentVersion>1.0-SNAPSHOT</currentVersion>
<currentVersion>1.0</currentVersion>
<description/>
<shortDescription>Run JUnit tests</shortDescription>
<url>http://maven.apache.org/reference/plugins/surefire-runner</url>

View File

@@ -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.
* <pre>
* java junit.textui.TestRunner [-wait] TestCaseClass
* </pre>
* TestRunner expects the name of a TestCase class as argument.
* If this class defines a static <code>suite</code> method it
* will be invoked and the returned test is run. Otherwise all
* the methods starting with "test" having no arguments are run.
* <p>
* When the wait command line argument is given TestRunner
* waits until the users types RETURN.
* <p>
* 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;
}
}