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