Coverage Report - org.apache.maven.plugin.pmd.PmdViolationCheckMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
PmdViolationCheckMojo
100%
40/40
78%
11/14
2,167
 
 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.ArrayList;
 26  
 import java.util.List;
 27  
 
 28  
 import org.apache.maven.plugin.MojoExecutionException;
 29  
 import org.apache.maven.plugin.MojoFailureException;
 30  
 import org.apache.maven.plugin.pmd.model.PmdErrorDetail;
 31  
 import org.apache.maven.plugin.pmd.model.PmdFile;
 32  
 import org.apache.maven.plugin.pmd.model.Violation;
 33  
 import org.apache.maven.plugin.pmd.model.io.xpp3.PmdXpp3Reader;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 36  
 
 37  
 /**
 38  
  * Fail the build if there were any PMD violations in the source code.
 39  
  *
 40  
  * @since 2.0
 41  
  * @version $Id$
 42  
  * @goal check
 43  
  * @phase verify
 44  
  * @execute goal="pmd"
 45  
  * @threadSafe
 46  
  */
 47  29
 public class PmdViolationCheckMojo
 48  
     extends AbstractPmdViolationCheckMojo<Violation>
 49  
 {
 50  
     /**
 51  
      * What priority level to fail the build on. Failures at or above this level
 52  
      * will stop the build. Anything below will be warnings and will be
 53  
      * displayed in the build output if verbose=true. Note: Minimum Priority = 5
 54  
      * Maximum Priority = 0
 55  
      *
 56  
      * @parameter expression="${pmd.failurePriority}" default-value="5"
 57  
      * @required
 58  
      */
 59  
     private int failurePriority;
 60  
 
 61  
     /**
 62  
      * Skip the PMD checks.  Most useful on the command line
 63  
      * via "-Dpmd.skip=true".
 64  
      *
 65  
      * @parameter expression="${pmd.skip}" default-value="false"
 66  
      */
 67  
     private boolean skip;
 68  
 
 69  
     /** {@inheritDoc} */
 70  
     public void execute()
 71  
         throws MojoExecutionException, MojoFailureException
 72  
     {
 73  3
         if ( !skip )
 74  
         {
 75  3
             executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority );
 76  
         }
 77  2
     }
 78  
 
 79  
     /** {@inheritDoc} */
 80  
     protected void printError( Violation item, String severity )
 81  
     {
 82  
 
 83  6
         StringBuffer buff = new StringBuffer( 100 );
 84  6
         buff.append( "PMD " + severity + ": " );
 85  6
         if ( item.getViolationClass() != null )
 86  
         {
 87  5
             if ( item.getViolationPackage() != null )
 88  
             {
 89  5
                 buff.append( item.getViolationPackage() );
 90  5
                 buff.append( "." );
 91  
             }
 92  5
             buff.append( item.getViolationClass() );
 93  
         }
 94  
         else
 95  
         {
 96  1
             buff.append( item.getFileName() );
 97  
         }
 98  6
         buff.append( ":" );
 99  6
         buff.append( item.getBeginline() );
 100  6
         buff.append( " Rule:" ).append( item.getRule() );
 101  6
         buff.append( " Priority:" ).append( item.getPriority() );
 102  6
         buff.append( " " ).append( item.getText() ).append( "." );
 103  
 
 104  6
         this.getLog().info( buff.toString() );
 105  6
     }
 106  
     
 107  
     @Override
 108  
     protected List<Violation> getErrorDetails( File pmdFile )
 109  
         throws XmlPullParserException, IOException
 110  
     {
 111  3
         PmdXpp3Reader reader = new PmdXpp3Reader();
 112  3
         PmdErrorDetail details = reader.read( new FileReader( pmdFile ), false );
 113  
 
 114  3
         List<Violation> violations = new ArrayList<Violation>();
 115  3
         for( PmdFile file : details.getFiles() )
 116  
         {
 117  9
             String fullPath = file.getName();
 118  
             
 119  9
             for ( Violation violation : file.getViolations() )
 120  
             {
 121  18
                 violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) );
 122  18
                 violations.add( violation );
 123  
             }
 124  9
         }
 125  3
         return violations;
 126  
     }
 127  
     
 128  
     @Override
 129  
     protected int getPriority( Violation errorDetail )
 130  
     {
 131  18
         return errorDetail.getPriority();
 132  
     }
 133  
     
 134  
     @Override
 135  
     protected ViolationDetails<Violation> newViolationDetailsInstance()
 136  
     {
 137  3
         return new ViolationDetails<Violation>();
 138  
     }
 139  
     
 140  
     private String getFilename( String fullpath, String pkg )
 141  
     {
 142  18
         int index = fullpath.lastIndexOf( File.separatorChar );
 143  
 
 144  36
         while ( StringUtils.isNotEmpty( pkg ) )
 145  
         {
 146  36
             index = fullpath.substring( 0, index ).lastIndexOf( File.separatorChar );
 147  
 
 148  36
             int dot = pkg.indexOf( '.' );
 149  
 
 150  36
             if ( dot < 0 )
 151  
             {
 152  18
                 break;
 153  
             }
 154  18
             pkg = pkg.substring( dot + 1 );
 155  18
         }
 156  
 
 157  18
         return fullpath.substring( index + 1 );
 158  
     }
 159  
 }