Coverage Report - org.apache.maven.plugin.pmd.CpdViolationCheckMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
CpdViolationCheckMojo
39%
9/23
25%
1/4
1,4
 
 1  
 package org.apache.maven.plugin.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  
 import java.io.FileReader;
 24  
 import java.io.IOException;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.maven.plugin.MojoExecutionException;
 28  
 import org.apache.maven.plugin.MojoFailureException;
 29  
 import org.apache.maven.plugin.pmd.model.CpdErrorDetail;
 30  
 import org.apache.maven.plugin.pmd.model.CpdFile;
 31  
 import org.apache.maven.plugin.pmd.model.Duplication;
 32  
 import org.apache.maven.plugin.pmd.model.io.xpp3.CpdXpp3Reader;
 33  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 34  
 
 35  
 /**
 36  
  * Fail the build if there were any CPD violations in the source code.
 37  
  *
 38  
  * @since 2.0
 39  
  * @version $Id$
 40  
  * @goal cpd-check
 41  
  * @phase verify
 42  
  * @execute goal="cpd"
 43  
  * @threadSafe
 44  
  */
 45  4
 public class CpdViolationCheckMojo
 46  
     extends AbstractPmdViolationCheckMojo<Duplication>
 47  
 {
 48  
 
 49  
     /**
 50  
      * Skip the CPD violation checks.  Most useful on the command line
 51  
      * via "-Dcpd.skip=true".
 52  
      *
 53  
      * @parameter expression="${cpd.skip}" default-value="false"
 54  
      */
 55  
     private boolean skip;
 56  
 
 57  
     /** {@inheritDoc} */
 58  
     public void execute()
 59  
         throws MojoExecutionException, MojoFailureException
 60  
     {
 61  1
         if ( !skip )
 62  
         {
 63  1
             executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 );
 64  
         }
 65  1
     }
 66  
 
 67  
     /** {@inheritDoc} */
 68  
     protected void printError( Duplication item, String severity )
 69  
     {
 70  0
         int lines = item.getLines();
 71  
 
 72  
 
 73  0
         StringBuffer buff = new StringBuffer( 100 );
 74  0
         buff.append( "CPD " + severity + ": Found " );
 75  0
         buff.append( lines ).append( " lines of duplicated code at locations:" );
 76  0
         this.getLog().info( buff.toString() );
 77  
 
 78  
         
 79  0
         for( CpdFile file : item.getFiles() )
 80  
         {
 81  0
             buff.setLength( 0 );
 82  0
             buff.append( "    " );
 83  0
             buff.append( file.getPath() );
 84  0
             buff.append( " line " ).append( file.getLine() );
 85  0
             this.getLog().info( buff.toString() );
 86  
         }
 87  
 
 88  0
         this.getLog().debug( "CPD " + severity + ": Code Fragment " );
 89  0
         this.getLog().debug( item.getCodefragment() );
 90  0
     }
 91  
 
 92  
     /** {@inheritDoc} */
 93  
     protected List<Duplication> getErrorDetails( File cpdFile )
 94  
         throws XmlPullParserException, IOException
 95  
     {
 96  1
         CpdXpp3Reader reader = new CpdXpp3Reader();
 97  1
         CpdErrorDetail details = reader.read( new FileReader( cpdFile ), false );
 98  1
         return details.getDuplications();
 99  
     }
 100  
     
 101  
     @Override
 102  
     protected int getPriority( Duplication errorDetail )
 103  
     {
 104  1
         return 0;
 105  
     }
 106  
     
 107  
     @Override
 108  
     protected ViolationDetails<Duplication> newViolationDetailsInstance()
 109  
     {
 110  1
         return new ViolationDetails<Duplication>();
 111  
     }
 112  
 }