|
27 | 27 | import java.nio.file.Files; |
28 | 28 | import java.nio.file.Path; |
29 | 29 | import java.nio.file.Paths; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Locale; |
30 | 33 |
|
31 | 34 | import org.apache.commons.io.FileUtils; |
32 | 35 | import org.apache.commons.lang3.StringUtils; |
| 36 | +import org.apache.maven.execution.MavenSession; |
| 37 | +import org.apache.maven.model.Plugin; |
| 38 | +import org.apache.maven.plugin.LegacySupport; |
| 39 | +import org.apache.maven.plugin.MojoExecution; |
33 | 40 | import org.apache.maven.plugin.MojoExecutionException; |
| 41 | +import org.apache.maven.plugin.descriptor.MojoDescriptor; |
| 42 | +import org.apache.maven.plugin.descriptor.PluginDescriptor; |
| 43 | +import org.apache.maven.plugin.testing.AbstractMojoTestCase; |
| 44 | +import org.apache.maven.plugin.testing.ArtifactStubFactory; |
| 45 | +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; |
| 46 | +import org.apache.maven.project.DefaultProjectBuildingRequest; |
| 47 | +import org.apache.maven.project.MavenProject; |
| 48 | +import org.apache.maven.project.ProjectBuildingRequest; |
34 | 49 | import org.apache.maven.reporting.MavenReportException; |
| 50 | +import org.apache.maven.session.scope.internal.SessionScope; |
| 51 | +import org.eclipse.aether.DefaultRepositorySystemSession; |
| 52 | +import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; |
| 53 | +import org.eclipse.aether.repository.LocalRepository; |
35 | 54 | import org.w3c.dom.Document; |
36 | 55 |
|
37 | 56 | /** |
38 | 57 | * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a> |
39 | 58 | * @version $Id$ |
40 | 59 | */ |
41 | | -public class CpdReportTest extends AbstractPmdReportTestCase { |
| 60 | +public class CpdReportTest extends AbstractMojoTestCase { |
| 61 | + private ArtifactStubFactory artifactStubFactory; |
| 62 | + |
| 63 | + /** |
| 64 | + * Checks whether the string <code>contained</code> is contained in |
| 65 | + * the given <code>text</code>, ignoring case. |
| 66 | + * |
| 67 | + * @param text the string in which the search is executed |
| 68 | + * @param contains the string to be searched for |
| 69 | + * @return <code>true</code> if the text contains the string, otherwise <code>false</code> |
| 70 | + */ |
| 71 | + public static boolean lowerCaseContains(String text, String contains) { |
| 72 | + return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT)); |
| 73 | + } |
| 74 | + |
42 | 75 | /** |
43 | 76 | * {@inheritDoc} |
44 | 77 | */ |
45 | 78 | @Override |
46 | 79 | protected void setUp() throws Exception { |
47 | 80 | super.setUp(); |
| 81 | + CapturingPrintStream.init(true); |
| 82 | + |
| 83 | + artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); |
| 84 | + artifactStubFactory.getWorkingDir().mkdirs(); |
| 85 | + SessionScope sessionScope = lookup(SessionScope.class); |
| 86 | + sessionScope.enter(); |
48 | 87 | FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit")); |
49 | 88 | } |
50 | 89 |
|
@@ -267,8 +306,90 @@ private static void assertReportContains(String expectedMessage) throws IOExcept |
267 | 306 | "Expected '" + expectedMessage + "' in cpd.xml, but was:\n" + report, report.contains(expectedMessage)); |
268 | 307 | } |
269 | 308 |
|
270 | | - @Override |
271 | 309 | protected String getGoal() { |
272 | 310 | return "cpd"; |
273 | 311 | } |
| 312 | + |
| 313 | + @Override |
| 314 | + protected void tearDown() throws Exception { |
| 315 | + SessionScope lookup = lookup(SessionScope.class); |
| 316 | + lookup.exit(); |
| 317 | + super.tearDown(); |
| 318 | + } |
| 319 | + |
| 320 | + /** |
| 321 | + * Generate the report and return the generated file. |
| 322 | + * |
| 323 | + * @param goal the mojo goal |
| 324 | + * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/" |
| 325 | + * @return the generated HTML file |
| 326 | + * @throws Exception if any |
| 327 | + */ |
| 328 | + protected File generateReport(String goal, String pluginXml) throws Exception { |
| 329 | + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); |
| 330 | + AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); |
| 331 | + return generateReport(mojo, pluginXmlFile); |
| 332 | + } |
| 333 | + |
| 334 | + protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { |
| 335 | + AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); |
| 336 | + assertNotNull("Mojo not found.", mojo); |
| 337 | + |
| 338 | + SessionScope sessionScope = lookup(SessionScope.class); |
| 339 | + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); |
| 340 | + sessionScope.seed(MavenSession.class, mavenSession); |
| 341 | + |
| 342 | + DefaultRepositorySystemSession repositorySession = |
| 343 | + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); |
| 344 | + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() |
| 345 | + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); |
| 346 | + |
| 347 | + List<MavenProject> reactorProjects = |
| 348 | + mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); |
| 349 | + |
| 350 | + setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); |
| 351 | + setVariableValueToObject(mojo, "session", mavenSession); |
| 352 | + setVariableValueToObject(mojo, "repoSession", repositorySession); |
| 353 | + setVariableValueToObject(mojo, "reactorProjects", reactorProjects); |
| 354 | + setVariableValueToObject( |
| 355 | + mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); |
| 356 | + setVariableValueToObject( |
| 357 | + mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); |
| 358 | + return mojo; |
| 359 | + } |
| 360 | + |
| 361 | + protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { |
| 362 | + mojo.execute(); |
| 363 | + |
| 364 | + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); |
| 365 | + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); |
| 366 | + |
| 367 | + File outputDir = mojo.getReportOutputDirectory(); |
| 368 | + String filename = mojo.getOutputPath() + ".html"; |
| 369 | + |
| 370 | + return new File(outputDir, filename); |
| 371 | + } |
| 372 | + |
| 373 | + /** |
| 374 | + * Read the contents of the specified file into a string. |
| 375 | + */ |
| 376 | + protected String readFile(File file) throws IOException { |
| 377 | + return new String(Files.readAllBytes(file.toPath())); |
| 378 | + } |
| 379 | + |
| 380 | + private MojoExecution getMockMojoExecution() { |
| 381 | + MojoDescriptor mojoDescriptor = new MojoDescriptor(); |
| 382 | + mojoDescriptor.setGoal(getGoal()); |
| 383 | + |
| 384 | + MojoExecution execution = new MojoExecution(mojoDescriptor); |
| 385 | + |
| 386 | + PluginDescriptor pluginDescriptor = new PluginDescriptor(); |
| 387 | + Plugin plugin = new Plugin(); |
| 388 | + plugin.setGroupId("org.apache.maven.plugins"); |
| 389 | + plugin.setArtifactId("maven-pmd-plugin"); |
| 390 | + pluginDescriptor.setPlugin(plugin); |
| 391 | + mojoDescriptor.setPluginDescriptor(pluginDescriptor); |
| 392 | + |
| 393 | + return execution; |
| 394 | + } |
274 | 395 | } |
0 commit comments