Coverage Report - org.apache.maven.plugin.verifier.VerifierMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
VerifierMojo
91%
56/61
92%
26/28
2,8
 
 1  
 package org.apache.maven.plugin.verifier;
 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 org.apache.maven.plugin.AbstractMojo;
 23  
 import org.apache.maven.plugin.MojoExecutionException;
 24  
 import org.apache.maven.plugin.verifier.model.Verifications;
 25  
 import org.apache.maven.plugin.verifier.model.io.xpp3.VerificationsXpp3Reader;
 26  
 import org.codehaus.plexus.util.FileUtils;
 27  
 import org.codehaus.plexus.util.IOUtil;
 28  
 
 29  
 import java.io.File;
 30  
 import java.io.FileReader;
 31  
 import java.io.IOException;
 32  
 import java.io.Reader;
 33  
 import java.util.Iterator;
 34  
 import java.util.regex.Matcher;
 35  
 import java.util.regex.Pattern;
 36  
 
 37  
 /**
 38  
  * Verifies the existence or non-existence of files/directories and optionally checks file content against a regular expression.
 39  
  *
 40  
  * @goal verify
 41  
  * @phase integration-test
 42  
  *
 43  
  * @author <a href="vmassol@apache.org">Vincent Massol</a>
 44  
  * @version $Id: VerifierMojo.java 900472 2010-01-18 17:36:37Z dennisl $
 45  
  */
 46  56
 public class VerifierMojo
 47  
     extends AbstractMojo
 48  
 {
 49  
     /**
 50  
      * Project base directory (prepended to relative file paths).
 51  
      *
 52  
      * @parameter expression="${basedir}"
 53  
      * @required
 54  
      */
 55  
     private File basedir;
 56  
 
 57  
     /**
 58  
      * The file containing the verifications to perform.
 59  
      *
 60  
      * @parameter default-value="${basedir}/src/test/verifier/verifications.xml" expression="${verifier.verificationFile}"
 61  
      * @required
 62  
      */
 63  
     private File verificationFile;
 64  
 
 65  
     /**
 66  
      * Whether the build will fail on verification errors.
 67  
      *
 68  
      * @parameter default-value="true" expression="${verifier.failOnError}"
 69  
      * @required
 70  
      */
 71  
     private boolean failOnError;
 72  
 
 73  56
     private VerificationResultPrinter resultPrinter = new ConsoleVerificationResultPrinter( getLog() );
 74  
 
 75  
     public void execute()
 76  
         throws MojoExecutionException
 77  
     {
 78  42
         VerificationResult results = verify();
 79  42
         this.resultPrinter.print( results );
 80  
 
 81  
         // Fail the build if there are errors
 82  42
         if ( this.failOnError && results.hasFailures() )
 83  
         {
 84  21
             throw new MojoExecutionException( "There are test failures" );
 85  
         }
 86  21
     }
 87  
 
 88  
     /**
 89  
      * @param file the file path of the file to check (can be relative or absolute). If relative
 90  
      *             the project's basedir will be prefixed.
 91  
      * @return the absolute file path of the file to check
 92  
      */
 93  
     protected File getAbsoluteFileToCheck( File file )
 94  
     {
 95  56
         File result = file;
 96  56
         if ( !file.isAbsolute() )
 97  
         {
 98  49
             result = new File(  basedir , file.getPath() );
 99  
         }
 100  56
         return result;
 101  
     }
 102  
 
 103  
     private VerificationResult verify()
 104  
         throws MojoExecutionException
 105  
     {
 106  42
         VerificationResult results = new VerificationResult();
 107  
 
 108  42
         Reader reader = null;
 109  
         try
 110  
         {
 111  42
             reader = new FileReader( this.verificationFile );
 112  
 
 113  42
             VerificationsXpp3Reader xppReader = new VerificationsXpp3Reader();
 114  42
             Verifications verifications = xppReader.read( reader );
 115  
 
 116  42
             for ( Iterator i = verifications.getFiles().iterator(); i.hasNext(); )
 117  
             {
 118  42
                 org.apache.maven.plugin.verifier.model.File file =
 119  
                     (org.apache.maven.plugin.verifier.model.File) i.next();
 120  
 
 121  
                 // Transform the file to check into an absolute path prefixing the basedir if
 122  
                 // the location is relative
 123  42
                 if ( file.getLocation() != null )
 124  
                 {
 125  42
                     file.setLocation( getAbsoluteFileToCheck( new File( file.getLocation() ) ).getPath() );
 126  42
                     verifyFile( file, results );
 127  
                 }
 128  
                 else
 129  
                 {
 130  0
                     throw new MojoExecutionException( "Missing <location> element" );
 131  
                 }
 132  
             }
 133  
         }
 134  0
         catch ( org.codehaus.plexus.util.xml.pull.XmlPullParserException e )
 135  
         {
 136  0
             throw new MojoExecutionException( "Error while verifying files", e );
 137  
         }
 138  0
         catch ( IOException e )
 139  
         {
 140  0
             throw new MojoExecutionException( "Error while verifying files", e );
 141  
         }
 142  
         finally
 143  
         {
 144  42
             IOUtil.close( reader );
 145  42
         }
 146  
 
 147  42
         return results;
 148  
     }
 149  
 
 150  
     private boolean verifyFile( org.apache.maven.plugin.verifier.model.File fileCheck, VerificationResult results )
 151  
         throws IOException
 152  
     {
 153  
         boolean result;
 154  
 
 155  42
         result = verifyFileExistence( fileCheck, results );
 156  42
         if ( result && fileCheck.getContains() != null )
 157  
         {
 158  14
             result = result && verifyFileContent( fileCheck, results );
 159  
         }
 160  
 
 161  42
         return result;
 162  
     }
 163  
 
 164  
     private boolean verifyFileContent( org.apache.maven.plugin.verifier.model.File fileCheck,
 165  
                                        VerificationResult results )
 166  
         throws IOException
 167  
     {
 168  14
         boolean result = false;
 169  
 
 170  14
         getLog().debug( "Verifying contents of " + fileCheck.getLocation() );
 171  
 
 172  14
         Pattern pattern = Pattern.compile( fileCheck.getContains() );
 173  
 
 174  
         // Note: Very inefficient way as we load the whole file in memory. If you have a better
 175  
         // idea, please submit it!
 176  14
         Matcher matcher = pattern.matcher( FileUtils.fileRead( new File( fileCheck.getLocation() ) ) );
 177  
 
 178  14
         if ( matcher.find() )
 179  
         {
 180  7
             result = true;
 181  
         }
 182  
         else
 183  
         {
 184  7
             results.addContentFailure( fileCheck );
 185  
         }
 186  
 
 187  14
         return result;
 188  
     }
 189  
 
 190  
     private boolean verifyFileExistence( org.apache.maven.plugin.verifier.model.File fileCheck,
 191  
                                          VerificationResult results )
 192  
     {
 193  42
         boolean result = false;
 194  
 
 195  42
         File physicalFile = new File( fileCheck.getLocation() );
 196  42
         if ( fileCheck.isExists() )
 197  
         {
 198  28
             getLog().debug( "Verifying existence of " + physicalFile );
 199  28
             result = physicalFile.exists();
 200  28
             if ( !result )
 201  
             {
 202  7
                 results.addExistenceFailure( fileCheck );
 203  
             }
 204  
         }
 205  
         else
 206  
         {
 207  14
             getLog().debug( "Verifying absence of " + physicalFile );
 208  14
             result = !physicalFile.exists();
 209  14
             if ( !result )
 210  
             {
 211  7
                 results.addNonExistenceFailure( fileCheck );
 212  
             }
 213  
         }
 214  
 
 215  42
         return result;
 216  
     }
 217  
 
 218  
     public void setBaseDir( File basedir )
 219  
     {
 220  56
         this.basedir = basedir;
 221  56
     }
 222  
 
 223  
     public void setVerificationFile( File file )
 224  
     {
 225  42
         this.verificationFile = file;
 226  42
     }
 227  
 
 228  
     public void setVerificationResultPrinter( VerificationResultPrinter printer )
 229  
     {
 230  42
         this.resultPrinter = printer;
 231  42
     }
 232  
 
 233  
     public void setFailOnError( boolean failOnError )
 234  
     {
 235  28
         this.failOnError = failOnError;
 236  28
     }
 237  
 }