View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.pmd;
20  
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  
24  import java.io.File;
25  import java.util.Locale;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.commons.lang3.StringUtils;
29  import org.w3c.dom.Document;
30  
31  /**
32   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
33   * @version $Id$
34   */
35  public class CpdReportTest extends AbstractPmdReportTestCase {
36      /**
37       * {@inheritDoc}
38       */
39      @Override
40      protected void setUp() throws Exception {
41          super.setUp();
42          Locale.setDefault(Locale.ENGLISH);
43          FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit"));
44      }
45  
46      /**
47       * Test CPDReport given the default configuration
48       *
49       * @throws Exception
50       */
51      public void testDefaultConfiguration() throws Exception {
52          File generatedReport =
53                  generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
54          assertTrue(new File(generatedReport.getAbsolutePath()).exists());
55  
56          // check if the CPD files were generated
57          File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
58          assertTrue(new File(generatedFile.getAbsolutePath()).exists());
59  
60          // check the contents of cpd.html
61          String str = readFile(generatedReport);
62          assertTrue(lowerCaseContains(str, "AppSample.java"));
63          assertTrue(lowerCaseContains(str, "App.java"));
64          assertTrue(lowerCaseContains(str, "public String dup( String str )"));
65          assertTrue(lowerCaseContains(str, "tmp = tmp + str.substring( i, i + 1);"));
66  
67          // the version should be logged
68          String output = CapturingPrintStream.getOutput();
69          assertTrue(output.contains("PMD version: " + AbstractPmdReport.getPmdVersion()));
70      }
71  
72      /**
73       * Test CPDReport with the text renderer given as "format=txt"
74       *
75       * @throws Exception
76       */
77      public void testTxtFormat() throws Exception {
78          generateReport("cpd", "custom-configuration/cpd-txt-format-configuration-plugin-config.xml");
79  
80          // check if the CPD files were generated
81          File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml");
82          assertTrue(new File(generatedFile.getAbsolutePath()).exists());
83          generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.txt");
84          assertTrue(new File(generatedFile.getAbsolutePath()).exists());
85  
86          // check the contents of cpd.txt
87          String str = readFile(generatedFile);
88          // Contents that should NOT be in the report
89          assertFalse(lowerCaseContains(str, "public static void main( String[] args )"));
90          // Contents that should be in the report
91          assertTrue(lowerCaseContains(str, "public void duplicateMethod( int i )"));
92      }
93  
94      /**
95       * Test CPDReport using custom configuration
96       *
97       * @throws Exception
98       */
99      public void testCustomConfiguration() throws Exception {
100         File generatedReport = generateReport("cpd", "custom-configuration/cpd-custom-configuration-plugin-config.xml");
101         assertTrue(new File(generatedReport.getAbsolutePath()).exists());
102 
103         // check if the CPD files were generated
104         File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv");
105         assertTrue(new File(generatedFile.getAbsolutePath()).exists());
106 
107         String str = readFile(generatedReport);
108         // Contents that should NOT be in the report
109         assertFalse(lowerCaseContains(str, "/Sample.java"));
110         assertFalse(lowerCaseContains(str, "public void duplicateMethod( int i )"));
111         // Contents that should be in the report
112         assertTrue(lowerCaseContains(str, "AnotherSample.java"));
113         assertTrue(lowerCaseContains(str, "public static void main( String[] args )"));
114         assertTrue(lowerCaseContains(str, "private String unusedMethod("));
115     }
116 
117     /**
118      * Test CPDReport with invalid format
119      *
120      * @throws Exception
121      */
122     public void testInvalidFormat() throws Exception {
123         try {
124             File testPom = new File(
125                     getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml");
126             AbstractPmdReport mojo = createReportMojo("cpd", testPom);
127             setVariableValueToObject(
128                     mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots());
129             generateReport(mojo, testPom);
130 
131             fail("MavenReportException must be thrown");
132         } catch (Exception e) {
133             assertTrue(true);
134         }
135     }
136 
137     public void testWriteNonHtml() throws Exception {
138         generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
139 
140         // check if the CPD files were generated
141         File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
142         assertTrue(new File(generatedFile.getAbsolutePath()).exists());
143 
144         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
145         Document pmdCpdDocument = builder.parse(generatedFile);
146         assertNotNull(pmdCpdDocument);
147 
148         String str = readFile(generatedFile);
149         assertTrue(lowerCaseContains(str, "AppSample.java"));
150         assertTrue(lowerCaseContains(str, "App.java"));
151         assertTrue(lowerCaseContains(str, "public String dup( String str )"));
152         assertTrue(lowerCaseContains(str, "tmp = tmp + str.substring( i, i + 1);"));
153     }
154 
155     /**
156      * verify the cpd.xml file is included in the site when requested.
157      * @throws Exception
158      */
159     public void testIncludeXmlInSite() throws Exception {
160         generateReport("cpd", "default-configuration/cpd-report-include-xml-in-site-plugin-config.xml");
161 
162         File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
163         assertTrue(new File(generatedFile.getAbsolutePath()).exists());
164 
165         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
166         Document pmdCpdDocument = builder.parse(generatedFile);
167         assertNotNull(pmdCpdDocument);
168 
169         String str = readFile(generatedFile);
170         assertTrue(str.contains("</pmd-cpd>"));
171 
172         File siteReport = new File(getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml");
173         assertTrue(new File(siteReport.getAbsolutePath()).exists());
174         String siteReportContent = readFile(siteReport);
175         assertTrue(siteReportContent.contains("</pmd-cpd>"));
176         assertEquals(str, siteReportContent);
177     }
178 
179     public void testSkipEmptyReportConfiguration() throws Exception {
180         // verify the generated files do not exist because PMD was skipped
181         File generatedReport = generateReport("cpd", "empty-report/cpd-skip-empty-report-plugin-config.xml");
182         assertFalse(new File(generatedReport.getAbsolutePath()).exists());
183     }
184 
185     public void testEmptyReportConfiguration() throws Exception {
186         // verify the generated files do exist, even if there are no violations
187         File generatedReport = generateReport("cpd", "empty-report/cpd-empty-report-plugin-config.xml");
188         assertTrue(new File(generatedReport.getAbsolutePath()).exists());
189 
190         String str = readFile(generatedReport);
191         assertFalse(lowerCaseContains(str, "Hello.java"));
192         assertTrue(str.contains("CPD found no problems in your source code."));
193     }
194 
195     public void testCpdEncodingConfiguration() throws Exception {
196         String originalEncoding = System.getProperty("file.encoding");
197         try {
198             System.setProperty("file.encoding", "UTF-16");
199 
200             generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
201 
202             // check if the CPD files were generated
203             File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
204             assertTrue(new File(generatedFile.getAbsolutePath()).exists());
205             String str = readFile(generatedFile);
206             assertTrue(lowerCaseContains(str, "AppSample.java"));
207         } finally {
208             System.setProperty("file.encoding", originalEncoding);
209         }
210     }
211 
212     public void testCpdJavascriptConfiguration() throws Exception {
213         generateReport("cpd", "default-configuration/cpd-javascript-plugin-config.xml");
214 
215         // verify  the generated file to exist and violations are reported
216         File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
217         assertTrue(new File(generatedFile.getAbsolutePath()).exists());
218         String str = readFile(generatedFile);
219         assertTrue(lowerCaseContains(str, "Sample.js"));
220         assertTrue(lowerCaseContains(str, "SampleDup.js"));
221     }
222 
223     public void testCpdJspConfiguration() throws Exception {
224         generateReport("cpd", "default-configuration/cpd-jsp-plugin-config.xml");
225 
226         // verify  the generated file to exist and violations are reported
227         File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
228         assertTrue(new File(generatedFile.getAbsolutePath()).exists());
229         String str = readFile(generatedFile);
230         assertTrue(lowerCaseContains(str, "sample.jsp"));
231         assertTrue(lowerCaseContains(str, "sampleDup.jsp"));
232     }
233 
234     public void testExclusionsConfiguration() throws Exception {
235         generateReport("cpd", "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml");
236 
237         // verify  the generated file to exist and no duplications are reported
238         File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
239         assertTrue(new File(generatedFile.getAbsolutePath()).exists());
240         String str = readFile(generatedFile);
241         assertEquals(0, StringUtils.countMatches(str, "<duplication"));
242     }
243 }