From 91d2f43c4f9ba05d4a2df9b12c58c7ef9788eef6 Mon Sep 17 00:00:00 2001 From: Samuel Nitsche Date: Tue, 6 Feb 2018 21:52:01 +0100 Subject: [PATCH 1/4] Refactored tests to use Surefire, Failsafe and JUnit 5 --- .travis.yml | 2 +- pom.xml | 87 ++++++++++++++++-- .../java/org/utplsql/cli/FileWalkerTest.java | 9 +- .../cli/RunCommandCoverageReporterIT.java | 86 ++++++++++++++++++ .../RunCommandCoverageReporterSystemTest.java | 91 ------------------- .../java/org/utplsql/cli/RunCommandIT.java | 30 ++++++ .../org/utplsql/cli/RunCommandSystemTest.java | 40 -------- .../java/org/utplsql/cli/RunCommandTest.java | 55 ++++++----- .../org/utplsql/cli/RunCommandTestHelper.java | 7 +- .../utplsql/cli/TestRunCommandChecker.java | 16 ++-- 10 files changed, 239 insertions(+), 184 deletions(-) create mode 100644 src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java delete mode 100644 src/test/java/org/utplsql/cli/RunCommandCoverageReporterSystemTest.java create mode 100644 src/test/java/org/utplsql/cli/RunCommandIT.java delete mode 100644 src/test/java/org/utplsql/cli/RunCommandSystemTest.java diff --git a/.travis.yml b/.travis.yml index 5ef4630..8201fd0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,7 +34,7 @@ install: script: - mvn package -DskipTests - - mvn package jar:jar appassembler:assemble + - mvn package verify jar:jar appassembler:assemble before_deploy: - bash .travis/create_release.sh diff --git a/pom.xml b/pom.xml index 052f898..71f6616 100644 --- a/pom.xml +++ b/pom.xml @@ -10,17 +10,19 @@ cli http://maven.apache.org - - UTF-8 - 1.8 - 1.8 - + + UTF-8 + 1.8 + 1.8 + 1.0.3 + 5.0.3 + org.utplsql java-api - 3.0.4-SNAPSHOT + 3.0.5-SNAPSHOT compile @@ -48,11 +50,17 @@ compile - junit - junit - 4.12 + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} test - + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + @@ -75,6 +83,43 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + **/*IT.java + + + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.19.1 + + + + integration-test + verify + + + + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + @@ -93,4 +138,26 @@ + + + utPLSQL-local + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + + ${dbUrl} + ${dbUser} + ${dbPass} + + + + + + + + diff --git a/src/test/java/org/utplsql/cli/FileWalkerTest.java b/src/test/java/org/utplsql/cli/FileWalkerTest.java index b8e2738..9c00a8d 100644 --- a/src/test/java/org/utplsql/cli/FileWalkerTest.java +++ b/src/test/java/org/utplsql/cli/FileWalkerTest.java @@ -1,12 +1,13 @@ package org.utplsql.cli; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.File; import java.util.Collections; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + /** * Created by Vinicius on 18/06/2017. */ @@ -18,7 +19,7 @@ public class FileWalkerTest { public void fileWalker_Relative() { List fileList = new FileWalker().getFileList(BASE_DIR, "source"); Collections.sort(fileList); - Assert.assertArrayEquals(new Object[] { + assertArrayEquals(new Object[] { "source/packages/package.pkb".replace('/', File.separatorChar), "source/packages/package.pks".replace('/', File.separatorChar), "source/script.sql".replace('/', File.separatorChar), @@ -30,7 +31,7 @@ public void fileWalker_Relative() { public void fileWalker_Absolute() { List fileList = new FileWalker().getFileList(BASE_DIR, "source", false); Collections.sort(fileList); - Assert.assertArrayEquals(new Object[] { + assertArrayEquals(new Object[] { BASE_DIR.getAbsolutePath() + "/source/packages/package.pkb".replace('/', File.separatorChar), BASE_DIR.getAbsolutePath() + "/source/packages/package.pks".replace('/', File.separatorChar), BASE_DIR.getAbsolutePath() + "/source/script.sql".replace('/', File.separatorChar), diff --git a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java new file mode 100644 index 0000000..c70b6e2 --- /dev/null +++ b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java @@ -0,0 +1,86 @@ +package org.utplsql.cli; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * System tests for Code Coverage Reporter + * + * @author pesse + */ +public class RunCommandCoverageReporterIT { + + private static final Pattern REGEX_COVERAGE_TITLE = Pattern.compile("([a-zA-Z0-9\\._]+)<\\/a>"); + + private String getTempCoverageFileName(int counter) { + + return "tmpCoverage_" + String.valueOf(System.currentTimeMillis()) + "_" + String.valueOf(counter) + ".html"; + } + + /** + * Returns a random filename which does not yet exist on the local path + * + * @return + */ + private Path getTempCoverageFilePath() { + int i = 1; + Path p = Paths.get(getTempCoverageFileName(i)); + + while (Files.exists(p) && i < 100) + p = Paths.get(getTempCoverageFileName(i++)); + + if (i >= 100) + throw new IllegalStateException("Could not get temporary file for coverage output"); + + return p; + } + + /** + * Checks Coverage HTML Output if a given packageName is listed + * + * @param content + * @param packageName + * @return + */ + private boolean hasCoverageListed(String content, String packageName) { + Matcher m = REGEX_COVERAGE_TITLE.matcher(content); + + while (m.find()) { + if (packageName.equals(m.group(1))) + return true; + } + + return false; + } + + @Test + public void run_CodeCoverageWithIncludeAndExclude() throws Exception { + + Path coveragePath = getTempCoverageFilePath(); + + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), + "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s", "-exclude=app.award_bonus,app.betwnstr"); + + try { + int result = runCmd.run(); + + String content = new Scanner(coveragePath).useDelimiter("\\Z").next(); + + assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name")); + assertEquals(false, hasCoverageListed(content, "app.award_bonus")); + assertEquals(false, hasCoverageListed(content, "app.betwnstr")); + + } finally { + Files.delete(coveragePath); + } + + } +} diff --git a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterSystemTest.java b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterSystemTest.java deleted file mode 100644 index 4ad03ca..0000000 --- a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterSystemTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.utplsql.cli; - -import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match; -import org.junit.Assert; -import org.junit.Test; -import org.utplsql.api.compatibility.OptionalFeatures; - -import java.io.File; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Scanner; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * System tests for Code Coverage Reporter - * - * @author pesse - */ -public class RunCommandCoverageReporterSystemTest { - - private static final Pattern REGEX_COVERAGE_TITLE = Pattern.compile("([a-zA-Z0-9\\._]+)<\\/a>"); - - private String getTempCoverageFileName(int counter) { - - return "tmpCoverage_" + String.valueOf(System.currentTimeMillis()) + "_" + String.valueOf(counter) + ".html"; - } - - /** - * Returns a random filename which does not yet exist on the local path - * - * @return - */ - private Path getTempCoverageFilePath() { - int i = 1; - Path p = Paths.get(getTempCoverageFileName(i)); - - while (Files.exists(p) && i < 100) - p = Paths.get(getTempCoverageFileName(i++)); - - if (i >= 100) - throw new IllegalStateException("Could not get temporary file for coverage output"); - - return p; - } - - /** Checks Coverage HTML Output if a given packageName is listed - * - * @param content - * @param packageName - * @return - */ - private boolean hasCoverageListed( String content, String packageName) { - Matcher m = REGEX_COVERAGE_TITLE.matcher(content); - - while ( m.find() ) { - if ( packageName.equals(m.group(1)) ) - return true; - } - - return false; - } - - @Test - public void run_CodeCoverageWithIncludeAndExclude() { - - try { - Path coveragePath = getTempCoverageFilePath(); - - RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), - "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s", "-exclude=app.award_bonus,app.betwnstr"); - - try { - int result = runCmd.run(); - - String content = new Scanner(coveragePath).useDelimiter("\\Z").next(); - - Assert.assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name")); - Assert.assertEquals(false, hasCoverageListed(content, "app.award_bonus")); - Assert.assertEquals(false, hasCoverageListed(content, "app.betwnstr")); - - } finally { - Files.delete(coveragePath); - } - } catch (Exception e) { - Assert.fail(e.getMessage()); - } - - } -} diff --git a/src/test/java/org/utplsql/cli/RunCommandIT.java b/src/test/java/org/utplsql/cli/RunCommandIT.java new file mode 100644 index 0000000..6aadbda --- /dev/null +++ b/src/test/java/org/utplsql/cli/RunCommandIT.java @@ -0,0 +1,30 @@ +package org.utplsql.cli; + +import org.junit.jupiter.api.Test; +import org.utplsql.api.compatibility.OptionalFeatures; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * System tests for run command. + */ +public class RunCommandIT { + + @Test + public void run_Default() throws Exception { + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), + "-f=ut_documentation_reporter", + "-c", + "--failure-exit-code=2"); + + int result = runCmd.run(); + + // Only expect failure-exit-code to work on several framework versions + if (OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(runCmd.getDatabaseVersion())) + assertEquals(2, result); + else + assertEquals(0, result); + } + + +} diff --git a/src/test/java/org/utplsql/cli/RunCommandSystemTest.java b/src/test/java/org/utplsql/cli/RunCommandSystemTest.java deleted file mode 100644 index 19e5488..0000000 --- a/src/test/java/org/utplsql/cli/RunCommandSystemTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.utplsql.cli; - -import com.beust.jcommander.JCommander; -import org.junit.Assert; -import org.junit.Test; -import org.utplsql.api.CustomTypes; -import org.utplsql.api.compatibility.OptionalFeatures; - -import java.util.List; - -/** - * System tests for run command. - */ -public class RunCommandSystemTest { - - - @Test - public void run_Default() { - RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), - "-f=ut_documentation_reporter", - "-c", - "--failure-exit-code=2"); - - try { - int result = runCmd.run(); - - // Only expect failure-exit-code to work on several framework versions - if (OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(runCmd.getDatabaseVersion()) ) - Assert.assertEquals(2, result); - else - Assert.assertEquals(0, result); - } - catch ( Exception e ) { - Assert.fail(e.getMessage()); - } - } - - - -} diff --git a/src/test/java/org/utplsql/cli/RunCommandTest.java b/src/test/java/org/utplsql/cli/RunCommandTest.java index f8248f3..775d506 100644 --- a/src/test/java/org/utplsql/cli/RunCommandTest.java +++ b/src/test/java/org/utplsql/cli/RunCommandTest.java @@ -1,13 +1,12 @@ package org.utplsql.cli; -import com.beust.jcommander.JCommander; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.utplsql.api.CustomTypes; -import org.utplsql.api.compatibility.OptionalFeatures; import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + /** * Unit test for run command. */ @@ -20,10 +19,10 @@ public void reporterOptions_Default() { List reporterOptionsList = runCmd.getReporterOptionsList(); ReporterOptions reporterOptions1 = reporterOptionsList.get(0); - Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); - Assert.assertNull(reporterOptions1.getOutputFileName()); - Assert.assertFalse(reporterOptions1.outputToFile()); - Assert.assertTrue(reporterOptions1.outputToScreen()); + assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + assertNull(reporterOptions1.getOutputFileName()); + assertFalse(reporterOptions1.outputToFile()); + assertTrue(reporterOptions1.outputToScreen()); } @Test @@ -33,10 +32,10 @@ public void reporterOptions_OneReporter() { List reporterOptionsList = runCmd.getReporterOptionsList(); ReporterOptions reporterOptions1 = reporterOptionsList.get(0); - Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); - Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); - Assert.assertTrue(reporterOptions1.outputToFile()); - Assert.assertFalse(reporterOptions1.outputToScreen()); + assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); + assertTrue(reporterOptions1.outputToFile()); + assertFalse(reporterOptions1.outputToScreen()); } @Test @@ -46,10 +45,10 @@ public void reporterOptions_OneReporterForceScreen() { List reporterOptionsList = runCmd.getReporterOptionsList(); ReporterOptions reporterOptions1 = reporterOptionsList.get(0); - Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); - Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); - Assert.assertTrue(reporterOptions1.outputToFile()); - Assert.assertTrue(reporterOptions1.outputToScreen()); + assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); + assertTrue(reporterOptions1.outputToFile()); + assertTrue(reporterOptions1.outputToScreen()); } @Test @@ -59,10 +58,10 @@ public void reporterOptions_OneReporterForceScreenInverse() { List reporterOptionsList = runCmd.getReporterOptionsList(); ReporterOptions reporterOptions1 = reporterOptionsList.get(0); - Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); - Assert.assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); - Assert.assertTrue(reporterOptions1.outputToFile()); - Assert.assertTrue(reporterOptions1.outputToScreen()); + assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + assertEquals(reporterOptions1.getOutputFileName(), "output.txt"); + assertTrue(reporterOptions1.outputToFile()); + assertTrue(reporterOptions1.outputToScreen()); } @Test @@ -74,16 +73,16 @@ public void reporterOptions_TwoReporters() { List reporterOptionsList = runCmd.getReporterOptionsList(); ReporterOptions reporterOptions1 = reporterOptionsList.get(0); - Assert.assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); - Assert.assertNull(reporterOptions1.getOutputFileName()); - Assert.assertFalse(reporterOptions1.outputToFile()); - Assert.assertTrue(reporterOptions1.outputToScreen()); + assertEquals(CustomTypes.UT_DOCUMENTATION_REPORTER, reporterOptions1.getReporterName()); + assertNull(reporterOptions1.getOutputFileName()); + assertFalse(reporterOptions1.outputToFile()); + assertTrue(reporterOptions1.outputToScreen()); ReporterOptions reporterOptions2 = reporterOptionsList.get(1); - Assert.assertEquals(CustomTypes.UT_COVERAGE_HTML_REPORTER, reporterOptions2.getReporterName()); - Assert.assertEquals(reporterOptions2.getOutputFileName(), "coverage.html"); - Assert.assertTrue(reporterOptions2.outputToFile()); - Assert.assertTrue(reporterOptions2.outputToScreen()); + assertEquals(CustomTypes.UT_COVERAGE_HTML_REPORTER, reporterOptions2.getReporterName()); + assertEquals(reporterOptions2.getOutputFileName(), "coverage.html"); + assertTrue(reporterOptions2.outputToFile()); + assertTrue(reporterOptions2.outputToScreen()); } } diff --git a/src/test/java/org/utplsql/cli/RunCommandTestHelper.java b/src/test/java/org/utplsql/cli/RunCommandTestHelper.java index 9f9cf4f..425659a 100644 --- a/src/test/java/org/utplsql/cli/RunCommandTestHelper.java +++ b/src/test/java/org/utplsql/cli/RunCommandTestHelper.java @@ -1,6 +1,7 @@ package org.utplsql.cli; import com.beust.jcommander.JCommander; +import org.utplsql.api.EnvironmentVariableUtil; class RunCommandTestHelper { private static String sUrl; @@ -8,9 +9,9 @@ class RunCommandTestHelper { private static String sPass; static { - sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE"; - sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app"; - sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app"; + sUrl = EnvironmentVariableUtil.getEnvValue("DB_URL", "192.168.99.100:1521:XE"); + sUser = EnvironmentVariableUtil.getEnvValue("DB_USER", "app"); + sPass = EnvironmentVariableUtil.getEnvValue("DB_PASS", "app"); } static RunCommand createRunCommand(String... args) { diff --git a/src/test/java/org/utplsql/cli/TestRunCommandChecker.java b/src/test/java/org/utplsql/cli/TestRunCommandChecker.java index fe807de..fcf2d21 100644 --- a/src/test/java/org/utplsql/cli/TestRunCommandChecker.java +++ b/src/test/java/org/utplsql/cli/TestRunCommandChecker.java @@ -1,19 +1,21 @@ package org.utplsql.cli; -import org.junit.Assert; -import org.junit.Test; +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; + public class TestRunCommandChecker { @Test public void getCheckFailOnErrorMessage() { // FailOnError option should work since 3.0.3+ framework - Assert.assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.0"))); - Assert.assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.1"))); - Assert.assertNotNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.2"))); - Assert.assertNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.3"))); - Assert.assertNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.4"))); + 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"))); + assertNull(RunCommandChecker.getCheckFailOnErrorMessage(2, new Version("3.0.4"))); } } From 8f4a7e1b9a0e7b6ea98576ce9798b30209d3ce68 Mon Sep 17 00:00:00 2001 From: Samuel Nitsche Date: Wed, 7 Feb 2018 09:59:03 +0100 Subject: [PATCH 2/4] First simple implementation of copying necessary assets Needs lots of refactoring --- src/main/java/org/utplsql/cli/RunCommand.java | 11 +++++++-- .../cli/RunCommandCoverageReporterIT.java | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/utplsql/cli/RunCommand.java b/src/main/java/org/utplsql/cli/RunCommand.java index 4f41d02..fbf5e3c 100644 --- a/src/main/java/org/utplsql/cli/RunCommand.java +++ b/src/main/java/org/utplsql/cli/RunCommand.java @@ -4,9 +4,8 @@ import com.beust.jcommander.Parameters; import org.utplsql.api.*; import org.utplsql.api.compatibility.CompatibilityProxy; -import org.utplsql.api.compatibility.OptionalFeatures; -import org.utplsql.api.exception.DatabaseNotCompatibleException; import org.utplsql.api.exception.SomeTestsFailedException; +import org.utplsql.api.reporter.CoverageHTMLReporter; import org.utplsql.api.reporter.Reporter; import org.utplsql.api.reporter.ReporterFactory; import org.utplsql.cli.exception.DatabaseConnectionFailed; @@ -15,6 +14,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; +import java.nio.file.Paths; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; @@ -219,6 +219,13 @@ private List initReporters( Connection conn, List rep for (ReporterOptions ro : reporterOptionsList) { Reporter reporter = ReporterFactory.createReporter(ro.getReporterName()); + + // Quick-hack for CoverageHTML Reporter + if ( reporter instanceof CoverageHTMLReporter && ro.outputToFile() ) { + ((CoverageHTMLReporter)reporter).setAssetsPath(ro.getOutputFileName()+"_assets/"); + CoverageHTMLReporter.writeReportAssetsTo(Paths.get(ro.getOutputFileName()+"_assets/")); + } + reporter.init(conn); ro.setReporterObj(reporter); reporterList.add(reporter); diff --git a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java index c70b6e2..979af9e 100644 --- a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java +++ b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java @@ -2,14 +2,17 @@ import org.junit.jupiter.api.Test; +import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * System tests for Code Coverage Reporter @@ -83,4 +86,25 @@ public void run_CodeCoverageWithIncludeAndExclude() throws Exception { } } + + @Test + public void coverageReporterWriteAssetsToOutput() throws Exception { + Path coveragePath = getTempCoverageFilePath(); + + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), + "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s"); + try { + int result = runCmd.run(); + + List reporterOptions = runCmd.getReporterOptionsList(); + File applicationJs = coveragePath.resolve(Paths.get("assets", "application.js")).toFile(); + + assertTrue(applicationJs.exists()); + + } finally { + if ( Files.exists(coveragePath)) + Files.delete(coveragePath); + } + + } } From 88c06885469b60dbc50593e4a57b71f7236c5bcb Mon Sep 17 00:00:00 2001 From: Samuel Nitsche Date: Fri, 9 Feb 2018 09:53:31 +0100 Subject: [PATCH 3/4] Fix and refactor CoverageHTMLTests --- .../cli/RunCommandCoverageReporterIT.java | 67 +++++++++++++------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java index 979af9e..cb7ce2e 100644 --- a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java +++ b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java @@ -1,13 +1,16 @@ package org.utplsql.cli; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; +import java.util.HashSet; import java.util.Scanner; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -23,6 +26,12 @@ public class RunCommandCoverageReporterIT { private static final Pattern REGEX_COVERAGE_TITLE = Pattern.compile("([a-zA-Z0-9\\._]+)<\\/a>"); + private Set tempPaths; + + private void addTempPath(Path path) { + tempPaths.add(path); + } + private String getTempCoverageFileName(int counter) { return "tmpCoverage_" + String.valueOf(System.currentTimeMillis()) + "_" + String.valueOf(counter) + ".html"; @@ -34,15 +43,19 @@ private String getTempCoverageFileName(int counter) { * @return */ private Path getTempCoverageFilePath() { + int i = 1; Path p = Paths.get(getTempCoverageFileName(i)); - while (Files.exists(p) && i < 100) + while ((Files.exists(p) || tempPaths.contains(p)) && i < 100) p = Paths.get(getTempCoverageFileName(i++)); if (i >= 100) throw new IllegalStateException("Could not get temporary file for coverage output"); + addTempPath(p); + addTempPath(Paths.get(p.toString()+"_assets")); + return p; } @@ -64,6 +77,11 @@ private boolean hasCoverageListed(String content, String packageName) { return false; } + @BeforeEach + public void setupTest() { + tempPaths = new HashSet<>(); + } + @Test public void run_CodeCoverageWithIncludeAndExclude() throws Exception { @@ -72,39 +90,48 @@ public void run_CodeCoverageWithIncludeAndExclude() throws Exception { RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s", "-exclude=app.award_bonus,app.betwnstr"); - try { - int result = runCmd.run(); - String content = new Scanner(coveragePath).useDelimiter("\\Z").next(); + int result = runCmd.run(); - assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name")); - assertEquals(false, hasCoverageListed(content, "app.award_bonus")); - assertEquals(false, hasCoverageListed(content, "app.betwnstr")); + String content = new Scanner(coveragePath).useDelimiter("\\Z").next(); - } finally { - Files.delete(coveragePath); - } + assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name")); + assertEquals(false, hasCoverageListed(content, "app.award_bonus")); + assertEquals(false, hasCoverageListed(content, "app.betwnstr")); } @Test public void coverageReporterWriteAssetsToOutput() throws Exception { Path coveragePath = getTempCoverageFilePath(); + Path coverageAssetsPath = Paths.get(coveragePath.toString() + "_assets"); RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s"); - try { - int result = runCmd.run(); - List reporterOptions = runCmd.getReporterOptionsList(); - File applicationJs = coveragePath.resolve(Paths.get("assets", "application.js")).toFile(); + runCmd.run(); - assertTrue(applicationJs.exists()); + File applicationJs = coverageAssetsPath.resolve(Paths.get("application.js")).toFile(); - } finally { - if ( Files.exists(coveragePath)) - Files.delete(coveragePath); - } + assertTrue(applicationJs.exists()); + + + } + @AfterEach + public void deleteTempFiles() { + tempPaths.forEach(p -> deleteDir(p.toFile())); + } + + void deleteDir(File file) { + if (file.exists()) { + File[] contents = file.listFiles(); + if (contents != null) { + for (File f : contents) { + deleteDir(f); + } + } + file.delete(); + } } } From 72659931eb064221b5ed3330c386a33fba7d2bc8 Mon Sep 17 00:00:00 2001 From: pesse Date: Sat, 10 Feb 2018 23:00:31 +0100 Subject: [PATCH 4/4] Add test to check HTML output, too --- .../java/org/utplsql/cli/RunCommandCoverageReporterIT.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java index cb7ce2e..52932bb 100644 --- a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java +++ b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java @@ -111,11 +111,13 @@ public void coverageReporterWriteAssetsToOutput() throws Exception { runCmd.run(); + // Check application file exists File applicationJs = coverageAssetsPath.resolve(Paths.get("application.js")).toFile(); - assertTrue(applicationJs.exists()); - + // Check correct script-part in HTML source exists + String content = new Scanner(coveragePath).useDelimiter("\\Z").next(); + assertTrue(content.contains("