Author: jkuhnert Date: Sat May 5 10:21:35 2007 New Revision: 535549 URL: http://svn.apache.org/viewvc?view=rev&rev=535549 Log: First pass at adding support for TestNG 5.6 and the supposed "future compatibility" features which rely on using a java.util.Properties map to configure testng.
Added: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test/java/TestSuiteRun.java (with props) Modified: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/pom.xml maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test-data/testng.xml maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/pom.xml maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/src/test/java/TestNGTest.java maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java maven/sandbox/branches/surefire/surefire-collaboration/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java Modified: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/pom.xml?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/pom.xml (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/pom.xml Sat May 5 10:21:35 2007 @@ -33,7 +33,7 @@ <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> - <version>5.5</version> + <version>5.6</version> <classifier>jdk15</classifier> <scope>test</scope> </dependency> @@ -45,9 +45,29 @@ <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> + <!-- <suiteXmlFiles> <file>src/test-data/testng.xml</file> </suiteXmlFiles> + --> + <properties> + <property> + <name>log</name> + <value>0</value> + </property> + <property> + <name>testng.suite.definitions</name> + <value>src/test-data/testng.xml</value> + </property> + </properties> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.5</source> + <target>1.5</target> </configuration> </plugin> @@ -69,5 +89,13 @@ </plugins> </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-report-plugin</artifactId> + </plugin> + </plugins> + </reporting> </project> Modified: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test-data/testng.xml URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test-data/testng.xml?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test-data/testng.xml (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test-data/testng.xml Sat May 5 10:21:35 2007 @@ -21,10 +21,11 @@ <suite name="Suite 1 Test" verbose="4"> - <test name="Sample Test"> - <classes> - <class name="TestNGSuiteTest"/> - </classes> - </test> + <test name="Sample Test"> + <classes> + <class name="TestNGSuiteTest" /> + <class name="TestSuiteRun" /> + </classes> + </test> </suite> Added: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test/java/TestSuiteRun.java URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test/java/TestSuiteRun.java?view=auto&rev=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test/java/TestSuiteRun.java (added) +++ maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test/java/TestSuiteRun.java Sat May 5 10:21:35 2007 @@ -0,0 +1,13 @@ +import org.testng.annotations.Test; + +/** + * + */ [EMAIL PROTECTED] +public class TestSuiteRun { + + public void test_Run() + { + assert true; + } +} Propchange: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test4/src/test/java/TestSuiteRun.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/pom.xml URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/pom.xml?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/pom.xml (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/pom.xml Sat May 5 10:21:35 2007 @@ -21,44 +21,44 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> + <modelVersion>4.0.0</modelVersion> - <groupId>org.apache.maven.plugins.surefire</groupId> - <artifactId>test6</artifactId> - <version>1.0-SNAPSHOT</version> - <name>TestNG group/parallel thread tests</name> - <description>Tests group/thread/parallel functionality of TestNG</description> + <groupId>org.apache.maven.plugins.surefire</groupId> + <artifactId>test6</artifactId> + <version>1.0-SNAPSHOT</version> + <name>TestNG group/parallel thread tests</name> + <description>Tests group/thread/parallel functionality of TestNG</description> - <dependencies> - <dependency> - <groupId>org.testng</groupId> - <artifactId>testng</artifactId> - <version>5.5</version> - <classifier>jdk15</classifier> - <scope>test</scope> - </dependency> - </dependencies> + <dependencies> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>5.5</version> + <classifier>jdk15</classifier> + <scope>test</scope> + </dependency> + </dependencies> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-surefire-plugin</artifactId> - <configuration> - <groups>functional</groups> - <threadCount>3</threadCount> - <parallel>methods</parallel> - </configuration> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <configuration> - <source>1.5</source> - <target>1.5</target> - </configuration> - </plugin> - </plugins> - </build> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <groups>functional</groups> + <threadCount>3</threadCount> + <parallel>methods</parallel> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.5</source> + <target>1.5</target> + </configuration> + </plugin> + </plugins> + </build> </project> Modified: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/src/test/java/TestNGTest.java URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/src/test/java/TestNGTest.java?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/src/test/java/TestNGTest.java (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/it/test6/src/test/java/TestNGTest.java Sat May 5 10:21:35 2007 @@ -25,7 +25,7 @@ public void check_Test_Count() { assert m_testCount == 3 : "Expected 3 tests to be run but local count was " + m_testCount; - } + } Object testObject; Modified: maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/maven-surefire-plugin/src/main/java/org/apache/maven/plugin/surefire/SurefirePlugin.java Sat May 5 10:21:35 2007 @@ -170,6 +170,14 @@ private Properties systemProperties; /** + * List of properties for configuring all TestNG related configurations. This is the new + * preferred method of configuring TestNG. + * + * @parameter + */ + private Properties properties; + + /** * Map of of plugin artifacts. * * @parameter expression="${plugin.artifactMap}" @@ -309,7 +317,7 @@ * * @parameter expression="${parallel}" * default-value="false" - * + * * @todo test how this works with forking, and console/file output parallelism */ private String parallel; @@ -446,6 +454,44 @@ return true; } + /** + * Converts old TestNG configuration parameters over to new properties based configuration + * method. (if any are defined the old way) + */ + void convertTestNGParameters() + { + if ( properties == null ) + { + properties = new Properties(); + } + + if (this.parallel != null) + properties.put("parallel", this.parallel); + if (this.excludedGroups != null) + properties.put("excludegroups", this.excludedGroups); + if (this.groups != null) + properties.put("groups", this.groups); + if (this.threadCount > 0) + properties.put("threadcount", Integer.valueOf(this.threadCount)); + + if (this.suiteXmlFiles != null) { + String list = ""; + for (int i=0; i < this.suiteXmlFiles.length; i++) { + list += this.suiteXmlFiles[i].getAbsolutePath(); + + if ((i + 1) < this.suiteXmlFiles.length) + list += ","; + } + + properties.put("testng.suite.definitions", list); + } + + if (this.testSourceDirectory != null) + properties.put("sourcedir", testSourceDirectory.getAbsolutePath()); + if (this.testClassesDirectory != null) + properties.put("testng.test.classpath", testClassesDirectory.getAbsolutePath()); + } + private SurefireBooter constructSurefireBooter() throws MojoExecutionException, MojoFailureException { @@ -484,7 +530,10 @@ // The plugin uses a JDK based profile to select the right testng. We might be explicity using a // different one since its based on the source level, not the JVM. Prune using the filter. + addProvider( surefireBooter, "surefire-testng", surefireArtifact.getBaseVersion(), testNgArtifact ); + + convertTestNGParameters(); } else if ( junitArtifact != null && junitArtifact.getBaseVersion().startsWith( "4" ) ) { @@ -511,24 +560,45 @@ throw new MojoExecutionException( "Error to resolving surefire provider dependency: " + e.getMessage(), e ); } - if ( suiteXmlFiles != null && suiteXmlFiles.length > 0 ) + if ( testNgArtifact != null && properties != null && properties.get("testng.suite.definitions") != null) { - if ( testNgArtifact == null ) + try { + + VersionRange range = VersionRange.createFromVersionSpec( "[5.6,)" ); + if ( !range.containsVersion( testNgArtifact.getSelectedVersion() ) && suiteXmlFiles != null) + { + // if earlier than 5.6 need to construct and execute the old non-properties way + + for ( int i = 0; i < suiteXmlFiles.length; i++ ) + { + File file = suiteXmlFiles[i]; + if ( file.exists() ) + { + surefireBooter.addTestSuite( "org.apache.maven.surefire.testng.TestNGXmlTestSuite", + new Object[]{file, testSourceDirectory.getAbsolutePath()} ); + } + } + } else { + + // do it the new way + + surefireBooter.addTestSuite( "org.apache.maven.surefire.testng.TestNGXmlTestSuite", new Object[]{ properties } ); + } + + } + catch ( InvalidVersionSpecificationException e ) { - throw new MojoExecutionException( "suiteXmlFiles is configured, but there is no TestNG dependency" ); + throw new MojoExecutionException( "Error determining the TestNG version requested: " + e.getMessage(), e ); } - for ( int i = 0; i < suiteXmlFiles.length; i++ ) + catch ( ArtifactResolutionException e ) { - File file = suiteXmlFiles[i]; - if ( file.exists() ) - { - surefireBooter.addTestSuite( "org.apache.maven.surefire.testng.TestNGXmlTestSuite", - new Object[]{file, testSourceDirectory.getAbsolutePath()} ); - } + throw new MojoExecutionException( "Error to resolving surefire provider dependency: " + e.getMessage(), e ); } } + else { + List includes; List excludes; @@ -561,20 +631,44 @@ if ( includes == null || includes.size() == 0 ) { includes = new ArrayList( - Arrays.asList( new String[]{"**/*Test*.java", "**/*Test.java", "**/*TestCase.java"} ) ); + Arrays.asList( new String[]{"**/*Test*.java", "**/*Test.java", "**/*TestCase.java"} ) ); } if ( excludes == null || excludes.size() == 0 ) { excludes = new ArrayList( - Arrays.asList( new String[]{"**/Abstract*Test.java", "**/Abstract*TestCase.java", "**/*$*"} ) ); + Arrays.asList( new String[]{"**/Abstract*Test.java", "**/Abstract*TestCase.java", "**/*$*"} ) ); } } if ( testNgArtifact != null ) { - surefireBooter.addTestSuite( "org.apache.maven.surefire.testng.TestNGDirectoryTestSuite", new Object[]{ - testClassesDirectory, includes, excludes, groups, excludedGroups, parallel, - new Integer( threadCount ), testSourceDirectory.getAbsolutePath()} ); + + try { + + VersionRange range = VersionRange.createFromVersionSpec( "[5.6,)" ); + if ( !range.containsVersion( testNgArtifact.getSelectedVersion() )) + { + // if earlier than 5.6 need to construct and execute the old non-properties way + + surefireBooter.addTestSuite( "org.apache.maven.surefire.testng.TestNGDirectoryTestSuite", new Object[]{ + testClassesDirectory, includes, excludes, groups, excludedGroups, Boolean.valueOf( parallel ), + new Integer( threadCount ), testSourceDirectory.getAbsolutePath()} ); + + } else { + + surefireBooter.addTestSuite( "org.apache.maven.surefire.testng.TestNGDirectoryTestSuite", + new Object[] { testClassesDirectory, includes, excludes, properties }); + } + + } + catch ( InvalidVersionSpecificationException e ) + { + throw new MojoExecutionException( "Error determining the TestNG version requested: " + e.getMessage(), e ); + } + catch ( ArtifactResolutionException e ) + { + throw new MojoExecutionException( "Error to resolving surefire provider dependency: " + e.getMessage(), e ); + } } else { @@ -591,8 +685,9 @@ // fall back to JUnit, which also contains POJO support. Also it can run // classes compiled against JUnit since it has a dependency on JUnit itself. surefireBooter.addTestSuite( junitDirectoryTestSuite, - new Object[]{testClassesDirectory, includes, excludes} ); + new Object[]{testClassesDirectory, includes, excludes} ); } + } // ---------------------------------------------------------------------- Modified: maven/sandbox/branches/surefire/surefire-collaboration/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SurefireBooter.java Sat May 5 10:21:35 2007 @@ -20,12 +20,7 @@ */ import org.apache.maven.surefire.Surefire; -import org.apache.maven.surefire.booter.output.FileOutputConsumerProxy; -import org.apache.maven.surefire.booter.output.ForkingStreamConsumer; -import org.apache.maven.surefire.booter.output.OutputConsumer; -import org.apache.maven.surefire.booter.output.StandardOutputConsumer; -import org.apache.maven.surefire.booter.output.SupressFooterOutputConsumerProxy; -import org.apache.maven.surefire.booter.output.SupressHeaderOutputConsumerProxy; +import org.apache.maven.surefire.booter.output.*; import org.apache.maven.surefire.testset.TestSetFailedException; import org.apache.maven.surefire.util.NestedRuntimeException; import org.apache.maven.surefire.util.UrlUtils; @@ -35,22 +30,12 @@ import org.codehaus.plexus.util.cli.CommandLineUtils; import org.codehaus.plexus.util.cli.StreamConsumer; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; /** * @author Jason van Zyl @@ -655,6 +640,31 @@ return list; } + private static Properties processPropertiesList( String propertiesList ) + { + String sl = propertiesList; + + if ( sl.startsWith( "{" ) && sl.endsWith( "}" ) ) + { + sl = sl.substring( 1, sl.length() - 1 ); + } + + Properties props = new Properties(); + + String[] stringArray = StringUtils.split( sl, "," ); + + for ( int i = 0; i < stringArray.length; i++ ) + { + String[] keyValue = StringUtils.split( stringArray[i], "="); + if (keyValue.length != 2) + throw new IllegalStateException("Expected to find key/value pair in configuration string: " + stringArray[i]); + + props.put(keyValue[0].trim(), keyValue[1].trim()); + } + + return props; + } + private static Properties loadProperties( File file ) throws IOException { @@ -728,10 +738,14 @@ { paramObjects[i] = Integer.valueOf( params[i] ); } + else if ( types[i].equals( Properties.class.getName() ) ) + { + paramObjects[i] = processPropertiesList(params[i]); + } else { // TODO: could attempt to construct with a String constructor if needed - throw new IllegalArgumentException( "Unknown parameter type: " + types[i] ); + throw new IllegalArgumentException( "Unknown parameter type: " + types[i]); } } } Modified: maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java Sat May 5 10:21:35 2007 @@ -51,7 +51,16 @@ private int threadCount; private String testSourceDirectory; - + + private Properties config; + + public TestNGDirectoryTestSuite( File basedir, ArrayList includes, ArrayList excludes, Properties config) + { + super( basedir, includes, excludes ); + + this.config = config; + } + public TestNGDirectoryTestSuite( File basedir, ArrayList includes, ArrayList excludes, String groups, String excludedGroups, String parallel, Integer threadCount, String testSourceDirectory ) @@ -70,12 +79,31 @@ } + public TestNGDirectoryTestSuite( File basedir, ArrayList includes, ArrayList excludes, String groups, + String excludedGroups, Boolean parallel, Integer threadCount, + String testSourceDirectory ) + { + super( basedir, includes, excludes ); + + this.groups = groups; + + this.excludedGroups = excludedGroups; + + this.parallel = parallel.toString(); + + this.threadCount = threadCount.intValue(); + + this.testSourceDirectory = testSourceDirectory; + + } + public Map locateTestSets( ClassLoader classLoader ) throws TestSetFailedException { // TODO: fix // override classloader. That keeps us all together for now, which makes it work, but could pose problems of // classloader separation if the tests use plexus-utils. + return super.locateTestSets( classLoader ); } @@ -98,6 +126,22 @@ throw new TestSetFailedException( "Unable to find test set '" + testSetName + "' in suite" ); } + if ( config != null ) + { + config.put("classloader", classLoader); + config.put("testclass", testSet.getTestClass() ); + config.put("suitename", "Single Test"); + config.put("testname", testSet.getName() ); + + TestNGExecutor.executeTestNG( this, config, reporterManager ); + + config.remove("classloader"); + config.remove("testclass"); + config.remove("suitename"); + config.remove("testname"); + return; + } + XmlSuite suite = new XmlSuite(); suite.setThreadCount(threadCount); @@ -125,6 +169,36 @@ throw new IllegalStateException( "You must call locateTestSets before calling execute" ); } + if ( config != null ) + { + StringBuffer classNames = new StringBuffer(); + for ( Iterator i = testSets.values().iterator(); i.hasNext(); ) + { + SurefireTestSet testSet = (SurefireTestSet) i.next(); + + classNames.append(testSet.getTestClass().getName()); + + if (i.hasNext()) + { + classNames.append(","); + } + } + + config.put("classloader", classLoader); + config.put("testclass", classNames.toString() ); + config.put("suitename", "Directory Suite"); + config.put("testname", "Directory tests"); + + TestNGExecutor.executeTestNG( this, config, reporterManager ); + + config.remove("classloader"); + config.remove("testclass"); + config.remove("suitename"); + config.remove("testname"); + + return; + } + XmlSuite suite = new XmlSuite(); suite.setThreadCount(threadCount); @@ -165,8 +239,6 @@ xmlTest.setExcludedGroups( Arrays.asList( excludedGroups.split( "," ) ) ); } - // if ( !TestNGClassFinder.isTestNGClass( testSet.getTestClass(), annotationFinder ) ) - // TODO: this is a bit dodgy, but isTestNGClass wasn't working try { Class junitClass = Class.forName( "junit.framework.Test" ); @@ -198,9 +270,7 @@ TestNGReporter reporter = new TestNGReporter( reporterManager, this ); testNG.addListener( (ITestListener) reporter ); testNG.addListener( (ISuiteListener) reporter ); - - TestNGExecutor.configureJreType(testNG, testSourceDirectory); - + // Set source path so testng can find javadoc annotations if not in 1.5 jvm if ( testSourceDirectory != null ) { @@ -210,10 +280,12 @@ // workaround for SUREFIRE-49 // TestNG always creates an output directory, and if not set the name for the directory is "null" testNG.setOutputDirectory( System.getProperty( "java.io.tmpdir" ) ); - + + TestNGExecutor.configureJreType(testNG, testSourceDirectory); + testNG.runSuitesLocally(); - // need to execute report end after testng has completely finished as the + // need to execute report end after testng has completely finished as the // reporter methods don't get called in the order that would allow for capturing // failures that happen in before/after suite configuration methods Modified: maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java Sat May 5 10:21:35 2007 @@ -30,6 +30,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; +import java.util.Properties; /** * Contains utility methods for executing TestNG. @@ -87,16 +88,31 @@ try { String jre = System.getProperty("java.vm.version"); - Method annotType = TestNGExecutor.getMethod(testNG.getClass(), "setAnnotations", 1); + if (annotType != null) { annotType.invoke(testNG, new Object[]{ jre.indexOf("1.4") > -1 ? "javadoc" : "jdk"}); - Method init = testNG.getClass().getDeclaredMethod("initializeAnnotationFinders", new Class[0]); + Method init = testNG.getClass().getDeclaredMethod("initializeListeners", new Class[0]); + init.setAccessible(true); + init.invoke(testNG, new Object[0]); + + init = testNG.getClass().getDeclaredMethod("initializeAnnotationFinders", new Class[0]); + init.setAccessible(true); + init.invoke(testNG, new Object[0]); + + init = testNG.getClass().getDeclaredMethod("initializeCommandLineSuites", new Class[0]); + init.setAccessible(true); + init.invoke(testNG, new Object[0]); + + init = testNG.getClass().getDeclaredMethod("initializeCommandLineSuitesParams", new Class[0]); + init.setAccessible(true); + init.invoke(testNG, new Object[0]); + + init = testNG.getClass().getDeclaredMethod("initializeCommandLineSuitesGroups", new Class[0]); init.setAccessible(true); init.invoke(testNG, new Object[0]); - } else if (Class.forName("org.testng.internal.annotations.AnnotationConfiguration") != null && AnnotationConfiguration.class.getMethod("getInstance", new Class[0]) != null) { @@ -106,14 +122,16 @@ } else { AnnotationConfiguration.getInstance().initialize(AnnotationConfiguration.JVM_15_CONFIG); } + } else { + throw new IllegalStateException("Unable to configure TestNG jre type."); } } catch (Throwable t) { throw new RuntimeException(t); } } - static void executeTestNG( SurefireTestSuite surefireSuite, String testSourceDirectory, XmlSuite suite, - ReporterManager reporterManager ) + static void executeTestNG( SurefireTestSuite surefireSuite, String testSourceDirectory, + XmlSuite suite, ReporterManager reporterManager ) { TestNG testNG = new TestNG( false ); @@ -147,5 +165,27 @@ // failures that happen in before/after suite configuration methods reporter.cleanupAfterTestsRun(); + } + + static void executeTestNG( SurefireTestSuite suite, Properties config, ReporterManager reporterManager ) + { + TestNG testNG = new TestNG( false ); + + Method execMethod = getMethod(testNG.getClass(), "configureAndRun", 1); + if (execMethod == null) + throw new IllegalArgumentException("Unable to find method <configureAndRun(Map)> on TestNG class provided"); + + TestNGReporter reporter = new TestNGReporter( reporterManager, suite ); + config.put("listener", reporter); + config.put("suitelistener", reporter); + + try { + + execMethod.invoke(testNG, new Object[] { config }); + } catch (Throwable t) + { + t.printStackTrace(); + return; + } } } Modified: maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java Sat May 5 10:21:35 2007 @@ -50,7 +50,9 @@ private boolean testStarted = false; private ITestContext _finishContext; - + + private boolean testSetCompleted = false; + /** * Constructs a new instance that will listen to * test updates from a [EMAIL PROTECTED] TestNG} class instance. @@ -155,6 +157,8 @@ { // TODO: remove this exception from the report manager } + + testSetCompleted = false; } public void onFinish( ITestContext context ) @@ -194,7 +198,13 @@ } catch (Throwable t) { t.printStackTrace(); } } - + + // Don't execute twice + if (testSetCompleted) + { + return; + } + String rawString = bundle.getString( "testSetCompletedNormally" ); ReportEntry report = @@ -203,6 +213,8 @@ reportManager.testSetCompleted( report ); reportManager.reset(); + + testSetCompleted = true; } /** Modified: maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java URL: http://svn.apache.org/viewvc/maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java?view=diff&rev=535549&r1=535548&r2=535549 ============================================================================== --- maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java (original) +++ maven/sandbox/branches/surefire/surefire-collaboration/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java Sat May 5 10:21:35 2007 @@ -47,6 +47,19 @@ private Map testSets; + private Properties config; + + /** + * Constructur used with TestNG >= 5.6. + * + * @param config + * The generic map of testng configuration properties gathered from plugin. + */ + public TestNGXmlTestSuite( Properties config ) + { + this.config = config; + } + /** * Creates a testng testset to be configured by the specified * xml file. @@ -65,6 +78,16 @@ public void execute( ReporterManager reporterManager, ClassLoader classLoader ) { + if ( config != null ) + { + config.put("classloader", classLoader); + + TestNGExecutor.executeTestNG( this, config, reporterManager ); + + config.remove("classloader"); + return; + } + if ( testSets == null ) { throw new IllegalStateException( "You must call locateTestSets before calling execute" ); @@ -82,6 +105,12 @@ public void execute( String testSetName, ReporterManager reporterManager, ClassLoader classLoader ) throws TestSetFailedException { + if (config != null) + { + executeSingle( testSetName, reporterManager, classLoader); + return; + } + if ( testSets == null ) { throw new IllegalStateException( "You must call locateTestSets before calling execute" ); @@ -113,22 +142,18 @@ suite.getTests().clear(); suite.getTests().addAll( originalTests ); } - /* - List originalTests = new ArrayList( suite.getTests() ); - for ( Iterator i = suite.getTests().iterator(); i.hasNext(); ) - { - XmlTest test = (XmlTest) i.next(); - if ( !test.getName().equals( testSetName ) ) - { - i.remove(); - } - } - - TestNGExecutor.executeTestNG( this, testSourceDirectory, suite, reporterManager ); + } - suite.getTests().clear(); - suite.getTests().addAll( originalTests ); - */ + void executeSingle( String testSetName, ReporterManager reporterManager, ClassLoader classLoader ) + throws TestSetFailedException + { + config.put("testSetName", testSetName); + config.put("classloader", classLoader); + + TestNGExecutor.executeTestNG( this, config, reporterManager ); + + config.remove("testSetName"); + config.remove("classloader"); } public int getNumTests() @@ -139,6 +164,11 @@ public int getNumTestSets() { + if (config != null) + { + return 1; + } + int count = 0; Iterator it = suites.iterator(); @@ -155,6 +185,11 @@ public Map locateTestSets( ClassLoader classLoader ) throws TestSetFailedException { + if (config != null) + { + return locateTestSets(classLoader, config); + } + if ( testSets != null ) { throw new IllegalStateException( "You can't call locateTestSets twice" ); @@ -217,20 +252,12 @@ testSets.put( xmlTest.getName(), xmlTest ); } } - /* - for ( Iterator i = suite.getTests().iterator(); i.hasNext(); ) - { - XmlTest xmlTest = (XmlTest) i.next(); - - if ( testSets.containsKey( xmlTest.getName() ) ) - { - throw new TestSetFailedException( "Duplicate test set '" + xmlTest.getName() + "'" ); - } - - // We don't need to put real test sets in here, the key is the important part - testSets.put( xmlTest.getName(), xmlTest ); - }*/ return testSets; + } + + Map locateTestSets( ClassLoader classLoader, Properties props) + { + return config; } }