View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.pmd;
20  
21  import java.io.File;
22  
23  /**
24   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
25   * @version $Id$
26   */
27  public class CpdViolationCheckMojoTest extends AbstractPmdReportTestCase {
28  
29      public void testDefaultConfiguration() throws Exception {
30          generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
31  
32          // clear the output from previous pmd:cpd execution
33          CapturingPrintStream.init(true);
34  
35          try {
36              File testPom = new File(
37                      getBasedir(),
38                      "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml");
39              final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo("cpd-check", testPom);
40              cpdViolationMojo.execute();
41  
42              fail("MojoFailureException should be thrown.");
43          } catch (final Exception e) {
44              // the version should be logged
45              String output = CapturingPrintStream.getOutput();
46              assertTrue(output.contains("PMD version: " + AbstractPmdReport.getPmdVersion()));
47  
48              assertTrue(e.getMessage().startsWith("You have 1 CPD duplication."));
49          }
50      }
51  
52      public void testNotFailOnViolation() throws Exception {
53  
54          generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
55  
56          File testPom = new File(
57                  getBasedir(),
58                  "src/test/resources/unit/default-configuration/cpd-check-notfailonviolation-plugin-config.xml");
59          final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo("cpd-check", testPom);
60          cpdViolationMojo.execute();
61  
62          assertTrue(true);
63      }
64  
65      public void testException() throws Exception {
66          try {
67              final File testPom = new File(
68                      getBasedir(),
69                      "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml");
70              final CpdViolationCheckMojo mojo = (CpdViolationCheckMojo) lookupMojo("cpd-check", testPom);
71              mojo.execute();
72  
73              fail("MojoFailureException should be thrown.");
74          } catch (final Exception e) {
75              assertTrue(true);
76          }
77      }
78  
79      public void testExclusionsConfiguration() throws Exception {
80          generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
81  
82          File testPom = new File(
83                  getBasedir(),
84                  "src/test/resources/unit/default-configuration/cpd-check-cpd-exclusions-configuration-plugin-config.xml");
85          final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo("cpd-check", testPom);
86  
87          // this call shouldn't throw an exception, as the classes with duplications have been excluded
88          cpdViolationMojo.execute();
89      }
90  }