Author: olamy
Date: Sat Oct 29 21:17:22 2011
New Revision: 1195040

URL: http://svn.apache.org/viewvc?rev=1195040&view=rev
Log:
[ARCHETYPE-388] enhance IntegrationTestMojo to be able to run verify script to 
do some asserts.

Added:
    
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/FileLogger.java
   (with props)
Modified:
    maven/archetype/trunk/maven-archetype-plugin/pom.xml
    
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/IntegrationTestMojo.java

Modified: maven/archetype/trunk/maven-archetype-plugin/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/archetype/trunk/maven-archetype-plugin/pom.xml?rev=1195040&r1=1195039&r2=1195040&view=diff
==============================================================================
--- maven/archetype/trunk/maven-archetype-plugin/pom.xml (original)
+++ maven/archetype/trunk/maven-archetype-plugin/pom.xml Sat Oct 29 21:17:22 
2011
@@ -93,6 +93,21 @@ under the License.
       <groupId>commons-collections</groupId>
       <artifactId>commons-collections</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.groovy</groupId>
+      <artifactId>groovy</artifactId>
+      <version>1.8.3</version>
+    </dependency>
+    <dependency>
+      <groupId>org.beanshell</groupId>
+      <artifactId>bsh</artifactId>
+      <version>2.0b4</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-script-interpreter</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
   </dependencies>
 
   <build>

Added: 
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/FileLogger.java
URL: 
http://svn.apache.org/viewvc/maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/FileLogger.java?rev=1195040&view=auto
==============================================================================
--- 
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/FileLogger.java
 (added)
+++ 
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/FileLogger.java
 Sat Oct 29 21:17:22 2011
@@ -0,0 +1,63 @@
+package org.apache.maven.archetype.mojos;
+
+/*
+ * 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 org.apache.maven.plugin.logging.Log;
+import org.apache.maven.shared.invoker.InvocationOutputHandler;
+import org.apache.maven.shared.scriptinterpreter.ExecutionLogger;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @since 2.2
+ */
+class FileLogger
+    extends org.apache.maven.shared.scriptinterpreter.FileLogger
+    implements InvocationOutputHandler, ExecutionLogger
+{
+
+
+    /**
+     * Creates a new logger that writes to the specified file.
+     * 
+     * @param outputFile The path to the output file, must not be 
<code>null</code>.
+     * @throws java.io.IOException If the output file could not be created.
+     */
+    public FileLogger( File outputFile )
+        throws IOException
+    {
+        super( outputFile, null );
+    }
+
+    /**
+     * Creates a new logger that writes to the specified file and optionally 
mirrors messages to the given mojo logger.
+     *
+     * @param outputFile The path to the output file, must not be 
<code>null</code>.
+     * @param log The mojo logger to additionally output messages to, may be 
<code>null</code> if not used.
+     * @throws java.io.IOException If the output file could not be created.
+     */
+    public FileLogger( File outputFile, Log log )
+        throws IOException
+    {
+        super( outputFile, log );
+    }
+
+}

Propchange: 
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/FileLogger.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/FileLogger.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: 
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/IntegrationTestMojo.java
URL: 
http://svn.apache.org/viewvc/maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/IntegrationTestMojo.java?rev=1195040&r1=1195039&r2=1195040&view=diff
==============================================================================
--- 
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/IntegrationTestMojo.java
 (original)
+++ 
maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/mojos/IntegrationTestMojo.java
 Sat Oct 29 21:17:22 2011
@@ -34,6 +34,8 @@ import org.apache.maven.shared.invoker.I
 import org.apache.maven.shared.invoker.InvocationResult;
 import org.apache.maven.shared.invoker.Invoker;
 import org.apache.maven.shared.invoker.MavenInvocationException;
+import org.apache.maven.shared.scriptinterpreter.BuildFailureException;
+import org.apache.maven.shared.scriptinterpreter.ScriptRunner;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
@@ -45,6 +47,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
 import java.util.Arrays;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Properties;
 
@@ -107,6 +110,42 @@ public class IntegrationTestMojo
      */
     private File testProjectsDirectory;
 
+    /**
+     * Relative path of a cleanup/verification hook script to run after 
executing the build. This script may be written
+     * with either BeanShell or Groovy. If the file extension is omitted (e.g. 
<code>verify</code>), the
+     * plugin searches for the file by trying out the well-known extensions 
<code>.bsh</code> and <code>.groovy</code>.
+     * If this script exists for a particular project but returns any non-null 
value different from <code>true</code> or
+     * throws an exception, the corresponding build is flagged as a failure.
+     *
+     * @parameter expression="${archetype.test.verifyScript}" 
default-value="verify"
+     * @since 2.2
+     */
+    private String postBuildHookScript;
+
+    /**
+     * Suppress logging to the <code>build.log</code> file.
+     *
+     * @parameter expression="${archetype.test.noLog}" default-value="false"
+     * @since 2.2
+     */
+    private boolean noLog;
+
+    /**
+     * Flag used to determine whether the build logs should be output to the 
normal mojo log.
+     *
+     * @parameter expression="${archetype.test.streamLogs}" 
default-value="false"
+     * @since 2.2
+     */
+    private boolean streamLogs;
+
+    /**
+     * The file encoding for the post-build script.
+     *
+     * @parameter expression="${encoding}" 
default-value="${project.build.sourceEncoding}"
+     * @since 2.2
+     */
+    private String encoding;
+
     public void execute()
         throws MojoExecutionException, MojoFailureException
     {
@@ -269,7 +308,7 @@ public class IntegrationTestMojo
     }
 
     private void processIntegrationTest( File goalFile, File archetypeFile )
-        throws IntegrationTestFailure
+        throws IntegrationTestFailure, MojoExecutionException
     {
         getLog().info( "Processing Archetype IT project: " + 
goalFile.getParentFile().getName() );
 
@@ -339,7 +378,7 @@ public class IntegrationTestMojo
     }
 
     private void invokePostArchetypeGenerationGoals( String goals, File 
basedir )
-        throws IntegrationTestFailure
+        throws IntegrationTestFailure, IOException, MojoExecutionException
     {
         if ( StringUtils.isBlank( goals ) )
         {
@@ -350,9 +389,18 @@ public class IntegrationTestMojo
 
         getLog().info( "Invoking post-archetype-generation goals: " + goals );
 
+        FileLogger logger = setupLogger( basedir );
+
         InvocationRequest request = new 
DefaultInvocationRequest().setBaseDirectory( basedir ).setGoals(
             Arrays.asList( StringUtils.split( goals, "," ) ) );
 
+        if ( logger != null )
+        {
+            request.setErrorHandler( logger );
+
+            request.setOutputHandler( logger );
+        }
+
         try
         {
             InvocationResult result = invoker.execute( request );
@@ -369,6 +417,45 @@ public class IntegrationTestMojo
         {
             throw new IntegrationTestFailure( "Cannot run additions goals.", e 
);
         }
+
+        // verify result
+        ScriptRunner scriptRunner = new ScriptRunner( getLog() );
+        scriptRunner.setScriptEncoding( encoding );
+
+        try
+        {
+            scriptRunner.run( "post-build script", basedir, 
postBuildHookScript, new LinkedHashMap<String, Object>(),
+                              logger, "failure post script", true );
+        }
+        catch ( BuildFailureException e )
+        {
+            throw new IntegrationTestFailure( "post build script failure 
failure: " + e.getMessage(), e );
+        }
+    }
+
+    private FileLogger setupLogger( File basedir )
+        throws IOException
+    {
+        FileLogger logger = null;
+
+        if ( !noLog )
+        {
+            File outputLog = new File( basedir, "build.log" );
+
+            if ( streamLogs )
+            {
+                logger = new FileLogger( outputLog, getLog() );
+            }
+            else
+            {
+                logger = new FileLogger( outputLog );
+            }
+
+            getLog().debug( "build log initialized in: " + outputLog );
+
+        }
+
+        return logger;
     }
 
     class IntegrationTestFailure


Reply via email to