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  /**
25   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
26   * @version $Id$
27   */
28  public class CpdViolationCheckMojoTest
29      extends AbstractPmdReportTestCase
30  {
31  
32      public void testDefaultConfiguration()
33          throws Exception
34      {
35          generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
36  
37          // clear the output from previous pmd:cpd execution
38          CapturingPrintStream.init( true );
39  
40          try
41          {
42              File testPom =
43                  new File( getBasedir(),
44                            "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml" );
45              final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom );
46              cpdViolationMojo.execute();
47  
48              fail( "MojoFailureException should be thrown." );
49          }
50          catch ( final Exception e )
51          {
52              // the version should be logged
53              String output = CapturingPrintStream.getOutput();
54              assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
55  
56              assertTrue( e.getMessage().startsWith( "You have 1 CPD duplication." ) );
57          }
58      }
59  
60      public void testNotFailOnViolation()
61          throws Exception
62      {
63  
64          generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
65  
66          File testPom =
67              new File( getBasedir(),
68                        "src/test/resources/unit/default-configuration/cpd-check-notfailonviolation-plugin-config.xml" );
69          final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom );
70          cpdViolationMojo.execute();
71  
72          assertTrue( true );
73      }
74  
75      public void testException()
76          throws Exception
77      {
78          try
79          {
80              final File testPom =
81                  new File( getBasedir(),
82                            "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml" );
83              final CpdViolationCheckMojo mojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom );
84              mojo.execute();
85  
86              fail( "MojoFailureException should be thrown." );
87          }
88          catch ( final Exception e )
89          {
90              assertTrue( true );
91          }
92      }
93  
94      public void testExclusionsConfiguration()
95          throws Exception
96      {
97          generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
98  
99          File testPom =
100             new File( getBasedir(),
101                       "src/test/resources/unit/default-configuration/cpd-check-cpd-exclusions-configuration-plugin-config.xml" );
102         final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom );
103 
104         // this call shouldn't throw an exception, as the classes with duplications have been excluded
105         cpdViolationMojo.execute();
106     }
107 }