/
PmdReportTest.java
816 lines (662 loc) · 38.6 KB
/
PmdReportTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
package org.apache.maven.plugins.pmd;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugins.pmd.exec.PmdExecutor;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.util.FileUtils;
import net.sourceforge.pmd.renderers.Renderer;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
/**
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
* @version $Id$
*/
public class PmdReportTest
extends AbstractPmdReportTest
{
/**
* {@inheritDoc}
*/
@Override
protected void setUp()
throws Exception
{
super.setUp();
Locale.setDefault( Locale.ENGLISH );
FileUtils.deleteDirectory( new File( getBasedir(), "target/test/unit" ) );
}
public void testDefaultConfiguration()
throws Exception
{
FileUtils.copyDirectoryStructure( new File( getBasedir(),
"src/test/resources/unit/default-configuration/jxr-files" ),
new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// check if the PMD files were generated
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// check if the rulesets, that have been applied, have been copied
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/maven-pmd-plugin-default.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// check if there's a link to the JXR files
String str = readFile( generatedFile );
assertTrue( str.contains( "/xref/def/configuration/App.html#L31" ) );
assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L45" ) );
// check if there's a priority column
assertTrue( str.contains( "<th>Priority</th>" ) );
// there should be a rule column
assertTrue( str.contains( "<th>Rule</th>" ) );
// along with a link to the rule
assertTrue( str.contains( "pmd_rules_java_bestpractices.html#unusedprivatefield\">UnusedPrivateField</a>" ) );
// there should be the section Violations By Priority
assertTrue( str.contains( "Violations By Priority</h2>" ) );
assertTrue( str.contains( "Priority 3</h3>" ) );
assertTrue( str.contains( "Priority 4</h3>" ) );
// the file App.java is mentioned 3 times: in prio 3, in prio 4 and in the files section
assertEquals( 3, StringUtils.countMatches( str, "def/configuration/App.java" ) );
// there must be no warnings (like deprecated rules) in the log output
String output = CapturingPrintStream.getOutput();
assertFalse( output.contains( "deprecated Rule name" ) );
assertFalse( output.contains( "Discontinue using Rule name" ) );
assertFalse( output.contains( "is referenced multiple times" ) );
// the version should be logged
assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
}
public void testDefaultConfigurationNotRenderRuleViolationPriority()
throws Exception
{
FileUtils.copyDirectoryStructure( new File( getBasedir(),
"src/test/resources/unit/default-configuration/jxr-files" ),
new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
// check that there's no priority column
assertFalse( str.contains( "<th>Priority</th>" ) );
}
public void testDefaultConfigurationNoRenderViolationsByPriority()
throws Exception
{
FileUtils.copyDirectoryStructure( new File( getBasedir(),
"src/test/resources/unit/default-configuration/jxr-files" ),
new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
// there should be no section Violations By Priority
assertFalse( str.contains( "Violations By Priority</h2>" ) );
assertFalse( str.contains( "Priority 3</h3>" ) );
assertFalse( str.contains( "Priority 4</h3>" ) );
// the file App.java is mentioned once: in the files section
assertEquals( 1, StringUtils.countMatches( str, "def/configuration/App.java" ) );
}
public void testDefaultConfigurationWithAnalysisCache()
throws Exception
{
FileUtils.copyDirectoryStructure( new File( getBasedir(),
"src/test/resources/unit/default-configuration/jxr-files" ),
new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site" ) );
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// check if the PMD analysis cache file has been generated
File cacheFile = new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/pmd/pmd.cache" );
assertTrue( FileUtils.fileExists( cacheFile.getAbsolutePath() ) );
}
public void testJavascriptConfiguration()
throws Exception
{
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// check if the PMD files were generated
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// these are the rulesets, that have been applied...
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/codestyle.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/errorprone.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
assertTrue( str.contains( "Avoid using global variables" ) );
}
public void testFileURL()
throws Exception
{
FileUtils.copyDirectoryStructure( new File( getBasedir(),
"src/test/resources/unit/default-configuration/jxr-files" ),
new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
// Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174).
int port = determineFreePort();
WireMockServer mockServer = new WireMockServer( port );
mockServer.start();
String sonarRuleset =
IOUtils.toString( getClass().getClassLoader().getResourceAsStream( "unit/default-configuration/rulesets/sonar-way-ruleset.xml" ),
StandardCharsets.UTF_8 );
String sonarMainPageHtml =
IOUtils.toString( getClass().getClassLoader().getResourceAsStream( "unit/default-configuration/rulesets/sonar-main-page.html" ),
StandardCharsets.UTF_8 );
final String sonarBaseUrl = "/profiles";
final String sonarProfileUrl = sonarBaseUrl + "/export?format=pmd&language=java&name=Sonar%2520way";
final String sonarExportRulesetUrl = "http://localhost:" + mockServer.port() + sonarProfileUrl;
WireMock.configureFor( "localhost", port );
WireMock.stubFor( WireMock.get( WireMock.urlEqualTo( sonarBaseUrl ) ).willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
"text/html" ).withBody( sonarMainPageHtml ) ) );
WireMock.stubFor( WireMock.get( WireMock.urlEqualTo( sonarProfileUrl ) ).willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
"text/xml" ).withBody( sonarRuleset ) ) );
URL url = getClass().getClassLoader().getResource( "rulesets/java/basic.xml" );
URL url2 = getClass().getClassLoader().getResource( "rulesets/java/unusedcode.xml" );
URL url3 = getClass().getClassLoader().getResource( "rulesets/java/imports.xml" );
mojo.setRulesets( new String[] { url.toString(), url2.toString(), url3.toString(), sonarExportRulesetUrl } );
mojo.execute();
// check if the PMD files were generated
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// the resolved and extracted rulesets
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/basic.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/imports.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/unusedcode.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile =
new File( getBasedir(),
"target/test/unit/default-configuration/target/pmd/rulesets/export_format_pmd_language_java_name_Sonar_2520way.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// check if there's a link to the JXR files
String str = readFile( generatedFile );
assertTrue( str.contains( "/xref/def/configuration/App.html#L31" ) );
assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L45" ) );
mockServer.stop();
}
private int determineFreePort()
{
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException( "Couldn't find a free port.", e );
}
}
/**
* With custom rulesets
*
* @throws Exception
*/
public void testCustomConfiguration()
throws Exception
{
File testPom =
new File( getBasedir(),
"src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// check the generated files
File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/pmd.csv" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/pmd/rulesets/custom.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// check if custom ruleset was applied
String str = readFile( generatedFile );
// codestyle.xml/ControlStatementBraces:
assertTrue( lowerCaseContains( str, "This statement should have braces" ) );
// Must be false as codestyle.xml/ControlStatementBraces with checkIfElseStmt=false is used
assertFalse( lowerCaseContains( str, "Avoid using if...else statements without curly braces" ) );
assertFalse( "unnecessary constructor should not be triggered because of low priority",
lowerCaseContains( str, "Avoid unnecessary constructors - the compiler will generate these for you" ) );
// veryLongVariableNameWithViolation is really too long
assertTrue( lowerCaseContains( str, "veryLongVariableNameWithViolation" ) );
// notSoLongVariableName should not be reported
assertFalse( lowerCaseContains( str, "notSoLongVariableName" ) );
}
/**
* Verify skip parameter
*
* @throws Exception
*/
public void testSkipConfiguration()
throws Exception
{
File testPom = new File( getBasedir(), "src/test/resources/unit/custom-configuration/skip-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// verify the generated files do not exist because PMD was skipped
File generatedFile = new File( getBasedir(), "target/test/unit/skip-configuration/target/pmd.csv" );
assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/custom.xml" );
assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" );
assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// the fact, the PMD execution has been skipped, should be logged
String output = CapturingPrintStream.getOutput();
assertTrue ( output.contains( "Skipping PMD execution" ) );
}
public void testSkipEmptyReportConfiguration()
throws Exception
{
File testPom =
new File( getBasedir(), "src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// verify the generated files do not exist because PMD was skipped
File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/pmd.html" );
assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
}
public void testEmptyReportConfiguration()
throws Exception
{
File testPom = new File( getBasedir(), "src/test/resources/unit/empty-report/empty-report-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// verify the generated files do exist, even if there are no violations
File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/pmd.html" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
assertFalse( lowerCaseContains( str, "Hello.java" ) );
assertEquals( 1, StringUtils.countMatches( str, "PMD found no problems in your source code." ) );
// no sections files or violations by priority
assertFalse( str.contains( "Files</h2>" ) );
assertFalse( str.contains( "Violations By Priority</h2>" ) );
}
public void testInvalidFormat()
throws Exception
{
try
{
File testPom =
new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
setVariableValueToObject( mojo, "compileSourceRoots", mojo.project.getCompileSourceRoots() );
mojo.executeReport( Locale.ENGLISH );
fail( "Must throw MavenReportException." );
}
catch ( Exception e )
{
assertTrue( true );
}
}
public void testInvalidTargetJdk()
throws Exception
{
try
{
File testPom =
new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-target-jdk-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
fail( "Must throw MavenReportException." );
}
catch ( Exception e )
{
assertTrue( true );
}
}
/**
* verify the pmd.xml file is included in the site when requested.
* @throws Exception
*/
public void testIncludeXmlInSite()
throws Exception
{
File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-report-include-xml-in-site-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// verify the pmd file is included in site
File generatedXmlFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedXmlFile.getAbsolutePath() ) );
String pmdXmlTarget = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" ) );
assertTrue( pmdXmlTarget.contains( "</pmd>" ) );
// check that pmd.xml file has the closing element
String pmdXml = readFile( generatedXmlFile );
assertTrue( pmdXml.contains( "</pmd>" ) );
}
/**
* Read the contents of the specified file object into a string
*
* @param file the file to be read
* @return a String object that contains the contents of the file
* @throws java.io.IOException
*/
private String readFile( File file )
throws IOException
{
try ( BufferedReader reader = new BufferedReader( new FileReader( file ) ) )
{
final StringBuilder str = new StringBuilder( (int) file.length() );
for ( String line = reader.readLine(); line != null; line = reader.readLine() )
{
str.append( ' ' );
str.append( line );
str.append( '\n' );
}
return str.toString();
}
}
/**
* Verify the correct working of the locationTemp method
*
* @throws Exception
*/
public void testLocationTemp()
throws Exception
{
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
assertEquals( "locationTemp is not correctly encoding filename",
"export_format_pmd_language_java_name_some_2520name.xml",
mojo.getLocationTemp( "http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name" ) );
}
/**
* Verify that suppressMarker works
*
* @throws Exception
*/
public void testSuppressMarkerConfiguration()
throws Exception
{
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// check if the PMD files were generated
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
// check that there is no violation reported for "unusedVar2" - as it is suppressed
assertFalse( str.contains( "Avoid unused private fields such as 'unusedVar2'.\n </violation>" ) );
// but it appears as suppressed
assertTrue( str.contains( "suppressiontype=\"nopmd\" msg=\"Avoid unused private fields such as 'unusedVar2'.\"" ));
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// check if there's a link to the JXR files
str = readFile( generatedFile );
assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L27" ) );
// suppressed violation
assertTrue( str.contains( "Avoid unused private fields such as 'unusedVar2'." ) );
}
public void testSuppressMarkerConfigurationWithoutRendering()
throws Exception
{
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// check if the PMD files were generated
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
// check that there is no violation reported for "unusedVar2" - as it is suppressed
assertFalse( str.contains( "Avoid unused private fields such as 'unusedVar2'.\n </violation>" ) );
// but it appears as suppressed
assertTrue( str.contains( "suppressiontype=\"nopmd\" msg=\"Avoid unused private fields such as 'unusedVar2'.\"" ));
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// check if there's a link to the JXR files
str = readFile( generatedFile );
assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L27" ) );
// suppressed violations are not rendered
assertFalse( str.contains( "Avoid unused private fields such as 'unusedVar2'." ) );
}
public void testJspConfiguration()
throws Exception
{
File testPom = new File( getBasedir(),
"src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
// check if the PMD files were generated
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
// these are the rulesets, that have been applied...
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/codestyle.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/design.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/errorprone.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/security.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
assertTrue(str.contains("JSP file should use UTF-8 encoding"));
assertTrue(str.contains("Using unsanitized JSP expression can lead to Cross Site Scripting (XSS) attacks"));
assertTrue(str.contains("Avoid having style information in JSP files."));
}
public void testPMDProcessingError()
throws Exception
{
File testPom = new File( getBasedir(),
"src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
try {
mojo.execute();
fail("Expected exception");
} catch (RuntimeException e) {
assertTrue( e.getMessage().endsWith( "Found 1 PMD processing errors" ) );
}
}
public void testPMDProcessingErrorWithDetailsSkipped()
throws Exception
{
File testPom = new File( getBasedir(),
"src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
String output = CapturingPrintStream.getOutput();
assertTrue ( output.contains( "There are 1 PMD processing errors:" ) );
File generatedFile = new File( getBasedir(), "target/test/unit/parse-error/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
assertTrue( str.contains( "Error while parsing" ) );
// The parse exception must be in the XML report
assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
str = readFile( generatedFile );
// The parse exception must also be in the HTML report
assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
}
public void testPMDProcessingErrorWithDetailsNoReport()
throws Exception
{
File testPom = new File( getBasedir(),
"src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
String output = CapturingPrintStream.getOutput();
assertTrue ( output.contains( "There are 1 PMD processing errors:" ) );
File generatedFile = new File( getBasedir(), "target/test/unit/parse-error/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
assertTrue( str.contains( "Error while parsing" ) );
// The parse exception must be in the XML report
assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
str = readFile( generatedFile );
// The parse exception must NOT be in the HTML report, since reportProcessingErrors is false
assertFalse( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
}
public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception {
File testPom = new File(getBasedir(), "src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml");
PmdReport mojo = (PmdReport) lookupMojo ("pmd", testPom);
mojo.execute();
File generatedFile = new File( getBasedir(), "target/test/unit/exclude-roots/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
assertTrue( "Seems like all directories are excluded now", str.contains("ForLoopShouldBeWhileLoop") );
assertFalse( "Exclusion of an exact source directory not working", str.contains( "OverrideBothEqualsAndHashcode" ) );
assertFalse( "Exclusion of basedirectory with subdirectories not working (MPMD-178)", str.contains( "JumbledIncrementer") );
}
public void testViolationExclusion()
throws Exception
{
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml" );
final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
assertEquals(0, StringUtils.countMatches(str, "<violation"));
}
public void testCustomRenderer() throws MavenReportException
{
final Renderer renderer = PmdExecutor.createRenderer( "net.sourceforge.pmd.renderers.TextRenderer", "UTF-8" );
assertNotNull(renderer);
}
public void testCodeClimateRenderer() throws MavenReportException
{
final Renderer renderer = PmdExecutor.createRenderer( "net.sourceforge.pmd.renderers.CodeClimateRenderer", "UTF-8" );
assertNotNull(renderer);
}
public void testPmdReportCustomRulesNoExternalInfoUrl()
throws Exception
{
FileUtils.copyDirectoryStructure( new File( getBasedir(),
"src/test/resources/unit/default-configuration/jxr-files" ),
new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
String str = readFile( generatedFile );
// custom rule without link
assertEquals( 2, StringUtils.countMatches( str, "<td>CustomRule</td>" ) );
// standard rule with link
assertEquals( 4, StringUtils.countMatches( str, "\">UnusedPrivateField</a></td>" ) );
}
public void testPmdReportResolveRulesets()
throws Exception
{
int port = determineFreePort();
WireMockServer mockServer = new WireMockServer( port );
mockServer.start();
String sonarRuleset =
IOUtils.toString( getClass().getClassLoader().getResourceAsStream( "unit/default-configuration/rulesets/sonar-way-ruleset.xml" ),
StandardCharsets.UTF_8 );
final String sonarProfileUrl = "/profiles/export?format=pmd&language=java&name=Sonar%2520way";
final String sonarExportRulesetUrl = "http://localhost:" + mockServer.port() + sonarProfileUrl;
final String myRulesetBaseUrl = "/config/my-ruleset.xml";
final String myRulesetUrl = "http://localhost:" + mockServer.port() + myRulesetBaseUrl;
final String notAInternalRulesetBaseUrl = "/projects/OURPROJECT/repos/ourproject-pmd/raw/InProgressRuleset.xml?at=refs%2Fheads%2Fmaster";
final String notAInternalRulesetUrl = "http://localhost:" + mockServer.port() + notAInternalRulesetBaseUrl;
WireMock.configureFor( "localhost", port );
WireMock.stubFor( WireMock.get( WireMock.urlEqualTo( sonarProfileUrl ) )
.willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
"text/xml" ).withBody( sonarRuleset ) ) );
WireMock.stubFor( WireMock.get( WireMock.urlEqualTo( myRulesetBaseUrl ) )
.willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
"text/xml" ).withBody( sonarRuleset ) ) );
WireMock.stubFor( WireMock.get( WireMock.urlEqualTo( notAInternalRulesetBaseUrl ) )
.willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
"text/xml" ).withBody( sonarRuleset ) ) );
FileUtils.copyDirectoryStructure( new File( getBasedir(),
"src/test/resources/unit/default-configuration/jxr-files" ),
new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
File testPom =
new File( getBasedir(),
"src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.rulesets[3] = sonarExportRulesetUrl;
mojo.rulesets[4] = myRulesetUrl;
mojo.rulesets[5] = notAInternalRulesetUrl;
mojo.execute();
// these are the rulesets, that have been copied to target/pmd/rulesets
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/custom-rules.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/java-design.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/export_format_pmd_language_java_name_Sonar_2520way.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/my-ruleset.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/InProgressRuleset.xml_at_refs_2Fheads_2Fmaster.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
mockServer.stop();
}
}