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 AbstractPmdReportTest
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 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
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
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 String output = CapturingPrintStream.getOutput();
85 assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
86 }
87
88
89
90
91
92
93 public void testTxtFormat()
94 throws Exception
95 {
96 File testPom =
97 new File( getBasedir(),
98 "src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml" );
99 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
100 mojo.execute();
101
102
103 File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml" );
104 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
105 generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.txt" );
106 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
107
108
109 String str = readFile( generatedFile );
110
111 assertFalse( lowerCaseContains( str, "public static void main( String[] args )" ) );
112
113 assertTrue( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
114 }
115
116
117
118
119
120
121 public void testCustomConfiguration()
122 throws Exception
123 {
124 File testPom =
125 new File( getBasedir(),
126 "src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml" );
127 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
128 mojo.execute();
129
130
131 File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv" );
132 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
133
134 generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" );
135 renderer( mojo, generatedFile );
136 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
137
138
139 String str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
140 assertFalse( lowerCaseContains( str, "/Sample.java" ) );
141
142 str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
143 assertFalse( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
144
145
146 str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
147 assertTrue( lowerCaseContains( str, "AnotherSample.java" ) );
148
149 str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
150 assertTrue( lowerCaseContains( str, "public static void main( String[] args )" ) );
151
152 str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
153 assertTrue( lowerCaseContains( str, "private String unusedMethod(" ) );
154 }
155
156
157
158
159
160
161 public void testInvalidFormat()
162 throws Exception
163 {
164 try
165 {
166 File testPom =
167 new File( getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml" );
168 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
169 setVariableValueToObject( mojo, "compileSourceRoots", mojo.project.getCompileSourceRoots() );
170 mojo.execute();
171
172 fail( "MavenReportException must be thrown" );
173 }
174 catch ( Exception e )
175 {
176 assertTrue( true );
177 }
178
179 }
180
181
182
183
184
185
186
187
188 private String readFile( File file )
189 throws IOException
190 {
191 String strTmp;
192 StringBuilder str = new StringBuilder( (int) file.length() );
193 try ( BufferedReader in = new BufferedReader( new FileReader( file ) ) )
194 {
195 while ( ( strTmp = in.readLine() ) != null )
196 {
197 str.append( ' ' );
198 str.append( strTmp );
199 }
200 }
201
202 return str.toString();
203 }
204
205 public void testWriteNonHtml()
206 throws Exception
207 {
208 File testPom =
209 new File( getBasedir(),
210 "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
211 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
212 assertNotNull( mojo );
213 mojo.execute();
214
215 File tReport = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
216
217 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
218 Document pmdCpdDocument = builder.parse( tReport );
219 assertNotNull( pmdCpdDocument );
220
221 String str = readFile( tReport );
222 assertTrue( lowerCaseContains( str, "AppSample.java" ) );
223 assertTrue( lowerCaseContains( str, "App.java" ) );
224 assertTrue( lowerCaseContains( str, "public String dup( String str )" ) );
225 assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
226 }
227
228
229
230
231
232 public void testIncludeXmlInSite()
233 throws Exception
234 {
235 File testPom =
236 new File( getBasedir(),
237 "src/test/resources/unit/default-configuration/cpd-report-include-xml-in-site-plugin-config.xml" );
238 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
239 assertNotNull( mojo );
240 mojo.execute();
241
242 File tReport = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
243 assertTrue( FileUtils.fileExists( tReport.getAbsolutePath() ) );
244
245 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
246 Document pmdCpdDocument = builder.parse( tReport );
247 assertNotNull( pmdCpdDocument );
248
249 String str = readFile( tReport );
250 assertTrue( str.contains( "</pmd-cpd>" ) );
251
252 File siteReport = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml" );
253 assertTrue( FileUtils.fileExists( siteReport.getAbsolutePath() ) );
254 String siteReportContent = readFile( siteReport );
255 assertTrue( siteReportContent.contains( "</pmd-cpd>" ) );
256 assertEquals( str, siteReportContent );
257 }
258
259
260 public void testSkipEmptyReportConfiguration()
261 throws Exception
262 {
263 File testPom =
264 new File( getBasedir(), "src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml" );
265 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
266 mojo.execute();
267
268
269 File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" );
270 assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
271 }
272
273 public void testEmptyReportConfiguration()
274 throws Exception
275 {
276 File testPom =
277 new File( getBasedir(), "src/test/resources/unit/empty-report/cpd-empty-report-plugin-config.xml" );
278 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
279 mojo.execute();
280
281
282 File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" );
283 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
284 String str = readFile( new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" ) );
285 assertFalse( lowerCaseContains( str, "Hello.java" ) );
286 assertTrue( str.contains( "CPD found no problems in your source code." ) );
287 }
288
289 public void testCpdEncodingConfiguration()
290 throws Exception
291 {
292 String originalEncoding = System.getProperty( "file.encoding" );
293 try
294 {
295 System.setProperty( "file.encoding", "UTF-16" );
296
297 File testPom =
298 new File( getBasedir(),
299 "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
300 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
301 mojo.execute();
302
303
304 File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
305 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
306 String str = readFile( generatedFile );
307 assertTrue( lowerCaseContains( str, "AppSample.java" ) );
308 }
309 finally
310 {
311 System.setProperty( "file.encoding", originalEncoding );
312 }
313 }
314
315 public void testCpdJavascriptConfiguration()
316 throws Exception
317 {
318 File testPom =
319 new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml" );
320 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
321 mojo.execute();
322
323
324 File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
325 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
326 String str = readFile( generatedFile );
327 assertTrue( lowerCaseContains( str, "Sample.js" ) );
328 assertTrue( lowerCaseContains( str, "SampleDup.js" ) );
329 }
330
331 public void testCpdJspConfiguration()
332 throws Exception
333 {
334 File testPom =
335 new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml" );
336 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
337 mojo.execute();
338
339
340 File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
341 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
342 String str = readFile( generatedFile );
343 assertTrue( lowerCaseContains( str, "sample.jsp" ) );
344 assertTrue( lowerCaseContains( str, "sampleDup.jsp" ) );
345 }
346
347 public void testExclusionsConfiguration()
348 throws Exception
349 {
350 File testPom =
351 new File( getBasedir(),
352 "src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml" );
353 final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
354 mojo.execute();
355
356
357 File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
358 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
359 String str = readFile( generatedFile );
360 assertEquals( 0, StringUtils.countMatches( str, "<duplication" ) );
361 }
362 }