1 package org.apache.maven.plugins.pmd;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39 public class CpdReportTest
40 extends AbstractPmdReportTestCase
41 {
42
43
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
56
57
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
66 File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
67 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
68
69
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
77 String output = CapturingPrintStream.getOutput();
78 assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
79 }
80
81
82
83
84
85
86 public void testTxtFormat()
87 throws Exception
88 {
89 generateReport( "cpd", "custom-configuration/cpd-txt-format-configuration-plugin-config.xml" );
90
91
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
98 String str = readFile( generatedFile );
99
100 assertFalse( lowerCaseContains( str, "public static void main( String[] args )" ) );
101
102 assertTrue( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
103 }
104
105
106
107
108
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
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
122 assertFalse( lowerCaseContains( str, "/Sample.java" ) );
123 assertFalse( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
124
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
132
133
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
157
158
159
160
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
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
201
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
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
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
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
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
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
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 }