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.checkstyle;
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  
27  import org.apache.maven.model.Plugin;
28  import org.apache.maven.plugin.LegacySupport;
29  import org.apache.maven.plugin.MojoExecution;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
33  import org.apache.maven.plugin.testing.ArtifactStubFactory;
34  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
35  import org.apache.maven.project.DefaultProjectBuildingRequest;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.project.ProjectBuilder;
38  import org.apache.maven.project.ProjectBuildingRequest;
39  import org.eclipse.aether.DefaultRepositorySystemSession;
40  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
41  import org.eclipse.aether.repository.LocalRepository;
42  
43  /**
44   * Abstract class to test reports generation.
45   */
46  public abstract class AbstractCheckstyleTestCase extends AbstractMojoTestCase {
47      private ArtifactStubFactory artifactStubFactory;
48  
49      /**
50       * The project to test.
51       */
52      private MavenProject testMavenProject;
53  
54      @Override
55      protected void setUp() throws Exception {
56          // required for mojo lookups to work
57          super.setUp();
58  
59          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
60          artifactStubFactory.getWorkingDir().mkdirs();
61      }
62  
63      /**
64       * Get the current Maven project
65       *
66       * @return the maven project
67       */
68      protected MavenProject getTestMavenProject() {
69          return testMavenProject;
70      }
71  
72      /**
73       * Get the generated report as file in the test maven project.
74       *
75       * @param name the name of the report
76       * @return the generated report as file
77       * @throws IOException if the return file doesn't exist
78       */
79      protected File getGeneratedReport(String name) throws IOException {
80          String outputDirectory = getBasedir() + "/target/test/test-harness/"
81                  + getTestMavenProject().getArtifactId();
82  
83          File report = new File(outputDirectory, name);
84          if (!report.exists()) {
85              throw new IOException("File not found. Attempted: " + report);
86          }
87  
88          return report;
89      }
90  
91      /**
92       * Generate the report and return the generated file
93       *
94       * @param goal the mojo goal.
95       * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
96       * @return the generated HTML file
97       * @throws Exception if any
98       */
99      protected File generateReport(String goal, String pluginXml) throws Exception {
100         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
101         CheckstyleReport mojo = createReportMojo(goal, pluginXmlFile);
102         return generateReport(mojo, pluginXmlFile);
103     }
104 
105     protected CheckstyleReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
106         CheckstyleReport mojo = (CheckstyleReport) lookupMojo(goal, pluginXmlFile);
107         assertNotNull("Mojo not found.", mojo);
108 
109         LegacySupport legacySupport = lookup(LegacySupport.class);
110         legacySupport.setSession(newMavenSession(new MavenProjectStub()));
111         DefaultRepositorySystemSession repoSession =
112                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
113         repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
114                 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
115 
116         List<MavenProject> reactorProjects =
117                 mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
118 
119         setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
120         setVariableValueToObject(mojo, "session", legacySupport.getSession());
121         setVariableValueToObject(mojo, "repoSession", legacySupport.getRepositorySession());
122         setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
123         setVariableValueToObject(
124                 mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories());
125         setVariableValueToObject(
126                 mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site"));
127         return mojo;
128     }
129 
130     protected File generateReport(CheckstyleReport mojo, File pluginXmlFile) throws Exception {
131         mojo.execute();
132 
133         ProjectBuilder builder = lookup(ProjectBuilder.class);
134 
135         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
136         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
137 
138         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
139 
140         File outputDir = mojo.getReportOutputDirectory();
141         String filename = mojo.getOutputName() + ".html";
142 
143         return new File(outputDir, filename);
144     }
145 
146     /**
147      * Read the contents of the specified file object into a string
148      */
149     protected String readFile(File checkstyleTestDir, String fileName) throws IOException {
150         return new String(Files.readAllBytes(checkstyleTestDir.toPath().resolve(fileName)));
151     }
152 
153     private MojoExecution getMockMojoExecution() {
154         MojoDescriptor md = new MojoDescriptor();
155         md.setGoal(getGoal());
156 
157         MojoExecution me = new MojoExecution(md);
158 
159         PluginDescriptor pd = new PluginDescriptor();
160         Plugin p = new Plugin();
161         p.setGroupId("org.apache.maven.plugins");
162         p.setArtifactId("maven-checkstyle-plugin");
163         pd.setPlugin(p);
164         md.setPluginDescriptor(pd);
165 
166         return me;
167     }
168 
169     protected abstract String getGoal();
170 }