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