Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
Expand All @@ -49,11 +48,6 @@
public abstract class AbstractPmdReportTestCase extends AbstractMojoTestCase {
private ArtifactStubFactory artifactStubFactory;

/**
* The current project to be test.
*/
private MavenProject testMavenProject;

@Override
protected void setUp() throws Exception {
super.setUp();
Expand All @@ -64,38 +58,10 @@ protected void setUp() throws Exception {
}

/**
* Get the current Maven project
* Generate the report and return the generated file.
*
* @return the maven project
*/
protected MavenProject getTestMavenProject() {
return testMavenProject;
}

/**
* Get the generated report as file in the test maven project.
*
* @param name the name of the report.
* @return the generated report as file
* @throws IOException if the return file doesnt exist
*/
protected File getGeneratedReport(String name) throws IOException {
String outputDirectory =
getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId();

File report = new File(outputDirectory, name);
if (!report.exists()) {
throw new IOException("File not found. Attempted: " + report);
}

return report;
}

/**
* Generate the report and return the generated file
*
* @param goal the mojo goal.
* @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
* @param goal the mojo goal
* @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/"
* @return the generated HTML file
* @throws Exception if any
*/
Expand Down Expand Up @@ -133,52 +99,48 @@ protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) th
protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception {
mojo.execute();

ProjectBuilder builder = lookup(ProjectBuilder.class);

ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());

testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();

File outputDir = mojo.getReportOutputDirectory();
String filename = mojo.getOutputPath() + ".html";

return new File(outputDir, filename);
}

/**
* Read the contents of the specified file object into a string
* Read the contents of the specified file into a string.
*/
protected String readFile(File file) throws IOException {
return new String(Files.readAllBytes(file.toPath()));
}

/**
* Checks, whether the string <code>contained</code> is contained in
* the given <code>text</code> ignoring case.
* Checks whether the string <code>contained</code> is contained in
* the given <code>text</code>, ignoring case.
*
* @param text the string in which the search is executed
* @param contains the string, the should be searched
* @return <code>true</code> if the string is contained, otherwise <code>false</code>.
* @param contains the string to be searched for
* @return <code>true</code> if the text contains the string, otherwise <code>false</code>
*/
public static boolean lowerCaseContains(String text, String contains) {
return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
}

private MojoExecution getMockMojoExecution() {
MojoDescriptor md = new MojoDescriptor();
md.setGoal(getGoal());
MojoDescriptor mojoDescriptor = new MojoDescriptor();
mojoDescriptor.setGoal(getGoal());

MojoExecution me = new MojoExecution(md);
MojoExecution execution = new MojoExecution(mojoDescriptor);

PluginDescriptor pd = new PluginDescriptor();
Plugin p = new Plugin();
p.setGroupId("org.apache.maven.plugins");
p.setArtifactId("maven-pmd-plugin");
pd.setPlugin(p);
md.setPluginDescriptor(pd);
PluginDescriptor pluginDescriptor = new PluginDescriptor();
Plugin plugin = new Plugin();
plugin.setGroupId("org.apache.maven.plugins");
plugin.setArtifactId("maven-pmd-plugin");
pluginDescriptor.setPlugin(plugin);
mojoDescriptor.setPluginDescriptor(pluginDescriptor);

return me;
return execution;
}

protected abstract String getGoal();
Expand Down