Coverage Report - org.apache.maven.plugin.changes.ChangesCheckMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ChangesCheckMojo
37%
9/24
25%
4/16
7.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.text.ParseException;
 24  
 import java.text.SimpleDateFormat;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.apache.maven.plugin.MojoExecutionException;
 29  
 import org.apache.maven.plugins.annotations.Mojo;
 30  
 import org.apache.maven.plugins.annotations.Parameter;
 31  
 import org.apache.maven.plugins.changes.model.Release;
 32  
 
 33  
 /**
 34  
  * Goal which checks that the changes.xml file has the necessary data to
 35  
  * generate an announcement or a report for the current release.
 36  
  *
 37  
  * @author Justin Edelson
 38  
  * @author Dennis Lundberg
 39  
  * @since 2.4
 40  
  */
 41  
 @Mojo( name = "changes-check", threadSafe = true )
 42  0
 public class ChangesCheckMojo
 43  
     extends AbstractChangesMojo
 44  
 {
 45  
     /**
 46  
      * The format that a correct release date should have. This value will be
 47  
      * used as a pattern to try to create a date.
 48  
      */
 49  
     @Parameter( property = "changes.releaseDateFormat", defaultValue = "yyyy-MM-dd" )
 50  
     private String releaseDateFormat;
 51  
 
 52  
     /**
 53  
      * Version of the artifact.
 54  
      */
 55  
     @Parameter( property = "changes.version", defaultValue = "${project.version}", required = true )
 56  
     private String version;
 57  
 
 58  
     /**
 59  
      * The path of the <code>changes.xml</code> file that will be checked.
 60  
      */
 61  
     @Parameter( property = "changes.xmlPath", defaultValue = "src/changes/changes.xml" )
 62  
     private File xmlPath;
 63  
 
 64  
     /**
 65  
      * Flag controlling snapshot processing. If set, versions ending with <code>-SNAPSHOT</code> won't be checked.
 66  
      *
 67  
      * @since 2.7
 68  
      */
 69  
     @Parameter( property = "changes.skipSnapshots", defaultValue = "false" )
 70  
     private boolean skipSnapshots;
 71  
 
 72  0
     private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
 73  
 
 74  
     /**
 75  
      * Check that the latest release contains a valid release date.
 76  
      *
 77  
      * @throws MojoExecutionException
 78  
      */
 79  
     public void execute()
 80  
         throws MojoExecutionException
 81  
     {
 82  
         // Run only at the execution root
 83  0
         if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() )
 84  
         {
 85  0
             getLog().info( "Skipping the changes check in this project because it's not the Execution Root" );
 86  
         }
 87  
         else
 88  
         {
 89  0
             if ( this.version.endsWith( "-SNAPSHOT" ) && this.skipSnapshots )
 90  
             {
 91  0
                 getLog().info( "Skipping snapshot version '" + this.version + "'." );
 92  
             }
 93  0
             else if ( xmlPath.exists() )
 94  
             {
 95  0
                 ChangesXML xml = new ChangesXML( xmlPath, getLog() );
 96  0
                 ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
 97  0
                 Release release =
 98  
                     releaseUtils.getLatestRelease( releaseUtils.convertReleaseList( xml.getReleaseList() ), version );
 99  
 
 100  0
                 if ( !isValidDate( release.getDateRelease(), releaseDateFormat ) )
 101  
                 {
 102  0
                     throw new MojoExecutionException(
 103  
                         "The file " + xmlPath.getAbsolutePath() + " has an invalid release date." );
 104  
 
 105  
                 }
 106  0
             }
 107  
             else
 108  
             {
 109  0
                 getLog().warn( "The file " + xmlPath.getAbsolutePath() + " does not exist." );
 110  
             }
 111  
         }
 112  0
     }
 113  
 
 114  
     /**
 115  
      * Use the pattern to try to parse a Date from the given string.
 116  
      *
 117  
      * @param string A date as text
 118  
      * @param pattern A pattern that can be used by {@link SimpleDateFormat}
 119  
      * @return <code>true</code> if the string can be parsed as a date using the pattern, otherwise <code>false</code>
 120  
      */
 121  
     protected static boolean isValidDate( String string, String pattern )
 122  
     {
 123  26
         if ( StringUtils.isEmpty( string ) )
 124  
         {
 125  12
             return false;
 126  
         }
 127  
 
 128  14
         if ( StringUtils.isEmpty( pattern ) )
 129  
         {
 130  8
             return false;
 131  
         }
 132  
 
 133  
         try
 134  
         {
 135  6
             SimpleDateFormat df = new SimpleDateFormat( pattern );
 136  6
             df.parse( string );
 137  2
             return true;
 138  
         }
 139  4
         catch ( ParseException e )
 140  
         {
 141  4
             return false;
 142  
         }
 143  
     }
 144  
 }