Skip to content

Commit b7f99e2

Browse files
pesseSamuel Nitsche
authored andcommitted
Added system-test for code coverage creation
Also separated unit- and system-tests for RunCommand and refactored RunCommand a bit to use the fluent api provided
1 parent daf939b commit b7f99e2

File tree

6 files changed

+170
-60
lines changed

6 files changed

+170
-60
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.utplsql</groupId>
2222
<artifactId>java-api</artifactId>
23-
<version>3.0.4</version>
23+
<version>3.0.4-SNAPSHOT</version>
2424
<scope>compile</scope>
2525
<exclusions>
2626
<exclusion>

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,9 @@ public int run() throws Exception {
184184
.testMappingOptions(testMappingOptions[0])
185185
.colorConsole(this.colorConsole)
186186
.failOnErrors(true)
187-
.skipCompatibilityCheck(skipCompatibilityCheck);
188-
189-
for (String includeObject: finalIncludeObjectsList){
190-
testRunner.includeObject(includeObject);
191-
}
192-
for (String excludeObject: finalExcludeObjectsList){
193-
testRunner.excludeObject(excludeObject);
194-
}
187+
.skipCompatibilityCheck(skipCompatibilityCheck)
188+
.includeObjects(finalIncludeObjectsList)
189+
.excludeObjects(finalExcludeObjectsList);
195190

196191
testRunner.run(conn);
197192
} catch (SomeTestsFailedException e) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.utplsql.cli;
2+
3+
import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.utplsql.api.compatibility.OptionalFeatures;
7+
8+
import java.io.File;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.Scanner;
13+
import java.util.regex.Matcher;
14+
import java.util.regex.Pattern;
15+
16+
/**
17+
* System tests for Code Coverage Reporter
18+
*
19+
* @author pesse
20+
*/
21+
public class RunCommandCoverageReporterSystemTest {
22+
23+
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>");
24+
25+
private String getTempCoverageFileName(int counter) {
26+
27+
return "tmpCoverage_" + String.valueOf(System.currentTimeMillis()) + "_" + String.valueOf(counter) + ".html";
28+
}
29+
30+
/**
31+
* Returns a random filename which does not yet exist on the local path
32+
*
33+
* @return
34+
*/
35+
private Path getTempCoverageFilePath() {
36+
int i = 1;
37+
Path p = Paths.get(getTempCoverageFileName(i));
38+
39+
while (Files.exists(p) && i < 100)
40+
p = Paths.get(getTempCoverageFileName(i++));
41+
42+
if (i >= 100)
43+
throw new IllegalStateException("Could not get temporary file for coverage output");
44+
45+
return p;
46+
}
47+
48+
/** Checks Coverage HTML Output if a given packageName is listed
49+
*
50+
* @param content
51+
* @param packageName
52+
* @return
53+
*/
54+
private boolean hasCoverageListed( String content, String packageName) {
55+
Matcher m = REGEX_COVERAGE_TITLE.matcher(content);
56+
57+
while ( m.find() ) {
58+
if ( packageName.equals(m.group(1)) )
59+
return true;
60+
}
61+
62+
return false;
63+
}
64+
65+
@Test
66+
public void run_CodeCoverageWithIncludeAndExclude() {
67+
68+
try {
69+
Path coveragePath = getTempCoverageFilePath();
70+
71+
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(),
72+
"-f=ut_coverage_html_reporter", "-o=" + coveragePath, "-s", "-exclude=app.award_bonus,app.betwnstr");
73+
74+
try {
75+
int result = runCmd.run();
76+
77+
String content = new Scanner(coveragePath).useDelimiter("\\Z").next();
78+
79+
Assert.assertEquals(true, hasCoverageListed(content, "app.remove_rooms_by_name"));
80+
Assert.assertEquals(false, hasCoverageListed(content, "app.award_bonus"));
81+
Assert.assertEquals(false, hasCoverageListed(content, "app.betwnstr"));
82+
83+
} finally {
84+
Files.delete(coveragePath);
85+
}
86+
} catch (Exception e) {
87+
Assert.fail(e.getMessage());
88+
}
89+
90+
}
91+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.utplsql.cli;
2+
3+
import com.beust.jcommander.JCommander;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.utplsql.api.CustomTypes;
7+
import org.utplsql.api.compatibility.OptionalFeatures;
8+
9+
import java.util.List;
10+
11+
/**
12+
* System tests for run command.
13+
*/
14+
public class RunCommandSystemTest {
15+
16+
17+
@Test
18+
public void run_Default() {
19+
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(),
20+
"-f=ut_documentation_reporter",
21+
"-c",
22+
"--failure-exit-code=2");
23+
24+
try {
25+
int result = runCmd.run();
26+
27+
// Only expect failure-exit-code to work on several framework versions
28+
if (OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(runCmd.getDatabaseVersion()) )
29+
Assert.assertEquals(2, result);
30+
else
31+
Assert.assertEquals(0, result);
32+
}
33+
catch ( Exception e ) {
34+
Assert.fail(e.getMessage());
35+
}
36+
}
37+
38+
39+
40+
}

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

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,9 @@
1313
*/
1414
public class RunCommandTest {
1515

16-
private static String sUrl;
17-
private static String sUser;
18-
private static String sPass;
19-
20-
static {
21-
sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE";
22-
sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app";
23-
sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app";
24-
}
25-
26-
private RunCommand createRunCommand(String... args) {
27-
RunCommand runCmd = new RunCommand();
28-
29-
JCommander.newBuilder()
30-
.addObject(runCmd)
31-
.args(args)
32-
.build();
33-
34-
return runCmd;
35-
}
36-
37-
private String getConnectionString() {
38-
return sUser + "/" + sPass + "@" + sUrl;
39-
}
40-
4116
@Test
4217
public void reporterOptions_Default() {
43-
RunCommand runCmd = createRunCommand(getConnectionString());
18+
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString());
4419

4520
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
4621

@@ -53,7 +28,7 @@ public void reporterOptions_Default() {
5328

5429
@Test
5530
public void reporterOptions_OneReporter() {
56-
RunCommand runCmd = createRunCommand(getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt");
31+
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt");
5732

5833
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
5934

@@ -66,7 +41,7 @@ public void reporterOptions_OneReporter() {
6641

6742
@Test
6843
public void reporterOptions_OneReporterForceScreen() {
69-
RunCommand runCmd = createRunCommand(getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt", "-s");
44+
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-o=output.txt", "-s");
7045

7146
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
7247

@@ -79,7 +54,7 @@ public void reporterOptions_OneReporterForceScreen() {
7954

8055
@Test
8156
public void reporterOptions_OneReporterForceScreenInverse() {
82-
RunCommand runCmd = createRunCommand(getConnectionString(), "-f=ut_documentation_reporter", "-s", "-o=output.txt");
57+
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(), "-f=ut_documentation_reporter", "-s", "-o=output.txt");
8358

8459
List<ReporterOptions> reporterOptionsList = runCmd.getReporterOptionsList();
8560

@@ -92,7 +67,7 @@ public void reporterOptions_OneReporterForceScreenInverse() {
9267

9368
@Test
9469
public void reporterOptions_TwoReporters() {
95-
RunCommand runCmd = createRunCommand(getConnectionString(),
70+
RunCommand runCmd = RunCommandTestHelper.createRunCommand(RunCommandTestHelper.getConnectionString(),
9671
"-f=ut_documentation_reporter",
9772
"-f=ut_coverage_html_reporter", "-o=coverage.html", "-s");
9873

@@ -111,25 +86,4 @@ public void reporterOptions_TwoReporters() {
11186
Assert.assertTrue(reporterOptions2.outputToScreen());
11287
}
11388

114-
@Test
115-
public void run_Default() {
116-
RunCommand runCmd = createRunCommand(getConnectionString(),
117-
"-f=ut_documentation_reporter",
118-
"-c",
119-
"--failure-exit-code=2");
120-
121-
try {
122-
int result = runCmd.run();
123-
124-
// Only expect failure-exit-code to work on several framework versions
125-
if (OptionalFeatures.FAIL_ON_ERROR.isAvailableFor(runCmd.getDatabaseVersion()) )
126-
Assert.assertEquals(2, result);
127-
else
128-
Assert.assertEquals(0, result);
129-
}
130-
catch ( Exception e ) {
131-
Assert.fail(e.getMessage());
132-
}
133-
}
134-
13589
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.utplsql.cli;
2+
3+
import com.beust.jcommander.JCommander;
4+
5+
class RunCommandTestHelper {
6+
private static String sUrl;
7+
private static String sUser;
8+
private static String sPass;
9+
10+
static {
11+
sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE";
12+
sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app";
13+
sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app";
14+
}
15+
16+
static RunCommand createRunCommand(String... args) {
17+
RunCommand runCmd = new RunCommand();
18+
19+
JCommander.newBuilder()
20+
.addObject(runCmd)
21+
.args(args)
22+
.build();
23+
24+
return runCmd;
25+
}
26+
27+
static String getConnectionString() {
28+
return sUser + "/" + sPass + "@" + sUrl;
29+
}
30+
}

0 commit comments

Comments
 (0)