Skip to content

Commit db9be5d

Browse files
authored
Merge pull request #62 from utPLSQL/bugfix/provide_coverage_html_assets
Bugfix/provide coverage html assets
2 parents 5ba83f4 + 7265993 commit db9be5d

11 files changed

+301
-186
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ install:
3434

3535
script:
3636
- mvn package -DskipTests
37-
- mvn package jar:jar appassembler:assemble
37+
- mvn package verify jar:jar appassembler:assemble
3838

3939
before_deploy:
4040
- bash .travis/create_release.sh

pom.xml

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010
<name>cli</name>
1111
<url>http://maven.apache.org</url>
1212

13-
<properties>
14-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<maven.compiler.source>1.8</maven.compiler.source>
16-
<maven.compiler.target>1.8</maven.compiler.target>
17-
</properties>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<junit.platform.version>1.0.3</junit.platform.version>
18+
<junit.jupiter.version>5.0.3</junit.jupiter.version>
19+
</properties>
1820

1921
<dependencies>
2022
<dependency>
2123
<groupId>org.utplsql</groupId>
2224
<artifactId>java-api</artifactId>
23-
<version>3.0.4-SNAPSHOT</version>
25+
<version>3.0.5-SNAPSHOT</version>
2426
<scope>compile</scope>
2527
<exclusions>
2628
<exclusion>
@@ -48,11 +50,17 @@
4850
<scope>compile</scope>
4951
</dependency>
5052
<dependency>
51-
<groupId>junit</groupId>
52-
<artifactId>junit</artifactId>
53-
<version>4.12</version>
53+
<groupId>org.junit.jupiter</groupId>
54+
<artifactId>junit-jupiter-api</artifactId>
55+
<version>${junit.jupiter.version}</version>
5456
<scope>test</scope>
55-
</dependency>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.junit.jupiter</groupId>
60+
<artifactId>junit-jupiter-engine</artifactId>
61+
<version>${junit.jupiter.version}</version>
62+
<scope>test</scope>
63+
</dependency>
5664
</dependencies>
5765

5866
<build>
@@ -75,6 +83,43 @@
7583
</programs>
7684
</configuration>
7785
</plugin>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-surefire-plugin</artifactId>
89+
<version>2.19.1</version>
90+
<configuration>
91+
<excludes>
92+
<exclude>**/*IT.java</exclude>
93+
</excludes>
94+
</configuration>
95+
<dependencies>
96+
<dependency>
97+
<groupId>org.junit.platform</groupId>
98+
<artifactId>junit-platform-surefire-provider</artifactId>
99+
<version>${junit.platform.version}</version>
100+
</dependency>
101+
</dependencies>
102+
</plugin>
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-failsafe-plugin</artifactId>
106+
<version>2.19.1</version>
107+
<executions>
108+
<execution>
109+
<goals>
110+
<goal>integration-test</goal>
111+
<goal>verify</goal>
112+
</goals>
113+
</execution>
114+
</executions>
115+
<dependencies>
116+
<dependency>
117+
<groupId>org.junit.platform</groupId>
118+
<artifactId>junit-platform-surefire-provider</artifactId>
119+
<version>${junit.platform.version}</version>
120+
</dependency>
121+
</dependencies>
122+
</plugin>
78123
</plugins>
79124
</build>
80125

@@ -93,4 +138,26 @@
93138
</repository>
94139
</repositories>
95140

141+
<profiles>
142+
<profile>
143+
<id>utPLSQL-local</id>
144+
<build>
145+
<pluginManagement>
146+
<plugins>
147+
<plugin>
148+
<groupId>org.apache.maven.plugins</groupId>
149+
<artifactId>maven-failsafe-plugin</artifactId>
150+
<configuration>
151+
<environmentVariables>
152+
<DB_URL>${dbUrl}</DB_URL>
153+
<DB_USER>${dbUser}</DB_USER>
154+
<DB_PASS>${dbPass}</DB_PASS>
155+
</environmentVariables>
156+
</configuration>
157+
</plugin>
158+
</plugins>
159+
</pluginManagement>
160+
</build>
161+
</profile>
162+
</profiles>
96163
</project>

src/main/java/org/utplsql/cli/RunCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
import com.beust.jcommander.Parameters;
55
import org.utplsql.api.*;
66
import org.utplsql.api.compatibility.CompatibilityProxy;
7-
import org.utplsql.api.compatibility.OptionalFeatures;
8-
import org.utplsql.api.exception.DatabaseNotCompatibleException;
97
import org.utplsql.api.exception.SomeTestsFailedException;
8+
import org.utplsql.api.reporter.CoverageHTMLReporter;
109
import org.utplsql.api.reporter.Reporter;
1110
import org.utplsql.api.reporter.ReporterFactory;
1211
import org.utplsql.cli.exception.DatabaseConnectionFailed;
@@ -15,6 +14,7 @@
1514
import java.io.FileNotFoundException;
1615
import java.io.FileOutputStream;
1716
import java.io.PrintStream;
17+
import java.nio.file.Paths;
1818
import java.sql.Connection;
1919
import java.sql.SQLException;
2020
import java.util.ArrayList;
@@ -219,6 +219,13 @@ private List<Reporter> initReporters( Connection conn, List<ReporterOptions> rep
219219

220220
for (ReporterOptions ro : reporterOptionsList) {
221221
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
222+
223+
// Quick-hack for CoverageHTML Reporter
224+
if ( reporter instanceof CoverageHTMLReporter && ro.outputToFile() ) {
225+
((CoverageHTMLReporter)reporter).setAssetsPath(ro.getOutputFileName()+"_assets/");
226+
CoverageHTMLReporter.writeReportAssetsTo(Paths.get(ro.getOutputFileName()+"_assets/"));
227+
}
228+
222229
reporter.init(conn);
223230
ro.setReporterObj(reporter);
224231
reporterList.add(reporter);

src/test/java/org/utplsql/cli/FileWalkerTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.utplsql.cli;
22

3-
import org.junit.Assert;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
54

65
import java.io.File;
76
import java.util.Collections;
87
import java.util.List;
98

9+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
10+
1011
/**
1112
* Created by Vinicius on 18/06/2017.
1213
*/
@@ -18,7 +19,7 @@ public class FileWalkerTest {
1819
public void fileWalker_Relative() {
1920
List<String> fileList = new FileWalker().getFileList(BASE_DIR, "source");
2021
Collections.sort(fileList);
21-
Assert.assertArrayEquals(new Object[] {
22+
assertArrayEquals(new Object[] {
2223
"source/packages/package.pkb".replace('/', File.separatorChar),
2324
"source/packages/package.pks".replace('/', File.separatorChar),
2425
"source/script.sql".replace('/', File.separatorChar),
@@ -30,7 +31,7 @@ public void fileWalker_Relative() {
3031
public void fileWalker_Absolute() {
3132
List<String> fileList = new FileWalker().getFileList(BASE_DIR, "source", false);
3233
Collections.sort(fileList);
33-
Assert.assertArrayEquals(new Object[] {
34+
assertArrayEquals(new Object[] {
3435
BASE_DIR.getAbsolutePath() + "/source/packages/package.pkb".replace('/', File.separatorChar),
3536
BASE_DIR.getAbsolutePath() + "/source/packages/package.pks".replace('/', File.separatorChar),
3637
BASE_DIR.getAbsolutePath() + "/source/script.sql".replace('/', File.separatorChar),
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)