-
Notifications
You must be signed in to change notification settings - Fork 98
/
JGivenPlugin.java
98 lines (85 loc) · 4.27 KB
/
JGivenPlugin.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
package com.tngtech.jgiven.gradle;
import com.tngtech.jgiven.gradle.internal.JGivenHtmlReportImpl;
import com.tngtech.jgiven.impl.Config;
import com.tngtech.jgiven.impl.util.WordUtil;
import java.io.File;
import java.util.Objects;
import java.util.concurrent.Callable;
import org.gradle.api.Action;
import org.gradle.api.NonNullApi;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.internal.ConventionMapping;
import org.gradle.api.internal.IConventionAware;
import org.gradle.api.plugins.ReportingBasePlugin;
import org.gradle.api.reporting.Report;
import org.gradle.api.reporting.ReportingExtension;
import org.gradle.api.tasks.testing.Test;
@NonNullApi
public class JGivenPlugin implements Plugin<Project> {
@Override
public void apply(final Project project) {
project.getPluginManager().apply(ReportingBasePlugin.class);
addTaskExtension(project);
addDefaultReports(project);
configureJGivenReportDefaults(project);
}
private void addTaskExtension(Project project) {
project.getTasks().withType(Test.class).configureEach(this::applyTo);
}
private void applyTo(Test test) {
final String testName = test.getName();
final JGivenTaskExtension extension = test.getExtensions().create("jgiven", JGivenTaskExtension.class);
final Project project = test.getProject();
((IConventionAware) extension).getConventionMapping().map("resultsDir",
(Callable<File>) () -> project.file(project.getBuildDir() + "/jgiven-results/" + testName));
File resultsDir = extension.getResultsDir();
if (resultsDir != null) {
test.getOutputs().dir(resultsDir).withPropertyName("jgiven.resultsDir");
}
/* Java lambda classes are created at runtime with a non-deterministic classname.
* Therefore, the class name does not identify the implementation of the lambda,
* and changes between different Gradle runs.
* See: https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:how_does_it_work
*/
//noinspection Convert2Lambda
test.prependParallelSafeAction(new Action<Task>() {
@Override
public void execute(Task task) {
((Test) task).systemProperty(Config.JGIVEN_REPORT_DIR, extension.getResultsDir().getAbsolutePath());
}
});
}
private void configureJGivenReportDefaults(Project project) {
project.getTasks()
.withType(JGivenReportTask.class).forEach(reportTask ->
reportTask.getReports().all((Action<Report>) report ->
report.getRequired().convention(report.getName().equals(JGivenHtmlReportImpl.NAME))
));
}
private void addDefaultReports(final Project project) {
final ReportingExtension reportingExtension = Objects.requireNonNull(
project.getExtensions().findByType(ReportingExtension.class));
project.getTasks().withType(Test.class).forEach(test -> project.getTasks()
.register("jgiven" + WordUtil.capitalize(test.getName()) + "Report", JGivenReportTask.class)
.configure(reportTask -> configureDefaultReportTask(test, reportTask, reportingExtension))
);
}
private void configureDefaultReportTask(final Test test, JGivenReportTask reportTask,
final ReportingExtension reportingExtension) {
reportTask.mustRunAfter(test);
ConventionMapping mapping = ((IConventionAware) reportTask).getConventionMapping();
Callable<File> getResultsDirectory = () -> test.getExtensions()
.getByType(JGivenTaskExtension.class)
.getResultsDir();
mapping.map("results", getResultsDirectory);
Objects.requireNonNull(mapping.getConventionValue(reportTask.getReports(), "reports", false))
.all(report -> {
ConventionMapping reportMapping = ((IConventionAware) report).getConventionMapping();
String relativeFilePath = "jgiven" + "/" + test.getName() + "/" + report.getName();
Callable<File> getDestination = () -> reportingExtension.file(relativeFilePath);
reportMapping.map("destination", getDestination);
});
}
}