Updated Branches:
  refs/heads/master 93ef689e6 -> 25724d17c

[SUREFIRE-1019] correctly identify JUnit4 tests for the TestNG provider


Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/25724d17
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/25724d17
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/25724d17

Branch: refs/heads/master
Commit: 25724d17cddf8c27af0fe052b8d2a01c4f884838
Parents: 93ef689
Author: Andreas Gudian <agud...@apache.org>
Authored: Thu Jul 25 22:21:31 2013 +0200
Committer: Andreas Gudian <agud...@apache.org>
Committed: Thu Jul 25 22:21:31 2013 +0200

----------------------------------------------------------------------
 .../testng/TestNGDirectoryTestSuite.java        | 64 ++++++++++++++++++--
 1 file changed, 59 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/25724d17/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
----------------------------------------------------------------------
diff --git 
a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
 
b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
index b0d4631..62286eb 100644
--- 
a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
+++ 
b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
@@ -20,15 +20,17 @@ package org.apache.maven.surefire.testng;
  */
 
 import java.io.File;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.SortedMap;
 import java.util.TreeMap;
+
 import org.apache.maven.surefire.NonAbstractClassFilter;
 import org.apache.maven.surefire.report.ConsoleOutputCapture;
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
@@ -70,6 +72,10 @@ public class TestNGDirectoryTestSuite
 
     private final Class junitTestClass;
 
+    private Class<? extends Annotation> junitRunWithAnnotation;
+
+    private Class<? extends Annotation> junitTestAnnotation;
+
     public TestNGDirectoryTestSuite( String testSourceDirectory, Properties 
confOptions, File reportsDirectory, String testMethodPattern,
                                      RunOrderCalculator runOrderCalculator, 
ScanResult scanResult )
     {
@@ -83,6 +89,8 @@ public class TestNGDirectoryTestSuite
         this.scanResult = scanResult;
         this.testMethodPattern = testMethodPattern;
         this.junitTestClass = findJUnitTestClass();
+        this.junitRunWithAnnotation = findJUnitRunWithAnnotation();
+        this.junitTestAnnotation = findJUnitTestAnnotation();
         this.junitOptions = createJUnitOptions();
     }
 
@@ -135,16 +143,31 @@ public class TestNGDirectoryTestSuite
 
     private Class findJUnitTestClass()
     {
-        Class junitTest;
+        return lookupClass( "junit.framework.Test" );
+    }
+
+    private Class findJUnitRunWithAnnotation()
+    {
+        return lookupClass( "org.junit.runner.RunWith" );
+    }
+
+    private Class findJUnitTestAnnotation()
+    {
+        return lookupClass( "org.junit.Test" );
+    }
+
+    private Class lookupClass( String className )
+    {
+        Class junitClass;
         try
         {
-            junitTest = Class.forName( "junit.framework.Test" );
+            junitClass = Class.forName( className );
         }
         catch ( ClassNotFoundException e )
         {
-            junitTest = null;
+            junitClass = null;
         }
-        return junitTest;
+        return junitClass;
     }
 
     public void executeMulti( TestsToRun testsToRun, ReporterFactory 
reporterFactory )
@@ -194,6 +217,37 @@ public class TestNGDirectoryTestSuite
 
     private boolean isJUnitTest( Class c )
     {
+        return isJunit3Test( c ) || isJunit4Test( c );
+    }
+
+    private boolean isJunit4Test( Class c )
+    {
+        return hasJunit4RunWithAnnotation( c ) || hasJunit4TestAnnotation( c );
+    }
+
+    private boolean hasJunit4RunWithAnnotation( Class c )
+    {
+        return junitRunWithAnnotation != null && c.getAnnotation( 
junitRunWithAnnotation ) != null;
+    }
+
+    private boolean hasJunit4TestAnnotation( Class c )
+    {
+        if ( junitTestAnnotation != null )
+        {
+            for ( Method m : c.getMethods() )
+            {
+                if ( m.getAnnotation( junitTestAnnotation ) != null )
+                {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+    private boolean isJunit3Test( Class c )
+    {
         return junitTestClass != null && junitTestClass.isAssignableFrom( c );
     }
 

Reply via email to