659 @Test 660 public void currentGlobalMissingTest() throws Exception { 661 final ScriptEngineManager manager = new ScriptEngineManager(); 662 final ScriptEngine e = manager.getEngineByName("nashorn"); 663 664 final Context ctx = new Context(); 665 e.put("ctx", ctx); 666 e.eval("var obj = { foo: function(str) { return str.toUpperCase() } }"); 667 e.eval("ctx.set(obj)"); 668 final Invocable inv = (Invocable)e; 669 assertEquals("HELLO", inv.invokeMethod(ctx.get(), "foo", "hello")); 670 // try object literal 671 e.eval("ctx.set({ bar: function(str) { return str.toLowerCase() } })"); 672 assertEquals("hello", inv.invokeMethod(ctx.get(), "bar", "HELLO")); 673 // try array literal 674 e.eval("var arr = [ 'hello', 'world' ]"); 675 e.eval("ctx.set(arr)"); 676 assertEquals("helloworld", inv.invokeMethod(ctx.get(), "join", "")); 677 } 678 679 // @bug JDK-8068889: ConsString arguments to a functional interface wasn't converted to string. 680 @Test 681 public void functionalInterfaceStringTest() throws Exception { 682 final ScriptEngineManager manager = new ScriptEngineManager(); 683 final ScriptEngine e = manager.getEngineByName("nashorn"); 684 final AtomicBoolean invoked = new AtomicBoolean(false); 685 e.put("f", new Function<String, String>() { 686 @Override 687 public String apply(String t) { 688 invoked.set(true); 689 return t; 690 } 691 }); 692 assertEquals(e.eval("var x = 'a'; x += 'b'; f(x)"), "ab"); 693 assertTrue(invoked.get()); 694 } 695 696 // @bug JDK-8068889: ScriptObject arguments to a functional interface wasn't converted to a mirror. 697 @Test 698 public void functionalInterfaceObjectTest() throws Exception { 699 final ScriptEngineManager manager = new ScriptEngineManager(); 700 final ScriptEngine e = manager.getEngineByName("nashorn"); 701 final AtomicBoolean invoked = new AtomicBoolean(false); 702 e.put("c", new Consumer<Object>() { 703 @Override 704 public void accept(Object t) { 705 assertTrue(t instanceof ScriptObjectMirror); 706 assertEquals(((ScriptObjectMirror)t).get("a"), "xyz"); 707 invoked.set(true); 708 } 709 }); 710 e.eval("var x = 'xy'; x += 'z';c({a:x})"); 711 assertTrue(invoked.get()); 712 } 713 714 // @bug 8068524: NashornScriptEngineFactory.getParameter() throws IAE 715 // for an unknown key, doesn't conform to the general spec 716 @Test 717 public void getParameterInvalidKeyTest() throws Exception { 718 final ScriptEngineManager manager = new ScriptEngineManager(); 719 final ScriptEngine e = manager.getEngineByName("nashorn"); 720 // no exception expected here! 721 Object value = e.getFactory().getParameter("no value assigned to this key"); 722 assertNull(value); 723 } 724 725 // @bug JDK-8068889: ConsString arguments to a functional interface wasn't converted to string. 726 @Test 727 public void functionalInterfaceStringTest() throws Exception { 728 final ScriptEngineManager manager = new ScriptEngineManager(); 729 final ScriptEngine e = manager.getEngineByName("nashorn"); 730 final AtomicBoolean invoked = new AtomicBoolean(false); 731 e.put("f", new Function<String, String>() { 732 @Override 733 public String apply(String t) { | 659 @Test 660 public void currentGlobalMissingTest() throws Exception { 661 final ScriptEngineManager manager = new ScriptEngineManager(); 662 final ScriptEngine e = manager.getEngineByName("nashorn"); 663 664 final Context ctx = new Context(); 665 e.put("ctx", ctx); 666 e.eval("var obj = { foo: function(str) { return str.toUpperCase() } }"); 667 e.eval("ctx.set(obj)"); 668 final Invocable inv = (Invocable)e; 669 assertEquals("HELLO", inv.invokeMethod(ctx.get(), "foo", "hello")); 670 // try object literal 671 e.eval("ctx.set({ bar: function(str) { return str.toLowerCase() } })"); 672 assertEquals("hello", inv.invokeMethod(ctx.get(), "bar", "HELLO")); 673 // try array literal 674 e.eval("var arr = [ 'hello', 'world' ]"); 675 e.eval("ctx.set(arr)"); 676 assertEquals("helloworld", inv.invokeMethod(ctx.get(), "join", "")); 677 } 678 679 // @bug 8068524: NashornScriptEngineFactory.getParameter() throws IAE 680 // for an unknown key, doesn't conform to the general spec 681 @Test 682 public void getParameterInvalidKeyTest() throws Exception { 683 final ScriptEngineManager manager = new ScriptEngineManager(); 684 final ScriptEngine e = manager.getEngineByName("nashorn"); 685 // no exception expected here! 686 Object value = e.getFactory().getParameter("no value assigned to this key"); 687 assertNull(value); 688 } 689 690 // @bug JDK-8068889: ConsString arguments to a functional interface wasn't converted to string. 691 @Test 692 public void functionalInterfaceStringTest() throws Exception { 693 final ScriptEngineManager manager = new ScriptEngineManager(); 694 final ScriptEngine e = manager.getEngineByName("nashorn"); 695 final AtomicBoolean invoked = new AtomicBoolean(false); 696 e.put("f", new Function<String, String>() { 697 @Override 698 public String apply(String t) { |