test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java

Print this page
rev 1199 : 8072595: nashorn should not use obj.getClass() for null checks
Reviewed-by: hannesw, attila


 704     }
 705 
 706     // @bug JDK-8068889: ScriptObject arguments to a functional interface wasn't converted to a mirror.
 707     @Test
 708     public void functionalInterfaceObjectTest() throws Exception {
 709         final ScriptEngineManager manager = new ScriptEngineManager();
 710         final ScriptEngine e = manager.getEngineByName("nashorn");
 711         final AtomicBoolean invoked = new AtomicBoolean(false);
 712         e.put("c", new Consumer<Object>() {
 713             @Override
 714             public void accept(Object t) {
 715                 assertTrue(t instanceof ScriptObjectMirror);
 716                 assertEquals(((ScriptObjectMirror)t).get("a"), "xyz");
 717                 invoked.set(true);
 718             }
 719         });
 720         e.eval("var x = 'xy'; x += 'z';c({a:x})");
 721         assertTrue(invoked.get());
 722     }
 723 









 724     // @bug JDK-8068603: NashornScriptEngine.put/get() impls don't conform to NPE, IAE spec assertions
 725     @Test
 726     public void illegalBindingsValuesTest() throws Exception {
 727         final ScriptEngineManager manager = new ScriptEngineManager();
 728         final ScriptEngine e = manager.getEngineByName("nashorn");
 729 
 730         try {
 731             e.put(null, "null-value");
 732             fail();
 733         } catch (NullPointerException x) {
 734             // expected
 735         }
 736 
 737         try {
 738             e.put("", "empty-value");
 739             fail();
 740         } catch (IllegalArgumentException x) {
 741             // expected
 742         }
 743 




 704     }
 705 
 706     // @bug JDK-8068889: ScriptObject arguments to a functional interface wasn't converted to a mirror.
 707     @Test
 708     public void functionalInterfaceObjectTest() throws Exception {
 709         final ScriptEngineManager manager = new ScriptEngineManager();
 710         final ScriptEngine e = manager.getEngineByName("nashorn");
 711         final AtomicBoolean invoked = new AtomicBoolean(false);
 712         e.put("c", new Consumer<Object>() {
 713             @Override
 714             public void accept(Object t) {
 715                 assertTrue(t instanceof ScriptObjectMirror);
 716                 assertEquals(((ScriptObjectMirror)t).get("a"), "xyz");
 717                 invoked.set(true);
 718             }
 719         });
 720         e.eval("var x = 'xy'; x += 'z';c({a:x})");
 721         assertTrue(invoked.get());
 722     }
 723 
 724     @Test
 725     public void testLengthOnArrayLikeObjects() throws Exception {
 726         final ScriptEngine e = new ScriptEngineManager().getEngineByName("nashorn");
 727         final Object val = e.eval("var arr = { length: 1, 0: 1}; arr.length");
 728 
 729         assertTrue(Number.class.isAssignableFrom(val.getClass()));
 730         assertTrue(((Number)val).intValue() == 1);
 731     }
 732 
 733     // @bug JDK-8068603: NashornScriptEngine.put/get() impls don't conform to NPE, IAE spec assertions
 734     @Test
 735     public void illegalBindingsValuesTest() throws Exception {
 736         final ScriptEngineManager manager = new ScriptEngineManager();
 737         final ScriptEngine e = manager.getEngineByName("nashorn");
 738 
 739         try {
 740             e.put(null, "null-value");
 741             fail();
 742         } catch (NullPointerException x) {
 743             // expected
 744         }
 745 
 746         try {
 747             e.put("", "empty-value");
 748             fail();
 749         } catch (IllegalArgumentException x) {
 750             // expected
 751         }
 752