Repository: maven-surefire Updated Branches: refs/heads/master 9cefb7ba4 -> af4414138
[SUREFIRE] refactoring Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/af441413 Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/af441413 Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/af441413 Branch: refs/heads/master Commit: af441413896ef14036ecf4d6c0f44f314c457db5 Parents: 9cefb7b Author: Tibor17 <tibo...@lycos.com> Authored: Sun Dec 13 01:30:50 2015 +0100 Committer: Tibor17 <tibo...@lycos.com> Committed: Sun Dec 13 01:30:50 2015 +0100 ---------------------------------------------------------------------- .../testng/TestNGDirectoryTestSuite.java | 88 ++++++-------------- .../maven/surefire/testng/TestNGExecutor.java | 12 +-- .../maven/surefire/testng/TestNGProvider.java | 10 ++- .../maven/surefire/testng/TestNGReporter.java | 45 ++++------ .../maven/surefire/testng/TestNGTestSet.java | 57 ------------- .../surefire/testng/TestNGXmlTestSuite.java | 14 ++-- .../apache/maven/surefire/testng/TestSuite.java | 61 ++++++++++++++ 7 files changed, 119 insertions(+), 168 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/af441413/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 312e6c6..4e1a7f3 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 @@ -23,19 +23,19 @@ 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.List; import java.util.Map; import org.apache.maven.surefire.cli.CommandLineOption; -import org.apache.maven.surefire.report.ReportEntry; -import org.apache.maven.surefire.report.ReporterException; import org.apache.maven.surefire.report.RunListener; -import org.apache.maven.surefire.report.SimpleReportEntry; import org.apache.maven.surefire.testset.TestListResolver; import org.apache.maven.surefire.testset.TestSetFailedException; import org.apache.maven.surefire.util.TestsToRun; +import static org.apache.maven.surefire.testng.TestNGExecutor.run; + /** * Test suite for TestNG based on a directory of Java test classes. Can also execute JUnit tests. * @@ -43,8 +43,8 @@ import org.apache.maven.surefire.util.TestsToRun; * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a> */ final class TestNGDirectoryTestSuite + extends TestSuite { - private final Map<String, String> options; private final Map<String, String> junitOptions; @@ -104,22 +104,22 @@ final class TestNGDirectoryTestSuite { options.put( "suitename", testClass.getName() ); - startTestSuite( reporter, this ); + startTestSuite( reporter ); - final Map<String, String> optionsToUse = isJUnitTest( testClass ) ? junitOptions : options; + Map<String, String> optionsToUse = isJUnitTest( testClass ) ? junitOptions : options; - TestNGExecutor.run( new Class<?>[]{ testClass }, testSourceDirectory, optionsToUse, reporter, - reportsDirectory, methodFilter, mainCliOptions, skipAfterFailureCount ); + run( Collections.<Class<?>>singleton( testClass ), testSourceDirectory, optionsToUse, reporter, + reportsDirectory, methodFilter, mainCliOptions, skipAfterFailureCount ); - finishTestSuite( reporter, this ); + finishTestSuite( reporter ); } private void executeLazy( TestsToRun testsToRun, RunListener reporterManager ) throws TestSetFailedException { - for ( Class<?> c : testsToRun ) + for ( Class<?> testToRun : testsToRun ) { - executeSingleClass( reporterManager, c ); + executeSingleClass( reporterManager, testToRun ); } } @@ -168,15 +168,15 @@ final class TestNGDirectoryTestSuite { List<Class<?>> testNgTestClasses = new ArrayList<Class<?>>(); List<Class<?>> junitTestClasses = new ArrayList<Class<?>>(); - for ( Class<?> c : testsToRun ) + for ( Class<?> testToRun : testsToRun ) { - if ( isJUnitTest( c ) ) + if ( isJUnitTest( testToRun ) ) { - junitTestClasses.add( c ); + junitTestClasses.add( testToRun ); } else { - testNgTestClasses.add( c ); + testNgTestClasses.add( testToRun ); } } @@ -187,22 +187,18 @@ final class TestNGDirectoryTestSuite testNgReportsDirectory = new File( reportsDirectory, "testng-native-results" ); junitReportsDirectory = new File( reportsDirectory, "testng-junit-results" ); } - startTestSuite( reporterManager, this ); + startTestSuite( reporterManager ); - Class<?>[] testClasses = testNgTestClasses.toArray( new Class<?>[testNgTestClasses.size()] ); - - TestNGExecutor.run( testClasses, testSourceDirectory, options, reporterManager, - testNgReportsDirectory, methodFilter, mainCliOptions, skipAfterFailureCount ); + run( testNgTestClasses, testSourceDirectory, options, reporterManager, + testNgReportsDirectory, methodFilter, mainCliOptions, skipAfterFailureCount ); if ( !junitTestClasses.isEmpty() ) { - testClasses = junitTestClasses.toArray( new Class[junitTestClasses.size()] ); - - TestNGExecutor.run( testClasses, testSourceDirectory, junitOptions, reporterManager, - junitReportsDirectory, methodFilter, mainCliOptions, skipAfterFailureCount ); + run( junitTestClasses, testSourceDirectory, junitOptions, reporterManager, + junitReportsDirectory, methodFilter, mainCliOptions, skipAfterFailureCount ); } - finishTestSuite( reporterManager, this ); + finishTestSuite( reporterManager ); } private boolean isJUnitTest( Class<?> c ) @@ -248,45 +244,9 @@ final class TestNGDirectoryTestSuite return junitOptions; } - public static void startTestSuite( RunListener reporterManager, Object suite ) - { - ReportEntry report = new SimpleReportEntry( suite.getClass().getName(), getSuiteName( suite ) ); - - try - { - reporterManager.testSetStarting( report ); - } - catch ( ReporterException e ) - { - // TODO: remove this exception from the report manager - } - } - - public static void finishTestSuite( RunListener reporterManager, Object suite ) + @Override + Map<String, String> getOptions() { - ReportEntry report = new SimpleReportEntry( suite.getClass().getName(), getSuiteName( suite ) ); - - reporterManager.testSetCompleted( report ); - } - - String getSuiteName() - { - String result = options.get( "suitename" ); - return result == null ? "TestSuite" : result; - } - - private static String getSuiteName( Object suite ) - { - String result = "TestSuite"; - if ( suite instanceof TestNGDirectoryTestSuite ) - { - result = ( (TestNGDirectoryTestSuite) suite ).getSuiteName(); - } - else if ( suite instanceof TestNGXmlTestSuite ) - { - result = ( (TestNGXmlTestSuite) suite ).getSuiteName(); - } - - return result; + return options; } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/af441413/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 7d7700a..529ed89 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 @@ -28,7 +28,6 @@ import org.apache.maven.surefire.testng.utils.FailFastListener; import org.apache.maven.surefire.testng.utils.Stoppable; import org.apache.maven.surefire.testset.TestListResolver; 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; @@ -49,6 +48,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import static org.apache.maven.surefire.util.ReflectionUtils.instantiate; +import static org.apache.maven.surefire.util.ReflectionUtils.tryLoadClass; import static org.apache.maven.surefire.util.internal.ConcurrencyUtils.countDownToZero; /** @@ -60,20 +60,20 @@ import static org.apache.maven.surefire.util.internal.ConcurrencyUtils.countDown final class TestNGExecutor { /** The default name for a suite launched from the maven surefire plugin */ - public static final String DEFAULT_SUREFIRE_SUITE_NAME = "Surefire suite"; + private static final String DEFAULT_SUREFIRE_SUITE_NAME = "Surefire suite"; /** 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 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" ); + tryLoadClass( TestNGExecutor.class.getClassLoader(), "org.testng.annotations.Test" ) != null; private TestNGExecutor() { throw new IllegalStateException( "not instantiable constructor" ); } - static void run( Class<?>[] testClasses, String testSourceDirectory, + static void run( Iterable<Class<?>> testClasses, String testSourceDirectory, Map<String, String> options, // string,string because TestNGMapConfigurator#configure() RunListener reportManager, File reportsDirectory, TestListResolver methodFilter, List<CommandLineOption> mainCliOptions, @@ -267,7 +267,7 @@ final class TestNGExecutor return xms; } - public static void run( List<String> suiteFiles, String testSourceDirectory, + static void run( List<String> suiteFiles, String testSourceDirectory, Map<String, String> options, // string,string because TestNGMapConfigurator#configure() RunListener reportManager, File reportsDirectory, int skipAfterFailureCount ) throws TestSetFailedException http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/af441413/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java ---------------------------------------------------------------------- diff --git a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java index 91117d2..cd2a99a 100644 --- a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java +++ b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGProvider.java @@ -25,7 +25,6 @@ import org.apache.maven.surefire.booter.CommandReader; import org.apache.maven.surefire.cli.CommandLineOption; import org.apache.maven.surefire.providerapi.AbstractProvider; import org.apache.maven.surefire.providerapi.ProviderParameters; -import org.apache.maven.surefire.report.ConsoleOutputCapture; import org.apache.maven.surefire.report.ConsoleOutputReceiver; import org.apache.maven.surefire.report.ReporterConfiguration; import org.apache.maven.surefire.report.ReporterFactory; @@ -46,6 +45,7 @@ import java.util.List; import java.util.Map; import static org.apache.maven.surefire.booter.CommandReader.getReader; +import static org.apache.maven.surefire.report.ConsoleOutputCapture.startCapture; import static org.apache.maven.surefire.testset.TestListResolver.getEmptyTestListResolver; import static org.apache.maven.surefire.testset.TestListResolver.optionallyWildcardFilter; import static org.apache.maven.surefire.util.TestsToRun.fromClass; @@ -106,9 +106,13 @@ public class TestNGProvider final ReporterFactory reporterFactory = providerParameters.getReporterFactory(); final RunListener reporter = reporterFactory.createReporter(); - ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter ); - RunResult runResult; + /** + * {@link org.apache.maven.surefire.report.ConsoleOutputCapture#startCapture(ConsoleOutputReceiver)} + * called in prior to initializing variable {@link #testsToRun} + */ + startCapture( (ConsoleOutputReceiver) reporter ); + RunResult runResult; try { if ( isTestNGXmlTestSuite( testRequest ) ) http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/af441413/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java ---------------------------------------------------------------------- diff --git a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java index e8ff18c..bb4f411 100644 --- a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java +++ b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGReporter.java @@ -19,7 +19,6 @@ package org.apache.maven.surefire.testng; * under the License. */ -import java.util.ResourceBundle; import org.apache.maven.surefire.report.CategorizedReportEntry; import org.apache.maven.surefire.report.PojoStackTraceWriter; import org.apache.maven.surefire.report.ReportEntry; @@ -32,6 +31,8 @@ import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; +import static org.apache.maven.surefire.report.SimpleReportEntry.withException; + /** * Listens for and provides and adaptor layer so that * TestNG tests can report their status to the current @@ -43,20 +44,13 @@ import org.testng.ITestResult; public class TestNGReporter implements ITestListener, ISuiteListener { - public static final String SUREFIRE_BUNDLE_NAME = "org.apache.maven.surefire.surefire"; - - private final ResourceBundle bundle = ResourceBundle.getBundle( SUREFIRE_BUNDLE_NAME ); - - /** - * core Surefire reporting - */ private final RunListener reporter; /** * Constructs a new instance that will listen to - * test updates from a {@link TestNG} class instance. + * test updates from a {@link org.testng.TestNG} class instance. * <p/> - * <p/>It is assumed that the requisite {@link TestNG#addListener(ITestListener)} + * <p/>It is assumed that the requisite {@link org.testng.TestNG#addListener(ITestListener)} * method call has already associated with this instance <i>before</i> the test * suite is run. * @@ -65,21 +59,13 @@ public class TestNGReporter public TestNGReporter( RunListener reportManager ) { this.reporter = reportManager; - - if ( reportManager == null ) - { - throw new IllegalArgumentException( "ReportManager passed in was null." ); - } - } public void onTestStart( ITestResult result ) { - String rawString = bundle.getString( "testStarting" ); String group = groupString( result.getMethod().getGroups(), result.getTestClass().getName() ); - ReportEntry report = - new CategorizedReportEntry( getSource( result ), getUserFriendlyTestName( result ), group ); - reporter.testStarting( report ); + String userFriendlyTestName = getUserFriendlyTestName( result ); + reporter.testStarting( new CategorizedReportEntry( getSource( result ), userFriendlyTestName, group ) ); } private String getSource( ITestResult result ) @@ -95,11 +81,10 @@ public class TestNGReporter public void onTestFailure( ITestResult result ) { - ReportEntry report = SimpleReportEntry.withException( getSource( result ), getUserFriendlyTestName( result ), - new PojoStackTraceWriter( - result.getTestClass().getRealClass().getName(), - result.getMethod().getMethodName(), - result.getThrowable() ) ); + ReportEntry report = withException( getSource( result ), getUserFriendlyTestName( result ), + new PojoStackTraceWriter( result.getTestClass().getRealClass().getName(), + result.getMethod().getMethodName(), + result.getThrowable() ) ); reporter.testFailed( report ); } @@ -113,17 +98,15 @@ public class TestNGReporter public void onTestSkipped( ITestResult result ) { ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) ); - reporter.testSkipped( report ); } public void onTestFailedButWithinSuccessPercentage( ITestResult result ) { - ReportEntry report = SimpleReportEntry.withException( getSource( result ), getUserFriendlyTestName( result ), - new PojoStackTraceWriter( - result.getTestClass().getRealClass().getName(), - result.getMethod().getMethodName(), - result.getThrowable() ) ); + ReportEntry report = withException( getSource( result ), getUserFriendlyTestName( result ), + new PojoStackTraceWriter( result.getTestClass().getRealClass().getName(), + result.getMethod().getMethodName(), + result.getThrowable() ) ); reporter.testSucceeded( report ); } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/af441413/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java ---------------------------------------------------------------------- diff --git a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java deleted file mode 100644 index 558b02f..0000000 --- a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGTestSet.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.apache.maven.surefire.testng; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/** - * Main plugin point for running testng tests within the Surefire runtime - * infrastructure. - * - * @author jkuhnert - */ -public class TestNGTestSet -{ - private Class testClass; - - /** - * Creates a new test testset that will process the class being - * passed in to determine the testing configuration. - * - * @param testClass The test class - */ - public TestNGTestSet( Class testClass ) - { - if ( testClass == null ) - { - throw new NullPointerException( "testClass is null" ); - } - - this.testClass = testClass; - } - - public String getName() - { - return testClass.getName(); - } - - public Class getTestClass() - { - return testClass; - } -} http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/af441413/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java ---------------------------------------------------------------------- diff --git a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java index 4617110..03bc55b 100644 --- a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java +++ b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGXmlTestSuite.java @@ -23,11 +23,10 @@ import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; + import org.apache.maven.surefire.report.RunListener; import org.apache.maven.surefire.testset.TestSetFailedException; -import static org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.finishTestSuite; -import static org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.startTestSuite; import static org.apache.maven.surefire.testng.TestNGExecutor.run; /** @@ -37,6 +36,7 @@ import static org.apache.maven.surefire.testng.TestNGExecutor.run; * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a> */ final class TestNGXmlTestSuite + extends TestSuite { private final List<File> suiteFiles; @@ -71,9 +71,9 @@ final class TestNGXmlTestSuite { throw new IllegalStateException( "You must call locateTestSets before calling execute" ); } - startTestSuite( reporter, this ); + startTestSuite( reporter ); run( suiteFilePaths, testSourceDirectory, options, reporter, reportsDirectory, skipAfterFailureCount ); - finishTestSuite( reporter, this ); + finishTestSuite( reporter ); } void locateTestSets() @@ -101,9 +101,9 @@ final class TestNGXmlTestSuite } } - String getSuiteName() + @Override + Map<String, String> getOptions() { - String result = options.get( "suitename" ); - return result == null ? "TestSuite" : result; + return options; } } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/af441413/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestSuite.java ---------------------------------------------------------------------- diff --git a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestSuite.java b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestSuite.java new file mode 100644 index 0000000..a74ef0d --- /dev/null +++ b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestSuite.java @@ -0,0 +1,61 @@ +package org.apache.maven.surefire.testng; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.surefire.report.ReportEntry; +import org.apache.maven.surefire.report.ReporterException; +import org.apache.maven.surefire.report.RunListener; +import org.apache.maven.surefire.report.SimpleReportEntry; + +import java.util.Map; + +/** + * Abstract class which implements common functions. + */ +abstract class TestSuite +{ + abstract Map<String, String> getOptions(); + + final String getSuiteName() + { + String result = getOptions().get( "suitename" ); + return result == null ? "TestSuite" : result; + } + + final void startTestSuite( RunListener reporterManager ) + { + ReportEntry report = new SimpleReportEntry( getClass().getName(), getSuiteName() ); + + try + { + reporterManager.testSetStarting( report ); + } + catch ( ReporterException e ) + { + // TODO: remove this exception from the report manager + } + } + + final void finishTestSuite( RunListener reporterManager ) + { + ReportEntry report = new SimpleReportEntry( getClass().getName(), getSuiteName() ); + reporterManager.testSetCompleted( report ); + } +}