diff --git a/README.md b/README.md index d8391e3..bb43dee 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,12 @@ utplsql run "my/Username"/"myP@ssword"@connectstring -dbout - Enables DBMS_OUTPUT in the TestRunner-Session (--dbms_output) Default: false + +-random - Enables random order of test executions +(--random-test-order) Default: false + +-seed - Sets the seed to use for random test execution order. If set, it sets -random to true +(--random-test-order-seed) ``` Parameters -f, -o, -s are correlated. That is parameters -o and -s are controlling outputs for reporter specified by the preceding -f parameter. diff --git a/pom.xml b/pom.xml index b1fc034..a74200d 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,12 @@ ${junit.jupiter.version} test + + org.hamcrest + hamcrest + 2.1 + test + diff --git a/src/main/java/org/utplsql/cli/RunCommand.java b/src/main/java/org/utplsql/cli/RunCommand.java index e85b9e7..9ebc251 100644 --- a/src/main/java/org/utplsql/cli/RunCommand.java +++ b/src/main/java/org/utplsql/cli/RunCommand.java @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import org.utplsql.api.*; import org.utplsql.api.compatibility.CompatibilityProxy; +import org.utplsql.api.compatibility.OptionalFeatures; import org.utplsql.api.db.DefaultDatabaseInformation; import org.utplsql.api.exception.DatabaseNotCompatibleException; import org.utplsql.api.exception.OracleCreateStatmenetStuckException; @@ -126,6 +127,18 @@ public class RunCommand implements ICommand { ) private boolean enableDbmsOutput = false; + @Parameter( + names = {"-random", "--random-test-order"}, + description = "Enables random order of test executions (default: DISABLED)" + ) + private boolean randomTestOrder = false; + + @Parameter( + names = {"-seed", "--random-test-order-seed"}, + description = "Sets the seed to use for random test execution order. If set, it sets -random to true" + ) + private Integer randomTestOrderSeed; + private CompatibilityProxy compatibilityProxy; private ReporterFactory reporterFactory; private ReporterManager reporterManager; @@ -162,11 +175,7 @@ public int doRun() throws OracleCreateStatmenetStuckException { initDatabase(dataSource); reporterList = initReporters(dataSource); - // Output a message if --failureExitCode is set but database framework is not capable of - String msg = RunCommandChecker.getCheckFailOnErrorMessage(failureExitCode, compatibilityProxy.getUtPlsqlVersion()); - if (msg != null) { - System.out.println(msg); - } + checkForCompatibility(compatibilityProxy.getUtPlsqlVersion()); ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size()); @@ -228,7 +237,28 @@ public int run() { return Cli.DEFAULT_ERROR_CODE; } - private TestRunner newTestRunner( List reporterList) { + private void checkForCompatibility( Version utPlSqlVersion ) { + if (!OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(utPlSqlVersion) && failureExitCode != 1 ) { + System.out.println("You specified option `--failure-exit-code` but your database framework version (" + + utPlSqlVersion.getNormalizedString() + ") is not able to " + + "redirect failureCodes. Please upgrade to a newer version if you want to use that feature."); + } + + if ( !OptionalFeatures.RANDOM_EXECUTION_ORDER.isAvailableFor(utPlSqlVersion) && randomTestOrder ) { + System.out.println("You specified option `-random` but your database framework version (" + + utPlSqlVersion.getNormalizedString() + ") is not able to " + + "redirect failureCodes. Please upgrade to a newer version if you want to use that feature."); + } + + if ( !OptionalFeatures.RANDOM_EXECUTION_ORDER.isAvailableFor(utPlSqlVersion) && randomTestOrderSeed != null ) { + System.out.println("You specified option `-seed` but your database framework version (" + + utPlSqlVersion.getNormalizedString() + ") is not able to " + + "redirect failureCodes. Please upgrade to a newer version if you want to use that feature."); + } + + } + + TestRunner newTestRunner( List reporterList) { final File baseDir = new File("").getAbsoluteFile(); @@ -241,7 +271,9 @@ private TestRunner newTestRunner( List reporterList) { .failOnErrors(true) .skipCompatibilityCheck(skipCompatibilityCheck) .includeObjects(getObjectList(includeObjects)) - .excludeObjects(getObjectList(excludeObjects)); + .excludeObjects(getObjectList(excludeObjects)) + .randomTestOrder(randomTestOrder) + .randomTestOrderSeed(randomTestOrderSeed); } private ArrayList getObjectList(String includeObjects) { diff --git a/src/main/java/org/utplsql/cli/RunCommandChecker.java b/src/main/java/org/utplsql/cli/RunCommandChecker.java index 6adce56..ed9d43d 100644 --- a/src/main/java/org/utplsql/cli/RunCommandChecker.java +++ b/src/main/java/org/utplsql/cli/RunCommandChecker.java @@ -27,18 +27,4 @@ static void checkOracleI18nExists(Connection con) throws SQLException { System.out.println("Download from http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html"); } } - - /** Returns a warning message if failureExitCode is specified but database version is too low - * - * @param failureExitCode - * @param databaseVersion - */ - static String getCheckFailOnErrorMessage(int failureExitCode, Version databaseVersion) { - if ( failureExitCode != 1 && !OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(databaseVersion)) { - return "Your database framework version (" + databaseVersion.getNormalizedString() + ") is not able to " + - "redirect failureCodes. Please upgrade to a newer version if you want to use that feature."; - } - - return null; - } } diff --git a/src/test/java/org/utplsql/cli/RunCommandTest.java b/src/test/java/org/utplsql/cli/RunCommandTest.java index 941a0e1..e2ec155 100644 --- a/src/test/java/org/utplsql/cli/RunCommandTest.java +++ b/src/test/java/org/utplsql/cli/RunCommandTest.java @@ -1,10 +1,15 @@ package org.utplsql.cli; import org.junit.jupiter.api.Test; +import org.utplsql.api.TestRunnerOptions; import org.utplsql.api.reporter.CoreReporters; +import java.util.ArrayList; import java.util.List; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.*; /** @@ -92,4 +97,33 @@ void connectionString_asSysdba() { assertEquals("sys as sysdba/mypass@connectstring/service", runCmd.getConnectionInfo().getConnectionString()); } + + @Test + void randomOrder_default() { + RunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString()); + + TestRunnerOptions options = runCmd.newTestRunner(new ArrayList<>()).getOptions(); + assertThat(options.randomTestOrder, equalTo(false)); + assertThat(options.randomTestOrderSeed, nullValue()); + } + + @Test + void randomOrder_withoutSeed() { + RunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), + "-random"); + + TestRunnerOptions options = runCmd.newTestRunner(new ArrayList<>()).getOptions(); + assertThat(options.randomTestOrder, equalTo(true)); + assertThat(options.randomTestOrderSeed, nullValue()); + } + + @Test + void randomOrder_withSeed() { + RunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), + "-seed=42"); + + TestRunnerOptions options = runCmd.newTestRunner(new ArrayList<>()).getOptions(); + assertThat(options.randomTestOrder, equalTo(true)); + assertThat(options.randomTestOrderSeed, equalTo(42)); + } } diff --git a/src/test/java/org/utplsql/cli/TestRunCommandChecker.java b/src/test/java/org/utplsql/cli/TestRunCommandChecker.java deleted file mode 100644 index 803a3f1..0000000 --- a/src/test/java/org/utplsql/cli/TestRunCommandChecker.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.utplsql.cli; - -import org.junit.jupiter.api.Test; -import org.utplsql.api.Version; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; - -class TestRunCommandChecker { - - @Test - void getCheckFailOnErrorMessage() - { - // FailOnError option should work since 3.0.3+ framework - assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.0"))); - assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.1"))); - assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.2"))); - assertNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.3.1266"))); - assertNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.4"))); - } -}