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.File;
23  
24  import org.apache.maven.plugin.MojoFailureException;
25  
26  /**
27   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
28   * @version $Id$
29   */
30  public class PmdViolationCheckMojoTest
31      extends AbstractPmdReportTestCase
32  {
33  
34      public void testDefaultConfiguration()
35          throws Exception
36      {
37          generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" );
38  
39          // clear the output from previous pmd:pmd execution
40          CapturingPrintStream.init( true );
41  
42          try
43          {
44              final File testPom =
45                  new File( getBasedir(),
46                            "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml" );
47              final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
48              mojo.execute();
49  
50              fail( "MojoFailureException should be thrown." );
51          }
52          catch ( final Exception e )
53          {
54              // the version should be logged
55              String output = CapturingPrintStream.getOutput();
56              assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
57  
58              assertTrue( e.getMessage().startsWith( "You have 8 PMD violations." ) );
59          }
60      }
61  
62      public void testNotFailOnViolation()
63          throws Exception
64      {
65          generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" );
66  
67          File testPom =
68              new File( getBasedir(),
69                        "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml" );
70          final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
71          pmdViolationMojo.execute();
72  
73          assertTrue( true );
74      }
75  
76      public void testMaxAllowedViolations()
77          throws Exception
78      {
79          generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" );
80  
81          File testPom =
82              new File( getBasedir(),
83                  "src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml" );
84          final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
85          pmdViolationMojo.execute();
86  
87          testPom =
88              new File( getBasedir(),
89                  "src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml" );
90          final PmdViolationCheckMojo pmdViolationMojoFail = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
91  
92          try
93          {
94              pmdViolationMojoFail.execute();
95              fail( "Exception Expected" );
96          }
97          catch ( final MojoFailureException e )
98          {
99              String message = e.getMessage();
100             if ( message.contains( "You have 5 PMD violations and 3 warnings." ) )
101             {
102                 System.out.println( "Caught expected message: " + e.getMessage() );// expected
103             }
104             else
105             {
106                 throw new AssertionError( "Expected: '" + message
107                     + "' to contain 'You have 5 PMD violations and 3 warnings.'" );
108             }
109         }
110 
111     }
112 
113     public void testFailurePriority()
114         throws Exception
115     {
116         generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" );
117 
118         File testPom =
119             new File( getBasedir(),
120                       "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml" );
121         PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
122         pmdViolationMojo.execute();
123 
124         testPom =
125             new File( getBasedir(),
126                       "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml" );
127         pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
128         try
129         {
130             pmdViolationMojo.execute();
131             fail( "Exception Expected" );
132         }
133         catch ( final MojoFailureException e )
134         {
135             String message = e.getMessage();
136             if ( message.contains( "You have 5 PMD violations and 3 warnings." ) )
137             {
138                 System.out.println( "Caught expected message: " + e.getMessage() );// expected
139             }
140             else
141             {
142                 throw new AssertionError( "Expected: '" + message
143                     + "' to contain 'You have 5 PMD violations and 3 warnings.'" );
144             }
145         }
146 
147     }
148 
149     public void testException()
150         throws Exception
151     {
152         try
153         {
154             final File testPom =
155                 new File( getBasedir(),
156                           "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml" );
157             final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
158             mojo.execute();
159 
160             fail( "MojoFailureException should be thrown." );
161         }
162         catch ( final Exception e )
163         {
164             assertTrue( true );
165         }
166 
167     }
168 
169     public void testViolationExclusion()
170         throws Exception
171     {
172         generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" );
173 
174         File testPom =
175             new File( getBasedir(),
176                       "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml" );
177         final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
178 
179         // this call shouldn't throw an exception, as the classes with violations have been excluded
180         pmdViolationMojo.execute();
181     }
182 }