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/22 21:32:05 UTC
[maven-script-interpreter] 01/01: [MSHARED-910] remove redundant
option failOnException
This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch MSHARED-910
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git
commit 3a7d383b41b49b82d8dfae5d33fcc13160d98b43
Author: Slawomir Jaranowski <sl...@payu.pl>
AuthorDate: Wed Jul 22 08:31:23 2020 +0200
[MSHARED-910] remove redundant option failOnException
---
.../scriptinterpreter/RunErrorException.java | 50 --------------
.../scriptinterpreter/RunFailureException.java | 68 ------------------
.../ScriptEvaluationException.java | 10 +++
.../shared/scriptinterpreter/ScriptRunner.java | 59 ++++++----------
src/site/apt/index.apt.vm | 13 ++--
.../shared/scriptinterpreter/ScriptRunnerTest.java | 80 +++++++++++++++++++---
.../resources/bsh-test/no-return.bsh} | 30 +-------
.../resources/groovy-test/return-false.groovy} | 30 +-------
8 files changed, 109 insertions(+), 231 deletions(-)
diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/RunErrorException.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/RunErrorException.java
deleted file mode 100644
index 9e09a45..0000000
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/RunErrorException.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package org.apache.maven.shared.scriptinterpreter;
-
-/*
- * 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.
- */
-
-/**
- * Signals an error in a sub build run by the Invoker Plugin. This can be caused by a selector/pre-build
- * script throwing an exception or an error in forking the Maven build itself.
- *
- * @author Stephen Connolly
- */
-public class RunErrorException
- extends RunFailureException
-{
-
- /**
- * The serial version identifier for this class.
- */
- private static final long serialVersionUID = 236131530635863815L;
-
- /**
- * Creates a new exception with the specified detail message.
- *
- * @param message The detail message, may be <code>null</code>.
- * @param type The type of build failure, may not be <code>null</code>.
- * @param cause The cause of the build error.
- */
- public RunErrorException( String message, String type, Throwable cause )
- {
- super( message, type );
- initCause( cause );
- }
-
-}
diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/RunFailureException.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/RunFailureException.java
deleted file mode 100644
index 19361b4..0000000
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/RunFailureException.java
+++ /dev/null
@@ -1,68 +0,0 @@
-package org.apache.maven.shared.scriptinterpreter;
-
-/*
- * 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.
- */
-
-/**
- * Signals a failure of a sub build run by the Invoker Plugin. This can be caused by an unsuccessful pre-/post-build
- * script or a failure of the forked Maven build itself.
- *
- * @author Benjamin Bentmann
- */
-public class RunFailureException
- extends Exception
-{
-
- /**
- * The serial version identifier for this class.
- */
- private static final long serialVersionUID = 236131530635863815L;
-
- /**
- * The type of the build failure.
- */
- private final String type;
-
- /**
- * Creates a new exception with the specified detail message.
- *
- * @param message The detail message, may be <code>null</code>.
- * @param type The type of build failure, may not be <code>null</code>.
- */
- public RunFailureException( String message, String type )
- {
- super( message );
- if ( type == null )
- {
- throw new IllegalArgumentException( "missing failure type" );
- }
- this.type = type;
- }
-
- /**
- * Returns the type of build failure.
- *
- * @return The type of build failure, never <code>null</code>.
- */
- public String getType()
- {
- return type;
- }
-
-}
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 1d65a67..8d6ab37 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
@@ -44,4 +44,14 @@ public class ScriptEvaluationException
super( cause );
}
+ /**
+ * Creates a new exception with the specified message.
+ *
+ * @param message The message with description of exception.
+ */
+ public ScriptEvaluationException( String message )
+ {
+ super( message );
+ }
+
}
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 850055e..5749f62 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java
@@ -19,8 +19,8 @@ package org.apache.maven.shared.scriptinterpreter;
* under the License.
*/
-import org.apache.maven.shared.utils.io.FileUtils;
import org.apache.maven.shared.utils.StringUtils;
+import org.apache.maven.shared.utils.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -127,18 +127,12 @@ public class ScriptRunner
* to skip the script execution and may not have extensions (resolution will search).
* @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.
- * @param stage The stage of the build job the script is invoked in, must not be <code>null</code>. This is for
- * logging purpose only.
- * @param failOnException If <code>true</code> and the script throws an exception, then a
- * {@link RunFailureException} will be thrown, otherwise a {@link RunErrorException} will be thrown on
- * script exception.
* @throws IOException If an I/O error occurred while reading the script file.
- * @throws RunFailureException If the script did not return <code>true</code> of threw an exception.
+ * @throws ScriptEvaluationException 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, ? extends Object> context, final ExecutionLogger logger, String stage,
- boolean failOnException )
- throws IOException, RunFailureException
+ final Map<String, ?> context, final ExecutionLogger logger )
+ throws IOException, ScriptEvaluationException
{
if ( relativeScriptPath == null )
{
@@ -158,7 +152,7 @@ public class ScriptRunner
LOG.info( "run {} {}.{}",
scriptDescription, relativeScriptPath, FileUtils.extension( scriptFile.getAbsolutePath() ) );
- executeRun( scriptDescription, scriptFile, context, logger, stage, failOnException );
+ executeRun( scriptDescription, scriptFile, context, logger );
}
/**
@@ -168,16 +162,12 @@ public class ScriptRunner
* @param scriptFile The path to the script, may be <code>null</code> to skip the script execution.
* @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.
- * @param stage The stage of the build job the script is invoked in, must not be <code>null</code>. This is for
- * logging purpose only.
- * @param failOnException If <code>true</code> and the script throws an exception, then a {@link
- * RunFailureException} will be thrown, otherwise a {@link RunErrorException} will be thrown on script exception.
* @throws IOException If an I/O error occurred while reading the script file.
- * @throws RunFailureException If the script did not return <code>true</code> of threw an exception.
+ * @throws ScriptEvaluationException If the script did not return <code>true</code> of threw an exception.
*/
- public void run( final String scriptDescription, File scriptFile, final Map<String, ? extends Object> context,
- final ExecutionLogger logger, String stage, boolean failOnException )
- throws IOException, RunFailureException
+ public void run( final String scriptDescription, File scriptFile, final Map<String, ?> context,
+ final ExecutionLogger logger )
+ throws IOException, ScriptEvaluationException
{
if ( !scriptFile.exists() )
@@ -188,18 +178,13 @@ public class ScriptRunner
LOG.info( "run {} {}", scriptDescription, scriptFile.getAbsolutePath() );
- executeRun( scriptDescription, scriptFile, context, logger, stage, failOnException );
+ executeRun( scriptDescription, scriptFile, context, logger );
}
private void executeRun( final String scriptDescription, File scriptFile,
- final Map<String, ? extends Object> context, final ExecutionLogger logger, String stage,
- boolean failOnException )
- throws IOException, RunFailureException
+ final Map<String, ?> context, final ExecutionLogger logger )
+ throws IOException, ScriptEvaluationException
{
- Map<String, Object> globalVariables = new HashMap<>( this.globalVariables );
- globalVariables.put( "basedir", scriptFile.getParentFile() );
- globalVariables.put( "context", context );
-
ScriptInterpreter interpreter = getInterpreter( scriptFile );
if ( LOG.isDebugEnabled() )
{
@@ -230,7 +215,11 @@ public class ScriptRunner
PrintStream out = ( logger != null ) ? logger.getPrintStream() : null;
- result = interpreter.evaluateScript( script, classPath, globalVariables, out );
+ Map<String, Object> scriptVariables = new HashMap<>( this.globalVariables );
+ scriptVariables.put( "basedir", scriptFile.getParentFile() );
+ scriptVariables.put( "context", context );
+
+ result = interpreter.evaluateScript( script, classPath, scriptVariables, out );
if ( logger != null )
{
logger.consumeLine( "Finished " + scriptDescription + ": " + scriptFile );
@@ -243,20 +232,12 @@ public class ScriptRunner
{
t.printStackTrace( logger.getPrintStream() );
}
-
- if ( failOnException )
- {
- throw new RunFailureException( "The " + scriptDescription + " did not succeed.", stage );
- }
- else
- {
- throw new RunErrorException( "The " + scriptDescription + " did not succeed.", stage, t );
- }
+ throw e;
}
- if ( !( result == null || Boolean.TRUE.equals( result ) || "true".equals( result ) ) )
+ if ( !Boolean.parseBoolean( String.valueOf( result ) ) )
{
- throw new RunFailureException( "The " + scriptDescription + " returned " + result + ".", stage );
+ throw new ScriptEvaluationException( "The " + scriptDescription + " returned " + result + "." );
}
}
diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm
index 2dbae50..1bfb46a 100644
--- a/src/site/apt/index.apt.vm
+++ b/src/site/apt/index.apt.vm
@@ -76,7 +76,7 @@ ${project.name}
ScriptRunner scriptRunner = new ScriptRunner( );
scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify", buildContext(),
- new FileLogger( logFile ), "foo", true );
+ new FileLogger( logFile ) );
+---------
* Mirror output from script interpreter
@@ -101,7 +101,7 @@ class MyMirrorHandler implements FileLoggerMirrorHandler
ScriptRunner scriptRunner = new ScriptRunner( );
scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify", buildContext(),
- new FileLogger( logFile, new MyMirrorHandler() ), "foo", true );
+ new FileLogger( logFile, new MyMirrorHandler() ) );
+---------
@@ -119,9 +119,7 @@ class MyMirrorHandler implements FileLoggerMirrorHandler
+---------
- SystemStreamLog systemStreamLog = new SystemStreamLog();
-
- ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog );
+ ScriptRunner scriptRunner = new ScriptRunner( );
scriptRunner.setGlobalVariable( name, value );
+---------
@@ -148,12 +146,11 @@ class MyMirrorHandler implements FileLoggerMirrorHandler
You can add some additional classpath entries for your script execution
+---------
- SystemStreamLog systemStreamLog = new SystemStreamLog();
List<String> classpathEntries = list of jar paths
- ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog );
+ ScriptRunner scriptRunner = new ScriptRunner( );
scriptRunner.setClassPath( classpathEntries );
scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify", buildContext(),
- new FileLogger( logFile ), "foo", true );
+ new FileLogger( logFile ) );
+---------
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 46fc885..8f279a8 100644
--- a/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java
+++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java
@@ -51,7 +51,7 @@ public class ScriptRunnerTest
ScriptRunner scriptRunner = new ScriptRunner();
scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" );
scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify",
- buildContext(), fileLogger, "foo", true );
+ buildContext(), fileLogger );
}
String logContent = FileUtils.fileRead( logFile );
@@ -79,20 +79,51 @@ public class ScriptRunnerTest
{
ScriptRunner scriptRunner = new ScriptRunner();
scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "failed",
- buildContext(), fileLogger, "foo", false );
+ buildContext(), fileLogger );
}
catch ( Exception e )
{
catchedException = e;
}
- assertTrue( catchedException instanceof RunErrorException );
+ assertTrue( catchedException instanceof ScriptEvaluationException );
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
+ {
+ File logFile = new File( "target/build.log" );
+ if ( logFile.exists() )
+ {
+ logFile.delete();
+ }
+
+ TestMirrorHandler mirrorHandler = new TestMirrorHandler();
+
+ Exception 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 );
+ }
+ catch ( Exception e )
+ {
+ catchedException = e;
+ }
+
+ assertTrue( catchedException instanceof ScriptEvaluationException );
+ assertEquals( "The test returned null.", catchedException.getMessage() );
+ String logContent = FileUtils.fileRead( logFile );
+ assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/no-return.bsh" ).getPath() ) );
+ assertEquals( logContent, mirrorHandler.getLoggedMessage() );
+ }
+
+ @Test
public void testBeanshellWithFile() throws Exception
{
File logFile = new File( "target/build.log" );
@@ -108,7 +139,7 @@ public class ScriptRunnerTest
ScriptRunner scriptRunner = new ScriptRunner();
scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" );
scriptRunner.run( "test", new File( "src/test/resources/bsh-test/verify.bsh" ),
- buildContext(), fileLogger, "foo", true );
+ buildContext(), fileLogger );
}
String logContent = FileUtils.fileRead( logFile );
@@ -134,7 +165,7 @@ public class ScriptRunnerTest
ScriptRunner scriptRunner = new ScriptRunner();
scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" );
scriptRunner.run( "test", new File( "src/test/resources/groovy-test" ), "verify",
- buildContext(), fileLogger, "foo", true );
+ buildContext(), fileLogger );
}
String logContent = FileUtils.fileRead( logFile );
@@ -163,20 +194,51 @@ public class ScriptRunnerTest
{
ScriptRunner scriptRunner = new ScriptRunner();
scriptRunner.run( "test", new File( "src/test/resources/groovy-test" ), "failed",
- buildContext(), fileLogger, "foo", true );
+ buildContext(), fileLogger );
}
catch ( Exception e )
{
catchedException = e;
}
- assertTrue( catchedException instanceof RunFailureException );
+ assertTrue( catchedException instanceof ScriptEvaluationException );
String logContent = FileUtils.fileRead( logFile );
assertTrue( logContent.contains( new File( "src/test/resources/groovy-test/failed.groovy" ).getPath() ) );
assertEquals( logContent, mirrorHandler.getLoggedMessage() );
}
@Test
+ public void groovyReturnedFalseShouldThrowException() throws Exception
+ {
+ File logFile = new File( "target/build.log" );
+ if ( logFile.exists() )
+ {
+ logFile.delete();
+ }
+
+ TestMirrorHandler mirrorHandler = new TestMirrorHandler();
+
+ Exception catchedException = null;
+
+ try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) )
+ {
+ ScriptRunner scriptRunner = new ScriptRunner();
+ scriptRunner.run( "test", new File( "src/test/resources/groovy-test" ),
+ "return-false", buildContext(), fileLogger );
+ }
+ catch ( Exception e )
+ {
+ catchedException = e;
+ }
+
+ assertTrue( catchedException instanceof ScriptEvaluationException );
+ 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() ) );
+ assertEquals( logContent, mirrorHandler.getLoggedMessage() );
+ }
+
+ @Test
public void testGroovyWithFile() throws Exception
{
File logFile = new File( "target/build.log" );
@@ -191,7 +253,7 @@ public class ScriptRunnerTest
{
ScriptRunner scriptRunner = new ScriptRunner();
scriptRunner.run( "test", new File( "src/test/resources/groovy-test/verify.groovy" ),
- buildContext(), fileLogger, "foo", true );
+ buildContext(), fileLogger );
}
String logContent = FileUtils.fileRead( logFile );
@@ -201,7 +263,7 @@ public class ScriptRunnerTest
assertEquals( logContent, mirrorHandler.getLoggedMessage() );
}
- private Map<String, ? extends Object> buildContext()
+ private Map<String, ?> buildContext()
{
Map<String, Object> context = new HashMap<>();
context.put( "foo", "bar" );
diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java b/src/test/resources/bsh-test/no-return.bsh
similarity index 50%
copy from src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
copy to src/test/resources/bsh-test/no-return.bsh
index 1d65a67..bd244d0 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
+++ b/src/test/resources/bsh-test/no-return.bsh
@@ -1,5 +1,3 @@
-package org.apache.maven.shared.scriptinterpreter;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -9,7 +7,7 @@ package org.apache.maven.shared.scriptinterpreter;
* "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
@@ -19,29 +17,3 @@ package org.apache.maven.shared.scriptinterpreter;
* under the License.
*/
-/**
- * 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
- */
-public class ScriptEvaluationException
- extends Exception
-{
-
- /**
- * The serial version identifier for this class.
- */
- private static final long serialVersionUID = 199336743291078393L;
-
- /**
- * Creates a new exception with the specified cause.
- *
- * @param cause The cause, may be <code>null</code>.
- */
- public ScriptEvaluationException( Throwable cause )
- {
- super( cause );
- }
-
-}
diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java b/src/test/resources/groovy-test/return-false.groovy
similarity index 50%
copy from src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
copy to src/test/resources/groovy-test/return-false.groovy
index 1d65a67..e9e4898 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
+++ b/src/test/resources/groovy-test/return-false.groovy
@@ -1,5 +1,3 @@
-package org.apache.maven.shared.scriptinterpreter;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -9,7 +7,7 @@ package org.apache.maven.shared.scriptinterpreter;
* "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
@@ -19,29 +17,5 @@ package org.apache.maven.shared.scriptinterpreter;
* under the License.
*/
-/**
- * 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
- */
-public class ScriptEvaluationException
- extends Exception
-{
-
- /**
- * The serial version identifier for this class.
- */
- private static final long serialVersionUID = 199336743291078393L;
-
- /**
- * Creates a new exception with the specified cause.
- *
- * @param cause The cause, may be <code>null</code>.
- */
- public ScriptEvaluationException( Throwable cause )
- {
- super( cause );
- }
-}
+return false