This is an automated email from the ASF dual-hosted git repository. olamy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git
The following commit(s) were added to refs/heads/master by this push: new 98477ff Bump maven-shared-components from 37 to 38 (#83) 98477ff is described below commit 98477ff454573c658792d804f5eb200c53cc9b32 Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> AuthorDate: Fri Dec 2 08:05:35 2022 +1000 Bump maven-shared-components from 37 to 38 (#83) * Bump maven-shared-components from 37 to 38 Bumps [maven-shared-components](https://github.com/apache/maven-parent) from 37 to 38. - [Release notes](https://github.com/apache/maven-parent/releases) - [Commits](https://github.com/apache/maven-parent/commits) --- updated-dependencies: - dependency-name: org.apache.maven.shared:maven-shared-components dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <supp...@github.com> * fix style Signed-off-by: Olivier Lamy <ol...@apache.org> Signed-off-by: dependabot[bot] <supp...@github.com> Signed-off-by: Olivier Lamy <ol...@apache.org> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Olivier Lamy <ol...@apache.org> --- pom.xml | 15 +- .../BeanShellScriptInterpreter.java | 129 +++++---- .../shared/scriptinterpreter/ExecutionLogger.java | 23 +- .../maven/shared/scriptinterpreter/FileLogger.java | 110 ++++---- .../scriptinterpreter/FileLoggerMirrorHandler.java | 23 +- .../scriptinterpreter/GroovyScriptInterpreter.java | 103 ++++---- .../ScriptEvaluationException.java | 34 ++- .../shared/scriptinterpreter/ScriptException.java | 37 ++- .../scriptinterpreter/ScriptInterpreter.java | 31 ++- .../scriptinterpreter/ScriptReturnException.java | 29 +- .../shared/scriptinterpreter/ScriptRunner.java | 217 ++++++++------- .../BeanShellScriptInterpreterTest.java | 57 ++-- .../shared/scriptinterpreter/FileLoggerTest.java | 112 ++++---- .../GroovyScriptInterpreterTest.java | 91 ++++--- .../shared/scriptinterpreter/ScriptRunnerTest.java | 292 +++++++++------------ .../scriptinterpreter/TestMirrorHandler.java | 36 ++- 16 files changed, 713 insertions(+), 626 deletions(-) diff --git a/pom.xml b/pom.xml index 6ac9748..00aad76 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,4 @@ -<?xml version='1.0' encoding='UTF-8'?> +<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file @@ -22,8 +22,8 @@ <parent> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-shared-components</artifactId> - <version>37</version> - <relativePath /> + <version>38</version> + <relativePath/> </parent> <artifactId>maven-script-interpreter</artifactId> @@ -35,8 +35,8 @@ <scm> <connection>scm:git:https://gitbox.apache.org/repos/asf/maven-script-interpreter.git</connection> <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/maven-script-interpreter.git</developerConnection> - <url>https://github.com/apache/maven-script-interpreter/tree/${project.scm.tag}</url> <tag>HEAD</tag> + <url>https://github.com/apache/maven-script-interpreter/tree/${project.scm.tag}</url> </scm> <issueManagement> <system>jira</system> @@ -44,7 +44,7 @@ </issueManagement> <ciManagement> <system>Jenkins</system> - <url>https://ci-builds.apache.org/job/Maven/job/maven-box/job/maven-script-interpreter/</url> + <url>https://ci-maven.apache.org/job/Maven/job/maven-box/job/maven-script-interpreter/</url> </ciManagement> <distributionManagement> <site> @@ -52,13 +52,10 @@ <url>scm:svn:https://svn.apache.org/repos/asf/maven/website/components/${maven.site.path}</url> </site> </distributionManagement> - + <properties> - <javaVersion>8</javaVersion> <project.build.outputTimestamp>2020-10-15T00:00:05Z</project.build.outputTimestamp> <slf4j.version>1.7.36</slf4j.version> - <surefire.version>3.0.0-M6</surefire.version> - <checkstyle.violation.ignore>None</checkstyle.violation.ignore> </properties> <dependencyManagement> 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 e35c99f..1a17246 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -23,7 +41,6 @@ import bsh.Capabilities; import bsh.EvalError; import bsh.Interpreter; import bsh.TargetError; - import java.io.File; import java.io.IOException; import java.io.PrintStream; @@ -35,98 +52,72 @@ import java.util.Map; * * @author Benjamin Bentmann */ -class BeanShellScriptInterpreter - implements ScriptInterpreter -{ +class BeanShellScriptInterpreter implements ScriptInterpreter { /** {@inheritDoc} */ @Override - public Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables, - PrintStream scriptOutput ) - throws ScriptEvaluationException - { + public Object evaluateScript( + String script, + List<String> classPath, + Map<String, ? extends Object> 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 ( Map.Entry<String, ?> entry : globalVariables.entrySet() ) - { - try - { - engine.set( entry.getKey(), entry.getValue() ); - } - catch ( EvalError e ) - { - throw new RuntimeException( e ); + if (globalVariables != null) { + for (Map.Entry<String, ?> 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 611ef91..8ee0799 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ExecutionLogger.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ExecutionLogger.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -26,8 +44,7 @@ import java.io.PrintStream; * * @author Olivier Lamy */ -public interface ExecutionLogger -{ +public interface ExecutionLogger { /** * The stream which will catch the output of the {@link org.apache.maven.shared.scriptinterpreter.ScriptRunner}. * @@ -40,5 +57,5 @@ public interface ExecutionLogger * * @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 ee5f851..aeb9d8b 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -29,8 +47,7 @@ import java.io.PrintStream; * <p>FileLogger class.</p> * */ -public class FileLogger implements ExecutionLogger, AutoCloseable -{ +public class FileLogger implements ExecutionLogger, AutoCloseable { /** * The path to the log file. @@ -48,9 +65,8 @@ public class FileLogger implements ExecutionLogger, AutoCloseable * @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); } /** @@ -60,29 +76,22 @@ public class FileLogger implements ExecutionLogger, AutoCloseable * @param mirrorHandler The class which handle mirrored message, can be <code>null</code>. * @throws java.io.IOException If the output file could not be created. */ - public FileLogger( File outputFile, FileLoggerMirrorHandler mirrorHandler ) throws IOException - { + public FileLogger(File outputFile, FileLoggerMirrorHandler mirrorHandler) throws IOException { this.file = outputFile; OutputStream outputStream; - if ( outputFile != null ) - { + if (outputFile != null) { outputFile.getParentFile().mkdirs(); - outputStream = new FileOutputStream( outputFile ); - } - else - { + outputStream = new FileOutputStream(outputFile); + } else { outputStream = new NullOutputStream(); } - if ( mirrorHandler != null ) - { - stream = new PrintStream( new MirrorStreamWrapper( outputStream, mirrorHandler ) ); - } - else - { - stream = new PrintStream( outputStream ); + if (mirrorHandler != null) { + stream = new PrintStream(new MirrorStreamWrapper(outputStream, mirrorHandler)); + } else { + stream = new PrintStream(outputStream); } } @@ -91,8 +100,7 @@ public class FileLogger implements ExecutionLogger, AutoCloseable * * @return The path to the output file, never <code>null</code>. */ - public File getOutputFile() - { + public File getOutputFile() { return file; } @@ -102,8 +110,7 @@ public class FileLogger implements ExecutionLogger, AutoCloseable * @return The underlying stream used to write message to the log file, never <code>null</code>. */ @Override - public PrintStream getPrintStream() - { + public PrintStream getPrintStream() { return stream; } @@ -114,90 +121,77 @@ public class FileLogger implements ExecutionLogger, AutoCloseable * @param line The message to log. */ @Override - public void consumeLine( String line ) - { - stream.println( line ); + public void consumeLine(String line) { + stream.println(line); stream.flush(); } /** * 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 static class MirrorStreamWrapper extends OutputStream { private final OutputStream out; private final FileLoggerMirrorHandler mirrorHandler; private StringBuilder lineBuffer; - MirrorStreamWrapper( OutputStream outputStream, FileLoggerMirrorHandler mirrorHandler ) - { + 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 ) ); + 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 ) ); + 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 - { + public void flush() throws IOException { out.flush(); int len = lineBuffer.length(); - if ( len == 0 ) - { + 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' ) ) - { + while (len > 0 && (lineBuffer.charAt(len - 1) == '\n' || lineBuffer.charAt(len - 1) == '\r')) { len--; } - lineBuffer.setLength( len ); + lineBuffer.setLength(len); - mirrorHandler.consumeOutput( lineBuffer.toString() ); + mirrorHandler.consumeOutput(lineBuffer.toString()); // clear buffer lineBuffer = new StringBuilder(); } } - private static class NullOutputStream extends OutputStream - { + private static class NullOutputStream extends OutputStream { @Override - public void write( int b ) - { + public void write(int b) { // do nothing } @Override - public void write( byte[] b, int off, int len ) - { + 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 index 25ef0d5..b6c4b93 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLoggerMirrorHandler.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLoggerMirrorHandler.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -24,13 +42,12 @@ package org.apache.maven.shared.scriptinterpreter; * * @since 1.3 */ -public interface FileLoggerMirrorHandler -{ +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 ); + 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 53c4aba..5b2a8a9 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -21,82 +39,67 @@ package org.apache.maven.shared.scriptinterpreter; import groovy.lang.Binding; import groovy.lang.GroovyShell; -import org.codehaus.groovy.control.CompilerConfiguration; -import org.codehaus.groovy.runtime.powerassert.PowerAssertionError; -import org.codehaus.groovy.tools.RootLoader; - import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.net.URL; import java.util.List; import java.util.Map; +import org.codehaus.groovy.control.CompilerConfiguration; +import org.codehaus.groovy.runtime.powerassert.PowerAssertionError; +import org.codehaus.groovy.tools.RootLoader; /** * Provides a facade to evaluate Groovy scripts. - * + * * @author Benjamin Bentmann */ -class GroovyScriptInterpreter - implements ScriptInterpreter -{ +class GroovyScriptInterpreter implements ScriptInterpreter { /** {@inheritDoc} */ @Override - public Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables, - PrintStream scriptOutput ) - throws ScriptEvaluationException - { + public Object evaluateScript( + String script, + List<String> classPath, + Map<String, ? extends Object> globalVariables, + PrintStream scriptOutput) + throws ScriptEvaluationException { PrintStream origOut = System.out; PrintStream origErr = System.err; - try ( RootLoader childFirstLoader = new RootLoader( new URL[] {}, getClass().getClassLoader() ) ) - { + try (RootLoader childFirstLoader = + new RootLoader(new URL[] {}, getClass().getClassLoader())) { - if ( scriptOutput != null ) - { - System.setErr( scriptOutput ); - System.setOut( scriptOutput ); + if (scriptOutput != null) { + System.setErr(scriptOutput); + System.setOut(scriptOutput); } - if ( classPath != null && !classPath.isEmpty() ) - { - for ( String path : classPath ) - { - childFirstLoader.addURL( new File( path ).toURI().toURL() ); + if (classPath != null && !classPath.isEmpty()) { + for (String path : classPath) { + childFirstLoader.addURL(new File(path).toURI().toURL()); } } - GroovyShell interpreter = new GroovyShell( childFirstLoader, - new Binding( globalVariables ), - new CompilerConfiguration( CompilerConfiguration.DEFAULT ) ); + GroovyShell interpreter = new GroovyShell( + childFirstLoader, + new Binding(globalVariables), + new CompilerConfiguration(CompilerConfiguration.DEFAULT)); - try - { - return interpreter.evaluate( script ); - } - catch ( ThreadDeath e ) - { + try { + return interpreter.evaluate(script); + } catch (ThreadDeath e) { throw e; + } catch (PowerAssertionError e) { + throw new ScriptEvaluationException("Assertion Error", e); + } catch (Throwable e) { + throw new ScriptEvaluationException(e); } - catch ( PowerAssertionError e ) - { - throw new ScriptEvaluationException( "Assertion Error", e ); - } - catch ( Throwable e ) - { - throw new ScriptEvaluationException( e ); - } - } - catch ( IOException e ) - { - throw new ScriptEvaluationException( e ); - } - finally - { - System.setErr( origErr ); - System.setOut( origOut ); + } catch (IOException e) { + throw new ScriptEvaluationException(e); + } finally { + System.setErr(origErr); + System.setOut(origOut); } } - } 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 cf22d79..291cc3a 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -25,9 +43,7 @@ package org.apache.maven.shared.scriptinterpreter; * * @author Benjamin Bentmann */ -public class ScriptEvaluationException - extends ScriptException -{ +public class ScriptEvaluationException extends ScriptException { /** * The serial version identifier for this class. @@ -36,12 +52,11 @@ public class ScriptEvaluationException /** * Creates a new exception with the specified cause. - * + * * @param cause The cause, may be <code>null</code>. */ - ScriptEvaluationException( Throwable cause ) - { - super( cause ); + ScriptEvaluationException(Throwable cause) { + super(cause); } /** @@ -51,8 +66,7 @@ public class ScriptEvaluationException * @param cause The cause, may be <code>null</code>. * @since 1.3 */ - public ScriptEvaluationException( String message, Throwable cause ) - { - super( message, cause ); + 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 index 1f24e70..0782a9b 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptException.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -25,9 +43,7 @@ package org.apache.maven.shared.scriptinterpreter; * @author Slawomir Jaranowski * @since 1.3 */ -public class ScriptException - extends Exception -{ +public class ScriptException extends Exception { private static final long serialVersionUID = 4553276474852776472L; /** @@ -36,18 +52,15 @@ public class ScriptException * @param message The message, may be <code>null</code>. * @param cause The cause, may be <code>null</code>. */ - public ScriptException( String message, Throwable cause ) - { - super( message, cause ); + public ScriptException(String message, Throwable cause) { + super(message, cause); } - ScriptException( String message ) - { - super( message ); + ScriptException(String message) { + super(message); } - ScriptException( Throwable cause ) - { - super( cause ); + 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 bde2541..0973cb4 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -29,8 +47,7 @@ import java.util.Map; * * @author Benjamin Bentmann */ -public interface ScriptInterpreter -{ +public interface ScriptInterpreter { /** * Evaluates the specified script. @@ -46,8 +63,10 @@ public interface ScriptInterpreter * @return The return value from the script, can be <code>null</code> * @throws ScriptEvaluationException If the script evaluation produced an error. */ - Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables, - PrintStream scriptOutput ) - throws ScriptEvaluationException; - + Object evaluateScript( + String script, + List<String> classPath, + Map<String, ? extends Object> 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 index 51c416a..7fc8aa9 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptReturnException.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -25,8 +43,7 @@ package org.apache.maven.shared.scriptinterpreter; * @author Slawomir Jaranowski * @since 1.3 */ -public class ScriptReturnException extends ScriptException -{ +public class ScriptReturnException extends ScriptException { private static final long serialVersionUID = -4705573157701206786L; /** @@ -40,9 +57,8 @@ public class ScriptReturnException extends ScriptException * @param message The message, may be <code>null</code>. * @param result The cause, may be <code>null</code>. */ - ScriptReturnException( String message, Object result ) - { - super( message ); + ScriptReturnException(String message, Object result) { + super(message); this.result = result; } @@ -51,8 +67,7 @@ public class ScriptReturnException extends ScriptException * * @return script result. */ - public Object getResult() - { + 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 df52a8b..3810041 100644 --- a/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java +++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptRunner.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -19,10 +37,6 @@ package org.apache.maven.shared.scriptinterpreter; * under the License. */ -import org.apache.commons.io.FilenameUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.io.IOException; import java.io.PrintStream; @@ -33,17 +47,18 @@ import java.util.LinkedHashMap; import java.util.List; 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 */ -public class ScriptRunner -{ +public class ScriptRunner { - - private static final Logger LOG = LoggerFactory.getLogger( ScriptRunner.class ); + 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,11 +84,10 @@ public class ScriptRunner /** * Creates a new script runner with BSH and Groovy interpreters. */ - public ScriptRunner() - { + public ScriptRunner() { scriptInterpreters = new LinkedHashMap<>(); - scriptInterpreters.put( "bsh", new BeanShellScriptInterpreter() ); - scriptInterpreters.put( "groovy", new GroovyScriptInterpreter() ); + scriptInterpreters.put("bsh", new BeanShellScriptInterpreter()); + scriptInterpreters.put("groovy", new GroovyScriptInterpreter()); globalVariables = new HashMap<>(); classPath = new ArrayList<>(); } @@ -84,9 +98,8 @@ public class ScriptRunner * @param id The Id of interpreter * @param scriptInterpreter the Script Interpreter implementation */ - public void addScriptInterpreter( String id, ScriptInterpreter scriptInterpreter ) - { - scriptInterpreters.put( id, scriptInterpreter ); + public void addScriptInterpreter(String id, ScriptInterpreter scriptInterpreter) { + scriptInterpreters.put(id, scriptInterpreter); } /** @@ -95,9 +108,8 @@ public class ScriptRunner * @param name The name of the variable, must not be <code>null</code>. * @param value The value of the variable, may be <code>null</code>. */ - public void setGlobalVariable( String name, Object value ) - { - this.globalVariables.put( name, value ); + public void setGlobalVariable(String name, Object value) { + this.globalVariables.put(name, value); } /** @@ -108,9 +120,8 @@ public class ScriptRunner * 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<String> classPath ) - { - this.classPath = ( classPath != null ) ? new ArrayList<>( classPath ) : new ArrayList<>(); + public void setClassPath(List<String> classPath) { + this.classPath = (classPath != null) ? new ArrayList<>(classPath) : new ArrayList<>(); } /** @@ -119,9 +130,8 @@ public class ScriptRunner * @param encoding The file encoding of the hook scripts, may be <code>null</code> or empty to use the platform's * default encoding. */ - public void setScriptEncoding( String encoding ) - { - this.encoding = ( encoding != null && encoding.length() > 0 ) ? encoding : null; + public void setScriptEncoding(String encoding) { + this.encoding = (encoding != null && encoding.length() > 0) ? encoding : null; } /** @@ -136,29 +146,36 @@ public class ScriptRunner * @throws IOException If an I/O error occurred while reading the script file. * @throws ScriptException If the script did not return <code>true</code> of threw an exception. */ - public void run( final String scriptDescription, final File basedir, final String relativeScriptPath, - final Map<String, ?> context, final ExecutionLogger logger ) - throws IOException, ScriptException - { - if ( relativeScriptPath == null ) - { - LOG.debug( "{}: relativeScriptPath is null, not executing script", scriptDescription ); + public void run( + final String scriptDescription, + final File basedir, + final String relativeScriptPath, + final Map<String, ?> 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() ) - { - LOG.debug( "{} : no script '{}' found in directory {}", scriptDescription, relativeScriptPath, - basedir.getAbsolutePath() ); + if (!scriptFile.exists()) { + LOG.debug( + "{} : no script '{}' found in directory {}", + scriptDescription, + relativeScriptPath, + basedir.getAbsolutePath()); return; } - LOG.info( "run {} {}.{}", - scriptDescription, relativeScriptPath, FilenameUtils.getExtension( scriptFile.getAbsolutePath() ) ); + LOG.info( + "run {} {}.{}", + scriptDescription, + relativeScriptPath, + FilenameUtils.getExtension(scriptFile.getAbsolutePath())); - executeRun( scriptDescription, scriptFile, context, logger ); + executeRun(scriptDescription, scriptFile, context, logger); } /** @@ -171,87 +188,70 @@ public class ScriptRunner * @throws IOException If an I/O error occurred while reading the script file. * @throws ScriptException If the script did not return <code>true</code> of threw an exception. */ - public void run( final String scriptDescription, File scriptFile, final Map<String, ?> context, - final ExecutionLogger logger ) - throws IOException, ScriptException - { + public void run( + final String scriptDescription, File scriptFile, final Map<String, ?> context, final ExecutionLogger logger) + throws IOException, ScriptException { - if ( !scriptFile.exists() ) - { - LOG.debug( "{} : script file not found in directory {}", scriptDescription, scriptFile.getAbsolutePath() ); + if (!scriptFile.exists()) { + LOG.debug("{} : script file not found in directory {}", scriptDescription, scriptFile.getAbsolutePath()); return; } - LOG.info( "run {} {}", scriptDescription, scriptFile.getAbsolutePath() ); + LOG.info("run {} {}", scriptDescription, scriptFile.getAbsolutePath()); - executeRun( scriptDescription, scriptFile, context, logger ); + executeRun(scriptDescription, scriptFile, context, logger); } - private void executeRun( final String scriptDescription, File scriptFile, - final Map<String, ?> context, final ExecutionLogger logger ) - throws IOException, ScriptException - { - ScriptInterpreter interpreter = getInterpreter( scriptFile ); - if ( LOG.isDebugEnabled() ) - { + private void executeRun( + final String scriptDescription, File scriptFile, final Map<String, ?> 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 ); - LOG.debug( "Running script with {} :{}", name, scriptFile ); + name = name.substring(name.lastIndexOf('.') + 1); + LOG.debug("Running script with {} :{}", name, scriptFile); } String script; - try - { - byte[] bytes = Files.readAllBytes( scriptFile.toPath() ); - if ( encoding != null ) - { - script = new String( bytes, encoding ); - } - else - { - script = new String( bytes ); + try { + byte[] bytes = Files.readAllBytes(scriptFile.toPath()); + if (encoding != null) { + script = new String(bytes, encoding); + } else { + script = new String(bytes); } - } - catch ( IOException e ) - { + } catch (IOException e) { String errorMessage = - "error reading " + scriptDescription + " " + scriptFile.getPath() + ", " + e.getMessage(); - throw new IOException( errorMessage, e ); + "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; - Map<String, Object> scriptVariables = new HashMap<>( this.globalVariables ); - scriptVariables.put( "basedir", scriptFile.getParentFile() ); - scriptVariables.put( "context", context ); + Map<String, Object> scriptVariables = new HashMap<>(this.globalVariables); + scriptVariables.put("basedir", scriptFile.getParentFile()); + scriptVariables.put("context", context); - result = interpreter.evaluateScript( script, classPath, scriptVariables, out ); - if ( logger != null ) - { - logger.consumeLine( "Finished " + scriptDescription + ": " + scriptFile ); + result = interpreter.evaluateScript(script, classPath, scriptVariables, out); + if (logger != null) { + logger.consumeLine("Finished " + scriptDescription + ": " + scriptFile); } - } - catch ( ScriptEvaluationException e ) - { - Throwable t = ( e.getCause() != null ) ? e.getCause() : e; - if ( logger != null ) - { - t.printStackTrace( logger.getPrintStream() ); + } catch (ScriptEvaluationException e) { + Throwable t = (e.getCause() != null) ? e.getCause() : e; + if (logger != null) { + t.printStackTrace(logger.getPrintStream()); } throw e; } - if ( !( result == null || Boolean.parseBoolean( String.valueOf( result ) ) ) ) - { - throw new ScriptReturnException( "The " + scriptDescription + " returned " + result + ".", result ); + if (!(result == null || Boolean.parseBoolean(String.valueOf(result)))) { + throw new ScriptReturnException("The " + scriptDescription + " returned " + result + ".", result); } } @@ -262,15 +262,11 @@ public class ScriptRunner * @param scriptFile The script file to resolve, may be <code>null</code>. * @return The effective path to the script file or <code>null</code> if the input was <code>null</code>. */ - 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; } @@ -287,15 +283,12 @@ public class ScriptRunner * @param scriptFile The script file for which to determine an interpreter, must not be <code>null</code>. * @return The script interpreter for the file, never <code>null</code>. */ - 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" ); + 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/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java index 4477b28..45ba6fa 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreterTest.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -19,46 +37,41 @@ package org.apache.maven.shared.scriptinterpreter; * under the License. */ +import static org.junit.jupiter.api.Assertions.assertEquals; + 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 */ -public class BeanShellScriptInterpreterTest -{ +public class BeanShellScriptInterpreterTest { @Test - public void testEvaluateScript() - throws Exception - { + 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()); } @Test - public void testEvaluateScriptVars() - throws Exception - { + public void testEvaluateScriptVars() throws Exception { Map<String, Object> vars = new HashMap<>(); - vars.put( "testVar", "data" ); + 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 index 8943456..c57d2b0 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -19,114 +37,100 @@ package org.apache.maven.shared.scriptinterpreter; * under the License. */ -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; import java.io.File; import java.io.IOException; import java.nio.file.Files; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; /** * FileLoggerTest */ -public class 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" ); + public void nullOutputFileNoMirror() throws IOException { + try (FileLogger fileLogger = new FileLogger(null)) { + fileLogger.consumeLine("Test1"); + fileLogger.getPrintStream().println("Test2"); fileLogger.getPrintStream().flush(); - assertNull( fileLogger.getOutputFile() ); + assertNull(fileLogger.getOutputFile()); } } @Test - public void nullOutputFileWithMirror() throws IOException - { + public void nullOutputFileWithMirror() throws IOException { TestMirrorHandler mirrorHandler = new TestMirrorHandler(); - try ( FileLogger fileLogger = new FileLogger( null, mirrorHandler ) ) - { - fileLogger.consumeLine( "Test1" ); - fileLogger.getPrintStream().println( "Test2" ); + try (FileLogger fileLogger = new FileLogger(null, mirrorHandler)) { + fileLogger.consumeLine("Test1"); + fileLogger.getPrintStream().println("Test2"); fileLogger.getPrintStream().flush(); - assertNull( fileLogger.getOutputFile() ); + assertNull(fileLogger.getOutputFile()); } - assertEquals( EXPECTED_LOG, mirrorHandler.getLoggedMessage() ); + assertEquals(EXPECTED_LOG, mirrorHandler.getLoggedMessage()); } @Test - public void nullOutputFileWithMirrorWriteByte() throws IOException - { + public void nullOutputFileWithMirrorWriteByte() throws IOException { TestMirrorHandler mirrorHandler = new TestMirrorHandler(); - try ( FileLogger fileLogger = new FileLogger( null, mirrorHandler ) ) - { - fileLogger.getPrintStream().write( 'A' ); + try (FileLogger fileLogger = new FileLogger(null, mirrorHandler)) { + fileLogger.getPrintStream().write('A'); fileLogger.getPrintStream().flush(); - assertNull( fileLogger.getOutputFile() ); + assertNull(fileLogger.getOutputFile()); } - assertEquals( "A" + System.lineSeparator(), mirrorHandler.getLoggedMessage() ); + assertEquals("A" + System.lineSeparator(), mirrorHandler.getLoggedMessage()); } @Test - public void outputFileNoMirror() throws IOException - { - File outputFile = new File( "target/test.log" ); - if ( outputFile.exists() ) - { + 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" ); + try (FileLogger fileLogger = new FileLogger(outputFile)) { + fileLogger.consumeLine("Test1"); + fileLogger.getPrintStream().println("Test2"); fileLogger.getPrintStream().flush(); - assertEquals( outputFile, fileLogger.getOutputFile() ); + assertEquals(outputFile, fileLogger.getOutputFile()); } - assertTrue( outputFile.exists() ); - assertEquals( EXPECTED_LOG, new String( Files.readAllBytes(outputFile.toPath())) ); + 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() ) - { + 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" ); + try (FileLogger fileLogger = new FileLogger(outputFile, mirrorHandler)) { + fileLogger.consumeLine("Test1"); + fileLogger.getPrintStream().println("Test2"); fileLogger.getPrintStream().flush(); - assertEquals( outputFile, fileLogger.getOutputFile() ); + assertEquals(outputFile, fileLogger.getOutputFile()); } - assertEquals( EXPECTED_LOG, mirrorHandler.getLoggedMessage() ); + assertEquals(EXPECTED_LOG, mirrorHandler.getLoggedMessage()); - assertTrue( outputFile.exists() ); - assertEquals( EXPECTED_LOG, new String( Files.readAllBytes(outputFile.toPath())) ); + 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 0e8d1a3..ae77ee9 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreterTest.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -19,7 +37,7 @@ package org.apache.maven.shared.scriptinterpreter; * under the License. */ -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.ByteArrayOutputStream; import java.io.File; @@ -28,70 +46,71 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; /** * Tests the Groovy interpreter facade. * * @author Benjamin Bentmann */ -public class GroovyScriptInterpreterTest -{ +public class GroovyScriptInterpreterTest { @Test - public void testEvaluateScript() throws Exception - { + 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() ); + assertEquals( + Boolean.TRUE, + interpreter.evaluateScript("print \"Test\"\nreturn true", null, null, new PrintStream(out))); + assertEquals("Test", out.toString()); } @Test - public void testEvaluateScriptWithDefaultClassPath() throws Exception - { + public void testEvaluateScriptWithDefaultClassPath() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ScriptInterpreter interpreter = new GroovyScriptInterpreter(); - assertEquals( Boolean.TRUE, interpreter.evaluateScript( - "print getClass().getResource( \"/class-path.txt\" ).getPath().toURI().getPath()\nreturn true", - null, null, new PrintStream( out ) ) ); + 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() ); + String testClassPath = + new File("target/test-classes/class-path.txt").toURI().getPath(); + assertEquals(testClassPath, out.toString()); } @Test - public void testEvaluateScriptWithCustomClassPath() throws Exception - { + public void testEvaluateScriptWithCustomClassPath() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ScriptInterpreter interpreter = new GroovyScriptInterpreter(); - List<String> classPath = Collections.singletonList( new File( "src/test-class-path" ).getAbsolutePath() ); + List<String> 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 ) ) ); + 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() ); + String testClassPath = + new File("src/test-class-path/class-path.txt").toURI().getPath(); + assertEquals(testClassPath, out.toString()); } @Test - public void testEvaluateScriptVars() throws Exception - { + public void testEvaluateScriptVars() throws Exception { Map<String, Object> vars = new HashMap<>(); - vars.put( "testVar", "data" ); + 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() ); + 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 4e6dc54..b69ceb0 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/ScriptRunnerTest.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -19,80 +37,68 @@ package org.apache.maven.shared.scriptinterpreter; * under the License. */ -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; import java.io.File; import java.nio.file.Files; import java.util.HashMap; import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; /** * @author Olivier Lamy */ -public class ScriptRunnerTest -{ +public class ScriptRunnerTest { @Test - public void testBeanshell() throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + 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 ) ) - { + 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 ); + 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" ) ); + 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() ); + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } @Test - public void beanshellReturnedNullShouldBeOk() throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + 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 ) ) - { + 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 ); + 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() ); + 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() ) - { + public void failedBeanshellShouldCreateProperLogsMessage() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { logFile.delete(); } @@ -100,29 +106,23 @@ public class ScriptRunnerTest Exception catchedException = null; - try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) ) - { + 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 ) - { + 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() ); + 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()); } @Test - public void beanshellReturnedNotTrueShouldThrowException() throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + public void beanshellReturnedNotTrueShouldThrowException() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { logFile.delete(); } @@ -130,111 +130,90 @@ public class ScriptRunnerTest ScriptReturnException catchedException = null; - try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) ) - { + 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 ) - { + 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() ); + 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()); } - @Test - public void testBeanshellWithFile() throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + public void testBeanshellWithFile() 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 ) ) - { + 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 ); + scriptRunner.setGlobalVariable("globalVar", "Yeah baby it's rocks"); + scriptRunner.run("test", new File("src/test/resources/bsh-test/verify.bsh"), 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" ) ); + 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() ); + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } @Test - public void testGroovy() throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + public void testGroovy() 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 ) ) - { + 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 ); + 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" ) ); + 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() ); + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } @Test - public void groovyReturnedNullShouldBeOk() throws Exception - { - File logFile = new File( "target/build.log" ); - if ( logFile.exists() ) - { + public void groovyReturnedNullShouldBeOk() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { logFile.delete(); } TestMirrorHandler mirrorHandler = new TestMirrorHandler(); - try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) ) - { + 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 ); + 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() ); + 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() ) - { + public void failedGroovyShouldCreateProperLogsMessage() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { logFile.delete(); } @@ -242,29 +221,23 @@ public class ScriptRunnerTest Exception catchedException = null; - try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) ) - { + 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 ) - { + 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() ); + 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() ) - { + public void groovyReturnedFalseShouldThrowException() throws Exception { + File logFile = new File("target/build.log"); + if (logFile.exists()) { logFile.delete(); } @@ -272,53 +245,46 @@ public class ScriptRunnerTest ScriptReturnException catchedException = null; - try ( FileLogger fileLogger = new FileLogger( logFile, mirrorHandler ) ) - { + 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 ) - { + 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() ); + 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() ) - { + 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 ) ) - { + 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 ); + 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" ) ); + 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() ); + assertEquals(logContent, mirrorHandler.getLoggedMessage()); } - private Map<String, ?> buildContext() - { + private Map<String, ?> buildContext() { Map<String, Object> context = new HashMap<>(); - context.put( "foo", "bar" ); + 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 index 611f85c..627704a 100644 --- a/src/test/java/org/apache/maven/shared/scriptinterpreter/TestMirrorHandler.java +++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/TestMirrorHandler.java @@ -1,3 +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. + */ package org.apache.maven.shared.scriptinterpreter; /* @@ -19,30 +37,24 @@ package org.apache.maven.shared.scriptinterpreter; * under the License. */ -import java.io.IOException; - /** * Implementing {@link FileLoggerMirrorHandler} for testing */ -class TestMirrorHandler implements FileLoggerMirrorHandler -{ +class TestMirrorHandler implements FileLoggerMirrorHandler { private StringBuilder loggedMessage; - public TestMirrorHandler() - { + public TestMirrorHandler() { loggedMessage = new StringBuilder(); } - public String getLoggedMessage() - { + public String getLoggedMessage() { return loggedMessage.toString(); } @Override - public void consumeOutput( String message ) - { - loggedMessage.append( message ); - loggedMessage.append( System.lineSeparator() ); + public void consumeOutput(String message) { + loggedMessage.append(message); + loggedMessage.append(System.lineSeparator()); } }