changeset 7057:f5416026cdf5

8009115: jtreg tests under jdk/test/javax/script should use nashorn as script engine Reviewed-by: alanb
author sundar
date Wed, 27 Feb 2013 17:22:44 +0530
parents d623f520557b
children 13013dedcdfd
files test/javax/script/CauseExceptionTest.java test/javax/script/ExceptionTest.java test/javax/script/GetInterfaceTest.java test/javax/script/Helper.java test/javax/script/RhinoExceptionTest.java test/javax/script/StringWriterPrintTest.java test/javax/script/Test3.js test/javax/script/Test5.java test/javax/script/Test5.js test/javax/script/Test6.java test/javax/script/Test7.js test/javax/script/UnescapedBracketRegExTest.java test/javax/script/VersionTest.java
diffstat 13 files changed, 147 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/test/javax/script/CauseExceptionTest.java	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/CauseExceptionTest.java	Wed Feb 27 17:22:44 2013 +0530
@@ -24,7 +24,7 @@
 /*
  * @test
  * @bug 6869617
- * @summary RhinoScriptEngine bug : ScriptException cause not set (with fix)
+ * @summary ScriptEngine bug : ScriptException cause not set (with fix)
  */
 
 import javax.script.*;
@@ -33,12 +33,12 @@
 public class CauseExceptionTest {
     public static void main(String[] args) throws ScriptException, NoSuchMethodException {
         ScriptEngineManager sem = new ScriptEngineManager();
-        ScriptEngine engine = sem.getEngineByName("js");
+        ScriptEngine engine = sem.getEngineByName("nashorn");
         if (engine == null) {
             System.out.println("Warning: No js engine found; test vacuously passes.");
             return;
         }
-        engine.eval("function hello_world() { println('hello world'); throw 'out of here'; } ");
+        engine.eval("function hello_world() { print('hello world'); throw 'out of here'; } ");
         Invocable invocable = (Invocable) engine;
         try {
             invocable.invokeFunction("hello_world", (Object[])null);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/script/ExceptionTest.java	Wed Feb 27 17:22:44 2013 +0530
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2006, 2008, 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.
+ */
+
+/*
+ * @test
+ * @bug 6474943 6705893
+ * @summary Test that script engine exception messages are
+ * available from ScriptException.
+ */
+
+import java.io.*;
+import javax.script.*;
+
+public class ExceptionTest {
+    private static final String ERROR_MSG = "error from JavaScript";
+
+    public static void main(String[] args) throws Exception {
+        ScriptEngineManager m = new ScriptEngineManager();
+        ScriptEngine engine = Helper.getJsEngine(m);
+        if (engine == null) {
+            System.out.println("Warning: No js engine found; test vacuously passes.");
+            return;
+        }
+        engine.put("msg", ERROR_MSG);
+        try {
+            engine.eval("throw new Error(msg);");
+        } catch (ScriptException exp) {
+            if (exp.getMessage().indexOf(ERROR_MSG) == -1) {
+                throw exp;
+            }
+        }
+        try {
+            engine.eval("throw (msg);");
+        } catch (ScriptException exp) {
+            if (exp.getMessage().indexOf(ERROR_MSG) == -1) {
+                throw exp;
+            }
+        }
+        try {
+            CompiledScript scr = ((Compilable)engine).compile("throw new Error(msg);");
+            scr.eval();
+        } catch (ScriptException exp) {
+            if (exp.getMessage().indexOf(ERROR_MSG) == -1) {
+                throw exp;
+            }
+        }
+        try {
+            CompiledScript scr = ((Compilable)engine).compile("throw msg;");
+            scr.eval();
+        } catch (ScriptException exp) {
+            if (exp.getMessage().indexOf(ERROR_MSG) == -1) {
+                throw exp;
+            }
+        }
+    }
+}
--- a/test/javax/script/GetInterfaceTest.java	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/GetInterfaceTest.java	Wed Feb 27 17:22:44 2013 +0530
@@ -22,6 +22,7 @@
  */
 
 /*
+ * @run ignore
  * @test
  * @bug 6960211
  * @summary JavaScript engine allows creation of interface although methods not available.
@@ -32,10 +33,10 @@
 public class GetInterfaceTest {
     public static void main(String[] args) throws Exception {
         ScriptEngineManager manager = new ScriptEngineManager();
-        ScriptEngine engine = manager.getEngineByName("js");
+        ScriptEngine engine = manager.getEngineByName("nashorn");
 
         if (engine == null) {
-            System.out.println("Warning: No engine engine found; test vacuously passes.");
+            System.out.println("Warning: No js engine engine found; test vacuously passes.");
             return;
         }
 
--- a/test/javax/script/Helper.java	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/Helper.java	Wed Feb 27 17:22:44 2013 +0530
@@ -24,13 +24,13 @@
 
 /**
  * Helper class to consolidate testing requirements for a js engine.
- * A js engine is required as part of Sun's product JDK.
+ * A js engine is required as part of Oracle's product JDK.
  */
 public class Helper {
     private Helper() {}; // Don't instantiate
 
     public static ScriptEngine getJsEngine(ScriptEngineManager m) {
-        ScriptEngine e  = m.getEngineByName("js");
+        ScriptEngine e  = m.getEngineByName("nashorn");
         if (e == null &&
             System.getProperty("java.runtime.name").startsWith("Java(TM)")) {
             // A js engine is requied for Sun's product JDK
--- a/test/javax/script/RhinoExceptionTest.java	Tue Feb 26 17:38:29 2013 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-/*
- * Copyright (c) 2006, 2008, 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.
- */
-
-/*
- * @test
- * @bug 6474943 6705893
- * @summary Test that Rhino exception messages are
- * available from ScriptException.
- */
-
-import java.io.*;
-import javax.script.*;
-
-public class RhinoExceptionTest {
-    private static final String ERROR_MSG = "error from JavaScript";
-
-    public static void main(String[] args) throws Exception {
-        ScriptEngineManager m = new ScriptEngineManager();
-        ScriptEngine engine = Helper.getJsEngine(m);
-        if (engine == null) {
-            System.out.println("Warning: No js engine found; test vacuously passes.");
-            return;
-        }
-        engine.put("msg", ERROR_MSG);
-        try {
-            engine.eval("throw new Error(msg);");
-        } catch (ScriptException exp) {
-            if (exp.getMessage().indexOf(ERROR_MSG) == -1) {
-                throw exp;
-            }
-        }
-        try {
-            engine.eval("throw (msg);");
-        } catch (ScriptException exp) {
-            if (exp.getMessage().indexOf(ERROR_MSG) == -1) {
-                throw exp;
-            }
-        }
-        try {
-            CompiledScript scr = ((Compilable)engine).compile("throw new Error(msg);");
-            scr.eval();
-        } catch (ScriptException exp) {
-            if (exp.getMessage().indexOf(ERROR_MSG) == -1) {
-                throw exp;
-            }
-        }
-        try {
-            CompiledScript scr = ((Compilable)engine).compile("throw msg;");
-            scr.eval();
-        } catch (ScriptException exp) {
-            if (exp.getMessage().indexOf(ERROR_MSG) == -1) {
-                throw exp;
-            }
-        }
-    }
-}
--- a/test/javax/script/StringWriterPrintTest.java	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/StringWriterPrintTest.java	Wed Feb 27 17:22:44 2013 +0530
@@ -33,9 +33,9 @@
 public class StringWriterPrintTest {
     public static void main(String[] args) throws ScriptException {
         ScriptEngineManager sem = new ScriptEngineManager();
-        ScriptEngine engine = sem.getEngineByName("js");
+        ScriptEngine engine = sem.getEngineByName("nashorn");
         if (engine == null) {
-            System.out.println("Warning: No js engine found; test vacuously passes.");
+            System.out.println("Warning: No nashorn engine found; test vacuously passes.");
             return;
         }
         StringWriter sw = new StringWriter();
--- a/test/javax/script/Test3.js	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/Test3.js	Wed Feb 27 17:22:44 2013 +0530
@@ -1,22 +1,24 @@
+var ScriptContext = javax.script.ScriptContext;
+
 if (key == undefined || key != 'engine value') {
     throw "unexpected engine scope value";
 }
 
 // pre-defined context variable refers to current ScriptContext
-if (context.getAttribute('key', context.GLOBAL_SCOPE) != 'global value') {
+if (context.getAttribute('key', ScriptContext.GLOBAL_SCOPE) != 'global value') {
     throw "unexpected global scope value";
 }
 
 // change the engine scope value
 key = 'new engine value';
 
-if (context.getAttribute('key', context.GLOBAL_SCOPE) != 'global value') {
+if (context.getAttribute('key', ScriptContext.GLOBAL_SCOPE) != 'global value') {
     throw "global scope should not change here";
 }
 
 // delete engine scope value
 delete key;
 
-if (key == undefined && key != 'xglobal value') {
+if (key == undefined && key != 'global value') {
     throw 'global scope should be visible after engine scope removal';
 }
--- a/test/javax/script/Test5.java	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/Test5.java	Wed Feb 27 17:22:44 2013 +0530
@@ -48,16 +48,24 @@
                 System.out.println("engine scope only");
                 e.put("count", new Integer(1));
 
-                Reader reader = new FileReader(
-                    new File(System.getProperty("test.src", "."), "Test5.js"));
-                engine.eval(reader,ctxt);
+                try (Reader reader = new FileReader(
+                    new File(System.getProperty("test.src", "."), "Test5.js"))) {
+                    engine.eval(reader,ctxt);
+                }
+
                 System.out.println("both scopes");
                 ctxt.setBindings(g, ScriptContext.GLOBAL_SCOPE);
                 e.put("count", new Integer(2));
-                engine.eval(reader,ctxt);
+                try (Reader reader = new FileReader(
+                    new File(System.getProperty("test.src", "."), "Test5.js"))) {
+                    engine.eval(reader,ctxt);
+                }
                 System.out.println("only global");
                 e.put("count", new Integer(3));
-                ctxt.setAttribute("key", null, ScriptContext.ENGINE_SCOPE);
-                engine.eval(reader,ctxt);
+                ctxt.removeAttribute("key", ScriptContext.ENGINE_SCOPE);
+                try (Reader reader = new FileReader(
+                    new File(System.getProperty("test.src", "."), "Test5.js"))) {
+                    engine.eval(reader,ctxt);
+                }
         }
 }
--- a/test/javax/script/Test5.js	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/Test5.js	Wed Feb 27 17:22:44 2013 +0530
@@ -1,6 +1,5 @@
-var key;
-var count;
 
+var ScriptContext = javax.script.ScriptContext;
 print(count);
 
 switch (count) {
@@ -9,7 +8,7 @@
             if (key != 'value in engine') {
                 throw "unexpected engine scope value";
             }
-            if (context.getAttribute("key", context.GLOBAL_SCOPE ) != null) {
+            if (context.getAttribute("key", ScriptContext.GLOBAL_SCOPE ) != null) {
                 throw "unexpected global scope value";
             }
             break;
@@ -19,7 +18,7 @@
             if (key != 'value in engine') {
                 throw "unexpected engine scope value";
             }
-            if (context.getAttribute("key", context.GLOBAL_SCOPE ) != 
+            if (context.getAttribute("key", ScriptContext.GLOBAL_SCOPE ) != 
                 "value in global") {
                 throw "unexpected global scope value";
             }
@@ -30,7 +29,7 @@
             if (key != 'value in global') {
                 throw "unexpected global scope value";
             }
-            if (context.getAttribute("key", context.GLOBAL_SCOPE ) != 
+            if (context.getAttribute("key", ScriptContext.GLOBAL_SCOPE ) != 
                 "value in global") {
                 throw "unexpected global scope value";
             }
--- a/test/javax/script/Test6.java	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/Test6.java	Wed Feb 27 17:22:44 2013 +0530
@@ -40,11 +40,23 @@
                 System.out.println("Warning: No js engine found; test vacuously passes.");
                 return;
             }
-            Reader reader = new FileReader(
-                new File(System.getProperty("test.src", "."), "Test6.js"));
-            engine.eval(reader);
+
+            try (Reader reader = new FileReader(
+                new File(System.getProperty("test.src", "."), "Test6.js"))) {
+                engine.eval(reader);
+            }
             Object res = engine.get("res");
-            CompiledScript scr = ((Compilable)engine).compile(reader);
+
+            CompiledScript scr = null;
+            try (Reader reader = new FileReader(
+                new File(System.getProperty("test.src", "."), "Test6.js"))) {
+                scr = ((Compilable)engine).compile(reader);
+            }
+
+            if (scr == null) {
+                throw new RuntimeException("compilation failed!");
+            }
+
             scr.eval();
             Object res1 = engine.get("res");
             if (! res.equals(res1)) {
--- a/test/javax/script/Test7.js	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/Test7.js	Wed Feb 27 17:22:44 2013 +0530
@@ -1,9 +1,14 @@
 //this is the first line of Test7.js
 var filename;
+try {
+    load("nashorn:mozilla_compat.js");
+} catch (e) {
+    //ignored
+}
 importPackage(java.io);
 importPackage(java);
 var f = new File(filename);
 var r = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
 
-var firstLine = r.readLine() + '';
+var firstLine = r.readLine();
 print(firstLine);
--- a/test/javax/script/UnescapedBracketRegExTest.java	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/UnescapedBracketRegExTest.java	Wed Feb 27 17:22:44 2013 +0530
@@ -24,7 +24,7 @@
 /*
  * @test
  * @bug 7012701
- * @summary 7012701 Add a test to check that Rhino's RegExp parser accepts unescaped '['
+ * @summary 7012701 Add a test to check that RegExp parser accepts unescaped '['
  */
 
 import javax.script.*;
@@ -33,9 +33,9 @@
 public class UnescapedBracketRegExTest {
     public static void main(String[] args) throws ScriptException {
         ScriptEngineManager sem = new ScriptEngineManager();
-        ScriptEngine engine = sem.getEngineByName("js");
+        ScriptEngine engine = sem.getEngineByName("nashorn");
         if (engine == null) {
-            System.out.println("Warning: No js engine found; test vacuously passes.");
+            System.out.println("Warning: No nashorn engine found; test vacuously passes.");
             return;
         }
         // the following throws exception
--- a/test/javax/script/VersionTest.java	Tue Feb 26 17:38:29 2013 -0800
+++ b/test/javax/script/VersionTest.java	Wed Feb 27 17:22:44 2013 +0530
@@ -31,9 +31,7 @@
 import java.io.*;
 
 public class VersionTest  {
-
-        private static final String JS_LANG_VERSION = "1.8";
-        private static final String JS_ENGINE_VERSION = "1.7 release 3 PRERELEASE";
+        private static final String JS_LANG_VERSION = "ECMA - 262 Edition 5.1";
 
         public static void main(String[] args) throws Exception {
             ScriptEngineManager manager = new ScriptEngineManager();
@@ -48,9 +46,18 @@
                             JS_LANG_VERSION);
             }
             String engineVersion = jsengine.getFactory().getEngineVersion();
-            if (! engineVersion.equals(JS_ENGINE_VERSION)) {
-                throw new RuntimeException("Expected Rhino version is " +
-                            JS_ENGINE_VERSION);
+            String expectedVersion = getNashornVersion();
+            if (! engineVersion.equals(expectedVersion)) {
+                throw new RuntimeException("Expected version is " + expectedVersion);
+            }
+        }
+
+        private static String getNashornVersion() {
+            try {
+                Class versionClass = Class.forName("jdk.nashorn.internal.runtime.Version");
+                return (String) versionClass.getMethod("version").invoke(null);
+            } catch (Exception e) {
+                return "Version Unknown!";
             }
         }
 }