Skip to content

Commit 1de65d5

Browse files
committed
Dynamically calculate xrefTestLocation
1 parent 6bca001 commit 1de65d5

File tree

18 files changed

+277
-45
lines changed

18 files changed

+277
-45
lines changed

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

Lines changed: 28 additions & 17 deletions
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,16 +79,19 @@ public abstract class AbstractSurefireReport extends AbstractMavenReport {
7779
private File reportsDirectory;
7880

7981
/**
80-
* Location of the Xrefs to link.
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 = "${project.reporting.outputDirectory}/xref-test")
83-
private File xrefLocation;
85+
@Parameter(property = "linkXRef", defaultValue = "true")
86+
private boolean linkXRef;
8487

8588
/**
86-
* Whether to link the XRef if found.
89+
* Location where Test Source XRef is generated for this project.
90+
* <br>
91+
* <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref-test}
8792
*/
88-
@Parameter(defaultValue = "true", property = "linkXRef")
89-
private boolean linkXRef;
93+
@Parameter
94+
private File xrefTestLocation;
9095

9196
/**
9297
* Whether to build an aggregated report at the root, or build individual reports.
@@ -149,7 +154,7 @@ public void executeReport(Locale locale) {
149154
locale,
150155
getConsoleLogger(),
151156
getReportsDirectories(),
152-
determineXrefLocation(),
157+
constructXrefTestLocation(),
153158
showSuccess);
154159
r.render();
155160
}
@@ -251,25 +256,27 @@ private List<MavenProject> getProjectsWithoutRoot() {
251256
return result;
252257
}
253258

254-
private String determineXrefLocation() {
259+
private String constructXrefTestLocation() {
255260
String location = null;
256-
257261
if (linkXRef) {
258-
String relativePath = PathTool.getRelativePath(
259-
getReportOutputDirectory().getAbsolutePath(), xrefLocation.getAbsolutePath());
262+
File xrefTestLocation = getXrefTestLocation();
263+
264+
String relativePath =
265+
PathTool.getRelativePath(getReportOutputDirectory().getAbsolutePath(), xrefTestLocation.getAbsolutePath());
260266
if (relativePath == null || relativePath.isEmpty()) {
261267
relativePath = ".";
262268
}
263-
relativePath = relativePath + "/" + xrefLocation.getName();
264-
if (xrefLocation.exists()) {
269+
relativePath = relativePath + "/" + xrefTestLocation.getName();
270+
if (xrefTestLocation.exists()) {
265271
// XRef was already generated by manual execution of a lifecycle binding
266272
location = relativePath;
267273
} else {
268274
// Not yet generated - check if the report is on its way
269-
for (Object o : project.getReportPlugins()) {
270-
ReportPlugin report = (ReportPlugin) o;
271-
272-
String artifactId = report.getArtifactId();
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();
273280
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
274281
location = relativePath;
275282
}
@@ -283,6 +290,10 @@ private String determineXrefLocation() {
283290
return location;
284291
}
285292

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testBasicSurefireReport() throws Exception {
9090
File outputDir = (File) getVariableValueFromObject(mojo, "outputDirectory");
9191
boolean showSuccess = (Boolean) getVariableValueFromObject(mojo, "showSuccess");
9292
File reportsDir = (File) getVariableValueFromObject(mojo, "reportsDirectory");
93-
File xrefLocation = (File) getVariableValueFromObject(mojo, "xrefLocation");
93+
File xrefTestLocation = (File) getVariableValueFromObject(mojo, "xrefTestLocation");
9494
boolean linkXRef = (Boolean) getVariableValueFromObject(mojo, "linkXRef");
9595

9696
assertEquals(new File(getBasedir() + "/target/site/unit/basic-surefire-report-test"), outputDir);
@@ -101,7 +101,7 @@ public void testBasicSurefireReport() throws Exception {
101101
reportsDir.getAbsolutePath());
102102
assertEquals(
103103
new File(getBasedir() + "/target/site/unit/basic-surefire-report-test/xref-test").getAbsolutePath(),
104-
xrefLocation.getAbsolutePath());
104+
xrefTestLocation.getAbsolutePath());
105105
assertTrue(linkXRef);
106106

107107
mojo.execute();

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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,6 @@ public File getFile() {
4949
return new File(getBasedir(), "plugin-config.xml");
5050
}
5151

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

maven-surefire-report-plugin/src/test/resources/unit/basic-surefire-report-anchor-test-cases/plugin-config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<reportsDirectory>${basedir}/src/test/resources/unit/basic-surefire-report-anchor-test-cases/surefire-reports
3030
</reportsDirectory>
3131
<outputName>surefire-report</outputName>
32-
<xrefLocation>${basedir}/target/site/unit/basic-surefire-report-anchor-test-cases/xref-test</xrefLocation>
32+
<xrefTestLocation>${basedir}/target/site/unit/basic-surefire-report-anchor-test-cases/xref-test</xrefTestLocation>
3333
</configuration>
3434
</plugin>
3535
</plugins>

maven-surefire-report-plugin/src/test/resources/unit/basic-surefire-report-linkxref-false/plugin-config.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@
2929
<reportsDirectory>${basedir}/src/test/resources/unit/basic-surefire-report-linkxref-false/surefire-reports
3030
</reportsDirectory>
3131
<outputName>surefire-report</outputName>
32-
<xrefLocation>${basedir}/target/site/unit/basic-surefire-report-linkxref-false/xref-test</xrefLocation>
32+
<xrefTestLocation>${basedir}/target/site/unit/basic-surefire-report-linkxref-false/xref-test</xrefTestLocation>
3333
<linkXRef>false</linkXRef>
3434
</configuration>
3535
</plugin>
3636
</plugins>
3737
</build>
38+
<reporting>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-jxr-plugin</artifactId>
43+
</plugin>
44+
</plugins>
45+
</reporting>
3846
</project>

0 commit comments

Comments
 (0)