506 assertEquals(sw.toString(), println("hello")); 507 } 508 509 @Test 510 // check that print prints all arguments (more than one) 511 public void printManyTest() { 512 final ScriptEngineManager m = new ScriptEngineManager(); 513 final ScriptEngine e = m.getEngineByName("nashorn"); 514 final StringWriter sw = new StringWriter(); 515 e.getContext().setWriter(sw); 516 try { 517 e.eval("print(34, true, 'hello')"); 518 } catch (final Throwable t) { 519 t.printStackTrace(); 520 fail(t.getMessage()); 521 } 522 523 assertEquals(sw.toString(), println("34 true hello")); 524 } 525 526 private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 527 528 // Returns String that would be the result of calling PrintWriter.println 529 // of the given String. (This is to handle platform specific newline). 530 private static String println(final String str) { 531 return str + LINE_SEPARATOR; 532 } 533 } | 506 assertEquals(sw.toString(), println("hello")); 507 } 508 509 @Test 510 // check that print prints all arguments (more than one) 511 public void printManyTest() { 512 final ScriptEngineManager m = new ScriptEngineManager(); 513 final ScriptEngine e = m.getEngineByName("nashorn"); 514 final StringWriter sw = new StringWriter(); 515 e.getContext().setWriter(sw); 516 try { 517 e.eval("print(34, true, 'hello')"); 518 } catch (final Throwable t) { 519 t.printStackTrace(); 520 fail(t.getMessage()); 521 } 522 523 assertEquals(sw.toString(), println("34 true hello")); 524 } 525 526 @Test 527 public void scriptObjectAutoConversionTest() throws ScriptException { 528 final ScriptEngineManager m = new ScriptEngineManager(); 529 final ScriptEngine e = m.getEngineByName("nashorn"); 530 e.eval("obj = { foo: 'hello' }"); 531 e.put("Window", e.eval("Packages.jdk.nashorn.api.scripting.Window")); 532 assertEquals(e.eval("Window.funcJSObject(obj)"), "hello"); 533 assertEquals(e.eval("Window.funcScriptObjectMirror(obj)"), "hello"); 534 assertEquals(e.eval("Window.funcMap(obj)"), "hello"); 535 assertEquals(e.eval("Window.funcJSObject(obj)"), "hello"); 536 } 537 538 private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 539 540 // Returns String that would be the result of calling PrintWriter.println 541 // of the given String. (This is to handle platform specific newline). 542 private static String println(final String str) { 543 return str + LINE_SEPARATOR; 544 } 545 } |