From ea7091756bb65ba5667ac8246efba6ac3f04322e Mon Sep 17 00:00:00 2001 From: pyxide Date: Fri, 20 Jan 2017 16:33:35 +0100 Subject: [PATCH] Enhanced JavaxScriptRunner, Junit, docs, Javadoc JavaxScriptRunner: - Better compiled script reference name to avoid reference name collision, project wide : execName + ".compiled." + getScript().hashCode() + "." + getProject().hashCode(); ScriptDef: - Fixed Javadoc + use of 3rd person [(oracle style guideline)](http://www.oracle.com/technetwork/articles/java/index-137868.html#styleguide) Task manual: documentation for `encoding` and `compiled` options. - `encoding` : script.html, scriptdef.html, conditions.html, selectors.html, mapper.html, filterchain.html - `compiled` : scriptdef.html Added source file `encoding` option on: - org.apache.tools.ant.taskdefs.optional.Script - org.apache.tools.ant.types.optional.AbstractScriptComponent (inherited by ScriptCondition, ScriptMapper) - org.apache.tools.ant.types.optional.ScriptFilter - org.apache.tools.ant.types.optional.ScriptSelector Test units : - org.apache.tools.ant.taskdefs.optional.script.ScriptDefTest, script.xml : src + encoding - org.apache.tools.ant.taskdefs.optional.RhinoScriptDefTest, scriptdef.xml : src + encoding, compiled - new file: src/etc/testcases/taskdefs/optional/script/heavy-script.js --- manual/Tasks/conditions.html | 5 ++ manual/Tasks/script.html | 7 +- manual/Tasks/scriptdef.html | 14 ++++ manual/Types/filterchain.html | 5 ++ manual/Types/mapper.html | 5 ++ manual/Types/selectors.html | 5 ++ .../testcases/taskdefs/optional/script.xml | 22 +++++ .../taskdefs/optional/script/heavy-script.js | 68 +++++++++++++++ .../taskdefs/optional/script/scriptdef.xml | 84 +++++++++++++++++++ .../taskdefs/optional/script/ScriptDef.java | 38 +++++---- .../ant/util/optional/JavaxScriptRunner.java | 18 ++-- .../taskdefs/optional/RhinoScriptTest.java | 22 +++++ .../optional/script/ScriptDefTest.java | 41 +++++++++ 13 files changed, 303 insertions(+), 31 deletions(-) create mode 100644 src/etc/testcases/taskdefs/optional/script/heavy-script.js diff --git a/manual/Tasks/conditions.html b/manual/Tasks/conditions.html index 08d0bccec..895da5426 100644 --- a/manual/Tasks/conditions.html +++ b/manual/Tasks/conditions.html @@ -536,6 +536,11 @@ an explanation of scripts and dependencies. filename of script source No + + encoding + The encoding of the script source. since Ant 1.10.1. + No - defaults to default JVM encoding + setbeans whether to have all properties, references and targets as diff --git a/manual/Tasks/script.html b/manual/Tasks/script.html index fb008224a..7a56eec89 100644 --- a/manual/Tasks/script.html +++ b/manual/Tasks/script.html @@ -102,6 +102,11 @@ different location values.

The location of the script as a file, if not inline No + + encoding + The encoding of the script as a file. since Ant 1.10.1. + No - defaults to default JVM encoding + setbeans @@ -282,7 +287,7 @@ filesizes of all files a <fileset/> caught.

// Access to Ant-Properties by their names dir = project.getProperty("fs.dir"); includes = MyProject.getProperty("fs.includes"); - excludes = self.getProject() .getProperty("fs.excludes"); + excludes = self.getProject().getProperty("fs.excludes"); // Create a <fileset dir="" includes=""/> fs = project.createDataType("fileset"); diff --git a/manual/Tasks/scriptdef.html b/manual/Tasks/scriptdef.html index 38c3ffff0..de601a583 100644 --- a/manual/Tasks/scriptdef.html +++ b/manual/Tasks/scriptdef.html @@ -115,6 +115,20 @@ more information on writing scripts, please refer to the The location of the script as a file, if not inline No + + encoding + The encoding of the script as a file. since Ant 1.10.1. + No - defaults to default JVM encoding + + + compiled + If true, the script is compiled before the first + evaluation for faster multiple executions, on the condition that the manager is "javax" and the + target engine implements javax.script.Compilable. + Note that the bsf manager may automatically compiles the script. + since Ant 1.10.1. + No - defaults to false + uri diff --git a/manual/Types/filterchain.html b/manual/Types/filterchain.html index 90dd74bef..14968160d 100644 --- a/manual/Types/filterchain.html +++ b/manual/Types/filterchain.html @@ -1565,6 +1565,11 @@ Must be a supported Apache BSF or JSR 223 language No + + encoding + The encoding of the script as a file. since Ant 1.10.1. + No - defaults to default JVM encoding + setbeans whether to have all properties, references and targets as diff --git a/manual/Types/mapper.html b/manual/Types/mapper.html index 5aff311e6..f6bc76dd2 100644 --- a/manual/Types/mapper.html +++ b/manual/Types/mapper.html @@ -805,6 +805,11 @@ an explanation of scripts and dependencies. No + + encoding + The encoding of the script as a file. since Ant 1.10.1. + No - defaults to default JVM encoding + setbeans whether to have all properties, references and targets as diff --git a/manual/Types/selectors.html b/manual/Types/selectors.html index a79138003..6a080cbc3 100644 --- a/manual/Types/selectors.html +++ b/manual/Types/selectors.html @@ -1107,6 +1107,11 @@ filename of the script no + + encoding + The encoding of the script as a file. since Ant 1.10.1. + No - defaults to default JVM encoding + setbeans whether to have all properties, references and targets as diff --git a/src/etc/testcases/taskdefs/optional/script.xml b/src/etc/testcases/taskdefs/optional/script.xml index 841f70d27..c098289bc 100644 --- a/src/etc/testcases/taskdefs/optional/script.xml +++ b/src/etc/testcases/taskdefs/optional/script.xml @@ -17,6 +17,10 @@ --> + + + + This build-file is intended to be run from the test cases @@ -43,4 +47,22 @@ ]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java index 9f3854fe0..7a410e0ab 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java @@ -40,7 +40,7 @@ import org.apache.tools.ant.util.ScriptRunnerBase; import org.apache.tools.ant.util.ScriptRunnerHelper; /** - * Define a task using a script + * Defines a task using a script. * * @since Ant 1.6 */ @@ -76,7 +76,7 @@ public class ScriptDef extends DefBase { } /** - * set the name under which this script will be activated in a build + * Sets the name under which this script will be activated in a build * file * * @param name the name of the script @@ -104,7 +104,7 @@ public class ScriptDef extends DefBase { private String name; /** - * Set the attribute name + * Sets the attribute name * * @param name the attribute name */ @@ -114,7 +114,7 @@ public class ScriptDef extends DefBase { } /** - * Add an attribute definition to this script. + * Adds an attribute definition to this script. * * @param attribute the attribute definition. */ @@ -136,7 +136,7 @@ public class ScriptDef extends DefBase { private String className; /** - * set the tag name for this nested element + * Sets the tag name for this nested element * * @param name the name of this nested element */ @@ -145,7 +145,7 @@ public class ScriptDef extends DefBase { } /** - * Set the type of this element. This is the name of an + * Sets the type of this element. This is the name of an * Ant task or type which is to be used when this element is to be * created. This is an alternative to specifying the class name directly * @@ -157,7 +157,7 @@ public class ScriptDef extends DefBase { } /** - * Set the classname of the class to be used for the nested element. + * Sets the classname of the class to be used for the nested element. * This specifies the class directly and is an alternative to specifying * the Ant type name. * @@ -170,7 +170,7 @@ public class ScriptDef extends DefBase { } /** - * Add a nested element definition. + * Adds a nested element definition. * * @param nestedElement the nested element definition. */ @@ -179,7 +179,7 @@ public class ScriptDef extends DefBase { } /** - * Define the script. + * Defines the script. */ public void execute() { if (name == null) { @@ -257,7 +257,7 @@ public class ScriptDef extends DefBase { } /** - * Find or create the script repository - it is stored in the project. + * Finds or creates the script repository - it is stored in the project. * This method is synchronized on the project under {@link MagicNames#SCRIPT_REPOSITORY} * @return the current script repository registered as a reference. */ @@ -277,7 +277,7 @@ public class ScriptDef extends DefBase { } /** - * Create a nested element to be configured. + * Creates a nested element to be configured. * * @param elementName the name of the nested element. * @return object representing the element name. @@ -322,7 +322,7 @@ public class ScriptDef extends DefBase { } /** - * Execute the script. + * Executes the script. * * @param attributes collection of attributes * @param elements a list of nested element values. @@ -334,7 +334,7 @@ public class ScriptDef extends DefBase { } /** - * Execute the script. + * Executes the script. * This is called by the script instance to execute the script for this * definition. * @@ -375,13 +375,14 @@ public class ScriptDef extends DefBase { * Defines the compilation feature ; optional. * * @param compiled enables the script compilation if available. + * @since Ant 1.10.1 */ public void setCompiled(boolean compiled) { helper.setCompiled(compiled); } /** - * Load the script from an external file ; optional. + * Loads the script from an external file ; optional. * * @param file the file containing the script source. */ @@ -390,16 +391,17 @@ public class ScriptDef extends DefBase { } /** - * Set the encoding of the script from an external file ; optional. + * Sets the encoding of the script from an external file ; optional. * * @param encoding the encoding of the file containing the script source. + * @since Ant 1.10.1 */ public void setEncoding(String encoding) { helper.setEncoding(encoding); } /** - * Set the script text. + * Sets the script text. * * @param text a component of the script text to be added. */ @@ -408,8 +410,8 @@ public class ScriptDef extends DefBase { } /** - * Add any source resource. - * @since Ant1.7.1 + * Adds any source resource. + * @since Ant 1.7.1 * @param resource source of script */ public void add(ResourceCollection resource) { diff --git a/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java b/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java index 7209e2884..63ab4d93a 100644 --- a/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java +++ b/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java @@ -84,14 +84,14 @@ public class JavaxScriptRunner extends ScriptRunnerBase { if (getCompiled()) { - final String compiledScriptRefName = execName + ".compiledScript.0123456789"; + final String compiledScriptRefName = execName + ".compiled." + getScript().hashCode() + "." + getProject().hashCode(); if (null == compiledScript) { compiledScript = getProject().getReference(compiledScriptRefName); } if (null == compiledScript) { - ReflectWrapper engine = createEngine(); + final ReflectWrapper engine = createEngine(); if (engine == null) { throw new BuildException( "Unable to create javax script engine for " @@ -111,7 +111,7 @@ public class JavaxScriptRunner extends ScriptRunnerBase { compiledScript = new ReflectWrapper(compiled); } else { - getProject().log("script compilation not available", Project.MSG_VERBOSE); + getProject().log("script compilation not available for " + execName, Project.MSG_VERBOSE); compiledScript = new ReflectWrapper(null); } getProject().addReference(compiledScriptRefName, compiledScript); @@ -123,7 +123,7 @@ public class JavaxScriptRunner extends ScriptRunnerBase { applyBindings(simpleBindings); - getProject().log("run compiled script " + execName, Project.MSG_DEBUG); + getProject().log("run compiled script " + compiledScriptRefName, Project.MSG_DEBUG); final Class bindingsClass = Class.forName("javax.script.Bindings", true, getClass().getClassLoader()); @@ -173,15 +173,9 @@ public class JavaxScriptRunner extends ScriptRunnerBase { String key = (String) i.next(); Object value = getBeans().get(key); if ("FX".equalsIgnoreCase(getLanguage())) { - engine.invoke( - "put", String.class, key - + ":" + value.getClass().getName(), - Object.class, value); - } else { - engine.invoke( - "put", String.class, key, - Object.class, value); + key += ":" + value.getClass().getName(); } + engine.invoke("put", String.class, key, Object.class, value); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/RhinoScriptTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/RhinoScriptTest.java index 35576dcb6..5b41d0f4f 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/RhinoScriptTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/RhinoScriptTest.java @@ -17,12 +17,14 @@ */ package org.apache.tools.ant.taskdefs.optional; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Tests the examples of the <script> task docs. @@ -64,4 +66,24 @@ public class RhinoScriptTest { assertTrue(index > -1); } + @Test + public void testUseSrcAndEncoding() { + final String readerEncoding = "UTF-8"; + buildRule.getProject().setProperty("useSrcAndEncoding.reader.encoding", readerEncoding); + buildRule.executeTarget("useSrcAndEncoding"); + } + + @Test + public void testUseSrcAndEncodingFailure() { + final String readerEncoding = "ISO-8859-1"; + buildRule.getProject().setProperty("useSrcAndEncoding.reader.encoding", readerEncoding); + try { + buildRule.executeTarget("useSrcAndEncoding"); + fail("should have failed with reader's encoding [" + readerEncoding + + "] different from the writer's encoding [" + buildRule.getProject().getProperty("useSrcAndEncoding.encoding") + "]"); + } + catch(BuildException e) { + assertTrue(e.getMessage().matches("expected but was ")); + } + } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java index 5d4d4cf61..0b2ebce61 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java @@ -142,5 +142,46 @@ public class ScriptDefTest { log.indexOf("Attribute value = test") != -1); } + @Test + public void testUseSrcAndEncoding() { + final String readerEncoding = "UTF-8"; + buildRule.getProject().setProperty("useSrcAndEncoding.reader.encoding", readerEncoding); + buildRule.executeTarget("useSrcAndEncoding"); + } + + @Test + public void testUseSrcAndEncodingFailure() { + final String readerEncoding = "ISO-8859-1"; + buildRule.getProject().setProperty("useSrcAndEncoding.reader.encoding", readerEncoding); + try { + buildRule.executeTarget("useSrcAndEncoding"); + fail("should have failed with reader's encoding [" + readerEncoding + + "] different from the writer's encoding [" + + buildRule.getProject().getProperty("useSrcAndEncoding.encoding") + "]"); + } + catch(BuildException e) { + assertTrue(e.getMessage().matches( + "expected but was ")); + } + } + @Test + public void testUseCompiled() { + + final long duration; + { + long start = System.nanoTime(); + buildRule.executeTarget("useCompiled"); + duration = System.nanoTime() - start; + } + + final long notCompiledDuration; + { + long start = System.nanoTime(); + buildRule.executeTarget("useNotCompiled"); + notCompiledDuration = System.nanoTime() - start; + } + + assertTrue(duration < notCompiledDuration); + } }