Skip to content

Commit

Permalink
Dynamically calculate xrefTestLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Dec 10, 2023
1 parent 2a1d9c3 commit 217dc1d
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import java.net.URLClassLoader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.Reporting;
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -78,16 +80,19 @@ public abstract class AbstractSurefireReport extends AbstractMavenReport {
private File reportsDirectory;

/**
* Location of the Xrefs to link.
* Link the violation line numbers to the (Test) Source XRef. Links will be created automatically if the JXR plugin is
* being used.
*/
@Parameter(defaultValue = "${project.reporting.outputDirectory}/xref-test")
private File xrefLocation;
@Parameter(property = "linkXRef", defaultValue = "true")
private boolean linkXRef;

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

/**
* Whether to build an aggregated report at the root, or build individual reports.
Expand Down Expand Up @@ -150,7 +155,7 @@ public void executeReport(Locale locale) throws MavenReportException {
locale,
getConsoleLogger(),
getReportsDirectories(),
determineXrefLocation(),
constructXrefTestLocation(),
showSuccess);
r.render();
}
Expand Down Expand Up @@ -252,12 +257,13 @@ private List<MavenProject> getProjectsWithoutRoot() {
return result;
}

private String determineXrefLocation() {
private String constructXrefTestLocation() {
String location = null;

if (linkXRef) {
String relativePath = PathTool.getRelativePath(
getReportOutputDirectory().getAbsolutePath(), xrefLocation.getAbsolutePath());
File xrefLocation = getXrefTestLocation();

String relativePath =
PathTool.getRelativePath(getReportOutputDirectory().getAbsolutePath(), xrefLocation.getAbsolutePath());
if (relativePath == null || relativePath.isEmpty()) {
relativePath = ".";
}
Expand All @@ -267,10 +273,11 @@ private String determineXrefLocation() {
location = relativePath;
} else {
// Not yet generated - check if the report is on its way
for (Object o : project.getReportPlugins()) {
ReportPlugin report = (ReportPlugin) o;

String artifactId = report.getArtifactId();
Reporting reporting = project.getModel().getReporting();
List<ReportPlugin> reportPlugins =
reporting != null ? reporting.getPlugins() : Collections.<ReportPlugin>emptyList();
for (ReportPlugin plugin : reportPlugins) {
String artifactId = plugin.getArtifactId();
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
location = relativePath;
}
Expand All @@ -284,6 +291,10 @@ private String determineXrefLocation() {
return location;
}

private File getXrefTestLocation() {
return xrefTestLocation != null ? xrefTestLocation : new File(getReportOutputDirectory(), "xref-test" );
}

/**
* @param locale The locale
* @param key The key to search for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testBasicSurefireReport() throws Exception {
File outputDir = (File) getVariableValueFromObject(mojo, "outputDirectory");
boolean showSuccess = (Boolean) getVariableValueFromObject(mojo, "showSuccess");
File reportsDir = (File) getVariableValueFromObject(mojo, "reportsDirectory");
File xrefLocation = (File) getVariableValueFromObject(mojo, "xrefLocation");
File xrefTestLocation = (File) getVariableValueFromObject(mojo, "xrefTestLocation");
boolean linkXRef = (Boolean) getVariableValueFromObject(mojo, "linkXRef");

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

mojo.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/basic-surefire-report-anchor-test-cases/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/basic-surefire-report-anchor-test-cases/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/basic-surefire-report-anchor-test-cases/xref-test</xrefTestLocation>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/basic-surefire-report-linkxref-false/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/basic-surefire-report-linkxref-false/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/basic-surefire-report-linkxref-false/xref-test</xrefTestLocation>
<linkXRef>false</linkXRef>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/basic-surefire-report-reporting-null/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/basic-surefire-report-test/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/basic-surefire-report-test/xref-test</xrefTestLocation>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/basic-surefire-report-success-false/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/basic-surefire-report-success-false/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/basic-surefire-report-success-false/xref-test</xrefTestLocation>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/basic-surefire-report-test/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/basic-surefire-report-test/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/basic-surefire-report-test/xref-test</xrefTestLocation>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/surefire-report-enclosed-trimStackTrace/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/surefire-report-enclosed-trimStackTrace/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/surefire-report-enclosed-trimStackTrace/xref-test</xrefTestLocation>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/surefire-report-enclosed/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/surefire-report-enclosed/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/surefire-report-enclosed/xref-test</xrefTestLocation>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/surefire-report-nestedClass-trimStackTrace/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/surefire-report-nestedClass-trimStackTrace/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/surefire-report-nestedClass-trimStackTrace/xref-test</xrefTestLocation>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/surefire-report-nestedClass/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/surefire-report-nestedClass/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/surefire-report-nestedClass/xref-test</xrefTestLocation>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<reportsDirectory>${basedir}/src/test/resources/unit/surefire-report-single-error/surefire-reports
</reportsDirectory>
<outputName>surefire-report</outputName>
<xrefLocation>${basedir}/target/site/unit/surefire-report-single-error/xref-test</xrefLocation>
<xrefTestLocation>${basedir}/target/site/unit/surefire-report-single-error/xref-test</xrefTestLocation>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
Expand Down

0 comments on commit 217dc1d

Please sign in to comment.