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.BufferedReader;
23  import java.io.File;
24  import java.io.FileReader;
25  import java.io.IOException;
26  import java.util.Locale;
27  
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  
31  import org.apache.commons.lang3.StringUtils;
32  import org.codehaus.plexus.util.FileUtils;
33  import org.w3c.dom.Document;
34  
35  /**
36   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
37   * @version $Id$
38   */
39  public class CpdReportTest
40      extends AbstractPmdReportTest
41  {
42      /**
43       * {@inheritDoc}
44       */
45      @Override
46      protected void setUp()
47          throws Exception
48      {
49          super.setUp();
50          Locale.setDefault( Locale.ENGLISH );
51          FileUtils.deleteDirectory( new File( getBasedir(), "target/test/unit" ) );
52      }
53  
54      /**
55       * Test CPDReport given the default configuration
56       *
57       * @throws Exception
58       */
59      public void testDefaultConfiguration()
60          throws Exception
61      {
62          File testPom =
63              new File( getBasedir(),
64                        "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
65          CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
66          mojo.execute();
67  
68          // check if the CPD files were generated
69          File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
70          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
71  
72          generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" );
73          renderer( mojo, generatedFile );
74          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
75  
76          // check the contents of cpd.html
77          String str = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) );
78          assertTrue( lowerCaseContains( str, "AppSample.java" ) );
79          assertTrue( lowerCaseContains( str, "App.java" ) );
80          assertTrue( lowerCaseContains( str, "public String dup( String str )" ) );
81          assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
82      }
83  
84      /**
85       * Test CPDReport with the text renderer given as "format=txt"
86       *
87       * @throws Exception
88       */
89      public void testTxtFormat()
90          throws Exception
91      {
92          File testPom =
93              new File( getBasedir(),
94                        "src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml" );
95          CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
96          mojo.execute();
97  
98          // check if the CPD files were generated
99          File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml" );
100         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
101         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.txt" );
102         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
103 
104         // check the contents of cpd.txt
105         String str = readFile( generatedFile );
106         // Contents that should NOT be in the report
107         assertFalse( lowerCaseContains( str, "public static void main( String[] args )" ) );
108         // Contents that should be in the report
109         assertTrue( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
110     }
111 
112     /**
113      * Test CPDReport using custom configuration
114      *
115      * @throws Exception
116      */
117     public void testCustomConfiguration()
118         throws Exception
119     {
120         File testPom =
121             new File( getBasedir(),
122                       "src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml" );
123         CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
124         mojo.execute();
125 
126         // check if the CPD files were generated
127         File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv" );
128         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
129 
130         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" );
131         renderer( mojo, generatedFile );
132         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
133 
134         // Contents that should NOT be in the report
135         String str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
136         assertFalse( lowerCaseContains( str, "/Sample.java" ) );
137 
138         str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
139         assertFalse( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
140 
141         // Contents that should be in the report
142         str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
143         assertTrue( lowerCaseContains( str, "AnotherSample.java" ) );
144 
145         str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
146         assertTrue( lowerCaseContains( str, "public static void main( String[] args )" ) );
147 
148         str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
149         assertTrue( lowerCaseContains( str, "private String unusedMethod(" ) );
150     }
151 
152     /**
153      * Test CPDReport with invalid format
154      *
155      * @throws Exception
156      */
157     public void testInvalidFormat()
158         throws Exception
159     {
160         try
161         {
162             File testPom =
163                 new File( getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml" );
164             CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
165             setVariableValueToObject( mojo, "compileSourceRoots", mojo.project.getCompileSourceRoots() );
166             mojo.execute();
167 
168             fail( "MavenReportException must be thrown" );
169         }
170         catch ( Exception e )
171         {
172             assertTrue( true );
173         }
174 
175     }
176 
177     /**
178      * Read the contents of the specified file object into a string
179      *
180      * @param file the file to be read
181      * @return a String object that contains the contents of the file
182      * @throws java.io.IOException
183      */
184     private String readFile( File file )
185         throws IOException
186     {
187         String strTmp;
188         StringBuilder str = new StringBuilder( (int) file.length() );
189         try ( BufferedReader in = new BufferedReader( new FileReader( file ) ) )
190         {
191             while ( ( strTmp = in.readLine() ) != null )
192             {
193                 str.append( ' ' );
194                 str.append( strTmp );
195             }
196         }
197 
198         return str.toString();
199     }
200 
201     public void testWriteNonHtml()
202         throws Exception
203     {
204         File testPom =
205             new File( getBasedir(),
206                       "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
207         CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
208         assertNotNull( mojo );
209         mojo.execute();
210 
211         File tReport = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
212 
213         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
214         Document pmdCpdDocument = builder.parse( tReport );
215         assertNotNull( pmdCpdDocument );
216 
217         String str = readFile( tReport );
218         assertTrue( lowerCaseContains( str, "AppSample.java" ) );
219         assertTrue( lowerCaseContains( str, "App.java" ) );
220         assertTrue( lowerCaseContains( str, "public String dup( String str )" ) );
221         assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
222     }
223 
224     /**
225      * verify the cpd.xml file is included in the site when requested.
226      * @throws Exception
227      */
228     public void testIncludeXmlInSite()
229             throws Exception
230     {
231         File testPom =
232                 new File( getBasedir(),
233                           "src/test/resources/unit/default-configuration/cpd-report-include-xml-in-site-plugin-config.xml" );
234         CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
235         assertNotNull( mojo );
236         mojo.execute();
237 
238         File tReport = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
239         assertTrue( FileUtils.fileExists( tReport.getAbsolutePath() ) );
240 
241         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
242         Document pmdCpdDocument = builder.parse( tReport );
243         assertNotNull( pmdCpdDocument );
244 
245         String str = readFile( tReport );
246         assertTrue( str.contains( "</pmd-cpd>" ) );
247 
248         File siteReport = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml" );
249         assertTrue( FileUtils.fileExists( siteReport.getAbsolutePath() ) );
250         String siteReportContent = readFile( siteReport );
251         assertTrue( siteReportContent.contains( "</pmd-cpd>" ) );
252         assertEquals( str, siteReportContent );
253     }
254 
255 
256     public void testSkipEmptyReportConfiguration()
257         throws Exception
258     {
259         File testPom =
260             new File( getBasedir(), "src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml" );
261         CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
262         mojo.execute();
263 
264         // verify the generated files do not exist because PMD was skipped
265         File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" );
266         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
267     }
268 
269     public void testEmptyReportConfiguration()
270         throws Exception
271     {
272         File testPom =
273             new File( getBasedir(), "src/test/resources/unit/empty-report/cpd-empty-report-plugin-config.xml" );
274         CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
275         mojo.execute();
276 
277         // verify the generated files do exist, even if there are no violations
278         File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" );
279         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
280         String str = readFile( new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" ) );
281         assertFalse( lowerCaseContains( str, "Hello.java" ) );
282         assertTrue( str.contains( "CPD found no problems in your source code." ) );
283     }
284 
285     public void testCpdEncodingConfiguration()
286         throws Exception
287     {
288         String originalEncoding = System.getProperty( "file.encoding" );
289         try
290         {
291             System.setProperty( "file.encoding", "UTF-16" );
292 
293             File testPom =
294                 new File( getBasedir(),
295                           "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
296             CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
297             mojo.execute();
298 
299             // check if the CPD files were generated
300             File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
301             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
302             String str = readFile( generatedFile );
303             assertTrue( lowerCaseContains( str, "AppSample.java" ) );
304         }
305         finally
306         {
307             System.setProperty( "file.encoding", originalEncoding );
308         }
309     }
310 
311     public void testCpdJavascriptConfiguration()
312         throws Exception
313     {
314         File testPom =
315                 new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml" );
316             CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
317             mojo.execute();
318 
319             // verify  the generated file to exist and violations are reported
320             File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
321             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
322             String str = readFile( generatedFile );
323             assertTrue( lowerCaseContains( str, "Sample.js" ) );
324             assertTrue( lowerCaseContains( str, "SampleDup.js" ) );
325     }
326 
327     public void testCpdJspConfiguration()
328             throws Exception
329     {
330         File testPom =
331                 new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml" );
332             CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
333             mojo.execute();
334 
335             // verify  the generated file to exist and violations are reported
336             File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
337             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
338             String str = readFile( generatedFile );
339             assertTrue( lowerCaseContains( str, "sample.jsp" ) );
340             assertTrue( lowerCaseContains( str, "sampleDup.jsp" ) );
341     }
342 
343     public void testExclusionsConfiguration()
344             throws Exception
345     {
346         File testPom =
347             new File( getBasedir(),
348                       "src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml" );
349         final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
350         mojo.execute();
351 
352         // verify  the generated file to exist and no duplications are reported
353         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
354         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
355         String str = readFile( generatedFile );
356         assertEquals( 0, StringUtils.countMatches( str, "<duplication" ) );
357     }
358 }