Author: dennisl
Date: Thu Dec 16 23:22:01 2010
New Revision: 1050224

URL: http://svn.apache.org/viewvc?rev=1050224&view=rev
Log:
[MCHANGES-140] Create a changes check mojo
Submitted by: Justin Edelson
Reviewed by: Dennis Lundberg

I have modified the original patch to use a date format instead of a special 
token to recognize an invalid release date. A test case and documentation was 
also added.

Added:
    
maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugin/changes/ChangesCheckMojo.java
   (with props)
    
maven/plugins/trunk/maven-changes-plugin/src/site/apt/examples/check-changes-file.apt.vm
   (with props)
    
maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugin/changes/ChangesCheckMojoTestCase.java
   (with props)
Modified:
    maven/plugins/trunk/maven-changes-plugin/src/site/apt/index.apt
    maven/plugins/trunk/maven-changes-plugin/src/site/site.xml

Added: 
maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugin/changes/ChangesCheckMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugin/changes/ChangesCheckMojo.java?rev=1050224&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugin/changes/ChangesCheckMojo.java
 (added)
+++ 
maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugin/changes/ChangesCheckMojo.java
 Thu Dec 16 23:22:01 2010
@@ -0,0 +1,121 @@
+package org.apache.maven.plugin.changes;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.changes.model.Release;
+
+/**
+ * Goal which checks that the changes.xml file has the necessary data to
+ * generate an announcement or a report for the current release.
+ * 
+ * @goal changes-check
+ * @author Justin Edelson
+ * @author Dennis Lundberg
+ * @since 2.4
+ */
+public class ChangesCheckMojo extends AbstractMojo
+{
+    /**
+     * The format that a correct release date should have. This value will be
+     * used as a pattern to try to create a date.
+     *
+     * @parameter expression="${changes.releaseDateFormat}" 
default-value="yyyy-MM-dd"
+     */
+    private String releaseDateFormat;
+
+    /**
+     * Version of the artifact.
+     *
+     * @parameter expression="${changes.version}" 
default-value="${project.version}"
+     * @required
+     */
+    private String version;
+
+    /**
+     * The path of the <code>changes.xml</code> file that will be checked.
+     *
+     * @parameter expression="${changes.xmlPath}" 
default-value="src/changes/changes.xml"
+     */
+    private File xmlPath;
+
+    private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() );
+
+    /**
+     * Check that the latest release contains a valid release date.
+     *
+     * @throws MojoExecutionException
+     */
+    public void execute()
+        throws MojoExecutionException
+    {
+        if ( xmlPath.exists() )
+        {
+            ChangesXML xml = new ChangesXML( xmlPath, getLog() );
+            Release release = releaseUtils.getLatestRelease( 
xml.getReleaseList(), version );
+            if ( !isValidDate( release.getDateRelease(), releaseDateFormat ) )
+            {
+                throw new MojoExecutionException(
+                    "The file " + xmlPath.getAbsolutePath() + " has an invalid 
release date." );
+            }
+        }
+        else
+        {
+            getLog().warn( "The file " + xmlPath.getAbsolutePath() + " does 
not exist." );
+        }
+    }
+
+    /**
+     * Use the pattern to try to parse a Date from the given string.
+     *
+     * @param string A date as text
+     * @param pattern A pattern that can be used by {...@link SimpleDateFormat}
+     * @return <code>true</code> if the string can be parsed as a date using 
the pattern, otherwise <code>false</code>
+     */
+    protected static boolean isValidDate( String string, String pattern )
+    {
+        if ( StringUtils.isEmpty( string ) )
+        {
+            return false;
+        }
+
+        if ( StringUtils.isEmpty( pattern ) )
+        {
+            return false;
+        }
+
+        try
+        {
+            SimpleDateFormat df = new SimpleDateFormat( pattern );
+            df.parse( string );
+            return true;
+        }
+        catch ( ParseException e )
+        {
+            return false;
+        }
+    }
+}

Propchange: 
maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugin/changes/ChangesCheckMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/trunk/maven-changes-plugin/src/main/java/org/apache/maven/plugin/changes/ChangesCheckMojo.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author Id

Added: 
maven/plugins/trunk/maven-changes-plugin/src/site/apt/examples/check-changes-file.apt.vm
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/site/apt/examples/check-changes-file.apt.vm?rev=1050224&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-changes-plugin/src/site/apt/examples/check-changes-file.apt.vm
 (added)
+++ 
maven/plugins/trunk/maven-changes-plugin/src/site/apt/examples/check-changes-file.apt.vm
 Thu Dec 16 23:22:01 2010
@@ -0,0 +1,66 @@
+ ------
+ Check Your changes.xml File
+ ------
+ Dennis Lundberg
+ ------
+ 2010-12-16
+ ------
+
+ ~~ Licensed to the Apache Software Foundation (ASF) under one
+ ~~ or more contributor license agreements.  See the NOTICE file
+ ~~ distributed with this work for additional information
+ ~~ regarding copyright ownership.  The ASF licenses this file
+ ~~ to you under the Apache License, Version 2.0 (the
+ ~~ "License"); you may not use this file except in compliance
+ ~~ with the License.  You may obtain a copy of the License at
+ ~~
+ ~~   http://www.apache.org/licenses/LICENSE-2.0
+ ~~
+ ~~ Unless required by applicable law or agreed to in writing,
+ ~~ software distributed under the License is distributed on an
+ ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~~ KIND, either express or implied.  See the License for the
+ ~~ specific language governing permissions and limitations
+ ~~ under the License.
+
+ ~~ NOTE: For help with the syntax of this file, see:
+ ~~ http://maven.apache.org/doxia/references/apt-format.html
+
+
+Check Your changes.xml File
+
+  Since version 2.4 this plugin has a {{{../changes-check-mojo.html}goal}} that
+  checks that your <<<changes.xml>>> file has a valid release date.
+
+  You can attach this goal to the <<<release>>> phase if you want the check to
+  be performed automatically when you release the project.
+
+* Configuring the Plugin
+
+  Configure the plugin like this to tell it to check your <<<changes.xml>>>
+  file.
+
++-----------------+
+<project>
+  ...
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-changes-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <id>check-changes</id>
+            <phase>release</phase>
+            <goals>
+              <goal>changes-check</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  ...
+</project>
++-----------------+

Propchange: 
maven/plugins/trunk/maven-changes-plugin/src/site/apt/examples/check-changes-file.apt.vm
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/trunk/maven-changes-plugin/src/site/apt/examples/check-changes-file.apt.vm
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author Id

Modified: maven/plugins/trunk/maven-changes-plugin/src/site/apt/index.apt
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/site/apt/index.apt?rev=1050224&r1=1050223&r2=1050224&view=diff
==============================================================================
--- maven/plugins/trunk/maven-changes-plugin/src/site/apt/index.apt (original)
+++ maven/plugins/trunk/maven-changes-plugin/src/site/apt/index.apt Thu Dec 16 
23:22:01 2010
@@ -3,7 +3,7 @@
  ------
  Dennis Lundberg
  ------
- 2009-10-02
+ 2010-12-16
  ------
 
  ~~ Licensed to the Apache Software Foundation (ASF) under one
@@ -42,6 +42,8 @@ Maven Changes Plugin
 
   * {{{./announcement-generate-mojo.html}changes:announcement-generate}} 
generate a release announcement.
 
+  * {{{./changes-check-mojo.html}changes:changes-check}} check that the 
<<<changes.xml>>> file contains a valid release date.
+
   * {{{./changes-report-mojo.html}changes:changes-report}} create a report 
showing what has changed between different releases of the project.
 
   * {{{./changes-validate-mojo.html}changes:changes-validate}} validate the 
<<<changes.xml>>> file.
@@ -77,7 +79,9 @@ Maven Changes Plugin
   To provide you with better understanding of some usages of the Changes 
Plugin,
   you can take a look at the following examples:
 
-  * {{{./examples/alternate-changes-xml-location.html}Alternate Location for 
the changes.xml File}}
+  * {{{./examples/alternate-changes-xml-location.html}Alternate Location for 
the <<<changes.xml>>> File}}
+
+  * {{{./examples/check-changes-file.html}Check Your <<<changes.xml>>> File}}
 
   * {{{./examples/configuring-trac-report.html}Configuring the Trac Report}}
 
@@ -89,4 +93,4 @@ Maven Changes Plugin
 
   * {{{./examples/using-a-custom-announcement-template.html}Using a Custom 
Announcement Template}}
 
-  * {{{./examples/changes-file-validation.html}Validate your <<<changes.xml>>> 
file}}
+  * {{{./examples/changes-file-validation.html}Validate Your <<<changes.xml>>> 
File}}

Modified: maven/plugins/trunk/maven-changes-plugin/src/site/site.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/site/site.xml?rev=1050224&r1=1050223&r2=1050224&view=diff
==============================================================================
--- maven/plugins/trunk/maven-changes-plugin/src/site/site.xml (original)
+++ maven/plugins/trunk/maven-changes-plugin/src/site/site.xml Thu Dec 16 
23:22:01 2010
@@ -41,6 +41,7 @@ under the License.
     </menu>
     <menu name="Examples">
       <item name="Alternate Location for the changes.xml File" 
href="examples/alternate-changes-xml-location.html"/>
+      <item name="Check Your changes.xml File" 
href="examples/check-changes-file.html"/>
       <item name="Configuring the Trac Report" 
href="examples/configuring-trac-report.html"/>
       <item name="Customizing the JIRA Report" 
href="examples/customizing-jira-report.html"/>
       <item name="SMTP Authentication" 
href="examples/smtp-authentication.html"/>

Added: 
maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugin/changes/ChangesCheckMojoTestCase.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugin/changes/ChangesCheckMojoTestCase.java?rev=1050224&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugin/changes/ChangesCheckMojoTestCase.java
 (added)
+++ 
maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugin/changes/ChangesCheckMojoTestCase.java
 Thu Dec 16 23:22:01 2010
@@ -0,0 +1,58 @@
+package org.apache.maven.plugin.changes;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+
+/**
+ * @author Dennis Lundberg
+ * @version $Id$
+ * @since 2.4
+ */
+public class ChangesCheckMojoTestCase
+    extends TestCase
+{
+    public void testIsValidDate() throws Exception
+    {
+        String pattern;
+
+        // null pattern
+        pattern = null;
+        assertFalse(ChangesCheckMojo.isValidDate( null, pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "", pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "2010-12-16", pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "pending", pattern ));
+
+        // empty pattern
+        pattern = "";
+        assertFalse(ChangesCheckMojo.isValidDate( null, pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "", pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "2010-12-16", pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "pending", pattern ));
+
+        // valid pattern
+        pattern = "yyyy-MM-dd";
+        assertFalse(ChangesCheckMojo.isValidDate( null, pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "", pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "2010-DD-MM", pattern ));
+        assertTrue(ChangesCheckMojo.isValidDate( "2010-12-16", pattern ));
+        assertFalse(ChangesCheckMojo.isValidDate( "pending", pattern ));
+    }
+}

Propchange: 
maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugin/changes/ChangesCheckMojoTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/trunk/maven-changes-plugin/src/test/java/org/apache/maven/plugin/changes/ChangesCheckMojoTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author Id


Reply via email to