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