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