Browse Source

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
master
pyxide Stefan Bodewig 8 years ago
parent
commit
ea7091756b
13 changed files with 303 additions and 31 deletions
  1. +5
    -0
      manual/Tasks/conditions.html
  2. +6
    -1
      manual/Tasks/script.html
  3. +14
    -0
      manual/Tasks/scriptdef.html
  4. +5
    -0
      manual/Types/filterchain.html
  5. +5
    -0
      manual/Types/mapper.html
  6. +5
    -0
      manual/Types/selectors.html
  7. +22
    -0
      src/etc/testcases/taskdefs/optional/script.xml
  8. +68
    -0
      src/etc/testcases/taskdefs/optional/script/heavy-script.js
  9. +84
    -0
      src/etc/testcases/taskdefs/optional/script/scriptdef.xml
  10. +20
    -18
      src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
  11. +6
    -12
      src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
  12. +22
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/RhinoScriptTest.java
  13. +41
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java

+ 5
- 0
manual/Tasks/conditions.html View File

@@ -536,6 +536,11 @@ an explanation of scripts and dependencies.
<td valign="top">filename of script source</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">The encoding of the script source. <em>since Ant 1.10.1.</em></td>
<td valign="top" align="center">No - defaults to default JVM encoding</td>
</tr>
<tr>
<td valign="top">setbeans</td>
<td valign="top">whether to have all properties, references and targets as


+ 6
- 1
manual/Tasks/script.html View File

@@ -102,6 +102,11 @@ different location values.</p>
<td valign="top">The location of the script as a file, if not inline</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">The encoding of the script as a file. <em>since Ant 1.10.1.</em></td>
<td valign="top" align="center">No - defaults to default JVM encoding</td>
</tr>
<tr>
<td valign="top">setbeans</td>
<td valign="top">
@@ -282,7 +287,7 @@ filesizes of all files a <code>&lt;fileset/&gt;</code> caught.</p>
// Access to Ant-Properties by their names
dir = <font color=blue>project</font>.getProperty("fs.dir");
includes = <font color=blue>MyProject</font>.getProperty("fs.includes");
excludes = <font color=blue>self.getProject()</font> .<font color=blue>getProperty("fs.excludes")</font>;
excludes = <font color=blue>self.getProject()</font>.<font color=blue>getProperty("fs.excludes")</font>;

// Create a &lt;fileset dir="" includes=""/&gt;
fs = project.<font color=blue>createDataType("fileset")</font>;


+ 14
- 0
manual/Tasks/scriptdef.html View File

@@ -115,6 +115,20 @@ more information on writing scripts, please refer to the
<td valign="top">The location of the script as a file, if not inline</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">The encoding of the script as a file. <em>since Ant 1.10.1.</em></td>
<td valign="top" align="center">No - defaults to default JVM encoding</td>
</tr>
<tr>
<td valign="top">compiled</td>
<td valign="top">If true, the script is compiled before the first
evaluation for faster multiple executions, on the condition that the <em>manager</em> is "javax" and the
target engine implements <code>javax.script.Compilable</code>.
Note that the <code>bsf</code> manager may automatically compiles the script.
<em>since Ant 1.10.1.</em></td>
<td valign="top" align="center">No - defaults to false</td>
</tr>
<tr>
<td valign="top">uri</td>
<td valign="top">


+ 5
- 0
manual/Types/filterchain.html View File

@@ -1565,6 +1565,11 @@ Must be a supported Apache BSF or JSR 223 language</td>
</td>
<td vAlign=top align="center">No</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">The encoding of the script as a file. <em>since Ant 1.10.1.</em></td>
<td valign="top" align="center">No - defaults to default JVM encoding</td>
</tr>
<tr>
<td valign="top">setbeans</td>
<td valign="top">whether to have all properties, references and targets as


+ 5
- 0
manual/Types/mapper.html View File

@@ -805,6 +805,11 @@ an explanation of scripts and dependencies.
</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">The encoding of the script as a file. <em>since Ant 1.10.1.</em></td>
<td valign="top" align="center">No - defaults to default JVM encoding</td>
</tr>
<tr>
<td valign="top">setbeans</td>
<td valign="top">whether to have all properties, references and targets as


+ 5
- 0
manual/Types/selectors.html View File

@@ -1107,6 +1107,11 @@
<td valign="top">filename of the script</td>
<td valign="top" align="center">no</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">The encoding of the script as a file. <em>since Ant 1.10.1.</em></td>
<td valign="top" align="center">No - defaults to default JVM encoding</td>
</tr>
<tr>
<td valign="top">setbeans</td>
<td valign="top">whether to have all properties, references and targets as


+ 22
- 0
src/etc/testcases/taskdefs/optional/script.xml View File

@@ -17,6 +17,10 @@
-->
<project name="testproject" default="def" basedir=".">

<import file="../../buildfiletest-base.xml"/>

<property name="root" location="../../../../.."/>

<target name="def">
<fail>This build-file is intended to be run from the test cases</fail>
</target>
@@ -43,4 +47,22 @@
]]></script>
</target>

<target name="useSrcAndEncoding">
<mkdir dir="${output}" />
<property name="useSrcAndEncoding.encoding" value="UTF-8"/>
<property name="useSrcAndEncoding.reader.encoding" value="${useSrcAndEncoding.encoding}"
description="Set a different encoding to raise a failure (ex. ISO-8859-1)" />
<property name="useSrcAndEncoding.file" location="${output}/script.useSrcAndEncoding.js"/>
<property name="useSrcAndEncoding.expectedProp" value="eacute [&#233;]" />

<echo file="${useSrcAndEncoding.file}" encoding="${useSrcAndEncoding.encoding}"
message="project.setNewProperty('useSrcAndEncoding.prop', '${useSrcAndEncoding.expectedProp}');"/>

<script language="javascript" src="${useSrcAndEncoding.file}" encoding="${useSrcAndEncoding.reader.encoding}"/>
<condition property="useSrcAndEncoding.testOK" >
<equals arg1="${useSrcAndEncoding.expectedProp}" arg2="${useSrcAndEncoding.prop}" />
</condition>
<fail message="expected &lt;${useSrcAndEncoding.expectedProp}&gt; but was &lt;${useSrcAndEncoding.prop}&gt;" unless="useSrcAndEncoding.testOK" />
</target>

</project>

+ 68
- 0
src/etc/testcases/taskdefs/optional/script/heavy-script.js View File

@@ -0,0 +1,68 @@

var functions = [
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },function (value) { return value; },
];

var v = 'a';
for(var i in functions) {
v = functions[i](v);
}

var echo = project.createTask("echo");
echo.setMessage("heavy-script done");
echo.perform();

+ 84
- 0
src/etc/testcases/taskdefs/optional/script/scriptdef.xml View File

@@ -17,6 +17,10 @@
-->
<project name="testproject" default="def" basedir=".">

<import file="../../../buildfiletest-base.xml"/>

<property name="root" location="../../../../../.."/>

<target name="def">
<fail>This build-file is intended to be run from the test cases</fail>
</target>
@@ -141,5 +145,85 @@
</scripttest>
</target>

<target name="useBeanshell">
<script language="beanshell"><![CDATA[
self.log("I'm here", org.apache.tools.ant.Project.MSG_INFO);
]]></script>
</target>

<target name="useSrcAndEncoding">
<mkdir dir="${output}" />
<property name="useSrcAndEncoding.encoding" value="UTF-8"/>
<property name="useSrcAndEncoding.reader.encoding" value="${useSrcAndEncoding.encoding}"
description="Set a different encoding to raise a failure (ex. ISO-8859-1)" />
<property name="useSrcAndEncoding.file" location="${output}/script.useSrcAndEncoding.js"/>
<property name="useSrcAndEncoding.expectedProp" value="eacute [&#233;]" />

<echo file="${useSrcAndEncoding.file}" encoding="${useSrcAndEncoding.encoding}"
message="project.setNewProperty('useSrcAndEncoding.prop', '${useSrcAndEncoding.expectedProp}');"/>

<scriptdef name="useSrcAndEncoding" language="javascript" src="${useSrcAndEncoding.file}" encoding="${useSrcAndEncoding.reader.encoding}"/>
<useSrcAndEncoding/>

<condition property="useSrcAndEncoding.testOK" >
<equals arg1="${useSrcAndEncoding.expectedProp}" arg2="${useSrcAndEncoding.prop}" />
</condition>
<fail message="expected &lt;${useSrcAndEncoding.expectedProp}&gt; but was &lt;${useSrcAndEncoding.prop}&gt;" unless="useSrcAndEncoding.testOK" />
</target>

<target name="useCompiled">

<!-- Test with 'javax' manager, 'bsf' manager already compiles the script. -->
<scriptdef manager="javax" name="heavyscript" language="javascript" src="heavy-script.js" encoding="UTF-8" compiled="true" />

<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />
<heavyscript />

</target>

<target name="useNotCompiled">

<scriptdef manager="javax" name="heavyscriptNotCompiled" language="javascript" src="heavy-script.js" encoding="UTF-8" compiled="false" />

<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />
<heavyscriptNotCompiled />

</target>

</project>

+ 20
- 18
src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java View File

@@ -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) {


+ 6
- 12
src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java View File

@@ -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);
}
}



+ 22
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/optional/RhinoScriptTest.java View File

@@ -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 &lt;script&gt; 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 <eacute \\[\u00e9]> but was <eacute \\[\u00c3\u00a9]>"));
}
}
}

+ 41
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java View File

@@ -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 <eacute \\[\u00e9]> but was <eacute \\[\u00c3\u00a9]>"));
}
}

@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);
}
}

Loading…
Cancel
Save