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 testFailurePriority()
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-failonpriority-plugin-config.xml" );
93          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-failandwarnonpriority-plugin-config.xml" );
99          pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
100         try
101         {
102             pmdViolationMojo.execute();
103             fail( "Exception Expected" );
104         }
105         catch ( final MojoFailureException e )
106         {
107             String message = e.getMessage();
108             if ( message.contains( "You have 5 PMD violations and 3 warnings." ) )
109             {
110                 System.out.println( "Caught expected message: " + e.getMessage() );// expected
111             }
112             else
113             {
114                 throw new AssertionError( "Expected: '" + message
115                     + "' to contain 'You have 5 PMD violations and 3 warnings.'" );
116             }
117         }
118 
119     }
120 
121     public void testException()
122         throws Exception
123     {
124         try
125         {
126             final File testPom =
127                 new File( getBasedir(),
128                           "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml" );
129             final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
130             mojo.execute();
131 
132             fail( "MojoFailureException should be thrown." );
133         }
134         catch ( final Exception e )
135         {
136             assertTrue( true );
137         }
138 
139     }
140 
141     public void testViolationExclusion()
142         throws Exception
143     {
144         File testPom =
145             new File( getBasedir(),
146                       "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
147         final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
148         mojo.execute();
149 
150         testPom =
151             new File( getBasedir(),
152                       "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml" );
153         final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
154 
155         // this call shouldn't throw an exception, as the classes with violations have been excluded
156         pmdViolationMojo.execute();
157     }
158 }