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
85
86
87
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
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
105 String str = readFile( generatedFile );
106
107 assertFalse( lowerCaseContains( str, "public static void main( String[] args )" ) );
108
109 assertTrue( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
110 }
111
112
113
114
115
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
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
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
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
154
155
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
179
180
181
182
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
226
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
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
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
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
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
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
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 }