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