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