Coverage Report - org.apache.maven.plugin.pmd.PmdViolationCheckMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
PmdViolationCheckMojo
100 %
29/29
70 %
7/10
2,667
 
 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 PMD violations in the source code.
 33  
  *
 34  
  * @since 2.0
 35  
  * @version $Id: org.apache.maven.plugin.pmd.PmdViolationCheckMojo.html 816688 2012-05-08 15:14:44Z hboutemy $
 36  
  * @goal check
 37  
  * @phase verify
 38  
  * @execute goal="pmd"
 39  
  */
 40  10
 public class PmdViolationCheckMojo
 41  
     extends AbstractPmdViolationCheckMojo
 42  
 {
 43  
     /**
 44  
      * What priority level to fail the build on. Failures at or above this level
 45  
      * will stop the build. Anything below will be warnings and will be
 46  
      * displayed in the build output if verbose=true. Note: Minimum Priority = 5
 47  
      * Maximum Priority = 0
 48  
      *
 49  
      * @parameter expression="${pmd.failurePriority}" default-value="5"
 50  
      * @required
 51  
      */
 52  
     private int failurePriority;
 53  
 
 54  
     /**
 55  
      * Skip the PMD checks.  Most useful on the command line
 56  
      * via "-Dpmd.skip=true".
 57  
      *
 58  
      * @parameter expression="${pmd.skip}" default-value="false"
 59  
      */
 60  
     private boolean skip;
 61  
 
 62  
     /** {@inheritDoc} */
 63  
     public void execute()
 64  
         throws MojoExecutionException, MojoFailureException
 65  
     {
 66  6
         if ( !skip )
 67  
         {
 68  6
             executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority );
 69  
         }
 70  4
     }
 71  
 
 72  
     /** {@inheritDoc} */
 73  
     protected void printError( Map item, String severity )
 74  
     {
 75  
 
 76  12
         StringBuffer buff = new StringBuffer( 100 );
 77  12
         buff.append( "PMD " + severity + ": " );
 78  12
         if ( item.containsKey( "class" ) )
 79  
         {
 80  10
             if ( item.containsKey( "package" ) )
 81  
             {
 82  10
                 buff.append( item.get( "package" ) );
 83  10
                 buff.append( "." );
 84  
             }
 85  10
             buff.append( item.get( "class" ) );
 86  
         }
 87  
         else
 88  
         {
 89  2
             buff.append( item.get( "filename" ) );
 90  
         }
 91  12
         buff.append( ":" );
 92  12
         buff.append( item.get( "beginline" ) );
 93  12
         buff.append( " Rule:" ).append( item.get( "rule" ) );
 94  12
         buff.append( " Priority:" ).append( item.get( "priority" ) );
 95  12
         buff.append( " " ).append( item.get( "text" ) ).append( "." );
 96  
 
 97  12
         this.getLog().info( buff.toString() );
 98  12
     }
 99  
 
 100  
     /** {@inheritDoc} */
 101  
     protected Map getErrorDetails( XmlPullParser xpp )
 102  
         throws XmlPullParserException, IOException
 103  
     {
 104  36
         int index = 0;
 105  36
         int attributeCount = 0;
 106  36
         HashMap msgs = new HashMap();
 107  
 
 108  36
         attributeCount = xpp.getAttributeCount();
 109  414
         while ( index < attributeCount )
 110  
         {
 111  
 
 112  378
             msgs.put( xpp.getAttributeName( index ), xpp.getAttributeValue( index ) );
 113  
 
 114  378
             index++;
 115  
         }
 116  
 
 117  
         // get the tag's text
 118  36
         if ( xpp.next() == XmlPullParser.TEXT )
 119  
         {
 120  36
             msgs.put( "text", xpp.getText().trim() );
 121  
         }
 122  36
         return msgs;
 123  
     }
 124  
 }