View Javadoc
1   package org.apache.maven.plugins.checkstyle;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.file.Files;
25  import java.util.Locale;
26  
27  import org.apache.maven.plugin.LegacySupport;
28  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29  import org.apache.maven.plugin.testing.ArtifactStubFactory;
30  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
31  import org.apache.maven.project.DefaultProjectBuildingRequest;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.project.ProjectBuilder;
34  import org.apache.maven.project.ProjectBuildingRequest;
35  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
36  import org.eclipse.aether.DefaultRepositorySystemSession;
37  import org.eclipse.aether.repository.LocalRepository;
38  
39  /**
40   * Abstract class to test reports generation.
41   */
42  public abstract class AbstractCheckstyleTestCase
43      extends AbstractMojoTestCase
44  {
45      private Locale oldLocale;
46  
47      private ArtifactStubFactory artifactStubFactory;
48  
49      /**
50       * The current project to be test.
51       */
52      private MavenProject testMavenProject;
53  
54      @Override
55      protected void setUp()
56          throws Exception
57      {
58          // required for mojo lookups to work
59          super.setUp();
60  
61          oldLocale = Locale.getDefault();
62          Locale.setDefault( Locale.ENGLISH );
63  
64          artifactStubFactory = new DependencyArtifactStubFactory( getTestFile( "target" ), true, false );
65          artifactStubFactory.getWorkingDir().mkdirs();
66      }
67  
68      @Override
69      protected void tearDown()
70          throws Exception
71      {
72          super.tearDown();
73  
74          Locale.setDefault( oldLocale );
75          oldLocale = null;
76      }
77  
78      /**
79       * Get the current Maven project
80       *
81       * @return the maven project
82       */
83      protected MavenProject getTestMavenProject()
84      {
85          return testMavenProject;
86      }
87  
88      /**
89       * Get the generated report as file in the test maven project.
90       *
91       * @param name the name of the report.
92       * @return the generated report as file
93       * @throws IOException if the return file doesnt exist
94       */
95      protected File getGeneratedReport( String name )
96          throws IOException
97      {
98          String outputDirectory = getBasedir() + "/target/test/test-harness/" + getTestMavenProject().getArtifactId();
99  
100         File report = new File( outputDirectory, name );
101         if ( !report.exists() )
102         {
103             throw new IOException( "File not found. Attempted: " + report );
104         }
105 
106         return report;
107     }
108 
109     /**
110      * Generate the report and return the generated file
111      *
112      * @param goal the mojo goal.
113      * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
114      * @return the generated HTML file
115      * @throws Exception if any
116      */
117     protected File generateReport( String goal, String pluginXml )
118         throws Exception
119     {
120         File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
121         CheckstyleReport mojo  = createReportMojo( goal, pluginXmlFile );
122         return generateReport( mojo, pluginXmlFile );
123     }
124 
125     protected CheckstyleReport createReportMojo( String goal, File pluginXmlFile )
126         throws Exception
127     {
128         CheckstyleReport mojo = (CheckstyleReport) lookupMojo( goal, pluginXmlFile );
129         assertNotNull( "Mojo not found.", mojo );
130 
131         LegacySupport legacySupport = lookup( LegacySupport.class );
132         legacySupport.setSession( newMavenSession( new MavenProjectStub() ) );
133         DefaultRepositorySystemSession repoSession =
134             (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
135         repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, new LocalRepository( artifactStubFactory.getWorkingDir() ) ) );
136 
137         setVariableValueToObject( mojo, "session", legacySupport.getSession() );
138         setVariableValueToObject( mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories() );
139         return mojo;
140     }
141 
142     protected File generateReport( CheckstyleReport mojo, File pluginXmlFile )
143         throws Exception
144     {
145         mojo.execute();
146 
147         ProjectBuilder builder = lookup( ProjectBuilder.class );
148 
149         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
150         buildingRequest.setRepositorySession( lookup( LegacySupport.class ).getRepositorySession() );
151 
152         testMavenProject = builder.build( pluginXmlFile, buildingRequest ).getProject();
153 
154         File outputDir = mojo.getReportOutputDirectory();
155         String filename = mojo.getOutputName() + ".html";
156 
157         return new File( outputDir, filename );
158     }
159 
160     /**
161      * Read the contents of the specified file object into a string
162      */
163     protected String readFile( File checkstyleTestDir, String fileName ) throws IOException
164     {
165         return new String( Files.readAllBytes( checkstyleTestDir.toPath().resolve( fileName ) ) );
166     }
167 
168 }