You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2020/07/23 19:44:20 UTC

[maven-script-interpreter] branch master updated: [MSHARED-910] fix after test with maven-invoker-plugin

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git


The following commit(s) were added to refs/heads/master by this push:
     new e901545  [MSHARED-910] fix after test with maven-invoker-plugin
e901545 is described below

commit e90154517b4eafd131e9b731d1135de28534544b
Author: Slawomir Jaranowski <sl...@payu.pl>
AuthorDate: Thu Jul 23 20:57:07 2020 +0200

    [MSHARED-910] fix after test with maven-invoker-plugin
---
 .../scriptinterpreter/GroovyScriptInterpreter.java |  5 ++
 .../ScriptEvaluationException.java                 | 14 ++--
 .../shared/scriptinterpreter/ScriptException.java} | 29 +++++++-
 ...onException.java => ScriptReturnException.java} | 36 ++++------
 .../shared/scriptinterpreter/ScriptRunner.java     | 14 ++--
 .../shared/scriptinterpreter/ScriptRunnerTest.java | 80 ++++++++++++++++++----
 .../{no-return.bsh => return-not-true.bsh}         |  1 +
 .../bsh-test/{no-return.bsh => return-null.bsh}    |  1 +
 .../return-null.groovy}                            |  1 +
 9 files changed, 127 insertions(+), 54 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
index dd76784..0bf8e36 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
@@ -22,6 +22,7 @@ package org.apache.maven.shared.scriptinterpreter;
 import groovy.lang.Binding;
 import groovy.lang.GroovyShell;
 import org.codehaus.groovy.control.CompilerConfiguration;
+import org.codehaus.groovy.runtime.powerassert.PowerAssertionError;
 import org.codehaus.groovy.tools.RootLoader;
 
 import java.io.File;
@@ -80,6 +81,10 @@ class GroovyScriptInterpreter
             {
                 throw e;
             }
+            catch ( PowerAssertionError e )
+            {
+                throw new ScriptEvaluationException( "Assertion Error", e );
+            }
             catch ( Throwable e )
             {
                 throw new ScriptEvaluationException( e );
diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
index 8d6ab37..382aa61 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
@@ -26,7 +26,7 @@ package org.apache.maven.shared.scriptinterpreter;
  * @author Benjamin Bentmann
  */
 public class ScriptEvaluationException
-    extends Exception
+    extends ScriptException
 {
 
     /**
@@ -39,19 +39,13 @@ public class ScriptEvaluationException
      * 
      * @param cause The cause, may be <code>null</code>.
      */
-    public ScriptEvaluationException( Throwable cause )
+    ScriptEvaluationException( Throwable cause )
     {
         super( cause );
     }
 
-    /**
-     * Creates a new exception with the specified message.
-     *
-     * @param message The message with description of exception.
-     */
-    public ScriptEvaluationException( String message )
+    public ScriptEvaluationException( String message, Throwable cause )
     {
-        super( message );
+        super( message, cause );
     }
-
 }
diff --git a/src/test/resources/bsh-test/no-return.bsh b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java
similarity index 57%
copy from src/test/resources/bsh-test/no-return.bsh
copy to src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java
index bd244d0..f6062ef 100644
--- a/src/test/resources/bsh-test/no-return.bsh
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java
@@ -1,3 +1,5 @@
+package org.apache.maven.shared.scriptinterpreter;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -7,7 +9,7 @@
  * "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
+ *  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
@@ -17,3 +19,28 @@
  * under the License.
  */
 
+/**
+ * Common errors during script running.
+ * 
+ * @author Slawomir Jaranowski
+ */
+public class ScriptException
+    extends Exception
+{
+    private static final long serialVersionUID = 4553276474852776472L;
+
+    public ScriptException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+
+    ScriptException( String message )
+    {
+        super( message );
+    }
+
+    ScriptException( Throwable cause )
+    {
+        super( cause );
+    }
+}
diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java
similarity index 54%
copy from src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
copy to src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java
index 8d6ab37..3958197 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java
@@ -20,38 +20,30 @@ package org.apache.maven.shared.scriptinterpreter;
  */
 
 /**
- * Signals an error during parsing/evaluation of a script. This can either be a syntax error in the script itself or an
- * exception triggered by the methods it invoked.
- * 
- * @author Benjamin Bentmann
+ * Signals an invalid value returned from script execution.
+ *
+ * @author Slawomir Jaranowski
  */
-public class ScriptEvaluationException
-    extends Exception
+public class ScriptReturnException extends ScriptException
 {
 
-    /**
-     * The serial version identifier for this class.
-     */
-    private static final long serialVersionUID = 199336743291078393L;
+    private static final long serialVersionUID = -4705573157701206786L;
 
-    /**
-     * Creates a new exception with the specified cause.
-     * 
-     * @param cause The cause, may be <code>null</code>.
-     */
-    public ScriptEvaluationException( Throwable cause )
+    private final Object result;
+
+    ScriptReturnException( String message, Object result )
     {
-        super( cause );
+        super( message );
+        this.result = result;
     }
 
     /**
-     * Creates a new exception with the specified message.
+     * Retrieve result returned by script.
      *
-     * @param message The message with description of exception.
+     * @return script result.
      */
-    public ScriptEvaluationException( String message )
+    public Object getResult()
     {
-        super( message );
+        return result;
     }
-
 }
diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java
index 5749f62..a3f0f2e 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java
@@ -128,11 +128,11 @@ public class ScriptRunner
      * @param context The key-value storage used to share information between hook scripts, may be <code>null</code>.
      * @param logger The logger to redirect the script output to, may be <code>null</code> to use stdout/stderr.
      * @throws IOException If an I/O error occurred while reading the script file.
-     * @throws ScriptEvaluationException If the script did not return <code>true</code> of threw an exception.
+     * @throws ScriptException If the script did not return <code>true</code> of threw an exception.
      */
     public void run( final String scriptDescription, final File basedir, final String relativeScriptPath,
                      final Map<String, ?> context, final ExecutionLogger logger )
-            throws IOException, ScriptEvaluationException
+            throws IOException, ScriptException
     {
         if ( relativeScriptPath == null )
         {
@@ -163,11 +163,11 @@ public class ScriptRunner
      * @param context The key-value storage used to share information between hook scripts, may be <code>null</code>.
      * @param logger The logger to redirect the script output to, may be <code>null</code> to use stdout/stderr.
      * @throws IOException         If an I/O error occurred while reading the script file.
-     * @throws ScriptEvaluationException If the script did not return <code>true</code> of threw an exception.
+     * @throws ScriptException If the script did not return <code>true</code> of threw an exception.
      */
     public void run( final String scriptDescription, File scriptFile, final Map<String, ?> context,
                      final ExecutionLogger logger )
-            throws IOException, ScriptEvaluationException
+            throws IOException, ScriptException
     {
 
         if ( !scriptFile.exists() )
@@ -183,7 +183,7 @@ public class ScriptRunner
 
     private void executeRun( final String scriptDescription, File scriptFile,
                              final Map<String, ?> context, final ExecutionLogger logger )
-            throws IOException, ScriptEvaluationException
+            throws IOException, ScriptException
     {
         ScriptInterpreter interpreter = getInterpreter( scriptFile );
         if ( LOG.isDebugEnabled() )
@@ -235,9 +235,9 @@ public class ScriptRunner
             throw e;
         }
 
-        if ( !Boolean.parseBoolean( String.valueOf( result ) ) )
+        if ( !( result == null || Boolean.parseBoolean( String.valueOf( result ) ) ) )
         {
-            throw new ScriptEvaluationException( "The " + scriptDescription + " returned " + result + "." );
+            throw new ScriptReturnException( "The " + scriptDescription + " returned " + result + ".", result );
         }
     }
 
diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java
index 8f279a8..5437020 100644
--- a/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java
+++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java
@@ -27,6 +27,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 /**
@@ -63,6 +64,30 @@ public class ScriptRunnerTest
     }
 
     @Test
+    public void beanshellReturnedNullShouldBeOk() throws Exception
+    {
+        File logFile = new File( "target/build.log" );
+        if ( logFile.exists() )
+        {
+            logFile.delete();
+        }
+
+        TestMirrorHandler mirrorHandler = new TestMirrorHandler();
+
+        try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) )
+        {
+            ScriptRunner scriptRunner = new ScriptRunner();
+            scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ),
+                    "return-null", null, fileLogger );
+        }
+
+        String logContent = FileUtils.fileRead( logFile );
+        assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/return-null.bsh" ).getPath() ) );
+        assertTrue( logContent.contains( "ok with null result" ) );
+        assertEquals( logContent, mirrorHandler.getLoggedMessage() );
+    }
+
+    @Test
     public void failedBeanshellShouldCreateProperLogsMessage() throws Exception
     {
         File logFile = new File( "target/build.log" );
@@ -81,19 +106,19 @@ public class ScriptRunnerTest
             scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "failed",
                     buildContext(), fileLogger );
         }
-        catch ( Exception e )
+        catch ( ScriptEvaluationException e )
         {
             catchedException = e;
         }
 
-        assertTrue( catchedException instanceof ScriptEvaluationException );
+        assertNotNull( catchedException );
         String logContent = FileUtils.fileRead( logFile );
         assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/failed.bsh" ).getPath() ) );
         assertEquals( logContent, mirrorHandler.getLoggedMessage() );
     }
 
     @Test
-    public void noReturnFromBeanshellShouldThrowException() throws Exception
+    public void beanshellReturnedNotTrueShouldThrowException() throws Exception
     {
         File logFile = new File( "target/build.log" );
         if ( logFile.exists() )
@@ -103,26 +128,27 @@ public class ScriptRunnerTest
 
         TestMirrorHandler mirrorHandler = new TestMirrorHandler();
 
-        Exception catchedException = null;
+        ScriptReturnException catchedException = null;
 
         try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) )
         {
             ScriptRunner scriptRunner = new ScriptRunner();
             scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ),
-                    "no-return", buildContext(), fileLogger );
+                    "return-not-true", null, fileLogger );
         }
-        catch ( Exception e )
+        catch ( ScriptReturnException e )
         {
             catchedException = e;
         }
 
-        assertTrue( catchedException instanceof ScriptEvaluationException );
-        assertEquals( "The test returned null.", catchedException.getMessage() );
+        assertEquals( "Not true value", catchedException.getResult() );
+        assertEquals( "The test returned Not true value.", catchedException.getMessage() );
         String logContent = FileUtils.fileRead( logFile );
-        assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/no-return.bsh" ).getPath() ) );
+        assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/return-not-true.bsh" ).getPath() ) );
         assertEquals( logContent, mirrorHandler.getLoggedMessage() );
     }
 
+
     @Test
     public void testBeanshellWithFile() throws Exception
     {
@@ -178,6 +204,32 @@ public class ScriptRunnerTest
     }
 
     @Test
+    public void groovyReturnedNullShouldBeOk() throws Exception
+    {
+        File logFile = new File( "target/build.log" );
+        if ( logFile.exists() )
+        {
+            logFile.delete();
+        }
+
+        TestMirrorHandler mirrorHandler = new TestMirrorHandler();
+
+        try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) )
+        {
+            ScriptRunner scriptRunner = new ScriptRunner();
+            scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" );
+            scriptRunner.run( "test", new File( "src/test/resources/groovy-test" ),
+                    "return-null", null, fileLogger );
+        }
+
+        String logContent = FileUtils.fileRead( logFile );
+        assertTrue(
+                logContent.contains( new File( "src/test/resources/groovy-test/return-null.groovy" ).getPath() ) );
+        assertTrue( logContent.contains( "ok with null result" ) );
+        assertEquals( logContent, mirrorHandler.getLoggedMessage() );
+    }
+
+    @Test
     public void failedGroovyShouldCreateProperLogsMessage() throws Exception
     {
         File logFile = new File( "target/build.log" );
@@ -196,12 +248,12 @@ public class ScriptRunnerTest
             scriptRunner.run( "test", new File( "src/test/resources/groovy-test" ), "failed",
                     buildContext(), fileLogger );
         }
-        catch ( Exception e )
+        catch ( ScriptEvaluationException e )
         {
             catchedException = e;
         }
 
-        assertTrue( catchedException instanceof ScriptEvaluationException );
+        assertNotNull( catchedException );
         String logContent = FileUtils.fileRead( logFile );
         assertTrue( logContent.contains( new File( "src/test/resources/groovy-test/failed.groovy" ).getPath() ) );
         assertEquals( logContent, mirrorHandler.getLoggedMessage() );
@@ -218,7 +270,7 @@ public class ScriptRunnerTest
 
         TestMirrorHandler mirrorHandler = new TestMirrorHandler();
 
-        Exception catchedException = null;
+        ScriptReturnException catchedException = null;
 
         try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) )
         {
@@ -226,12 +278,12 @@ public class ScriptRunnerTest
             scriptRunner.run( "test", new File( "src/test/resources/groovy-test" ),
                     "return-false", buildContext(), fileLogger );
         }
-        catch ( Exception e )
+        catch ( ScriptReturnException e )
         {
             catchedException = e;
         }
 
-        assertTrue( catchedException instanceof ScriptEvaluationException );
+        assertEquals( false, catchedException.getResult() );
         assertEquals( "The test returned false.", catchedException.getMessage() );
         String logContent = FileUtils.fileRead( logFile );
         assertTrue( logContent.contains( new File( "src/test/resources/groovy-test/return-false.groovy" ).getPath() ) );
diff --git a/src/test/resources/bsh-test/no-return.bsh b/src/test/resources/bsh-test/return-not-true.bsh
similarity index 97%
copy from src/test/resources/bsh-test/no-return.bsh
copy to src/test/resources/bsh-test/return-not-true.bsh
index bd244d0..1ddb06a 100644
--- a/src/test/resources/bsh-test/no-return.bsh
+++ b/src/test/resources/bsh-test/return-not-true.bsh
@@ -17,3 +17,4 @@
  * under the License.
  */
 
+return "Not true value"
diff --git a/src/test/resources/bsh-test/no-return.bsh b/src/test/resources/bsh-test/return-null.bsh
similarity index 95%
copy from src/test/resources/bsh-test/no-return.bsh
copy to src/test/resources/bsh-test/return-null.bsh
index bd244d0..7f1f85a 100644
--- a/src/test/resources/bsh-test/no-return.bsh
+++ b/src/test/resources/bsh-test/return-null.bsh
@@ -17,3 +17,4 @@
  * under the License.
  */
 
+System.out.print("ok with null result");
diff --git a/src/test/resources/bsh-test/no-return.bsh b/src/test/resources/groovy-test/return-null.groovy
similarity index 96%
rename from src/test/resources/bsh-test/no-return.bsh
rename to src/test/resources/groovy-test/return-null.groovy
index bd244d0..9b54311 100644
--- a/src/test/resources/bsh-test/no-return.bsh
+++ b/src/test/resources/groovy-test/return-null.groovy
@@ -17,3 +17,4 @@
  * under the License.
  */
 
+println 'ok with null result'