-
Notifications
You must be signed in to change notification settings - Fork 348
/
AbstractPitAggregationReportMojo.java
141 lines (120 loc) · 4.95 KB
/
AbstractPitAggregationReportMojo.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
package org.pitest.maven.report;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.util.FileUtils;
import org.pitest.aggregate.ReportAggregator;
import org.pitest.functional.FCollection;
import org.pitest.maven.DependencyFilter;
import org.pitest.mutationtest.config.DirectoryResultOutputStrategy;
import org.pitest.mutationtest.config.PluginServices;
import org.pitest.mutationtest.config.UndatedReportDirCreationStrategy;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
/**
* Common code for report aggregation mojo.
*/
abstract class AbstractPitAggregationReportMojo extends PitReportMojo {
private static final String REPORT_DIR_RELATIVE_TO_PROJECT = "target/pit-reports";
private static final String MUTATION_RESULT_FILTER = "mutations.xml";
private static final String LINECOVERAGE_FILTER = "linecoverage.xml";
/**
* The projects in the reactor.
*/
@Parameter(property = "reactorProjects", readonly = true)
List<MavenProject> reactorProjects;
private final ReportSourceLocator reportSourceLocator = new ReportSourceLocator();
/**
* @return projects to inspect for report files.
*/
abstract Collection<MavenProject> findDependencies();
@Override
protected void executeReport(final Locale locale)
throws MavenReportException {
if (!canGenerateReport()) {
getLog().info("Skipping");
return;
}
try {
final Collection<MavenProject> allProjects = findDependencies();
final ReportAggregator.Builder reportAggregationBuilder = ReportAggregator
.builder();
for (final MavenProject proj : allProjects) {
addProjectFiles(reportAggregationBuilder, proj);
}
final ReportAggregator reportAggregator = reportAggregationBuilder
.inputCharSet(Charset.forName(this.getInputEncoding()))
.outputCharset(Charset.forName(this.getOutputEncoding()))
.resultOutputStrategy(new DirectoryResultOutputStrategy(
getReportsDirectory().getAbsolutePath(),
new UndatedReportDirCreationStrategy()))
.build();
reportAggregator.aggregateReport();
} catch (final Exception e) {
throw new MavenReportException(e.getMessage(), e);
}
}
private void addProjectFiles(
final ReportAggregator.Builder reportAggregationBuilder,
final MavenProject proj) throws Exception {
final File projectBaseDir = proj.getBasedir();
List<File> files = getProjectFilesByFilter(projectBaseDir,
MUTATION_RESULT_FILTER);
for (final File file : files) {
reportAggregationBuilder.addMutationResultsFile(file);
}
files = getProjectFilesByFilter(projectBaseDir, LINECOVERAGE_FILTER);
for (final File file : files) {
reportAggregationBuilder.addLineCoverageFile(file);
}
files = convertToRootDirs(proj.getCompileSourceRoots(),
proj.getTestCompileSourceRoots());
for (final File file : files) {
reportAggregationBuilder.addSourceCodeDirectory(file);
}
files = getCompiledDirs(proj);
for (final File file : files) {
reportAggregationBuilder.addCompiledCodeDirectory(file);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private List<File> convertToRootDirs(final List... directoryLists) {
final List<String> roots = new ArrayList<>();
for (final List directoryList : directoryLists) {
roots.addAll(directoryList);
}
return FCollection.map(roots, File::new);
}
private List<File> getProjectFilesByFilter(final File projectBaseDir,
final String filter) throws IOException {
File reportsDir = projectBaseDir.toPath().resolve(REPORT_DIR_RELATIVE_TO_PROJECT).toFile();
if (!reportsDir.exists()) {
return new ArrayList<>();
}
File latestReportDir = reportSourceLocator.locate(reportsDir, getLog());
final List<File> files = FileUtils.getFiles(latestReportDir, filter, "");
return files == null ? new ArrayList<>() : files;
}
@SuppressWarnings("unchecked")
private List<File> getCompiledDirs(final MavenProject project)
throws Exception {
final List<String> sourceRoots = new ArrayList<>();
for (final Object artifactObj : FCollection
.filter(project.getPluginArtifactMap().values(), new DependencyFilter(
PluginServices.makeForLoader(this.getClass().getClassLoader())))) {
final Artifact artifact = (Artifact) artifactObj;
sourceRoots.add(artifact.getFile().getAbsolutePath());
}
return convertToRootDirs(project.getTestCompileSourceRoots(),
Arrays.asList(project.getBuild().getOutputDirectory(),
project.getBuild().getTestOutputDirectory()),
sourceRoots);
}
}