Skip to content

Commit 05e4681

Browse files
committed
[SUREFIRE-2227] Dynamically calculate xrefTestLocation
1 parent f1a419a commit 05e4681

File tree

19 files changed

+282
-57
lines changed

19 files changed

+282
-57
lines changed

maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/AbstractSurefireReport.java

+27-23
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
import java.net.URLClassLoader;
2626
import java.text.MessageFormat;
2727
import java.util.ArrayList;
28+
import java.util.Collections;
2829
import java.util.Iterator;
2930
import java.util.List;
3031
import java.util.Locale;
3132
import java.util.MissingResourceException;
3233
import java.util.ResourceBundle;
3334

3435
import org.apache.maven.model.ReportPlugin;
36+
import org.apache.maven.model.Reporting;
3537
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
3638
import org.apache.maven.plugins.annotations.Component;
3739
import org.apache.maven.plugins.annotations.Parameter;
@@ -77,23 +79,19 @@ public abstract class AbstractSurefireReport extends AbstractMavenReport {
7779
private File reportsDirectory;
7880

7981
/**
80-
* The projects in the reactor for aggregation report.
82+
* Link the violation line numbers to the (Test) Source XRef. Links will be created automatically if the JXR plugin is
83+
* being used.
8184
*/
82-
@Parameter(defaultValue = "${reactorProjects}", readonly = true)
83-
private List<MavenProject> reactorProjects;
84-
85-
/**
86-
* Link the failed tests line numbers to the source xref. Will link
87-
* automatically if Maven JXR plugin is being used.
88-
*/
89-
@Parameter(defaultValue = "true", property = "linkXRef")
85+
@Parameter(property = "linkXRef", defaultValue = "true")
9086
private boolean linkXRef;
9187

9288
/**
93-
* Location of the Xrefs to link.
89+
* Location where Test Source XRef is generated for this project.
90+
* <br>
91+
* <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref-test}
9492
*/
95-
@Parameter(defaultValue = "${project.reporting.outputDirectory}/xref-test")
96-
private File xrefLocation;
93+
@Parameter
94+
private File xrefTestLocation;
9795

9896
/**
9997
* Whether to build an aggregated report at the root, or build individual reports.
@@ -156,7 +154,7 @@ public void executeReport(Locale locale) {
156154
locale,
157155
getConsoleLogger(),
158156
getReportsDirectories(),
159-
determineXrefLocation(),
157+
constructXrefTestLocation(),
160158
showSuccess);
161159
r.render();
162160
}
@@ -258,26 +256,28 @@ private List<MavenProject> getProjectsWithoutRoot() {
258256
return result;
259257
}
260258

261-
private String determineXrefLocation() {
259+
private String constructXrefTestLocation() {
262260
String location = null;
263-
264261
if (linkXRef) {
262+
File xrefTestLocation = getXrefTestLocation();
263+
265264
String relativePath = PathTool.getRelativePath(
266-
getReportOutputDirectory().getAbsolutePath(), xrefLocation.getAbsolutePath());
265+
getReportOutputDirectory().getAbsolutePath(), xrefTestLocation.getAbsolutePath());
267266
if (relativePath == null || relativePath.isEmpty()) {
268267
relativePath = ".";
269268
}
270-
relativePath = relativePath + "/" + xrefLocation.getName();
271-
if (xrefLocation.exists()) {
269+
relativePath = relativePath + "/" + xrefTestLocation.getName();
270+
if (xrefTestLocation.exists()) {
272271
// XRef was already generated by manual execution of a lifecycle binding
273272
location = relativePath;
274273
} else {
275274
// Not yet generated - check if the report is on its way
276-
for (Object o : project.getReportPlugins()) {
277-
ReportPlugin report = (ReportPlugin) o;
278-
279-
String artifactId = report.getArtifactId();
280-
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
275+
Reporting reporting = project.getModel().getReporting();
276+
List<ReportPlugin> reportPlugins =
277+
reporting != null ? reporting.getPlugins() : Collections.<ReportPlugin>emptyList();
278+
for (ReportPlugin plugin : reportPlugins) {
279+
String artifactId = plugin.getArtifactId();
280+
if ("maven-jxr-plugin".equals(artifactId)) {
281281
location = relativePath;
282282
}
283283
}
@@ -290,6 +290,10 @@ private String determineXrefLocation() {
290290
return location;
291291
}
292292

293+
private File getXrefTestLocation() {
294+
return xrefTestLocation != null ? xrefTestLocation : new File(getReportOutputDirectory(), "xref-test");
295+
}
296+
293297
/**
294298
* @param locale The locale
295299
* @param key The key to search for

maven-surefire-report-plugin/src/main/java/org/apache/maven/plugins/surefire/report/SurefireReportRenderer.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class SurefireReportRenderer extends AbstractMavenReportRenderer {
5151

5252
private final SurefireReportParser parser;
5353
private final List<ReportTestSuite> testSuites;
54-
private final String xrefLocation;
54+
private final String xrefTestLocation;
5555
private final boolean showSuccess;
5656

5757
public SurefireReportRenderer(
@@ -61,7 +61,7 @@ public SurefireReportRenderer(
6161
Locale locale,
6262
ConsoleLogger consoleLogger,
6363
List<File> reportsDirectories,
64-
String xrefLocation,
64+
String xrefTestLocation,
6565
boolean showSuccess) {
6666
super(sink);
6767
this.i18n = i18n;
@@ -70,7 +70,7 @@ public SurefireReportRenderer(
7070
parser = new SurefireReportParser(reportsDirectories, consoleLogger);
7171
testSuites = parser.parseXMLReportFiles();
7272
this.showSuccess = showSuccess;
73-
this.xrefLocation = xrefLocation;
73+
this.xrefTestLocation = xrefTestLocation;
7474
}
7575

7676
@Override
@@ -507,13 +507,13 @@ private void renderSectionFailureDetails() {
507507

508508
String fullClassName = testCase.getFullClassName();
509509
String errorLineNumber = testCase.getFailureErrorLine();
510-
if (xrefLocation != null) {
510+
if (xrefTestLocation != null) {
511511
String path = fullClassName.replace('.', '/');
512-
sink.link(xrefLocation + "/" + path + ".html#L" + errorLineNumber);
512+
sink.link(xrefTestLocation + "/" + path + ".html#L" + errorLineNumber);
513513
}
514514
sink.text(fullClassName + ":" + errorLineNumber);
515515

516-
if (xrefLocation != null) {
516+
if (xrefTestLocation != null) {
517517
sink.link_();
518518
}
519519
sink.unknown("div", TAG_TYPE_END, null);

maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/SurefireReportTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void testBasicSurefireReport() throws Exception {
9191
boolean showSuccess = (Boolean) getVariableValueFromObject(mojo, "showSuccess");
9292
File reportsDir = (File) getVariableValueFromObject(mojo, "reportsDirectory");
9393
String outputName = (String) getVariableValueFromObject(mojo, "outputName");
94-
File xrefLocation = (File) getVariableValueFromObject(mojo, "xrefLocation");
94+
File xrefTestLocation = (File) getVariableValueFromObject(mojo, "xrefTestLocation");
9595
boolean linkXRef = (Boolean) getVariableValueFromObject(mojo, "linkXRef");
9696

9797
assertEquals(new File(getBasedir() + "/target/site/unit/basic-surefire-report-test"), outputDir);
@@ -103,7 +103,7 @@ public void testBasicSurefireReport() throws Exception {
103103
assertEquals("surefire", outputName);
104104
assertEquals(
105105
new File(getBasedir() + "/target/site/unit/basic-surefire-report-test/xref-test").getAbsolutePath(),
106-
xrefLocation.getAbsolutePath());
106+
xrefTestLocation.getAbsolutePath());
107107
assertTrue(linkXRef);
108108

109109
mojo.execute();

maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/stubs/EnclosedStub.java

+33
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,40 @@
1818
*/
1919
package org.apache.maven.plugins.surefire.report.stubs;
2020

21+
import java.io.FileInputStream;
22+
import java.io.InputStream;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.apache.maven.model.Model;
27+
import org.apache.maven.model.ReportPlugin;
28+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
29+
2130
public class EnclosedStub extends SurefireReportMavenProjectStub {
31+
private List<ReportPlugin> reportPlugins = new ArrayList<>();
32+
33+
public EnclosedStub() {
34+
MavenXpp3Reader pomReader = new MavenXpp3Reader();
35+
Model model = null;
36+
37+
try (InputStream is = new FileInputStream(getFile())) {
38+
model = pomReader.read(is);
39+
setModel(model);
40+
} catch (Exception e) {
41+
}
42+
43+
setReportPlugins(model.getReporting().getPlugins());
44+
}
45+
46+
public void setReportPlugins(List<ReportPlugin> plugins) {
47+
this.reportPlugins = plugins;
48+
}
49+
50+
/** {@inheritDoc} */
51+
@Override
52+
public List<ReportPlugin> getReportPlugins() {
53+
return reportPlugins;
54+
}
2255

2356
@Override
2457
protected String getProjectDirName() {

maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/stubs/EnclosedTrimStackTraceStub.java

+33
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,40 @@
1818
*/
1919
package org.apache.maven.plugins.surefire.report.stubs;
2020

21+
import java.io.FileInputStream;
22+
import java.io.InputStream;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.apache.maven.model.Model;
27+
import org.apache.maven.model.ReportPlugin;
28+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
29+
2130
public class EnclosedTrimStackTraceStub extends SurefireReportMavenProjectStub {
31+
private List<ReportPlugin> reportPlugins = new ArrayList<>();
32+
33+
public EnclosedTrimStackTraceStub() {
34+
MavenXpp3Reader pomReader = new MavenXpp3Reader();
35+
Model model = null;
36+
37+
try (InputStream is = new FileInputStream(getFile())) {
38+
model = pomReader.read(is);
39+
setModel(model);
40+
} catch (Exception e) {
41+
}
42+
43+
setReportPlugins(model.getReporting().getPlugins());
44+
}
45+
46+
public void setReportPlugins(List<ReportPlugin> plugins) {
47+
this.reportPlugins = plugins;
48+
}
49+
50+
/** {@inheritDoc} */
51+
@Override
52+
public List<ReportPlugin> getReportPlugins() {
53+
return reportPlugins;
54+
}
2255

2356
@Override
2457
protected String getProjectDirName() {

maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/stubs/NestedClassStub.java

+33
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,40 @@
1818
*/
1919
package org.apache.maven.plugins.surefire.report.stubs;
2020

21+
import java.io.FileInputStream;
22+
import java.io.InputStream;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.apache.maven.model.Model;
27+
import org.apache.maven.model.ReportPlugin;
28+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
29+
2130
public class NestedClassStub extends SurefireReportMavenProjectStub {
31+
private List<ReportPlugin> reportPlugins = new ArrayList<>();
32+
33+
public NestedClassStub() {
34+
MavenXpp3Reader pomReader = new MavenXpp3Reader();
35+
Model model = null;
36+
37+
try (InputStream is = new FileInputStream(getFile())) {
38+
model = pomReader.read(is);
39+
setModel(model);
40+
} catch (Exception e) {
41+
}
42+
43+
setReportPlugins(model.getReporting().getPlugins());
44+
}
45+
46+
public void setReportPlugins(List<ReportPlugin> plugins) {
47+
this.reportPlugins = plugins;
48+
}
49+
50+
/** {@inheritDoc} */
51+
@Override
52+
public List<ReportPlugin> getReportPlugins() {
53+
return reportPlugins;
54+
}
2255

2356
@Override
2457
protected String getProjectDirName() {

maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/stubs/NestedClassTrimStackTraceStub.java

+33
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,40 @@
1818
*/
1919
package org.apache.maven.plugins.surefire.report.stubs;
2020

21+
import java.io.FileInputStream;
22+
import java.io.InputStream;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.apache.maven.model.Model;
27+
import org.apache.maven.model.ReportPlugin;
28+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
29+
2130
public class NestedClassTrimStackTraceStub extends SurefireReportMavenProjectStub {
31+
private List<ReportPlugin> reportPlugins = new ArrayList<>();
32+
33+
public NestedClassTrimStackTraceStub() {
34+
MavenXpp3Reader pomReader = new MavenXpp3Reader();
35+
Model model = null;
36+
37+
try (InputStream is = new FileInputStream(getFile())) {
38+
model = pomReader.read(is);
39+
setModel(model);
40+
} catch (Exception e) {
41+
}
42+
43+
setReportPlugins(model.getReporting().getPlugins());
44+
}
45+
46+
public void setReportPlugins(List<ReportPlugin> plugins) {
47+
this.reportPlugins = plugins;
48+
}
49+
50+
/** {@inheritDoc} */
51+
@Override
52+
public List<ReportPlugin> getReportPlugins() {
53+
return reportPlugins;
54+
}
2255

2356
@Override
2457
protected String getProjectDirName() {

maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/stubs/SingleErrorStub.java

+33
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,40 @@
1818
*/
1919
package org.apache.maven.plugins.surefire.report.stubs;
2020

21+
import java.io.FileInputStream;
22+
import java.io.InputStream;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import org.apache.maven.model.Model;
27+
import org.apache.maven.model.ReportPlugin;
28+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
29+
2130
public class SingleErrorStub extends SurefireReportMavenProjectStub {
31+
private List<ReportPlugin> reportPlugins = new ArrayList<>();
32+
33+
public SingleErrorStub() {
34+
MavenXpp3Reader pomReader = new MavenXpp3Reader();
35+
Model model = null;
36+
37+
try (InputStream is = new FileInputStream(getFile())) {
38+
model = pomReader.read(is);
39+
setModel(model);
40+
} catch (Exception e) {
41+
}
42+
43+
setReportPlugins(model.getReporting().getPlugins());
44+
}
45+
46+
public void setReportPlugins(List<ReportPlugin> plugins) {
47+
this.reportPlugins = plugins;
48+
}
49+
50+
/** {@inheritDoc} */
51+
@Override
52+
public List<ReportPlugin> getReportPlugins() {
53+
return reportPlugins;
54+
}
2255

2356
@Override
2457
protected String getProjectDirName() {

maven-surefire-report-plugin/src/test/java/org/apache/maven/plugins/surefire/report/stubs/SurefireReportMavenProjectStub.java

-16
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,6 @@ public File getFile() {
4747
return new File(getBasedir(), "plugin-config.xml");
4848
}
4949

50-
/**
51-
* {@inheritDoc}
52-
*/
53-
@Override
54-
public List<ReportPlugin> getReportPlugins() {
55-
Reporting reporting = new Reporting();
56-
57-
ReportPlugin reportPlugin = new ReportPlugin();
58-
reportPlugin.setGroupId("org.apache.maven.plugins");
59-
reportPlugin.setArtifactId("maven-jxr-plugin");
60-
reportPlugin.setVersion("2.0-SNAPSHOT");
61-
reporting.addPlugin(reportPlugin);
62-
63-
return reporting.getPlugins();
64-
}
65-
6650
@Override
6751
public List<ArtifactRepository> getRemoteArtifactRepositories() {
6852
ArtifactRepository repository = new MavenArtifactRepository(

0 commit comments

Comments
 (0)