View Javadoc
1   package org.apache.maven.plugins.pmd;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.BufferedReader;
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.FileReader;
26  import java.io.IOException;
27  import java.io.PrintStream;
28  import java.net.ServerSocket;
29  import java.net.URL;
30  import java.nio.charset.StandardCharsets;
31  import java.util.Locale;
32  
33  import org.apache.commons.io.IOUtils;
34  import org.apache.commons.lang3.StringUtils;
35  import org.codehaus.plexus.util.FileUtils;
36  
37  import com.github.tomakehurst.wiremock.WireMockServer;
38  import com.github.tomakehurst.wiremock.client.WireMock;
39  
40  /**
41   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
42   * @version $Id$
43   */
44  public class PmdReportTest
45      extends AbstractPmdReportTest
46  {
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      protected void setUp()
52          throws Exception
53      {
54          super.setUp();
55          Locale.setDefault( Locale.ENGLISH );
56          FileUtils.deleteDirectory( new File( getBasedir(), "target/test/unit" ) );
57      }
58  
59      public void testDefaultConfiguration()
60          throws Exception
61      {
62          FileUtils.copyDirectoryStructure( new File( getBasedir(),
63                                                      "src/test/resources/unit/default-configuration/jxr-files" ),
64                                            new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
65  
66          File testPom =
67              new File( getBasedir(),
68                        "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
69          PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
70          mojo.execute();
71  
72          // check if the PMD files were generated
73          File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
74          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
75  
76          // check if the rulesets, that have been applied, have been copied
77          generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/maven-pmd-plugin-default.xml" );
78          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
79  
80          generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
81          renderer( mojo, generatedFile );
82          assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
83  
84          // check if there's a link to the JXR files
85          String str = readFile( generatedFile );
86  
87          assertTrue( str.contains( "/xref/def/configuration/App.html#L31" ) );
88  
89          assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L45" ) );
90  
91          // check if there's a priority column
92          assertTrue( str.contains( "Priority" ) );
93      }
94  
95      public void testDefaultConfigurationNotRenderRuleViolationPriority()
96              throws Exception
97      {
98          FileUtils.copyDirectoryStructure( new File( getBasedir(),
99                                                      "src/test/resources/unit/default-configuration/jxr-files" ),
100                                           new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
101 
102         File testPom =
103             new File( getBasedir(),
104                       "src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml" );
105         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
106         mojo.execute();
107 
108         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
109         renderer( mojo, generatedFile );
110         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
111 
112         String str = readFile( generatedFile );
113 
114         // check that there's no priority column
115         assertFalse( str.contains( "Priority" ) );
116     }
117 
118     public void testDefaultConfigurationWithAnalysisCache()
119             throws Exception
120     {
121         FileUtils.copyDirectoryStructure( new File( getBasedir(),
122                                                     "src/test/resources/unit/default-configuration/jxr-files" ),
123                                           new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site" ) );
124 
125         File testPom =
126             new File( getBasedir(),
127                       "src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml" );
128         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
129         mojo.execute();
130 
131         // check if the PMD analysis cache file has been generated
132         File cacheFile = new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/pmd/pmd.cache" );
133         assertTrue( FileUtils.fileExists( cacheFile.getAbsolutePath() ) );
134     }
135 
136     public void testJavascriptConfiguration()
137         throws Exception
138     {
139         File testPom =
140             new File( getBasedir(),
141                       "src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml" );
142         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
143         mojo.execute();
144 
145         // check if the PMD files were generated
146         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
147         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
148 
149         // these are the rulesets, that have been applied...
150         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/bestpractices.xml" );
151         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
152 
153         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/codestyle.xml" );
154         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
155 
156         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/errorprone.xml" );
157         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
158 
159         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
160         renderer( mojo, generatedFile );
161         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
162 
163         String str = readFile( generatedFile );
164         assertTrue( str.contains( "Avoid using global variables" ) );
165     }
166 
167     public void testFileURL()
168         throws Exception
169     {
170         FileUtils.copyDirectoryStructure( new File( getBasedir(),
171                                                     "src/test/resources/unit/default-configuration/jxr-files" ),
172                                           new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
173 
174         File testPom =
175             new File( getBasedir(),
176                       "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
177         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
178 
179         // Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174).
180         int port = determineFreePort();
181         WireMockServer mockServer = new WireMockServer( port );
182         mockServer.start();
183 
184         String sonarRuleset =
185             IOUtils.toString( getClass().getClassLoader().getResourceAsStream( "unit/default-configuration/rulesets/sonar-way-ruleset.xml" ),
186                     StandardCharsets.UTF_8 );
187 
188         String sonarMainPageHtml =
189             IOUtils.toString( getClass().getClassLoader().getResourceAsStream( "unit/default-configuration/rulesets/sonar-main-page.html" ),
190                     StandardCharsets.UTF_8 );
191 
192         final String sonarBaseUrl = "/profiles";
193         final String sonarProfileUrl = sonarBaseUrl + "/export?format=pmd&language=java&name=Sonar%2520way";
194         final String sonarExportRulesetUrl = "http://localhost:" + mockServer.port() + sonarProfileUrl;
195 
196         mockServer.stubFor( WireMock.get( WireMock.urlEqualTo( sonarBaseUrl ) ).willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
197                                                                                                                                                "text/html" ).withBody( sonarMainPageHtml ) ) );
198 
199         mockServer.stubFor( WireMock.get( WireMock.urlEqualTo( sonarProfileUrl ) ).willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
200                                                                                                                                                   "text/xml" ).withBody( sonarRuleset ) ) );
201 
202         URL url = getClass().getClassLoader().getResource( "rulesets/java/basic.xml" );
203         URL url2 = getClass().getClassLoader().getResource( "rulesets/java/unusedcode.xml" );
204         URL url3 = getClass().getClassLoader().getResource( "rulesets/java/imports.xml" );
205         mojo.setRulesets( new String[] { url.toString(), url2.toString(), url3.toString(), sonarExportRulesetUrl } );
206 
207         mojo.execute();
208 
209         // check if the PMD files were generated
210         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
211         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
212 
213         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/basic.xml" );
214         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
215 
216         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/imports.xml" );
217         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
218 
219         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/unusedcode.xml" );
220         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
221 
222         generatedFile =
223             new File( getBasedir(),
224                       "target/test/unit/default-configuration/target/export_format_pmd_language_java_name_Sonar_2520way.xml" );
225         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
226 
227         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
228         renderer( mojo, generatedFile );
229         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
230 
231         // check if there's a link to the JXR files
232         String str = readFile( generatedFile );
233 
234         assertTrue( str.contains( "/xref/def/configuration/App.html#L31" ) );
235 
236         assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L45" ) );
237 
238         mockServer.stop();
239     }
240 
241     private int determineFreePort()
242     {
243         try (ServerSocket socket = new ServerSocket(0)) {
244             return socket.getLocalPort();
245         } catch (IOException e) {
246             throw new RuntimeException( "Couldn't find a free port.", e );
247         }
248     }
249 
250     /**
251      * With custom rulesets
252      *
253      * @throws Exception
254      */
255     public void testCustomConfiguration()
256         throws Exception
257     {
258         File testPom =
259             new File( getBasedir(),
260                       "src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml" );
261 
262         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
263         mojo.execute();
264 
265         // check the generated files
266         File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/pmd.csv" );
267         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
268 
269         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/custom.xml" );
270         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
271 
272         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" );
273         renderer( mojo, generatedFile );
274         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
275 
276         // check if custom ruleset was applied
277         String str = readFile( generatedFile );
278 
279         // codestyle.xml/ControlStatementBraces:
280         assertTrue( lowerCaseContains( str, "This statement should have braces" ) );
281 
282         // Must be false as codestyle.xml/ControlStatementBraces with checkIfElseStmt=false is used
283         assertFalse( lowerCaseContains( str, "Avoid using if...else statements without curly braces" ) );
284 
285         assertFalse( "unnecessary constructor should not be triggered because of low priority",
286                     lowerCaseContains( str, "Avoid unnecessary constructors - the compiler will generate these for you" ) );
287 
288         // veryLongVariableNameWithViolation is really too long
289         assertTrue( lowerCaseContains( str, "veryLongVariableNameWithViolation" ) );
290         // notSoLongVariableName should not be reported
291         assertFalse( lowerCaseContains( str, "notSoLongVariableName" ) );
292     }
293 
294     /**
295      * Verify skip parameter
296      *
297      * @throws Exception
298      */
299     public void testSkipConfiguration()
300         throws Exception
301     {
302         File testPom = new File( getBasedir(), "src/test/resources/unit/custom-configuration/skip-plugin-config.xml" );
303         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
304         mojo.execute();
305 
306         // verify the generated files do not exist because PMD was skipped
307         File generatedFile = new File( getBasedir(), "target/test/unit/skip-configuration/target/pmd.csv" );
308         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
309 
310         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/custom.xml" );
311         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
312 
313         generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" );
314         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
315     }
316 
317     public void testSkipEmptyReportConfiguration()
318         throws Exception
319     {
320         File testPom =
321             new File( getBasedir(), "src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml" );
322         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
323         mojo.execute();
324 
325         // verify the generated files do not exist because PMD was skipped
326         File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/pmd.html" );
327         assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
328     }
329 
330     public void testEmptyReportConfiguration()
331         throws Exception
332     {
333         File testPom = new File( getBasedir(), "src/test/resources/unit/empty-report/empty-report-plugin-config.xml" );
334         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
335         mojo.execute();
336 
337         // verify the generated files do exist, even if there are no violations
338         File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/pmd.html" );
339         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
340         String str = readFile( generatedFile );
341         assertFalse( lowerCaseContains( str, "Hello.java" ) );
342     }
343 
344     public void testInvalidFormat()
345         throws Exception
346     {
347         try
348         {
349             File testPom =
350                 new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml" );
351             PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
352             setVariableValueToObject( mojo, "compileSourceRoots", mojo.project.getCompileSourceRoots() );
353             mojo.executeReport( Locale.ENGLISH );
354 
355             fail( "Must throw MavenReportException." );
356         }
357         catch ( Exception e )
358         {
359             assertTrue( true );
360         }
361     }
362 
363     public void testInvalidTargetJdk()
364         throws Exception
365     {
366         try
367         {
368             File testPom =
369                 new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-target-jdk-plugin-config.xml" );
370             PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
371             mojo.execute();
372 
373             fail( "Must throw MavenReportException." );
374         }
375         catch ( Exception e )
376         {
377             assertTrue( true );
378         }
379     }
380 
381     /**
382      * verify the pmd.xml file is included in the site when requested.
383      * @throws Exception
384      */
385     public void testIncludeXmlInSite()
386             throws Exception
387     {
388         File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-report-include-xml-in-site-plugin-config.xml" );
389         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
390         mojo.execute();
391 
392         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
393         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
394         // verify the pmd file is included in site
395         File generatedXmlFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.xml" );
396         assertTrue( FileUtils.fileExists( generatedXmlFile.getAbsolutePath() ) );
397 
398         String pmdXmlTarget = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" ) );
399         assertTrue( pmdXmlTarget.contains( "</pmd>" ) );
400 
401         // check that pmd.xml file has the closing element
402         String pmdXml = readFile( generatedXmlFile );
403         assertTrue( pmdXml.contains( "</pmd>" ) );
404     }
405 
406     /**
407      * Read the contents of the specified file object into a string
408      *
409      * @param file the file to be read
410      * @return a String object that contains the contents of the file
411      * @throws java.io.IOException
412      */
413     private String readFile( File file )
414         throws IOException
415     {
416         try ( BufferedReader reader = new BufferedReader( new FileReader( file ) ) )
417         {
418             final StringBuilder str = new StringBuilder( (int) file.length() );
419 
420             for ( String line = reader.readLine(); line != null; line = reader.readLine() )
421             {
422                 str.append( ' ' );
423                 str.append( line );
424                 str.append( '\n' );
425             }
426             return str.toString();
427         }
428     }
429 
430     /**
431      * Verify the correct working of the locationTemp method
432      *
433      * @throws Exception
434      */
435     public void testLocationTemp()
436         throws Exception
437     {
438 
439         File testPom =
440             new File( getBasedir(),
441                       "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
442         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
443 
444         assertEquals( "locationTemp is not correctly encoding filename",
445                       "export_format_pmd_language_java_name_some_2520name.xml",
446                       mojo.getLocationTemp( "http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name" ) );
447 
448     }
449 
450     /**
451      * Verify that suppressMarker works
452      *
453      * @throws Exception
454      */
455     public void testSuppressMarkerConfiguration()
456         throws Exception
457     {
458         File testPom =
459             new File( getBasedir(),
460                       "src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml" );
461         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
462         mojo.execute();
463 
464         // check if the PMD files were generated
465         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
466         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
467 
468         String str = readFile( generatedFile );
469 
470         // check that there is no violation reported for "unusedVar2" - as it is suppressed
471         assertFalse( str.contains( "Avoid unused private fields such as 'unusedVar2'." ) );
472     }
473 
474     public void testJspConfiguration()
475             throws Exception
476     {
477         File testPom = new File( getBasedir(),
478                 "src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml" );
479         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
480         mojo.execute();
481 
482         // check if the PMD files were generated
483         File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
484         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
485 
486         // these are the rulesets, that have been applied...
487         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/bestpractices.xml" );
488         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
489 
490         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/codestyle.xml" );
491         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
492 
493         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/design.xml" );
494         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
495 
496         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/errorprone.xml" );
497         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
498 
499         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/security.xml" );
500         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
501 
502         generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
503         renderer( mojo, generatedFile );
504         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
505 
506         String str = readFile( generatedFile );
507         assertTrue(str.contains("JSP file should use UTF-8 encoding"));
508         assertTrue(str.contains("Using unsanitized JSP expression can lead to Cross Site Scripting (XSS) attacks"));
509         assertTrue(str.contains("Avoid having style information in JSP files."));
510     }
511 
512     public void testPMDProcessingError()
513             throws Exception
514     {
515         File testPom = new File( getBasedir(),
516                 "src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml" );
517         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
518         try {
519             mojo.execute();
520             fail("Expected exception");
521         } catch (RuntimeException e) {
522             assertTrue( e.getMessage().endsWith( "Found 1 PMD processing errors" ) );
523         }
524     }
525 
526     public void testPMDProcessingErrorWithDetailsSkipped()
527             throws Exception
528     {
529         File testPom = new File( getBasedir(),
530                 "src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml" );
531         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
532 
533         PrintStream originalOut = System.out;
534         ByteArrayOutputStream logging = new ByteArrayOutputStream();
535         System.setOut( new PrintStream( logging ) );
536 
537         try {
538             mojo.execute();
539             String output = logging.toString();
540             assertTrue ( output.contains( "There are 1 PMD processing errors:" ) );
541 
542             File generatedFile = new File( getBasedir(), "target/test/unit/parse-error/target/pmd.xml" );
543             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
544             String str = readFile( generatedFile );
545             assertTrue( str.contains( "Error while parsing" ) );
546             // The parse exception must be in the XML report
547             assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
548 
549             generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
550             renderer( mojo, generatedFile );
551             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
552             str = readFile( generatedFile );
553             // The parse exception must also be in the HTML report
554             assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
555 
556         } finally {
557             System.setOut( originalOut );
558             System.out.println( logging.toString() );
559         }
560     }
561 
562     public void testPMDProcessingErrorWithDetailsNoReport()
563             throws Exception
564     {
565         File testPom = new File( getBasedir(),
566                 "src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml" );
567         PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
568 
569         PrintStream originalOut = System.out;
570         ByteArrayOutputStream logging = new ByteArrayOutputStream();
571         System.setOut( new PrintStream( logging ) );
572 
573         try {
574             mojo.execute();
575             String output = logging.toString();
576             assertTrue ( output.contains( "There are 1 PMD processing errors:" ) );
577 
578             File generatedFile = new File( getBasedir(), "target/test/unit/parse-error/target/pmd.xml" );
579             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
580             String str = readFile( generatedFile );
581             assertTrue( str.contains( "Error while parsing" ) );
582             // The parse exception must be in the XML report
583             assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
584 
585             generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
586             renderer( mojo, generatedFile );
587             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
588             str = readFile( generatedFile );
589             // The parse exception must NOT be in the HTML report, since reportProcessingErrors is false
590             assertFalse( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) );
591 
592         } finally {
593             System.setOut( originalOut );
594             System.out.println( logging.toString() );
595         }
596     }
597 
598     public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception {
599         File testPom = new File(getBasedir(), "src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml");
600         PmdReport mojo = (PmdReport) lookupMojo ("pmd", testPom);
601         mojo.execute();
602 
603         File generatedFile = new File( getBasedir(), "target/test/unit/exclude-roots/target/pmd.xml" );
604         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
605         String str = readFile( generatedFile );
606 
607         assertTrue( "Seems like all directories are excluded now", str.contains("ForLoopShouldBeWhileLoop") );
608         assertFalse( "Exclusion of an exact source directory not working", str.contains( "OverrideBothEqualsAndHashcode" ) );
609         assertFalse( "Exclusion of basedirectory with subdirectories not working (MPMD-178)", str.contains( "JumbledIncrementer") );
610     }
611 
612     public void testViolationExclusion()
613             throws Exception
614         {
615             File testPom =
616                 new File( getBasedir(),
617                           "src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml" );
618             final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
619             mojo.execute();
620 
621             File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
622             assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
623             String str = readFile( generatedFile );
624 
625             assertEquals(0, StringUtils.countMatches(str, "<violation"));
626         }
627 }