forked from scoverage/gradle-scoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoverageFunctionalTest.java
201 lines (137 loc) · 5.83 KB
/
ScoverageFunctionalTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package org.scoverage;
import groovy.util.Node;
import groovy.util.XmlParser;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.BuildTask;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Assert;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import static org.hamcrest.number.IsCloseTo.closeTo;
import static org.junit.Assert.assertThat;
public abstract class ScoverageFunctionalTest {
private final String projectName;
private final GradleRunner runner;
private final XmlParser parser;
protected ScoverageFunctionalTest(String projectName) {
this.projectName = projectName;
this.runner = GradleRunner.create()
.withProjectDir(projectDir())
.withPluginClasspath()
.forwardOutput();
try {
this.parser = new XmlParser();
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected File projectDir() {
return new File("src/functionalTest/resources/projects/" + projectName);
}
protected File buildDir() {
return buildDir(projectDir());
}
protected File buildDir(File projectDir) {
return projectDir.toPath().resolve("build").toFile();
}
protected File reportDir() {
return reportDir(projectDir());
}
protected File reportDir(File projectDir) {
return buildDir(projectDir).toPath().resolve(ScoveragePlugin.getDEFAULT_REPORT_DIR()).toFile();
}
protected AssertableBuildResult run(String... arguments) {
configureArguments(arguments);
return new AssertableBuildResult(runner.build());
}
protected AssertableBuildResult runAndFail(String... arguments) {
configureArguments(arguments);
return new AssertableBuildResult(runner.buildAndFail());
}
protected AssertableBuildResult dryRun(String... arguments) {
List<String> withDryArgument = new ArrayList<String>(Arrays.asList(arguments));
withDryArgument.add("--dry-run");
return run(withDryArgument.toArray(new String[]{}));
}
protected void assertCoverage(Double expected) throws Exception {
assertCoverage(expected, reportDir());
}
protected void assertCoverage(Double expected, File reportDir) throws Exception {
assertThat(coverage(reportDir, CoverageType.Statement), closeTo(expected, 1.0));
assertThat(coverage(reportDir, CoverageType.Line), closeTo(expected, 1.0));
}
protected File resolve(File file, String relativePath) {
return file.toPath().resolve(relativePath).toFile();
}
private Double coverage(File reportDir, CoverageType coverageType) throws IOException, SAXException, ParseException {
File reportFile = reportDir.toPath().resolve(coverageType.getFileName()).toFile();
Node xml = parser.parse(reportFile);
Object attribute = xml.attribute(coverageType.getParamName());
double rawValue = NumberFormat.getInstance().parse(attribute.toString()).doubleValue();
return coverageType.normalize(rawValue) * 100.0;
}
private void configureArguments(String... arguments) {
List<String> fullArguments = new ArrayList<String>();
fullArguments.add("-PscalaVersionMajor=2");
fullArguments.add("-PscalaVersionMinor=12");
fullArguments.add("-PscalaVersionBuild=8");
fullArguments.add("-PjunitVersion=5.3.2");
fullArguments.add("-PjunitPlatformVersion=1.3.2");
fullArguments.add("-PscalatestVersion=3.0.8");
fullArguments.addAll(Arrays.asList(arguments));
runner.withArguments(fullArguments);
}
protected static class AssertableBuildResult {
private final BuildResult result;
private AssertableBuildResult(BuildResult result) {
this.result = result;
}
public BuildResult getResult() {
return result;
}
public void assertNoTasks() {
Assert.assertEquals(0, result.getTasks().size());
}
public void assertTaskExists(String taskName) {
Assert.assertTrue(taskExists(taskName));
}
public void assertTaskDoesntExist(String taskName) {
Assert.assertFalse(taskExists(taskName));
}
public void assertTaskSkipped(String taskName) {
BuildTask task = getTask(taskName);
Assert.assertTrue(task == null || task.getOutcome() == TaskOutcome.SKIPPED);
}
public void assertTaskSucceeded(String taskName) {
assertTaskOutcome(taskName, TaskOutcome.SUCCESS);
}
public void assertTaskFailed(String taskName) {
assertTaskOutcome(taskName, TaskOutcome.FAILED);
}
public void assertTaskOutcome(String taskName, TaskOutcome outcome) {
BuildTask task = getTask(taskName);
Assert.assertNotNull(task);
Assert.assertEquals(outcome, task.getOutcome());
}
private BuildTask getTask(String taskName) {
return result.task(fullTaskName(taskName));
}
private String fullTaskName(String taskName) {
return ":" + taskName;
}
private boolean taskExists(String taskName) {
Pattern regex = Pattern.compile("^(> Task )?" + fullTaskName(taskName), Pattern.MULTILINE);
return regex.matcher(result.getOutput()).find();
}
}
}