diff options
Diffstat (limited to 'test/src/jdk/nashorn/api/scripting/InvocableTest.java')
-rw-r--r-- | test/src/jdk/nashorn/api/scripting/InvocableTest.java | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/test/src/jdk/nashorn/api/scripting/InvocableTest.java b/test/src/jdk/nashorn/api/scripting/InvocableTest.java index a6722f57..4e44a296 100644 --- a/test/src/jdk/nashorn/api/scripting/InvocableTest.java +++ b/test/src/jdk/nashorn/api/scripting/InvocableTest.java @@ -25,6 +25,8 @@ package jdk.nashorn.api.scripting; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; import java.util.Objects; import java.util.function.Function; import javax.script.Invocable; @@ -34,16 +36,15 @@ import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleScriptContext; import org.testng.Assert; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; import org.testng.annotations.Test; /** * Tests for javax.script.Invocable implementation of nashorn. */ +@SuppressWarnings("javadoc") public class InvocableTest { - private void log(String msg) { + private static void log(final String msg) { org.testng.Reporter.log(msg, true); } @@ -69,12 +70,12 @@ public class InvocableTest { * evaluating script with different Context set. */ public void invokeMethodDifferentContextTest() { - ScriptEngineManager m = new ScriptEngineManager(); - ScriptEngine e = m.getEngineByName("nashorn"); + final ScriptEngineManager m = new ScriptEngineManager(); + final ScriptEngine e = m.getEngineByName("nashorn"); try { // define an object with method on it - Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); + final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); @@ -99,7 +100,7 @@ public class InvocableTest { try { final Object obj = e.eval("({})"); - final Object res = ((Invocable) e).invokeMethod(obj, null); + ((Invocable) e).invokeMethod(obj, null); fail("should have thrown NPE"); } catch (final Exception exp) { if (!(exp instanceof NullPointerException)) { @@ -119,7 +120,7 @@ public class InvocableTest { try { final Object obj = e.eval("({})"); - final Object res = ((Invocable) e).invokeMethod(obj, "nonExistentMethod"); + ((Invocable) e).invokeMethod(obj, "nonExistentMethod"); fail("should have thrown NoSuchMethodException"); } catch (final Exception exp) { if (!(exp instanceof NoSuchMethodException)) { @@ -180,7 +181,7 @@ public class InvocableTest { final ScriptEngine engine2 = m.getEngineByName("nashorn"); try { - Object obj = engine1.eval("({ run: function() {} })"); + final Object obj = engine1.eval("({ run: function() {} })"); // pass object from engine1 to engine2 as 'thiz' for invokeMethod ((Invocable) engine2).invokeMethod(obj, "run"); fail("should have thrown IllegalArgumentException"); @@ -211,7 +212,7 @@ public class InvocableTest { // try interface on specific script object try { e.eval("var obj = { run: function() { print('run from obj'); } };"); - Object obj = e.get("obj"); + final Object obj = e.get("obj"); final Runnable runnable = inv.getInterface(obj, Runnable.class); runnable.run(); } catch (final Exception exp) { @@ -307,17 +308,17 @@ public class InvocableTest { * switching to use different ScriptContext. */ public void getInterfaceDifferentContext() { - ScriptEngineManager m = new ScriptEngineManager(); - ScriptEngine e = m.getEngineByName("nashorn"); + final ScriptEngineManager m = new ScriptEngineManager(); + final ScriptEngine e = m.getEngineByName("nashorn"); try { - Object obj = e.eval("({ run: function() { } })"); + final Object obj = e.eval("({ run: function() { } })"); // change script context - ScriptContext ctxt = new SimpleScriptContext(); + final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); - Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); + final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); r.run(); } catch (final Exception exp) { exp.printStackTrace(); @@ -376,7 +377,7 @@ public class InvocableTest { final ScriptEngine engine2 = m.getEngineByName("nashorn"); try { - Object obj = engine1.eval("({ run: function() {} })"); + final Object obj = engine1.eval("({ run: function() {} })"); // pass object from engine1 to engine2 as 'thiz' for getInterface ((Invocable) engine2).getInterface(obj, Runnable.class); fail("should have thrown IllegalArgumentException"); @@ -397,7 +398,7 @@ public class InvocableTest { final ScriptEngine e = m.getEngineByName("nashorn"); try { - final Object res = ((Invocable) e).invokeFunction(null); + ((Invocable)e).invokeFunction(null); fail("should have thrown NPE"); } catch (final Exception exp) { if (!(exp instanceof NullPointerException)) { @@ -417,7 +418,7 @@ public class InvocableTest { final ScriptEngine e = m.getEngineByName("nashorn"); try { - final Object res = ((Invocable) e).invokeFunction("NonExistentFunc"); + ((Invocable)e).invokeFunction("NonExistentFunc"); fail("should have thrown NoSuchMethodException"); } catch (final Exception exp) { if (!(exp instanceof NoSuchMethodException)) { @@ -433,12 +434,12 @@ public class InvocableTest { * Bindings. */ public void invokeFunctionDifferentContextTest() { - ScriptEngineManager m = new ScriptEngineManager(); - ScriptEngine e = m.getEngineByName("nashorn"); + final ScriptEngineManager m = new ScriptEngineManager(); + final ScriptEngine e = m.getEngineByName("nashorn"); try { // define an object with method on it - Object obj = e.eval("function hello() { return 'Hello World!'; }"); + e.eval("function hello() { return 'Hello World!'; }"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); // change engine's current context @@ -525,14 +526,14 @@ public class InvocableTest { } @Test - @SuppressWarnings("unchecked") public void defaultMethodTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Invocable inv = (Invocable) e; - Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})"); - Function<String, String> func = inv.getInterface(obj, Function.class); + final Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})"); + @SuppressWarnings("unchecked") + final Function<String, String> func = inv.getInterface(obj, Function.class); assertEquals(func.apply("hello"), "HELLO"); } } |