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
131 lines
3.8 KiB
Java
131 lines
3.8 KiB
Java
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;
|
|
|
|
public class TestRunnerBooter
|
|
{
|
|
private Project project;
|
|
|
|
public void setProject( Project project )
|
|
{
|
|
this.project = project;
|
|
}
|
|
|
|
public void execute()
|
|
throws Exception
|
|
{
|
|
// maven.build.dest
|
|
// maven.test.dest
|
|
|
|
IsolatedClassLoader classLoader = new IsolatedClassLoader();
|
|
|
|
File basedir = project.getFile().getParentFile();
|
|
|
|
System.setProperty( "basedir", basedir.getPath() );
|
|
|
|
Thread.currentThread().setContextClassLoader( classLoader );
|
|
|
|
String mavenRepoLocal = project.getProperty( "maven.repo.local" );
|
|
|
|
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() );
|
|
|
|
classLoader.addURL( new File( basedir, "target/test-classes/" ).toURL() );
|
|
|
|
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[] {
|
|
ClassLoader.class,
|
|
String[].class
|
|
} );
|
|
|
|
m.invoke( testRunner, new Object[]{
|
|
classLoader,
|
|
tests
|
|
} );
|
|
}
|
|
|
|
// We need to deal with populating a plugin classloader with its requirements.
|
|
private void processDependencies( Project project, IsolatedClassLoader classLoader )
|
|
throws MalformedURLException
|
|
{
|
|
if ( project.getArtifacts() == null )
|
|
{
|
|
return;
|
|
}
|
|
|
|
for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
|
|
{
|
|
Artifact artifact = (Artifact) i.next();
|
|
|
|
Dependency dependency = artifact.getDependency();
|
|
|
|
// Only add compile type dependencies to classloader
|
|
if ( dependency.getType().equals( "jar" ) )
|
|
{
|
|
// We have the jar and the classloader to push it into so
|
|
// lets do it!
|
|
if ( artifact.exists() )
|
|
{
|
|
classLoader.addURL( artifact.getFile().toURL() );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|