View Javadoc

1   package org.apache.maven.plugin.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 org.apache.maven.plugin.MojoFailureException;
23  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
24  
25  import java.io.File;
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 = new File( getBasedir(),
50                                       "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml" );
51              final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
52              mojo.execute();
53  
54              fail( "MojoFailureException should be thrown." );
55          }
56          catch ( final Exception e )
57          {
58              assertTrue( true );
59          }
60      }
61  
62      public void testNotFailOnViolation()
63          throws Exception
64      {
65          File testPom = new File( getBasedir(),
66                                   "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
67          final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
68          mojo.execute();
69  
70          testPom = new File( getBasedir(),
71                              "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml" );
72          final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
73          pmdViolationMojo.execute();
74  
75          assertTrue( true );
76      }
77  
78      public void testFailurePriority()
79          throws Exception
80      {
81          File testPom = new File( getBasedir(),
82                                   "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
83          final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
84          mojo.execute();
85  
86          testPom = new File( getBasedir(),
87                              "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml" );
88          PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
89          pmdViolationMojo.execute();
90  
91          testPom = new File( getBasedir(),
92                              "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml" );
93          pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
94          try
95          {
96              pmdViolationMojo.execute();
97              fail( "Exception Expected" );
98          }
99          catch ( final MojoFailureException e )
100         {
101             String message = e.getMessage();
102             if ( message.contains( "You have 5 PMD violations and 3 warnings." ) ) {
103                 System.out.println( "Caught Expected Message: " + e.getMessage() );// expected
104             } 
105             else
106             {
107                 throw new AssertionError( "Expected: '" + message + "' to contain 'You have 5 PMD violations and 3 warnings.'" );
108             }                        
109         }
110 
111     }
112 
113     public void testException()
114         throws Exception
115     {
116         try
117         {
118             final File testPom = new File( getBasedir(),
119                                            "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml" );
120             final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
121             mojo.execute();
122 
123             fail( "MojoFailureException should be thrown." );
124         }
125         catch ( final Exception e )
126         {
127             assertTrue( true );
128         }
129 
130     }
131 
132     public void testViolationExclusion()
133         throws Exception
134     {
135         File testPom = new File( getBasedir(),
136                                  "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
137         final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
138         mojo.execute();
139 
140         testPom = new File( getBasedir(),
141                             "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml" );
142         final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
143         pmdViolationMojo.execute();
144 
145         assertTrue( true );
146     }
147 }