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  import org.apache.maven.project.MavenProject;
25  
26  /**
27   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
28   * @version $Id$
29   */
30  public class PmdViolationCheckMojoTest extends AbstractPmdReportTestCase {
31  
32      public void testDefaultConfiguration() throws Exception {
33          generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
34  
35          try {
36              final File testPom = new File(
37                      getBasedir(),
38                      "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml");
39              final PmdViolationCheckMojo mojo = lookupMojo(getGoal(), testPom);
40              mojo.execute();
41  
42              fail("MojoFailureException should be thrown.");
43          } catch (final MojoFailureException e) {
44              assertTrue(
45                      e.getMessage().startsWith("PMD " + AbstractPmdReport.getPmdVersion() + " has found 8 violations."));
46          }
47      }
48  
49      public void testNotFailOnViolation() throws Exception {
50          generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
51  
52          File testPom = new File(
53                  getBasedir(),
54                  "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml");
55          final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
56          pmdViolationMojo.execute();
57      }
58  
59      public void testMaxAllowedViolations() throws Exception {
60          generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
61  
62          File testPom = new File(
63                  getBasedir(),
64                  "src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml");
65          final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
66          pmdViolationMojo.execute();
67  
68          testPom = new File(
69                  getBasedir(),
70                  "src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml");
71          final PmdViolationCheckMojo pmdViolationMojoFail = lookupMojo(getGoal(), testPom);
72  
73          try {
74              pmdViolationMojoFail.execute();
75              fail("Exception Expected");
76          } catch (final MojoFailureException e) {
77              assertTrue(e.getMessage()
78                      .startsWith("PMD " + AbstractPmdReport.getPmdVersion()
79                              + " has found 5 violations and issued 3 warnings."));
80          }
81      }
82  
83      public void testFailurePriority() throws Exception {
84          generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
85  
86          File testPom = new File(
87                  getBasedir(),
88                  "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml");
89          PmdViolationCheckMojo pmdViolationCheckMojo = lookupMojo(getGoal(), testPom);
90          pmdViolationCheckMojo.execute();
91  
92          testPom = new File(
93                  getBasedir(),
94                  "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml");
95          pmdViolationCheckMojo = lookupMojo(getGoal(), testPom);
96  
97          try {
98              pmdViolationCheckMojo.execute();
99              fail("MojoFailureException expected");
100         } catch (final MojoFailureException e) {
101             assertTrue(e.getMessage()
102                     .startsWith("PMD " + AbstractPmdReport.getPmdVersion()
103                             + " has found 5 violations and issued 3 warnings."));
104         }
105     }
106 
107     public void testException() throws Exception {
108         try {
109             final File testPom = new File(
110                     getBasedir(),
111                     "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml");
112             final PmdViolationCheckMojo mojo = lookupMojo(getGoal(), testPom);
113             mojo.project = new MavenProject();
114             mojo.execute();
115 
116             fail("MojoFailureException should be thrown.");
117         } catch (final MojoFailureException e) {
118             assertNotNull(e.getMessage());
119         }
120     }
121 
122     public void testViolationExclusion() throws Exception {
123         generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
124 
125         File testPom = new File(
126                 getBasedir(),
127                 "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml");
128         final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
129 
130         // this call shouldn't throw an exception, as the classes with violations have been excluded
131         pmdViolationMojo.execute();
132     }
133 
134     @Override
135     protected String getGoal() {
136         return "check";
137     }
138 }