Skip to content

Commit

Permalink
[SUREFIRE-2227] Dynamically calculate xrefTestLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Aug 18, 2024
1 parent 83bbbb1 commit 455a919
Show file tree
Hide file tree
Showing 19 changed files with 282 additions and 57 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 @@ -77,23 +79,19 @@ public abstract class AbstractSurefireReport extends AbstractMavenReport {
private File reportsDirectory;

/**
* The projects in the reactor for aggregation report.
* Link the violation line numbers to the (Test) Source XRef. Links will be created automatically if the JXR plugin is
* being used.
*/
@Parameter(defaultValue = "${reactorProjects}", readonly = true)
private List<MavenProject> reactorProjects;

/**
* Link the failed tests line numbers to the source xref. Will link
* automatically if Maven JXR plugin is being used.
*/
@Parameter(defaultValue = "true", property = "linkXRef")
@Parameter(property = "linkXRef", defaultValue = "true")
private boolean linkXRef;

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

/**
* Whether to build an aggregated report at the root, or build individual reports.
Expand Down Expand Up @@ -156,7 +154,7 @@ public void executeReport(Locale locale) {
locale,
getConsoleLogger(),
getReportsDirectories(),
determineXrefLocation(),
constructXrefTestLocation(),
showSuccess);
r.render();
}
Expand Down Expand Up @@ -258,26 +256,28 @@ private List<MavenProject> getProjectsWithoutRoot() {
return result;
}

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

if (linkXRef) {
File xrefTestLocation = getXrefTestLocation();

String relativePath = PathTool.getRelativePath(
getReportOutputDirectory().getAbsolutePath(), xrefLocation.getAbsolutePath());
getReportOutputDirectory().getAbsolutePath(), xrefTestLocation.getAbsolutePath());
if (relativePath == null || relativePath.isEmpty()) {
relativePath = ".";
}
relativePath = relativePath + "/" + xrefLocation.getName();
if (xrefLocation.exists()) {
relativePath = relativePath + "/" + xrefTestLocation.getName();
if (xrefTestLocation.exists()) {
// XRef was already generated by manual execution of a lifecycle binding
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();
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
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)) {
location = relativePath;
}
}
Expand All @@ -290,6 +290,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 @@ -51,7 +51,7 @@ public class SurefireReportRenderer extends AbstractMavenReportRenderer {

private final SurefireReportParser parser;
private final List<ReportTestSuite> testSuites;
private final String xrefLocation;
private final String xrefTestLocation;
private final boolean showSuccess;

public SurefireReportRenderer(
Expand All @@ -61,7 +61,7 @@ public SurefireReportRenderer(
Locale locale,
ConsoleLogger consoleLogger,
List<File> reportsDirectories,
String xrefLocation,
String xrefTestLocation,
boolean showSuccess) {
super(sink);
this.i18n = i18n;
Expand All @@ -70,7 +70,7 @@ public SurefireReportRenderer(
parser = new SurefireReportParser(reportsDirectories, consoleLogger);
testSuites = parser.parseXMLReportFiles();
this.showSuccess = showSuccess;
this.xrefLocation = xrefLocation;
this.xrefTestLocation = xrefTestLocation;
}

@Override
Expand Down Expand Up @@ -507,13 +507,13 @@ private void renderSectionFailureDetails() {

String fullClassName = testCase.getFullClassName();
String errorLineNumber = testCase.getFailureErrorLine();
if (xrefLocation != null) {
if (xrefTestLocation != null) {
String path = fullClassName.replace('.', '/');
sink.link(xrefLocation + "/" + path + ".html#L" + errorLineNumber);
sink.link(xrefTestLocation + "/" + path + ".html#L" + errorLineNumber);
}
sink.text(fullClassName + ":" + errorLineNumber);

if (xrefLocation != null) {
if (xrefTestLocation != null) {
sink.link_();
}
sink.unknown("div", TAG_TYPE_END, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testBasicSurefireReport() throws Exception {
boolean showSuccess = (Boolean) getVariableValueFromObject(mojo, "showSuccess");
File reportsDir = (File) getVariableValueFromObject(mojo, "reportsDirectory");
String outputName = (String) getVariableValueFromObject(mojo, "outputName");
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 @@ -103,7 +103,7 @@ public void testBasicSurefireReport() throws Exception {
assertEquals("surefire", outputName);
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 @@ -18,7 +18,40 @@
*/
package org.apache.maven.plugins.surefire.report.stubs;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.model.Model;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

public class EnclosedStub extends SurefireReportMavenProjectStub {
private List<ReportPlugin> reportPlugins = new ArrayList<>();

public EnclosedStub() {
MavenXpp3Reader pomReader = new MavenXpp3Reader();
Model model = null;

try (InputStream is = new FileInputStream(getFile())) {
model = pomReader.read(is);
setModel(model);
} catch (Exception e) {
}

setReportPlugins(model.getReporting().getPlugins());
}

public void setReportPlugins(List<ReportPlugin> plugins) {
this.reportPlugins = plugins;
}

/** {@inheritDoc} */
@Override
public List<ReportPlugin> getReportPlugins() {
return reportPlugins;
}

@Override
protected String getProjectDirName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,40 @@
*/
package org.apache.maven.plugins.surefire.report.stubs;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.model.Model;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

public class EnclosedTrimStackTraceStub extends SurefireReportMavenProjectStub {
private List<ReportPlugin> reportPlugins = new ArrayList<>();

public EnclosedTrimStackTraceStub() {
MavenXpp3Reader pomReader = new MavenXpp3Reader();
Model model = null;

try (InputStream is = new FileInputStream(getFile())) {
model = pomReader.read(is);
setModel(model);
} catch (Exception e) {
}

setReportPlugins(model.getReporting().getPlugins());
}

public void setReportPlugins(List<ReportPlugin> plugins) {
this.reportPlugins = plugins;
}

/** {@inheritDoc} */
@Override
public List<ReportPlugin> getReportPlugins() {
return reportPlugins;
}

@Override
protected String getProjectDirName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,40 @@
*/
package org.apache.maven.plugins.surefire.report.stubs;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.model.Model;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

public class NestedClassStub extends SurefireReportMavenProjectStub {
private List<ReportPlugin> reportPlugins = new ArrayList<>();

public NestedClassStub() {
MavenXpp3Reader pomReader = new MavenXpp3Reader();
Model model = null;

try (InputStream is = new FileInputStream(getFile())) {
model = pomReader.read(is);
setModel(model);
} catch (Exception e) {
}

setReportPlugins(model.getReporting().getPlugins());
}

public void setReportPlugins(List<ReportPlugin> plugins) {
this.reportPlugins = plugins;
}

/** {@inheritDoc} */
@Override
public List<ReportPlugin> getReportPlugins() {
return reportPlugins;
}

@Override
protected String getProjectDirName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,40 @@
*/
package org.apache.maven.plugins.surefire.report.stubs;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.model.Model;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

public class NestedClassTrimStackTraceStub extends SurefireReportMavenProjectStub {
private List<ReportPlugin> reportPlugins = new ArrayList<>();

public NestedClassTrimStackTraceStub() {
MavenXpp3Reader pomReader = new MavenXpp3Reader();
Model model = null;

try (InputStream is = new FileInputStream(getFile())) {
model = pomReader.read(is);
setModel(model);
} catch (Exception e) {
}

setReportPlugins(model.getReporting().getPlugins());
}

public void setReportPlugins(List<ReportPlugin> plugins) {
this.reportPlugins = plugins;
}

/** {@inheritDoc} */
@Override
public List<ReportPlugin> getReportPlugins() {
return reportPlugins;
}

@Override
protected String getProjectDirName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,40 @@
*/
package org.apache.maven.plugins.surefire.report.stubs;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.model.Model;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

public class SingleErrorStub extends SurefireReportMavenProjectStub {
private List<ReportPlugin> reportPlugins = new ArrayList<>();

public SingleErrorStub() {
MavenXpp3Reader pomReader = new MavenXpp3Reader();
Model model = null;

try (InputStream is = new FileInputStream(getFile())) {
model = pomReader.read(is);
setModel(model);
} catch (Exception e) {
}

setReportPlugins(model.getReporting().getPlugins());
}

public void setReportPlugins(List<ReportPlugin> plugins) {
this.reportPlugins = plugins;
}

/** {@inheritDoc} */
@Override
public List<ReportPlugin> getReportPlugins() {
return reportPlugins;
}

@Override
protected String getProjectDirName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ public File getFile() {
return new File(getBasedir(), "plugin-config.xml");
}

/**
* {@inheritDoc}
*/
@Override
public List<ReportPlugin> getReportPlugins() {
Reporting reporting = new Reporting();

ReportPlugin reportPlugin = new ReportPlugin();
reportPlugin.setGroupId("org.apache.maven.plugins");
reportPlugin.setArtifactId("maven-jxr-plugin");
reportPlugin.setVersion("2.0-SNAPSHOT");
reporting.addPlugin(reportPlugin);

return reporting.getPlugins();
}

@Override
public List<ArtifactRepository> getRemoteArtifactRepositories() {
ArtifactRepository repository = new MavenArtifactRepository(
Expand Down
Loading

0 comments on commit 455a919

Please sign in to comment.