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.Locale;
25  
26  import org.apache.maven.plugin.LegacySupport;
27  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28  import org.apache.maven.plugin.testing.ArtifactStubFactory;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  import org.apache.maven.project.DefaultProjectBuildingRequest;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.project.ProjectBuilder;
33  import org.apache.maven.project.ProjectBuildingRequest;
34  import org.eclipse.aether.DefaultRepositorySystemSession;
35  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
36  import org.eclipse.aether.repository.LocalRepository;
37  
38  /**
39   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
40   * @version $Id$
41   * @since 2.5
42   */
43  public abstract class AbstractPmdReportTestCase extends AbstractMojoTestCase {
44      private ArtifactStubFactory artifactStubFactory;
45  
46      /**
47       * The current project to be test.
48       */
49      private MavenProject testMavenProject;
50  
51      @Override
52      protected void setUp() throws Exception {
53          super.setUp();
54          CapturingPrintStream.init(true);
55  
56          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
57          artifactStubFactory.getWorkingDir().mkdirs();
58      }
59  
60      /**
61       * Get the current Maven project
62       *
63       * @return the maven project
64       */
65      protected MavenProject getTestMavenProject() {
66          return testMavenProject;
67      }
68  
69      /**
70       * Get the generated report as file in the test maven project.
71       *
72       * @param name the name of the report.
73       * @return the generated report as file
74       * @throws IOException if the return file doesnt exist
75       */
76      protected File getGeneratedReport(String name) throws IOException {
77          String outputDirectory =
78                  getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId();
79  
80          File report = new File(outputDirectory, name);
81          if (!report.exists()) {
82              throw new IOException("File not found. Attempted: " + report);
83          }
84  
85          return report;
86      }
87  
88      /**
89       * Generate the report and return the generated file
90       *
91       * @param goal the mojo goal.
92       * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
93       * @return the generated HTML file
94       * @throws Exception if any
95       */
96      protected File generateReport(String goal, String pluginXml) throws Exception {
97          File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml);
98          AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile);
99          return generateReport(mojo, pluginXmlFile);
100     }
101 
102     protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
103         AbstractPmdReport mojo = (AbstractPmdReport) lookupMojo(goal, pluginXmlFile);
104         assertNotNull("Mojo not found.", mojo);
105 
106         LegacySupport legacySupport = lookup(LegacySupport.class);
107         legacySupport.setSession(newMavenSession(new MavenProjectStub()));
108         DefaultRepositorySystemSession repoSession =
109                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
110         repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
111                 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
112 
113         setVariableValueToObject(mojo, "session", legacySupport.getSession());
114         setVariableValueToObject(mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories());
115         return mojo;
116     }
117 
118     protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception {
119         mojo.execute();
120 
121         ProjectBuilder builder = lookup(ProjectBuilder.class);
122 
123         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
124         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
125 
126         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
127 
128         File outputDir = mojo.getReportOutputDirectory();
129         String filename = mojo.getOutputName() + ".html";
130 
131         return new File(outputDir, filename);
132     }
133 
134     /**
135      * Read the contents of the specified file object into a string
136      */
137     protected String readFile(File file) throws IOException {
138         return new String(Files.readAllBytes(file.toPath()));
139     }
140 
141     /**
142      * Checks, whether the string <code>contained</code> is contained in
143      * the given <code>text</code> ignoring case.
144      *
145      * @param text the string in which the search is executed
146      * @param contains the string, the should be searched
147      * @return <code>true</code> if the string is contained, otherwise <code>false</code>.
148      */
149     public static boolean lowerCaseContains(String text, String contains) {
150         return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
151     }
152 }