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