View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
47   * @version $Id$
48   * @since 2.5
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       * Generate the report and return the generated file.
73       *
74       * @param goal the mojo goal
75       * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/"
76       * @return the generated HTML file
77       * @throws Exception if any
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      * Read the contents of the specified file into a string.
126      */
127     protected String readFile(File file) throws IOException {
128         return new String(Files.readAllBytes(file.toPath()));
129     }
130 
131     /**
132      * Checks whether the string <code>contained</code> is contained in
133      * the given <code>text</code>, ignoring case.
134      *
135      * @param text the string in which the search is executed
136      * @param contains the string to be searched for
137      * @return <code>true</code> if the text contains the string, otherwise <code>false</code>
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 }