|
| 1 | +package org.utplsql.cli; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.Scanner; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.regex.Matcher; |
| 15 | +import java.util.regex.Pattern; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 19 | + |
| 20 | +/** |
| 21 | + * System tests for Code Coverage Reporter |
| 22 | + * |
| 23 | + * @author pesse |
| 24 | + */ |
| 25 | +public class RunCommandCoverageReporterIT { |
| 26 | + |
| 27 | + private static final Pattern REGEX_COVERAGE_TITLE = Pattern.compile("<a href=\"[a-zA-Z0-9#]+\" class=\"src_link\" title=\"[a-zA-Z\\._]+\">([a-zA-Z0-9\\._]+)<\\/a>"); |
| 28 | + |
| 29 | + private Set<Path> tempPaths; |
| 30 | + |
| 31 | + private void addTempPath(Path path) { |
| 32 | + tempPaths.add(path); |
| 33 | + } |
| 34 | + |
| 35 | + private String getTempCoverageFileName(int counter) { |
| 36 | + |
| 37 | + return "tmpCoverage_" + String.valueOf(System.currentTimeMillis()) + "_" + String.valueOf(counter) + ".html"; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns a random filename which does not yet exist on the local path |
| 42 | + * |
| 43 | + * @return |
| 44 | + */ |
| 45 | + private Path getTempCoverageFilePath() { |
| 46 | + |
| 47 | + int i = 1; |
| 48 | + Path p = Paths.get(getTempCoverageFileName(i)); |
| 49 | + |
| 50 | + while ((Files.exists(p) || tempPaths.contains(p)) && i < 100) |
| 51 | + p = Paths.get(getTempCoverageFileName(i++)); |
| 52 | + |
| 53 | + if (i >= 100) |
| 54 | + throw new IllegalStateException("Could not get temporary file for coverage output"); |
| 55 | + |
| 56 | + addTempPath(p); |
| 57 | + addTempPath(Paths.get(p.toString()+"_assets")); |
| 58 | + |
| 59 | + return p; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Checks Coverage HTML Output if a given packageName is listed |
| 64 | + * |
| 65 | + * @param content |
| 66 | + * @param packageName |
| 67 | + * @return |
| 68 | + */ |
| 69 | + private boolean hasCoverageListed(String content, String packageName) { |
| 70 | + Matcher m = REGEX_COVERAGE_TITLE.matcher(content); |
| 71 | + |
| 72 | + while (m.find()) { |
| 73 | + if (packageName.equals(m.group(1))) |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + @BeforeEach |
| 81 | + public void setupTest() { |
| 82 | + tempPaths = new HashSet<>(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void run_CodeCoverageWithIncludeAndExclude() throws Exception { |
| 87 | + |
| 88 | + Path coveragePath = getTempCoverageFilePath(); |
| 89 | + |
| 90 | + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), |
| 91 | + "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s", "-exclude=app.award_bonus,app.betwnstr"); |
| 92 | + |
| 93 | + |
| 94 | + int result = runCmd.run(); |
| 95 | + |
| 96 | + String content = new Scanner(coveragePath).useDelimiter("\\Z").next(); |
| 97 | + |
| 98 | + assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name")); |
| 99 | + assertEquals(false, hasCoverageListed(content, "app.award_bonus")); |
| 100 | + assertEquals(false, hasCoverageListed(content, "app.betwnstr")); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void coverageReporterWriteAssetsToOutput() throws Exception { |
| 106 | + Path coveragePath = getTempCoverageFilePath(); |
| 107 | + Path coverageAssetsPath = Paths.get(coveragePath.toString() + "_assets"); |
| 108 | + |
| 109 | + RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), |
| 110 | + "-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s"); |
| 111 | + |
| 112 | + runCmd.run(); |
| 113 | + |
| 114 | + // Check application file exists |
| 115 | + File applicationJs = coverageAssetsPath.resolve(Paths.get("application.js")).toFile(); |
| 116 | + assertTrue(applicationJs.exists()); |
| 117 | + |
| 118 | + // Check correct script-part in HTML source exists |
| 119 | + String content = new Scanner(coveragePath).useDelimiter("\\Z").next(); |
| 120 | + assertTrue(content.contains("<script src='" + coverageAssetsPath.toString() + "/application.js' type='text/javascript'>")); |
| 121 | + } |
| 122 | + |
| 123 | + @AfterEach |
| 124 | + public void deleteTempFiles() { |
| 125 | + tempPaths.forEach(p -> deleteDir(p.toFile())); |
| 126 | + } |
| 127 | + |
| 128 | + void deleteDir(File file) { |
| 129 | + if (file.exists()) { |
| 130 | + File[] contents = file.listFiles(); |
| 131 | + if (contents != null) { |
| 132 | + for (File f : contents) { |
| 133 | + deleteDir(f); |
| 134 | + } |
| 135 | + } |
| 136 | + file.delete(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments