aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorsundar <none@none>2014-01-09 19:23:34 +0530
committersundar <none@none>2014-01-09 19:23:34 +0530
commit73086ab978897ab9fef952428ae8a6ea34ecee85 (patch)
tree998ccb6f870d3ca1f832081d1d65aac2890e0291 /test
parent03b7763e1efd33fdec735b34d8c847c259b087cf (diff)
downloadnashorn-73086ab978897ab9fef952428ae8a6ea34ecee85.tar.gz
8031359: Invocable.getInterface() works incorrectly if interface has default methods
Reviewed-by: attila, hannesw
Diffstat (limited to 'test')
-rw-r--r--test/script/basic/JDK-8031359.js62
-rw-r--r--test/script/basic/JDK-8031359.js.EXPECTED6
-rw-r--r--test/src/jdk/nashorn/api/scripting/InvocableTest.java13
3 files changed, 81 insertions, 0 deletions
diff --git a/test/script/basic/JDK-8031359.js b/test/script/basic/JDK-8031359.js
new file mode 100644
index 00000000..278fd7d2
--- /dev/null
+++ b/test/script/basic/JDK-8031359.js
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8031359: Invocable.getInterface() works incorrectly if interface has default methods
+ *
+ * @test
+ * @run
+ */
+
+var func = new java.util.function.Function() {
+ apply: function(arg) {
+ print("func called with " + arg);
+ return arg.toUpperCase();
+ }
+};
+
+// Function.andThen is a default method
+func.andThen(func)("hello");
+
+// Function.compose is another default method
+func.compose(new java.util.function.Function() {
+ apply: function(arg) {
+ print("compose called with " + arg);
+ return arg.charAt(0);
+ }
+})("hello");
+
+var func2 = new java.util.function.Function() {
+ apply: function(arg) {
+ print("I am func2: " + arg);
+ return arg;
+ },
+
+ andThen: function(func) {
+ print("This is my andThen!");
+ return func;
+ }
+};
+
+func2.apply("hello");
+func2.andThen(func);
diff --git a/test/script/basic/JDK-8031359.js.EXPECTED b/test/script/basic/JDK-8031359.js.EXPECTED
new file mode 100644
index 00000000..3fb72f1b
--- /dev/null
+++ b/test/script/basic/JDK-8031359.js.EXPECTED
@@ -0,0 +1,6 @@
+func called with hello
+func called with HELLO
+compose called with hello
+func called with h
+I am func2: hello
+This is my andThen!
diff --git a/test/src/jdk/nashorn/api/scripting/InvocableTest.java b/test/src/jdk/nashorn/api/scripting/InvocableTest.java
index fad3f372..a6722f57 100644
--- a/test/src/jdk/nashorn/api/scripting/InvocableTest.java
+++ b/test/src/jdk/nashorn/api/scripting/InvocableTest.java
@@ -26,6 +26,7 @@
package jdk.nashorn.api.scripting;
import java.util.Objects;
+import java.util.function.Function;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
@@ -522,4 +523,16 @@ public class InvocableTest {
Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]");
Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]");
}
+
+ @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);
+ assertEquals(func.apply("hello"), "HELLO");
+ }
}