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 AbstractPmdReportTestCase
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 generatedReport = generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
63          assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
64  
65          // check if the CPD files were generated
66          File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
67          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
68  
69          // check the contents of cpd.html
70          String str = readFile( generatedReport );
71          assertTrue( lowerCaseContains( str, "AppSample.java" ) );
72          assertTrue( lowerCaseContains( str, "App.java" ) );
73          assertTrue( lowerCaseContains( str, "public String dup( String str )" ) );
74          assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
75  
76          // the version should be logged
77          String output = CapturingPrintStream.getOutput();
78          assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
79      }
80  
81      /**
82       * Test CPDReport with the text renderer given as "format=txt"
83       *
84       * @throws Exception
85       */
86      public void testTxtFormat()
87          throws Exception
88      {
89          generateReport( "cpd", "custom-configuration/cpd-txt-format-configuration-plugin-config.xml" );
90  
91          // check if the CPD files were generated
92          File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml" );
93          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
94          generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.txt" );
95          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
96  
97          // check the contents of cpd.txt
98          String str = readFile( generatedFile );
99          // Contents that should NOT be in the report
100         assertFalse( lowerCaseContains( str, "public static void main( String[] args )" ) );
101         // Contents that should be in the report
102         assertTrue( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
103     }
104 
105     /**
106      * Test CPDReport using custom configuration
107      *
108      * @throws Exception
109      */
110     public void testCustomConfiguration()
111         throws Exception
112     {
113         File generatedReport = generateReport( "cpd", "custom-configuration/cpd-custom-configuration-plugin-config.xml" );
114         assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
115 
116         // check if the CPD files were generated
117         File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv" );
118         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
119 
120         String str = readFile( generatedReport );
121         // Contents that should NOT be in the report
122         assertFalse( lowerCaseContains( str, "/Sample.java" ) );
123         assertFalse( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
124         // Contents that should be in the report
125         assertTrue( lowerCaseContains( str, "AnotherSample.java" ) );
126         assertTrue( lowerCaseContains( str, "public static void main( String[] args )" ) );
127         assertTrue( lowerCaseContains( str, "private String unusedMethod(" ) );
128     }
129 
130     /**
131      * Test CPDReport with invalid format
132      *
133      * @throws Exception
134      */
135     public void testInvalidFormat()
136         throws Exception
137     {
138         try
139         {
140             File testPom =
141                     new File( getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml" );
142             AbstractPmdReport mojo  = createReportMojo( "cpd", testPom );
143             setVariableValueToObject( mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots() );
144             generateReport( mojo, testPom );
145 
146             fail( "MavenReportException must be thrown" );
147         }
148         catch ( Exception e )
149         {
150             assertTrue( true );
151         }
152 
153     }
154 
155     /**
156      * Read the contents of the specified file object into a string
157      *
158      * @param file the file to be read
159      * @return a String object that contains the contents of the file
160      * @throws java.io.IOException
161      */
162     private String readFile( File file )
163         throws IOException
164     {
165         String strTmp;
166         StringBuilder str = new StringBuilder( (int) file.length() );
167         try ( BufferedReader in = new BufferedReader( new FileReader( file ) ) )
168         {
169             while ( ( strTmp = in.readLine() ) != null )
170             {
171                 str.append( ' ' );
172                 str.append( strTmp );
173             }
174         }
175 
176         return str.toString();
177     }
178 
179     public void testWriteNonHtml()
180         throws Exception
181     {
182         generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
183 
184         // check if the CPD files were generated
185         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
186         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
187 
188         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
189         Document pmdCpdDocument = builder.parse( generatedFile );
190         assertNotNull( pmdCpdDocument );
191 
192         String str = readFile( generatedFile );
193         assertTrue( lowerCaseContains( str, "AppSample.java" ) );
194         assertTrue( lowerCaseContains( str, "App.java" ) );
195         assertTrue( lowerCaseContains( str, "public String dup( String str )" ) );
196         assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
197     }
198 
199     /**
200      * verify the cpd.xml file is included in the site when requested.
201      * @throws Exception
202      */
203     public void testIncludeXmlInSite()
204             throws Exception
205     {
206         generateReport( "cpd", "default-configuration/cpd-report-include-xml-in-site-plugin-config.xml" );
207 
208         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
209         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
210 
211         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
212         Document pmdCpdDocument = builder.parse( generatedFile );
213         assertNotNull( pmdCpdDocument );
214 
215         String str = readFile( generatedFile );
216         assertTrue( str.contains( "</pmd-cpd>" ) );
217 
218         File siteReport = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml" );
219         assertTrue( FileUtils.fileExists( siteReport.getAbsolutePath() ) );
220         String siteReportContent = readFile( siteReport );
221         assertTrue( siteReportContent.contains( "</pmd-cpd>" ) );
222         assertEquals( str, siteReportContent );
223     }
224 
225 
226     public void testSkipEmptyReportConfiguration()
227         throws Exception
228     {
229         // verify the generated files do not exist because PMD was skipped
230         File generatedReport = generateReport( "cpd", "empty-report/cpd-skip-empty-report-plugin-config.xml" );
231         assertFalse( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
232     }
233 
234     public void testEmptyReportConfiguration()
235         throws Exception
236     {
237         // verify the generated files do exist, even if there are no violations
238         File generatedReport = generateReport( "cpd", "empty-report/cpd-empty-report-plugin-config.xml" );
239         assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
240 
241         String str = readFile( generatedReport );
242         assertFalse( lowerCaseContains( str, "Hello.java" ) );
243         assertTrue( str.contains( "CPD found no problems in your source code." ) );
244     }
245 
246     public void testCpdEncodingConfiguration()
247         throws Exception
248     {
249         String originalEncoding = System.getProperty( "file.encoding" );
250         try
251         {
252             System.setProperty( "file.encoding", "UTF-16" );
253 
254             generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
255 
256             // check if the CPD files were generated
257             File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
258             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
259             String str = readFile( generatedFile );
260             assertTrue( lowerCaseContains( str, "AppSample.java" ) );
261         }
262         finally
263         {
264             System.setProperty( "file.encoding", originalEncoding );
265         }
266     }
267 
268     public void testCpdJavascriptConfiguration()
269         throws Exception
270     {
271         generateReport( "cpd", "default-configuration/cpd-javascript-plugin-config.xml" );
272 
273         // verify  the generated file to exist and violations are reported
274         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
275         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
276         String str = readFile( generatedFile );
277         assertTrue( lowerCaseContains( str, "Sample.js" ) );
278         assertTrue( lowerCaseContains( str, "SampleDup.js" ) );
279     }
280 
281     public void testCpdJspConfiguration()
282             throws Exception
283     {
284         generateReport( "cpd", "default-configuration/cpd-jsp-plugin-config.xml" );
285 
286         // verify  the generated file to exist and violations are reported
287         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
288         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
289         String str = readFile( generatedFile );
290         assertTrue( lowerCaseContains( str, "sample.jsp" ) );
291         assertTrue( lowerCaseContains( str, "sampleDup.jsp" ) );
292     }
293 
294     public void testExclusionsConfiguration()
295             throws Exception
296     {
297         generateReport( "cpd", "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml" );
298 
299         // verify  the generated file to exist and no duplications are reported
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         assertEquals( 0, StringUtils.countMatches( str, "<duplication" ) );
304     }
305 }