Coverage Report - org.apache.maven.plugin.changes.ChangesValidatorMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ChangesValidatorMojo
85%
18/21
75%
9/12
5.5
 
 1  
 package org.apache.maven.plugin.changes;
 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.util.List;
 24  
 
 25  
 import org.apache.maven.plugin.AbstractMojo;
 26  
 import org.apache.maven.plugin.MojoExecutionException;
 27  
 import org.apache.maven.plugin.MojoFailureException;
 28  
 import org.apache.maven.plugin.changes.schema.ChangesSchemaValidator;
 29  
 import org.apache.maven.plugin.changes.schema.SchemaValidatorException;
 30  
 import org.apache.maven.plugin.changes.schema.XmlValidationHandler;
 31  
 import org.xml.sax.SAXParseException;
 32  
 
 33  
 /**
 34  
  * 
 35  
  * Goal which validate the <code>changes.xml</code> file.
 36  
  * 
 37  
  * @goal changes-validate
 38  
  * 
 39  
  * @author <a href="mailto:olamy@apache.org">olamy</a>
 40  
  * @version $Id: ChangesValidatorMojo.java 1098310 2011-05-01 14:10:27Z dennisl $
 41  
  * @since 2.1
 42  
  * @threadSafe
 43  
  */
 44  3
 public class ChangesValidatorMojo
 45  
     extends AbstractMojo
 46  
 {
 47  
 
 48  
     /**
 49  
      * @component role="org.apache.maven.plugin.changes.schema.ChangesSchemaValidator" roleHint="default"
 50  
      */
 51  
     private ChangesSchemaValidator changesSchemaValidator;
 52  
 
 53  
     /**
 54  
      * The changes xsd version.
 55  
      *
 56  
      * @parameter expression="${changes.xsdVersion}" default-value="1.0.0"
 57  
      */
 58  
     private String changesXsdVersion;
 59  
 
 60  
     /**
 61  
      * Mojo failure if validation failed. If not and validation failed only a warning will be logged.
 62  
      *
 63  
      * @parameter expression="${changes.validate.failed}" default-value="false"
 64  
      */
 65  
     private boolean failOnError;
 66  
 
 67  
     /**
 68  
      * The path of the <code>changes.xml</code> file that will be converted into an HTML report.
 69  
      *
 70  
      * @parameter expression="${changes.xmlPath}" default-value="src/changes/changes.xml"
 71  
      */
 72  
     private File xmlPath;
 73  
 
 74  
     /** 
 75  
      * @see org.apache.maven.plugin.Mojo#execute()
 76  
      */
 77  
     public void execute()
 78  
         throws MojoExecutionException, MojoFailureException
 79  
     {
 80  
 
 81  3
         if ( !xmlPath.exists() )
 82  
         {
 83  0
             getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." );
 84  0
             return;
 85  
         }
 86  
 
 87  
         try
 88  
         {
 89  3
             XmlValidationHandler xmlValidationHandler = changesSchemaValidator
 90  
                 .validateXmlWithSchema( xmlPath, changesXsdVersion, failOnError );
 91  2
             boolean hasErrors = !xmlValidationHandler.getErrors().isEmpty();
 92  2
             if ( hasErrors )
 93  
             {
 94  1
                 logSchemaValidation( xmlValidationHandler.getErrors() );
 95  1
                 if ( failOnError )
 96  
                 {
 97  0
                     throw new MojoExecutionException( "changes.xml file " + xmlPath.getAbsolutePath()
 98  
                         + " is not valid, see previous errors." );
 99  
                 }
 100  
                 else
 101  
                 {
 102  1
                     getLog().info( " skip previous validation errors due to failOnError=false." );
 103  
                 }
 104  
             }
 105  
         }
 106  1
         catch ( SchemaValidatorException e )
 107  
         {
 108  1
             if ( failOnError )
 109  
             {
 110  1
                 throw new MojoExecutionException( "failed to validate changes.xml file " + xmlPath.getAbsolutePath()
 111  
                     + ": " + e.getMessage(), e );
 112  
             }
 113  2
         }
 114  2
     }
 115  
 
 116  
     private void logSchemaValidation( List<SAXParseException> errors )
 117  
     {
 118  1
         getLog().warn( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() );
 119  1
         getLog().warn( "validation errors: " );
 120  1
         for ( SAXParseException error : errors )
 121  
         {
 122  1
             getLog().warn( error.getMessage() );
 123  
         }
 124  1
     }
 125  
 
 126  
 }