maven-surefire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krosenv...@apache.org
Subject svn commit: r1045380 - in /maven/surefire/trunk: surefire-api/src/main/java/org/apache/maven/surefire/util/ surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/
Date Mon, 13 Dec 2010 21:23:53 GMT
Author: krosenvold
Date: Mon Dec 13 21:23:53 2010
New Revision: 1045380

URL: http://svn.apache.org/viewvc?rev=1045380&view=rev
Log:
[SUREFIRE-662] Fixed for TestNG

Modified:
    maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/DefaultDirectoryScanner.java
    maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
    maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java

Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/DefaultDirectoryScanner.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/DefaultDirectoryScanner.java?rev=1045380&r1=1045379&r2=1045380&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/DefaultDirectoryScanner.java
(original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/DefaultDirectoryScanner.java
Mon Dec 13 21:23:53 2010
@@ -68,7 +68,7 @@ public class DefaultDirectoryScanner
 
             Class testClass = loadClass( classLoader, className );
 
-            if ( scannerFilter.accept( testClass ) )
+            if ( scannerFilter == null || scannerFilter.accept( testClass ) )
             {
                 result.add( testClass );
             }

Modified: maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java?rev=1045380&r1=1045379&r2=1045380&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
(original)
+++ maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
Mon Dec 13 21:23:53 2010
@@ -19,24 +19,26 @@ package org.apache.maven.surefire.testng
  * under the License.
  */
 
-import java.io.File;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.apache.maven.surefire.report.DefaultReportEntry;
 import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.ReporterException;
+import org.apache.maven.surefire.report.ReporterFactory;
 import org.apache.maven.surefire.report.ReporterManager;
 import org.apache.maven.surefire.report.ReporterManagerFactory;
 import org.apache.maven.surefire.suite.AbstractDirectoryTestSuite;
 import org.apache.maven.surefire.testset.SurefireTestSet;
 import org.apache.maven.surefire.testset.TestSetFailedException;
+import org.apache.maven.surefire.util.TestsToRun;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
 
 /**
  * Test suite for TestNG based on a directory of Java test classes. Can also execute JUnit
tests.
@@ -86,6 +88,94 @@ public class TestNGDirectoryTestSuite
         return new TestNGTestSet( testClass );
     }
 
+    public void execute( TestsToRun testsToRun, ReporterFactory reporterManagerFactory )
+        throws ReporterException, TestSetFailedException
+    {
+        ReporterManager reporterManager = (ReporterManager) reporterManagerFactory.createReporter();
+        startTestSuite( reporterManager, this );
+
+        if ( testsToRun.size() == 0 )
+        {
+            return;
+        }
+
+        if ( testsToRun.size() > 1 )
+        {
+            executeMulti( testsToRun, reporterManagerFactory );
+            return;
+        }
+        TestNGExecutor.run( new Class[]{ (Class) testsToRun.iterator().next() }, this.testSourceDirectory,
this.options,
+                            this.version, this.classifier, reporterManager, this, reportsDirectory
);
+
+        finishTestSuite( reporterManager, this );
+    }
+
+    public void executeMulti( TestsToRun testsToRun, ReporterFactory reporterManagerFactory
)
+        throws ReporterException, TestSetFailedException
+    {
+        Class junitTest;
+        try
+        {
+            junitTest = Class.forName( "junit.framework.Test" );
+        }
+        catch ( ClassNotFoundException e )
+        {
+            junitTest = null;
+        }
+
+        List testNgTestClasses = new ArrayList();
+        List junitTestClasses = new ArrayList();
+        for ( Iterator it = testsToRun.iterator(); it.hasNext(); )
+        {
+            Class testSet = (Class) it.next();
+            Class c = testSet;
+            if ( junitTest != null && junitTest.isAssignableFrom( c ) )
+            {
+                junitTestClasses.add( c );
+            }
+            else
+            {
+                testNgTestClasses.add( c );
+            }
+        }
+
+        File testNgReportsDirectory = reportsDirectory, junitReportsDirectory = reportsDirectory;
+
+        if ( junitTestClasses.size() > 0 && testNgTestClasses.size() > 0 )
+        {
+            testNgReportsDirectory = new File( reportsDirectory, "testng-native-results"
);
+            junitReportsDirectory = new File( reportsDirectory, "testng-junit-results" );
+        }
+
+        ReporterManager reporterManager =
+            new SynchronizedReporterManager( (ReporterManager) reporterManagerFactory.createReporter()
);
+        startTestSuite( reporterManager, this );
+
+        Class[] testClasses = (Class[]) testNgTestClasses.toArray( new Class[0] );
+
+        TestNGExecutor.run( testClasses, this.testSourceDirectory, this.options, this.version,
this.classifier,
+                            reporterManager, this, testNgReportsDirectory );
+
+        if ( junitTestClasses.size() > 0 )
+        {
+            testClasses = (Class[]) junitTestClasses.toArray( new Class[0] );
+
+            Map junitOptions = new HashMap();
+            for ( Iterator it = this.options.keySet().iterator(); it.hasNext(); )
+            {
+                Object key = it.next();
+                junitOptions.put( key, options.get( key ) );
+            }
+
+            junitOptions.put( "junit", Boolean.TRUE );
+
+            TestNGExecutor.run( testClasses, this.testSourceDirectory, junitOptions, this.version,
this.classifier,
+                                reporterManager, this, junitReportsDirectory );
+        }
+
+        finishTestSuite( reporterManager, this );
+    }
+
     // single class test
     public void execute( String testSetName, ReporterManagerFactory reporterManagerFactory,
ClassLoader classLoader )
         throws ReporterException, TestSetFailedException
@@ -110,6 +200,7 @@ public class TestNGDirectoryTestSuite
         finishTestSuite( reporterManager, this );
     }
 
+    // todo: Delete as soon as we build surefire itself with 2.7
     public void execute( ReporterManagerFactory reporterManagerFactory, ClassLoader classLoader
)
         throws ReporterException, TestSetFailedException
     {

Modified: maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java
URL: http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java?rev=1045380&r1=1045379&r2=1045380&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java
(original)
+++ maven/surefire/trunk/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java
Mon Dec 13 21:23:53 2010
@@ -26,11 +26,12 @@ import org.apache.maven.surefire.report.
 import org.apache.maven.surefire.report.ReporterFactory;
 import org.apache.maven.surefire.report.ReporterManagerFactory;
 import org.apache.maven.surefire.suite.RunResult;
-import org.apache.maven.surefire.suite.SurefireTestSuite;
 import org.apache.maven.surefire.testset.DirectoryScannerParameters;
 import org.apache.maven.surefire.testset.TestArtifactInfo;
 import org.apache.maven.surefire.testset.TestRequest;
 import org.apache.maven.surefire.testset.TestSetFailedException;
+import org.apache.maven.surefire.util.DirectoryScanner;
+import org.apache.maven.surefire.util.TestsToRun;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -56,8 +57,12 @@ public class TestNGProvider
 
     private final DirectoryScannerParameters directoryScannerParameters;
 
+    private final DirectoryScanner directoryScanner;
+
     private final TestRequest testRequest;
 
+    private TestsToRun testsToRun;
+
     private final File basedir;
 
     public TestNGProvider( ProviderParameters booterParameters )
@@ -70,6 +75,7 @@ public class TestNGProvider
         basedir = directoryScannerParameters != null ? directoryScannerParameters.getTestClassesDirectory()
: null;
         testArtifactInfo = booterParameters.getTestArtifactInfo();
         reporterConfiguration = booterParameters.getReporterConfiguration();
+        this.directoryScanner = booterParameters.getDirectoryScanner();
     }
 
     public Boolean isRunnable()
@@ -80,15 +86,29 @@ public class TestNGProvider
     public RunResult invoke( Object forkTestSet )
         throws TestSetFailedException, ReporterException
     {
-        SurefireTestSuite suite = getActiveSuite();
-        suite.locateTestSets( testClassLoader );
-        if ( forkTestSet != null && testRequest == null )
+
+        if ( isTestNGXmlTestSuite( testRequest ) )
         {
-            suite.execute( (String) forkTestSet, (ReporterManagerFactory) reporterFactory,
testClassLoader );
+            TestNGXmlTestSuite testNGXmlTestSuite = getXmlSuite();
+            testNGXmlTestSuite.locateTestSets( testClassLoader );
+            if ( forkTestSet != null && testRequest == null )
+            {
+                testNGXmlTestSuite.execute( (String) forkTestSet, (ReporterManagerFactory)
reporterFactory,
+                                            testClassLoader );
+            }
+            else
+            {
+                testNGXmlTestSuite.execute( (ReporterManagerFactory) reporterFactory, testClassLoader
);
+            }
         }
         else
         {
-            suite.execute( (ReporterManagerFactory) reporterFactory, testClassLoader );
+            if ( testsToRun == null )
+            {
+                testsToRun = forkTestSet == null ? scanClassPath() : TestsToRun.fromClass(
(Class) forkTestSet );
+            }
+            TestNGDirectoryTestSuite suite = getDirectorySuite();
+            suite.execute( testsToRun, reporterFactory );
         }
 
         return reporterFactory.close();
@@ -119,20 +139,30 @@ public class TestNGProvider
     }
 
 
-    public SurefireTestSuite getActiveSuite()
-    {
-        return isTestNGXmlTestSuite( testRequest ) ? (SurefireTestSuite) getXmlSuite() :
getDirectorySuite();
-    }
-
     public Iterator getSuites()
     {
-        try
+        if ( isTestNGXmlTestSuite( testRequest ) )
         {
-            return getActiveSuite().locateTestSets( testClassLoader ).keySet().iterator();
+            try
+            {
+                return getXmlSuite().locateTestSets( testClassLoader ).keySet().iterator();
+            }
+            catch ( TestSetFailedException e )
+            {
+                throw new RuntimeException( e );
+            }
         }
-        catch ( TestSetFailedException e )
+        else
         {
-            throw new RuntimeException( e );
+            testsToRun = scanClassPath();
+            return testsToRun.iterator();
         }
     }
+
+    private TestsToRun scanClassPath()
+    {
+        return directoryScanner.locateTestClasses( testClassLoader, null );
+    }
+
+
 }



Mime
View raw message