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.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   * Abstract class to test reports generation.
40   */
41  public abstract class AbstractCheckstyleTestCase extends AbstractMojoTestCase {
42      private Locale oldLocale;
43  
44      private ArtifactStubFactory artifactStubFactory;
45  
46      /**
47       * The project to test.
48       */
49      private MavenProject testMavenProject;
50  
51      @Override
52      protected void setUp() throws Exception {
53          // required for mojo lookups to work
54          super.setUp();
55  
56          oldLocale = Locale.getDefault();
57          Locale.setDefault(Locale.ENGLISH);
58  
59          artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
60          artifactStubFactory.getWorkingDir().mkdirs();
61      }
62  
63      @Override
64      protected void tearDown() throws Exception {
65          super.tearDown();
66  
67          Locale.setDefault(oldLocale);
68          oldLocale = null;
69      }
70  
71      /**
72       * Get the current Maven project
73       *
74       * @return the maven project
75       */
76      protected MavenProject getTestMavenProject() {
77          return testMavenProject;
78      }
79  
80      /**
81       * Get the generated report as file in the test maven project.
82       *
83       * @param name the name of the report
84       * @return the generated report as file
85       * @throws IOException if the return file doesn't exist
86       */
87      protected File getGeneratedReport(String name) throws IOException {
88          String outputDirectory = getBasedir() + "/target/test/test-harness/"
89                  + getTestMavenProject().getArtifactId();
90  
91          File report = new File(outputDirectory, name);
92          if (!report.exists()) {
93              throw new IOException("File not found. Attempted: " + report);
94          }
95  
96          return report;
97      }
98  
99      /**
100      * Generate the report and return the generated file
101      *
102      * @param goal the mojo goal.
103      * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
104      * @return the generated HTML file
105      * @throws Exception if any
106      */
107     protected File generateReport(String goal, String pluginXml) throws Exception {
108         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
109         CheckstyleReport mojo = createReportMojo(goal, pluginXmlFile);
110         return generateReport(mojo, pluginXmlFile);
111     }
112 
113     protected CheckstyleReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
114         CheckstyleReport mojo = (CheckstyleReport) lookupMojo(goal, pluginXmlFile);
115         assertNotNull("Mojo not found.", mojo);
116 
117         LegacySupport legacySupport = lookup(LegacySupport.class);
118         legacySupport.setSession(newMavenSession(new MavenProjectStub()));
119         DefaultRepositorySystemSession repoSession =
120                 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
121         repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
122                 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
123 
124         setVariableValueToObject(mojo, "session", legacySupport.getSession());
125         setVariableValueToObject(mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories());
126         return mojo;
127     }
128 
129     protected File generateReport(CheckstyleReport mojo, File pluginXmlFile) throws Exception {
130         mojo.execute();
131 
132         ProjectBuilder builder = lookup(ProjectBuilder.class);
133 
134         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
135         buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
136 
137         testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
138 
139         File outputDir = mojo.getReportOutputDirectory();
140         String filename = mojo.getOutputName() + ".html";
141 
142         return new File(outputDir, filename);
143     }
144 
145     /**
146      * Read the contents of the specified file object into a string
147      */
148     protected String readFile(File checkstyleTestDir, String fileName) throws IOException {
149         return new String(Files.readAllBytes(checkstyleTestDir.toPath().resolve(fileName)));
150     }
151 }