1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.pmd;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.plugin.LegacySupport;
31 import org.apache.maven.plugin.MojoExecution;
32 import org.apache.maven.plugin.descriptor.MojoDescriptor;
33 import org.apache.maven.plugin.descriptor.PluginDescriptor;
34 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35 import org.apache.maven.plugin.testing.ArtifactStubFactory;
36 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
37 import org.apache.maven.project.DefaultProjectBuildingRequest;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.project.ProjectBuildingRequest;
40 import org.apache.maven.session.scope.internal.SessionScope;
41 import org.eclipse.aether.DefaultRepositorySystemSession;
42 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
43 import org.eclipse.aether.repository.LocalRepository;
44
45
46
47
48
49
50 public abstract class AbstractPmdReportTestCase extends AbstractMojoTestCase {
51 private ArtifactStubFactory artifactStubFactory;
52
53 @Override
54 protected void setUp() throws Exception {
55 super.setUp();
56 CapturingPrintStream.init(true);
57
58 artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
59 artifactStubFactory.getWorkingDir().mkdirs();
60 SessionScope sessionScope = lookup(SessionScope.class);
61 sessionScope.enter();
62 }
63
64 @Override
65 protected void tearDown() throws Exception {
66 SessionScope lookup = lookup(SessionScope.class);
67 lookup.exit();
68 super.tearDown();
69 }
70
71
72
73
74
75
76
77
78
79 protected File generateReport(String goal, String pluginXml) throws Exception {
80 File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml);
81 AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile);
82 return generateReport(mojo, pluginXmlFile);
83 }
84
85 protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
86 AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile);
87 assertNotNull("Mojo not found.", mojo);
88
89 SessionScope sessionScope = lookup(SessionScope.class);
90 MavenSession mavenSession = newMavenSession(new MavenProjectStub());
91 sessionScope.seed(MavenSession.class, mavenSession);
92
93 DefaultRepositorySystemSession repositorySession =
94 (DefaultRepositorySystemSession) mavenSession.getRepositorySession();
95 repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
96 .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir())));
97
98 List<MavenProject> reactorProjects =
99 mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
100
101 setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
102 setVariableValueToObject(mojo, "session", mavenSession);
103 setVariableValueToObject(mojo, "repoSession", repositorySession);
104 setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
105 setVariableValueToObject(
106 mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories());
107 setVariableValueToObject(
108 mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site"));
109 return mojo;
110 }
111
112 protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception {
113 mojo.execute();
114
115 ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
116 buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
117
118 File outputDir = mojo.getReportOutputDirectory();
119 String filename = mojo.getOutputPath() + ".html";
120
121 return new File(outputDir, filename);
122 }
123
124
125
126
127 protected String readFile(File file) throws IOException {
128 return new String(Files.readAllBytes(file.toPath()));
129 }
130
131
132
133
134
135
136
137
138
139 public static boolean lowerCaseContains(String text, String contains) {
140 return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
141 }
142
143 private MojoExecution getMockMojoExecution() {
144 MojoDescriptor mojoDescriptor = new MojoDescriptor();
145 mojoDescriptor.setGoal(getGoal());
146
147 MojoExecution execution = new MojoExecution(mojoDescriptor);
148
149 PluginDescriptor pluginDescriptor = new PluginDescriptor();
150 Plugin plugin = new Plugin();
151 plugin.setGroupId("org.apache.maven.plugins");
152 plugin.setArtifactId("maven-pmd-plugin");
153 pluginDescriptor.setPlugin(plugin);
154 mojoDescriptor.setPluginDescriptor(pluginDescriptor);
155
156 return execution;
157 }
158
159 protected abstract String getGoal();
160 }