SUREFIRE-1044 o Make runOrder work even when suite names and test names are specified (order of suites determined by the 'first' test in suite) o also fix ClassNotFound exception when using testNG with JDK 1.4 style javadoc annotations
Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/741c094e Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/741c094e Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/741c094e Branch: refs/heads/master Commit: 741c094e6ac1f9dec67ab4025ee519aec7dda49d Parents: be98de5 Author: Andreas Gudian <agud...@apache.org> Authored: Wed Jan 1 22:26:55 2014 +0100 Committer: Andreas Gudian <agud...@apache.org> Committed: Wed Jan 1 22:26:55 2014 +0100 ---------------------------------------------------------------------- .../surefire/its/CheckTestNgVersionsIT.java | 16 ++- .../maven/surefire/testng/TestNGExecutor.java | 116 +++++++++++++------ 2 files changed, 92 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/741c094e/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/CheckTestNgVersionsIT.java ---------------------------------------------------------------------- diff --git a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/CheckTestNgVersionsIT.java b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/CheckTestNgVersionsIT.java index ab3cc6e..fb3a51e 100644 --- a/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/CheckTestNgVersionsIT.java +++ b/surefire-integration-tests/src/test/java/org/apache/maven/surefire/its/CheckTestNgVersionsIT.java @@ -137,7 +137,7 @@ public class CheckTestNgVersionsIT @Test public void test514() throws Exception { - runTestNgTest( "5.14", false ); // runOrder is not working + runTestNgTest( "5.14" ); } @Test public void test5141() @@ -155,22 +155,28 @@ public class CheckTestNgVersionsIT @Test public void test60() throws Exception { - runTestNgTest( "6.0" ); + runTestNgTest( "6.0", false ); } @Test public void test685() throws Exception { - runTestNgTest( "6.8.5" ); + runTestNgTestWithRunOrder( "6.8.5" ); } - public void runTestNgTest( String version ) + private void runTestNgTestWithRunOrder( String version ) throws Exception { runTestNgTest( version, true ); } - public void runTestNgTest( String version, boolean validateRunOrder ) + private void runTestNgTest( String version ) + throws Exception + { + runTestNgTest( version, false ); + } + + private void runTestNgTest( String version, boolean validateRunOrder ) throws Exception { http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/741c094e/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java ---------------------------------------------------------------------- diff --git a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java index 35a339c..5655739 100644 --- a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java +++ b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGExecutor.java @@ -23,6 +23,7 @@ import org.apache.maven.surefire.booter.ProviderParameterNames; import org.apache.maven.surefire.report.RunListener; import org.apache.maven.surefire.testng.conf.Configurator; import org.apache.maven.surefire.testset.TestSetFailedException; +import org.apache.maven.surefire.util.ReflectionUtils; import org.apache.maven.surefire.util.internal.StringUtils; import org.testng.TestNG; import org.testng.annotations.Test; @@ -32,6 +33,7 @@ import org.testng.xml.XmlSuite; import org.testng.xml.XmlTest; import java.io.File; +import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -54,6 +56,9 @@ public class TestNGExecutor /** The default name for a test launched from the maven surefire plugin */ public static final String DEFAULT_SUREFIRE_TEST_NAME = "Surefire test"; + private static final boolean HAS_TEST_ANNOTATION_ON_CLASSPATH = + null != ReflectionUtils.tryLoadClass( TestNGExecutor.class.getClassLoader(), "org.testng.annotations.Test" ); + private TestNGExecutor() { // noop @@ -71,56 +76,97 @@ public class TestNGExecutor XmlMethodSelector groupMatchingSelector = getGroupMatchingSelector( options ); XmlMethodSelector methodNameFilteringSelector = getMethodNameFilteringSelector( methodNamePattern ); - Map<String, Map<String, List<XmlClass>>> suitesNames = new HashMap<String, Map<String, List<XmlClass>>>(); + Map<String, SuiteAndNamedTests> suitesNames = new HashMap<String, SuiteAndNamedTests>(); - for (Class testClass : testClasses) { - Test testAnnotation = (Test)testClass.getAnnotation(Test.class); - String suiteName = DEFAULT_SUREFIRE_SUITE_NAME; - String testName = DEFAULT_SUREFIRE_TEST_NAME; - if (testAnnotation != null) { + List<XmlSuite> xmlSuites = new ArrayList<XmlSuite>(); + for ( Class testClass : testClasses ) + { + TestMetadata metadata = findTestMetadata( testClass ); - if (testAnnotation.suiteName() != null) { - suiteName = testAnnotation.suiteName(); - } + SuiteAndNamedTests suiteAndNamedTests = suitesNames.get( metadata.suiteName ); + if ( suiteAndNamedTests == null ) + { + suiteAndNamedTests = new SuiteAndNamedTests(); + suiteAndNamedTests.xmlSuite.setName( metadata.suiteName ); + configurator.configure( suiteAndNamedTests.xmlSuite, options ); + xmlSuites.add( suiteAndNamedTests.xmlSuite ); - if (testAnnotation.testName() != null) { - testName = testAnnotation.testName(); - } + suitesNames.put( metadata.suiteName, suiteAndNamedTests ); } - Map<String, List<XmlClass>> testNames = suitesNames.get(suiteName); - if (testNames == null) { - testNames = new HashMap<String, List<XmlClass>>(); - suitesNames.put(suiteName, testNames); - } - List<XmlClass> xmlTestClasses = testNames.get(testName); - if (xmlTestClasses == null) { - xmlTestClasses = new ArrayList<XmlClass>(); - testNames.put(testName, xmlTestClasses); - } - xmlTestClasses.add(new XmlClass(testClass.getName())); - } - List<XmlSuite> xmlSuites = new ArrayList<XmlSuite>(suitesNames.size()); - for (Map.Entry<String, Map<String, List<XmlClass>>> suit : suitesNames.entrySet()) { - XmlSuite xmlSuite = new XmlSuite(); - xmlSuite.setName(suit.getKey()); - configurator.configure( xmlSuite, options ); - for (Map.Entry<String, List<XmlClass>> test : suit.getValue().entrySet()) { - XmlTest xmlTest = new XmlTest(xmlSuite); - xmlTest.setName(test.getKey()); + XmlTest xmlTest = suiteAndNamedTests.testNameToTest.get( metadata.testName ); + if ( xmlTest == null ) + { + xmlTest = new XmlTest( suiteAndNamedTests.xmlSuite ); + xmlTest.setName( metadata.testName ); addSelector( xmlTest, groupMatchingSelector ); addSelector( xmlTest, methodNameFilteringSelector ); - xmlTest.setXmlClasses(test.getValue()); + xmlTest.setXmlClasses( new ArrayList<XmlClass>() ); + + suiteAndNamedTests.testNameToTest.put( metadata.testName, xmlTest ); } - xmlSuites.add(xmlSuite); + xmlTest.getXmlClasses().add( new XmlClass( testClass.getName() ) ); } - testng.setXmlSuites(xmlSuites); + + testng.setXmlSuites( xmlSuites ); configurator.configure( testng, options ); postConfigure( testng, testSourceDirectory, reportManager, suite, reportsDirectory ); testng.run(); } + private static TestMetadata findTestMetadata( Class testClass ) + { + TestMetadata result = new TestMetadata(); + if ( HAS_TEST_ANNOTATION_ON_CLASSPATH ) + { + Test testAnnotation = findAnnotation( testClass, Test.class ); + if ( null != testAnnotation ) + { + if ( !StringUtils.isBlank( testAnnotation.suiteName() ) ) + { + result.suiteName = testAnnotation.suiteName(); + } + + if ( !StringUtils.isBlank( testAnnotation.testName() ) ) + { + result.testName = testAnnotation.testName(); + } + } + } + return result; + } + + private static <T extends Annotation> T findAnnotation( Class<?> clazz, Class<T> annotationType ) + { + if ( clazz == null ) + { + return null; + } + + T result = clazz.getAnnotation( annotationType ); + if ( result != null ) + { + return result; + } + + return findAnnotation( clazz.getSuperclass(), annotationType ); + } + + private static class TestMetadata + { + private String testName = DEFAULT_SUREFIRE_TEST_NAME; + + private String suiteName = DEFAULT_SUREFIRE_SUITE_NAME; + } + + private static class SuiteAndNamedTests + { + private XmlSuite xmlSuite = new XmlSuite(); + + private Map<String, XmlTest> testNameToTest = new HashMap<String, XmlTest>(); + } + private static void addSelector( XmlTest xmlTest, XmlMethodSelector selector ) { if ( selector != null )