Coverage Report - org.apache.maven.plugin.pmd.CpdViolationCheckMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
CpdViolationCheckMojo
52 %
25/48
84 %
11/13
3,333
 
 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.IOException;
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.maven.plugin.MojoExecutionException;
 27  
 import org.apache.maven.plugin.MojoFailureException;
 28  
 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
 29  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 30  
 
 31  
 /**
 32  
  * Fail the build if there were any CPD violations in the source code.
 33  
  *
 34  
  * @since 2.0
 35  
  * @version $Id$
 36  
  * @goal cpd-check
 37  
  * @phase verify
 38  
  * @execute goal="cpd"
 39  
  * @threadSafe
 40  
  */
 41  3
 public class CpdViolationCheckMojo
 42  
     extends AbstractPmdViolationCheckMojo
 43  
 {
 44  
 
 45  
     /**
 46  
      * Skip the CPD violation checks.  Most useful on the command line
 47  
      * via "-Dcpd.skip=true".
 48  
      *
 49  
      * @parameter expression="${cpd.skip}" default-value="false"
 50  
      */
 51  
     private boolean skip;
 52  
 
 53  
     /** {@inheritDoc} */
 54  
     public void execute()
 55  
         throws MojoExecutionException, MojoFailureException
 56  
     {
 57  1
         if ( !skip )
 58  
         {
 59  1
             executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 );
 60  
         }
 61  1
     }
 62  
 
 63  
     /** {@inheritDoc} */
 64  
     protected void printError( Map item, String severity )
 65  
     {
 66  0
         String lines = (String) item.get( "lines" );
 67  
 
 68  
 
 69  0
         StringBuffer buff = new StringBuffer( 100 );
 70  0
         buff.append( "CPD " + severity + ": Found " );
 71  0
         buff.append( lines ).append( " lines of duplicated code at locations:" );
 72  0
         this.getLog().info( buff.toString() );
 73  
 
 74  0
         buff.setLength( 0 );
 75  0
         buff.append( "    " );
 76  0
         Map file = (Map) item.get( "file" );
 77  0
         buff.append( file.get( "path" ) );
 78  0
         buff.append( " line " ).append( file.get( "line" ) );
 79  0
         this.getLog().info( buff.toString() );
 80  
 
 81  0
         buff.setLength( 0 );
 82  0
         buff.append( "    " );
 83  0
         file = (Map) item.get( "file1" );
 84  0
         buff.append( file.get( "path" ) );
 85  0
         buff.append( " line " ).append( file.get( "line" ) );
 86  0
         this.getLog().info( buff.toString() );
 87  
 
 88  0
         Map codefrag = (Map) item.get( "codefragment" );
 89  0
         String codefragstr = (String) codefrag.get( "text" );
 90  0
         this.getLog().debug( "CPD " + severity + ": Code Fragment " );
 91  0
         this.getLog().debug( codefragstr );
 92  0
     }
 93  
 
 94  
     /** {@inheritDoc} */
 95  
     protected Map getErrorDetails( XmlPullParser xpp )
 96  
         throws XmlPullParserException, IOException
 97  
     {
 98  4
         int index = 0;
 99  4
         int attributeCount = 0;
 100  4
         HashMap msgs = new HashMap();
 101  
 
 102  4
         attributeCount = xpp.getAttributeCount();
 103  10
         while ( index < attributeCount )
 104  
         {
 105  6
             msgs.put( xpp.getAttributeName( index ), xpp.getAttributeValue( index ) );
 106  
 
 107  6
             index++;
 108  
         }
 109  
 
 110  4
         int tp = xpp.next();
 111  12
         while ( tp != XmlPullParser.END_TAG )
 112  
         {
 113  
             // get the tag's text
 114  8
             switch ( tp )
 115  
             {
 116  
             case XmlPullParser.TEXT:
 117  5
                 msgs.put( "text", xpp.getText().trim() );
 118  5
                 break;
 119  
             case XmlPullParser.START_TAG:
 120  
                 {
 121  3
                     String nm = xpp.getName();
 122  3
                     if ( msgs.containsKey( nm ) )
 123  
                     {
 124  1
                         int cnt = 1;
 125  1
                         while ( msgs.containsKey( nm + cnt ) )
 126  
                         {
 127  0
                             ++cnt;
 128  
                         }
 129  1
                         nm = nm + cnt;
 130  
                     }
 131  3
                     msgs.put( nm, getErrorDetails( xpp ) );
 132  3
                     break;
 133  
                 }
 134  
             default:
 135  
             }
 136  8
             tp = xpp.next();
 137  
         }
 138  4
         return msgs;
 139  
     }
 140  
 }