From 8823687a89e4f0f70a956043f2d78b817db675eb Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Sun, 4 Jul 2021 21:13:48 +0200 Subject: [PATCH 1/8] Extract cli logic from TestNG --- .../main/java/org/testng/TestNGAntTask.java | 3 +- .../main/java/org/testng/CliTestNgRunner.java | 84 ++++ .../main/java/org/testng/CommandLineArgs.java | 450 +++++++++++------- .../org/testng/JCommanderCliTestNgRunner.java | 36 ++ .../org/testng/JCommanderCommandLineArgs.java | 421 ++++++++++++++++ .../org/testng/SurefireCommandLineArgs.java | 238 +++++++++ .../src/main/java/org/testng/TestNG.java | 420 ++-------------- .../src/test/java/test/CommandLineTest.java | 21 +- .../commandline/CommandLineOverridesXml.java | 10 +- .../FailurePolicyTest.java | 14 +- .../test/methodselectors/CommandLineTest.java | 33 +- .../MethodSelectorInSuiteTest.java | 5 +- .../src/test/java/test/mixed/MixedTest.java | 12 +- .../test/reports/EmailableReporterTest.java | 3 +- 14 files changed, 1178 insertions(+), 572 deletions(-) create mode 100644 testng-core/src/main/java/org/testng/CliTestNgRunner.java create mode 100644 testng-core/src/main/java/org/testng/JCommanderCliTestNgRunner.java create mode 100644 testng-core/src/main/java/org/testng/JCommanderCommandLineArgs.java create mode 100644 testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java diff --git a/testng-ant/src/main/java/org/testng/TestNGAntTask.java b/testng-ant/src/main/java/org/testng/TestNGAntTask.java index 412b796ef9..1af5d2077a 100644 --- a/testng-ant/src/main/java/org/testng/TestNGAntTask.java +++ b/testng-ant/src/main/java/org/testng/TestNGAntTask.java @@ -499,7 +499,8 @@ public void execute() throws BuildException { List argv = createArguments(); if (!forkJvm) { - TestNG tng = TestNG.privateMain(argv.toArray(new String[0]), null); + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + TestNG tng = CliTestNgRunner.Main.privateMain(cliRunner, argv.toArray(new String[0]), null); actOnResult(tng.getStatus(), false); return; } diff --git a/testng-core/src/main/java/org/testng/CliTestNgRunner.java b/testng-core/src/main/java/org/testng/CliTestNgRunner.java new file mode 100644 index 0000000000..ba0dbeaa18 --- /dev/null +++ b/testng-core/src/main/java/org/testng/CliTestNgRunner.java @@ -0,0 +1,84 @@ +package org.testng; + +import org.testng.internal.ExitCode; +import org.testng.log4testng.Logger; + +public interface CliTestNgRunner { + + CommandLineArgs parse(String[] argv) throws CommandLineArgs.ParameterException; + + void usage(); + + void clear(); + + default void exitWithError(String msg) { + System.err.println(msg); + usage(); + System.exit(1); + } + + class Main { + + private static final Logger LOGGER = Logger.getLogger(TestNG.class); + + /** + * The TestNG entry point for command line execution. + * + * @param argv the TestNG command line parameters. + */ + public static void main(String[] argv) { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + TestNG testng = privateMain(cliRunner, argv, null); + System.exit(testng.getStatus()); + } + + /** + * Note: this method is not part of the public API and is meant for internal usage only. + * + * @param argv The param arguments + * @param listener The listener + * @return The TestNG instance + */ + public static TestNG privateMain( + CliTestNgRunner cliRunner, String[] argv, ITestListener listener) { + TestNG result = new TestNG(); + + if (null != listener) { + result.addListener(listener); + } + + // Parse the arguments + try { + CommandLineArgs cla = cliRunner.parse(argv); + validateCommandLineParameters(cla); + cla.configure(result); + } catch (CommandLineArgs.ParameterException ex) { + cliRunner.exitWithError(ex.getMessage()); + } + + // Run + try { + result.run(); + } catch (TestNGException ex) { + if (TestRunner.getVerbose() > 1) { + ex.printStackTrace(System.out); + } else { + LOGGER.error(ex.getMessage()); + } + result.setExitCode(ExitCode.newExitCodeRepresentingFailure()); + } + + return result; + } + + /** + * Double check that the command line parameters are valid. + * + * @param args The command line to check + */ + protected static void validateCommandLineParameters(CommandLineArgs args) + throws CommandLineArgs.ParameterException { + args.validate(); + } + } +} diff --git a/testng-core/src/main/java/org/testng/CommandLineArgs.java b/testng-core/src/main/java/org/testng/CommandLineArgs.java index d3a752a226..bb5c96747d 100644 --- a/testng-core/src/main/java/org/testng/CommandLineArgs.java +++ b/testng-core/src/main/java/org/testng/CommandLineArgs.java @@ -1,245 +1,359 @@ package org.testng; -import com.beust.jcommander.Parameter; -import java.util.ArrayList; import java.util.List; -import org.testng.collections.Lists; +import org.testng.internal.ReporterConfig; +import org.testng.xml.XmlMethodSelector; import org.testng.xml.XmlSuite; -public class CommandLineArgs { +/** + * The command line parameters are: + * + * + */ +public interface CommandLineArgs { - @Parameter(description = "The XML suite files to run") - public List suiteFiles = Lists.newArrayList(); + String LOG = "-log"; - public static final String LOG = "-log"; - public static final String VERBOSE = "-verbose"; + /** @return The XML suite files to run */ + List getSuiteFiles(); - @Parameter( - names = {LOG, VERBOSE}, - description = "Level of verbosity") - public Integer verbose; + String VERBOSE = "-verbose"; - public static final String GROUPS = "-groups"; + /** @return Level of verbosity */ + Integer getVerbose(); - @Parameter(names = GROUPS, description = "Comma-separated list of group names to be run") - public String groups; + String GROUPS = "-groups"; - public static final String EXCLUDED_GROUPS = "-excludegroups"; + /** @return Comma-separated list of group names to be run */ + String getGroups(); - @Parameter( - names = EXCLUDED_GROUPS, - description = "Comma-separated list of group names to " + " exclude") - public String excludedGroups; + String EXCLUDED_GROUPS = "-excludegroups"; - public static final String OUTPUT_DIRECTORY = "-d"; + /** @return Comma-separated list of group names to exclude */ + String getExcludedGroups(); - @Parameter(names = OUTPUT_DIRECTORY, description = "Output directory") - public String outputDirectory; + String OUTPUT_DIRECTORY = "-d"; - public static final String JUNIT = "-junit"; + /** @return Output directory */ + String getOutputDirectory(); - @Parameter(names = JUNIT, description = "JUnit mode") - public Boolean junit = Boolean.FALSE; + String JUNIT = "-junit"; - public static final String MIXED = "-mixed"; + /** @return JUnit mode */ + Boolean isJUnit(); - @Parameter( - names = MIXED, - description = - "Mixed mode - autodetect the type of current test" - + " and run it with appropriate runner") - public Boolean mixed = Boolean.FALSE; + String MIXED = "-mixed"; - public static final String LISTENER = "-listener"; + /** @return Mixed mode - autodetect the type of current test and run it with appropriate runner */ + Boolean isMixed(); - @Parameter( - names = LISTENER, - description = - "List of .class files or list of class names" - + " implementing ITestListener or ISuiteListener") - public String listener; + String LISTENER = "-listener"; - public static final String METHOD_SELECTORS = "-methodselectors"; + /** + * @return List of .class files or list of class names implementing ITestListener or + * ISuiteListener + */ + List> getListener(); - @Parameter( - names = METHOD_SELECTORS, - description = "List of .class files or list of class " + "names implementing IMethodSelector") - public String methodSelectors; + String METHOD_SELECTORS = "-methodselectors"; - public static final String OBJECT_FACTORY = "-objectfactory"; + /** @return List of .class files or list of class names implementing IMethodSelector */ + List getMethodSelectors(); - @Parameter( - names = OBJECT_FACTORY, - description = - "List of .class files or list of class " + "names implementing ITestRunnerFactory") - public String objectFactory; + String OBJECT_FACTORY = "-objectfactory"; - public static final String PARALLEL = "-parallel"; + /** @return List of .class files or list of class names implementing ITestRunnerFactory */ + Class getObjectFactory(); - @Parameter(names = PARALLEL, description = "Parallel mode (methods, tests or classes)") - public XmlSuite.ParallelMode parallelMode; + String PARALLEL = "-parallel"; - public static final String CONFIG_FAILURE_POLICY = "-configfailurepolicy"; + /** @return Parallel mode (methods, tests or classes) */ + XmlSuite.ParallelMode getParallelMode(); - @Parameter( - names = CONFIG_FAILURE_POLICY, - description = "Configuration failure policy (skip or continue)") - public String configFailurePolicy; + String CONFIG_FAILURE_POLICY = "-configfailurepolicy"; - public static final String THREAD_COUNT = "-threadcount"; + /** @return Configuration failure policy (skip or continue) */ + XmlSuite.FailurePolicy getConfigFailurePolicy(); - @Parameter( - names = THREAD_COUNT, - description = "Number of threads to use when running tests " + "in parallel") - public Integer threadCount; + String THREAD_COUNT = "-threadcount"; - public static final String DATA_PROVIDER_THREAD_COUNT = "-dataproviderthreadcount"; + /** @return Number of threads to use when running tests in parallel */ + Integer getThreadCount(); - @Parameter( - names = DATA_PROVIDER_THREAD_COUNT, - description = "Number of threads to use when " + "running data providers") - public Integer dataProviderThreadCount; + String DATA_PROVIDER_THREAD_COUNT = "-dataproviderthreadcount"; - public static final String SUITE_NAME = "-suitename"; + /** @return Number of threads to use when running data providers */ + Integer getDataProviderThreadCount(); - @Parameter( - names = SUITE_NAME, - description = - "Default name of test suite, if not specified " - + "in suite definition file or source code") - public String suiteName; + String SUITE_NAME = "-suitename"; - public static final String TEST_NAME = "-testname"; + /** + * @return Default name of test suite, if not specified in suite definition file or source code + */ + String getSuiteName(); - @Parameter( - names = TEST_NAME, - description = - "Default name of test, if not specified in suite" + "definition file or source code") - public String testName; + String TEST_NAME = "-testname"; - public static final String REPORTER = "-reporter"; + /** @return Default name of test, if not specified in suite definition file or source code */ + String getTestName(); - @Parameter(names = REPORTER, description = "Extended configuration for custom report listener") - public String reporter; + String REPORTER = "-reporter"; - public static final String USE_DEFAULT_LISTENERS = "-usedefaultlisteners"; + /** @return Extended configuration for custom report listener */ + ReporterConfig getReporter(); - @Parameter(names = USE_DEFAULT_LISTENERS, description = "Whether to use the default listeners") - public String useDefaultListeners = "true"; + String USE_DEFAULT_LISTENERS = "-usedefaultlisteners"; - public static final String SKIP_FAILED_INVOCATION_COUNTS = "-skipfailedinvocationcounts"; + /** @return Whether to use the default listeners */ + Boolean useDefaultListeners(); - @Parameter(names = SKIP_FAILED_INVOCATION_COUNTS, hidden = true) - public Boolean skipFailedInvocationCounts; + String SKIP_FAILED_INVOCATION_COUNTS = "-skipfailedinvocationcounts"; - public static final String TEST_CLASS = "-testclass"; + Boolean skipFailedInvocationCounts(); - @Parameter(names = TEST_CLASS, description = "The list of test classes") - public String testClass; + String TEST_CLASS = "-testclass"; - public static final String TEST_NAMES = "-testnames"; + /** @return The list of test classes */ + List> getTestClass(); - @Parameter(names = TEST_NAMES, description = "The list of test names to run") - public String testNames; + String TEST_NAMES = "-testnames"; - public static final String TEST_JAR = "-testjar"; + /** @return The list of test names to run */ + List getTestNames(); - @Parameter(names = TEST_JAR, description = "A jar file containing the tests") - public String testJar; + String TEST_JAR = "-testjar"; - public static final String XML_PATH_IN_JAR = "-xmlpathinjar"; - public static final String XML_PATH_IN_JAR_DEFAULT = "testng.xml"; + /** @return A jar file containing the tests */ + String getTestJar(); - @Parameter( - names = XML_PATH_IN_JAR, - description = - "The full path to the xml file inside the jar file (only valid if -testjar was specified)") - public String xmlPathInJar = XML_PATH_IN_JAR_DEFAULT; + String XML_PATH_IN_JAR = "-xmlpathinjar"; - public static final String TEST_RUNNER_FACTORY = "-testrunfactory"; + /** + * @return The full path to the xml file inside the jar file (only valid if -testjar was + * specified) + */ + String getXmlPathInJar(); - @Parameter( - names = {TEST_RUNNER_FACTORY, "-testRunFactory"}, - description = "The factory used to create tests") - public String testRunnerFactory; + String TEST_RUNNER_FACTORY = "-testrunfactory"; - public static final String PORT = "-port"; + /** @return The factory used to create tests */ + Class getTestRunnerFactory(); - @Parameter(names = PORT, description = "The port") - public Integer port; + String PORT = "-port"; - public static final String HOST = "-host"; + /** @return The port */ + Integer getPort(); - @Parameter(names = HOST, description = "The host", hidden = true) - public String host; + String HOST = "-host"; - public static final String METHODS = "-methods"; + /** @return The host */ + String getHost(); - @Parameter(names = METHODS, description = "Comma separated of test methods") - public List commandLineMethods = new ArrayList<>(); + String METHODS = "-methods"; - public static final String SUITE_THREAD_POOL_SIZE = "-suitethreadpoolsize"; - public static final Integer SUITE_THREAD_POOL_SIZE_DEFAULT = 1; + /** @return Comma separated of test methods */ + List getCommandLineMethods(); - @Parameter( - names = SUITE_THREAD_POOL_SIZE, - description = "Size of the thread pool to use" + " to run suites") - public Integer suiteThreadPoolSize = SUITE_THREAD_POOL_SIZE_DEFAULT; + String SUITE_THREAD_POOL_SIZE = "-suitethreadpoolsize"; + Integer SUITE_THREAD_POOL_SIZE_DEFAULT = 1; - public static final String RANDOMIZE_SUITES = "-randomizesuites"; + /** @return Size of the thread pool to use to run suites */ + Integer getSuiteThreadPoolSize(); - @Parameter( - names = RANDOMIZE_SUITES, - hidden = true, - description = "Whether to run suites in same order as specified in XML or not") - public Boolean randomizeSuites = Boolean.FALSE; + String RANDOMIZE_SUITES = "-randomizesuites"; - public static final String DEBUG = "-debug"; + /** @return Whether to run suites in same order as specified in XML or not */ + Boolean isRandomizeSuites(); - @Parameter(names = DEBUG, hidden = true, description = "Used to debug TestNG") - public Boolean debug = Boolean.FALSE; + String DEBUG = "-debug"; - public static final String ALWAYS_RUN_LISTENERS = "-alwaysrunlisteners"; + /** @return Used to debug TestNG */ + Boolean isDebug(); - @Parameter( - names = ALWAYS_RUN_LISTENERS, - description = "Should MethodInvocation Listeners be run even for skipped methods") - public Boolean alwaysRunListeners = Boolean.TRUE; + String ALWAYS_RUN_LISTENERS = "-alwaysrunlisteners"; - public static final String THREAD_POOL_FACTORY_CLASS = "-threadpoolfactoryclass"; + /** @return Should MethodInvocation Listeners be run even for skipped methods */ + Boolean alwaysRunListeners(); - @Parameter( - names = THREAD_POOL_FACTORY_CLASS, - description = "The threadpool executor factory implementation that TestNG should use.") - public String threadPoolFactoryClass; + String THREAD_POOL_FACTORY_CLASS = "-threadpoolfactoryclass"; - public static final String DEPENDENCY_INJECTOR_FACTORY = "-dependencyinjectorfactory"; + /** @return The threadpool executor factory implementation that TestNG should use. */ + String getThreadPoolFactoryClass(); - @Parameter( - names = DEPENDENCY_INJECTOR_FACTORY, - description = "The dependency injector factory implementation that TestNG should use.") - public String dependencyInjectorFactoryClass; + String DEPENDENCY_INJECTOR_FACTORY = "-dependencyinjectorfactory"; - public static final String FAIL_IF_ALL_TESTS_SKIPPED = "-failwheneverythingskipped"; + /** @return The dependency injector factory implementation that TestNG should use. */ + Class getDependencyInjectorFactory(); - @Parameter( - names = FAIL_IF_ALL_TESTS_SKIPPED, - description = "Should TestNG fail execution if all tests were skipped and nothing was run.") - public Boolean failIfAllTestsSkipped = false; + String FAIL_IF_ALL_TESTS_SKIPPED = "-failwheneverythingskipped"; - public static final String LISTENERS_TO_SKIP_VIA_SPI = "-spilistenerstoskip"; + /** @return Should TestNG fail execution if all tests were skipped and nothing was run. */ + Boolean failIfAllTestsSkipped(); - @Parameter( - names = LISTENERS_TO_SKIP_VIA_SPI, - description = - "Comma separated fully qualified class names of listeners that should be skipped from being wired in via Service Loaders.") - public String spiListenersToSkip = ""; + String LISTENERS_TO_SKIP_VIA_SPI = "-spilistenerstoskip"; - public static final String OVERRIDE_INCLUDED_METHODS = "-overrideincludedmethods"; + /** + * @return Comma separated fully qualified class names of listeners that should be skipped from + * being wired in via Service Loaders. + */ + List getSpiListenersToSkip(); - @Parameter( - names = OVERRIDE_INCLUDED_METHODS, - description = - "Comma separated fully qualified class names of listeners that should be skipped from being wired in via Service Loaders.") - public Boolean overrideIncludedMethods = false; + String OVERRIDE_INCLUDED_METHODS = "-overrideincludedmethods"; + + /** + * @return Comma separated fully qualified class names of listeners that should be skipped from + * being wired in via Service Loaders. + */ + Boolean overrideIncludedMethods(); + + default void validate() throws ParameterException { + List> testClasses = getTestClass(); + List testNgXml = getSuiteFiles(); + String testJar = getTestJar(); + List methods = getCommandLineMethods(); + + if (testClasses == null + && testJar == null + && (testNgXml == null || testNgXml.isEmpty()) + && (methods == null || methods.isEmpty())) { + throw new ParameterException( + "You need to specify at least one testng.xml, one class" + " or one method"); + } + + String groups = getGroups(); + String excludedGroups = getExcludedGroups(); + + if (testJar == null + && (null != groups || null != excludedGroups) + && testClasses == null + && (testNgXml == null || testNgXml.isEmpty())) { + throw new ParameterException("Groups option should be used with testclass option"); + } + + Boolean junit = isJUnit(); + Boolean mixed = isMixed(); + if (junit && mixed) { + throw new ParameterException( + CommandLineArgs.MIXED + " can't be combined with " + CommandLineArgs.JUNIT); + } + } + + /** + * Configure the TestNG instance based on the command line parameters. + * + * @param tng The TestNG instance + */ + default void configure(TestNG tng) { + if (getVerbose() != null) { + tng.setVerbose(getVerbose()); + } + if (getDependencyInjectorFactory() != null) { + tng.setInjectorFactory(getDependencyInjectorFactory()); + } + if (getThreadPoolFactoryClass() != null) { + tng.setExecutorFactoryClass(getThreadPoolFactoryClass()); + } + tng.setOutputDirectory(getOutputDirectory()); + + List> testClasses = getTestClass(); + if (testClasses != null) { + tng.setTestClasses(testClasses.toArray(new Class[0])); + } + + tng.setOutputDirectory(getOutputDirectory()); + + if (getTestNames() != null) { + tng.setTestNames(getTestNames()); + } + + if (useDefaultListeners() != null) { + tng.setUseDefaultListeners(useDefaultListeners()); + } + + tng.setGroups(getGroups()); + tng.setExcludedGroups(getExcludedGroups()); + tng.setTestJar(getTestJar()); + tng.setXmlPathInJar(getXmlPathInJar()); + tng.setJUnit(isJUnit()); + tng.setMixed(isMixed()); + tng.setSkipFailedInvocationCounts(skipFailedInvocationCounts()); + tng.toggleFailureIfAllTestsWereSkipped(failIfAllTestsSkipped()); + tng.setListenersToSkipFromBeingWiredInViaServiceLoaders(getSpiListenersToSkip()); + + if (overrideIncludedMethods() != null) { + tng.setOverrideIncludedMethods(overrideIncludedMethods()); + } + + if (getParallelMode() != null) { + tng.setParallel(getParallelMode()); + } + if (getConfigFailurePolicy() != null) { + tng.setConfigFailurePolicy(getConfigFailurePolicy()); + } + if (getThreadCount() != null) { + tng.setThreadCount(getThreadCount()); + } + if (getDataProviderThreadCount() != null) { + tng.setDataProviderThreadCount(getDataProviderThreadCount()); + } + if (getSuiteName() != null) { + tng.setDefaultSuiteName(getSuiteName()); + } + if (getTestName() != null) { + tng.setDefaultTestName(getTestName()); + } + if (getListener() != null) { + tng.setListenerClasses(getListener()); + } + + if (getMethodSelectors() != null) { + for (XmlMethodSelector selector : getMethodSelectors()) { + tng.addMethodSelector(selector); + } + } + + if (getObjectFactory() != null) { + tng.setObjectFactory(getObjectFactory()); + } + if (getTestRunnerFactory() != null) { + tng.setTestRunnerFactoryClass(getTestRunnerFactory()); + } + + if (getReporter() != null) { + tng.addReporter(getReporter()); + } + + if (!getCommandLineMethods().isEmpty()) { + tng.setCommandLineMethods(getCommandLineMethods()); + } + + if (getSuiteFiles() != null) { + tng.setTestSuites(getSuiteFiles()); + } + + tng.setSuiteThreadPoolSize(getSuiteThreadPoolSize()); + tng.setRandomizeSuites(isRandomizeSuites()); + tng.alwaysRunListeners(alwaysRunListeners()); + } + + class ParameterException extends Exception { + + ParameterException(String message) { + super(message); + } + + ParameterException(String message, Throwable cause) { + super(message, cause); + } + } } diff --git a/testng-core/src/main/java/org/testng/JCommanderCliTestNgRunner.java b/testng-core/src/main/java/org/testng/JCommanderCliTestNgRunner.java new file mode 100644 index 0000000000..934516815b --- /dev/null +++ b/testng-core/src/main/java/org/testng/JCommanderCliTestNgRunner.java @@ -0,0 +1,36 @@ +package org.testng; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.ParameterException; + +public class JCommanderCliTestNgRunner implements CliTestNgRunner { + + private JCommander jCommander; + + @Override + public CommandLineArgs parse(String[] argv) throws CommandLineArgs.ParameterException { + CommandLineArgs cla = new JCommanderCommandLineArgs(); + + try { + jCommander = new JCommander(cla); + jCommander.parse(argv); + } catch (ParameterException ex) { + throw new CommandLineArgs.ParameterException(ex.getMessage(), ex); + } + + return cla; + } + + @Override + public void usage() { + if (jCommander == null) { + jCommander = new JCommander(new JCommanderCommandLineArgs()); + } + jCommander.usage(); + } + + @Override + public void clear() { + jCommander = null; + } +} diff --git a/testng-core/src/main/java/org/testng/JCommanderCommandLineArgs.java b/testng-core/src/main/java/org/testng/JCommanderCommandLineArgs.java new file mode 100644 index 0000000000..6a0b972086 --- /dev/null +++ b/testng-core/src/main/java/org/testng/JCommanderCommandLineArgs.java @@ -0,0 +1,421 @@ +package org.testng; + +import com.beust.jcommander.Parameter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.testng.collections.Lists; +import org.testng.internal.ClassHelper; +import org.testng.internal.ReporterConfig; +import org.testng.internal.Utils; +import org.testng.log4testng.Logger; +import org.testng.xml.XmlMethodSelector; +import org.testng.xml.XmlSuite; + +public class JCommanderCommandLineArgs implements CommandLineArgs { + + private static final Logger LOGGER = Logger.getLogger(TestNG.class); + + @Parameter(description = "The XML suite files to run") + private List suiteFiles = Lists.newArrayList(); + + @Parameter( + names = {LOG, VERBOSE}, + description = "Level of verbosity") + private Integer verbose; + + @Parameter(names = GROUPS, description = "Comma-separated list of group names to be run") + private String groups; + + @Parameter( + names = EXCLUDED_GROUPS, + description = "Comma-separated list of group names to exclude") + private String excludedGroups; + + @Parameter(names = OUTPUT_DIRECTORY, description = "Output directory") + private String outputDirectory; + + @Parameter(names = JUNIT, description = "JUnit mode") + public Boolean junit = Boolean.FALSE; + + @Parameter( + names = MIXED, + description = + "Mixed mode - autodetect the type of current test" + + " and run it with appropriate runner") + private Boolean mixed = Boolean.FALSE; + + @Parameter( + names = LISTENER, + description = + "List of .class files or list of class names" + + " implementing ITestListener or ISuiteListener") + private String listener; + + @Parameter( + names = METHOD_SELECTORS, + description = "List of .class files or list of class names implementing IMethodSelector") + private String methodSelectors; + + @Parameter( + names = OBJECT_FACTORY, + description = "List of .class files or list of class names implementing ITestRunnerFactory") + private String objectFactory; + + @Parameter(names = PARALLEL, description = "Parallel mode (methods, tests or classes)") + private XmlSuite.ParallelMode parallelMode; + + @Parameter( + names = CONFIG_FAILURE_POLICY, + description = "Configuration failure policy (skip or continue)") + private String configFailurePolicy; + + @Parameter( + names = THREAD_COUNT, + description = "Number of threads to use when running tests in parallel") + private Integer threadCount; + + @Parameter( + names = DATA_PROVIDER_THREAD_COUNT, + description = "Number of threads to use when running data providers") + private Integer dataProviderThreadCount; + + @Parameter( + names = SUITE_NAME, + description = + "Default name of test suite, if not specified in suite definition file or source code") + private String suiteName; + + @Parameter( + names = TEST_NAME, + description = + "Default name of test, if not specified in suite definition file or source code") + private String testName; + + @Parameter(names = REPORTER, description = "Extended configuration for custom report listener") + private String reporter; + + // Note: can't use a Boolean field here because we are allowing a boolean + // parameter with an arity of 1 ("-usedefaultlisteners false") + @Parameter(names = USE_DEFAULT_LISTENERS, description = "Whether to use the default listeners") + private String useDefaultListeners = "true"; + + @Parameter(names = SKIP_FAILED_INVOCATION_COUNTS, hidden = true) + private Boolean skipFailedInvocationCounts; + + @Parameter(names = TEST_CLASS, description = "The list of test classes") + private String testClass; + + @Parameter(names = TEST_NAMES, description = "The list of test names to run") + private String testNames; + + @Parameter(names = TEST_JAR, description = "A jar file containing the tests") + private String testJar; + + @Parameter( + names = XML_PATH_IN_JAR, + description = + "The full path to the xml file inside the jar file (only valid if -testjar was specified)") + private String xmlPathInJar = TestNG.XML_PATH_IN_JAR_DEFAULT; + + @Parameter( + names = {TEST_RUNNER_FACTORY, "-testRunFactory"}, + description = "The factory used to create tests") + private String testRunnerFactory; + + @Parameter(names = PORT, description = "The port") + private Integer port; + + @Parameter(names = HOST, description = "The host", hidden = true) + private String host; + + @Parameter(names = METHODS, description = "Comma separated of test methods") + private List commandLineMethods = new ArrayList<>(); + + @Parameter( + names = SUITE_THREAD_POOL_SIZE, + description = "Size of the thread pool to use" + " to run suites") + private Integer suiteThreadPoolSize = SUITE_THREAD_POOL_SIZE_DEFAULT; + + @Parameter( + names = RANDOMIZE_SUITES, + hidden = true, + description = "Whether to run suites in same order as specified in XML or not") + private Boolean randomizeSuites = Boolean.FALSE; + + @Parameter(names = DEBUG, hidden = true, description = "Used to debug TestNG") + private Boolean debug = Boolean.FALSE; + + @Parameter( + names = ALWAYS_RUN_LISTENERS, + description = "Should MethodInvocation Listeners be run even for skipped methods") + private Boolean alwaysRunListeners = Boolean.TRUE; + + @Parameter( + names = THREAD_POOL_FACTORY_CLASS, + description = "The threadpool executor factory implementation that TestNG should use.") + private String threadPoolFactoryClass; + + @Parameter( + names = DEPENDENCY_INJECTOR_FACTORY, + description = "The dependency injector factory implementation that TestNG should use.") + private String dependencyInjectorFactoryClass; + + @Parameter( + names = FAIL_IF_ALL_TESTS_SKIPPED, + description = "Should TestNG fail execution if all tests were skipped and nothing was run.") + private Boolean failIfAllTestsSkipped = false; + + @Parameter( + names = LISTENERS_TO_SKIP_VIA_SPI, + description = + "Comma separated fully qualified class names of listeners that should be skipped from being wired in via Service Loaders.") + private String spiListenersToSkip = ""; + + @Parameter( + names = OVERRIDE_INCLUDED_METHODS, + description = + "Comma separated fully qualified class names of listeners that should be skipped from being wired in via Service Loaders.") + private Boolean overrideIncludedMethods = false; + + @Override + public List getSuiteFiles() { + return suiteFiles; + } + + @Override + public Integer getVerbose() { + return verbose; + } + + @Override + public String getGroups() { + return groups; + } + + @Override + public String getExcludedGroups() { + return excludedGroups; + } + + @Override + public String getOutputDirectory() { + return outputDirectory; + } + + @Override + public Boolean isJUnit() { + return junit; + } + + @Override + public Boolean isMixed() { + return mixed; + } + + @Override + public List> getListener() { + if (listener == null) { + return null; + } + String sep = ";"; + if (listener.contains(",")) { + sep = ","; + } + String[] strs = Utils.split(listener, sep); + List> classes = Lists.newArrayList(); + + for (String cls : strs) { + Class clazz = ClassHelper.fileToClass(cls); + if (ITestNGListener.class.isAssignableFrom(clazz)) { + classes.add((Class) clazz); + } + } + + return classes; + } + + @Override + public List getMethodSelectors() { + if (methodSelectors == null) { + return null; + } + String[] strs = Utils.split(methodSelectors, ","); + List selectors = new ArrayList<>(strs.length); + for (String cls : strs) { + String[] sel = Utils.split(cls, ":"); + try { + if (sel.length == 2) { + XmlMethodSelector selector = new XmlMethodSelector(); + selector.setName(sel[0]); + selector.setPriority(Integer.parseInt(sel[1])); + selectors.add(selector); + } else { + LOGGER.error("Method selector value was not in the format org.example.Selector:4"); + } + } catch (NumberFormatException nfe) { + LOGGER.error("Method selector value was not in the format org.example.Selector:4"); + } + } + return selectors; + } + + @Override + public Class getObjectFactory() { + if (objectFactory == null) { + return null; + } + return (Class) ClassHelper.fileToClass(objectFactory); + } + + @Override + public XmlSuite.ParallelMode getParallelMode() { + return parallelMode; + } + + @Override + public XmlSuite.FailurePolicy getConfigFailurePolicy() { + return XmlSuite.FailurePolicy.getValidPolicy(configFailurePolicy); + } + + @Override + public Integer getThreadCount() { + return threadCount; + } + + @Override + public Integer getDataProviderThreadCount() { + return dataProviderThreadCount; + } + + @Override + public String getSuiteName() { + return suiteName; + } + + @Override + public String getTestName() { + return testName; + } + + @Override + public ReporterConfig getReporter() { + return ReporterConfig.deserialize(reporter); + } + + @Override + public Boolean useDefaultListeners() { + if (useDefaultListeners == null) { + return null; + } + return "true".equalsIgnoreCase(useDefaultListeners); + } + + @Override + public Boolean skipFailedInvocationCounts() { + return skipFailedInvocationCounts; + } + + @Override + public List> getTestClass() { + if (testClass == null) { + return null; + } + String[] strClasses = testClass.split(","); + List> classes = Lists.newArrayList(); + for (String c : strClasses) { + classes.add(ClassHelper.fileToClass(c)); + } + + return classes; + } + + @Override + public List getTestNames() { + if (testNames == null) { + return null; + } + return Arrays.asList(testNames.split(",")); + } + + @Override + public String getTestJar() { + return testJar; + } + + @Override + public String getXmlPathInJar() { + return xmlPathInJar; + } + + @Override + public Class getTestRunnerFactory() { + if (testRunnerFactory == null) { + return null; + } + return (Class) ClassHelper.fileToClass(testRunnerFactory); + } + + @Override + public Integer getPort() { + return port; + } + + @Override + public String getHost() { + return host; + } + + @Override + public List getCommandLineMethods() { + return commandLineMethods; + } + + @Override + public Integer getSuiteThreadPoolSize() { + return suiteThreadPoolSize; + } + + @Override + public Boolean isRandomizeSuites() { + return randomizeSuites; + } + + @Override + public Boolean isDebug() { + return debug; + } + + @Override + public Boolean alwaysRunListeners() { + return alwaysRunListeners; + } + + @Override + public String getThreadPoolFactoryClass() { + return threadPoolFactoryClass; + } + + @Override + public Class getDependencyInjectorFactory() { + if (dependencyInjectorFactoryClass == null) { + return null; + } + return (Class) ClassHelper.forName(dependencyInjectorFactoryClass); + } + + @Override + public Boolean failIfAllTestsSkipped() { + return failIfAllTestsSkipped; + } + + @Override + public List getSpiListenersToSkip() { + return Arrays.asList(spiListenersToSkip.split(",")); + } + + @Override + public Boolean overrideIncludedMethods() { + return overrideIncludedMethods; + } +} diff --git a/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java b/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java new file mode 100644 index 0000000000..fa401385d3 --- /dev/null +++ b/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java @@ -0,0 +1,238 @@ +package org.testng; + +import java.util.List; +import java.util.Map; +import org.testng.internal.Utils; +import org.testng.xml.XmlSuite; + +public class SurefireCommandLineArgs implements CommandLineArgs { + + private final Map cmdLineArgs; + + public SurefireCommandLineArgs(Map cmdLineArgs) { + this.cmdLineArgs = cmdLineArgs; + } + + @Override + public List getSuiteFiles() { + return null; + } + + @Override + public Integer getVerbose() { + int value = parseInt(cmdLineArgs.get(CommandLineArgs.LOG)); + if (value != -1) { + return value; + } + + return null; + } + + @Override + public String getGroups() { + return (String) cmdLineArgs.get(CommandLineArgs.GROUPS); + } + + @Override + public String getExcludedGroups() { + return (String) cmdLineArgs.get(CommandLineArgs.EXCLUDED_GROUPS); + } + + @Override + public String getOutputDirectory() { + return (String) cmdLineArgs.get(CommandLineArgs.OUTPUT_DIRECTORY); + } + + @Override + public Boolean isJUnit() { + return (Boolean) cmdLineArgs.get(CommandLineArgs.JUNIT); + } + + @Override + public Boolean isMixed() { + return (Boolean) cmdLineArgs.get(CommandLineArgs.MIXED); + } + + @Override + public String getListener() { + Object listeners = cmdLineArgs.get(CommandLineArgs.LISTENER); + if (listeners instanceof List) { + return Utils.join((List) listeners, ","); + } else { + return (String) listeners; + } + } + + @Override + public String getMethodSelectors() { + return (String) cmdLineArgs.get(CommandLineArgs.METHOD_SELECTORS); + } + + @Override + public String getObjectFactory() { + return (String) cmdLineArgs.get(CommandLineArgs.OBJECT_FACTORY); + } + + @Override + public XmlSuite.ParallelMode getParallelMode() { + String parallelMode = (String) cmdLineArgs.get(CommandLineArgs.PARALLEL); + return XmlSuite.ParallelMode.getValidParallel(parallelMode); + } + + @Override + public String getConfigFailurePolicy() { + return (String) cmdLineArgs.get(CommandLineArgs.CONFIG_FAILURE_POLICY); + } + + @Override + public Integer getThreadCount() { + int value = parseInt(cmdLineArgs.get(CommandLineArgs.THREAD_COUNT)); + if (value != -1) { + return value; + } + + return null; + } + + @Override + public Integer getDataProviderThreadCount() { + // Not supported by Surefire yet + int value = parseInt(cmdLineArgs.get(CommandLineArgs.DATA_PROVIDER_THREAD_COUNT)); + if (value != -1) { + return value; + } + + return null; + } + + @Override + public String getSuiteName() { + return (String) cmdLineArgs.get(CommandLineArgs.SUITE_NAME); + } + + @Override + public String getTestName() { + return (String) cmdLineArgs.get(CommandLineArgs.TEST_NAME); + } + + @Override + public String getReporter() { + return (String) cmdLineArgs.get(CommandLineArgs.REPORTER); + } + + @Override + public String useDefaultListeners() { + return (String) cmdLineArgs.get(CommandLineArgs.USE_DEFAULT_LISTENERS); + } + + @Override + public Boolean skipFailedInvocationCounts() { + return (Boolean) cmdLineArgs.get(CommandLineArgs.SKIP_FAILED_INVOCATION_COUNTS); + } + + @Override + public String getTestClass() { + return (String) cmdLineArgs.get(CommandLineArgs.TEST_CLASS); + } + + @Override + public String getTestNames() { + return (String) cmdLineArgs.get(CommandLineArgs.TEST_NAMES); + } + + @Override + public String getTestJar() { + return (String) cmdLineArgs.get(CommandLineArgs.TEST_JAR); + } + + @Override + public String getXmlPathInJar() { + return (String) cmdLineArgs.get(CommandLineArgs.XML_PATH_IN_JAR); + } + + @Override + public String getTestRunnerFactory() { + return (String) cmdLineArgs.get(CommandLineArgs.TEST_RUNNER_FACTORY); + } + + @Override + public Integer getPort() { + return null; + } + + @Override + public String getHost() { + return null; + } + + @Override + public List getCommandLineMethods() { + return null; + } + + @Override + public Integer getSuiteThreadPoolSize() { + int value = parseInt(cmdLineArgs.get(CommandLineArgs.SUITE_THREAD_POOL_SIZE)); + if (value != -1) { + return value; + } + + return null; + } + + @Override + public Boolean isRandomizeSuites() { + return null; + } + + @Override + public Boolean isDebug() { + return null; + } + + @Override + public Boolean alwaysRunListeners() { + return null; + } + + @Override + public String getThreadPoolFactoryClass() { + return (String) cmdLineArgs.get(CommandLineArgs.DEPENDENCY_INJECTOR_FACTORY); + } + + @Override + public String getDependencyInjectorFactory() { + return null; + } + + @Override + public Boolean failIfAllTestsSkipped() { + return Boolean.parseBoolean( + cmdLineArgs + .getOrDefault(CommandLineArgs.FAIL_IF_ALL_TESTS_SKIPPED, Boolean.FALSE) + .toString()); + } + + @Override + public String getSpiListenersToSkip() { + return (String) cmdLineArgs.getOrDefault(CommandLineArgs.LISTENERS_TO_SKIP_VIA_SPI, ""); + } + + @Override + public Boolean overrideIncludedMethods() { + return null; + } + + private static int parseInt(Object value) { + if (value == null) { + return -1; + } + if (value instanceof String) { + return Integer.parseInt(String.valueOf(value)); + } + if (value instanceof Integer) { + return (Integer) value; + } + throw new IllegalArgumentException("Unable to parse " + value + " as an Integer."); + } +} diff --git a/testng-core/src/main/java/org/testng/TestNG.java b/testng-core/src/main/java/org/testng/TestNG.java index b098e56527..c84ee9887b 100644 --- a/testng-core/src/main/java/org/testng/TestNG.java +++ b/testng-core/src/main/java/org/testng/TestNG.java @@ -4,8 +4,6 @@ import static org.testng.internal.Utils.isStringEmpty; import static org.testng.internal.Utils.isStringNotEmpty; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; import java.io.File; import java.io.IOException; import java.net.URLClassLoader; @@ -80,26 +78,9 @@ * * You can also define which groups to include or exclude, assign parameters, etc... * - *

The command line parameters are: - * - *

    - *
  • -d outputdir: specify the output directory - *
  • -testclass class_name: specifies one or several class names - *
  • -testjar jar_name: specifies the jar containing the tests - *
  • -sourcedir src1;src2: ; separated list of source directories (used only when - * javadoc annotations are used) - *
  • -target - *
  • -groups - *
  • -testrunfactory - *
  • -listener - *
- * *

Please consult documentation for more details. * *

FIXME: should support more than simple paths for suite xmls - * - * @see #usage() - * @author Cedric Beust */ @SuppressWarnings({"unused", "unchecked", "rawtypes"}) public class TestNG { @@ -121,8 +102,6 @@ public class TestNG { private static TestNG m_instance; - private static JCommander m_jCommander; - private List m_commandLineMethods; protected List m_suites = Lists.newArrayList(); private List m_cmdlineSuites; @@ -179,7 +158,9 @@ public class TestNG { private String m_jarPath; /** The path of the testng.xml file inside the jar file */ - private String m_xmlPathInJar = CommandLineArgs.XML_PATH_IN_JAR_DEFAULT; + public static final String XML_PATH_IN_JAR_DEFAULT = "testng.xml"; + + private String m_xmlPathInJar = XML_PATH_IN_JAR_DEFAULT; private List m_stringSuites = Lists.newArrayList(); @@ -238,7 +219,11 @@ public void toggleFailureIfAllTestsWereSkipped(boolean failIfAllTestsSkipped) { * wired in via service loaders. */ public void setListenersToSkipFromBeingWiredInViaServiceLoaders(String... listeners) { - m_listenersToSkipFromBeingWiredIn.addAll(Arrays.asList(listeners)); + setListenersToSkipFromBeingWiredInViaServiceLoaders(Arrays.asList(listeners)); + } + + public void setListenersToSkipFromBeingWiredInViaServiceLoaders(List listeners) { + m_listenersToSkipFromBeingWiredIn.addAll(listeners); } public int getStatus() { @@ -409,7 +394,8 @@ public void initializeSuitesAndJarFile() { /** @param threadCount Define the number of threads in the thread pool. */ public void setThreadCount(int threadCount) { if (threadCount < 1) { - exitWithError("Cannot use a threadCount parameter less than 1; 1 > " + threadCount); + throw new IllegalArgumentException( + "Cannot use a threadCount parameter less than 1; 1 > " + threadCount); } m_threadCount = threadCount; @@ -435,6 +421,10 @@ public void setCommandLineSuite(XmlSuite suite) { m_suites.add(suite); } + public void setCommandLineMethods(List commandLineMethods) { + m_commandLineMethods = commandLineMethods; + } + /** * Set the test classes to be run by this TestNG object. This method will create a dummy suite * that will wrap these classes called "Command Line Test". @@ -619,7 +609,7 @@ public void setGroups(String groups) { m_includedGroups = Utils.split(groups, ","); } - private void setTestRunnerFactoryClass( + public void setTestRunnerFactoryClass( Class testRunnerFactoryClass) { setTestRunnerFactory(m_objectFactory.newInstance(testRunnerFactoryClass)); } @@ -656,7 +646,7 @@ public void setListenerClasses(List> classes) { @Deprecated public void addListener(Object listener) { if (!(listener instanceof ITestNGListener)) { - exitWithError( + throw new IllegalArgumentException( "Listener " + listener + " must be one of ITestListener, ISuiteListener, IReporter, " @@ -1046,12 +1036,10 @@ public void run() { if (exitCodeListener.noTestsFound()) { if (TestRunner.getVerbose() > 1) { System.err.println("[TestNG] No tests found. Nothing was run"); - usage(); } } m_instance = null; - m_jCommander = null; } /** @@ -1083,13 +1071,6 @@ private void runExecutionListeners(boolean start) { } } - private static void usage() { - if (m_jCommander == null) { - m_jCommander = new JCommander(new CommandLineArgs()); - } - m_jCommander.usage(); - } - private void generateReports(List suiteRunners) { for (IReporter reporter : m_reporters.values()) { try { @@ -1114,7 +1095,6 @@ private void generateReports(List suiteRunners) { public List runSuitesLocally() { if (m_suites.isEmpty()) { error("No test suite found. Nothing to run"); - usage(); return Collections.emptyList(); } @@ -1329,194 +1309,23 @@ protected IConfiguration getConfiguration() { return m_configuration; } - /** - * The TestNG entry point for command line execution. - * - * @param argv the TestNG command line parameters. - */ + /** @deprecated */ + @Deprecated public static void main(String[] argv) { - TestNG testng = privateMain(argv, null); - System.exit(testng.getStatus()); + CliTestNgRunner.Main.main(argv); } - /** - * Note: this method is not part of the public API and is meant for internal usage only. - * - * @param argv The param arguments - * @param listener The listener - * @return The TestNG instance - */ + /** @deprecated */ + @Deprecated public static TestNG privateMain(String[] argv, ITestListener listener) { - TestNG result = new TestNG(); - - if (null != listener) { - result.addListener(listener); - } - - // - // Parse the arguments - // - try { - CommandLineArgs cla = new CommandLineArgs(); - - m_jCommander = new JCommander(cla); - m_jCommander.parse(argv); - validateCommandLineParameters(cla); - result.configure(cla); - } catch (ParameterException ex) { - exitWithError(ex.getMessage()); - } - - // - // Run - // - try { - result.run(); - } catch (TestNGException ex) { - if (TestRunner.getVerbose() > 1) { - ex.printStackTrace(System.out); - } else { - error(ex.getMessage()); - } - result.exitCode = ExitCode.newExitCodeRepresentingFailure(); - } - - return result; + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + return CliTestNgRunner.Main.privateMain(cliRunner, argv, listener); } - /** - * Configure the TestNG instance based on the command line parameters. - * - * @param cla The command line parameters - */ + /** @deprecated */ + @Deprecated protected void configure(CommandLineArgs cla) { - if (cla.verbose != null) { - setVerbose(cla.verbose); - } - if (cla.dependencyInjectorFactoryClass != null) { - Class clazz = ClassHelper.forName(cla.dependencyInjectorFactoryClass); - if (clazz != null && IInjectorFactory.class.isAssignableFrom(clazz)) { - m_configuration.setInjectorFactory( - m_objectFactory.newInstance((Class) clazz)); - } - } - if (cla.threadPoolFactoryClass != null) { - setExecutorFactoryClass(cla.threadPoolFactoryClass); - } - setOutputDirectory(cla.outputDirectory); - - String testClasses = cla.testClass; - if (null != testClasses) { - String[] strClasses = testClasses.split(","); - List> classes = Lists.newArrayList(); - for (String c : strClasses) { - classes.add(ClassHelper.fileToClass(c)); - } - - setTestClasses(classes.toArray(new Class[0])); - } - - setOutputDirectory(cla.outputDirectory); - - if (cla.testNames != null) { - setTestNames(Arrays.asList(cla.testNames.split(","))); - } - - // Note: can't use a Boolean field here because we are allowing a boolean - // parameter with an arity of 1 ("-usedefaultlisteners false") - if (cla.useDefaultListeners != null) { - setUseDefaultListeners("true".equalsIgnoreCase(cla.useDefaultListeners)); - } - - setGroups(cla.groups); - setExcludedGroups(cla.excludedGroups); - setTestJar(cla.testJar); - setXmlPathInJar(cla.xmlPathInJar); - setJUnit(cla.junit); - setMixed(cla.mixed); - setSkipFailedInvocationCounts(cla.skipFailedInvocationCounts); - toggleFailureIfAllTestsWereSkipped(cla.failIfAllTestsSkipped); - setListenersToSkipFromBeingWiredInViaServiceLoaders(cla.spiListenersToSkip.split(",")); - - m_configuration.setOverrideIncludedMethods(cla.overrideIncludedMethods); - - if (cla.parallelMode != null) { - setParallel(cla.parallelMode); - } - if (cla.configFailurePolicy != null) { - setConfigFailurePolicy(XmlSuite.FailurePolicy.getValidPolicy(cla.configFailurePolicy)); - } - if (cla.threadCount != null) { - setThreadCount(cla.threadCount); - } - if (cla.dataProviderThreadCount != null) { - setDataProviderThreadCount(cla.dataProviderThreadCount); - } - if (cla.suiteName != null) { - setDefaultSuiteName(cla.suiteName); - } - if (cla.testName != null) { - setDefaultTestName(cla.testName); - } - if (cla.listener != null) { - String sep = ";"; - if (cla.listener.contains(",")) { - sep = ","; - } - String[] strs = Utils.split(cla.listener, sep); - List> classes = Lists.newArrayList(); - - for (String cls : strs) { - Class clazz = ClassHelper.fileToClass(cls); - if (ITestNGListener.class.isAssignableFrom(clazz)) { - classes.add((Class) clazz); - } - } - - setListenerClasses(classes); - } - - if (null != cla.methodSelectors) { - String[] strs = Utils.split(cla.methodSelectors, ","); - for (String cls : strs) { - String[] sel = Utils.split(cls, ":"); - try { - if (sel.length == 2) { - addMethodSelector(sel[0], Integer.parseInt(sel[1])); - } else { - error("Method selector value was not in the format org.example.Selector:4"); - } - } catch (NumberFormatException nfe) { - error("Method selector value was not in the format org.example.Selector:4"); - } - } - } - - if (cla.objectFactory != null) { - setObjectFactory( - (Class) ClassHelper.fileToClass(cla.objectFactory)); - } - if (cla.testRunnerFactory != null) { - setTestRunnerFactoryClass( - (Class) ClassHelper.fileToClass(cla.testRunnerFactory)); - } - - ReporterConfig reporterConfig = ReporterConfig.deserialize(cla.reporter); - if (reporterConfig != null) { - addReporter(reporterConfig); - } - - if (cla.commandLineMethods.size() > 0) { - m_commandLineMethods = cla.commandLineMethods; - } - - if (cla.suiteFiles != null) { - setTestSuites(cla.suiteFiles); - } - - setSuiteThreadPoolSize(cla.suiteThreadPoolSize); - setRandomizeSuites(cla.randomizeSuites); - alwaysRunListeners(cla.alwaysRunListeners); + cla.configure(this); } public void setSuiteThreadPoolSize(Integer suiteThreadPoolSize) { @@ -1547,19 +1356,6 @@ public void setSourcePath(String path) { // nop } - private static int parseInt(Object value) { - if (value == null) { - return -1; - } - if (value instanceof String) { - return Integer.parseInt(String.valueOf(value)); - } - if (value instanceof Integer) { - return (Integer) value; - } - throw new IllegalArgumentException("Unable to parse " + value + " as an Integer."); - } - /** * This method is invoked by Maven's Surefire to configure the runner, do not remove unless you * know for sure that Surefire has been updated to use the new configure(CommandLineArgs) method. @@ -1570,113 +1366,7 @@ private static int parseInt(Object value) { @SuppressWarnings({"unchecked"}) @Deprecated public void configure(Map cmdLineArgs) { - CommandLineArgs result = new CommandLineArgs(); - - int value = parseInt(cmdLineArgs.get(CommandLineArgs.LOG)); - if (value != -1) { - result.verbose = value; - } - result.outputDirectory = (String) cmdLineArgs.get(CommandLineArgs.OUTPUT_DIRECTORY); - - String testClasses = (String) cmdLineArgs.get(CommandLineArgs.TEST_CLASS); - if (null != testClasses) { - result.testClass = testClasses; - } - - String testNames = (String) cmdLineArgs.get(CommandLineArgs.TEST_NAMES); - if (testNames != null) { - result.testNames = testNames; - } - - String useDefaultListeners = (String) cmdLineArgs.get(CommandLineArgs.USE_DEFAULT_LISTENERS); - if (null != useDefaultListeners) { - result.useDefaultListeners = useDefaultListeners; - } - - result.groups = (String) cmdLineArgs.get(CommandLineArgs.GROUPS); - result.excludedGroups = (String) cmdLineArgs.get(CommandLineArgs.EXCLUDED_GROUPS); - result.testJar = (String) cmdLineArgs.get(CommandLineArgs.TEST_JAR); - result.xmlPathInJar = (String) cmdLineArgs.get(CommandLineArgs.XML_PATH_IN_JAR); - result.junit = (Boolean) cmdLineArgs.get(CommandLineArgs.JUNIT); - result.mixed = (Boolean) cmdLineArgs.get(CommandLineArgs.MIXED); - result.skipFailedInvocationCounts = - (Boolean) cmdLineArgs.get(CommandLineArgs.SKIP_FAILED_INVOCATION_COUNTS); - result.failIfAllTestsSkipped = - Boolean.parseBoolean( - cmdLineArgs - .getOrDefault(CommandLineArgs.FAIL_IF_ALL_TESTS_SKIPPED, Boolean.FALSE) - .toString()); - result.spiListenersToSkip = - (String) cmdLineArgs.getOrDefault(CommandLineArgs.LISTENERS_TO_SKIP_VIA_SPI, ""); - String parallelMode = (String) cmdLineArgs.get(CommandLineArgs.PARALLEL); - if (parallelMode != null) { - result.parallelMode = XmlSuite.ParallelMode.getValidParallel(parallelMode); - } - - value = parseInt(cmdLineArgs.get(CommandLineArgs.THREAD_COUNT)); - if (value != -1) { - result.threadCount = value; - } - - // Not supported by Surefire yet - value = parseInt(cmdLineArgs.get(CommandLineArgs.DATA_PROVIDER_THREAD_COUNT)); - if (value != -1) { - result.dataProviderThreadCount = value; - } - String defaultSuiteName = (String) cmdLineArgs.get(CommandLineArgs.SUITE_NAME); - if (defaultSuiteName != null) { - result.suiteName = defaultSuiteName; - } - - String defaultTestName = (String) cmdLineArgs.get(CommandLineArgs.TEST_NAME); - if (defaultTestName != null) { - result.testName = defaultTestName; - } - - Object listeners = cmdLineArgs.get(CommandLineArgs.LISTENER); - if (listeners instanceof List) { - result.listener = Utils.join((List) listeners, ","); - } else { - result.listener = (String) listeners; - } - - String ms = (String) cmdLineArgs.get(CommandLineArgs.METHOD_SELECTORS); - if (null != ms) { - result.methodSelectors = ms; - } - - String objectFactory = (String) cmdLineArgs.get(CommandLineArgs.OBJECT_FACTORY); - if (null != objectFactory) { - result.objectFactory = objectFactory; - } - - String runnerFactory = (String) cmdLineArgs.get(CommandLineArgs.TEST_RUNNER_FACTORY); - if (null != runnerFactory) { - result.testRunnerFactory = runnerFactory; - } - - String reporterConfigs = (String) cmdLineArgs.get(CommandLineArgs.REPORTER); - if (reporterConfigs != null) { - result.reporter = reporterConfigs; - } - - String failurePolicy = (String) cmdLineArgs.get(CommandLineArgs.CONFIG_FAILURE_POLICY); - if (failurePolicy != null) { - result.configFailurePolicy = failurePolicy; - } - - value = parseInt(cmdLineArgs.get(CommandLineArgs.SUITE_THREAD_POOL_SIZE)); - if (value != -1) { - result.suiteThreadPoolSize = value; - } - - String dependencyInjectorFactoryClass = - (String) cmdLineArgs.get(CommandLineArgs.DEPENDENCY_INJECTOR_FACTORY); - if (dependencyInjectorFactoryClass != null) { - result.dependencyInjectorFactoryClass = dependencyInjectorFactoryClass; - } - - configure(result); + (new SurefireCommandLineArgs(cmdLineArgs)).configure(this); } /** @param testNames Only run the specified tests from the suite. */ @@ -1688,7 +1378,7 @@ public void setSkipFailedInvocationCounts(Boolean skip) { m_skipFailedInvocationCounts = skip; } - private void addReporter(ReporterConfig reporterConfig) { + public void addReporter(ReporterConfig reporterConfig) { IReporter instance = newReporterInstance(reporterConfig); if (instance != null) { addListener(instance); @@ -1732,41 +1422,11 @@ public void setMixed(Boolean isMixed) { m_isMixed = isMixed; } - /** - * Double check that the command line parameters are valid. - * - * @param args The command line to check - */ - protected static void validateCommandLineParameters(CommandLineArgs args) { - String testClasses = args.testClass; - List testNgXml = args.suiteFiles; - String testJar = args.testJar; - List methods = args.commandLineMethods; - - if (testClasses == null - && testJar == null - && (testNgXml == null || testNgXml.isEmpty()) - && (methods == null || methods.isEmpty())) { - throw new ParameterException( - "You need to specify at least one testng.xml, one class" + " or one method"); - } - - String groups = args.groups; - String excludedGroups = args.excludedGroups; - - if (testJar == null - && (null != groups || null != excludedGroups) - && testClasses == null - && (testNgXml == null || testNgXml.isEmpty())) { - throw new ParameterException("Groups option should be used with testclass option"); - } - - Boolean junit = args.junit; - Boolean mixed = args.mixed; - if (junit && mixed) { - throw new ParameterException( - CommandLineArgs.MIXED + " can't be combined with " + CommandLineArgs.JUNIT); - } + /** @deprecated */ + @Deprecated + protected static void validateCommandLineParameters(CommandLineArgs args) + throws CommandLineArgs.ParameterException { + CliTestNgRunner.Main.validateCommandLineParameters(args); } /** @return true if at least one test failed. */ @@ -1784,9 +1444,9 @@ public boolean hasSkip() { return this.exitCode.hasSkip(); } + @Deprecated static void exitWithError(String msg) { System.err.println(msg); - usage(); System.exit(1); } @@ -1938,7 +1598,19 @@ public List getServiceLoaderListeners() { return Lists.newArrayList(serviceLoaderListeners.values()); } + public void setInjectorFactory(Class clazz) { + setInjectorFactory(m_objectFactory.newInstance(clazz)); + } + public void setInjectorFactory(IInjectorFactory factory) { this.m_configuration.setInjectorFactory(factory); } + + public void setOverrideIncludedMethods(boolean overrideIncludedMethods) { + m_configuration.setOverrideIncludedMethods(overrideIncludedMethods); + } + + public void setExitCode(ExitCode exitCode) { + this.exitCode = exitCode; + } } diff --git a/testng-core/src/test/java/test/CommandLineTest.java b/testng-core/src/test/java/test/CommandLineTest.java index 096d7b3ffb..0e9c335b22 100644 --- a/testng-core/src/test/java/test/CommandLineTest.java +++ b/testng-core/src/test/java/test/CommandLineTest.java @@ -11,10 +11,12 @@ import java.util.List; import java.util.Map; import org.testng.Assert; +import org.testng.CliTestNgRunner; import org.testng.CommandLineArgs; import org.testng.IInjectorFactory; import org.testng.ITestContext; import org.testng.ITestResult; +import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.Test; @@ -27,6 +29,7 @@ public class CommandLineTest { /** Test -junit */ @Test(groups = {"current"}) public void junitParsing() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String[] argv = { "-log", "0", @@ -37,7 +40,7 @@ public void junitParsing() { "test.sample.JUnitSample1" }; TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); List passed = tla.getPassedTests(); assertEquals(passed.size(), 2); @@ -52,13 +55,14 @@ public void junitParsing() { /** Test the absence of -junit */ @Test(groups = {"current"}) public void junitParsing2() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String[] argv = { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(), "-testclass", "test.sample.JUnitSample1" }; TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); List passed = tla.getPassedTests(); assertEquals(passed.size(), 0); @@ -67,6 +71,7 @@ public void junitParsing2() { /** Test the ability to override the default command line Suite name */ @Test(groups = {"current"}) public void suiteNameOverride() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String suiteName = "MySuiteName"; String[] argv = { "-log", @@ -80,7 +85,7 @@ public void suiteNameOverride() { suiteName }; TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); List contexts = tla.getTestContexts(); assertTrue(contexts.size() > 0); @@ -92,6 +97,7 @@ public void suiteNameOverride() { /** Test the ability to override the default command line test name */ @Test(groups = {"current"}) public void testNameOverride() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String testName = "My Test Name"; String[] argv = { "-log", @@ -105,7 +111,7 @@ public void testNameOverride() { testName }; TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); List contexts = tla.getTestContexts(); assertTrue(contexts.size() > 0); @@ -116,7 +122,9 @@ public void testNameOverride() { @Test public void testUseDefaultListenersArgument() { - TestNG.privateMain( + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + CliTestNgRunner.Main.privateMain( + cliRunner, new String[] { "-log", "0", "-usedefaultlisteners", "false", "-testclass", "test.sample.JUnitSample1" }, @@ -125,13 +133,14 @@ public void testUseDefaultListenersArgument() { @Test public void testMethodParameter() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String[] argv = { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(), "-methods", "test.sample.Sample2.method1,test.sample.Sample2.method3", }; TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); List passed = tla.getPassedTests(); Assert.assertEquals(passed.size(), 2); diff --git a/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java b/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java index c81cd57b97..6bbf1ffc3a 100644 --- a/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java +++ b/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java @@ -10,6 +10,8 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import org.testng.CliTestNgRunner; +import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.Test; @@ -76,14 +78,18 @@ public void ensureParallelismIsHonoredWhenOnlyClassesSpecifiedInJar() throws IOE "-listener", LocalLogAggregator.class.getCanonicalName() }; - TestNG.privateMain(args, null); + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + CliTestNgRunner.Main.privateMain(cliRunner, args, null); Set logs = LocalLogAggregator.getLogs(); assertThat(logs).hasSize(2); } @Test(description = "GITHUB-1810") public void ensureNoNullPointerExceptionIsThrown() throws IOException { - TestNG testng = TestNG.privateMain(new String[] {createTemporarySuiteAndGetItsPath()}, null); + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + TestNG testng = + CliTestNgRunner.Main.privateMain( + cliRunner, new String[] {createTemporarySuiteAndGetItsPath()}, null); assertThat(testng.getStatus()).isEqualTo(8); } diff --git a/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java b/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java index 329fe4248a..cb64bbc64e 100644 --- a/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java +++ b/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java @@ -3,7 +3,9 @@ import static org.testng.Assert.assertEquals; import static test.SimpleBaseTest.getPathToResource; +import org.testng.CliTestNgRunner; import org.testng.ITestContext; +import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.BeforeClass; @@ -102,8 +104,9 @@ public void commandLineTest_policyAsSkip() { "-testclass", ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName() }; + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); verify(tla, 1, 1, 2); } @@ -121,8 +124,9 @@ public void commandLineTest_policyAsContinue() { "-testclass", ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName() }; + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); verify(tla, 2, 0, 2); } @@ -139,8 +143,9 @@ public void commandLineTestWithXMLFile_policyAsSkip() { "skip", getPathToResource("testng-configfailure.xml") }; + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); verify(tla, 1, 1, 2); } @@ -157,8 +162,9 @@ public void commandLineTestWithXMLFile_policyAsContinue() { "continue", getPathToResource("testng-configfailure.xml") }; + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); verify(tla, 2, 0, 2); } diff --git a/testng-core/src/test/java/test/methodselectors/CommandLineTest.java b/testng-core/src/test/java/test/methodselectors/CommandLineTest.java index b79a45ad96..5815316ffa 100644 --- a/testng-core/src/test/java/test/methodselectors/CommandLineTest.java +++ b/testng-core/src/test/java/test/methodselectors/CommandLineTest.java @@ -2,9 +2,10 @@ import java.util.List; import org.assertj.core.api.Assertions; +import org.testng.CliTestNgRunner; import org.testng.ITestResult; +import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; -import org.testng.TestNG; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import test.SimpleBaseTest; @@ -43,8 +44,9 @@ public void setup() { @Test public void commandLineNegativePriorityAllGroups() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); ARG_WITHOUT_GROUPS[7] = "test.methodselectors.AllTestsMethodSelector:-1"; - TestNG.privateMain(ARG_WITHOUT_GROUPS, tla); + CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITHOUT_GROUPS, tla); String[] passed = {"test1", "test2", "test3"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -53,8 +55,9 @@ public void commandLineNegativePriorityAllGroups() { @Test public void commandLineNegativePriorityGroup2() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); ARG_WITHOUT_GROUPS[7] = "test.methodselectors.Test2MethodSelector:-1"; - TestNG.privateMain(ARG_WITHOUT_GROUPS, tla); + CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITHOUT_GROUPS, tla); String[] passed = {"test2"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -63,9 +66,10 @@ public void commandLineNegativePriorityGroup2() { @Test public void commandLineLessThanPriorityTest1Test() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); ARG_WITH_GROUPS[7] = "test.methodselectors.Test2MethodSelector:5"; ARG_WITH_GROUPS[9] = "test1"; - TestNG.privateMain(ARG_WITH_GROUPS, tla); + CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITH_GROUPS, tla); String[] passed = {"test1", "test2"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -74,9 +78,10 @@ public void commandLineLessThanPriorityTest1Test() { @Test public void commandLineGreaterThanPriorityTest1Test2() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); ARG_WITH_GROUPS[7] = "test.methodselectors.Test2MethodSelector:15"; ARG_WITH_GROUPS[9] = "test1"; - TestNG.privateMain(ARG_WITH_GROUPS, tla); + CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITH_GROUPS, tla); String[] passed = {"test2"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -85,9 +90,10 @@ public void commandLineGreaterThanPriorityTest1Test2() { @Test public void commandLineLessThanPriorityAllTests() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); ARG_WITH_GROUPS[7] = "test.methodselectors.AllTestsMethodSelector:5"; ARG_WITH_GROUPS[9] = "test1"; - TestNG.privateMain(ARG_WITH_GROUPS, tla); + CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITH_GROUPS, tla); String[] passed = {"test1", "test2", "test3"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -96,10 +102,11 @@ public void commandLineLessThanPriorityAllTests() { @Test public void commandLineMultipleSelectors() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); ARG_WITH_GROUPS[7] = "test.methodselectors.NoTestSelector:7,test.methodselectors.Test2MethodSelector:5"; ARG_WITH_GROUPS[9] = "test1"; - TestNG.privateMain(ARG_WITH_GROUPS, tla); + CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITH_GROUPS, tla); String[] passed = {"test1", "test2"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -108,8 +115,9 @@ public void commandLineMultipleSelectors() { @Test public void commandLineNoTest1Selector() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); ARG_WITHOUT_GROUPS[7] = "test.methodselectors.NoTest1MethodSelector:5"; - TestNG.privateMain(ARG_WITHOUT_GROUPS, tla); + CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITHOUT_GROUPS, tla); String[] passed = {"test2", "test3"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -118,9 +126,10 @@ public void commandLineNoTest1Selector() { @Test public void commandLineTestWithXmlFile() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); ARG_WITHOUT_CLASSES[5] = "test.methodselectors.NoTest1MethodSelector:5"; ARG_WITHOUT_CLASSES[6] = getPathToResource("testng-methodselectors.xml"); - TestNG.privateMain(ARG_WITHOUT_CLASSES, tla); + CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITHOUT_CLASSES, tla); String[] passed = {"test2", "test3"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -129,6 +138,7 @@ public void commandLineTestWithXmlFile() { @Test(description = "GITHUB-2407") public void testOverrideExcludedMethodsCommandLineExclusions() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String[] args = new String[] { "src/test/resources/test/methodselectors/sampleTest.xml", @@ -141,7 +151,7 @@ public void testOverrideExcludedMethodsCommandLineExclusions() { "-overrideincludedmethods" }; - TestNG.privateMain(args, tla); + CliTestNgRunner.Main.privateMain(cliRunner, args, tla); // test1 is excluded, so only test2 is left in the passed list String[] passed = {"test2"}; @@ -152,6 +162,7 @@ public void testOverrideExcludedMethodsCommandLineExclusions() { @Test(description = "GITHUB-2407") public void testOverrideExcludedMethodsSuiteExclusions() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String[] args = new String[] { "src/test/resources/test/methodselectors/sampleTestExclusions.xml", @@ -162,7 +173,7 @@ public void testOverrideExcludedMethodsSuiteExclusions() { "-overrideincludedmethods" }; - TestNG.privateMain(args, tla); + CliTestNgRunner.Main.privateMain(cliRunner, args, tla); String[] passed = {}; String[] failed = {}; diff --git a/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java b/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java index c68e43689c..e2d62e5a87 100644 --- a/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java +++ b/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java @@ -3,8 +3,10 @@ import java.util.Collections; import java.util.List; import org.testng.Assert; +import org.testng.CliTestNgRunner; import org.testng.ITestNGListener; import org.testng.ITestResult; +import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.BeforeMethod; @@ -58,13 +60,14 @@ public void xmlXmlSuite() { @Test public void fileOnCommandLine() { + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String[] args = new String[] { "-d", OutputDirectoryPatch.getOutputDirectory(), getPathToResource("methodselector-in-xml.xml") }; - TestNG.privateMain(args, m_tla); + CliTestNgRunner.Main.privateMain(cliRunner, args, m_tla); validate(new String[] {"test2"}); } diff --git a/testng-core/src/test/java/test/mixed/MixedTest.java b/testng-core/src/test/java/test/mixed/MixedTest.java index e338847df6..11e9a53e07 100644 --- a/testng-core/src/test/java/test/mixed/MixedTest.java +++ b/testng-core/src/test/java/test/mixed/MixedTest.java @@ -1,8 +1,9 @@ package test.mixed; import org.testng.Assert; +import org.testng.CliTestNgRunner; +import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; -import org.testng.TestNG; import org.testng.annotations.Test; import test.BaseTest; import testhelper.OutputDirectoryPatch; @@ -24,8 +25,9 @@ public void mixedWithExcludedGroups() { "-testclass", "test.mixed.JUnit3Test1,test.mixed.JUnit4Test1,test.mixed.TestNGTest1,test.mixed.TestNGGroups" }; + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); Assert.assertEquals( tla.getPassedTests().size(), @@ -45,8 +47,9 @@ public void mixedClasses() { "-testclass", "test.mixed.JUnit3Test1,test.mixed.JUnit4Test1,test.mixed.TestNGTest1" }; + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); Assert.assertEquals(tla.getPassedTests().size(), 6); Assert.assertEquals(tla.getFailedTests().size(), 0); @@ -63,8 +66,9 @@ public void mixedMethods() { "-methods", "test.mixed.JUnit3Test1.testB,test.mixed.JUnit4Test1.atest,test.mixed.TestNGTest1.tngCustomTest1" }; + CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - TestNG.privateMain(argv, tla); + CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); Assert.assertEquals(tla.getPassedTests().size(), 3); Assert.assertEquals(tla.getFailedTests().size(), 0); diff --git a/testng-core/src/test/java/test/reports/EmailableReporterTest.java b/testng-core/src/test/java/test/reports/EmailableReporterTest.java index cc35f2b1a0..6e9c288a24 100644 --- a/testng-core/src/test/java/test/reports/EmailableReporterTest.java +++ b/testng-core/src/test/java/test/reports/EmailableReporterTest.java @@ -5,6 +5,7 @@ import java.io.File; import java.lang.reflect.Method; import java.security.Permission; +import org.testng.CliTestNgRunner; import org.testng.IReporter; import org.testng.ITestNGListener; import org.testng.TestNG; @@ -91,7 +92,7 @@ private void runTestViaMainMethod(String clazzName, String jvm) { if (jvm != null) { System.setProperty(jvm, filename); } - TestNG.main(args); + CliTestNgRunner.Main.main(args); } catch (SecurityException t) { // Gobble Security exception } finally { From e04b883691d49bedfbd6a97da99a5180d92a9995 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Fri, 16 Jul 2021 22:36:12 +0200 Subject: [PATCH 2/8] Move Jcommander into its own module --- settings.gradle.kts | 1 + testng-ant/build.gradle.kts | 1 + testng-core/build.gradle.kts | 1 - .../org/testng/AbstractCommandLineArgs.java | 119 ++++++++++++ .../main/java/org/testng/CliTestNgRunner.java | 32 ++- .../org/testng/SurefireCommandLineArgs.java | 42 ++-- .../src/main/java/org/testng/TestNG.java | 3 +- .../src/test/java/test/CommandLineTest.java | 105 +++++----- .../commandline/CommandLineOverridesXml.java | 28 ++- .../FailurePolicyTest.java | 108 +++++------ .../github1417/YetAnotherTestClassSample.java | 4 +- .../test/junitreports/JUnitReportsTest.java | 3 +- .../test/methodselectors/CommandLineTest.java | 182 ++++++------------ .../MethodSelectorInSuiteTest.java | 22 +-- .../src/test/java/test/mixed/MixedTest.java | 79 ++++---- .../TestExecutionListenerInvocationOrder.java | 3 +- testng-jcommander/build.gradle.kts | 10 + .../src/main/java/org/testng/Converter.java | 0 .../org/testng/JCommanderCliTestNgRunner.java | 0 .../org/testng/JCommanderCommandLineArgs.java | 82 ++------ .../services/org.testng.CliTestNgRunner | 1 + 21 files changed, 408 insertions(+), 418 deletions(-) create mode 100644 testng-core/src/main/java/org/testng/AbstractCommandLineArgs.java create mode 100644 testng-jcommander/build.gradle.kts rename {testng-core => testng-jcommander}/src/main/java/org/testng/Converter.java (100%) rename {testng-core => testng-jcommander}/src/main/java/org/testng/JCommanderCliTestNgRunner.java (100%) rename {testng-core => testng-jcommander}/src/main/java/org/testng/JCommanderCommandLineArgs.java (78%) create mode 100644 testng-jcommander/src/main/resources/META-INF/services/org.testng.CliTestNgRunner diff --git a/settings.gradle.kts b/settings.gradle.kts index fd968bb14f..58db1733c5 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -25,6 +25,7 @@ include(":testng-bom") include(":testng-collections") include(":testng-core") include(":testng-core-api") +include(":testng-jcommander") include(":testng-reflection-utils") include(":testng-runner-api") include(":testng-runner-junit4") diff --git a/testng-ant/build.gradle.kts b/testng-ant/build.gradle.kts index f918ec836e..c269675fa8 100644 --- a/testng-ant/build.gradle.kts +++ b/testng-ant/build.gradle.kts @@ -6,6 +6,7 @@ dependencies { api("org.apache.ant:ant:_") implementation(projects.testngCore) + implementation(projects.testngJcommander) testImplementation(projects.testngAsserts) testImplementation("org.apache.ant:ant-testutil:_") } diff --git a/testng-core/build.gradle.kts b/testng-core/build.gradle.kts index 8c8e1a2be6..46856df48a 100644 --- a/testng-core/build.gradle.kts +++ b/testng-core/build.gradle.kts @@ -26,7 +26,6 @@ dependencies { api(projects.testngCoreApi) // Annotations have to be available on the compile classpath for the proper compilation api("com.google.code.findbugs:jsr305:_") - api("com.beust:jcommander:_") "guiceApi"(platform("com.google.inject:guice-bom:_")) "guiceApi"("com.google.inject:guice") diff --git a/testng-core/src/main/java/org/testng/AbstractCommandLineArgs.java b/testng-core/src/main/java/org/testng/AbstractCommandLineArgs.java new file mode 100644 index 0000000000..3708d3e81a --- /dev/null +++ b/testng-core/src/main/java/org/testng/AbstractCommandLineArgs.java @@ -0,0 +1,119 @@ +package org.testng; + +import org.testng.collections.Lists; +import org.testng.internal.ClassHelper; +import org.testng.internal.Utils; +import org.testng.log4testng.Logger; +import org.testng.xml.XmlMethodSelector; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public abstract class AbstractCommandLineArgs implements CommandLineArgs { + + private static final Logger LOGGER = Logger.getLogger(TestNG.class); + + protected abstract String[] getListenerValues(); + + public List> getListener() { + String[] strs = getListenerValues(); + if (strs == null) { + return null; + } + List> classes = Lists.newArrayList(); + for (String cls : strs) { + Class clazz = ClassHelper.fileToClass(cls); + if (ITestNGListener.class.isAssignableFrom(clazz)) { + classes.add((Class) clazz); + } + } + + return classes; + } + + protected abstract String getMethodSelectorsValue(); + + @Override + public List getMethodSelectors() { + String methodSelectors = getMethodSelectorsValue(); + if (methodSelectors == null) { + return null; + } + String[] strs = Utils.split(methodSelectors, ","); + List selectors = new ArrayList<>(strs.length); + for (String cls : strs) { + String[] sel = Utils.split(cls, ":"); + try { + if (sel.length == 2) { + XmlMethodSelector selector = new XmlMethodSelector(); + selector.setName(sel[0]); + selector.setPriority(Integer.parseInt(sel[1])); + selectors.add(selector); + } else { + LOGGER.error("Method selector value was not in the format org.example.Selector:4"); + } + } catch (NumberFormatException nfe) { + LOGGER.error("Method selector value was not in the format org.example.Selector:4"); + } + } + return selectors; + } + + protected abstract String getObjectFactoryValue(); + + @Override + public Class getObjectFactory() { + String objectFactory = getObjectFactoryValue(); + if (objectFactory == null) { + return null; + } + return (Class) ClassHelper.fileToClass(objectFactory); + } + + protected abstract String getTestClassValue(); + + @Override + public List> getTestClass() { + String testClass = getTestClassValue(); + if (testClass == null) { + return null; + } + String[] strClasses = testClass.split(","); + List> classes = Lists.newArrayList(); + for (String c : strClasses) { + classes.add(ClassHelper.fileToClass(c)); + } + + return classes; + } + + protected abstract String getTestNamesValue(); + + @Override + public List getTestNames() { + String testNames = getTestNamesValue(); + if (testNames == null) { + return null; + } + return Arrays.asList(testNames.split(",")); + } + + protected abstract String getTestRunnerFactoryValue(); + + @Override + public Class getTestRunnerFactory() { + String testRunnerFactory = getObjectFactoryValue(); + if (testRunnerFactory == null) { + return null; + } + return (Class) ClassHelper.fileToClass(testRunnerFactory); + } + + protected abstract String getSpiListenersToSkipValue(); + + @Override + public List getSpiListenersToSkip() { + String spiListenersToSkip = getSpiListenersToSkipValue(); + return Arrays.asList(spiListenersToSkip.split(",")); + } +} diff --git a/testng-core/src/main/java/org/testng/CliTestNgRunner.java b/testng-core/src/main/java/org/testng/CliTestNgRunner.java index ba0dbeaa18..d362f6c662 100644 --- a/testng-core/src/main/java/org/testng/CliTestNgRunner.java +++ b/testng-core/src/main/java/org/testng/CliTestNgRunner.java @@ -1,7 +1,10 @@ package org.testng; +import org.testng.collections.Lists; import org.testng.internal.ExitCode; import org.testng.log4testng.Logger; +import java.util.List; +import java.util.ServiceLoader; public interface CliTestNgRunner { @@ -21,14 +24,35 @@ class Main { private static final Logger LOGGER = Logger.getLogger(TestNG.class); + private static final CliTestNgRunner RUNNER = findRunner(); + + private static CliTestNgRunner findRunner() { + List runners = Lists.newArrayList(); + ServiceLoader runnerLoader = ServiceLoader.load(CliTestNgRunner.class); + for (CliTestNgRunner runner : runnerLoader) { + runners.add(runner); + } + if (runners.isEmpty()) { + throw new TestNGException("No runner found"); + } + CliTestNgRunner runner = runners.get(0); + if (runners.size() > 1) { + LOGGER.warn("Too many runners found. Takes the first one: " + runner.getClass()); + } + return runner; + } + + public static CliTestNgRunner getRunner() { + return RUNNER; + } + /** * The TestNG entry point for command line execution. * * @param argv the TestNG command line parameters. */ public static void main(String[] argv) { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - TestNG testng = privateMain(cliRunner, argv, null); + TestNG testng = privateMain(RUNNER, argv, null); System.exit(testng.getStatus()); } @@ -41,6 +65,10 @@ public static void main(String[] argv) { */ public static TestNG privateMain( CliTestNgRunner cliRunner, String[] argv, ITestListener listener) { + if (cliRunner == null) { + LOGGER.warn("No runner passed, use the default one: " + RUNNER.getClass()); + cliRunner = RUNNER; + } TestNG result = new TestNG(); if (null != listener) { diff --git a/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java b/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java index fa401385d3..6b7b76002c 100644 --- a/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java +++ b/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java @@ -2,10 +2,10 @@ import java.util.List; import java.util.Map; -import org.testng.internal.Utils; +import org.testng.internal.ReporterConfig; import org.testng.xml.XmlSuite; -public class SurefireCommandLineArgs implements CommandLineArgs { +public class SurefireCommandLineArgs extends AbstractCommandLineArgs { private final Map cmdLineArgs; @@ -54,22 +54,22 @@ public Boolean isMixed() { } @Override - public String getListener() { + protected String[] getListenerValues() { Object listeners = cmdLineArgs.get(CommandLineArgs.LISTENER); if (listeners instanceof List) { - return Utils.join((List) listeners, ","); + return ((List) listeners).toArray(new String[0]); } else { - return (String) listeners; + return ((String) listeners).split(","); } } @Override - public String getMethodSelectors() { + protected String getMethodSelectorsValue() { return (String) cmdLineArgs.get(CommandLineArgs.METHOD_SELECTORS); } @Override - public String getObjectFactory() { + protected String getObjectFactoryValue() { return (String) cmdLineArgs.get(CommandLineArgs.OBJECT_FACTORY); } @@ -80,8 +80,9 @@ public XmlSuite.ParallelMode getParallelMode() { } @Override - public String getConfigFailurePolicy() { - return (String) cmdLineArgs.get(CommandLineArgs.CONFIG_FAILURE_POLICY); + public XmlSuite.FailurePolicy getConfigFailurePolicy() { + String configFailurePolicy = (String) cmdLineArgs.get(CommandLineArgs.CONFIG_FAILURE_POLICY); + return XmlSuite.FailurePolicy.getValidPolicy(configFailurePolicy); } @Override @@ -116,13 +117,18 @@ public String getTestName() { } @Override - public String getReporter() { - return (String) cmdLineArgs.get(CommandLineArgs.REPORTER); + public ReporterConfig getReporter() { + String reporter = (String) cmdLineArgs.get(CommandLineArgs.REPORTER); + return ReporterConfig.deserialize(reporter); } @Override - public String useDefaultListeners() { - return (String) cmdLineArgs.get(CommandLineArgs.USE_DEFAULT_LISTENERS); + public Boolean useDefaultListeners() { + String useDefaultListeners = (String) cmdLineArgs.get(CommandLineArgs.USE_DEFAULT_LISTENERS); + if (useDefaultListeners == null) { + return null; + } + return Boolean.valueOf(useDefaultListeners); } @Override @@ -131,12 +137,12 @@ public Boolean skipFailedInvocationCounts() { } @Override - public String getTestClass() { + protected String getTestClassValue() { return (String) cmdLineArgs.get(CommandLineArgs.TEST_CLASS); } @Override - public String getTestNames() { + protected String getTestNamesValue() { return (String) cmdLineArgs.get(CommandLineArgs.TEST_NAMES); } @@ -151,7 +157,7 @@ public String getXmlPathInJar() { } @Override - public String getTestRunnerFactory() { + protected String getTestRunnerFactoryValue() { return (String) cmdLineArgs.get(CommandLineArgs.TEST_RUNNER_FACTORY); } @@ -201,7 +207,7 @@ public String getThreadPoolFactoryClass() { } @Override - public String getDependencyInjectorFactory() { + public Class getDependencyInjectorFactory() { return null; } @@ -214,7 +220,7 @@ public Boolean failIfAllTestsSkipped() { } @Override - public String getSpiListenersToSkip() { + protected String getSpiListenersToSkipValue() { return (String) cmdLineArgs.getOrDefault(CommandLineArgs.LISTENERS_TO_SKIP_VIA_SPI, ""); } diff --git a/testng-core/src/main/java/org/testng/TestNG.java b/testng-core/src/main/java/org/testng/TestNG.java index c84ee9887b..7758ccd817 100644 --- a/testng-core/src/main/java/org/testng/TestNG.java +++ b/testng-core/src/main/java/org/testng/TestNG.java @@ -1318,8 +1318,7 @@ public static void main(String[] argv) { /** @deprecated */ @Deprecated public static TestNG privateMain(String[] argv, ITestListener listener) { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - return CliTestNgRunner.Main.privateMain(cliRunner, argv, listener); + return CliTestNgRunner.Main.privateMain(null, argv, listener); } /** @deprecated */ diff --git a/testng-core/src/test/java/test/CommandLineTest.java b/testng-core/src/test/java/test/CommandLineTest.java index 0e9c335b22..205fa29eaa 100644 --- a/testng-core/src/test/java/test/CommandLineTest.java +++ b/testng-core/src/test/java/test/CommandLineTest.java @@ -7,16 +7,15 @@ import com.google.inject.Module; import com.google.inject.Stage; import java.lang.reflect.Field; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.testng.Assert; -import org.testng.CliTestNgRunner; import org.testng.CommandLineArgs; import org.testng.IInjectorFactory; import org.testng.ITestContext; import org.testng.ITestResult; -import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.Test; @@ -29,18 +28,14 @@ public class CommandLineTest { /** Test -junit */ @Test(groups = {"current"}) public void junitParsing() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - String[] argv = { - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-junit", - "-testclass", - "test.sample.JUnitSample1" - }; TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setJUnit(true); + testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.run(); List passed = tla.getPassedTests(); assertEquals(passed.size(), 2); @@ -55,14 +50,13 @@ public void junitParsing() { /** Test the absence of -junit */ @Test(groups = {"current"}) public void junitParsing2() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - String[] argv = { - "-log", "0", - "-d", OutputDirectoryPatch.getOutputDirectory(), - "-testclass", "test.sample.JUnitSample1" - }; TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.run(); List passed = tla.getPassedTests(); assertEquals(passed.size(), 0); @@ -71,21 +65,17 @@ public void junitParsing2() { /** Test the ability to override the default command line Suite name */ @Test(groups = {"current"}) public void suiteNameOverride() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String suiteName = "MySuiteName"; - String[] argv = { - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-junit", - "-testclass", - "test.sample.JUnitSample1", - "-suitename", - suiteName - }; + TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setJUnit(true); + testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.setDefaultSuiteName(suiteName); + testng.run(); List contexts = tla.getTestContexts(); assertTrue(contexts.size() > 0); @@ -97,21 +87,17 @@ public void suiteNameOverride() { /** Test the ability to override the default command line test name */ @Test(groups = {"current"}) public void testNameOverride() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); String testName = "My Test Name"; - String[] argv = { - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-junit", - "-testclass", - "test.sample.JUnitSample1", - "-testname", - testName - }; + TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setJUnit(true); + testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.setDefaultTestName(testName); + testng.run(); List contexts = tla.getTestContexts(); assertTrue(contexts.size() > 0); @@ -122,25 +108,24 @@ public void testNameOverride() { @Test public void testUseDefaultListenersArgument() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - CliTestNgRunner.Main.privateMain( - cliRunner, - new String[] { - "-log", "0", "-usedefaultlisteners", "false", "-testclass", "test.sample.JUnitSample1" - }, - null); + TestNG testng = new TestNG(); + testng.setVerbose(0); + testng.setUseDefaultListeners(false); + testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.run(); } @Test public void testMethodParameter() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - String[] argv = { - "-log", "0", - "-d", OutputDirectoryPatch.getOutputDirectory(), - "-methods", "test.sample.Sample2.method1,test.sample.Sample2.method3", - }; TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setCommandLineMethods(Collections.singletonList( + "test.sample.Sample2.method1,test.sample.Sample2.method3" + )); + testng.run(); List passed = tla.getPassedTests(); Assert.assertEquals(passed.size(), 2); diff --git a/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java b/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java index 6bbf1ffc3a..e6e9f3288e 100644 --- a/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java +++ b/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java @@ -10,8 +10,6 @@ import java.util.Collections; import java.util.List; import java.util.Set; -import org.testng.CliTestNgRunner; -import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.Test; @@ -69,27 +67,23 @@ public void ensureThatParallelismAndThreadCountAreRallied() { public void ensureParallelismIsHonoredWhenOnlyClassesSpecifiedInJar() throws IOException { Class[] classes = new Class[] {TestSampleA.class, TestSampleB.class}; File jarfile = JarCreator.generateJar(classes); - String[] args = - new String[] { - "-parallel", - "classes", - "-testjar", - jarfile.getAbsolutePath(), - "-listener", - LocalLogAggregator.class.getCanonicalName() - }; - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - CliTestNgRunner.Main.privateMain(cliRunner, args, null); + + TestNG testng = new TestNG(); + testng.setParallel(XmlSuite.ParallelMode.CLASSES); + testng.setTestJar(jarfile.getAbsolutePath()); + testng.setListenerClasses(Collections.singletonList(LocalLogAggregator.class)); + testng.run(); + Set logs = LocalLogAggregator.getLogs(); assertThat(logs).hasSize(2); } @Test(description = "GITHUB-1810") public void ensureNoNullPointerExceptionIsThrown() throws IOException { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - TestNG testng = - CliTestNgRunner.Main.privateMain( - cliRunner, new String[] {createTemporarySuiteAndGetItsPath()}, null); + TestNG testng = new TestNG(); + testng.setTestSuites(Collections.singletonList(createTemporarySuiteAndGetItsPath())); + testng.run(); + assertThat(testng.getStatus()).isEqualTo(8); } diff --git a/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java b/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java index cb64bbc64e..112a095903 100644 --- a/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java +++ b/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java @@ -3,9 +3,9 @@ import static org.testng.Assert.assertEquals; import static test.SimpleBaseTest.getPathToResource; -import org.testng.CliTestNgRunner; +import java.util.Collections; +import java.util.List; import org.testng.ITestContext; -import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.BeforeClass; @@ -77,8 +77,9 @@ public void confFailureTest( @Test public void confFailureTestInvolvingGroups() { - Class[] classesUnderTest = - new Class[] {ClassWithFailedBeforeClassMethodAndBeforeGroupsAfterClassAfterGroups.class}; + Class[] classesUnderTest = new Class[]{ + ClassWithFailedBeforeClassMethodAndBeforeGroupsAfterClassAfterGroups.class + }; TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); @@ -88,83 +89,74 @@ public void confFailureTestInvolvingGroups() { testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE); testng.setGroups("group1"); testng.run(); + verify(tla, 1, 3, 1); } @Test - public void commandLineTest_policyAsSkip() { - String[] argv = - new String[] { - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-configfailurepolicy", - "skip", - "-testclass", - ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName() - }; - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + public void testPolicyAsSkip() { + Class[] classesUnderTest = new Class[]{ClassWithFailedBeforeMethodAndMultipleTests.class}; + TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setTestClasses(classesUnderTest); + testng.addListener(tla); + testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.SKIP); + testng.setVerbose(0); + testng.run(); verify(tla, 1, 1, 2); } @Test - public void commandLineTest_policyAsContinue() { - String[] argv = - new String[] { - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-configfailurepolicy", - "continue", - "-testclass", - ClassWithFailedBeforeMethodAndMultipleTests.class.getCanonicalName() - }; - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + public void testPolicyAsContinue() { + Class[] classesUnderTest = new Class[]{ClassWithFailedBeforeMethodAndMultipleTests.class}; + TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setTestClasses(classesUnderTest); + testng.addListener(tla); + testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE); + testng.setVerbose(0); + testng.run(); verify(tla, 2, 0, 2); } @Test - public void commandLineTestWithXMLFile_policyAsSkip() { - String[] argv = - new String[] { - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-configfailurepolicy", - "skip", - getPathToResource("testng-configfailure.xml") - }; - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + public void testPolicyAsSkipWithXMLFile() { + List suitesUnderTest = Collections.singletonList( + getPathToResource("testng-configfailure.xml") + ); + TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.setTestSuites(suitesUnderTest); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.addListener(tla); + testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.SKIP); + testng.setVerbose(0); + testng.run(); verify(tla, 1, 1, 2); } @Test - public void commandLineTestWithXMLFile_policyAsContinue() { - String[] argv = - new String[] { - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-configfailurepolicy", - "continue", - getPathToResource("testng-configfailure.xml") - }; - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); + public void testPolicyAsContinueWithXMLFile() { + List suitesUnderTest = Collections.singletonList( + getPathToResource("testng-configfailure.xml") + ); + TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.setTestSuites(suitesUnderTest); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.addListener(tla); + testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE); + testng.setVerbose(0); + testng.run(); verify(tla, 2, 0, 2); } diff --git a/testng-core/src/test/java/test/github1417/YetAnotherTestClassSample.java b/testng-core/src/test/java/test/github1417/YetAnotherTestClassSample.java index 81def3d872..52a16773de 100644 --- a/testng-core/src/test/java/test/github1417/YetAnotherTestClassSample.java +++ b/testng-core/src/test/java/test/github1417/YetAnotherTestClassSample.java @@ -1,6 +1,6 @@ package test.github1417; -import com.beust.jcommander.internal.Lists; +import java.util.ArrayList; import java.util.List; import org.testng.Assert; import org.testng.annotations.AfterClass; @@ -11,7 +11,7 @@ public class YetAnotherTestClassSample { private static YetAnotherTestClassSample instance; - private List browsers = Lists.newArrayList(); + private List browsers = new ArrayList<>(); public YetAnotherTestClassSample() { setInstance(this); diff --git a/testng-core/src/test/java/test/junitreports/JUnitReportsTest.java b/testng-core/src/test/java/test/junitreports/JUnitReportsTest.java index 1b29639817..e0ace8a1e1 100644 --- a/testng-core/src/test/java/test/junitreports/JUnitReportsTest.java +++ b/testng-core/src/test/java/test/junitreports/JUnitReportsTest.java @@ -5,7 +5,6 @@ import static org.testng.Assert.*; import static test.junitreports.TestClassContainerForGithubIssue1265.*; -import com.beust.jcommander.internal.Lists; import java.io.File; import java.io.IOException; import java.nio.file.Path; @@ -109,7 +108,7 @@ public void testTestCaseOrderingInJUnitReportReporterWhenPrioritiesDefined() thr add("testRoles004_Post"); } }; - List actual = Lists.newLinkedList(); + List actual = new LinkedList<>(); for (Testcase testcase : suite.getTestcase()) { actual.add(testcase.getName().trim()); } diff --git a/testng-core/src/test/java/test/methodselectors/CommandLineTest.java b/testng-core/src/test/java/test/methodselectors/CommandLineTest.java index 5815316ffa..cc92d3ef78 100644 --- a/testng-core/src/test/java/test/methodselectors/CommandLineTest.java +++ b/testng-core/src/test/java/test/methodselectors/CommandLineTest.java @@ -1,112 +1,30 @@ package test.methodselectors; +import java.util.Collections; import java.util.List; import org.assertj.core.api.Assertions; -import org.testng.CliTestNgRunner; import org.testng.ITestResult; -import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; -import org.testng.annotations.BeforeMethod; +import org.testng.TestNG; import org.testng.annotations.Test; import test.SimpleBaseTest; import testhelper.OutputDirectoryPatch; public class CommandLineTest extends SimpleBaseTest { - private final String[] ARG_WITHOUT_CLASSES = - new String[] { - "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(), "-methodselectors", "", "" - }; - - private final String[] ARG_WITH_GROUPS = - new String[] { - "-log", "0", - "-d", OutputDirectoryPatch.getOutputDirectory(), - "-testclass", "test.methodselectors.SampleTest", - "-methodselectors", "", - "-groups", "" - }; - - private final String[] ARG_WITHOUT_GROUPS = - new String[] { - "-log", "0", - "-d", OutputDirectoryPatch.getOutputDirectory(), - "-testclass", "test.methodselectors.SampleTest", - "-methodselectors", "", - }; - - private TestListenerAdapter tla; - - @BeforeMethod - public void setup() { - tla = new TestListenerAdapter(); - } - - @Test - public void commandLineNegativePriorityAllGroups() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - ARG_WITHOUT_GROUPS[7] = "test.methodselectors.AllTestsMethodSelector:-1"; - CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITHOUT_GROUPS, tla); - String[] passed = {"test1", "test2", "test3"}; - String[] failed = {}; - verifyTests("Passed", passed, tla.getPassedTests()); - verifyTests("Failed", failed, tla.getFailedTests()); - } - - @Test - public void commandLineNegativePriorityGroup2() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - ARG_WITHOUT_GROUPS[7] = "test.methodselectors.Test2MethodSelector:-1"; - CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITHOUT_GROUPS, tla); - String[] passed = {"test2"}; - String[] failed = {}; - verifyTests("Passed", passed, tla.getPassedTests()); - verifyTests("Failed", failed, tla.getFailedTests()); - } - - @Test - public void commandLineLessThanPriorityTest1Test() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - ARG_WITH_GROUPS[7] = "test.methodselectors.Test2MethodSelector:5"; - ARG_WITH_GROUPS[9] = "test1"; - CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITH_GROUPS, tla); - String[] passed = {"test1", "test2"}; - String[] failed = {}; - verifyTests("Passed", passed, tla.getPassedTests()); - verifyTests("Failed", failed, tla.getFailedTests()); - } - @Test - public void commandLineGreaterThanPriorityTest1Test2() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - ARG_WITH_GROUPS[7] = "test.methodselectors.Test2MethodSelector:15"; - ARG_WITH_GROUPS[9] = "test1"; - CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITH_GROUPS, tla); - String[] passed = {"test2"}; - String[] failed = {}; - verifyTests("Passed", passed, tla.getPassedTests()); - verifyTests("Failed", failed, tla.getFailedTests()); - } + public void multipleSelectors() { + TestListenerAdapter tla = new TestListenerAdapter(); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setTestClasses(new Class[]{test.methodselectors.SampleTest.class}); + testng.addMethodSelector("test.methodselectors.NoTestSelector", 7); + testng.addMethodSelector("test.methodselectors.Test2MethodSelector", 5); + testng.setGroups("test1"); + testng.run(); - @Test - public void commandLineLessThanPriorityAllTests() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - ARG_WITH_GROUPS[7] = "test.methodselectors.AllTestsMethodSelector:5"; - ARG_WITH_GROUPS[9] = "test1"; - CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITH_GROUPS, tla); - String[] passed = {"test1", "test2", "test3"}; - String[] failed = {}; - verifyTests("Passed", passed, tla.getPassedTests()); - verifyTests("Failed", failed, tla.getFailedTests()); - } - - @Test - public void commandLineMultipleSelectors() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - ARG_WITH_GROUPS[7] = - "test.methodselectors.NoTestSelector:7,test.methodselectors.Test2MethodSelector:5"; - ARG_WITH_GROUPS[9] = "test1"; - CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITH_GROUPS, tla); String[] passed = {"test1", "test2"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -115,9 +33,15 @@ public void commandLineMultipleSelectors() { @Test public void commandLineNoTest1Selector() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - ARG_WITHOUT_GROUPS[7] = "test.methodselectors.NoTest1MethodSelector:5"; - CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITHOUT_GROUPS, tla); + TestListenerAdapter tla = new TestListenerAdapter(); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setTestClasses(new Class[]{test.methodselectors.SampleTest.class}); + testng.addMethodSelector("test.methodselectors.NoTest1MethodSelector", 5); + testng.run(); + String[] passed = {"test2", "test3"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -126,10 +50,17 @@ public void commandLineNoTest1Selector() { @Test public void commandLineTestWithXmlFile() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - ARG_WITHOUT_CLASSES[5] = "test.methodselectors.NoTest1MethodSelector:5"; - ARG_WITHOUT_CLASSES[6] = getPathToResource("testng-methodselectors.xml"); - CliTestNgRunner.Main.privateMain(cliRunner, ARG_WITHOUT_CLASSES, tla); + TestListenerAdapter tla = new TestListenerAdapter(); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.addMethodSelector("test.methodselectors.NoTest1MethodSelector", 5); + testng.setTestSuites(Collections.singletonList( + getPathToResource("testng-methodselectors.xml") + )); + testng.run(); + String[] passed = {"test2", "test3"}; String[] failed = {}; verifyTests("Passed", passed, tla.getPassedTests()); @@ -138,20 +69,17 @@ public void commandLineTestWithXmlFile() { @Test(description = "GITHUB-2407") public void testOverrideExcludedMethodsCommandLineExclusions() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - String[] args = - new String[] { - "src/test/resources/test/methodselectors/sampleTest.xml", - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-excludegroups", - "test1", - "-overrideincludedmethods" - }; - - CliTestNgRunner.Main.privateMain(cliRunner, args, tla); + TestListenerAdapter tla = new TestListenerAdapter(); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setTestSuites(Collections.singletonList( + getPathToResource("test/methodselectors/sampleTest.xml") + )); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setExcludedGroups("test1"); + testng.setOverrideIncludedMethods(true); + testng.run(); // test1 is excluded, so only test2 is left in the passed list String[] passed = {"test2"}; @@ -162,18 +90,16 @@ public void testOverrideExcludedMethodsCommandLineExclusions() { @Test(description = "GITHUB-2407") public void testOverrideExcludedMethodsSuiteExclusions() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - String[] args = - new String[] { - "src/test/resources/test/methodselectors/sampleTestExclusions.xml", - "-log", - "0", - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-overrideincludedmethods" - }; - - CliTestNgRunner.Main.privateMain(cliRunner, args, tla); + TestListenerAdapter tla = new TestListenerAdapter(); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setTestSuites(Collections.singletonList( + getPathToResource("test/methodselectors/sampleTestExclusions.xm") + )); + testng.setVerbose(0); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setOverrideIncludedMethods(true); + testng.run(); String[] passed = {}; String[] failed = {}; diff --git a/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java b/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java index e2d62e5a87..7d4f3e2782 100644 --- a/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java +++ b/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java @@ -3,10 +3,7 @@ import java.util.Collections; import java.util.List; import org.testng.Assert; -import org.testng.CliTestNgRunner; -import org.testng.ITestNGListener; import org.testng.ITestResult; -import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.BeforeMethod; @@ -42,7 +39,7 @@ public void programmaticXmlSuite() { XmlClass testClass = new XmlClass(test.methodselectors.SampleTest.class); test.setXmlClasses(Collections.singletonList(testClass)); tng.setXmlSuites(Collections.singletonList(suite)); - tng.addListener((ITestNGListener) m_tla); + tng.addListener( m_tla); tng.run(); validate(new String[] {"test2"}); @@ -51,27 +48,14 @@ public void programmaticXmlSuite() { @Test public void xmlXmlSuite() { TestNG tng = create(); + tng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); tng.setTestSuites(Collections.singletonList(getPathToResource("methodselector-in-xml.xml"))); - tng.addListener((ITestNGListener) m_tla); + tng.addListener(m_tla); tng.run(); validate(new String[] {"test2"}); } - @Test - public void fileOnCommandLine() { - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); - String[] args = - new String[] { - "-d", - OutputDirectoryPatch.getOutputDirectory(), - getPathToResource("methodselector-in-xml.xml") - }; - CliTestNgRunner.Main.privateMain(cliRunner, args, m_tla); - - validate(new String[] {"test2"}); - } - private void validate(String[] expectPassed) { List passed = m_tla.getPassedTests(); Assert.assertEquals(passed.size(), expectPassed.length); diff --git a/testng-core/src/test/java/test/mixed/MixedTest.java b/testng-core/src/test/java/test/mixed/MixedTest.java index 11e9a53e07..05a62ebfdc 100644 --- a/testng-core/src/test/java/test/mixed/MixedTest.java +++ b/testng-core/src/test/java/test/mixed/MixedTest.java @@ -1,33 +1,32 @@ package test.mixed; import org.testng.Assert; -import org.testng.CliTestNgRunner; -import org.testng.JCommanderCliTestNgRunner; import org.testng.TestListenerAdapter; +import org.testng.TestNG; import org.testng.annotations.Test; import test.BaseTest; import testhelper.OutputDirectoryPatch; +import java.util.Arrays; -/** @author lukas */ public class MixedTest extends BaseTest { + @Test public void mixedWithExcludedGroups() { - String[] argv = { - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-log", - "0", - "-mixed", - "-groups", - "unit", - "-excludegroups", - "ignore", - "-testclass", - "test.mixed.JUnit3Test1,test.mixed.JUnit4Test1,test.mixed.TestNGTest1,test.mixed.TestNGGroups" - }; - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setVerbose(0); + testng.setMixed(true); + testng.setGroups("unit"); + testng.setExcludedGroups("ignore"); + testng.setTestClasses(new Class[]{ + test.mixed.JUnit3Test1.class, + test.mixed.JUnit4Test1.class, + test.mixed.TestNGTest1.class, + test.mixed.TestNGGroups.class, + }); + testng.run(); Assert.assertEquals( tla.getPassedTests().size(), @@ -38,18 +37,18 @@ public void mixedWithExcludedGroups() { @Test public void mixedClasses() { - String[] argv = { - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-log", - "0", - "-mixed", - "-testclass", - "test.mixed.JUnit3Test1,test.mixed.JUnit4Test1,test.mixed.TestNGTest1" - }; - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setVerbose(0); + testng.setMixed(true); + testng.setTestClasses(new Class[]{ + test.mixed.JUnit3Test1.class, + test.mixed.JUnit4Test1.class, + test.mixed.TestNGTest1.class, + }); + testng.run(); Assert.assertEquals(tla.getPassedTests().size(), 6); Assert.assertEquals(tla.getFailedTests().size(), 0); @@ -57,18 +56,18 @@ public void mixedClasses() { @Test public void mixedMethods() { - String[] argv = { - "-d", - OutputDirectoryPatch.getOutputDirectory(), - "-mixed", - "-log", - "0", - "-methods", - "test.mixed.JUnit3Test1.testB,test.mixed.JUnit4Test1.atest,test.mixed.TestNGTest1.tngCustomTest1" - }; - CliTestNgRunner cliRunner = new JCommanderCliTestNgRunner(); TestListenerAdapter tla = new TestListenerAdapter(); - CliTestNgRunner.Main.privateMain(cliRunner, argv, tla); + TestNG testng = new TestNG(); + testng.addListener(tla); + testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); + testng.setVerbose(0); + testng.setCommandLineMethods(Arrays.asList( + "test.mixed.JUnit3Test1.test", + "test.mixed.JUnit3Test1.testB", + "test.mixed.JUnit4Test1.atest", + "test.mixed.TestNGTest1.tngCustomTest1" + )); + testng.run(); Assert.assertEquals(tla.getPassedTests().size(), 3); Assert.assertEquals(tla.getFailedTests().size(), 0); diff --git a/testng-core/src/test/java/test/testng1231/TestExecutionListenerInvocationOrder.java b/testng-core/src/test/java/test/testng1231/TestExecutionListenerInvocationOrder.java index 74e733eb6a..861d7a4b10 100644 --- a/testng-core/src/test/java/test/testng1231/TestExecutionListenerInvocationOrder.java +++ b/testng-core/src/test/java/test/testng1231/TestExecutionListenerInvocationOrder.java @@ -1,6 +1,5 @@ package test.testng1231; -import com.beust.jcommander.internal.Lists; import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -26,7 +25,7 @@ public void testListenerOrder() { public static class TestListenerFor1231 implements IExecutionListener, IAlterSuiteListener, IReporter, ISuiteListener { - public static LinkedList order = Lists.newLinkedList(); + public static LinkedList order = new LinkedList<>(); @Override public void onExecutionStart() { diff --git a/testng-jcommander/build.gradle.kts b/testng-jcommander/build.gradle.kts new file mode 100644 index 0000000000..d3e7203b01 --- /dev/null +++ b/testng-jcommander/build.gradle.kts @@ -0,0 +1,10 @@ +plugins { + id("testng.java-library") +} + +dependencies { + api("com.beust:jcommander:_") + + implementation(projects.testngCore) + testImplementation(projects.testngAsserts) +} diff --git a/testng-core/src/main/java/org/testng/Converter.java b/testng-jcommander/src/main/java/org/testng/Converter.java similarity index 100% rename from testng-core/src/main/java/org/testng/Converter.java rename to testng-jcommander/src/main/java/org/testng/Converter.java diff --git a/testng-core/src/main/java/org/testng/JCommanderCliTestNgRunner.java b/testng-jcommander/src/main/java/org/testng/JCommanderCliTestNgRunner.java similarity index 100% rename from testng-core/src/main/java/org/testng/JCommanderCliTestNgRunner.java rename to testng-jcommander/src/main/java/org/testng/JCommanderCliTestNgRunner.java diff --git a/testng-core/src/main/java/org/testng/JCommanderCommandLineArgs.java b/testng-jcommander/src/main/java/org/testng/JCommanderCommandLineArgs.java similarity index 78% rename from testng-core/src/main/java/org/testng/JCommanderCommandLineArgs.java rename to testng-jcommander/src/main/java/org/testng/JCommanderCommandLineArgs.java index 6a0b972086..c2a7a50cba 100644 --- a/testng-core/src/main/java/org/testng/JCommanderCommandLineArgs.java +++ b/testng-jcommander/src/main/java/org/testng/JCommanderCommandLineArgs.java @@ -8,13 +8,9 @@ import org.testng.internal.ClassHelper; import org.testng.internal.ReporterConfig; import org.testng.internal.Utils; -import org.testng.log4testng.Logger; -import org.testng.xml.XmlMethodSelector; import org.testng.xml.XmlSuite; -public class JCommanderCommandLineArgs implements CommandLineArgs { - - private static final Logger LOGGER = Logger.getLogger(TestNG.class); +public class JCommanderCommandLineArgs extends AbstractCommandLineArgs { @Parameter(description = "The XML suite files to run") private List suiteFiles = Lists.newArrayList(); @@ -214,7 +210,7 @@ public Boolean isMixed() { } @Override - public List> getListener() { + protected String[] getListenerValues() { if (listener == null) { return null; } @@ -222,50 +218,17 @@ public List> getListener() { if (listener.contains(",")) { sep = ","; } - String[] strs = Utils.split(listener, sep); - List> classes = Lists.newArrayList(); - - for (String cls : strs) { - Class clazz = ClassHelper.fileToClass(cls); - if (ITestNGListener.class.isAssignableFrom(clazz)) { - classes.add((Class) clazz); - } - } - - return classes; + return Utils.split(listener, sep); } @Override - public List getMethodSelectors() { - if (methodSelectors == null) { - return null; - } - String[] strs = Utils.split(methodSelectors, ","); - List selectors = new ArrayList<>(strs.length); - for (String cls : strs) { - String[] sel = Utils.split(cls, ":"); - try { - if (sel.length == 2) { - XmlMethodSelector selector = new XmlMethodSelector(); - selector.setName(sel[0]); - selector.setPriority(Integer.parseInt(sel[1])); - selectors.add(selector); - } else { - LOGGER.error("Method selector value was not in the format org.example.Selector:4"); - } - } catch (NumberFormatException nfe) { - LOGGER.error("Method selector value was not in the format org.example.Selector:4"); - } - } - return selectors; + protected String getMethodSelectorsValue() { + return methodSelectors; } @Override - public Class getObjectFactory() { - if (objectFactory == null) { - return null; - } - return (Class) ClassHelper.fileToClass(objectFactory); + protected String getObjectFactoryValue() { + return objectFactory; } @Override @@ -317,25 +280,13 @@ public Boolean skipFailedInvocationCounts() { } @Override - public List> getTestClass() { - if (testClass == null) { - return null; - } - String[] strClasses = testClass.split(","); - List> classes = Lists.newArrayList(); - for (String c : strClasses) { - classes.add(ClassHelper.fileToClass(c)); - } - - return classes; + protected String getTestClassValue() { + return testClass; } @Override - public List getTestNames() { - if (testNames == null) { - return null; - } - return Arrays.asList(testNames.split(",")); + protected String getTestNamesValue() { + return testNames; } @Override @@ -349,11 +300,8 @@ public String getXmlPathInJar() { } @Override - public Class getTestRunnerFactory() { - if (testRunnerFactory == null) { - return null; - } - return (Class) ClassHelper.fileToClass(testRunnerFactory); + protected String getTestRunnerFactoryValue() { + return testRunnerFactory; } @Override @@ -410,8 +358,8 @@ public Boolean failIfAllTestsSkipped() { } @Override - public List getSpiListenersToSkip() { - return Arrays.asList(spiListenersToSkip.split(",")); + protected String getSpiListenersToSkipValue() { + return spiListenersToSkip; } @Override diff --git a/testng-jcommander/src/main/resources/META-INF/services/org.testng.CliTestNgRunner b/testng-jcommander/src/main/resources/META-INF/services/org.testng.CliTestNgRunner new file mode 100644 index 0000000000..e2079b03f8 --- /dev/null +++ b/testng-jcommander/src/main/resources/META-INF/services/org.testng.CliTestNgRunner @@ -0,0 +1 @@ +org.testng.JCommanderCliTestNgRunner From 4af59642394b6c8d6689830a00de4e0b14920071 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Fri, 16 Jul 2021 22:39:15 +0200 Subject: [PATCH 3/8] Update CHANGES --- CHANGES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.txt b/CHANGES.txt index 26e4b3c5d2..53e3bc7d98 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ Current +New: Provide an SPI for 3rd party CLI integration (Julien Herr) Fixed: GITHUB-849: Performance improvement by fixing hashCode (testn & Vladimir Sitnikov) Fixed: GITHUB-2570: Use Guice injector for instantiate IRetryAnalyzer (Krishnan Mahadevan) Fix: use proper instances for beforeClass callback when different instances collide on hash codes From 7f4105c1ca8315613fc77a9a71f144d5ca7804af Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Fri, 16 Jul 2021 22:45:32 +0200 Subject: [PATCH 4/8] Add jcommander module into bom --- testng-bom/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/testng-bom/build.gradle.kts b/testng-bom/build.gradle.kts index 404bd17f22..7d8fc84250 100644 --- a/testng-bom/build.gradle.kts +++ b/testng-bom/build.gradle.kts @@ -12,6 +12,7 @@ dependencies { api(projects.testngCollections) api(projects.testngCoreApi) api(projects.testngCore) + api(projects.testngJcommander) api(projects.testngReflectionUtils) api(projects.testngRunnerApi) api(projects.testngRunnerJunit4) From fd54977ce13f75181e61dcb81cf4136556c49146 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Sat, 17 Jul 2021 14:35:37 +0200 Subject: [PATCH 5/8] Autostyle fix --- .../org/testng/AbstractCommandLineArgs.java | 6 ++-- .../main/java/org/testng/CliTestNgRunner.java | 4 +-- .../src/test/java/test/CommandLineTest.java | 15 ++++---- .../FailurePolicyTest.java | 19 +++++----- .../test/methodselectors/CommandLineTest.java | 20 +++++------ .../MethodSelectorInSuiteTest.java | 2 +- .../src/test/java/test/mixed/MixedTest.java | 36 +++++++++---------- .../org/testng/JCommanderCommandLineArgs.java | 1 - 8 files changed, 48 insertions(+), 55 deletions(-) diff --git a/testng-core/src/main/java/org/testng/AbstractCommandLineArgs.java b/testng-core/src/main/java/org/testng/AbstractCommandLineArgs.java index 3708d3e81a..ea835d6795 100644 --- a/testng-core/src/main/java/org/testng/AbstractCommandLineArgs.java +++ b/testng-core/src/main/java/org/testng/AbstractCommandLineArgs.java @@ -1,13 +1,13 @@ package org.testng; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import org.testng.collections.Lists; import org.testng.internal.ClassHelper; import org.testng.internal.Utils; import org.testng.log4testng.Logger; import org.testng.xml.XmlMethodSelector; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; public abstract class AbstractCommandLineArgs implements CommandLineArgs { diff --git a/testng-core/src/main/java/org/testng/CliTestNgRunner.java b/testng-core/src/main/java/org/testng/CliTestNgRunner.java index d362f6c662..c4f708b9e1 100644 --- a/testng-core/src/main/java/org/testng/CliTestNgRunner.java +++ b/testng-core/src/main/java/org/testng/CliTestNgRunner.java @@ -1,10 +1,10 @@ package org.testng; +import java.util.List; +import java.util.ServiceLoader; import org.testng.collections.Lists; import org.testng.internal.ExitCode; import org.testng.log4testng.Logger; -import java.util.List; -import java.util.ServiceLoader; public interface CliTestNgRunner { diff --git a/testng-core/src/test/java/test/CommandLineTest.java b/testng-core/src/test/java/test/CommandLineTest.java index 205fa29eaa..8debf8bea8 100644 --- a/testng-core/src/test/java/test/CommandLineTest.java +++ b/testng-core/src/test/java/test/CommandLineTest.java @@ -34,7 +34,7 @@ public void junitParsing() { testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setJUnit(true); - testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.setTestClasses(new Class[] {test.sample.JUnitSample1.class}); testng.run(); List passed = tla.getPassedTests(); @@ -55,7 +55,7 @@ public void junitParsing2() { testng.addListener(tla); testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); - testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.setTestClasses(new Class[] {test.sample.JUnitSample1.class}); testng.run(); List passed = tla.getPassedTests(); @@ -73,7 +73,7 @@ public void suiteNameOverride() { testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setJUnit(true); - testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.setTestClasses(new Class[] {test.sample.JUnitSample1.class}); testng.setDefaultSuiteName(suiteName); testng.run(); @@ -95,7 +95,7 @@ public void testNameOverride() { testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setJUnit(true); - testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.setTestClasses(new Class[] {test.sample.JUnitSample1.class}); testng.setDefaultTestName(testName); testng.run(); @@ -111,7 +111,7 @@ public void testUseDefaultListenersArgument() { TestNG testng = new TestNG(); testng.setVerbose(0); testng.setUseDefaultListeners(false); - testng.setTestClasses(new Class[]{test.sample.JUnitSample1.class}); + testng.setTestClasses(new Class[] {test.sample.JUnitSample1.class}); testng.run(); } @@ -122,9 +122,8 @@ public void testMethodParameter() { testng.addListener(tla); testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); - testng.setCommandLineMethods(Collections.singletonList( - "test.sample.Sample2.method1,test.sample.Sample2.method3" - )); + testng.setCommandLineMethods( + Collections.singletonList("test.sample.Sample2.method1,test.sample.Sample2.method3")); testng.run(); List passed = tla.getPassedTests(); diff --git a/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java b/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java index 112a095903..6fd96cec3f 100644 --- a/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java +++ b/testng-core/src/test/java/test/configurationfailurepolicy/FailurePolicyTest.java @@ -77,9 +77,8 @@ public void confFailureTest( @Test public void confFailureTestInvolvingGroups() { - Class[] classesUnderTest = new Class[]{ - ClassWithFailedBeforeClassMethodAndBeforeGroupsAfterClassAfterGroups.class - }; + Class[] classesUnderTest = + new Class[] {ClassWithFailedBeforeClassMethodAndBeforeGroupsAfterClassAfterGroups.class}; TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); @@ -95,7 +94,7 @@ public void confFailureTestInvolvingGroups() { @Test public void testPolicyAsSkip() { - Class[] classesUnderTest = new Class[]{ClassWithFailedBeforeMethodAndMultipleTests.class}; + Class[] classesUnderTest = new Class[] {ClassWithFailedBeforeMethodAndMultipleTests.class}; TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); @@ -111,7 +110,7 @@ public void testPolicyAsSkip() { @Test public void testPolicyAsContinue() { - Class[] classesUnderTest = new Class[]{ClassWithFailedBeforeMethodAndMultipleTests.class}; + Class[] classesUnderTest = new Class[] {ClassWithFailedBeforeMethodAndMultipleTests.class}; TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); @@ -127,9 +126,8 @@ public void testPolicyAsContinue() { @Test public void testPolicyAsSkipWithXMLFile() { - List suitesUnderTest = Collections.singletonList( - getPathToResource("testng-configfailure.xml") - ); + List suitesUnderTest = + Collections.singletonList(getPathToResource("testng-configfailure.xml")); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); @@ -145,9 +143,8 @@ public void testPolicyAsSkipWithXMLFile() { @Test public void testPolicyAsContinueWithXMLFile() { - List suitesUnderTest = Collections.singletonList( - getPathToResource("testng-configfailure.xml") - ); + List suitesUnderTest = + Collections.singletonList(getPathToResource("testng-configfailure.xml")); TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); diff --git a/testng-core/src/test/java/test/methodselectors/CommandLineTest.java b/testng-core/src/test/java/test/methodselectors/CommandLineTest.java index cc92d3ef78..983e095574 100644 --- a/testng-core/src/test/java/test/methodselectors/CommandLineTest.java +++ b/testng-core/src/test/java/test/methodselectors/CommandLineTest.java @@ -19,7 +19,7 @@ public void multipleSelectors() { testng.addListener(tla); testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); - testng.setTestClasses(new Class[]{test.methodselectors.SampleTest.class}); + testng.setTestClasses(new Class[] {test.methodselectors.SampleTest.class}); testng.addMethodSelector("test.methodselectors.NoTestSelector", 7); testng.addMethodSelector("test.methodselectors.Test2MethodSelector", 5); testng.setGroups("test1"); @@ -38,7 +38,7 @@ public void commandLineNoTest1Selector() { testng.addListener(tla); testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); - testng.setTestClasses(new Class[]{test.methodselectors.SampleTest.class}); + testng.setTestClasses(new Class[] {test.methodselectors.SampleTest.class}); testng.addMethodSelector("test.methodselectors.NoTest1MethodSelector", 5); testng.run(); @@ -56,9 +56,8 @@ public void commandLineTestWithXmlFile() { testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.addMethodSelector("test.methodselectors.NoTest1MethodSelector", 5); - testng.setTestSuites(Collections.singletonList( - getPathToResource("testng-methodselectors.xml") - )); + testng.setTestSuites( + Collections.singletonList(getPathToResource("testng-methodselectors.xml"))); testng.run(); String[] passed = {"test2", "test3"}; @@ -72,9 +71,8 @@ public void testOverrideExcludedMethodsCommandLineExclusions() { TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); testng.addListener(tla); - testng.setTestSuites(Collections.singletonList( - getPathToResource("test/methodselectors/sampleTest.xml") - )); + testng.setTestSuites( + Collections.singletonList(getPathToResource("test/methodselectors/sampleTest.xml"))); testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setExcludedGroups("test1"); @@ -93,9 +91,9 @@ public void testOverrideExcludedMethodsSuiteExclusions() { TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); testng.addListener(tla); - testng.setTestSuites(Collections.singletonList( - getPathToResource("test/methodselectors/sampleTestExclusions.xm") - )); + testng.setTestSuites( + Collections.singletonList( + getPathToResource("test/methodselectors/sampleTestExclusions.xm"))); testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setOverrideIncludedMethods(true); diff --git a/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java b/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java index 7d4f3e2782..12b9ca9c31 100644 --- a/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java +++ b/testng-core/src/test/java/test/methodselectors/MethodSelectorInSuiteTest.java @@ -39,7 +39,7 @@ public void programmaticXmlSuite() { XmlClass testClass = new XmlClass(test.methodselectors.SampleTest.class); test.setXmlClasses(Collections.singletonList(testClass)); tng.setXmlSuites(Collections.singletonList(suite)); - tng.addListener( m_tla); + tng.addListener(m_tla); tng.run(); validate(new String[] {"test2"}); diff --git a/testng-core/src/test/java/test/mixed/MixedTest.java b/testng-core/src/test/java/test/mixed/MixedTest.java index 05a62ebfdc..f55a795848 100644 --- a/testng-core/src/test/java/test/mixed/MixedTest.java +++ b/testng-core/src/test/java/test/mixed/MixedTest.java @@ -1,12 +1,12 @@ package test.mixed; +import java.util.Arrays; import org.testng.Assert; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.annotations.Test; import test.BaseTest; import testhelper.OutputDirectoryPatch; -import java.util.Arrays; public class MixedTest extends BaseTest { @@ -20,12 +20,13 @@ public void mixedWithExcludedGroups() { testng.setMixed(true); testng.setGroups("unit"); testng.setExcludedGroups("ignore"); - testng.setTestClasses(new Class[]{ - test.mixed.JUnit3Test1.class, - test.mixed.JUnit4Test1.class, - test.mixed.TestNGTest1.class, - test.mixed.TestNGGroups.class, - }); + testng.setTestClasses( + new Class[] { + test.mixed.JUnit3Test1.class, + test.mixed.JUnit4Test1.class, + test.mixed.TestNGTest1.class, + test.mixed.TestNGGroups.class, + }); testng.run(); Assert.assertEquals( @@ -43,11 +44,10 @@ public void mixedClasses() { testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setVerbose(0); testng.setMixed(true); - testng.setTestClasses(new Class[]{ - test.mixed.JUnit3Test1.class, - test.mixed.JUnit4Test1.class, - test.mixed.TestNGTest1.class, - }); + testng.setTestClasses( + new Class[] { + test.mixed.JUnit3Test1.class, test.mixed.JUnit4Test1.class, test.mixed.TestNGTest1.class, + }); testng.run(); Assert.assertEquals(tla.getPassedTests().size(), 6); @@ -61,12 +61,12 @@ public void mixedMethods() { testng.addListener(tla); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setVerbose(0); - testng.setCommandLineMethods(Arrays.asList( - "test.mixed.JUnit3Test1.test", - "test.mixed.JUnit3Test1.testB", - "test.mixed.JUnit4Test1.atest", - "test.mixed.TestNGTest1.tngCustomTest1" - )); + testng.setCommandLineMethods( + Arrays.asList( + "test.mixed.JUnit3Test1.test", + "test.mixed.JUnit3Test1.testB", + "test.mixed.JUnit4Test1.atest", + "test.mixed.TestNGTest1.tngCustomTest1")); testng.run(); Assert.assertEquals(tla.getPassedTests().size(), 3); diff --git a/testng-jcommander/src/main/java/org/testng/JCommanderCommandLineArgs.java b/testng-jcommander/src/main/java/org/testng/JCommanderCommandLineArgs.java index c2a7a50cba..b5e297ed67 100644 --- a/testng-jcommander/src/main/java/org/testng/JCommanderCommandLineArgs.java +++ b/testng-jcommander/src/main/java/org/testng/JCommanderCommandLineArgs.java @@ -2,7 +2,6 @@ import com.beust.jcommander.Parameter; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.testng.collections.Lists; import org.testng.internal.ClassHelper; From 6feb083bb86b528300c7d14e259fadacca6ceb69 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Sat, 24 Jul 2021 22:56:13 +0200 Subject: [PATCH 6/8] Fix tests --- testng-core/build.gradle.kts | 1 + .../main/java/org/testng/CommandLineArgs.java | 15 ++-- .../org/testng/SurefireCommandLineArgs.java | 3 + .../src/test/java/test/CommandLineTest.java | 3 +- .../java/test/groups/issue2232/IssueTest.java | 34 -------- .../src/test/java/test/mixed/MixedTest.java | 1 + .../test/reports/EmailableReporterTest.java | 74 +---------------- testng-jcommander/build.gradle.kts | 1 + .../java/test/groups/issue2232/IssueTest.java | 76 +++++++++++++++++ .../test/reports/EmailableReporterTest.java | 82 +++++++++++++++++++ testng-test-kit/build.gradle.kts | 6 ++ .../java/test/InvokedMethodNameListener.java | 6 +- .../src/main/java/test/MySecurityManager.java | 22 +++++ .../src/main}/java/test/SimpleBaseTest.java | 0 14 files changed, 208 insertions(+), 116 deletions(-) create mode 100644 testng-jcommander/src/test/java/test/groups/issue2232/IssueTest.java create mode 100644 testng-jcommander/src/test/java/test/reports/EmailableReporterTest.java rename {testng-core/src/test => testng-test-kit/src/main}/java/test/InvokedMethodNameListener.java (96%) create mode 100644 testng-test-kit/src/main/java/test/MySecurityManager.java rename {testng-core/src/test => testng-test-kit/src/main}/java/test/SimpleBaseTest.java (100%) diff --git a/testng-core/build.gradle.kts b/testng-core/build.gradle.kts index 46856df48a..782ce8817e 100644 --- a/testng-core/build.gradle.kts +++ b/testng-core/build.gradle.kts @@ -38,6 +38,7 @@ dependencies { implementation("org.webjars:jquery:_") testImplementation(projects.testngAsserts) + testImplementation(projects.testngTestKit) testImplementation("org.codehaus.groovy:groovy-all:_") testImplementation("org.spockframework:spock-core:_") testImplementation("org.apache-extras.beanshell:bsh:_") diff --git a/testng-core/src/main/java/org/testng/CommandLineArgs.java b/testng-core/src/main/java/org/testng/CommandLineArgs.java index bb5c96747d..f3d2c55f83 100644 --- a/testng-core/src/main/java/org/testng/CommandLineArgs.java +++ b/testng-core/src/main/java/org/testng/CommandLineArgs.java @@ -333,17 +333,22 @@ default void configure(TestNG tng) { tng.addReporter(getReporter()); } - if (!getCommandLineMethods().isEmpty()) { + if (getCommandLineMethods() != null && !getCommandLineMethods().isEmpty()) { tng.setCommandLineMethods(getCommandLineMethods()); } if (getSuiteFiles() != null) { tng.setTestSuites(getSuiteFiles()); } - - tng.setSuiteThreadPoolSize(getSuiteThreadPoolSize()); - tng.setRandomizeSuites(isRandomizeSuites()); - tng.alwaysRunListeners(alwaysRunListeners()); + if (getSuiteThreadPoolSize() != null) { + tng.setSuiteThreadPoolSize(getSuiteThreadPoolSize()); + } + if (isRandomizeSuites() != null) { + tng.setRandomizeSuites(isRandomizeSuites()); + } + if (alwaysRunListeners() != null) { + tng.alwaysRunListeners(alwaysRunListeners()); + } } class ParameterException extends Exception { diff --git a/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java b/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java index 6b7b76002c..8907225e53 100644 --- a/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java +++ b/testng-core/src/main/java/org/testng/SurefireCommandLineArgs.java @@ -56,6 +56,9 @@ public Boolean isMixed() { @Override protected String[] getListenerValues() { Object listeners = cmdLineArgs.get(CommandLineArgs.LISTENER); + if (listeners == null) { + return null; + } if (listeners instanceof List) { return ((List) listeners).toArray(new String[0]); } else { diff --git a/testng-core/src/test/java/test/CommandLineTest.java b/testng-core/src/test/java/test/CommandLineTest.java index 8debf8bea8..0a95f931b9 100644 --- a/testng-core/src/test/java/test/CommandLineTest.java +++ b/testng-core/src/test/java/test/CommandLineTest.java @@ -7,6 +7,7 @@ import com.google.inject.Module; import com.google.inject.Stage; import java.lang.reflect.Field; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -123,7 +124,7 @@ public void testMethodParameter() { testng.setVerbose(0); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setCommandLineMethods( - Collections.singletonList("test.sample.Sample2.method1,test.sample.Sample2.method3")); + Arrays.asList("test.sample.Sample2.method1", "test.sample.Sample2.method3")); testng.run(); List passed = tla.getPassedTests(); diff --git a/testng-core/src/test/java/test/groups/issue2232/IssueTest.java b/testng-core/src/test/java/test/groups/issue2232/IssueTest.java index e4c1f026b5..35c6f4f90b 100644 --- a/testng-core/src/test/java/test/groups/issue2232/IssueTest.java +++ b/testng-core/src/test/java/test/groups/issue2232/IssueTest.java @@ -48,38 +48,4 @@ private XmlSuite constructSuite() { xmltest.setPackages(Collections.singletonList(xmlpackage)); return xmlsuite; } - - @Test(invocationCount = 2, description = "GITHUB-2232") - // Ensuring that the bug doesn't surface even when tests are executed via the command line mode - public void commandlineTest() throws IOException, InterruptedException { - Path suitefile = - Files.write( - Files.createTempFile("testng", ".xml"), - constructSuite().toXml().getBytes(Charset.defaultCharset())); - List args = Collections.singletonList(suitefile.toFile().getAbsolutePath()); - int status = exec(Collections.emptyList(), args); - assertThat(status).isEqualTo(0); - } - - private int exec(List jvmArgs, List args) - throws IOException, InterruptedException { - - String javaHome = System.getProperty("java.home"); - String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; - String classpath = System.getProperty("java.class.path"); - String className = TestNG.class.getName(); - List command = new ArrayList<>(); - command.add(javaBin); - command.addAll(jvmArgs); - command.add("-cp"); - command.add(classpath); - command.add(className); - command.addAll(args); - Reporter.log("Executing the command " + command, 2, true); - ProcessBuilder builder = new ProcessBuilder(command); - Process process = builder.inheritIO().start(); - process.waitFor(); - - return process.exitValue(); - } } diff --git a/testng-core/src/test/java/test/mixed/MixedTest.java b/testng-core/src/test/java/test/mixed/MixedTest.java index f55a795848..f402e4b797 100644 --- a/testng-core/src/test/java/test/mixed/MixedTest.java +++ b/testng-core/src/test/java/test/mixed/MixedTest.java @@ -61,6 +61,7 @@ public void mixedMethods() { testng.addListener(tla); testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); testng.setVerbose(0); + testng.setMixed(true); testng.setCommandLineMethods( Arrays.asList( "test.mixed.JUnit3Test1.test", diff --git a/testng-core/src/test/java/test/reports/EmailableReporterTest.java b/testng-core/src/test/java/test/reports/EmailableReporterTest.java index 6e9c288a24..e4041c60f5 100644 --- a/testng-core/src/test/java/test/reports/EmailableReporterTest.java +++ b/testng-core/src/test/java/test/reports/EmailableReporterTest.java @@ -4,8 +4,6 @@ import java.io.File; import java.lang.reflect.Method; -import java.security.Permission; -import org.testng.CliTestNgRunner; import org.testng.IReporter; import org.testng.ITestNGListener; import org.testng.TestNG; @@ -15,6 +13,7 @@ import org.testng.annotations.Test; import org.testng.reporters.EmailableReporter; import org.testng.reporters.EmailableReporter2; +import test.MySecurityManager; import test.SimpleBaseTest; public class EmailableReporterTest extends SimpleBaseTest { @@ -42,17 +41,6 @@ public void testReportsNameCustomizationViaRunMethodInvocation(IReporter reporte runTestViaRunMethod(reporter, null /* no jvm arguments */); } - @Test(dataProvider = "getReporterNames", priority = 3) - public void testReportsNameCustomizationViaMainMethodInvocation(String clazzName) { - runTestViaMainMethod(clazzName, null /* no jvm arguments */); - } - - @Test(dataProvider = "getReporterNames", priority = 4) - public void testReportsNameCustomizationViaMainMethodInvocationAndJVMArguments( - String clazzName, String jvm) { - runTestViaMainMethod(clazzName, jvm); - } - @DataProvider(name = "getReporterInstances") public Object[][] getReporterInstances(Method method) { if (method.getName().toLowerCase().contains("jvmarguments")) { @@ -64,47 +52,6 @@ public Object[][] getReporterInstances(Method method) { return new Object[][] {{new EmailableReporter()}, {new EmailableReporter2()}}; } - @DataProvider(name = "getReporterNames") - public Object[][] getReporterNames(Method method) { - if (method.getName().toLowerCase().contains("jvmarguments")) { - return new Object[][] { - {EmailableReporter.class.getName(), "emailable.report.name"}, - {EmailableReporter2.class.getName(), "emailable.report2.name"} - }; - } - return new Object[][] { - {EmailableReporter.class.getName()}, {EmailableReporter2.class.getName()} - }; - } - - private void runTestViaMainMethod(String clazzName, String jvm) { - String name = Long.toString(System.currentTimeMillis()); - File output = createDirInTempDir(name); - String filename = "report" + name + ".html"; - String[] args = { - "-d", - output.getAbsolutePath(), - "-reporter", - clazzName + ":fileName=" + filename, - "src/test/resources/1332.xml" - }; - try { - if (jvm != null) { - System.setProperty(jvm, filename); - } - CliTestNgRunner.Main.main(args); - } catch (SecurityException t) { - // Gobble Security exception - } finally { - if (jvm != null) { - // reset the jvm arguments - System.setProperty(jvm, ""); - } - } - File actual = new File(output.getAbsolutePath(), filename); - assertThat(actual).exists(); - } - private void runTestViaRunMethod(IReporter reporter, String jvm) { String name = Long.toString(System.currentTimeMillis()); File output = createDirInTempDir(name); @@ -134,23 +81,4 @@ private void runTestViaRunMethod(IReporter reporter, String jvm) { File actual = new File(output.getAbsolutePath(), filename); assertThat(actual).exists(); } - - public static class MySecurityManager extends SecurityManager { - - private SecurityManager baseSecurityManager; - - MySecurityManager(SecurityManager baseSecurityManager) { - this.baseSecurityManager = baseSecurityManager; - } - - @Override - public void checkPermission(Permission permission) { - if (permission.getName().startsWith("exitVM")) { - throw new SecurityException("System exit not allowed"); - } - if (baseSecurityManager != null) { - baseSecurityManager.checkPermission(permission); - } - } - } } diff --git a/testng-jcommander/build.gradle.kts b/testng-jcommander/build.gradle.kts index d3e7203b01..18c9ec2f00 100644 --- a/testng-jcommander/build.gradle.kts +++ b/testng-jcommander/build.gradle.kts @@ -7,4 +7,5 @@ dependencies { implementation(projects.testngCore) testImplementation(projects.testngAsserts) + testImplementation(projects.testngTestKit) } diff --git a/testng-jcommander/src/test/java/test/groups/issue2232/IssueTest.java b/testng-jcommander/src/test/java/test/groups/issue2232/IssueTest.java new file mode 100644 index 0000000000..709a722930 --- /dev/null +++ b/testng-jcommander/src/test/java/test/groups/issue2232/IssueTest.java @@ -0,0 +1,76 @@ +package test.groups.issue2232; + +import org.testng.Reporter; +import org.testng.TestNG; +import org.testng.annotations.Test; +import org.testng.xml.XmlGroups; +import org.testng.xml.XmlPackage; +import org.testng.xml.XmlRun; +import org.testng.xml.XmlSuite; +import org.testng.xml.XmlTest; +import test.SimpleBaseTest; +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class IssueTest extends SimpleBaseTest { + + @Test(invocationCount = 2, description = "GITHUB-2232") + // Ensuring that the bug doesn't surface even when tests are executed via the command line mode + public void commandlineTest() throws IOException, InterruptedException { + Path suitefile = + Files.write( + Files.createTempFile("testng", ".xml"), + constructSuite().toXml().getBytes(Charset.defaultCharset())); + List args = Collections.singletonList(suitefile.toFile().getAbsolutePath()); + int status = exec(Collections.emptyList(), args); + assertThat(status).isEqualTo(0); + } + + private XmlSuite constructSuite() { + XmlSuite xmlsuite = createXmlSuite("2232_suite"); + xmlsuite.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE); + xmlsuite.setThreadCount(256); + xmlsuite.setParallel(XmlSuite.ParallelMode.CLASSES); + XmlTest xmltest = createXmlTest(xmlsuite, "2232_test"); + XmlRun xmlrun = new XmlRun(); + xmlrun.onInclude("Group2"); + xmlrun.onExclude("Broken"); + XmlGroups xmlgroup = new XmlGroups(); + xmlgroup.setRun(xmlrun); + xmltest.setGroups(xmlgroup); + XmlPackage xmlpackage = new XmlPackage(); + xmlpackage.setName(getClass().getPackage().getName() + ".samples.*"); + xmltest.setPackages(Collections.singletonList(xmlpackage)); + return xmlsuite; + } + + private int exec(List jvmArgs, List args) + throws IOException, InterruptedException { + + String javaHome = System.getProperty("java.home"); + String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; + String classpath = System.getProperty("java.class.path"); + String className = TestNG.class.getName(); + List command = new ArrayList<>(); + command.add(javaBin); + command.addAll(jvmArgs); + command.add("-cp"); + command.add(classpath); + command.add(className); + command.addAll(args); + Reporter.log("Executing the command " + command, 2, true); + ProcessBuilder builder = new ProcessBuilder(command); + Process process = builder.inheritIO().start(); + process.waitFor(); + + return process.exitValue(); + } +} diff --git a/testng-jcommander/src/test/java/test/reports/EmailableReporterTest.java b/testng-jcommander/src/test/java/test/reports/EmailableReporterTest.java new file mode 100644 index 0000000000..7a12e9df21 --- /dev/null +++ b/testng-jcommander/src/test/java/test/reports/EmailableReporterTest.java @@ -0,0 +1,82 @@ +package test.reports; + +import org.testng.CliTestNgRunner; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.testng.reporters.EmailableReporter; +import org.testng.reporters.EmailableReporter2; +import test.MySecurityManager; +import test.SimpleBaseTest; +import java.io.File; +import java.lang.reflect.Method; + +import static org.assertj.core.api.Assertions.assertThat; + +public class EmailableReporterTest extends SimpleBaseTest { + private SecurityManager manager; + + @BeforeClass(alwaysRun = true) + public void setup() { + manager = System.getSecurityManager(); + System.setSecurityManager(new MySecurityManager(manager)); + } + + @AfterClass(alwaysRun = true) + public void cleanup() { + System.setSecurityManager(manager); + } + + @Test(dataProvider = "getReporterNames", priority = 3) + public void testReportsNameCustomizationViaMainMethodInvocation(String clazzName) { + runTestViaMainMethod(clazzName, null /* no jvm arguments */); + } + + @Test(dataProvider = "getReporterNames", priority = 4) + public void testReportsNameCustomizationViaMainMethodInvocationAndJVMArguments( + String clazzName, String jvm) { + runTestViaMainMethod(clazzName, jvm); + } + + @DataProvider(name = "getReporterNames") + public Object[][] getReporterNames(Method method) { + if (method.getName().toLowerCase().contains("jvmarguments")) { + return new Object[][] { + {EmailableReporter.class.getName(), "emailable.report.name"}, + {EmailableReporter2.class.getName(), "emailable.report2.name"} + }; + } + return new Object[][] { + {EmailableReporter.class.getName()}, {EmailableReporter2.class.getName()} + }; + } + + private void runTestViaMainMethod(String clazzName, String jvm) { + String name = Long.toString(System.currentTimeMillis()); + File output = createDirInTempDir(name); + String filename = "report" + name + ".html"; + String[] args = { + "-d", + output.getAbsolutePath(), + "-reporter", + clazzName + ":fileName=" + filename, + "src/test/resources/1332.xml" + }; + try { + if (jvm != null) { + System.setProperty(jvm, filename); + } + CliTestNgRunner.Main.main(args); + } catch (SecurityException t) { + // Gobble Security exception + } finally { + if (jvm != null) { + // reset the jvm arguments + System.setProperty(jvm, ""); + } + } + File actual = new File(output.getAbsolutePath(), filename); + assertThat(actual).exists(); + } +} diff --git a/testng-test-kit/build.gradle.kts b/testng-test-kit/build.gradle.kts index 0438ec3bf1..33a9b73800 100644 --- a/testng-test-kit/build.gradle.kts +++ b/testng-test-kit/build.gradle.kts @@ -1,3 +1,9 @@ plugins { id("testng.java-library") } + +dependencies { + api(projects.testngCoreApi) + implementation(projects.testngAsserts) + implementation(projects.testngCore) +} diff --git a/testng-core/src/test/java/test/InvokedMethodNameListener.java b/testng-test-kit/src/main/java/test/InvokedMethodNameListener.java similarity index 96% rename from testng-core/src/test/java/test/InvokedMethodNameListener.java rename to testng-test-kit/src/main/java/test/InvokedMethodNameListener.java index 40e5ab5845..4d15081a77 100644 --- a/testng-core/src/test/java/test/InvokedMethodNameListener.java +++ b/testng-test-kit/src/main/java/test/InvokedMethodNameListener.java @@ -1,7 +1,7 @@ package test; -import com.google.common.base.Joiner; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; @@ -149,7 +149,7 @@ private static String getName(ITestResult result) { name = name + "(" - + Joiner.on(",").useForNull("null").join(getParameterNames(result.getParameters())) + + String.join(",", getParameterNames(result.getParameters())) + ")"; } return name; @@ -162,7 +162,7 @@ private static List getParameterNames(Object[] parameters) { result.add("null"); } else { if (parameter instanceof Object[]) { - result.add("[" + Joiner.on(",").useForNull("null").join((Object[]) parameter) + "]"); + result.add("[" + Arrays.toString((Object[]) parameter) + "]"); } else { result.add(parameter.toString()); } diff --git a/testng-test-kit/src/main/java/test/MySecurityManager.java b/testng-test-kit/src/main/java/test/MySecurityManager.java new file mode 100644 index 0000000000..58c16915b7 --- /dev/null +++ b/testng-test-kit/src/main/java/test/MySecurityManager.java @@ -0,0 +1,22 @@ +package test; + +import java.security.Permission; + +public class MySecurityManager extends SecurityManager { + + private SecurityManager baseSecurityManager; + + public MySecurityManager(SecurityManager baseSecurityManager) { + this.baseSecurityManager = baseSecurityManager; + } + + @Override + public void checkPermission(Permission permission) { + if (permission.getName().startsWith("exitVM")) { + throw new SecurityException("System exit not allowed"); + } + if (baseSecurityManager != null) { + baseSecurityManager.checkPermission(permission); + } + } +} diff --git a/testng-core/src/test/java/test/SimpleBaseTest.java b/testng-test-kit/src/main/java/test/SimpleBaseTest.java similarity index 100% rename from testng-core/src/test/java/test/SimpleBaseTest.java rename to testng-test-kit/src/main/java/test/SimpleBaseTest.java From 56ca68ea75834f5d90c4e4360d19d2c7be56d222 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Sat, 24 Jul 2021 22:56:46 +0200 Subject: [PATCH 7/8] Add module in BOM --- testng/build.gradle.kts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/testng/build.gradle.kts b/testng/build.gradle.kts index 1bf8aa0b70..deab7304ae 100644 --- a/testng/build.gradle.kts +++ b/testng/build.gradle.kts @@ -21,6 +21,9 @@ java { api(platform("com.google.inject:guice-bom:_")) api("com.google.inject:guice") } + create("jcommander") { + implementation(projects.testngJcommander) + } create("junit") { implementation(projects.testngRunnerJunit4) } @@ -35,6 +38,7 @@ dependencies { // would be selected automatically shadedDependencyElements(projects.testngAsserts) shadedDependencyElements(projects.testngCore) + shadedDependencyElements(projects.testngJcommander) } tasks.mergedJar { From f80bee2c96e2c857b6acf2b1beda6df82bc966c6 Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Sat, 24 Jul 2021 22:58:39 +0200 Subject: [PATCH 8/8] Fix style --- .../src/test/java/test/CommandLineTest.java | 1 - .../java/test/groups/issue2232/IssueTest.java | 8 ------- .../java/test/groups/issue2232/IssueTest.java | 20 ++++++++-------- .../test/reports/EmailableReporterTest.java | 24 +++++++++---------- .../java/test/InvokedMethodNameListener.java | 6 +---- 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/testng-core/src/test/java/test/CommandLineTest.java b/testng-core/src/test/java/test/CommandLineTest.java index 0a95f931b9..c94d7767bb 100644 --- a/testng-core/src/test/java/test/CommandLineTest.java +++ b/testng-core/src/test/java/test/CommandLineTest.java @@ -8,7 +8,6 @@ import com.google.inject.Stage; import java.lang.reflect.Field; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; diff --git a/testng-core/src/test/java/test/groups/issue2232/IssueTest.java b/testng-core/src/test/java/test/groups/issue2232/IssueTest.java index 35c6f4f90b..c939e3f91a 100644 --- a/testng-core/src/test/java/test/groups/issue2232/IssueTest.java +++ b/testng-core/src/test/java/test/groups/issue2232/IssueTest.java @@ -2,15 +2,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; -import org.testng.Reporter; import org.testng.TestNG; import org.testng.annotations.Test; import org.testng.xml.XmlGroups; diff --git a/testng-jcommander/src/test/java/test/groups/issue2232/IssueTest.java b/testng-jcommander/src/test/java/test/groups/issue2232/IssueTest.java index 709a722930..b122896953 100644 --- a/testng-jcommander/src/test/java/test/groups/issue2232/IssueTest.java +++ b/testng-jcommander/src/test/java/test/groups/issue2232/IssueTest.java @@ -1,5 +1,15 @@ package test.groups.issue2232; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import org.testng.Reporter; import org.testng.TestNG; import org.testng.annotations.Test; @@ -9,16 +19,6 @@ import org.testng.xml.XmlSuite; import org.testng.xml.XmlTest; import test.SimpleBaseTest; -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; public class IssueTest extends SimpleBaseTest { diff --git a/testng-jcommander/src/test/java/test/reports/EmailableReporterTest.java b/testng-jcommander/src/test/java/test/reports/EmailableReporterTest.java index 7a12e9df21..0eac309be5 100644 --- a/testng-jcommander/src/test/java/test/reports/EmailableReporterTest.java +++ b/testng-jcommander/src/test/java/test/reports/EmailableReporterTest.java @@ -1,5 +1,9 @@ package test.reports; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.lang.reflect.Method; import org.testng.CliTestNgRunner; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -9,10 +13,6 @@ import org.testng.reporters.EmailableReporter2; import test.MySecurityManager; import test.SimpleBaseTest; -import java.io.File; -import java.lang.reflect.Method; - -import static org.assertj.core.api.Assertions.assertThat; public class EmailableReporterTest extends SimpleBaseTest { private SecurityManager manager; @@ -43,12 +43,12 @@ public void testReportsNameCustomizationViaMainMethodInvocationAndJVMArguments( public Object[][] getReporterNames(Method method) { if (method.getName().toLowerCase().contains("jvmarguments")) { return new Object[][] { - {EmailableReporter.class.getName(), "emailable.report.name"}, - {EmailableReporter2.class.getName(), "emailable.report2.name"} + {EmailableReporter.class.getName(), "emailable.report.name"}, + {EmailableReporter2.class.getName(), "emailable.report2.name"} }; } return new Object[][] { - {EmailableReporter.class.getName()}, {EmailableReporter2.class.getName()} + {EmailableReporter.class.getName()}, {EmailableReporter2.class.getName()} }; } @@ -57,11 +57,11 @@ private void runTestViaMainMethod(String clazzName, String jvm) { File output = createDirInTempDir(name); String filename = "report" + name + ".html"; String[] args = { - "-d", - output.getAbsolutePath(), - "-reporter", - clazzName + ":fileName=" + filename, - "src/test/resources/1332.xml" + "-d", + output.getAbsolutePath(), + "-reporter", + clazzName + ":fileName=" + filename, + "src/test/resources/1332.xml" }; try { if (jvm != null) { diff --git a/testng-test-kit/src/main/java/test/InvokedMethodNameListener.java b/testng-test-kit/src/main/java/test/InvokedMethodNameListener.java index 4d15081a77..6b83db94f8 100644 --- a/testng-test-kit/src/main/java/test/InvokedMethodNameListener.java +++ b/testng-test-kit/src/main/java/test/InvokedMethodNameListener.java @@ -146,11 +146,7 @@ private static String getName(ITestResult result) { name = testName + "#" + methodName; } if (result.getParameters().length != 0) { - name = - name - + "(" - + String.join(",", getParameterNames(result.getParameters())) - + ")"; + name = name + "(" + String.join(",", getParameterNames(result.getParameters())) + ")"; } return name; }