From c0a63883372e8f726d4fe3169e60a31e817c3ede Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Mon, 20 Sep 2004 03:14:22 +0000 Subject: [PATCH] o active the new text file reports which capture any stack traces that occur as well as stdout and std err o there is a new parameter that is accepted so you can use a simplifed regex from the command line m2 surefire:test -D=test=FooTest is implicitly **/FooTest.java and the directory scanner is used so you can this to run a single test if you like or a set of tests: m2 surefire:test -Dtest=*Foo* is implicitly **/*Foo*.java You can also specify a comma separated list if you wish: m2 surefire:test -Dtest=*Foo*,*Bar* whic is implicitly **/*Foo*.java,**/*Bar*.java This just makes running a smaller set of tests easier while trying to pin point problems. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163120 13f79535-47bb-0310-9956-ffa450edef68 --- maven-plugins/maven-surefire-plugin/pom.xml | 4 +- .../org/apache/maven/test/SurefirePlugin.java | 125 ++++++++++++++++-- 2 files changed, 116 insertions(+), 13 deletions(-) diff --git a/maven-plugins/maven-surefire-plugin/pom.xml b/maven-plugins/maven-surefire-plugin/pom.xml index b7bcfd1c54..acd9453e4e 100644 --- a/maven-plugins/maven-surefire-plugin/pom.xml +++ b/maven-plugins/maven-surefire-plugin/pom.xml @@ -17,12 +17,12 @@ surefire surefire - 1.1 + 1.2-SNAPSHOT surefire surefire-booter - 1.1 + 1.2-SNAPSHOT diff --git a/maven-plugins/maven-surefire-plugin/src/main/java/org/apache/maven/test/SurefirePlugin.java b/maven-plugins/maven-surefire-plugin/src/main/java/org/apache/maven/test/SurefirePlugin.java index 3803fd7446..84137ba9bf 100644 --- a/maven-plugins/maven-surefire-plugin/src/main/java/org/apache/maven/test/SurefirePlugin.java +++ b/maven-plugins/maven-surefire-plugin/src/main/java/org/apache/maven/test/SurefirePlugin.java @@ -7,6 +7,12 @@ import org.apache.maven.plugin.PluginExecutionResponse; import java.io.File; import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.StringTokenizer; +import java.lang.reflect.Array; /** * @goal test @@ -67,6 +73,20 @@ import java.util.List; * validator="" * expression="#project.classpathElements" * description="" + * @parameter + * name="reportsDirectory" + * type="String" + * required="false" + * validator="" + * expression="#project.build.directory/surefire-reports" + * description="Base directory where all reports are written to." + * @parameter + * name="test" + * type="String" + * required="false" + * validator="" + * expression="#test" + * description="Specify this parameter if you want to use the test regex notation to select tests to run." * * @author Jason van Zyl * @version $Id$ @@ -92,26 +112,60 @@ public class SurefirePlugin String testClassesDirectory = (String) request.getParameter( "testClassesDirectory" ); - List includes = (List) request.getParameter( "includes" ); - - List excludes = (List) request.getParameter( "excludes" ); - String[] classpathElements = (String[]) request.getParameter( "classpathElements" ); + String reportsDirectory = (String) request.getParameter( "reportsDirectory" ); + + String test = (String) request.getParameter( "test" ); + + // ---------------------------------------------------------------------- + // Setup the surefire booter + // ---------------------------------------------------------------------- + + SurefireBooter surefireBooter = new SurefireBooter(); + + surefireBooter.setReportsDirectory( reportsDirectory ); + + // ---------------------------------------------------------------------- + // Check to see if we are running a single test. The raw parameter will + // come through if it has not been set. + // ---------------------------------------------------------------------- + + if ( !test.equals( "#test" ) ) + { + // FooTest -> **/FooTest.java + + List includes = new ArrayList(); + + List excludes = new ArrayList(); + + String[] testRegexes = split( test, ",", -1 ); + + for ( int i = 0; i < testRegexes.length; i++ ) + { + includes.add( "**/" + testRegexes[i] + ".java" ); + } + + surefireBooter.addBattery( "org.codehaus.surefire.battery.DirectoryBattery", new Object[]{basedir, includes, excludes} ); + } + else + { + List includes = (List) request.getParameter( "includes" ); + + List excludes = (List) request.getParameter( "excludes" ); + + surefireBooter.addBattery( "org.codehaus.surefire.battery.DirectoryBattery", new Object[]{basedir, includes, excludes} ); + } + // ---------------------------------------------------------------------- // // ---------------------------------------------------------------------- System.setProperty( "basedir", basedir ); - SurefireBooter surefireBooter = new SurefireBooter(); - - surefireBooter.addBattery( "org.codehaus.surefire.battery.DirectoryBattery", new Object[]{basedir, includes, excludes} ); - - // TODO: there should be a better way to construct a classpath surefireBooter.addClassPathUrl( new File( mavenRepoLocal, "junit/jars/junit-3.8.1.jar" ).getPath() ); - surefireBooter.addClassPathUrl( new File( mavenRepoLocal, "surefire/jars/surefire-1.1.jar" ).getPath() ); + surefireBooter.addClassPathUrl( new File( mavenRepoLocal, "surefire/jars/surefire-1.2-SNAPSHOT.jar" ).getPath() ); surefireBooter.addClassPathUrl( new File( classesDirectory ).getPath() ); @@ -122,7 +176,9 @@ public class SurefirePlugin surefireBooter.addClassPathUrl( classpathElements[i] ); } - surefireBooter.addReport( "org.codehaus.surefire.report.ConsoleReport" ); + surefireBooter.addReport( "org.codehaus.surefire.report.ConsoleReporter" ); + + surefireBooter.addReport( "org.codehaus.surefire.report.FileReporter" ); boolean success = surefireBooter.run(); @@ -131,4 +187,51 @@ public class SurefirePlugin response.setExecutionFailure( true, new SurefireFailureResponse( null ) ); } } + + protected String[] split( String str, String separator, int max ) + { + StringTokenizer tok = null; + if ( separator == null ) + { + // Null separator means we're using StringTokenizer's default + // delimiter, which comprises all whitespace characters. + tok = new StringTokenizer( str ); + } + else + { + tok = new StringTokenizer( str, separator ); + } + + int listSize = tok.countTokens(); + if ( max > 0 && listSize > max ) + { + listSize = max; + } + + String[] list = new String[listSize]; + int i = 0; + int lastTokenBegin = 0; + int lastTokenEnd = 0; + while ( tok.hasMoreTokens() ) + { + if ( max > 0 && i == listSize - 1 ) + { + // In the situation where we hit the max yet have + // tokens left over in our input, the last list + // element gets all remaining text. + String endToken = tok.nextToken(); + lastTokenBegin = str.indexOf( endToken, lastTokenEnd ); + list[i] = str.substring( lastTokenBegin ); + break; + } + else + { + list[i] = tok.nextToken(); + lastTokenBegin = str.indexOf( list[i], lastTokenEnd ); + lastTokenEnd = lastTokenBegin + list[i].length(); + } + i++; + } + return list; + } }