View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.pmd;
20  
21  import java.io.File;
22  
23  import org.apache.maven.plugin.MojoFailureException;
24  
25  /**
26   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
27   * @version $Id$
28   */
29  public class PmdViolationCheckMojoTest extends AbstractPmdReportTestCase {
30  
31      public void testDefaultConfiguration() throws Exception {
32          generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
33  
34          // clear the output from previous pmd:pmd execution
35          CapturingPrintStream.init(true);
36  
37          try {
38              final File testPom = new File(
39                      getBasedir(),
40                      "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml");
41              final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
42              mojo.execute();
43  
44              fail("MojoFailureException should be thrown.");
45          } catch (final Exception e) {
46              // the version should be logged
47              String output = CapturingPrintStream.getOutput();
48              assertTrue(output.contains("PMD version: " + AbstractPmdReport.getPmdVersion()));
49  
50              assertTrue(e.getMessage().startsWith("You have 8 PMD violations."));
51          }
52      }
53  
54      public void testNotFailOnViolation() throws Exception {
55          generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
56  
57          File testPom = new File(
58                  getBasedir(),
59                  "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml");
60          final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
61          pmdViolationMojo.execute();
62  
63          assertTrue(true);
64      }
65  
66      public void testMaxAllowedViolations() throws Exception {
67          generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
68  
69          File testPom = new File(
70                  getBasedir(),
71                  "src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml");
72          final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
73          pmdViolationMojo.execute();
74  
75          testPom = new File(
76                  getBasedir(),
77                  "src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml");
78          final PmdViolationCheckMojo pmdViolationMojoFail = (PmdViolationCheckMojo) lookupMojo("check", testPom);
79  
80          try {
81              pmdViolationMojoFail.execute();
82              fail("Exception Expected");
83          } catch (final MojoFailureException e) {
84              String message = e.getMessage();
85              if (message.contains("You have 5 PMD violations and 3 warnings.")) {
86                  System.out.println("Caught expected message: " + e.getMessage()); // expected
87              } else {
88                  throw new AssertionError(
89                          "Expected: '" + message + "' to contain 'You have 5 PMD violations and 3 warnings.'");
90              }
91          }
92      }
93  
94      public void testFailurePriority() throws Exception {
95          generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
96  
97          File testPom = new File(
98                  getBasedir(),
99                  "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml");
100         PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
101         pmdViolationMojo.execute();
102 
103         testPom = new File(
104                 getBasedir(),
105                 "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml");
106         pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
107         try {
108             pmdViolationMojo.execute();
109             fail("Exception Expected");
110         } catch (final MojoFailureException e) {
111             String message = e.getMessage();
112             if (message.contains("You have 5 PMD violations and 3 warnings.")) {
113                 System.out.println("Caught expected message: " + e.getMessage()); // expected
114             } else {
115                 throw new AssertionError(
116                         "Expected: '" + message + "' to contain 'You have 5 PMD violations and 3 warnings.'");
117             }
118         }
119     }
120 
121     public void testException() throws Exception {
122         try {
123             final File testPom = new File(
124                     getBasedir(),
125                     "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml");
126             final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
127             mojo.execute();
128 
129             fail("MojoFailureException should be thrown.");
130         } catch (final Exception e) {
131             assertTrue(true);
132         }
133     }
134 
135     public void testViolationExclusion() throws Exception {
136         generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
137 
138         File testPom = new File(
139                 getBasedir(),
140                 "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml");
141         final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo("check", testPom);
142 
143         // this call shouldn't throw an exception, as the classes with violations have been excluded
144         pmdViolationMojo.execute();
145     }
146 }