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