View Javadoc

1   package org.apache.maven.plugin.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 org.codehaus.plexus.util.FileUtils;
23  import org.codehaus.plexus.util.IOUtil;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileReader;
28  import java.io.IOException;
29  import java.net.URL;
30  import java.util.Locale;
31  
32  /**
33   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
34   * @version $Id: PmdReportTest.html 853015 2013-03-04 21:10:54Z olamy $
35   */
36  public class PmdReportTest
37      extends AbstractPmdReportTest
38  {
39      /**
40       * {@inheritDoc}
41       */
42      protected void setUp()
43          throws Exception
44      {
45          super.setUp();
46          FileUtils.deleteDirectory( new File( getBasedir(), "target/test/unit" ) );
47      }
48  
49      public void testDefaultConfiguration()
50          throws Exception
51      {
52          FileUtils.copyDirectoryStructure(
53              new File( getBasedir(), "src/test/resources/unit/default-configuration/jxr-files" ),
54              new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
55  
56          File testPom = new File( getBasedir(),
57                                   "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
58          PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
59          mojo.execute();
60  
61          //check if the PMD files were generated
62          File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
63          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
64  
65          // check if the rulesets, that have been applied, have been copied
66          generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/java-basic.xml" );
67          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
68  
69          generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/java-imports.xml" );
70          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
71  
72          generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/java-unusedcode.xml" );
73          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
74  
75          generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
76          renderer( mojo, generatedFile );
77          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
78  
79          //check if there's a link to the JXR files
80          String str =
81              readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ) );
82  
83          assertTrue( str.indexOf( "/xref/def/configuration/App.html#31" ) != -1 );
84  
85          assertTrue( str.indexOf( "/xref/def/configuration/AppSample.html#45" ) != -1 );
86      }
87  
88  
89      public void testJavascriptConfiguration()
90          throws Exception
91      {
92          File testPom = new File( getBasedir(),
93                                   "src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml" );
94          PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
95          mojo.execute();
96  
97          // check if the PMD files were generated
98          File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
99          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
100 
101         // these are the rulesets, that have been applied...
102         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/ecmascript-basic.xml" );
103         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
104 
105         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/ecmascript-braces.xml" );
106         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
107 
108         generatedFile =
109             new File( getBasedir(), "target/test/unit/default-configuration/target/ecmascript-unnecessary.xml" );
110         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
111 
112         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
113         renderer( mojo, generatedFile );
114         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
115 
116         String str = readFile( generatedFile );
117         assertTrue( str.indexOf( "Avoid using global variables" ) != -1 );
118     }
119 
120     public void testFileURL()
121         throws Exception
122     {
123         FileUtils.copyDirectoryStructure(
124             new File( getBasedir(), "src/test/resources/unit/default-configuration/jxr-files" ),
125             new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
126 
127         File testPom = new File( getBasedir(),
128                                  "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
129         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
130 
131         URL url = getClass().getClassLoader().getResource( "rulesets/java/basic.xml" );
132         URL url2 = getClass().getClassLoader().getResource( "rulesets/java/unusedcode.xml" );
133         URL url3 = getClass().getClassLoader().getResource( "rulesets/java/imports.xml" );
134         mojo.setRulesets( new String[]{ url.toString(), url2.toString(), url3.toString() } );
135 
136         mojo.execute();
137 
138         //check if the PMD files were generated
139         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
140         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
141 
142         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/basic.xml" );
143         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
144 
145         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/imports.xml" );
146         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
147 
148         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/unusedcode.xml" );
149         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
150 
151         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
152         renderer( mojo, generatedFile );
153         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
154 
155         //check if there's a link to the JXR files
156         String str =
157             readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ) );
158 
159         assertTrue( str.indexOf( "/xref/def/configuration/App.html#31" ) != -1 );
160 
161         assertTrue( str.indexOf( "/xref/def/configuration/AppSample.html#45" ) != -1 );
162     }
163 
164     /**
165      * With custom rulesets
166      *
167      * @throws Exception
168      */
169     public void testCustomConfiguration()
170         throws Exception
171     {
172         File testPom = new File( getBasedir(),
173                                  "src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml" );
174 
175         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
176         mojo.execute();
177 
178         //check the generated files
179         File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/pmd.csv" );
180         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
181 
182         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/custom.xml" );
183         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
184 
185         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" );
186         renderer( mojo, generatedFile );
187         renderer( mojo, generatedFile );
188         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
189 
190         //check if custom ruleset was applied
191         String str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" ) );
192         assertTrue( str.toLowerCase().indexOf( "Avoid using if statements without curly braces".toLowerCase() ) != -1 );
193 
194         assertTrue(
195             str.toLowerCase().indexOf( "Avoid using if...else statements without curly braces".toLowerCase() ) != -1 );
196 
197         assertTrue( "unnecessary constructor should not be triggered because of low priority",
198                     str.toLowerCase().indexOf(
199                         "Avoid unnecessary constructors - the compiler will generate these for you".toLowerCase() )
200                         == -1 );
201 
202     }
203 
204     /**
205      * Verify skip parameter
206      *
207      * @throws Exception
208      */
209     public void testSkipConfiguration()
210         throws Exception
211     {
212         File testPom = new File( getBasedir(), "src/test/resources/unit/custom-configuration/skip-plugin-config.xml" );
213         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
214         mojo.execute();
215 
216         // verify the generated files do not exist because PMD was skipped
217         File generatedFile = new File( getBasedir(), "target/test/unit/skip-configuration/target/pmd.csv" );
218         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
219 
220         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/custom.xml" );
221         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
222 
223         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" );
224         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
225     }
226 
227     public void testInvalidFormat()
228         throws Exception
229     {
230         try
231         {
232             File testPom =
233                 new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml" );
234             PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
235             setVariableValueToObject( mojo, "compileSourceRoots", mojo.project.getCompileSourceRoots() );
236             mojo.executeReport( Locale.ENGLISH );
237 
238             fail( "Must throw MavenReportException." );
239         }
240         catch ( Exception e )
241         {
242             assertTrue( true );
243         }
244     }
245 
246     public void testInvalidTargetJdk()
247         throws Exception
248     {
249         try
250         {
251             File testPom =
252                 new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-target-jdk-plugin-config.xml" );
253             PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
254             mojo.execute();
255 
256             fail( "Must throw MavenReportException." );
257         }
258         catch ( Exception e )
259         {
260             assertTrue( true );
261         }
262     }
263 
264 
265     /**
266      * Read the contents of the specified file object into a string
267      *
268      * @param file the file to be read
269      * @return a String object that contains the contents of the file
270      * @throws java.io.IOException
271      */
272     private String readFile( File file )
273         throws IOException
274     {
275         String strTmp;
276         StringBuilder str = new StringBuilder( (int) file.length() );
277         FileReader reader = null;
278         BufferedReader in = null;
279         try
280         {
281             reader = new FileReader( file );
282             in = new BufferedReader( reader );
283 
284             while ( ( strTmp = in.readLine() ) != null )
285             {
286                 str.append( ' ' );
287                 str.append( strTmp );
288             }
289             in.close();
290         }
291         finally
292         {
293             IOUtil.close( in );
294             IOUtil.close( reader );
295         }
296 
297         return str.toString();
298     }
299 
300     /**
301      * Verify the correct working of the localtionTemp method
302      *
303      * @throws Exception
304      */
305     public void testLocationTemp()
306         throws Exception
307     {
308 
309         File testPom = new File( getBasedir(),
310                                  "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
311         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
312 
313         assertEquals( "locationTemp is not correctly encoding filename",
314                       "export_format_pmd_language_java_name_some_2520name.xml", mojo.getLocationTemp(
315             "http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name" ) );
316 
317     }
318 
319 }