View Javadoc
1   package org.apache.maven.plugins.pmd;
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.DefaultRepositorySystemSession;
36  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
37  import org.eclipse.aether.repository.LocalRepository;
38  
39  /**
40   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
41   * @version $Id$
42   * @since 2.5
43   */
44  public abstract class AbstractPmdReportTestCase
45      extends AbstractMojoTestCase
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          super.setUp();
59          CapturingPrintStream.init( true );
60  
61          artifactStubFactory = new DependencyArtifactStubFactory( getTestFile( "target" ), true, false );
62          artifactStubFactory.getWorkingDir().mkdirs();
63      }
64  
65      /**
66       * Get the current Maven project
67       *
68       * @return the maven project
69       */
70      protected MavenProject getTestMavenProject()
71      {
72          return testMavenProject;
73      }
74  
75      /**
76       * Get the generated report as file in the test maven project.
77       *
78       * @param name the name of the report.
79       * @return the generated report as file
80       * @throws IOException if the return file doesnt exist
81       */
82      protected File getGeneratedReport( String name )
83          throws IOException
84      {
85          String outputDirectory = getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId();
86  
87          File report = new File( outputDirectory, name );
88          if ( !report.exists() )
89          {
90              throw new IOException( "File not found. Attempted: " + report );
91          }
92  
93          return report;
94      }
95  
96      /**
97       * Generate the report and return the generated file
98       *
99       * @param goal the mojo goal.
100      * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
101      * @return the generated HTML file
102      * @throws Exception if any
103      */
104     protected File generateReport( String goal, String pluginXml )
105         throws Exception
106     {
107         File pluginXmlFile = new File( getBasedir(), "src/test/resources/unit/" + pluginXml );
108         AbstractPmdReport mojo  = createReportMojo( goal, pluginXmlFile );
109         return generateReport( mojo, pluginXmlFile );
110     }
111 
112     protected AbstractPmdReport createReportMojo( String goal, File pluginXmlFile )
113         throws Exception
114     {
115         AbstractPmdReport mojo = (AbstractPmdReport) lookupMojo( goal, pluginXmlFile );
116         assertNotNull( "Mojo not found.", mojo );
117 
118         LegacySupport legacySupport = lookup( LegacySupport.class );
119         legacySupport.setSession( newMavenSession( new MavenProjectStub() ) );
120         DefaultRepositorySystemSession repoSession =
121             (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
122         repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().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( AbstractPmdReport mojo, File pluginXmlFile )
130         throws Exception
131     {
132         mojo.execute();
133 
134         ProjectBuilder builder = lookup( ProjectBuilder.class );
135 
136         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
137         buildingRequest.setRepositorySession( lookup( LegacySupport.class ).getRepositorySession() );
138 
139         testMavenProject = builder.build( pluginXmlFile, buildingRequest ).getProject();
140 
141         File outputDir = mojo.getReportOutputDirectory();
142         String filename = mojo.getOutputName() + ".html";
143 
144         return new File( outputDir, filename );
145     }
146 
147     /**
148      * Read the contents of the specified file object into a string
149      */
150     protected String readFile( File pmdTestDir, String fileName ) throws IOException
151     {
152         return new String( Files.readAllBytes( pmdTestDir.toPath().resolve( fileName ) ) );
153     }
154 
155 
156     /**
157      * Checks, whether the string <code>contained</code> is contained in
158      * the given <code>text</code> ignoring case.
159      *
160      * @param text the string in which the search is executed
161      * @param contains the string, the should be searched
162      * @return <code>true</code> if the string is contained, otherwise <code>false</code>.
163      */
164     public static boolean lowerCaseContains( String text, String contains )
165     {
166         return text.toLowerCase( Locale.ROOT ).contains( contains.toLowerCase( Locale.ROOT ) );
167     }
168 }