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      /** {@inheritDoc} */
35      protected void setUp()
36          throws Exception
37      {
38          super.setUp();
39      }
40  
41      public void testDefaultConfiguration()
42          throws Exception
43      {
44          try
45          {
46              File testPom = new File( getBasedir(),
47                                       "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml" );
48              PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
49              mojo.execute();
50  
51              fail( "MojoFailureException should be thrown." );
52          }
53          catch ( Exception e )
54          {
55              assertTrue( true );
56          }
57      }
58  
59      public void testNotFailOnViolation()
60          throws Exception
61      {
62          File testPom = new File( getBasedir(),
63                                   "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
64          PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
65          mojo.execute();
66  
67          testPom = new File( getBasedir(),
68                              "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml" );
69          PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
70          pmdViolationMojo.execute();
71  
72          assertTrue( true );
73      }
74  
75      public void testFailurePriority()
76          throws Exception
77      {
78          File testPom = new File( getBasedir(),
79                                   "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml" );
80          PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
81          pmdViolationMojo.execute();
82  
83          testPom = new File( getBasedir(),
84                              "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml" );
85          pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
86          try
87          {
88              pmdViolationMojo.execute();
89              fail( "Exception Expected" );
90          }
91          catch ( MojoFailureException e )
92          {
93              System.out.println( "Caught Expected Message: " + e.getMessage() );// expected
94          }
95  
96      }
97  
98      public void testException()
99          throws Exception
100     {
101         try
102         {
103             File testPom = new File( getBasedir(),
104                                      "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml" );
105             PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom );
106             mojo.execute();
107 
108             fail( "MojoFailureException should be thrown." );
109         }
110         catch ( Exception e )
111         {
112             assertTrue( true );
113         }
114 
115     }
116 }