Mercurial > hg > rhino-tests
changeset 39:c07fce915c2b draft
* src/org/RhinoTests/CompilableTest.java:
Added two new tests.
* src/org/RhinoTests/CompilableClassTest.java:
Removed unnecessarry explicit casting.
* src/org/RhinoTests/CompiledScriptTest.java:
Fixed typo.
author | Pavel Tisnovsky <ptisnovs@redhat.com> |
---|---|
date | Thu, 20 Sep 2012 12:05:21 +0200 |
parents | be1b5714e3f0 |
children | 0c41d28346b9 |
files | ChangeLog src/org/RhinoTests/CompilableClassTest.java src/org/RhinoTests/CompilableTest.java src/org/RhinoTests/CompiledScriptTest.java |
diffstat | 4 files changed, 118 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Wed Sep 19 10:18:40 2012 +0200 +++ b/ChangeLog Thu Sep 20 12:05:21 2012 +0200 @@ -1,3 +1,12 @@ +2012-09-20 Pavel Tisnovsky <ptisnovs@redhat.com> + + * src/org/RhinoTests/CompilableTest.java: + Added two new tests. + * src/org/RhinoTests/CompilableClassTest.java: + Removed unnecessarry explicit casting. + * src/org/RhinoTests/CompiledScriptTest.java: + Fixed typo. + 2012-09-19 Pavel Tisnovsky <ptisnovs@redhat.com> * src/org/RhinoTests/CompilableClassTest.java:
--- a/src/org/RhinoTests/CompilableClassTest.java Wed Sep 19 10:18:40 2012 +0200 +++ b/src/org/RhinoTests/CompilableClassTest.java Thu Sep 20 12:05:21 2012 +0200 @@ -51,7 +51,6 @@ import javax.script.Compilable; import javax.script.ScriptEngineManager; -import javax.script.ScriptEngine; @@ -92,7 +91,7 @@ * Test for method javax.script.Compilable.getClass().isInstance() */ protected void testIsInstance() { - assertTrue(this.compilableClass.isInstance((Compilable)(new ScriptEngineManager().getEngineByName(Constants.EngineNames.ENGINE_NAME_JavaScript))), + assertTrue(this.compilableClass.isInstance((new ScriptEngineManager().getEngineByName(Constants.EngineNames.ENGINE_NAME_JavaScript))), "Method Compilable.getClass().isInstance() returns wrong value"); }
--- a/src/org/RhinoTests/CompilableTest.java Wed Sep 19 10:18:40 2012 +0200 +++ b/src/org/RhinoTests/CompilableTest.java Thu Sep 20 12:05:21 2012 +0200 @@ -40,18 +40,122 @@ package org.RhinoTests; +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + /** - * TODO: not implemented + * This test case check the behaviour of CompiledScript abstract class and it's + * descendents. + * * @author Pavel Tisnovsky - * + * */ -public class CompilableTest { +public class CompilableTest extends BaseRhinoTest { + + /** + * Empty, but still valid script. + */ + private static final String EMPTY_SCRIPT = ""; + + /** + * Instance of ScriptEngineManager which is used by all tests in this test + * case. + */ + ScriptEngineManager engineManager; + + /** + * Instance of ScriptEngine which is used by all tests in this test case. + */ + ScriptEngine scriptEngine; + + @Override + protected void setUp(String[] args) { + // setup attribute used by tests + this.engineManager = new ScriptEngineManager(); + this.scriptEngine = this.engineManager.getEngineByName("JavaScript"); + } + + @Override + protected void tearDown() { + // this block could be empty + return; + } + + /** + * Helper method which tries to retrieve an instance of class which + * implements CompiledScript interface for a given script. + * + * @param scriptText + * script source code + * @return instance of CompiledScript class + * @throws AssertionError + * when CompilingEngine cannot be retrieved + * @throws ScriptException + * thrown when script cannot be compiled + */ + private CompiledScript getCompiledScript(String scriptText) throws AssertionError, ScriptException { + // check if retyping could be done + assertTrue(this.scriptEngine instanceof Compilable, "ScriptEngine does not implement Compilable"); + // scriptEngine should be also retyped to Compilable, at least in case of JavaScript. + Compilable compilingEngine = (Compilable) this.scriptEngine; + // should not happen, but... + assertNotNull(compilingEngine, "cannot get compiling engine"); + // try to compile given script + return compileScript(scriptText, compilingEngine); + } + + /** + * Helper method which tries to compile given JavaScript. + * + * @param scriptText script source code + * @param compilingEngine instance of class which implements Compilable interface + * @return compiled script + * @throws ScriptException + * @throws AssertionError + */ + private CompiledScript compileScript(String scriptText, Compilable compilingEngine) throws ScriptException, AssertionError { + CompiledScript script = compilingEngine.compile(scriptText); + assertNotNull(script, "cannot compile script"); + return script; + } + + /** + * Test if it is possible to compile script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testCompileScriptStoredInString() throws ScriptException { + Compilable compilingEngine = (Compilable)this.scriptEngine; + assertNotNull(compilingEngine, "cannot get compiling engine"); + if (compilingEngine != null) { + CompiledScript script = compilingEngine.compile(EMPTY_SCRIPT); + assertNotNull(script, "cannot compile script"); + } + } + + /** + * Test if it is possible to compile and then run script from a string. + * + * @throws ScriptException + * this exception is thrown when this test case failed. + */ + protected void testCompileAndRunSimpleScriptStoredInString() throws ScriptException { + CompiledScript script = getCompiledScript(EMPTY_SCRIPT); + Object result = script.eval(); + assertNull(result, "result should be null"); + } + /** * Entry point to this test case. * * @param args parameters passed from command line */ public static void main(String[] args) { + new CompilableTest().doTests(args); } }
--- a/src/org/RhinoTests/CompiledScriptTest.java Wed Sep 19 10:18:40 2012 +0200 +++ b/src/org/RhinoTests/CompiledScriptTest.java Thu Sep 20 12:05:21 2012 +0200 @@ -67,6 +67,7 @@ @Override protected void setUp(String[] args) { + // setup attribute used by tests this.engineManager = new ScriptEngineManager(); this.scriptEngine = this.engineManager.getEngineByName("JavaScript"); }