From 07c04293a0c426eb4cc7c741566a17d9ebe00e04 Mon Sep 17 00:00:00 2001 From: xzl Date: Wed, 8 May 2024 13:55:03 +0800 Subject: [PATCH] feat: update maven-script-interpreter to 1.4 Maven Script Interpreter Issue: https://github.com/deepin-community/sig-deepin-sysdev-team/issues/547 Log: update repo --- .asf.yaml | 26 ++ .gitignore | 1 - Jenkinsfile | 20 ++ README.md | 100 +++++++ debian/changelog | 13 + debian/compat | 1 - debian/control | 30 +- debian/copyright | 2 +- debian/libmaven-script-interpreter-java.poms | 2 +- debian/maven.ignoreRules | 2 +- debian/maven.properties | 2 + debian/maven.rules | 5 +- debian/orig-tar.sh | 20 -- debian/rules | 3 - debian/watch | 7 +- pom.xml | 117 ++++---- .../BeanShellScriptInterpreter.java | 133 ++++----- .../scriptinterpreter/ExecutionLogger.java | 16 +- .../shared/scriptinterpreter/FileLogger.java | 179 ++++++----- .../FileLoggerMirrorHandler.java | 34 +++ .../GroovyScriptInterpreter.java | 96 +++--- .../scriptinterpreter/RunErrorException.java | 51 ---- .../RunFailureException.java | 69 ----- .../ScriptEvaluationException.java | 29 +- .../scriptinterpreter/ScriptException.java | 47 +++ .../scriptinterpreter/ScriptInterpreter.java | 23 +- .../ScriptReturnException.java | 54 ++++ .../scriptinterpreter/ScriptRunner.java | 272 +++++++---------- src/site/apt/index.apt.vm | 94 ++++-- src/site/site.xml | 11 +- src/site/xdoc/download.xml.vm | 6 +- src/test-class-path/class-path.txt | 18 ++ .../BeanShellScriptInterpreterTest.java | 50 ++-- .../scriptinterpreter/FileLoggerTest.java | 118 ++++++++ .../GroovyScriptInterpreterTest.java | 85 ++++-- .../scriptinterpreter/ScriptRunnerTest.java | 282 +++++++++++++----- .../scriptinterpreter/TestMirrorHandler.java | 41 +++ src/test/resources/bsh-test/failed.bsh | 20 ++ .../resources/bsh-test/return-not-true.bsh | 20 ++ src/test/resources/bsh-test/return-null.bsh | 20 ++ src/test/resources/class-path.txt | 18 ++ src/test/resources/groovy-test/failed.groovy | 25 ++ .../resources/groovy-test/return-false.groovy | 21 ++ .../resources/groovy-test/return-null.groovy | 20 ++ src/test/resources/groovy-test/verify.groovy | 3 +- 45 files changed, 1410 insertions(+), 796 deletions(-) create mode 100644 .asf.yaml delete mode 100644 .gitignore create mode 100644 Jenkinsfile create mode 100644 README.md delete mode 100644 debian/compat delete mode 100755 debian/orig-tar.sh create mode 100644 src/main/java/org/apache/maven/shared/scriptinterpreter/FileLoggerMirrorHandler.java delete mode 100644 src/main/java/org/apache/maven/shared/scriptinterpreter/RunErrorException.java delete mode 100644 src/main/java/org/apache/maven/shared/scriptinterpreter/RunFailureException.java create mode 100644 src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java create mode 100644 src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java create mode 100644 src/test-class-path/class-path.txt create mode 100644 src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java create mode 100644 src/test/java/org/apache/maven/shared/scriptinterpreter/TestMirrorHandler.java create mode 100644 src/test/resources/bsh-test/failed.bsh create mode 100644 src/test/resources/bsh-test/return-not-true.bsh create mode 100644 src/test/resources/bsh-test/return-null.bsh create mode 100644 src/test/resources/class-path.txt create mode 100644 src/test/resources/groovy-test/failed.groovy create mode 100644 src/test/resources/groovy-test/return-false.groovy create mode 100644 src/test/resources/groovy-test/return-null.groovy diff --git a/.asf.yaml b/.asf.yaml new file mode 100644 index 0000000..5a6a6f1 --- /dev/null +++ b/.asf.yaml @@ -0,0 +1,26 @@ +# +# 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. +# +# see https://s.apache.org/asfyaml +github: + description: "Apache Maven Script Interpreter" + homepage: https://maven.apache.org/shared/maven-script-interpreter/ + labels: + - java + - build-management + - maven-shared + - maven + - hacktoberfest diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 224e7f0..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.pc/ diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..06ba6fc --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,20 @@ +/** + * 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. + */ + +asfMavenTlpStdBuild(jdks:[ "8", "11", "17", "19"]) diff --git a/README.md b/README.md new file mode 100644 index 0000000..2fd23c9 --- /dev/null +++ b/README.md @@ -0,0 +1,100 @@ + +Contributing to [Apache Maven Script Interpreter](https://maven.apache.org/shared/maven-script-interpreter/) +====================== + +[![ASF Jira](https://img.shields.io/endpoint?url=https%3A%2F%2Fmaven.apache.org%2Fbadges%2Fasf_jira-MSHARED.json)][jira] +[![Apache License, Version 2.0, January 2004](https://img.shields.io/github/license/apache/maven.svg?label=License)][license] +[![Maven Central](https://img.shields.io/maven-central/v/org.apache.maven.shared/maven-script-interpreter.svg?label=Maven%20Central)](https://search.maven.org/artifact/org.apache.maven.shared/maven-script-interpreter) +[![Reproducible Builds](https://img.shields.io/badge/Reproducible_Builds-ok-green?labelColor=blue)](https://github.com/jvm-repo-rebuild/reproducible-central/blob/master/content/org/apache/maven/shared/maven-script-interpreter/README.md) +[![Jenkins Status](https://img.shields.io/jenkins/s/https/ci-maven.apache.org/job/Maven/job/maven-box/job/maven-script-interpreter/job/master.svg)][build] +[![Jenkins tests](https://img.shields.io/jenkins/t/https/ci-maven.apache.org/job/Maven/job/maven-box/job/maven-script-interpreter/job/master.svg)][test-results] + + +You have found a bug or you have an idea for a cool new feature? Contributing +code is a great way to give something back to the open source community. Before +you dig right into the code, there are a few guidelines that we need +contributors to follow so that we can have a chance of keeping on top of +things. + +Getting Started +--------------- + ++ Make sure you have a [JIRA account](https://issues.apache.org/jira/). ++ Make sure you have a [GitHub account](https://github.com/signup/free). ++ If you're planning to implement a new feature, it makes sense to discuss your changes + on the [dev list][ml-list] first. + This way you can make sure you're not wasting your time on something that isn't + considered to be in Apache Maven's scope. ++ Submit a ticket for your issue, assuming one does not already exist. + + Clearly describe the issue, including steps to reproduce when it is a bug. + + Make sure you fill in the earliest version that you know has the issue. ++ Fork the repository on GitHub. + +Making and Submitting Changes +-------------- + +We accept Pull Requests via GitHub. The [developer mailing list][ml-list] is the +main channel of communication for contributors. +There are some guidelines which will make applying PRs easier for us: ++ Create a topic branch from where you want to base your work (this is usually the master branch). + Push your changes to a topic branch in your fork of the repository. ++ Make commits of logical units. ++ Respect the original code style: by using the same [codestyle][code-style], + patches should only highlight the actual difference, not being disturbed by any formatting issues: + + Only use spaces for indentation. + + Create minimal diffs - disable on save actions like reformat source code or organize imports. + If you feel the source code should be reformatted, create a separate PR for this change. + + Check for unnecessary whitespace with `git diff --check` before committing. ++ Make sure your commit messages are in the proper format. Your commit message should contain the key of the JIRA issue. +``` +[MSHARED-XXX] - Subject of the JIRA Ticket + Optional supplemental description. +``` ++ Make sure you have added the necessary tests (JUnit/IT) for your changes. ++ Run all the tests with `mvn -Prun-its verify` to assure nothing else was accidentally broken. ++ Submit a pull request to the repository in the Apache organization. ++ Update your JIRA ticket and include a link to the pull request in the ticket. + +If you plan to contribute on a regular basis, please consider filing a [contributor license agreement][cla]. + +Making Trivial Changes +---------------------- + +For changes of a trivial nature to comments and documentation, it is not always +necessary to create a new ticket in JIRA. In this case, it is appropriate to +start the first line of a commit with '(doc)' instead of a ticket number. + +Additional Resources +-------------------- + ++ [Contributing patches](https://maven.apache.org/guides/development/guide-maven-development.html#Creating_and_submitting_a_patch) ++ [Apache Maven Shared Components project page][jira] ++ [Contributor License Agreement][cla] ++ [General GitHub documentation](https://help.github.com/) ++ [GitHub pull request documentation](https://help.github.com/send-pull-requests/) ++ [Apache Maven Twitter Account](https://twitter.com/ASFMavenProject) ++ #Maven IRC channel on freenode.org + +[jira]: https://issues.apache.org/jira/projects/MSHARED/ +[license]: https://www.apache.org/licenses/LICENSE-2.0 +[ml-list]: https://maven.apache.org/mailing-lists.html +[code-style]: https://maven.apache.org/developers/conventions/code.html +[cla]: https://www.apache.org/licenses/#clas +[maven-wiki]: https://cwiki.apache.org/confluence/display/MAVEN/Index +[test-results]: https://ci-maven.apache.org/job/Maven/job/maven-box/job/maven-script-interpreter/job/master/lastCompletedBuild/testReport/ +[build]: https://ci-maven.apache.org/job/Maven/job/maven-box/job/maven-script-interpreter/job/master/ diff --git a/debian/changelog b/debian/changelog index 3659d40..c3c97d0 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,16 @@ +maven-script-interpreter (1.4-1) unstable; urgency=medium + + * New upstream release + - Updated the maven-debian-helper rules + - Removed the dependency on libmaven-shared-utils-java + * Depend on libmaven-parent-java + * Standards-Version updated to 4.6.2 + * Switch to debhelper level 13 + * Use salsa.debian.org Vcs-* URLs + * Track and download the new releases from GitHub + + -- Emmanuel Bourg Tue, 10 Jan 2023 00:14:29 +0100 + maven-script-interpreter (1.2-1) unstable; urgency=medium * New upstream release diff --git a/debian/compat b/debian/compat deleted file mode 100644 index f599e28..0000000 --- a/debian/compat +++ /dev/null @@ -1 +0,0 @@ -10 diff --git a/debian/control b/debian/control index cb55559..426a844 100644 --- a/debian/control +++ b/debian/control @@ -3,24 +3,26 @@ Section: java Priority: optional Maintainer: Debian Java Maintainers Uploaders: Emmanuel Bourg -Build-Depends: ant, - debhelper (>= 10), - default-jdk, - groovy, - junit4, - libbsh-java (>= 2.0b4), - libmaven3-core-java, - libplexus-component-metadata-java, - libmaven-shared-utils-java, - maven-debian-helper (>= 1.5) -Standards-Version: 4.0.0 -Vcs-Git: https://anonscm.debian.org/git/pkg-java/maven-script-interpreter.git -Vcs-Browser: https://anonscm.debian.org/cgit/pkg-java/maven-script-interpreter.git +Build-Depends: + ant, + debhelper-compat (= 13), + default-jdk, + groovy, + junit4, + libeclipse-sisu-maven-plugin-java, + libbsh-java (>= 2.0b4), + libmaven-parent-java, + libmaven3-core-java, + libmodello-maven-plugin-java, + maven-debian-helper (>= 1.5) +Standards-Version: 4.6.2 +Vcs-Git: https://salsa.debian.org/java-team/maven-script-interpreter.git +Vcs-Browser: https://salsa.debian.org/java-team/maven-script-interpreter Homepage: https://maven.apache.org/shared/maven-script-interpreter/ Package: libmaven-script-interpreter-java Architecture: all -Depends: ${maven:Depends}, ${misc:Depends} +Depends: ${misc:Depends}, ${maven:Depends} Suggests: ${maven:OptionalDepends} Description: Maven Script Interpreter This component provides some utilities to interpret/execute some scripts diff --git a/debian/copyright b/debian/copyright index f0c260c..74c1940 100644 --- a/debian/copyright +++ b/debian/copyright @@ -1,6 +1,6 @@ Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Upstream-Name: Maven Script Interpreter -Source: https://maven.apache.org/shared/maven-script-interpreter/ +Source: https://github.com/apache/maven-script-interpreter Files: * Copyright: 2011-2012, The Apache Software Foundation diff --git a/debian/libmaven-script-interpreter-java.poms b/debian/libmaven-script-interpreter-java.poms index 5ad70f2..0417132 100644 --- a/debian/libmaven-script-interpreter-java.poms +++ b/debian/libmaven-script-interpreter-java.poms @@ -25,4 +25,4 @@ # --site-xml=: Optional, the location for site.xml if it needs to be installed. # Empty by default. [mh_install] # -pom.xml --no-parent --has-package-version +pom.xml --has-package-version diff --git a/debian/maven.ignoreRules b/debian/maven.ignoreRules index 8b13789..f02d2cc 100644 --- a/debian/maven.ignoreRules +++ b/debian/maven.ignoreRules @@ -1 +1 @@ - +org.apache.maven.plugins maven-checkstyle-plugin diff --git a/debian/maven.properties b/debian/maven.properties index 7c29bd7..dd4b615 100644 --- a/debian/maven.properties +++ b/debian/maven.properties @@ -2,3 +2,5 @@ # For example: # maven.test.skip=true +maven.compiler.source=8 +maven.compiler.target=8 diff --git a/debian/maven.rules b/debian/maven.rules index c8df1c1..1293ab6 100644 --- a/debian/maven.rules +++ b/debian/maven.rules @@ -1,5 +1,6 @@ junit junit jar s/4\..*/4.x/ * * org.apache.maven maven-plugin-api jar s/.*/3.x/ * * -org.codehaus.plexus plexus-component-annotations jar s/.*/1.5.5/ * * -org.codehaus.plexus plexus-component-metadata maven-plugin s/1.7.1/1.5.5/ * * +s/org.apache.groovy/org.codehaus.groovy/ * * s/.*/debian/ * * +s/org.apache-extras.beanshell/org.beanshell/ bsh * s/.*/debian/ * * +org.apache.maven.shared maven-shared-components pom s/.*/debian/ * * diff --git a/debian/orig-tar.sh b/debian/orig-tar.sh deleted file mode 100755 index 200f6d9..0000000 --- a/debian/orig-tar.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -e -# -# Removes unwanted content from the upstream sources. -# Called by uscan with '--upstream-version' -# - -VERSION=$2 -TAR=../maven-script-interpreter_$VERSION.orig.tar.xz -DIR=maven-script-interpreter-$VERSION -TAG=$(echo "maven-script-interpreter-$VERSION" | sed -re's/~(alpha|beta|rc)/-\1-/') - -svn export http://svn.apache.org/repos/asf/maven/shared/tags/${TAG}/ $DIR -XZ_OPT=--best tar -c -J -f $TAR --exclude '*.jar' --exclude '*.class' $DIR -rm -rf $DIR ../$TAG - -# move to directory 'tarballs' -if [ -r .svn/deb-layout ]; then - . .svn/deb-layout - mv $TAR $origDir && echo "moved $TAR to $origDir" -fi diff --git a/debian/rules b/debian/rules index 5985cac..2d33f6a 100755 --- a/debian/rules +++ b/debian/rules @@ -2,6 +2,3 @@ %: dh $@ - -get-orig-source: - uscan --download-current-version --force-download --no-symlink diff --git a/debian/watch b/debian/watch index 219ab90..19593cc 100644 --- a/debian/watch +++ b/debian/watch @@ -1,4 +1,3 @@ -version=3 -opts="uversionmangle=s/-(alpha|beta|rc)(-)?/~$1$2/" \ - http://svn.apache.org/repos/asf/maven/shared/tags/ \ - maven-script-interpreter-(\d.*)/ debian debian/orig-tar.sh +version=4 +opts="mode=git" \ +https://github.com/apache/maven-script-interpreter refs/tags/maven-script-interpreter-([\d.]+) diff --git a/pom.xml b/pom.xml index 6d0e932..d5a343b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,4 @@ - + + org.apache.maven.plugins + maven-checkstyle-plugin + + config/maven_checks_nocodestyle.xml + + + + diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java index ae94200..6eb9de0 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java @@ -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 @@ * "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 @@ -18,11 +16,7 @@ * specific language governing permissions and limitations * under the License. */ - -import bsh.Capabilities; -import bsh.EvalError; -import bsh.Interpreter; -import bsh.TargetError; +package org.apache.maven.shared.scriptinterpreter; import java.io.File; import java.io.IOException; @@ -30,109 +24,82 @@ import java.util.List; import java.util.Map; -import org.codehaus.plexus.component.annotations.Component; +import bsh.Capabilities; +import bsh.EvalError; +import bsh.Interpreter; +import bsh.TargetError; /** * Provides a facade to evaluate BeanShell scripts. * * @author Benjamin Bentmann - * @version $Id: BeanShellScriptInterpreter.java 1361828 2012-07-15 22:30:27Z hboutemy $ */ -@Component( role = ScriptInterpreter.class, hint = "bsh" ) -public class BeanShellScriptInterpreter - implements ScriptInterpreter -{ +class BeanShellScriptInterpreter implements ScriptInterpreter { - /** - * {@inheritDoc} - */ - public Object evaluateScript( String script, List classPath, Map globalVariables, - PrintStream scriptOutput ) - throws ScriptEvaluationException - { + /** {@inheritDoc} */ + @Override + public Object evaluateScript( + String script, + List classPath, + Map globalVariables, + PrintStream scriptOutput) + throws ScriptEvaluationException { PrintStream origOut = System.out; PrintStream origErr = System.err; - try - { + try { Interpreter engine = new Interpreter(); - if ( scriptOutput != null ) - { - System.setErr( scriptOutput ); - System.setOut( scriptOutput ); - engine.setErr( scriptOutput ); - engine.setOut( scriptOutput ); + if (scriptOutput != null) { + System.setErr(scriptOutput); + System.setOut(scriptOutput); + engine.setErr(scriptOutput); + engine.setOut(scriptOutput); } - if ( !Capabilities.haveAccessibility() ) - { - try - { - Capabilities.setAccessibility( true ); - } - catch ( Exception e ) - { - if ( scriptOutput != null ) - { - e.printStackTrace( scriptOutput ); + if (!Capabilities.haveAccessibility()) { + try { + Capabilities.setAccessibility(true); + } catch (Exception e) { + if (scriptOutput != null) { + e.printStackTrace(scriptOutput); } } } - if ( classPath != null && !classPath.isEmpty() ) - { - for ( String path : classPath ) - { - try - { - engine.getClassManager().addClassPath( new File( path ).toURI().toURL() ); - } - catch ( IOException e ) - { - throw new RuntimeException( "bad class path: " + path, e ); + if (classPath != null && !classPath.isEmpty()) { + for (String path : classPath) { + try { + engine.getClassManager() + .addClassPath(new File(path).toURI().toURL()); + } catch (IOException e) { + throw new RuntimeException("bad class path: " + path, e); } } } - if ( globalVariables != null ) - { - for ( String variable : globalVariables.keySet() ) - { - Object value = globalVariables.get( variable ); - try - { - engine.set( variable, value ); - } - catch ( EvalError e ) - { - throw new RuntimeException( e ); + if (globalVariables != null) { + for (Map.Entry entry : globalVariables.entrySet()) { + try { + engine.set(entry.getKey(), entry.getValue()); + } catch (EvalError e) { + throw new RuntimeException(e); } } } - try - { - return engine.eval( script ); - } - catch ( TargetError e ) - { - throw new ScriptEvaluationException( e.getTarget() ); - } - catch ( ThreadDeath e ) - { + try { + return engine.eval(script); + } catch (TargetError e) { + throw new ScriptEvaluationException(e.getTarget()); + } catch (ThreadDeath e) { throw e; + } catch (Throwable e) { + throw new ScriptEvaluationException(e); } - catch ( Throwable e ) - { - throw new ScriptEvaluationException( e ); - } - } - finally - { - System.setErr( origErr ); - System.setOut( origOut ); + } finally { + System.setErr(origErr); + System.setOut(origOut); } } - } diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ExecutionLogger.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ExecutionLogger.java index efa06b6..ad254e7 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ExecutionLogger.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ExecutionLogger.java @@ -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 @@ -18,25 +16,27 @@ * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.shared.scriptinterpreter; import java.io.PrintStream; /** + *

ExecutionLogger interface.

+ * * @author Olivier Lamy */ -public interface ExecutionLogger -{ +public interface ExecutionLogger { /** - * The stream which will catch the output of the {@link ScriptRunner}. - * + * The stream which will catch the output of the {@link org.apache.maven.shared.scriptinterpreter.ScriptRunner}. + * * @return the output stream */ PrintStream getPrintStream(); /** * Consume logging from this component. - * + * * @param line the line to consume */ - void consumeLine( String line ); + void consumeLine(String line); } diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java index d78d226..e1168f3 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java @@ -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 @@ * "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 @@ -18,21 +16,19 @@ * specific language governing permissions and limitations * under the License. */ - -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.shared.utils.io.IOUtil; +package org.apache.maven.shared.scriptinterpreter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.PrintStream; /** - * @version $Id: FileLogger.java 1389141 2012-09-23 20:29:01Z hboutemy $ + *

FileLogger class.

+ * */ -public class FileLogger - implements ExecutionLogger -{ +public class FileLogger implements ExecutionLogger, AutoCloseable { /** * The path to the log file. @@ -44,119 +40,140 @@ public class FileLogger */ private PrintStream stream; - /** - * A flag whether the output stream should be closed during finalization of this logger. - */ - private boolean shouldFinalize = true; - - /** - * The optional mojo logger to additionally write messages to, can be null. - */ - private final Log log; - /** * Creates a new logger that writes to the specified file. - * - * @param outputFile The path to the output file, must not be null. + * + * @param outputFile The path to the output file, if null all message will be discarded. * @throws java.io.IOException If the output file could not be created. */ - public FileLogger( File outputFile ) - throws IOException - { - this( outputFile, null ); + public FileLogger(File outputFile) throws IOException { + this(outputFile, null); } /** - * Creates a new logger that writes to the specified file and optionally mirrors messages to the given mojo logger. + * Creates a new logger that writes to the specified file and optionally mirrors messages. * - * @param outputFile The path to the output file, must not be null. - * @param log The mojo logger to additionally output messages to, may be null if not used. + * @param outputFile The path to the output file, if null all message will be discarded. + * @param mirrorHandler The class which handle mirrored message, can be null. * @throws java.io.IOException If the output file could not be created. */ - public FileLogger( File outputFile, Log log ) - throws IOException - { + public FileLogger(File outputFile, FileLoggerMirrorHandler mirrorHandler) throws IOException { this.file = outputFile; - this.log = log; - - outputFile.getParentFile().mkdirs(); - stream = new PrintStream( new FileOutputStream( outputFile ) ); - - Runnable finalizer = new Runnable() - { - public void run() - { - try - { - finalize(); - } - catch ( Throwable e ) - { - // ignore - } - } - }; - Runtime.getRuntime().addShutdownHook( new Thread( finalizer ) ); + OutputStream outputStream; + + if (outputFile != null) { + outputFile.getParentFile().mkdirs(); + outputStream = new FileOutputStream(outputFile); + } else { + outputStream = new NullOutputStream(); + } + + if (mirrorHandler != null) { + stream = new PrintStream(new MirrorStreamWrapper(outputStream, mirrorHandler)); + } else { + stream = new PrintStream(outputStream); + } } /** * Gets the path to the output file. - * + * * @return The path to the output file, never null. */ - public File getOutputFile() - { + public File getOutputFile() { return file; } /** * Gets the underlying stream used to write message to the log file. - * + * * @return The underlying stream used to write message to the log file, never null. */ - public PrintStream getPrintStream() - { + @Override + public PrintStream getPrintStream() { return stream; } /** - * Writes the specified line to the log file and optionally to the mojo logger. - * + * Writes the specified line to the log file + * and invoke {@link FileLoggerMirrorHandler#consumeOutput(String)} if is given. + * * @param line The message to log. */ - public void consumeLine( String line ) - { - stream.println( line ); + @Override + public void consumeLine(String line) { + stream.println(line); stream.flush(); - - if ( log != null ) - { - log.info( line ); - } } /** * Closes the underlying file stream. */ - public void close() - { - if ( stream != null ) - { + public void close() { + if (stream != null) { stream.flush(); + stream.close(); + stream = null; } + } + + private static class MirrorStreamWrapper extends OutputStream { + private final OutputStream out; + private final FileLoggerMirrorHandler mirrorHandler; + + private StringBuilder lineBuffer; - IOUtil.close( stream ); + MirrorStreamWrapper(OutputStream outputStream, FileLoggerMirrorHandler mirrorHandler) { + this.out = outputStream; + this.mirrorHandler = mirrorHandler; + this.lineBuffer = new StringBuilder(); + } + + @Override + public void write(int b) throws IOException { + out.write(b); + lineBuffer.append((char) (b)); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + out.write(b, off, len); + lineBuffer.append(new String(b, off, len)); + } + + @Override + public void flush() throws IOException { + out.flush(); + + int len = lineBuffer.length(); + if (len == 0) { + // nothing to log + return; + } + + // remove line end for log + while (len > 0 && (lineBuffer.charAt(len - 1) == '\n' || lineBuffer.charAt(len - 1) == '\r')) { + len--; + } + lineBuffer.setLength(len); + + mirrorHandler.consumeOutput(lineBuffer.toString()); + + // clear buffer + lineBuffer = new StringBuilder(); + } } - /** - * Closes the underlying file stream. - */ - protected void finalize() - { - if ( shouldFinalize ) - { - close(); + private static class NullOutputStream extends OutputStream { + @Override + public void write(int b) { + // do nothing + } + + @Override + public void write(byte[] b, int off, int len) { + // do nothing } } } diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLoggerMirrorHandler.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLoggerMirrorHandler.java new file mode 100644 index 0000000..4819d0e --- /dev/null +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLoggerMirrorHandler.java @@ -0,0 +1,34 @@ +/* + * 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. + */ +package org.apache.maven.shared.scriptinterpreter; + +/** + * Handle output form interpreter. + * + * @since 1.3 + */ +public interface FileLoggerMirrorHandler { + /** + * Handle output message generated by script interpreter. + * This method is invoked when flush occurs on the underlying stream. + * + * @param message last message + */ + void consumeOutput(String message); +} 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 8b14586..f348e84 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java @@ -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 @@ * "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 @@ -18,84 +16,62 @@ * specific language governing permissions and limitations * under the License. */ - -import groovy.lang.Binding; -import groovy.lang.GroovyShell; -import org.apache.tools.ant.AntClassLoader; -import org.codehaus.groovy.control.CompilerConfiguration; -import org.codehaus.plexus.component.annotations.Component; +package org.apache.maven.shared.scriptinterpreter; import java.io.File; import java.io.PrintStream; -import java.io.PrintWriter; +import java.net.URL; import java.util.List; import java.util.Map; +import groovy.lang.Binding; +import groovy.lang.GroovyShell; +import org.codehaus.groovy.control.CompilerConfiguration; +import org.codehaus.groovy.tools.RootLoader; + /** * Provides a facade to evaluate Groovy scripts. - * + * * @author Benjamin Bentmann - * @version $Id: GroovyScriptInterpreter.java 1361828 2012-07-15 22:30:27Z hboutemy $ */ -@Component( role = ScriptInterpreter.class, hint = "groovy" ) -public class GroovyScriptInterpreter - implements ScriptInterpreter -{ +class GroovyScriptInterpreter implements ScriptInterpreter { - /** - * {@inheritDoc} - */ - public Object evaluateScript( String script, List classPath, Map globalVariables, - PrintStream scriptOutput ) - throws ScriptEvaluationException - { + /** {@inheritDoc} */ + @Override + public Object evaluateScript( + String script, + List classPath, + Map globalVariables, + PrintStream scriptOutput) + throws ScriptEvaluationException { PrintStream origOut = System.out; PrintStream origErr = System.err; - try - { - CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT ); + try (RootLoader childFirstLoader = + new RootLoader(new URL[] {}, getClass().getClassLoader())) { - if ( scriptOutput != null ) - { - System.setErr( scriptOutput ); - System.setOut( scriptOutput ); - config.setOutput( new PrintWriter( scriptOutput ) ); + if (scriptOutput != null) { + System.setErr(scriptOutput); + System.setOut(scriptOutput); } - ClassLoader loader = null; - if ( classPath != null && !classPath.isEmpty() ) - { - AntClassLoader childFirstLoader = new AntClassLoader( getClass().getClassLoader(), false ); - for ( String path : classPath ) - { - childFirstLoader.addPathComponent( new File( path ) ); + if (classPath != null && !classPath.isEmpty()) { + for (String path : classPath) { + childFirstLoader.addURL(new File(path).toURI().toURL()); } - loader = childFirstLoader; } - Binding binding = new Binding( globalVariables ); + GroovyShell interpreter = new GroovyShell( + childFirstLoader, + new Binding(globalVariables), + new CompilerConfiguration(CompilerConfiguration.DEFAULT)); - GroovyShell interpreter = new GroovyShell( loader, binding, config ); - - try - { - return interpreter.evaluate( script ); - } - catch ( ThreadDeath e ) - { - throw e; - } - catch ( Throwable e ) - { - throw new ScriptEvaluationException( e ); - } - } - finally - { - System.setErr( origErr ); - System.setOut( origOut ); + return interpreter.evaluate(script); + } catch (Throwable e) { + throw new ScriptEvaluationException(e); + } finally { + System.setErr(origErr); + System.setOut(origOut); } } - } 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 98d2103..0000000 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/RunErrorException.java +++ /dev/null @@ -1,51 +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 - * @version $Id: RunErrorException.java 1199475 2011-11-08 21:51:13Z olamy $ - */ -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 null. - * @param type The type of build failure, may not be null. - * @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 3beb519..0000000 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/RunFailureException.java +++ /dev/null @@ -1,69 +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 - * @version $Id: RunFailureException.java 1199475 2011-11-08 21:51:13Z olamy $ - */ -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 null. - * @param type The type of build failure, may not be null. - */ - 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 null. - */ - 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 71c215f..cad3bb1 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java @@ -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 @@ * "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 @@ -18,17 +16,15 @@ * specific language governing permissions and limitations * under the License. */ +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 - * @version $Id: ScriptEvaluationException.java 1194928 2011-10-29 16:37:19Z olamy $ */ -public class ScriptEvaluationException - extends Exception -{ +public class ScriptEvaluationException extends ScriptException { /** * The serial version identifier for this class. @@ -37,12 +33,21 @@ public class ScriptEvaluationException /** * Creates a new exception with the specified cause. - * + * * @param cause The cause, may be null. */ - public ScriptEvaluationException( Throwable cause ) - { - super( cause ); + ScriptEvaluationException(Throwable cause) { + super(cause); } + /** + * Creates a new exception with the specified message and cause. + * + * @param message The message, may be null. + * @param cause The cause, may be null. + * @since 1.3 + */ + public ScriptEvaluationException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java new file mode 100644 index 0000000..205d50d --- /dev/null +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java @@ -0,0 +1,47 @@ +/* + * 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. + */ +package org.apache.maven.shared.scriptinterpreter; + +/** + * Common errors during script running. + * + * @author Slawomir Jaranowski + * @since 1.3 + */ +public class ScriptException extends Exception { + private static final long serialVersionUID = 4553276474852776472L; + + /** + * Creates a new exception with the specified message and cause. + * + * @param message The message, may be null. + * @param cause The cause, may be null. + */ + 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/ScriptInterpreter.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java index e591b92..f2da6d8 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java @@ -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 @@ * "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 @@ -18,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.shared.scriptinterpreter; import java.io.PrintStream; import java.util.List; @@ -26,16 +25,14 @@ /** * Defines a simple abstraction used to plug-in several script interpreters for the pre-/post-build-hooks. Each * interpreter implementation should be stateless and support reuse. - * + * * @author Benjamin Bentmann - * @version $Id: ScriptInterpreter.java 1389141 2012-09-23 20:29:01Z hboutemy $ */ -public interface ScriptInterpreter -{ +public interface ScriptInterpreter { /** * Evaluates the specified script. - * + * * @param script The script contents to evaluate, must not be null. * @param classPath The additional class path for the script interpreter, may be null or empty if only * the plugin realm should be used for the script evaluation. If specified, this class path will precede @@ -47,8 +44,10 @@ public interface ScriptInterpreter * @return The return value from the script, can be null * @throws ScriptEvaluationException If the script evaluation produced an error. */ - Object evaluateScript( String script, List classPath, Map globalVariables, - PrintStream scriptOutput ) - throws ScriptEvaluationException; - + Object evaluateScript( + String script, + List classPath, + Map globalVariables, + PrintStream scriptOutput) + throws ScriptEvaluationException; } diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java new file mode 100644 index 0000000..6a8431f --- /dev/null +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java @@ -0,0 +1,54 @@ +/* + * 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. + */ +package org.apache.maven.shared.scriptinterpreter; + +/** + * Signals an invalid value returned from script execution. + * + * @author Slawomir Jaranowski + * @since 1.3 + */ +public class ScriptReturnException extends ScriptException { + + private static final long serialVersionUID = -4705573157701206786L; + /** + * Result + */ + private final Object result; + + /** + * Creates a new exception with the specified message and result. + * + * @param message The message, may be null. + * @param result The cause, may be null. + */ + ScriptReturnException(String message, Object result) { + super(message); + this.result = result; + } + + /** + * Retrieve result returned by script. + * + * @return script result. + */ + public Object getResult() { + 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 47d6ad1..485a7c1 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java @@ -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 @@ * "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 @@ -18,14 +16,12 @@ * specific language governing permissions and limitations * under the License. */ - -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.shared.utils.io.FileUtils; -import org.apache.maven.shared.utils.StringUtils; +package org.apache.maven.shared.scriptinterpreter; import java.io.File; import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; @@ -33,19 +29,18 @@ import java.util.Locale; import java.util.Map; +import org.apache.commons.io.FilenameUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Runs pre-/post-build hook scripts. * * @author Benjamin Bentmann - * @version $Id: ScriptRunner.java 1797598 2017-06-04 18:41:18Z hboutemy $ */ -public class ScriptRunner -{ +public class ScriptRunner { - /** - * The mojo logger to print diagnostic to, never null. - */ - private Log log; + private static final Logger LOG = LoggerFactory.getLogger(ScriptRunner.class); /** * The supported script interpreters, indexed by the lower-case file extension of their associated script files, @@ -69,48 +64,34 @@ public class ScriptRunner private String encoding; /** - * Creates a new script runner. - * - * @param log The mojo logger to print diagnostic to, must not be null. + * Creates a new script runner with BSH and Groovy interpreters. */ - public ScriptRunner( Log log ) - { - if ( log == null ) - { - throw new IllegalArgumentException( "missing logger" ); - } - this.log = log; - scriptInterpreters = new LinkedHashMap(); - scriptInterpreters.put( "bsh", new BeanShellScriptInterpreter() ); - scriptInterpreters.put( "groovy", new GroovyScriptInterpreter() ); - globalVariables = new HashMap(); - classPath = new ArrayList(); - } - - public void addScriptInterpreter( String id, ScriptInterpreter scriptInterpreter ) - { - scriptInterpreters.put( id, scriptInterpreter ); + public ScriptRunner() { + scriptInterpreters = new LinkedHashMap<>(); + scriptInterpreters.put("bsh", new BeanShellScriptInterpreter()); + scriptInterpreters.put("groovy", new GroovyScriptInterpreter()); + globalVariables = new HashMap<>(); + classPath = new ArrayList<>(); } /** - * Gets the mojo logger. + * Add new script Interpreter * - * @return The mojo logger, never null. + * @param id The Id of interpreter + * @param scriptInterpreter the Script Interpreter implementation */ - private Log getLog() - { - return log; + public void addScriptInterpreter(String id, ScriptInterpreter scriptInterpreter) { + scriptInterpreters.put(id, scriptInterpreter); } /** * Sets a global variable for the script interpreter. * - * @param name The name of the variable, must not be null. + * @param name The name of the variable, must not be null. * @param value The value of the variable, may be null. */ - public void setGlobalVariable( String name, Object value ) - { - this.globalVariables.put( name, value ); + public void setGlobalVariable(String name, Object value) { + this.globalVariables.put(name, value); } /** @@ -118,12 +99,11 @@ public void setGlobalVariable( String name, Object value ) * will not affect the scripts. * * @param classPath The additional class path for the script interpreter, may be null or empty if only - * the plugin realm should be used for the script evaluation. If specified, this class path will precede - * the artifacts from the plugin class path. + * the plugin realm should be used for the script evaluation. If specified, this class path will precede the + * artifacts from the plugin class path. */ - public void setClassPath( List classPath ) - { - this.classPath = ( classPath != null ) ? new ArrayList( classPath ) : new ArrayList(); + public void setClassPath(List classPath) { + this.classPath = (classPath != null) ? new ArrayList<>(classPath) : new ArrayList<>(); } /** @@ -132,9 +112,8 @@ public void setClassPath( List classPath ) * @param encoding The file encoding of the hook scripts, may be null or empty to use the platform's * default encoding. */ - public void setScriptEncoding( String encoding ) - { - this.encoding = StringUtils.isNotEmpty( encoding ) ? encoding : null; + public void setScriptEncoding(String encoding) { + this.encoding = (encoding != null && encoding.length() > 0) ? encoding : null; } /** @@ -146,38 +125,39 @@ public void setScriptEncoding( String encoding ) * 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 null. * @param logger The logger to redirect the script output to, may be null to use stdout/stderr. - * @param stage The stage of the build job the script is invoked in, must not be null. This is for - * logging purpose only. - * @param failOnException If true 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 true of threw an exception. + * @throws ScriptException If the script did not return true of threw an exception. */ - public void run( final String scriptDescription, final File basedir, final String relativeScriptPath, - final Map context, final ExecutionLogger logger, String stage, - boolean failOnException ) - throws IOException, RunFailureException - { - if ( relativeScriptPath == null ) - { - getLog().debug( scriptDescription + ": relativeScriptPath is null, not executing script" ); + public void run( + final String scriptDescription, + final File basedir, + final String relativeScriptPath, + final Map context, + final ExecutionLogger logger) + throws IOException, ScriptException { + if (relativeScriptPath == null) { + LOG.debug("{}: relativeScriptPath is null, not executing script", scriptDescription); return; } - final File scriptFile = resolveScript( new File( basedir, relativeScriptPath ) ); + final File scriptFile = resolveScript(new File(basedir, relativeScriptPath)); - if ( !scriptFile.exists() ) - { - getLog().debug( scriptDescription + ": no script '" + relativeScriptPath + "' found in directory " - + basedir.getAbsolutePath() ); + if (!scriptFile.exists()) { + LOG.debug( + "{} : no script '{}' found in directory {}", + scriptDescription, + relativeScriptPath, + basedir.getAbsolutePath()); return; } - getLog().info( "run " + scriptDescription + ' ' + relativeScriptPath + '.' - + FileUtils.extension( scriptFile.getAbsolutePath() ) ); + LOG.info( + "run {} {}.{}", + scriptDescription, + relativeScriptPath, + FilenameUtils.getExtension(scriptFile.getAbsolutePath())); - executeRun( scriptDescription, scriptFile, context, logger, stage, failOnException ); + executeRun(scriptDescription, scriptFile, context, logger); } /** @@ -187,104 +167,73 @@ public void run( final String scriptDescription, final File basedir, final Strin * @param scriptFile The path to the script, may be null to skip the script execution. * @param context The key-value storage used to share information between hook scripts, may be null. * @param logger The logger to redirect the script output to, may be null to use stdout/stderr. - * @param stage The stage of the build job the script is invoked in, must not be null. This is for - * logging purpose only. - * @param failOnException If true 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 true of threw an exception. + * @throws IOException If an I/O error occurred while reading the script file. + * @throws ScriptException If the script did not return true of threw an exception. */ - public void run( final String scriptDescription, File scriptFile, final Map context, - final ExecutionLogger logger, String stage, boolean failOnException ) - throws IOException, RunFailureException - { + public void run( + final String scriptDescription, File scriptFile, final Map context, final ExecutionLogger logger) + throws IOException, ScriptException { - if ( !scriptFile.exists() ) - { - getLog().debug( scriptDescription + ": script file not found in directory " - + scriptFile.getAbsolutePath() ); + if (!scriptFile.exists()) { + LOG.debug("{} : script file not found in directory {}", scriptDescription, scriptFile.getAbsolutePath()); return; } - getLog().info( "run " + scriptDescription + ' ' + scriptFile.getAbsolutePath() ); + 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 context, final ExecutionLogger logger, String stage, - boolean failOnException ) - throws IOException, RunFailureException - { - Map globalVariables = new HashMap( this.globalVariables ); - globalVariables.put( "basedir", scriptFile.getParentFile() ); - globalVariables.put( "context", context ); - - ScriptInterpreter interpreter = getInterpreter( scriptFile ); - if ( getLog().isDebugEnabled() ) - { + private void executeRun( + final String scriptDescription, File scriptFile, final Map context, final ExecutionLogger logger) + throws IOException, ScriptException { + ScriptInterpreter interpreter = getInterpreter(scriptFile); + if (LOG.isDebugEnabled()) { String name = interpreter.getClass().getName(); - name = name.substring( name.lastIndexOf( '.' ) + 1 ); - getLog().debug( "Running script with " + name + ": " + scriptFile ); + name = name.substring(name.lastIndexOf('.') + 1); + LOG.debug("Running script with {} :{}", name, scriptFile); } String script; - try - { - script = FileUtils.fileRead( scriptFile, encoding ); - } - catch ( IOException e ) - { + try { + byte[] bytes = Files.readAllBytes(scriptFile.toPath()); + if (encoding != null) { + script = new String(bytes, encoding); + } else { + script = new String(bytes); + } + } catch (IOException e) { String errorMessage = - "error reading " + scriptDescription + " " + scriptFile.getPath() + ", " + e.getMessage(); - IOException ioException = new IOException( errorMessage ); - ioException.initCause( e ); - throw ioException; + "error reading " + scriptDescription + " " + scriptFile.getPath() + ", " + e.getMessage(); + throw new IOException(errorMessage, e); } Object result; - try - { - if ( logger != null ) - { - logger.consumeLine( "Running " + scriptDescription + ": " + scriptFile ); + try { + if (logger != null) { + logger.consumeLine("Running " + scriptDescription + ": " + scriptFile); } - PrintStream out = ( logger != null ) ? logger.getPrintStream() : null; + PrintStream out = (logger != null) ? logger.getPrintStream() : null; - result = interpreter.evaluateScript( script, classPath, globalVariables, out ); - if ( logger != null ) - { - logger.consumeLine( "Finished " + scriptDescription + ": " + scriptFile ); - } - } - catch ( ScriptEvaluationException e ) - { - Throwable t = ( e.getCause() != null ) ? e.getCause() : e; - String msg = ( t.getMessage() != null ) ? t.getMessage() : t.toString(); - if ( getLog().isDebugEnabled() ) - { - String errorMessage = "Error evaluating " + scriptDescription + " " + scriptFile.getPath() + ", " + t; - getLog().debug( errorMessage, t ); - } - if ( logger != null ) - { - t.printStackTrace( logger.getPrintStream() ); - } - if ( failOnException ) - { - throw new RunFailureException( "The " + scriptDescription + " did not succeed. " + msg, stage ); + Map 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); } - else - { - throw new RunErrorException( "The " + scriptDescription + " did not succeed. " + msg, stage, t ); + } catch (ScriptEvaluationException e) { + Throwable t = (e.getCause() != null) ? e.getCause() : e; + if (logger != null) { + t.printStackTrace(logger.getPrintStream()); } + throw e; } - if ( !( result == null || Boolean.TRUE.equals( result ) || "true".equals( result ) ) ) - { - throw new RunFailureException( "The " + scriptDescription + " returned " + result + ".", stage ); + if (!(result == null || Boolean.parseBoolean(String.valueOf(result)))) { + throw new ScriptReturnException("The " + scriptDescription + " returned " + result + ".", result); } } @@ -295,15 +244,11 @@ private void executeRun( final String scriptDescription, File scriptFile, * @param scriptFile The script file to resolve, may be null. * @return The effective path to the script file or null if the input was null. */ - private File resolveScript( File scriptFile ) - { - if ( scriptFile != null && !scriptFile.exists() ) - { - for ( String ext : this.scriptInterpreters.keySet() ) - { - File candidateFile = new File( scriptFile.getPath() + '.' + ext ); - if ( candidateFile.exists() ) - { + private File resolveScript(File scriptFile) { + if (scriptFile != null && !scriptFile.exists()) { + for (String ext : this.scriptInterpreters.keySet()) { + File candidateFile = new File(scriptFile.getPath() + '.' + ext); + if (candidateFile.exists()) { scriptFile = candidateFile; break; } @@ -320,15 +265,12 @@ private File resolveScript( File scriptFile ) * @param scriptFile The script file for which to determine an interpreter, must not be null. * @return The script interpreter for the file, never null. */ - private ScriptInterpreter getInterpreter( File scriptFile ) - { - String ext = FileUtils.extension( scriptFile.getName() ).toLowerCase( Locale.ENGLISH ); - ScriptInterpreter interpreter = scriptInterpreters.get( ext ); - if ( interpreter == null ) - { - interpreter = scriptInterpreters.get( "bsh" ); + private ScriptInterpreter getInterpreter(File scriptFile) { + String ext = FilenameUtils.getExtension(scriptFile.getName()).toLowerCase(Locale.ENGLISH); + ScriptInterpreter interpreter = scriptInterpreters.get(ext); + if (interpreter == null) { + interpreter = scriptInterpreters.get("bsh"); } return interpreter; } - } diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm index f43ff0f..9e17463 100644 --- a/src/site/apt/index.apt.vm +++ b/src/site/apt/index.apt.vm @@ -40,81 +40,121 @@ ${project.name} +--------- -* Interpret beanshell script + <<>> has dependency only to core interpreters library, + all specific extensions should be added in your project. + For example, if you want to use {{{https://docs.groovy-lang.org/latest/html/documentation/grape.html}<>}} + in a <> script, you must add a dependency to Ivy in your project or in the plugin that will invoke the script: + ++--------- + + org.apache.ivy + ivy + ... + +--------- +* Using ScriptInterpreter + +** Interpret BeanShell script + ++--------- ScriptInterpreter interpreter = new BeanShellScriptInterpreter(); ByteArrayOutputStream out = new ByteArrayOutputStream(); interpreter.evaluateScript( script content, extra classPath entries, - Map globalVariables, new PrintStream( out ) ); - out.toString() returns script output - + Map globalVariables, new PrintStream( out ) ); +--------- -* Interpret a groovy script + <<>> returns script output. -+--------- +** Interpret a Groovy script ++--------- ScriptInterpreter interpreter = new GroovyScriptInterpreter(); ByteArrayOutputStream out = new ByteArrayOutputStream(); interpreter.evaluateScript( script content, extra classPath entries, - Map globalVariables, new PrintStream( out ) ); - out.toString() returns script output - + Map globalVariables, new PrintStream( out ) ); +--------- + <<>> returns script output. + * Using ScriptRunner - ScriptRunner class will detect the script file to run based on supported extensions (.bsh,.groovy). + <<>> class will detect the script file to run based on supported extensions (<<<.bsh>>>, <<<.groovy>>>). This class will search in the provided directory the script with the provided fileName and the supported extensions. - See {{{./apidocs/org/apache/maven/shared/scriptinterpreter/ScriptRunner.html}javadoc}} for run method. + See {{{./apidocs/org/apache/maven/shared/scriptinterpreter/ScriptRunner.html}javadoc}} for <<>> methods. +--------- - SystemStreamLog systemStreamLog = new SystemStreamLog(); - - ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); + 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 + + In order to do something more with script output, eg. log by your application you must implement <<>> + ++--------- +class MyMirrorHandler implements FileLoggerMirrorHandler +{ + void consumeOutput( String message ) + { + // this method is invoked every time when flush occurs on the underlying stream. + } +} ++--------- + + Now use it: + ++--------- + ScriptRunner scriptRunner = new ScriptRunner(); + scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify", buildContext(), + new FileLogger( logFile, new MyMirrorHandler() ) ); +--------- ** Global variables Your scripts will have by default two global variables: - * basedir: the base directory of your script + * <<>>: the base directory of your script - * context: the build context (see below) + * <<>>: the build context (see below) [] You can add more global variables as it. +--------- - - SystemStreamLog systemStreamLog = new SystemStreamLog(); - - ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); + ScriptRunner scriptRunner = new ScriptRunner(); scriptRunner.setGlobalVariable( name, value ); - +--------- ** Build context - You can pass some values to your script using a execution context which have the type << context>>>. + You can pass some values to your script using an execution context which have the type << context>>>: +--------- +private Map buildContext() +{ Map context = new HashMap(); context.put( "foo", "bar" ); return context; +} ++--------- + Then values are available in scripts context: + ++--------- // in your bsh script String value = context.get( "foo" ); ++--------- + value will be "bar" - // in your groovy script ++--------- + // in your Groovy script context.get("foo") +--------- @@ -123,12 +163,10 @@ ${project.name} You can add some additional classpath entries for your script execution +--------- - SystemStreamLog systemStreamLog = new SystemStreamLog(); List 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/site/site.xml b/src/site/site.xml index a18a2a6..fb03232 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -19,15 +19,20 @@ specific language governing permissions and limitations under the License. --> - + + + org.apache.maven.skins + maven-fluido-skin + 1.9 + - - + diff --git a/src/site/xdoc/download.xml.vm b/src/site/xdoc/download.xml.vm index 5d1650b..666d997 100644 --- a/src/site/xdoc/download.xml.vm +++ b/src/site/xdoc/download.xml.vm @@ -34,7 +34,7 @@ under the License.

In order to guard against corrupted downloads/installations, it is highly recommended to verify the signature - of the release bundles against the public KEYS used by the Apache Maven + of the release bundles against the public KEYS used by the Apache Maven developers.

${project.name} is distributed under the Apache License, version 2.0.

@@ -108,8 +108,8 @@ under the License. ${project.name} ${project.version} (Source zip) maven/shared/${project.artifactId}-${project.version}-source-release.zip - maven/shared/${project.artifactId}-${project.version}-source-release.zip.md5 - maven/shared/${project.artifactId}-${project.version}-source-release.zip.asc + maven/shared/${project.artifactId}-${project.version}-source-release.zip.sha512 + maven/shared/${project.artifactId}-${project.version}-source-release.zip.asc diff --git a/src/test-class-path/class-path.txt b/src/test-class-path/class-path.txt new file mode 100644 index 0000000..042f3ce --- /dev/null +++ b/src/test-class-path/class-path.txt @@ -0,0 +1,18 @@ +/* + * 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. + */ diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java index af5de46..a19614f 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java @@ -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 @@ * "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 @@ -18,44 +16,44 @@ * specific language governing permissions and limitations * under the License. */ - -import junit.framework.TestCase; +package org.apache.maven.shared.scriptinterpreter; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + /** * Tests the BeanShell interpreter facade. - * + * * @author Benjamin Bentmann - * @version $Id: BeanShellScriptInterpreterTest.java 1194925 2011-10-29 16:34:29Z olamy $ */ -public class BeanShellScriptInterpreterTest - extends TestCase -{ - - public void testEvaluateScript() - throws Exception - { +public class BeanShellScriptInterpreterTest { + @Test + public void testEvaluateScript() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ScriptInterpreter interpreter = new BeanShellScriptInterpreter(); - assertEquals( Boolean.TRUE, interpreter.evaluateScript( "System.out.print(\"Test\"); return true;", null, - null, new PrintStream( out ) ) ); - assertEquals( "Test", out.toString() ); + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript( + "System.out.print(\"Test\"); return true;", null, null, new PrintStream(out))); + assertEquals("Test", out.toString()); } - public void testEvaluateScriptVars() - throws Exception - { - Map vars = new HashMap(); - vars.put( "testVar", "data" ); + @Test + public void testEvaluateScriptVars() throws Exception { + Map vars = new HashMap<>(); + vars.put("testVar", "data"); ByteArrayOutputStream out = new ByteArrayOutputStream(); ScriptInterpreter interpreter = new BeanShellScriptInterpreter(); - assertEquals( Boolean.TRUE, interpreter.evaluateScript( "System.out.print(testVar); return true;", null, - vars, new PrintStream( out ) ) ); - assertEquals( "data", out.toString() ); + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript( + "System.out.print(testVar); return true;", null, vars, new PrintStream(out))); + assertEquals("data", out.toString()); } - } diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java new file mode 100644 index 0000000..2011437 --- /dev/null +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java @@ -0,0 +1,118 @@ +/* + * 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. + */ +package org.apache.maven.shared.scriptinterpreter; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * FileLoggerTest + */ +public class FileLoggerTest { + + public static final String EXPECTED_LOG = "Test1" + System.lineSeparator() + "Test2" + System.lineSeparator(); + + @Test + public void nullOutputFileNoMirror() throws IOException { + try (FileLogger fileLogger = new FileLogger(null)) { + fileLogger.consumeLine("Test1"); + fileLogger.getPrintStream().println("Test2"); + fileLogger.getPrintStream().flush(); + + assertNull(fileLogger.getOutputFile()); + } + } + + @Test + public void nullOutputFileWithMirror() throws IOException { + TestMirrorHandler mirrorHandler = new TestMirrorHandler(); + + try (FileLogger fileLogger = new FileLogger(null, mirrorHandler)) { + fileLogger.consumeLine("Test1"); + fileLogger.getPrintStream().println("Test2"); + fileLogger.getPrintStream().flush(); + + assertNull(fileLogger.getOutputFile()); + } + + assertEquals(EXPECTED_LOG, mirrorHandler.getLoggedMessage()); + } + + @Test + public void nullOutputFileWithMirrorWriteByte() throws IOException { + TestMirrorHandler mirrorHandler = new TestMirrorHandler(); + + try (FileLogger fileLogger = new FileLogger(null, mirrorHandler)) { + fileLogger.getPrintStream().write('A'); + fileLogger.getPrintStream().flush(); + + assertNull(fileLogger.getOutputFile()); + } + + assertEquals("A" + System.lineSeparator(), mirrorHandler.getLoggedMessage()); + } + + @Test + public void outputFileNoMirror() throws IOException { + File outputFile = new File("target/test.log"); + if (outputFile.exists()) { + outputFile.delete(); + } + + try (FileLogger fileLogger = new FileLogger(outputFile)) { + fileLogger.consumeLine("Test1"); + fileLogger.getPrintStream().println("Test2"); + fileLogger.getPrintStream().flush(); + + assertEquals(outputFile, fileLogger.getOutputFile()); + } + + assertTrue(outputFile.exists()); + assertEquals(EXPECTED_LOG, new String(Files.readAllBytes(outputFile.toPath()))); + } + + @Test + public void outputFileWithMirror() throws IOException { + File outputFile = new File("target/test.log"); + if (outputFile.exists()) { + outputFile.delete(); + } + TestMirrorHandler mirrorHandler = new TestMirrorHandler(); + + try (FileLogger fileLogger = new FileLogger(outputFile, mirrorHandler)) { + fileLogger.consumeLine("Test1"); + fileLogger.getPrintStream().println("Test2"); + fileLogger.getPrintStream().flush(); + + assertEquals(outputFile, fileLogger.getOutputFile()); + } + + assertEquals(EXPECTED_LOG, mirrorHandler.getLoggedMessage()); + + assertTrue(outputFile.exists()); + assertEquals(EXPECTED_LOG, new String(Files.readAllBytes(outputFile.toPath()))); + } +} diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java index 9ddfcb3..87f2b30 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java @@ -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 @@ * "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 @@ -18,44 +16,83 @@ * specific language governing permissions and limitations * under the License. */ - -import junit.framework.TestCase; +package org.apache.maven.shared.scriptinterpreter; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.PrintStream; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + /** * Tests the Groovy interpreter facade. - * + * * @author Benjamin Bentmann - * @version $Id: GroovyScriptInterpreterTest.java 1194925 2011-10-29 16:34:29Z olamy $ */ -public class GroovyScriptInterpreterTest - extends TestCase -{ +public class GroovyScriptInterpreterTest { + @Test + public void testEvaluateScript() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ScriptInterpreter interpreter = new GroovyScriptInterpreter(); + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript("print \"Test\"\nreturn true", null, null, new PrintStream(out))); + assertEquals("Test", out.toString()); + } - public void testEvaluateScript() - throws Exception - { + @Test + public void testEvaluateScriptWithDefaultClassPath() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ScriptInterpreter interpreter = new GroovyScriptInterpreter(); - assertEquals( Boolean.TRUE, interpreter.evaluateScript( "print \"Test\"\nreturn true", null, null, - new PrintStream( out ) ) ); - assertEquals( "Test", out.toString() ); + + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript( + "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true", + null, + null, + new PrintStream(out))); + + String testClassPath = + new File("target/test-classes/class-path.txt").toURI().getPath(); + assertEquals(testClassPath, out.toString()); } - public void testEvaluateScriptVars() - throws Exception - { - Map vars = new HashMap(); - vars.put( "testVar", "data" ); + @Test + public void testEvaluateScriptWithCustomClassPath() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ScriptInterpreter interpreter = new GroovyScriptInterpreter(); - assertEquals( Boolean.TRUE, interpreter.evaluateScript( "print testVar\nreturn true", null, vars, - new PrintStream( out ) ) ); - assertEquals( "data", out.toString() ); + + List classPath = Collections.singletonList(new File("src/test-class-path").getAbsolutePath()); + + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript( + "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true", + classPath, + null, + new PrintStream(out))); + + String testClassPath = + new File("src/test-class-path/class-path.txt").toURI().getPath(); + assertEquals(testClassPath, out.toString()); } + @Test + public void testEvaluateScriptVars() throws Exception { + Map vars = new HashMap<>(); + vars.put("testVar", "data"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ScriptInterpreter interpreter = new GroovyScriptInterpreter(); + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript("print testVar\nreturn true", null, vars, new PrintStream(out))); + assertEquals("data", out.toString()); + } } 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 09166d7..b0d64b9 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java @@ -1,4 +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 @@ -17,114 +16,257 @@ * specific language governing permissions and limitations * under the License. */ - -import junit.framework.TestCase; -import org.apache.maven.plugin.logging.SystemStreamLog; -import org.apache.maven.shared.utils.io.FileUtils; +package org.apache.maven.shared.scriptinterpreter; import java.io.File; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + /** * @author Olivier Lamy */ -public class ScriptRunnerTest - extends TestCase -{ - public void testBeanshell() - throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { +public class ScriptRunnerTest { + + @Test + public void testBeanshell() 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/bsh-test"), "verify", buildContext(), fileLogger); + } + + String logContent = new String(Files.readAllBytes(logFile.toPath())); + assertTrue(logContent.contains(new File("src/test/resources/bsh-test/verify.bsh").getPath())); + assertTrue(logContent.contains("foo=bar")); + assertTrue(logContent.contains("globalVar=Yeah baby it's rocks")); + + assertEquals(logContent, mirrorHandler.getLoggedMessage()); + } + + @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 = new String(Files.readAllBytes(logFile.toPath())); + 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"); + if (logFile.exists()) { logFile.delete(); } - SystemStreamLog systemStreamLog = new SystemStreamLog(); - ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); - scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" ); - scriptRunner.run( "test", new File( "src/test/resources/bsh-test" ), "verify", buildContext(), - new FileLogger( logFile ), "foo", true ); + TestMirrorHandler mirrorHandler = new TestMirrorHandler(); + + Exception catchedException = null; - String logContent = FileUtils.fileRead( logFile ); - assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/verify.bsh" ).getPath() ) ); - assertTrue( logContent.contains( "foo=bar" ) ); - assertTrue( logContent.contains( "globalVar=Yeah baby it's rocks")); + try (FileLogger fileLogger = new FileLogger(logFile, mirrorHandler)) { + ScriptRunner scriptRunner = new ScriptRunner(); + scriptRunner.run("test", new File("src/test/resources/bsh-test"), "failed", buildContext(), fileLogger); + } catch (ScriptEvaluationException e) { + catchedException = e; + } + assertNotNull(catchedException); + String logContent = new String(Files.readAllBytes(logFile.toPath())); + assertTrue(logContent.contains(new File("src/test/resources/bsh-test/failed.bsh").getPath())); + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } - public void testBeanshellWithFile() - throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + @Test + public void beanshellReturnedNotTrueShouldThrowException() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { logFile.delete(); } - SystemStreamLog systemStreamLog = new SystemStreamLog(); - ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); - scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" ); - scriptRunner.run( "test", new File( "src/test/resources/bsh-test/verify.bsh" ), buildContext(), - new FileLogger( logFile ), "foo", true ); + TestMirrorHandler mirrorHandler = new TestMirrorHandler(); - String logContent = FileUtils.fileRead( logFile ); - assertTrue( logContent.contains( new File( "src/test/resources/bsh-test/verify.bsh" ).getPath() ) ); - assertTrue( logContent.contains( "foo=bar" ) ); + ScriptReturnException catchedException = null; + try (FileLogger fileLogger = new FileLogger(logFile, mirrorHandler)) { + ScriptRunner scriptRunner = new ScriptRunner(); + scriptRunner.run("test", new File("src/test/resources/bsh-test"), "return-not-true", null, fileLogger); + } catch (ScriptReturnException e) { + catchedException = e; + } + assertEquals("Not true value", catchedException.getResult()); + assertEquals("The test returned Not true value.", catchedException.getMessage()); + String logContent = new String(Files.readAllBytes(logFile.toPath())); + assertTrue(logContent.contains(new File("src/test/resources/bsh-test/return-not-true.bsh").getPath())); + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } - public void testGroovy() - throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + @Test + public void testBeanshellWithFile() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { logFile.delete(); } - SystemStreamLog systemStreamLog = new SystemStreamLog(); - ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); - scriptRunner.setGlobalVariable( "globalVar", "Yeah baby it's rocks" ); - scriptRunner.run( "test", new File( "src/test/resources/groovy-test" ), "verify", buildContext(), - new FileLogger( logFile ), "foo", true ); + 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/bsh-test/verify.bsh"), buildContext(), fileLogger); + } - String logContent = FileUtils.fileRead( logFile ); - assertTrue( logContent.contains( new File( "src/test/resources/groovy-test/verify.groovy" ).getPath() ) ); - assertTrue( logContent.contains( "foo=bar" ) ); - assertTrue( logContent.contains( "globalVar=Yeah baby it's rocks")); + String logContent = new String(Files.readAllBytes(logFile.toPath())); + assertTrue(logContent.contains(new File("src/test/resources/bsh-test/verify.bsh").getPath())); + assertTrue(logContent.contains("foo=bar")); + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } - public void testGroovyWithFile() - throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + @Test + public void testGroovy() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { logFile.delete(); } - SystemStreamLog systemStreamLog = new SystemStreamLog(); - ScriptRunner scriptRunner = new ScriptRunner( systemStreamLog ); - scriptRunner.run( "test", new File( "src/test/resources/groovy-test/verify.groovy" ), buildContext(), - new FileLogger( logFile ), "foo", true ); + TestMirrorHandler mirrorHandler = new TestMirrorHandler(); - String logContent = FileUtils.fileRead( logFile ); - assertTrue( logContent.contains( new File( "src/test/resources/groovy-test/verify.groovy" ).getPath() ) ); - assertTrue( logContent.contains( "foo=bar" ) ); + 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"), "verify", buildContext(), fileLogger); + } + String logContent = new String(Files.readAllBytes(logFile.toPath())); + assertTrue(logContent.contains(new File("src/test/resources/groovy-test/verify.groovy").getPath())); + assertTrue(logContent.contains("foo=bar")); + assertTrue(logContent.contains("globalVar=Yeah baby it's rocks")); + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } + @Test + public void groovyReturnedNullShouldBeOk() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { + logFile.delete(); + } - private Map buildContext() - { - Map context = new HashMap(); - context.put( "foo", "bar" ); - return context; + 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 = new String(Files.readAllBytes(logFile.toPath())); + 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"); + 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"), "failed", buildContext(), fileLogger); + } catch (ScriptEvaluationException e) { + catchedException = e; + } + + assertNotNull(catchedException); + String logContent = new String(Files.readAllBytes(logFile.toPath())); + 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(); + + ScriptReturnException 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 (ScriptReturnException e) { + catchedException = e; + } + + assertEquals(false, catchedException.getResult()); + assertEquals("The test returned false.", catchedException.getMessage()); + String logContent = new String(Files.readAllBytes(logFile.toPath())); + 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"); + 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/groovy-test/verify.groovy"), buildContext(), fileLogger); + } + + String logContent = new String(Files.readAllBytes(logFile.toPath())); + assertTrue(logContent.contains(new File("src/test/resources/groovy-test/verify.groovy").getPath())); + assertTrue(logContent.contains("foo=bar")); + + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } + private Map buildContext() { + Map context = new HashMap<>(); + context.put("foo", "bar"); + return context; + } } diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/TestMirrorHandler.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/TestMirrorHandler.java new file mode 100644 index 0000000..d42ee92 --- /dev/null +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/TestMirrorHandler.java @@ -0,0 +1,41 @@ +/* + * 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. + */ +package org.apache.maven.shared.scriptinterpreter; + +/** + * Implementing {@link FileLoggerMirrorHandler} for testing + */ +class TestMirrorHandler implements FileLoggerMirrorHandler { + + private StringBuilder loggedMessage; + + public TestMirrorHandler() { + loggedMessage = new StringBuilder(); + } + + public String getLoggedMessage() { + return loggedMessage.toString(); + } + + @Override + public void consumeOutput(String message) { + loggedMessage.append(message); + loggedMessage.append(System.lineSeparator()); + } +} diff --git a/src/test/resources/bsh-test/failed.bsh b/src/test/resources/bsh-test/failed.bsh new file mode 100644 index 0000000..f8226bf --- /dev/null +++ b/src/test/resources/bsh-test/failed.bsh @@ -0,0 +1,20 @@ +/* + * 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. + */ + +throw new Exception( "test exception" ) diff --git a/src/test/resources/bsh-test/return-not-true.bsh b/src/test/resources/bsh-test/return-not-true.bsh new file mode 100644 index 0000000..1ddb06a --- /dev/null +++ b/src/test/resources/bsh-test/return-not-true.bsh @@ -0,0 +1,20 @@ +/* + * 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. + */ + +return "Not true value" diff --git a/src/test/resources/bsh-test/return-null.bsh b/src/test/resources/bsh-test/return-null.bsh new file mode 100644 index 0000000..7f1f85a --- /dev/null +++ b/src/test/resources/bsh-test/return-null.bsh @@ -0,0 +1,20 @@ +/* + * 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. + */ + +System.out.print("ok with null result"); diff --git a/src/test/resources/class-path.txt b/src/test/resources/class-path.txt new file mode 100644 index 0000000..042f3ce --- /dev/null +++ b/src/test/resources/class-path.txt @@ -0,0 +1,18 @@ +/* + * 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. + */ diff --git a/src/test/resources/groovy-test/failed.groovy b/src/test/resources/groovy-test/failed.groovy new file mode 100644 index 0000000..b1881f1 --- /dev/null +++ b/src/test/resources/groovy-test/failed.groovy @@ -0,0 +1,25 @@ +/* + * 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. + */ + + +def message = 'no Te Message' + +assert message.contains('Test Message') + +return true diff --git a/src/test/resources/groovy-test/return-false.groovy b/src/test/resources/groovy-test/return-false.groovy new file mode 100644 index 0000000..e9e4898 --- /dev/null +++ b/src/test/resources/groovy-test/return-false.groovy @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +return false diff --git a/src/test/resources/groovy-test/return-null.groovy b/src/test/resources/groovy-test/return-null.groovy new file mode 100644 index 0000000..9b54311 --- /dev/null +++ b/src/test/resources/groovy-test/return-null.groovy @@ -0,0 +1,20 @@ +/* + * 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. + */ + +println 'ok with null result' diff --git a/src/test/resources/groovy-test/verify.groovy b/src/test/resources/groovy-test/verify.groovy index f322050..94f6d45 100644 --- a/src/test/resources/groovy-test/verify.groovy +++ b/src/test/resources/groovy-test/verify.groovy @@ -22,7 +22,8 @@ if ( !( basedir instanceof File ) ) println "Global script variable not defined: basedir or not a File" throw new RuntimeException("Global script variable not defined: basedir or not a File"); } -assert (new File( basedir, "verify.groovy" ).exists()) +def verify = new File( basedir, "verify.groovy" ) +assert (verify.exists()) if ( !( context instanceof Map ) ) {