Mon, 06 Apr 2015 16:18:54 +0530
8076646: nashorn tests should avoid using package names used by nashorn sources
Reviewed-by: hannesw, lagergren
1.1 --- a/make/build.xml Wed Apr 01 13:22:52 2015 -0700 1.2 +++ b/make/build.xml Mon Apr 06 16:18:54 2015 +0530 1.3 @@ -282,12 +282,12 @@ 1.4 <fileset dir="${test.src.dir}/META-INF/services/"/> 1.5 </copy> 1.6 1.7 - <copy todir="${build.test.classes.dir}/jdk/nashorn/internal/runtime/resources"> 1.8 - <fileset dir="${test.src.dir}/jdk/nashorn/internal/runtime/resources"/> 1.9 + <copy todir="${build.test.classes.dir}/jdk/nashorn/internal/runtime/test/resources"> 1.10 + <fileset dir="${test.src.dir}/jdk/nashorn/internal/runtime/test/resources"/> 1.11 </copy> 1.12 1.13 - <copy todir="${build.test.classes.dir}/jdk/nashorn/api/scripting/resources"> 1.14 - <fileset dir="${test.src.dir}/jdk/nashorn/api/scripting/resources"/> 1.15 + <copy todir="${build.test.classes.dir}/jdk/nashorn/api/scripting/test/resources"> 1.16 + <fileset dir="${test.src.dir}/jdk/nashorn/api/scripting/test/resources"/> 1.17 </copy> 1.18 1.19 <!-- tests that check nashorn internals and internal API -->
2.1 --- a/test/script/sandbox/interfaceimpl.js Wed Apr 01 13:22:52 2015 -0700 2.2 +++ b/test/script/sandbox/interfaceimpl.js Mon Apr 06 16:18:54 2015 +0530 2.3 @@ -29,8 +29,8 @@ 2.4 * @security 2.5 */ 2.6 2.7 -var Window = Java.type("jdk.nashorn.api.scripting.Window"); 2.8 -var WindowEventHandler = Java.type("jdk.nashorn.api.scripting.WindowEventHandler"); 2.9 +var Window = Java.type("jdk.nashorn.api.scripting.test.Window"); 2.10 +var WindowEventHandler = Java.type("jdk.nashorn.api.scripting.test.WindowEventHandler"); 2.11 2.12 var w = new Window(); 2.13
3.1 --- a/test/script/trusted/JDK-8025629.js Wed Apr 01 13:22:52 2015 -0700 3.2 +++ b/test/script/trusted/JDK-8025629.js Mon Apr 06 16:18:54 2015 +0530 3.3 @@ -28,6 +28,6 @@ 3.4 * @run 3.5 */ 3.6 3.7 -load("classpath:jdk/nashorn/internal/runtime/resources/load_test.js") 3.8 +load("classpath:jdk/nashorn/internal/runtime/test/resources/load_test.js") 3.9 3.10 Assert.assertEquals(loadedFunc("hello"), "HELLO");
4.1 --- a/test/src/META-INF/services/java.sql.Driver Wed Apr 01 13:22:52 2015 -0700 4.2 +++ b/test/src/META-INF/services/java.sql.Driver Mon Apr 06 16:18:54 2015 +0530 4.3 @@ -1,1 +1,1 @@ 4.4 -jdk.nashorn.api.NashornSQLDriver 4.5 +jdk.nashorn.api.test.NashornSQLDriver
5.1 --- a/test/src/jdk/internal/dynalink/beans/CallerSensitiveTest.java Wed Apr 01 13:22:52 2015 -0700 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,37 +0,0 @@ 5.4 -/* 5.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 5.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 - * 5.8 - * This code is free software; you can redistribute it and/or modify it 5.9 - * under the terms of the GNU General Public License version 2 only, as 5.10 - * published by the Free Software Foundation. Oracle designates this 5.11 - * particular file as subject to the "Classpath" exception as provided 5.12 - * by Oracle in the LICENSE file that accompanied this code. 5.13 - * 5.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 5.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.17 - * version 2 for more details (a copy is included in the LICENSE file that 5.18 - * accompanied this code). 5.19 - * 5.20 - * You should have received a copy of the GNU General Public License version 5.21 - * 2 along with this work; if not, write to the Free Software Foundation, 5.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.23 - * 5.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.25 - * or visit www.oracle.com if you need additional information or have any 5.26 - * questions. 5.27 - */ 5.28 - 5.29 -package jdk.internal.dynalink.beans; 5.30 - 5.31 -import jdk.nashorn.test.models.ClassLoaderAware; 5.32 -import org.testng.annotations.Test; 5.33 - 5.34 -@SuppressWarnings("javadoc") 5.35 -public class CallerSensitiveTest { 5.36 - @Test 5.37 - public void testCallerSensitive() { 5.38 - BeansLinker.getLinkerForClass(ClassLoaderAware.class); 5.39 - } 5.40 -}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/src/jdk/internal/dynalink/beans/test/CallerSensitiveTest.java Mon Apr 06 16:18:54 2015 +0530 6.3 @@ -0,0 +1,38 @@ 6.4 +/* 6.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. Oracle designates this 6.11 + * particular file as subject to the "Classpath" exception as provided 6.12 + * by Oracle in the LICENSE file that accompanied this code. 6.13 + * 6.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.17 + * version 2 for more details (a copy is included in the LICENSE file that 6.18 + * accompanied this code). 6.19 + * 6.20 + * You should have received a copy of the GNU General Public License version 6.21 + * 2 along with this work; if not, write to the Free Software Foundation, 6.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.23 + * 6.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.25 + * or visit www.oracle.com if you need additional information or have any 6.26 + * questions. 6.27 + */ 6.28 + 6.29 +package jdk.internal.dynalink.beans.test; 6.30 + 6.31 +import jdk.internal.dynalink.beans.BeansLinker; 6.32 +import jdk.nashorn.test.models.ClassLoaderAware; 6.33 +import org.testng.annotations.Test; 6.34 + 6.35 +@SuppressWarnings("javadoc") 6.36 +public class CallerSensitiveTest { 6.37 + @Test 6.38 + public void testCallerSensitive() { 6.39 + BeansLinker.getLinkerForClass(ClassLoaderAware.class); 6.40 + } 6.41 +}
7.1 --- a/test/src/jdk/nashorn/api/NashornSQLDriver.java Wed Apr 01 13:22:52 2015 -0700 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,84 +0,0 @@ 7.4 -/* 7.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 7.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 - * 7.8 - * This code is free software; you can redistribute it and/or modify it 7.9 - * under the terms of the GNU General Public License version 2 only, as 7.10 - * published by the Free Software Foundation. Oracle designates this 7.11 - * particular file as subject to the "Classpath" exception as provided 7.12 - * by Oracle in the LICENSE file that accompanied this code. 7.13 - * 7.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 7.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.17 - * version 2 for more details (a copy is included in the LICENSE file that 7.18 - * accompanied this code). 7.19 - * 7.20 - * You should have received a copy of the GNU General Public License version 7.21 - * 2 along with this work; if not, write to the Free Software Foundation, 7.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.23 - * 7.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.25 - * or visit www.oracle.com if you need additional information or have any 7.26 - * questions. 7.27 - */ 7.28 - 7.29 -package jdk.nashorn.api; 7.30 - 7.31 -import java.sql.Connection; 7.32 -import java.sql.Driver; 7.33 -import java.sql.DriverManager; 7.34 -import java.sql.DriverPropertyInfo; 7.35 -import java.sql.SQLException; 7.36 -import java.sql.SQLFeatureNotSupportedException; 7.37 -import java.util.Properties; 7.38 -import java.util.logging.Logger; 7.39 - 7.40 -/** 7.41 - * A dummy SQL driver for testing purpose. 7.42 - */ 7.43 -public final class NashornSQLDriver implements Driver { 7.44 - static { 7.45 - try { 7.46 - DriverManager.registerDriver(new NashornSQLDriver(), null); 7.47 - } catch (final SQLException se) { 7.48 - throw new RuntimeException(se); 7.49 - } 7.50 - } 7.51 - 7.52 - @Override 7.53 - public boolean acceptsURL(final String url) { 7.54 - return url.startsWith("jdbc:nashorn:"); 7.55 - } 7.56 - 7.57 - @Override 7.58 - public Connection connect(final String url, final Properties info) { 7.59 - throw new UnsupportedOperationException("I am a dummy!!"); 7.60 - } 7.61 - 7.62 - @Override 7.63 - public int getMajorVersion() { 7.64 - return -1; 7.65 - } 7.66 - 7.67 - @Override 7.68 - public int getMinorVersion() { 7.69 - return -1; 7.70 - } 7.71 - 7.72 - @Override 7.73 - public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) { 7.74 - return new DriverPropertyInfo[0]; 7.75 - } 7.76 - 7.77 - @Override 7.78 - public boolean jdbcCompliant() { 7.79 - // no way! 7.80 - return false; 7.81 - } 7.82 - 7.83 - @Override 7.84 - public Logger getParentLogger() throws SQLFeatureNotSupportedException { 7.85 - throw new SQLFeatureNotSupportedException(); 7.86 - } 7.87 -}
8.1 --- a/test/src/jdk/nashorn/api/javaaccess/ArrayConversionTest.java Wed Apr 01 13:22:52 2015 -0700 8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 8.3 @@ -1,235 +0,0 @@ 8.4 -/* 8.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 8.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 - * 8.8 - * This code is free software; you can redistribute it and/or modify it 8.9 - * under the terms of the GNU General Public License version 2 only, as 8.10 - * published by the Free Software Foundation. Oracle designates this 8.11 - * particular file as subject to the "Classpath" exception as provided 8.12 - * by Oracle in the LICENSE file that accompanied this code. 8.13 - * 8.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 8.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.17 - * version 2 for more details (a copy is included in the LICENSE file that 8.18 - * accompanied this code). 8.19 - * 8.20 - * You should have received a copy of the GNU General Public License version 8.21 - * 2 along with this work; if not, write to the Free Software Foundation, 8.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.23 - * 8.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.25 - * or visit www.oracle.com if you need additional information or have any 8.26 - * questions. 8.27 - */ 8.28 - 8.29 -package jdk.nashorn.api.javaaccess; 8.30 - 8.31 -import static org.testng.AssertJUnit.assertEquals; 8.32 -import static org.testng.AssertJUnit.assertFalse; 8.33 -import static org.testng.AssertJUnit.assertNull; 8.34 -import static org.testng.AssertJUnit.assertTrue; 8.35 -import java.util.Arrays; 8.36 -import java.util.List; 8.37 -import javax.script.ScriptContext; 8.38 -import javax.script.ScriptEngine; 8.39 -import javax.script.ScriptEngineManager; 8.40 -import javax.script.ScriptException; 8.41 -import org.testng.TestNG; 8.42 -import org.testng.annotations.AfterClass; 8.43 -import org.testng.annotations.BeforeClass; 8.44 -import org.testng.annotations.Test; 8.45 - 8.46 -@SuppressWarnings("javadoc") 8.47 -public class ArrayConversionTest { 8.48 - private static ScriptEngine e = null; 8.49 - 8.50 - public static void main(final String[] args) { 8.51 - TestNG.main(args); 8.52 - } 8.53 - 8.54 - @BeforeClass 8.55 - public static void setUpClass() { 8.56 - e = new ScriptEngineManager().getEngineByName("nashorn"); 8.57 - } 8.58 - 8.59 - @AfterClass 8.60 - public static void tearDownClass() { 8.61 - e = null; 8.62 - } 8.63 - 8.64 - @Test 8.65 - public void testIntArrays() throws ScriptException { 8.66 - runTest("assertNullIntArray", "null"); 8.67 - runTest("assertEmptyIntArray", "[]"); 8.68 - runTest("assertSingle42IntArray", "[42]"); 8.69 - runTest("assertSingle42IntArray", "['42']"); 8.70 - runTest("assertIntArrayConversions", "[false, true, NaN, Infinity, -Infinity, 0.4, 0.6, null, undefined, [], {}, [1], [1, 2]]"); 8.71 - } 8.72 - 8.73 - @Test 8.74 - public void testIntIntArrays() throws ScriptException { 8.75 - runTest("assertNullIntIntArray", "null"); 8.76 - runTest("assertEmptyIntIntArray", "[]"); 8.77 - runTest("assertSingleEmptyIntIntArray", "[[]]"); 8.78 - runTest("assertSingleNullIntIntArray", "[null]"); 8.79 - runTest("assertLargeIntIntArray", "[[false], [1], [2, 3], [4, 5, 6], ['7', {valueOf: function() { return 8 }}]]"); 8.80 - } 8.81 - 8.82 - @Test 8.83 - public void testObjectObjectArrays() throws ScriptException { 8.84 - runTest("assertLargeObjectObjectArray", "[[false], [1], ['foo', 42.3], [{x: 17}]]"); 8.85 - } 8.86 - 8.87 - @Test 8.88 - public void testBooleanArrays() throws ScriptException { 8.89 - runTest("assertBooleanArrayConversions", "[false, true, '', 'false', 0, 1, 0.4, 0.6, {}, [], [false], [true], NaN, Infinity, null, undefined]"); 8.90 - } 8.91 - 8.92 - @Test 8.93 - public void testArrayAmbiguity() throws ScriptException { 8.94 - runTest("x", "'abc'"); 8.95 - runTest("x", "['foo', 'bar']"); 8.96 - } 8.97 - 8.98 - @Test 8.99 - public void testListArrays() throws ScriptException { 8.100 - runTest("assertListArray", "[['foo', 'bar'], ['apple', 'orange']]"); 8.101 - } 8.102 - 8.103 - @Test 8.104 - public void testVarArgs() throws ScriptException { 8.105 - // Sole NativeArray in vararg position becomes vararg array itself 8.106 - runTest("assertVarArg_42_17", "[42, 17]"); 8.107 - // NativeArray in vararg position becomes an argument if there are more arguments 8.108 - runTest("assertVarArg_array_17", "[42], 18"); 8.109 - // Only NativeArray is converted to vararg array, other objects (e.g. a function) aren't 8.110 - runTest("assertVarArg_function", "function() { return 'Hello' }"); 8.111 - } 8.112 - 8.113 - private static void runTest(final String testMethodName, final String argument) throws ScriptException { 8.114 - e.eval("Java.type('" + ArrayConversionTest.class.getName() + "')." + testMethodName + "(" + argument + ")"); 8.115 - } 8.116 - 8.117 - public static void assertNullIntArray(final int[] array) { 8.118 - assertNull(array); 8.119 - } 8.120 - 8.121 - public static void assertNullIntIntArray(final int[][] array) { 8.122 - assertNull(array); 8.123 - } 8.124 - 8.125 - public static void assertEmptyIntArray(final int[] array) { 8.126 - assertEquals(0, array.length); 8.127 - } 8.128 - 8.129 - public static void assertSingle42IntArray(final int[] array) { 8.130 - assertEquals(1, array.length); 8.131 - assertEquals(42, array[0]); 8.132 - } 8.133 - 8.134 - 8.135 - public static void assertIntArrayConversions(final int[] array) { 8.136 - assertEquals(13, array.length); 8.137 - assertEquals(0, array[0]); // false 8.138 - assertEquals(1, array[1]); // true 8.139 - assertEquals(0, array[2]); // NaN 8.140 - assertEquals(0, array[3]); // Infinity 8.141 - assertEquals(0, array[4]); // -Infinity 8.142 - assertEquals(0, array[5]); // 0.4 8.143 - assertEquals(0, array[6]); // 0.6 - floor, not round 8.144 - assertEquals(0, array[7]); // null 8.145 - assertEquals(0, array[8]); // undefined 8.146 - assertEquals(0, array[9]); // [] 8.147 - assertEquals(0, array[10]); // {} 8.148 - assertEquals(1, array[11]); // [1] 8.149 - assertEquals(0, array[12]); // [1, 2] 8.150 - } 8.151 - 8.152 - public static void assertEmptyIntIntArray(final int[][] array) { 8.153 - assertEquals(0, array.length); 8.154 - } 8.155 - 8.156 - public static void assertSingleEmptyIntIntArray(final int[][] array) { 8.157 - assertEquals(1, array.length); 8.158 - assertTrue(Arrays.equals(new int[0], array[0])); 8.159 - } 8.160 - 8.161 - public static void assertSingleNullIntIntArray(final int[][] array) { 8.162 - assertEquals(1, array.length); 8.163 - assertNull(null, array[0]); 8.164 - } 8.165 - 8.166 - public static void assertLargeIntIntArray(final int[][] array) { 8.167 - assertEquals(5, array.length); 8.168 - assertTrue(Arrays.equals(new int[] { 0 }, array[0])); 8.169 - assertTrue(Arrays.equals(new int[] { 1 }, array[1])); 8.170 - assertTrue(Arrays.equals(new int[] { 2, 3 }, array[2])); 8.171 - assertTrue(Arrays.equals(new int[] { 4, 5, 6 }, array[3])); 8.172 - assertTrue(Arrays.equals(new int[] { 7, 8 }, array[4])); 8.173 - } 8.174 - 8.175 - public static void assertLargeObjectObjectArray(final Object[][] array) throws ScriptException { 8.176 - assertEquals(4, array.length); 8.177 - assertTrue(Arrays.equals(new Object[] { Boolean.FALSE }, array[0])); 8.178 - assertTrue(Arrays.equals(new Object[] { 1 }, array[1])); 8.179 - assertTrue(Arrays.equals(new Object[] { "foo", 42.3d }, array[2])); 8.180 - assertEquals(1, array[3].length); 8.181 - e.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", array[3][0]); 8.182 - assertEquals(17, e.eval("obj.x")); 8.183 - } 8.184 - 8.185 - public static void assertBooleanArrayConversions(final boolean[] array) { 8.186 - assertEquals(16, array.length); 8.187 - assertFalse(array[0]); // false 8.188 - assertTrue(array[1]); // true 8.189 - assertFalse(array[2]); // '' 8.190 - assertTrue(array[3]); // 'false' (yep, every non-empty string converts to true) 8.191 - assertFalse(array[4]); // 0 8.192 - assertTrue(array[5]); // 1 8.193 - assertTrue(array[6]); // 0.4 8.194 - assertTrue(array[7]); // 0.6 8.195 - assertTrue(array[8]); // {} 8.196 - assertTrue(array[9]); // [] 8.197 - assertTrue(array[10]); // [false] 8.198 - assertTrue(array[11]); // [true] 8.199 - assertFalse(array[12]); // NaN 8.200 - assertTrue(array[13]); // Infinity 8.201 - assertFalse(array[14]); // null 8.202 - assertFalse(array[15]); // undefined 8.203 - } 8.204 - 8.205 - public static void assertListArray(final List<?>[] array) { 8.206 - assertEquals(2, array.length); 8.207 - assertEquals(Arrays.asList("foo", "bar"), array[0]); 8.208 - assertEquals(Arrays.asList("apple", "orange"), array[1]); 8.209 - } 8.210 - 8.211 - public static void assertVarArg_42_17(final Object... args) { 8.212 - assertEquals(2, args.length); 8.213 - assertEquals(42, ((Number)args[0]).intValue()); 8.214 - assertEquals(17, ((Number)args[1]).intValue()); 8.215 - } 8.216 - 8.217 - public static void assertVarArg_array_17(final Object... args) throws ScriptException { 8.218 - assertEquals(2, args.length); 8.219 - e.getBindings(ScriptContext.ENGINE_SCOPE).put("arr", args[0]); 8.220 - assertTrue((Boolean)e.eval("arr instanceof Array && arr.length == 1 && arr[0] == 42")); 8.221 - assertEquals(18, ((Number)args[1]).intValue()); 8.222 - } 8.223 - 8.224 - public static void assertVarArg_function(final Object... args) throws ScriptException { 8.225 - assertEquals(1, args.length); 8.226 - e.getBindings(ScriptContext.ENGINE_SCOPE).put("fn", args[0]); 8.227 - assertEquals("Hello", e.eval("fn()")); 8.228 - } 8.229 - 8.230 - 8.231 - 8.232 - public static void x(final String y) { 8.233 - assertEquals("abc", y); 8.234 - } 8.235 - public static void x(final String[] y) { 8.236 - assertTrue(Arrays.equals(new String[] { "foo", "bar"}, y)); 8.237 - } 8.238 -}
9.1 --- a/test/src/jdk/nashorn/api/javaaccess/BooleanAccessTest.java Wed Apr 01 13:22:52 2015 -0700 9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 9.3 @@ -1,219 +0,0 @@ 9.4 -/* 9.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 9.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.7 - * 9.8 - * This code is free software; you can redistribute it and/or modify it 9.9 - * under the terms of the GNU General Public License version 2 only, as 9.10 - * published by the Free Software Foundation. Oracle designates this 9.11 - * particular file as subject to the "Classpath" exception as provided 9.12 - * by Oracle in the LICENSE file that accompanied this code. 9.13 - * 9.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 9.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 9.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 9.17 - * version 2 for more details (a copy is included in the LICENSE file that 9.18 - * accompanied this code). 9.19 - * 9.20 - * You should have received a copy of the GNU General Public License version 9.21 - * 2 along with this work; if not, write to the Free Software Foundation, 9.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 9.23 - * 9.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 9.25 - * or visit www.oracle.com if you need additional information or have any 9.26 - * questions. 9.27 - */ 9.28 - 9.29 -package jdk.nashorn.api.javaaccess; 9.30 - 9.31 -import static org.testng.AssertJUnit.assertEquals; 9.32 -import static org.testng.AssertJUnit.assertTrue; 9.33 -import java.util.Arrays; 9.34 -import javax.script.ScriptEngine; 9.35 -import javax.script.ScriptEngineManager; 9.36 -import javax.script.ScriptException; 9.37 -import org.testng.TestNG; 9.38 -import org.testng.annotations.AfterClass; 9.39 -import org.testng.annotations.BeforeClass; 9.40 -import org.testng.annotations.Test; 9.41 - 9.42 -/** 9.43 - * @test 9.44 - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.BooleanAccessTest 9.45 - * @run testng/othervm jdk.nashorn.api.javaaccess.BooleanAccessTest 9.46 - */ 9.47 -@SuppressWarnings("javadoc") 9.48 -public class BooleanAccessTest { 9.49 - 9.50 - private static ScriptEngine e = null; 9.51 - private static SharedObject o = null; 9.52 - 9.53 - public static void main(final String[] args) { 9.54 - TestNG.main(args); 9.55 - } 9.56 - 9.57 - @BeforeClass 9.58 - public static void setUpClass() throws ScriptException { 9.59 - final ScriptEngineManager m = new ScriptEngineManager(); 9.60 - e = m.getEngineByName("nashorn"); 9.61 - o = new SharedObject(); 9.62 - e.put("o", o); 9.63 - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 9.64 - } 9.65 - 9.66 - @AfterClass 9.67 - public static void tearDownClass() { 9.68 - e = null; 9.69 - o = null; 9.70 - } 9.71 - 9.72 - @Test 9.73 - public void accessFieldBoolean() throws ScriptException { 9.74 - e.eval("var p_boolean = o.publicBoolean;"); 9.75 - assertEquals(o.publicBoolean, e.get("p_boolean")); 9.76 - assertEquals("boolean", e.eval("typeof p_boolean;")); 9.77 - e.eval("o.publicBoolean = false;"); 9.78 - assertEquals(false, o.publicBoolean); 9.79 - } 9.80 - 9.81 - @Test 9.82 - public void accessFieldBooleanArray() throws ScriptException { 9.83 - e.eval("var p_boolean_array = o.publicBooleanArray;"); 9.84 - assertEquals(o.publicBooleanArray[0], e.eval("o.publicBooleanArray[0]")); 9.85 - assertTrue(Arrays.equals(o.publicBooleanArray, (boolean[])e.get("p_boolean_array"))); 9.86 - e.eval("var t_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + 9.87 - "t_boolean_arr[0] = true;" + 9.88 - "t_boolean_arr[1] = false;" + 9.89 - "t_boolean_arr[2] = false;" + 9.90 - "o.publicBooleanArray = t_boolean_arr;"); 9.91 - assertTrue(Arrays.equals(new boolean[] { true, false, false }, o.publicBooleanArray)); 9.92 - e.eval("o.publicBooleanArray[0] = false;"); 9.93 - assertEquals(false, o.publicBooleanArray[0]); 9.94 - } 9.95 - 9.96 - @Test 9.97 - public void accessStaticFieldBoolean() throws ScriptException { 9.98 - e.eval("var ps_boolean = SharedObject.publicStaticBoolean;"); 9.99 - assertEquals(SharedObject.publicStaticBoolean, e.get("ps_boolean")); 9.100 - assertEquals("boolean", e.eval("typeof ps_boolean;")); 9.101 - e.eval("SharedObject.publicStaticBoolean = false;"); 9.102 - assertEquals(false, SharedObject.publicStaticBoolean); 9.103 - } 9.104 - 9.105 - @Test 9.106 - public void accessStaticFieldBooleanArray() throws ScriptException { 9.107 - e.eval("var ps_boolean_array = SharedObject.publicStaticBooleanArray;"); 9.108 - assertEquals(SharedObject.publicStaticBooleanArray[0], e.eval("SharedObject.publicStaticBooleanArray[0]")); 9.109 - assertTrue(Arrays.equals(SharedObject.publicStaticBooleanArray, (boolean[])e.get("ps_boolean_array"))); 9.110 - e.eval("var ts_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + 9.111 - "ts_boolean_arr[0] = true;" + 9.112 - "ts_boolean_arr[1] = false;" + 9.113 - "ts_boolean_arr[2] = true;" + 9.114 - "SharedObject.publicStaticBooleanArray = ts_boolean_arr;"); 9.115 - assertTrue(Arrays.equals(new boolean[] { true, false, true }, SharedObject.publicStaticBooleanArray)); 9.116 - e.eval("SharedObject.publicStaticBooleanArray[0] = false;"); 9.117 - assertEquals(false, SharedObject.publicStaticBooleanArray[0]); 9.118 - } 9.119 - 9.120 - @Test 9.121 - public void accessFinalFieldBoolean() throws ScriptException { 9.122 - e.eval("var pf_boolean = o.publicFinalBoolean;"); 9.123 - assertEquals(o.publicFinalBoolean, e.get("pf_boolean")); 9.124 - assertEquals("boolean", e.eval("typeof pf_boolean;")); 9.125 - e.eval("o.publicFinalBoolean = false;"); 9.126 - assertEquals(true, o.publicFinalBoolean); 9.127 - } 9.128 - 9.129 - @Test 9.130 - public void accessFinalFieldBooleanArray() throws ScriptException { 9.131 - e.eval("var pf_boolean_array = o.publicFinalBooleanArray;"); 9.132 - assertEquals(o.publicFinalBooleanArray[0], e.eval("o.publicFinalBooleanArray[0]")); 9.133 - assertTrue(Arrays.equals(o.publicFinalBooleanArray, (boolean[])e.get("pf_boolean_array"))); 9.134 - e.eval("var tf_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + 9.135 - "tf_boolean_arr[0] = false;" + 9.136 - "tf_boolean_arr[1] = false;" + 9.137 - "tf_boolean_arr[2] = true;" + 9.138 - "o.publicOFinalbjectArray = tf_boolean_arr;"); 9.139 - assertTrue(Arrays.equals(new boolean[] { false, false, true, false }, o.publicFinalBooleanArray)); 9.140 - e.eval("o.publicFinalBooleanArray[0] = true;"); 9.141 - assertEquals(true, o.publicFinalBooleanArray[0]); 9.142 - } 9.143 - 9.144 - @Test 9.145 - public void accessStaticFinalFieldBoolean() throws ScriptException { 9.146 - e.eval("var psf_boolean = SharedObject.publicStaticFinalBoolean;"); 9.147 - assertEquals(SharedObject.publicStaticFinalBoolean, e.get("psf_boolean")); 9.148 - assertEquals("boolean", e.eval("typeof psf_boolean;")); 9.149 - e.eval("SharedObject.publicStaticFinalBoolean = false;"); 9.150 - assertEquals(true, SharedObject.publicStaticFinalBoolean); 9.151 - } 9.152 - 9.153 - @Test 9.154 - public void accessStaticFinalFieldBooleanArray() throws ScriptException { 9.155 - e.eval("var psf_boolean_array = SharedObject.publicStaticFinalBooleanArray;"); 9.156 - assertEquals(SharedObject.publicStaticFinalBooleanArray[0], e.eval("SharedObject.publicStaticFinalBooleanArray[0]")); 9.157 - assertTrue(Arrays.equals(SharedObject.publicStaticFinalBooleanArray, (boolean[])e.get("psf_boolean_array"))); 9.158 - e.eval("var tsf_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + 9.159 - "tsf_boolean_arr[0] = false;" + 9.160 - "tsf_boolean_arr[1] = true;" + 9.161 - "tsf_boolean_arr[2] = false;" + 9.162 - "SharedObject.publicStaticFinalBooleanArray = tsf_boolean_arr;"); 9.163 - assertTrue(Arrays.equals(new boolean[] { false, true, false, false }, SharedObject.publicStaticFinalBooleanArray)); 9.164 - e.eval("SharedObject.publicStaticFinalBooleanArray[0] = true;"); 9.165 - assertEquals(true, SharedObject.publicStaticFinalBooleanArray[0]); 9.166 - } 9.167 - 9.168 - @Test 9.169 - public void accessFieldBooleanBoxing() throws ScriptException { 9.170 - e.eval("var p_boolean_box = o.publicBooleanBox;"); 9.171 - assertEquals(o.publicBooleanBox, e.get("p_boolean_box")); 9.172 - assertEquals("boolean", e.eval("typeof p_boolean_box;")); 9.173 - e.eval("o.publicBooleanBox = false;"); 9.174 - assertEquals(false, (boolean)o.publicBooleanBox); 9.175 - } 9.176 - 9.177 - @Test 9.178 - public void accessStaticFieldBooleanBoxing() throws ScriptException { 9.179 - e.eval("var ps_boolean_box = SharedObject.publicStaticBooleanBox;"); 9.180 - assertEquals(SharedObject.publicStaticBooleanBox, e.get("ps_boolean_box")); 9.181 - assertEquals("boolean", e.eval("typeof ps_boolean_box;")); 9.182 - e.eval("SharedObject.publicStaticBooleanBox = false;"); 9.183 - assertEquals(false, (boolean)SharedObject.publicStaticBooleanBox); 9.184 - } 9.185 - 9.186 - @Test 9.187 - public void accessFinalFieldBooleanBoxing() throws ScriptException { 9.188 - e.eval("var pf_boolean_box = o.publicFinalBooleanBox;"); 9.189 - assertEquals(o.publicFinalBooleanBox, e.get("pf_boolean_box")); 9.190 - assertEquals("boolean", e.eval("typeof pf_boolean_box;")); 9.191 - e.eval("o.publicFinalBooleanBox = false;"); 9.192 - assertEquals(true, (boolean)o.publicFinalBooleanBox); 9.193 - } 9.194 - 9.195 - @Test 9.196 - public void accessStaticFinalFieldBooleanBoxing() throws ScriptException { 9.197 - e.eval("var psf_boolean_box = SharedObject.publicStaticFinalBooleanBox;"); 9.198 - assertEquals(SharedObject.publicStaticFinalBooleanBox, e.get("psf_boolean_box")); 9.199 - assertEquals("boolean", e.eval("typeof psf_boolean_box;")); 9.200 - e.eval("SharedObject.publicStaticFinalBooleanBox = false;"); 9.201 - assertEquals(true, (boolean)SharedObject.publicStaticFinalBooleanBox); 9.202 - } 9.203 - 9.204 - @Test 9.205 - public void accessVolatileField() throws ScriptException { 9.206 - e.eval("var pv_boolean = o.volatileBoolean;"); 9.207 - assertEquals(o.volatileBoolean, e.get("pv_boolean")); 9.208 - assertEquals("boolean", e.eval("typeof pv_boolean;")); 9.209 - e.eval("o.volatileBoolean = false;"); 9.210 - assertEquals(false, o.volatileBoolean); 9.211 - } 9.212 - 9.213 - @Test 9.214 - public void accessTransientField() throws ScriptException { 9.215 - e.eval("var pt_boolean = o.transientBoolean;"); 9.216 - assertEquals(o.transientBoolean, e.get("pt_boolean")); 9.217 - assertEquals("boolean", e.eval("typeof pt_boolean;")); 9.218 - e.eval("o.transientBoolean = false;"); 9.219 - assertEquals(false, o.transientBoolean); 9.220 - } 9.221 - 9.222 -}
10.1 --- a/test/src/jdk/nashorn/api/javaaccess/ConsStringTest.java Wed Apr 01 13:22:52 2015 -0700 10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 10.3 @@ -1,99 +0,0 @@ 10.4 -/* 10.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 10.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 10.7 - * 10.8 - * This code is free software; you can redistribute it and/or modify it 10.9 - * under the terms of the GNU General Public License version 2 only, as 10.10 - * published by the Free Software Foundation. Oracle designates this 10.11 - * particular file as subject to the "Classpath" exception as provided 10.12 - * by Oracle in the LICENSE file that accompanied this code. 10.13 - * 10.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 10.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 10.17 - * version 2 for more details (a copy is included in the LICENSE file that 10.18 - * accompanied this code). 10.19 - * 10.20 - * You should have received a copy of the GNU General Public License version 10.21 - * 2 along with this work; if not, write to the Free Software Foundation, 10.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 10.23 - * 10.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 10.25 - * or visit www.oracle.com if you need additional information or have any 10.26 - * questions. 10.27 - */ 10.28 - 10.29 -package jdk.nashorn.api.javaaccess; 10.30 - 10.31 -import static org.testng.AssertJUnit.assertEquals; 10.32 -import java.util.HashMap; 10.33 -import java.util.Map; 10.34 -import javax.script.Bindings; 10.35 -import javax.script.ScriptContext; 10.36 -import javax.script.ScriptEngine; 10.37 -import javax.script.ScriptEngineManager; 10.38 -import javax.script.ScriptException; 10.39 -import jdk.nashorn.api.scripting.JSObject; 10.40 -import org.testng.TestNG; 10.41 -import org.testng.annotations.AfterClass; 10.42 -import org.testng.annotations.BeforeClass; 10.43 -import org.testng.annotations.Test; 10.44 - 10.45 -@SuppressWarnings("javadoc") 10.46 -public class ConsStringTest { 10.47 - private static ScriptEngine e = null; 10.48 - 10.49 - public static void main(final String[] args) { 10.50 - TestNG.main(args); 10.51 - } 10.52 - 10.53 - @BeforeClass 10.54 - public static void setUpClass() { 10.55 - e = new ScriptEngineManager().getEngineByName("nashorn"); 10.56 - } 10.57 - 10.58 - @AfterClass 10.59 - public static void tearDownClass() { 10.60 - e = null; 10.61 - } 10.62 - 10.63 - @Test 10.64 - public void testConsStringFlattening() throws ScriptException { 10.65 - final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 10.66 - final Map<Object, Object> m = new HashMap<>(); 10.67 - b.put("m", m); 10.68 - e.eval("var x = 'f'; x += 'oo'; var y = 'b'; y += 'ar'; m.put(x, y)"); 10.69 - assertEquals("bar", m.get("foo")); 10.70 - } 10.71 - 10.72 - @Test 10.73 - public void testConsStringFromMirror() throws ScriptException { 10.74 - final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 10.75 - //final Map<Object, Object> m = new HashMap<>(); 10.76 - e.eval("var x = 'f'; x += 'oo'; var obj = {x: x};"); 10.77 - assertEquals("foo", ((JSObject)b.get("obj")).getMember("x")); 10.78 - } 10.79 - 10.80 - @Test 10.81 - public void testArrayConsString() throws ScriptException { 10.82 - final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 10.83 - final ArrayHolder h = new ArrayHolder(); 10.84 - b.put("h", h); 10.85 - e.eval("var x = 'f'; x += 'oo'; h.array = [x];"); 10.86 - assertEquals(1, h.array.length); 10.87 - assertEquals("foo", h.array[0]); 10.88 - } 10.89 - 10.90 - 10.91 - public static class ArrayHolder { 10.92 - private Object[] array; 10.93 - 10.94 - public void setArray(final Object[] array) { 10.95 - this.array = array; 10.96 - } 10.97 - 10.98 - public Object[] getArray() { 10.99 - return array; 10.100 - } 10.101 - } 10.102 -}
11.1 --- a/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java Wed Apr 01 13:22:52 2015 -0700 11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 11.3 @@ -1,466 +0,0 @@ 11.4 -/* 11.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 11.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.7 - * 11.8 - * This code is free software; you can redistribute it and/or modify it 11.9 - * under the terms of the GNU General Public License version 2 only, as 11.10 - * published by the Free Software Foundation. Oracle designates this 11.11 - * particular file as subject to the "Classpath" exception as provided 11.12 - * by Oracle in the LICENSE file that accompanied this code. 11.13 - * 11.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 11.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11.17 - * version 2 for more details (a copy is included in the LICENSE file that 11.18 - * accompanied this code). 11.19 - * 11.20 - * You should have received a copy of the GNU General Public License version 11.21 - * 2 along with this work; if not, write to the Free Software Foundation, 11.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 11.23 - * 11.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 11.25 - * or visit www.oracle.com if you need additional information or have any 11.26 - * questions. 11.27 - */ 11.28 - 11.29 -package jdk.nashorn.api.javaaccess; 11.30 - 11.31 -import static org.testng.AssertJUnit.assertEquals; 11.32 -import static org.testng.AssertJUnit.assertTrue; 11.33 -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; 11.34 -import java.util.Arrays; 11.35 -import java.util.Calendar; 11.36 -import java.util.Locale; 11.37 -import javax.script.ScriptEngine; 11.38 -import javax.script.ScriptEngineManager; 11.39 -import javax.script.ScriptException; 11.40 -import org.testng.TestNG; 11.41 -import org.testng.annotations.AfterClass; 11.42 -import org.testng.annotations.BeforeClass; 11.43 -import org.testng.annotations.Test; 11.44 - 11.45 -/** 11.46 - * @test 11.47 - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.MethodAccessTest 11.48 - * @run testng/othervm jdk.nashorn.api.javaaccess.MethodAccessTest 11.49 - */ 11.50 -@SuppressWarnings("javadoc") 11.51 -public class MethodAccessTest { 11.52 - 11.53 - private static ScriptEngine e = null; 11.54 - private static SharedObject o = null; 11.55 - 11.56 - public static void main(final String[] args) { 11.57 - TestNG.main(args); 11.58 - } 11.59 - 11.60 - @BeforeClass 11.61 - public static void setUpClass() throws ScriptException { 11.62 - final ScriptEngineManager m = new ScriptEngineManager(); 11.63 - e = m.getEngineByName("nashorn"); 11.64 - o = new SharedObject(); 11.65 - o.setEngine(e); 11.66 - e.put("o", o); 11.67 - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 11.68 - e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;"); 11.69 - } 11.70 - 11.71 - @AfterClass 11.72 - public static void tearDownClass() { 11.73 - e = null; 11.74 - o = null; 11.75 - } 11.76 - 11.77 - @Test 11.78 - public void accessMethodthrowsCheckedException() throws ScriptException { 11.79 - e.eval("try {" + 11.80 - " var a = java.lang.Long.parseLong('foo');" + 11.81 - "} catch(e) {" + 11.82 - " var isThrown = true;" + 11.83 - " var isNumberException = e instanceof java.lang.NumberFormatException;" + 11.84 - "} finally {" + 11.85 - " var isFinalized = true;" + 11.86 - "}"); 11.87 - assertEquals("Exception thrown", true, e.get("isThrown")); 11.88 - assertEquals("Finally called", true, e.get("isFinalized")); 11.89 - assertEquals("Type is NumberFormatException", true, e.get("isNumberException")); 11.90 - } 11.91 - 11.92 - @Test 11.93 - public void accessMethodthrowsUnCheckedException() throws ScriptException { 11.94 - e.eval("try {" + 11.95 - " var a = java.lang.String.valueOf(null);" + 11.96 - "} catch(e) {" + 11.97 - " var isThrown = true;" + 11.98 - " var isNumberException = e instanceof java.lang.NullPointerException;" + 11.99 - "} finally {" + 11.100 - " var isFinalized = true;" + 11.101 - "}"); 11.102 - assertEquals(true, e.get("isThrown")); 11.103 - assertEquals(true, e.get("isFinalized")); 11.104 - assertEquals(true, e.get("isNumberException")); 11.105 - } 11.106 - 11.107 - @Test 11.108 - public void accessMethodStartsThread() throws ScriptException { 11.109 - e.eval("o.methodStartsThread();"); 11.110 - assertEquals(false, o.isFinished); 11.111 - } 11.112 - 11.113 - @Test 11.114 - public void accessStaticMethod() throws ScriptException { 11.115 - assertEquals(10, e.eval("java.lang.Math.abs(-10);")); 11.116 - } 11.117 - 11.118 - @Test 11.119 - public void accessSynchronousMethod() throws ScriptException { 11.120 - e.eval("var v = new java.util.Vector();" + "v.add(10);" + "v.add(20);" + "v.add(30);"); 11.121 - assertEquals(10, e.eval("v[0]")); 11.122 - assertEquals(20, e.eval("v[1]")); 11.123 - assertEquals(30, e.eval("v[2]")); 11.124 - assertEquals(3, e.eval("v.size()")); 11.125 - } 11.126 - 11.127 - @Test 11.128 - public void accessStaticSynchronousMethod() throws ScriptException { 11.129 - e.eval("var locales = java.util.Calendar.getAvailableLocales();"); 11.130 - final Locale[] locales = (Locale[])e.get("locales"); 11.131 - assertEquals(locales.length, Calendar.getAvailableLocales().length); 11.132 - } 11.133 - 11.134 - @Test 11.135 - public void accessNativeMethod() throws ScriptException { 11.136 - assertEquals(4.0, e.eval("java.lang.StrictMath.log10(10000);")); 11.137 - } 11.138 - 11.139 - @Test 11.140 - public void accessConstructorOfAbstractClass() throws ScriptException { 11.141 - e.eval("try {" + 11.142 - " var a = new java.util.AbstractList();" + 11.143 - " print('fail');" + 11.144 - "} catch(e) {" + 11.145 - " var isThrown = true;" + 11.146 - "}"); 11.147 - assertEquals(true, e.get("isThrown")); 11.148 - } 11.149 - 11.150 - @Test 11.151 - public void accessMethodVoid() throws ScriptException { 11.152 - o.isAccessed = false; 11.153 - e.eval("o.voidMethod();"); 11.154 - assertTrue(o.isAccessed); 11.155 - } 11.156 - 11.157 - @Test 11.158 - public void accessMethodBoolean() throws ScriptException { 11.159 - assertEquals(true, e.eval("o.booleanMethod(false);")); 11.160 - assertEquals(false, e.eval("o.booleanMethod(true);")); 11.161 - assertEquals(false, e.eval("o.booleanMethod('false');")); 11.162 - assertEquals(true, e.eval("o.booleanMethod('');")); 11.163 - assertEquals(true, e.eval("o.booleanMethod(0);")); 11.164 - } 11.165 - 11.166 - @Test 11.167 - public void accessMethodInt() throws ScriptException { 11.168 - assertEquals(0, e.eval("o.intMethod(0);")); 11.169 - assertEquals(-200, e.eval("o.intMethod(-100);")); 11.170 - assertEquals(0, e.eval("o.intMethod('0');")); 11.171 - assertEquals(-200, e.eval("o.intMethod('-100');")); 11.172 - } 11.173 - 11.174 - @Test 11.175 - public void accessMethodLong() throws ScriptException { 11.176 - assertEquals((long)0, e.eval("o.longMethod(0);")); 11.177 - assertEquals((long)400, e.eval("o.longMethod(200);")); 11.178 - assertEquals((long) 0, e.eval("o.longMethod('0');")); 11.179 - assertEquals((long) 400, e.eval("o.longMethod('200');")); 11.180 - } 11.181 - 11.182 - @Test 11.183 - public void accessMethodByte() throws ScriptException { 11.184 - assertEquals((byte) 0, e.eval("o.byteMethod(0);")); 11.185 - assertEquals((byte) 10, e.eval("o.byteMethod(5);")); 11.186 - assertEquals((byte) 0, e.eval("o.byteMethod('0');")); 11.187 - assertEquals((byte) 10, e.eval("o.byteMethod('5');")); 11.188 - } 11.189 - 11.190 - @Test 11.191 - public void accessMethodShort() throws ScriptException { 11.192 - assertEquals((short)0, e.eval("o.shortMethod(0);")); 11.193 - assertEquals((short)8000, e.eval("o.shortMethod(4000);")); 11.194 - assertEquals((short) 0, e.eval("o.shortMethod('0');")); 11.195 - assertEquals((short) 8000, e.eval("o.shortMethod('4000');")); 11.196 - } 11.197 - 11.198 - @Test 11.199 - public void accessMethodChar() throws ScriptException { 11.200 - assertEquals('A', e.eval("o.charMethod('a');")); 11.201 - assertEquals('Z', e.eval("o.charMethod('z');")); 11.202 - assertEquals(o.charMethod((char)0), e.eval("o.charMethod(0);")); 11.203 - assertEquals(o.charMethod((char)3150), e.eval("o.charMethod(3150);")); 11.204 - } 11.205 - 11.206 - @Test 11.207 - public void accessMethodFloat() throws ScriptException { 11.208 - assertEquals(0.0f, e.eval("o.floatMethod(0.0);")); 11.209 - assertEquals(4.2f, e.eval("o.floatMethod(2.1);")); 11.210 - assertEquals(0.0f, e.eval("o.floatMethod('0.0');")); 11.211 - assertEquals(4.2f, e.eval("o.floatMethod('2.1');")); 11.212 - } 11.213 - 11.214 - @Test 11.215 - public void accessMethodDouble() throws ScriptException { 11.216 - assertEquals(0.0, e.eval("o.doubleMethod(0.0);")); 11.217 - assertEquals(14.0, e.eval("o.doubleMethod(7.0);")); 11.218 - assertEquals(0.0, e.eval("o.doubleMethod('0.0');")); 11.219 - assertEquals(14.0, e.eval("o.doubleMethod('7.0');")); 11.220 - } 11.221 - 11.222 - @Test 11.223 - public void accessMethodBooleanBoxing() throws ScriptException { 11.224 - assertEquals(Boolean.TRUE, e.eval("o.booleanBoxingMethod(java.lang.Boolean.FALSE);")); 11.225 - assertEquals(Boolean.FALSE, e.eval("o.booleanBoxingMethod(java.lang.Boolean.TRUE);")); 11.226 - assertEquals(Boolean.TRUE, e.eval("o.booleanBoxingMethod('');")); 11.227 - assertEquals(Boolean.FALSE, e.eval("o.booleanBoxingMethod('false');")); 11.228 - } 11.229 - 11.230 - @Test 11.231 - public void accessMethodIntBoxing() throws ScriptException { 11.232 - assertEquals(0, e.eval("o.intBoxingMethod(0);")); 11.233 - assertEquals(-200, e.eval("o.intBoxingMethod(-100);")); 11.234 - assertTrue((int)e.eval("(new java.lang.Integer(2)).compareTo(10.0)") < 0); 11.235 - } 11.236 - 11.237 - @Test 11.238 - public void accessMethodLongBoxing() throws ScriptException { 11.239 - assertEquals((long) 0, e.eval("o.longBoxingMethod(0);")); 11.240 - assertEquals((long) 400, e.eval("o.longBoxingMethod(200);")); 11.241 - assertTrue((int)e.eval("(new java.lang.Long(2)).compareTo(10.0)") < 0); 11.242 - } 11.243 - 11.244 - @Test 11.245 - public void accessMethodByteBoxing() throws ScriptException { 11.246 - assertEquals((byte) 0, e.eval("o.byteBoxingMethod(0);")); 11.247 - assertEquals((byte) 10, e.eval("o.byteBoxingMethod(5);")); 11.248 - assertTrue((int)e.eval("(new java.lang.Byte(2)).compareTo(10.0)") < 0); 11.249 - } 11.250 - 11.251 - @Test 11.252 - public void accessMethodShortBoxing() throws ScriptException { 11.253 - assertEquals((short) 0, e.eval("o.shortBoxingMethod(0);")); 11.254 - assertEquals((short) 8000, e.eval("o.shortBoxingMethod(4000);")); 11.255 - assertTrue((int)e.eval("(new java.lang.Short(2)).compareTo(10.0)") < 0); 11.256 - } 11.257 - 11.258 - @Test 11.259 - public void accessMethodCharBoxing() throws ScriptException { 11.260 - assertEquals('A', e.eval("o.charBoxingMethod('a');")); 11.261 - assertEquals('Z', e.eval("o.charBoxingMethod('z');")); 11.262 - assertTrue((int)e.eval("(new java.lang.Character(2)).compareTo(10)") < 0); 11.263 - } 11.264 - 11.265 - @Test 11.266 - public void accessMethodFloatBoxing() throws ScriptException { 11.267 - assertEquals(0.0f, e.eval("o.floatBoxingMethod(0.0);")); 11.268 - assertEquals(4.2f, e.eval("o.floatBoxingMethod(2.1);")); 11.269 - assertTrue((int)e.eval("(new java.lang.Float(2.0)).compareTo(10.0)") < 0); 11.270 - } 11.271 - 11.272 - @Test 11.273 - public void accessMethodDoubleBoxing() throws ScriptException { 11.274 - assertEquals(0.0, e.eval("o.doubleBoxingMethod(0.0);")); 11.275 - assertEquals(14.0, e.eval("o.doubleBoxingMethod(7.0);")); 11.276 - assertTrue((int)e.eval("(new java.lang.Double(2)).compareTo(10.0)") < 0); 11.277 - } 11.278 - 11.279 - @Test 11.280 - public void accessMethodString() throws ScriptException { 11.281 - assertEquals("", e.eval("o.stringMethod('');")); 11.282 - assertEquals("abcabc", e.eval("o.stringMethod('abc');")); 11.283 - } 11.284 - 11.285 - @Test 11.286 - public void accessMethodObject() throws ScriptException { 11.287 - e.put("so", new Person(5)); 11.288 - e.eval("var rso = o.objectMethod(so);"); 11.289 - assertEquals(new Person(10), e.get("rso")); 11.290 - } 11.291 - 11.292 - @Test 11.293 - public void accessMethodBooleanArray() throws ScriptException { 11.294 - assertTrue(Arrays.equals(o.booleanArrayMethod(o.publicBooleanArray), (boolean[])e.eval("o.booleanArrayMethod(o.publicBooleanArray);"))); 11.295 - } 11.296 - 11.297 - @Test 11.298 - public void accessMethodIntArray() throws ScriptException { 11.299 - assertArrayEquals(o.intArrayMethod(o.publicIntArray), (int[])e.eval("o.intArrayMethod(o.publicIntArray);")); 11.300 - } 11.301 - 11.302 - @Test 11.303 - public void accessMethodLongArray() throws ScriptException { 11.304 - assertArrayEquals(o.longArrayMethod(o.publicLongArray), (long[])e.eval("o.longArrayMethod(o.publicLongArray);")); 11.305 - } 11.306 - 11.307 - @Test 11.308 - public void accessMethodByteArray() throws ScriptException { 11.309 - assertArrayEquals(o.byteArrayMethod(o.publicByteArray), (byte[])e.eval("o.byteArrayMethod(o.publicByteArray);")); 11.310 - } 11.311 - 11.312 - @Test 11.313 - public void accessMethodShortArray() throws ScriptException { 11.314 - assertArrayEquals(o.shortArrayMethod(o.publicShortArray), (short[])e.eval("o.shortArrayMethod(o.publicShortArray);")); 11.315 - } 11.316 - 11.317 - @Test 11.318 - public void accessMethodCharArray() throws ScriptException { 11.319 - assertArrayEquals(o.charArrayMethod(o.publicCharArray), (char[])e.eval("o.charArrayMethod(o.publicCharArray);")); 11.320 - } 11.321 - 11.322 - @Test 11.323 - public void accessMethodFloatArray() throws ScriptException { 11.324 - assertArrayEquals(o.floatArrayMethod(o.publicFloatArray), (float[])e.eval("o.floatArrayMethod(o.publicFloatArray);"), 1e-10f); 11.325 - } 11.326 - 11.327 - @Test 11.328 - public void accessMethodDoubleArray() throws ScriptException { 11.329 - assertArrayEquals(o.doubleArrayMethod(o.publicDoubleArray), (double[])e.eval("o.doubleArrayMethod(o.publicDoubleArray);"), 1e-10); 11.330 - } 11.331 - 11.332 - @Test 11.333 - public void accessMethodStringArray() throws ScriptException { 11.334 - assertArrayEquals(o.stringArrayMethod(o.publicStringArray), (String[])e.eval("o.stringArrayMethod(o.publicStringArray);")); 11.335 - } 11.336 - 11.337 - @Test 11.338 - public void accessMethodObjectArray() throws ScriptException { 11.339 - assertArrayEquals(o.objectArrayMethod(o.publicObjectArray), (Person[])e.eval("o.objectArrayMethod(o.publicObjectArray);")); 11.340 - } 11.341 - 11.342 - @Test 11.343 - public void accessDefaultConstructor() throws ScriptException { 11.344 - e.eval("var dc = new Packages.jdk.nashorn.api.javaaccess.Person()"); 11.345 - assertEquals(new Person(), e.get("dc")); 11.346 - } 11.347 - 11.348 - @Test 11.349 - public void accessCustomConstructor() throws ScriptException { 11.350 - e.eval("var cc = new Packages.jdk.nashorn.api.javaaccess.Person(17)"); 11.351 - assertEquals(new Person(17), e.get("cc")); 11.352 - } 11.353 - 11.354 - @Test 11.355 - public void accessMethod2PrimitiveParams() throws ScriptException { 11.356 - assertEquals(o.twoParamMethod(50, 40.0), e.eval("o.twoParamMethod(50,40);")); 11.357 - } 11.358 - 11.359 - @Test 11.360 - public void accessMethod3PrimitiveParams() throws ScriptException { 11.361 - assertEquals(o.threeParamMethod((short)10, 20L, 'b'), e.eval("o.threeParamMethod(10,20,'b');")); 11.362 - } 11.363 - 11.364 - @Test 11.365 - public void accessMethod2ObjectParams() throws ScriptException { 11.366 - assertArrayEquals(new Person[] { new Person(200), new Person(300) }, (Person[])e.eval("o.twoObjectParamMethod(new Person(300),new Person(200));")); 11.367 - } 11.368 - 11.369 - @Test 11.370 - public void accessMethod3ObjectParams() throws ScriptException { 11.371 - assertArrayEquals(new Person[] { new Person(3), new Person(2), new Person(1) }, (Person[])e.eval("o.threeObjectParamMethod(new Person(1),new Person(2),new Person(3));")); 11.372 - } 11.373 - 11.374 - @Test 11.375 - public void accessMethod8ObjectParams() throws ScriptException { 11.376 - assertArrayEquals(new Person[] { new Person(8), new Person(7), new Person(6), new Person(5), new Person(4), new Person(3), new Person(2), new Person(1) }, (Person[])e.eval("o.eightObjectParamMethod(new Person(1),new Person(2),new Person(3)," + "new Person(4),new Person(5),new Person(6),new Person(7),new Person(8));")); 11.377 - } 11.378 - 11.379 - @Test 11.380 - public void accessMethod9ObjectParams() throws ScriptException { 11.381 - assertArrayEquals(new Person[] { new Person(9), new Person(8), new Person(7), new Person(6), new Person(5), new Person(4), new Person(3), new Person(2), new Person(1) }, (Person[])e.eval("o.nineObjectParamMethod(new Person(1),new Person(2),new Person(3)," + "new Person(4),new Person(5),new Person(6)," + "new Person(7),new Person(8),new Person(9));")); 11.382 - } 11.383 - 11.384 - @Test 11.385 - public void accessMethodObjectEllipsis() throws ScriptException { 11.386 - assertArrayEquals(new Person[] { new Person(9), new Person(8), new Person(7), new Person(6), new Person(5), new Person(4), new Person(3), new Person(2), new Person(1) }, (Person[])e.eval("o.methodObjectEllipsis(new Person(1),new Person(2),new Person(3)," + "new Person(4),new Person(5),new Person(6)," + "new Person(7),new Person(8),new Person(9));")); 11.387 - assertArrayEquals(new Person[] {}, (Person[])e.eval("o.methodObjectEllipsis()")); 11.388 - assertArrayEquals(new Person[] { new Person(9) }, (Person[])e.eval("o.methodObjectEllipsis(new Person(9))")); 11.389 - } 11.390 - 11.391 - @Test 11.392 - public void accessMethodPrimitiveEllipsis() throws ScriptException { 11.393 - assertArrayEquals(new Person[] { new Person(1), new Person(3), new Person(2) }, (Person[])e.eval("o.methodPrimitiveEllipsis(1,3,2);")); 11.394 - assertArrayEquals(new Person[] {}, (Person[])e.eval("o.methodPrimitiveEllipsis();")); 11.395 - assertArrayEquals(o.methodPrimitiveEllipsis(9, 8, 7, 6, 5, 4, 3, 2, 1), (Person[])e.eval("o.methodPrimitiveEllipsis(9,8,7,6,5,4,3,2,1);")); 11.396 - } 11.397 - 11.398 - @Test 11.399 - public void accessMethodMixedEllipsis() throws ScriptException { 11.400 - assertArrayEquals(new Object[] { new Person(1), 12, "hello", true }, (Object[])e.eval("o.methodMixedEllipsis(new Person(1),12,'hello',true);")); 11.401 - assertArrayEquals(new Object[] {}, (Object[])e.eval("o.methodMixedEllipsis();")); 11.402 - } 11.403 - 11.404 - @Test 11.405 - public void accessMethodObjectWithEllipsis() throws ScriptException { 11.406 - assertArrayEquals(new Object[] { "hello", 12, 15, 16 }, (Object[])e.eval("o.methodObjectWithEllipsis('hello',12,15,16);")); 11.407 - assertArrayEquals(new Object[] { "hello" }, (Object[])e.eval("o.methodObjectWithEllipsis('hello');")); 11.408 - } 11.409 - 11.410 - @Test 11.411 - public void accessMethodPrimitiveWithEllipsis() throws ScriptException { 11.412 - assertArrayEquals(new Object[] { 14, 12L, 15L, 16L }, (Object[])e.eval("o.methodPrimitiveWithEllipsis(14,12,15,16);")); 11.413 - assertArrayEquals(new Object[] { 12 }, (Object[])e.eval("o.methodPrimitiveWithEllipsis(12);")); 11.414 - } 11.415 - 11.416 - @Test 11.417 - public void accessMethodMixedWithEllipsis() throws ScriptException { 11.418 - assertArrayEquals(new Object[] { "Hello", 10, true, -100500, 80d }, (Object[])e.eval("o.methodMixedWithEllipsis('Hello', 10, true, -100500,80.0);")); 11.419 - assertArrayEquals(new Object[] { "Nashorn", 15 }, (Object[])e.eval("o.methodMixedWithEllipsis('Nashorn',15);")); 11.420 - } 11.421 - 11.422 - @Test 11.423 - public void accessMethodOverloaded() throws ScriptException { 11.424 - assertEquals(0, e.eval("o.overloadedMethod(0);")); 11.425 - assertEquals(2000, e.eval("o.overloadedMethod(1000);")); 11.426 - assertEquals(2, e.eval("o.overloadedMethod('10');")); 11.427 - assertEquals(7, e.eval("o.overloadedMethod('Nashorn');")); 11.428 - assertEquals(4, e.eval("o.overloadedMethod('true');")); 11.429 - assertEquals(1, e.eval("o.overloadedMethod(true);")); 11.430 - assertEquals(0, e.eval("o.overloadedMethod(false);")); 11.431 - assertEquals(44, e.eval("o.overloadedMethod(new Person(22));")); 11.432 - assertEquals(0, e.eval("o.overloadedMethod(new Person());")); 11.433 - } 11.434 - 11.435 - @Test 11.436 - public void accessMethodDoubleVSintOverloaded() throws ScriptException { 11.437 - assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.0);")); 11.438 - assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(1000.0);")); 11.439 - assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.01);")); 11.440 - assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(100.02);")); 11.441 - assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(0);")); 11.442 - assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(1000);")); 11.443 - } 11.444 - 11.445 - @Test 11.446 - public void accessJavaMethodIntFromJSFromJavaFromJS() throws ScriptException { 11.447 - e.eval("function secondLevelMethodInt(a) {" 11.448 - + "return o.thirdLevelMethodInt(a);" 11.449 - + "}"); 11.450 - assertEquals(50, e.eval("o.firstLevelMethodInt(10);")); 11.451 - } 11.452 - 11.453 - @Test 11.454 - public void accessJavaMethodIntegerFromJSFromJavaFromJS() throws ScriptException { 11.455 - e.eval("function secondLevelMethodInteger(a) {" 11.456 - + "return o.thirdLevelMethodInteger(a);" 11.457 - + "}"); 11.458 - assertEquals(100, e.eval("o.firstLevelMethodInteger(10);")); 11.459 - } 11.460 - 11.461 - @Test 11.462 - public void accessJavaMethodObjectFromJSFromJavaFromJS() throws ScriptException { 11.463 - e.eval("function secondLevelMethodObject(p) {" 11.464 - + "return o.thirdLevelMethodObject(p);" 11.465 - + "}"); 11.466 - assertEquals(new Person(100), e.eval("o.firstLevelMethodObject(new Person(10));")); 11.467 - } 11.468 - 11.469 -}
12.1 --- a/test/src/jdk/nashorn/api/javaaccess/NumberAccessTest.java Wed Apr 01 13:22:52 2015 -0700 12.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 12.3 @@ -1,789 +0,0 @@ 12.4 -/* 12.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 12.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 12.7 - * 12.8 - * This code is free software; you can redistribute it and/or modify it 12.9 - * under the terms of the GNU General Public License version 2 only, as 12.10 - * published by the Free Software Foundation. Oracle designates this 12.11 - * particular file as subject to the "Classpath" exception as provided 12.12 - * by Oracle in the LICENSE file that accompanied this code. 12.13 - * 12.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 12.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12.17 - * version 2 for more details (a copy is included in the LICENSE file that 12.18 - * accompanied this code). 12.19 - * 12.20 - * You should have received a copy of the GNU General Public License version 12.21 - * 2 along with this work; if not, write to the Free Software Foundation, 12.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 12.23 - * 12.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 12.25 - * or visit www.oracle.com if you need additional information or have any 12.26 - * questions. 12.27 - */ 12.28 - 12.29 -package jdk.nashorn.api.javaaccess; 12.30 - 12.31 -import static org.testng.AssertJUnit.assertEquals; 12.32 -import static org.testng.AssertJUnit.assertTrue; 12.33 -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; 12.34 -import javax.script.ScriptEngine; 12.35 -import javax.script.ScriptEngineManager; 12.36 -import javax.script.ScriptException; 12.37 -import org.testng.TestNG; 12.38 -import org.testng.annotations.AfterClass; 12.39 -import org.testng.annotations.BeforeClass; 12.40 -import org.testng.annotations.Test; 12.41 - 12.42 -/** 12.43 - * @test 12.44 - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.NumberAccessTest 12.45 - * @run testng/othervm jdk.nashorn.api.javaaccess.NumberAccessTest 12.46 - */ 12.47 -@SuppressWarnings("javadoc") 12.48 -public class NumberAccessTest { 12.49 - 12.50 - private static ScriptEngine e; 12.51 - private static SharedObject o; 12.52 - 12.53 - public static void main(final String[] args) { 12.54 - TestNG.main(args); 12.55 - } 12.56 - 12.57 - @BeforeClass 12.58 - public static void setUpClass() throws ScriptException { 12.59 - final ScriptEngineManager m = new ScriptEngineManager(); 12.60 - e = m.getEngineByName("nashorn"); 12.61 - o = new SharedObject(); 12.62 - e.put("o", o); 12.63 - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 12.64 - } 12.65 - 12.66 - @AfterClass 12.67 - public static void tearDownClass() { 12.68 - e = null; 12.69 - o = null; 12.70 - } 12.71 - 12.72 - // --------------------------------long 12.73 - // tests------------------------------------ 12.74 - @Test 12.75 - public void accessFieldLong() throws ScriptException { 12.76 - e.eval("var p_long = o.publicLong;"); 12.77 - assertEquals(o.publicLong, e.get("p_long")); 12.78 - e.eval("o.publicLong = 12;"); 12.79 - assertEquals(12, o.publicLong); 12.80 - } 12.81 - 12.82 - @Test 12.83 - public void accessFieldLongArray() throws ScriptException { 12.84 - e.eval("var p_long_array = o.publicLongArray;"); 12.85 - assertEquals(o.publicLongArray[0], e.eval("o.publicLongArray[0];")); 12.86 - assertArrayEquals(o.publicLongArray, (long[])e.get("p_long_array")); 12.87 - e.eval("var t_long_arr = new (Java.type(\"long[]\"))(3);" + 12.88 - "t_long_arr[0] = -189009;" + 12.89 - "t_long_arr[1] = 456;" + 12.90 - "t_long_arr[2] = 600000001;" + 12.91 - "o.publicLongArray = t_long_arr;"); 12.92 - // e.eval("o.publicIntArray = [-189009,456,600000001];"); 12.93 - assertArrayEquals(new long[] { -189009, 456, 600000001 }, o.publicLongArray); 12.94 - e.eval("o.publicLongArray[0] = 10;"); 12.95 - assertEquals(10, o.publicLongArray[0]); 12.96 - } 12.97 - 12.98 - @Test 12.99 - public void accessStaticFieldLong() throws ScriptException { 12.100 - e.eval("var ps_long = SharedObject.publicStaticLong;"); 12.101 - assertEquals(SharedObject.publicStaticLong, e.get("ps_long")); 12.102 - e.eval("SharedObject.publicStaticLong = 120;"); 12.103 - assertEquals(120, SharedObject.publicStaticLong); 12.104 - } 12.105 - 12.106 - @Test 12.107 - public void accessStaticFieldLongArray() throws ScriptException { 12.108 - e.eval("var ps_long_array = SharedObject.publicStaticLongArray;"); 12.109 - assertEquals(SharedObject.publicStaticLongArray[0], e.eval("SharedObject.publicStaticLongArray[0];")); 12.110 - assertArrayEquals(SharedObject.publicStaticLongArray, (long[])e.get("ps_long_array")); 12.111 - e.eval("var ts_long_arr = new (Java.type(\"long[]\"))(3);" + 12.112 - "ts_long_arr[0] = -189009;" + 12.113 - "ts_long_arr[1] = 456;" + 12.114 - "ts_long_arr[2] = 600000001;" + 12.115 - "SharedObject.publicStaticLongArray = ts_long_arr;"); 12.116 - // e.eval("o.publicIntArray = [-189009,456,600000001];"); 12.117 - assertArrayEquals(new long[] { -189009, 456, 600000001 }, SharedObject.publicStaticLongArray); 12.118 - e.eval("SharedObject.publicStaticLongArray[0] = 10;"); 12.119 - assertEquals(10, SharedObject.publicStaticLongArray[0]); 12.120 - } 12.121 - 12.122 - @Test 12.123 - public void accessFinalFieldLong() throws ScriptException { 12.124 - e.eval("var pf_long = o.publicFinalLong;"); 12.125 - assertEquals(o.publicFinalLong, e.get("pf_long")); 12.126 - e.eval("o.publicFinalLong = 120;"); 12.127 - assertEquals(13353333333333333L, o.publicFinalLong); 12.128 - } 12.129 - 12.130 - @Test 12.131 - public void accessFinalFieldLongArray() throws ScriptException { 12.132 - e.eval("var pf_long_array = o.publicFinalLongArray;"); 12.133 - assertEquals(o.publicFinalLongArray[0], e.eval("o.publicFinalLongArray[0];")); 12.134 - assertArrayEquals(o.publicFinalLongArray, (long[])e.get("pf_long_array")); 12.135 - e.eval("var tf_long_arr = new (Java.type(\"long[]\"))(3);" + 12.136 - "tf_long_arr[0] = -189009;" + 12.137 - "tf_long_arr[1] = 456;" + 12.138 - "tf_long_arr[2] = 600000001;" + 12.139 - "o.publicFinalLongArray = tf_long_arr;"); 12.140 - // e.eval("o.publicIntArray = [-189009,456,600000001];"); 12.141 - assertArrayEquals(new long[] { 1901733333333L, -2247355555L, 3977377777L }, o.publicFinalLongArray); 12.142 - e.eval("o.publicFinalLongArray[0] = 10;"); 12.143 - assertEquals(10, o.publicFinalLongArray[0]); 12.144 - } 12.145 - 12.146 - @Test 12.147 - public void accessStaticFinalFieldLong() throws ScriptException { 12.148 - e.eval("var psf_long = SharedObject.publicStaticFinalLong;"); 12.149 - assertEquals(SharedObject.publicStaticFinalLong, e.get("psf_long")); 12.150 - e.eval("SharedObject.publicStaticFinalLong = 120;"); 12.151 - assertEquals(8333333333333L, SharedObject.publicStaticFinalLong); 12.152 - } 12.153 - 12.154 - @Test 12.155 - public void accessStaticFinalFieldLongArray() throws ScriptException { 12.156 - e.eval("var psf_long_array = SharedObject.publicStaticFinalLongArray;"); 12.157 - assertEquals(SharedObject.publicStaticFinalLongArray[0], e.eval("SharedObject.publicStaticFinalLongArray[0];")); 12.158 - assertArrayEquals(SharedObject.publicStaticFinalLongArray, (long[])e.get("psf_long_array")); 12.159 - e.eval("var tsf_long_arr = new (Java.type(\"long[]\"))(3);" + 12.160 - "tsf_long_arr[0] = -189009;" + 12.161 - "tsf_long_arr[1] = 456;" + 12.162 - "tsf_long_arr[2] = 600000001;" + 12.163 - "SharedObject.publicStaticFinalLongArray = tsf_long_arr;"); 12.164 - // e.eval("o.publicIntArray = [-189009,456,600000001];"); 12.165 - assertArrayEquals(new long[] { 19017383333L, -2247358L, 39773787L }, SharedObject.publicStaticFinalLongArray); 12.166 - e.eval("SharedObject.publicStaticFinalLongArray[0] = 10;"); 12.167 - assertEquals(10, SharedObject.publicStaticFinalLongArray[0]); 12.168 - } 12.169 - 12.170 - // --------------------------------int 12.171 - // tests------------------------------------ 12.172 - @Test 12.173 - public void accessFieldInt() throws ScriptException { 12.174 - e.eval("var p_int = o.publicInt;"); 12.175 - assertEquals(o.publicInt, e.get("p_int")); 12.176 - e.eval("o.publicInt = 14;"); 12.177 - assertEquals(14, o.publicInt); 12.178 - } 12.179 - 12.180 - @Test 12.181 - public void accessFieldIntArray() throws ScriptException { 12.182 - e.eval("var p_int_array = o.publicIntArray;"); 12.183 - assertEquals(o.publicIntArray[0], e.eval("o.publicIntArray[0];")); 12.184 - assertArrayEquals(o.publicIntArray, (int[])e.get("p_int_array")); 12.185 - e.eval("var t_int_arr = new (Java.type(\"int[]\"))(3);" + 12.186 - "t_int_arr[0] = 4;" + 12.187 - "t_int_arr[1] = 5;" + 12.188 - "t_int_arr[2] = 6;" + 12.189 - "o.publicIntArray = t_int_arr;"); 12.190 - assertArrayEquals(new int[] { 4, 5, 6 }, o.publicIntArray); 12.191 - e.eval("o.publicIntArray[0] = 100;"); 12.192 - assertEquals(100, o.publicIntArray[0]); 12.193 - } 12.194 - 12.195 - @Test 12.196 - public void accessStaticFieldInt() throws ScriptException { 12.197 - e.eval("var ps_int = SharedObject.publicStaticInt;"); 12.198 - assertEquals(SharedObject.publicStaticInt, e.get("ps_int")); 12.199 - e.eval("SharedObject.publicStaticInt = 140;"); 12.200 - assertEquals(140, SharedObject.publicStaticInt); 12.201 - } 12.202 - 12.203 - @Test 12.204 - public void accessStaticFieldIntArray() throws ScriptException { 12.205 - e.eval("var ps_int_array = SharedObject.publicStaticIntArray;"); 12.206 - assertEquals(SharedObject.publicStaticIntArray[0], e.eval("SharedObject.publicStaticIntArray[0];")); 12.207 - assertArrayEquals(SharedObject.publicStaticIntArray, (int[])e.get("ps_int_array")); 12.208 - e.eval("var ts_int_arr = new (Java.type(\"int[]\"))(3);" + 12.209 - "ts_int_arr[0] = 4;" + 12.210 - "ts_int_arr[1] = 5;" + 12.211 - "ts_int_arr[2] = 6;" + 12.212 - "SharedObject.publicStaticIntArray = ts_int_arr;"); 12.213 - assertArrayEquals(new int[] { 4, 5, 6 }, SharedObject.publicStaticIntArray); 12.214 - e.eval("SharedObject.publicStaticIntArray[0] = 100;"); 12.215 - assertEquals(100, SharedObject.publicStaticIntArray[0]); 12.216 - } 12.217 - 12.218 - @Test 12.219 - public void accessFinalFieldInt() throws ScriptException { 12.220 - e.eval("var pf_int = o.publicFinalInt;"); 12.221 - assertEquals(o.publicFinalInt, e.get("pf_int")); 12.222 - 12.223 - e.eval("o.publicFinalInt = 10;"); 12.224 - assertEquals(20712023, o.publicFinalInt); 12.225 - } 12.226 - 12.227 - @Test 12.228 - public void accessFinalFieldIntArray() throws ScriptException { 12.229 - e.eval("var pf_int_array = o.publicFinalIntArray;"); 12.230 - assertEquals(o.publicFinalIntArray[0], e.eval("o.publicFinalIntArray[0];")); 12.231 - assertArrayEquals(o.publicFinalIntArray, (int[])e.get("pf_int_array")); 12.232 - e.eval("var tf_int_arr = new (Java.type(\"int[]\"))(3);" + 12.233 - "tf_int_arr[0] = 4;" + 12.234 - "tf_int_arr[1] = 5;" + 12.235 - "tf_int_arr[2] = 6;" + 12.236 - "o.publicFinalIntArray = tf_int_arr;"); 12.237 - assertArrayEquals(new int[] { 50, 80, 130, 210, 340 }, o.publicFinalIntArray); 12.238 - e.eval("o.publicFinalIntArray[0] = 100;"); 12.239 - assertEquals(100, o.publicFinalIntArray[0]); 12.240 - } 12.241 - 12.242 - @Test 12.243 - public void accessStaticFinalFieldInt() throws ScriptException { 12.244 - e.eval("var psf_int = SharedObject.publicStaticFinalInt;"); 12.245 - assertEquals(SharedObject.publicStaticFinalInt, e.get("psf_int")); 12.246 - e.eval("SharedObject.publicStaticFinalInt = 140;"); 12.247 - assertEquals(207182023, SharedObject.publicStaticFinalInt); 12.248 - } 12.249 - 12.250 - @Test 12.251 - public void accessStaticFinalFieldIntArray() throws ScriptException { 12.252 - e.eval("var psf_int_array = SharedObject.publicStaticFinalIntArray;"); 12.253 - assertEquals(SharedObject.publicStaticFinalIntArray[0], e.eval("SharedObject.publicStaticFinalIntArray[0];")); 12.254 - assertArrayEquals(SharedObject.publicStaticFinalIntArray, (int[])e.get("psf_int_array")); 12.255 - e.eval("var tsf_int_arr = new (Java.type(\"int[]\"))(3);" + 12.256 - "tsf_int_arr[0] = 4;" + 12.257 - "tsf_int_arr[1] = 5;" + 12.258 - "tsf_int_arr[2] = 6;" + 12.259 - "SharedObject.publicStaticFinalIntArray = tsf_int_arr;"); 12.260 - assertArrayEquals(new int[] { 1308, 210, 340 }, SharedObject.publicStaticFinalIntArray); 12.261 - e.eval("SharedObject.publicStaticFinalIntArray[0] = 100;"); 12.262 - assertEquals(100, SharedObject.publicStaticFinalIntArray[0]); 12.263 - } 12.264 - 12.265 - // --------------------------------byte 12.266 - // tests------------------------------------ 12.267 - @Test 12.268 - public void accessFieldByte() throws ScriptException { 12.269 - e.eval("var p_byte = o.publicByte;"); 12.270 - assertEquals((double)o.publicByte, ((Number)e.get("p_byte")).doubleValue()); 12.271 - e.eval("o.publicByte = 16;"); 12.272 - assertEquals(16, o.publicByte); 12.273 - } 12.274 - 12.275 - @Test 12.276 - public void accessFieldByteArray() throws ScriptException { 12.277 - e.eval("var p_byte_array = o.publicByteArray;"); 12.278 - assertEquals(o.publicByteArray[0], e.eval("o.publicByteArray[0];")); 12.279 - assertArrayEquals(o.publicByteArray, (byte[])e.get("p_byte_array")); 12.280 - e.eval("var t_byte_arr = new (Java.type(\"byte[]\"))(3);" + 12.281 - "t_byte_arr[0] = -18;" + 12.282 - "t_byte_arr[1] = 56;" + 12.283 - "t_byte_arr[2] = 60;" + 12.284 - "o.publicByteArray = t_byte_arr;"); 12.285 - assertArrayEquals(new byte[] { -18, 56, 60 }, o.publicByteArray); 12.286 - e.eval("o.publicByteArray[0] = 100;"); 12.287 - assertEquals(100, o.publicByteArray[0]); 12.288 - } 12.289 - 12.290 - @Test 12.291 - public void accessStaticFieldByte() throws ScriptException { 12.292 - e.eval("var ps_byte = SharedObject.publicStaticByte;"); 12.293 - assertEquals((double)SharedObject.publicStaticByte, ((Number)e.get("ps_byte")).doubleValue()); 12.294 - e.eval("SharedObject.publicStaticByte = 16;"); 12.295 - assertEquals(16, SharedObject.publicStaticByte); 12.296 - } 12.297 - 12.298 - @Test 12.299 - public void accessStaticFieldByteArray() throws ScriptException { 12.300 - e.eval("var ps_byte_array = SharedObject.publicStaticByteArray;"); 12.301 - assertEquals(SharedObject.publicStaticByteArray[0], e.eval("SharedObject.publicStaticByteArray[0];")); 12.302 - assertArrayEquals(SharedObject.publicStaticByteArray, (byte[])e.get("ps_byte_array")); 12.303 - e.eval("var ts_byte_arr = new (Java.type(\"byte[]\"))(3);" + 12.304 - "ts_byte_arr[0] = -18;" + 12.305 - "ts_byte_arr[1] = 56;" + 12.306 - "ts_byte_arr[2] = 60;" + 12.307 - "SharedObject.publicStaticByteArray = ts_byte_arr;"); 12.308 - assertArrayEquals(new byte[] { -18, 56, 60 }, SharedObject.publicStaticByteArray); 12.309 - e.eval("SharedObject.publicStaticByteArray[0] = -90;"); 12.310 - assertEquals(-90, SharedObject.publicStaticByteArray[0]); 12.311 - } 12.312 - 12.313 - @Test 12.314 - public void accessFinalFieldByte() throws ScriptException { 12.315 - e.eval("var pf_byte = o.publicFinalByte;"); 12.316 - assertEquals((double)o.publicFinalByte, ((Number)e.get("pf_byte")).doubleValue()); 12.317 - e.eval("o.publicFinalByte = 16;"); 12.318 - assertEquals(-7, o.publicFinalByte); 12.319 - } 12.320 - 12.321 - @Test 12.322 - public void accessFinalFieldByteArray() throws ScriptException { 12.323 - e.eval("var pf_byte_array = o.publicFinalByteArray;"); 12.324 - assertEquals(o.publicFinalByteArray[0], e.eval("o.publicFinalByteArray[0];")); 12.325 - assertArrayEquals(o.publicFinalByteArray, (byte[])e.get("pf_byte_array")); 12.326 - e.eval("var tf_byte_arr = new (Java.type(\"byte[]\"))(3);" + 12.327 - "tf_byte_arr[0] = -18;" + 12.328 - "tf_byte_arr[1] = 56;" + 12.329 - "tf_byte_arr[2] = 60;" + 12.330 - "o.publicFinalByteArray = tf_byte_arr;"); 12.331 - assertArrayEquals(new byte[] { 1, 3, 6, 17, -128 }, o.publicFinalByteArray); 12.332 - e.eval("o.publicFinalByteArray[0] = -90;"); 12.333 - assertEquals(-90, o.publicFinalByteArray[0]); 12.334 - } 12.335 - 12.336 - @Test 12.337 - public void accessStaticFinalFieldByte() throws ScriptException { 12.338 - e.eval("var psf_byte = SharedObject.publicStaticFinalByte;"); 12.339 - assertEquals((double)SharedObject.publicStaticFinalByte, ((Number)e.get("psf_byte")).doubleValue()); 12.340 - e.eval("SharedObject.publicStaticFinalByte = 16;"); 12.341 - assertEquals(-70, SharedObject.publicStaticFinalByte); 12.342 - } 12.343 - 12.344 - @Test 12.345 - public void accessStaticFinalFieldByteArray() throws ScriptException { 12.346 - e.eval("var psf_byte_array = SharedObject.publicStaticFinalByteArray;"); 12.347 - assertEquals(SharedObject.publicStaticFinalByteArray[0], e.eval("SharedObject.publicStaticFinalByteArray[0];")); 12.348 - assertArrayEquals(SharedObject.publicStaticFinalByteArray, (byte[])e.get("psf_byte_array")); 12.349 - e.eval("var tsf_byte_arr = new (Java.type(\"byte[]\"))(3);" + 12.350 - "tsf_byte_arr[0] = -18;" + 12.351 - "tsf_byte_arr[1] = 56;" + 12.352 - "tsf_byte_arr[2] = 60;" + 12.353 - "SharedObject.publicStaticFinalByteArray = tsf_byte_arr;"); 12.354 - assertArrayEquals(new byte[] { 17, -128, 81 }, SharedObject.publicStaticFinalByteArray); 12.355 - e.eval("SharedObject.publicStaticFinalByteArray[0] = -90;"); 12.356 - assertEquals(-90, SharedObject.publicStaticFinalByteArray[0]); 12.357 - } 12.358 - 12.359 - // --------------------------------short 12.360 - // tests------------------------------------ 12.361 - @Test 12.362 - public void accessFieldShort() throws ScriptException { 12.363 - e.eval("var p_short = o.publicShort;"); 12.364 - assertEquals((double)o.publicShort, ((Number)e.get("p_short")).doubleValue()); 12.365 - e.eval("o.publicShort = 18;"); 12.366 - assertEquals(18, o.publicShort); 12.367 - } 12.368 - 12.369 - @Test 12.370 - public void accessFieldShortArray() throws ScriptException { 12.371 - e.eval("var p_short_array = o.publicShortArray;"); 12.372 - assertEquals(o.publicShortArray[0], e.eval("o.publicShortArray[0];")); 12.373 - assertArrayEquals(o.publicShortArray, (short[])e.get("p_short_array")); 12.374 - e.eval("var t_short_arr = new (Java.type(\"short[]\"))(3);" + 12.375 - "t_short_arr[0] = 90;" + 12.376 - "t_short_arr[1] = 5;" + 12.377 - "t_short_arr[2] = -6000;" + 12.378 - "o.publicShortArray = t_short_arr;"); 12.379 - assertArrayEquals(new short[] { 90, 5, -6000 }, o.publicShortArray); 12.380 - e.eval("o.publicShortArray[0] = -1000;"); 12.381 - assertEquals(-1000, o.publicShortArray[0]); 12.382 - } 12.383 - 12.384 - @Test 12.385 - public void accessStaticFieldShort() throws ScriptException { 12.386 - e.eval("var ps_short = SharedObject.publicStaticShort;"); 12.387 - assertEquals((double)SharedObject.publicStaticShort, ((Number)e.get("ps_short")).doubleValue()); 12.388 - e.eval("SharedObject.publicStaticShort = 180;"); 12.389 - assertEquals(180, SharedObject.publicStaticShort); 12.390 - } 12.391 - 12.392 - @Test 12.393 - public void accessStaticFieldShortArray() throws ScriptException { 12.394 - e.eval("var ps_short_array = SharedObject.publicStaticShortArray;"); 12.395 - assertEquals(SharedObject.publicStaticShortArray[0], e.eval("SharedObject.publicStaticShortArray[0];")); 12.396 - assertArrayEquals(SharedObject.publicStaticShortArray, (short[])e.get("ps_short_array")); 12.397 - e.eval("var ts_short_arr = new (Java.type(\"short[]\"))(3);" + 12.398 - "ts_short_arr[0] = 90;" + 12.399 - "ts_short_arr[1] = 5;" + 12.400 - "ts_short_arr[2] = -6000;" + 12.401 - "SharedObject.publicStaticShortArray = ts_short_arr;"); 12.402 - assertArrayEquals(new short[] { 90, 5, -6000 }, SharedObject.publicStaticShortArray); 12.403 - e.eval("SharedObject.publicStaticShortArray[0] = -1000;"); 12.404 - assertEquals(-1000, SharedObject.publicStaticShortArray[0]); 12.405 - } 12.406 - 12.407 - @Test 12.408 - public void accessFinalFieldShort() throws ScriptException { 12.409 - e.eval("var pf_short = o.publicFinalShort;"); 12.410 - assertEquals((double)o.publicFinalShort, ((Number)e.get("pf_short")).doubleValue()); 12.411 - e.eval("o.publicFinalShort = 180;"); 12.412 - assertEquals(31220, o.publicFinalShort); 12.413 - } 12.414 - 12.415 - @Test 12.416 - public void accessFinalFieldShortArray() throws ScriptException { 12.417 - e.eval("var pf_short_array = o.publicFinalShortArray;"); 12.418 - assertEquals(o.publicFinalShortArray[0], e.eval("o.publicFinalShortArray[0];")); 12.419 - assertArrayEquals(o.publicFinalShortArray, (short[])e.get("pf_short_array")); 12.420 - e.eval("var tf_short_arr = new (Java.type(\"short[]\"))(3);" + 12.421 - "tf_short_arr[0] = 90;" + 12.422 - "tf_short_arr[1] = 5;" + 12.423 - "tf_short_arr[2] = -6000;" + 12.424 - "o.publicFinalShortArray = tf_short_arr;"); 12.425 - assertArrayEquals(new short[] { 12240, 9200, -17289, 1200, 12 }, o.publicFinalShortArray); 12.426 - e.eval("o.publicFinalShortArray[0] = -1000;"); 12.427 - assertEquals(-1000, o.publicFinalShortArray[0]); 12.428 - } 12.429 - 12.430 - @Test 12.431 - public void accessStaticFinalFieldShort() throws ScriptException { 12.432 - e.eval("var psf_short = SharedObject.publicStaticFinalShort;"); 12.433 - assertEquals((double)SharedObject.publicStaticFinalShort, ((Number)e.get("psf_short")).doubleValue()); 12.434 - e.eval("SharedObject.publicStaticFinalShort = 180;"); 12.435 - assertEquals(8888, SharedObject.publicStaticFinalShort); 12.436 - } 12.437 - 12.438 - @Test 12.439 - public void accessStaticFinalFieldShortArray() throws ScriptException { 12.440 - e.eval("var psf_short_array = SharedObject.publicStaticFinalShortArray;"); 12.441 - assertEquals(SharedObject.publicStaticFinalShortArray[0], e.eval("SharedObject.publicStaticFinalShortArray[0];")); 12.442 - assertArrayEquals(SharedObject.publicStaticFinalShortArray, (short[])e.get("psf_short_array")); 12.443 - e.eval("var tsf_short_arr = new (Java.type(\"short[]\"))(3);" + 12.444 - "tsf_short_arr[0] = 90;" + 12.445 - "tsf_short_arr[1] = 5;" + 12.446 - "tsf_short_arr[2] = -6000;" + 12.447 - "SharedObject.publicStaticFinalShortArray = tsf_short_arr;"); 12.448 - assertArrayEquals(new short[] { 8240, 9280, -1289, 120, 812 }, SharedObject.publicStaticFinalShortArray); 12.449 - e.eval("SharedObject.publicStaticFinalShortArray[0] = -1000;"); 12.450 - assertEquals(-1000, SharedObject.publicStaticFinalShortArray[0]); 12.451 - } 12.452 - 12.453 - // --------------------------------char 12.454 - // tests------------------------------------ 12.455 - @Test 12.456 - public void accessFieldChar() throws ScriptException { 12.457 - e.eval("var p_char = o.publicChar;"); 12.458 - assertEquals(o.publicChar, e.get("p_char")); 12.459 - e.eval("o.publicChar = 'S';"); 12.460 - assertEquals('S', o.publicChar); 12.461 - e.eval("o.publicChar = 10;"); 12.462 - assertEquals(10, o.publicChar); 12.463 - e.eval("try {" 12.464 - + " o.publicChar = 'Big string';" + 12.465 - "} catch(e) {" + 12.466 - " var isThrown = true;" + 12.467 - "}"); 12.468 - assertEquals("Exception thrown", true, e.get("isThrown")); 12.469 - assertEquals(10, o.publicChar); 12.470 - } 12.471 - 12.472 - @Test 12.473 - public void accessFieldCharArray() throws ScriptException { 12.474 - e.eval("var p_char_array = o.publicCharArray;"); 12.475 - assertEquals(o.publicCharArray[0], e.eval("o.publicCharArray[0];")); 12.476 - assertArrayEquals(o.publicCharArray, (char[])e.get("p_char_array")); 12.477 - e.eval("var t_char_arr = new (Java.type(\"char[]\"))(3);" + 12.478 - "t_char_arr[0] = 'F';" + 12.479 - "t_char_arr[1] = 'o';" + 12.480 - "t_char_arr[2] = 'o';" + 12.481 - "o.publicCharArray = t_char_arr;"); 12.482 - assertArrayEquals("Foo".toCharArray(), o.publicCharArray); 12.483 - e.eval("o.publicCharArray[0] = 'Z';"); 12.484 - assertEquals('Z', o.publicCharArray[0]); 12.485 - } 12.486 - 12.487 - @Test 12.488 - public void accessStaticFieldChar() throws ScriptException { 12.489 - e.eval("var ps_char = SharedObject.publicStaticChar;"); 12.490 - assertEquals(SharedObject.publicStaticChar, e.get("ps_char")); 12.491 - e.eval("SharedObject.publicStaticChar = 'Z';"); 12.492 - assertEquals('Z', SharedObject.publicStaticChar); 12.493 - } 12.494 - 12.495 - @Test 12.496 - public void accessStaticFieldCharArray() throws ScriptException { 12.497 - e.eval("var ps_char_array = SharedObject.publicStaticCharArray;"); 12.498 - assertEquals(SharedObject.publicStaticCharArray[0], e.eval("SharedObject.publicStaticCharArray[0];")); 12.499 - assertArrayEquals(SharedObject.publicStaticCharArray, (char[])e.get("ps_char_array")); 12.500 - e.eval("var ts_char_arr = new (Java.type(\"char[]\"))(3);" + 12.501 - "ts_char_arr[0] = 'G';" + 12.502 - "ts_char_arr[1] = 'o';" + 12.503 - "ts_char_arr[2] = 'o';" + 12.504 - "SharedObject.publicStaticCharArray = ts_char_arr;"); 12.505 - assertArrayEquals("Goo".toCharArray(), SharedObject.publicStaticCharArray); 12.506 - e.eval("SharedObject.publicStaticCharArray[0] = 'Z';"); 12.507 - assertEquals('Z', SharedObject.publicStaticCharArray[0]); 12.508 - } 12.509 - 12.510 - @Test 12.511 - public void accessFinalFieldChar() throws ScriptException { 12.512 - e.eval("var pf_char = o.publicFinalChar;"); 12.513 - assertEquals(o.publicFinalChar, e.get("pf_char")); 12.514 - e.eval("o.publicFinalChar = 'S';"); 12.515 - assertEquals('E', o.publicFinalChar); 12.516 - } 12.517 - 12.518 - @Test 12.519 - public void accessFinalCharArray() throws ScriptException { 12.520 - e.eval("var pf_char_array = o.publicFinalCharArray;"); 12.521 - assertEquals(o.publicFinalCharArray[0], e.eval("o.publicFinalCharArray[0];")); 12.522 - assertArrayEquals(o.publicFinalCharArray, (char[])e.get("pf_char_array")); 12.523 - e.eval("var tf_char_arr = new (Java.type(\"char[]\"))(3);" + 12.524 - "tf_char_arr[0] = 'F';" + 12.525 - "tf_char_arr[1] = 'o';" + 12.526 - "tf_char_arr[2] = 'o';" + 12.527 - "o.publicFinalCharArray = tf_char_arr;"); 12.528 - assertArrayEquals("Nashorn hello".toCharArray(), o.publicFinalCharArray); 12.529 - e.eval("o.publicFinalCharArray[0] = 'Z';"); 12.530 - assertEquals('Z', o.publicFinalCharArray[0]); 12.531 - } 12.532 - 12.533 - @Test 12.534 - public void accessStaticFinalFieldChar() throws ScriptException { 12.535 - e.eval("var psf_char = SharedObject.publicStaticFinalChar;"); 12.536 - assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char")); 12.537 - e.eval("SharedObject.publicStaticFinalChar = 'Z';"); 12.538 - assertEquals('K', SharedObject.publicStaticFinalChar); 12.539 - } 12.540 - 12.541 - @Test 12.542 - public void accessStaticFinalFieldCharArray() throws ScriptException { 12.543 - e.eval("var psf_char_array = SharedObject.publicStaticFinalCharArray;"); 12.544 - assertEquals(SharedObject.publicStaticFinalCharArray[0], e.eval("SharedObject.publicStaticFinalCharArray[0];")); 12.545 - assertArrayEquals(SharedObject.publicStaticFinalCharArray, (char[])e.get("psf_char_array")); 12.546 - e.eval("var tsf_char_arr = new (Java.type(\"char[]\"))(3);" + 12.547 - "tsf_char_arr[0] = 'Z';" + 12.548 - "tsf_char_arr[1] = 'o';" + 12.549 - "tsf_char_arr[2] = 'o';" + 12.550 - "SharedObject.publicStaticFinalCharArray = tsf_char_arr;"); 12.551 - assertArrayEquals("StaticString".toCharArray(), SharedObject.publicStaticFinalCharArray); 12.552 - e.eval("SharedObject.publicStaticFinalCharArray[0] = 'Z';"); 12.553 - assertEquals('Z', SharedObject.publicStaticFinalCharArray[0]); 12.554 - } 12.555 - 12.556 - // --------------------------------float 12.557 - // tests------------------------------------ 12.558 - @Test 12.559 - public void accessFieldFloat() throws ScriptException { 12.560 - e.eval("var p_float = o.publicFloat;"); 12.561 - assertEquals((double)o.publicFloat, ((Number)e.get("p_float")).doubleValue()); 12.562 - o.publicFloat = 0.0f / 0.0f; 12.563 - assertEquals(true, e.eval("isNaN(o.publicFloat)")); 12.564 - o.publicFloat = 1.0f / 0.0f; 12.565 - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicFloat")); 12.566 - o.publicFloat = -1.0f / 0.0f; 12.567 - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicFloat")); 12.568 - e.eval("o.publicFloat = 20;"); 12.569 - assertEquals(20, o.publicFloat, 1e-10); 12.570 - e.eval("o.publicFloat = 0.0/0.0;"); 12.571 - assertTrue(Float.isNaN(o.publicFloat)); 12.572 - e.eval("o.publicFloat = 1.0/0.0;"); 12.573 - assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(o.publicFloat)); 12.574 - e.eval("o.publicFloat = -1.0/0.0;"); 12.575 - assertEquals(Float.NEGATIVE_INFINITY, o.publicFloat, 1e-10); 12.576 - } 12.577 - 12.578 - @Test 12.579 - public void accessFieldFloatArray() throws ScriptException { 12.580 - e.eval("var p_float_array = o.publicFloatArray;"); 12.581 - assertEquals(o.publicFloatArray[0], e.eval("o.publicFloatArray[0];")); 12.582 - assertArrayEquals(o.publicFloatArray, (float[])e.get("p_float_array"), 1e-10f); 12.583 - e.eval("var t_float_arr = new (Java.type(\"float[]\"))(3);" + 12.584 - "t_float_arr[0] = 9.0;" + 12.585 - "t_float_arr[1] = 5.12345;" + 12.586 - "t_float_arr[2] = -60.03;" + 12.587 - "o.publicFloatArray = t_float_arr;"); 12.588 - assertArrayEquals(new float[] { 9.0f, 5.12345f, -60.03f }, o.publicFloatArray, 1e-10f); 12.589 - e.eval("o.publicFloatArray[0] = -513.2;"); 12.590 - assertArrayEquals(new float[] { -513.2f, 5.12345f, -60.03f }, o.publicFloatArray, 1e-10f); 12.591 - } 12.592 - 12.593 - @Test 12.594 - public void accessStaticFieldFloat() throws ScriptException { 12.595 - e.eval("var ps_float = SharedObject.publicStaticFloat;"); 12.596 - assertEquals((double)SharedObject.publicStaticFloat, ((Number)e.get("ps_float")).doubleValue()); 12.597 - SharedObject.publicStaticFloat = 0.0f / 0.0f; 12.598 - assertEquals(true, e.eval("isNaN(SharedObject.publicStaticFloat)")); 12.599 - SharedObject.publicStaticFloat = 1.0f / 0.0f; 12.600 - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticFloat")); 12.601 - SharedObject.publicStaticFloat = -1.0f / 0.0f; 12.602 - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticFloat")); 12.603 - e.eval("SharedObject.publicStaticFloat = 20.0;"); 12.604 - assertEquals(20.0f, SharedObject.publicStaticFloat, 1e-10); 12.605 - e.eval("SharedObject.publicStaticFloat = 0.0/0.0;"); 12.606 - assertTrue(Float.isNaN(SharedObject.publicStaticFloat)); 12.607 - e.eval("SharedObject.publicStaticFloat = 1.0/0.0;"); 12.608 - assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); 12.609 - e.eval("SharedObject.publicStaticFloat = -1.0/0.0;"); 12.610 - assertEquals(Float.floatToIntBits(Float.NEGATIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); 12.611 - } 12.612 - 12.613 - @Test 12.614 - public void accessStaticFieldFloatArray() throws ScriptException { 12.615 - e.eval("var ps_float_array = SharedObject.publicStaticFloatArray;"); 12.616 - assertEquals(SharedObject.publicStaticFloatArray[0], e.eval("SharedObject.publicStaticFloatArray[0];")); 12.617 - assertArrayEquals(SharedObject.publicStaticFloatArray, (float[])e.get("ps_float_array"), 1e-10f); 12.618 - e.eval("var ts_float_arr = new (Java.type(\"float[]\"))(3);" + 12.619 - "ts_float_arr[0] = 9.0;" + 12.620 - "ts_float_arr[1] = 5.12345;" + 12.621 - "ts_float_arr[2] = -60.03;" + 12.622 - "SharedObject.publicStaticFloatArray = ts_float_arr;"); 12.623 - assertArrayEquals(new float[] { 9.0f, 5.12345f, -60.03f }, SharedObject.publicStaticFloatArray, 1e-10f); 12.624 - e.eval("SharedObject.publicStaticFloatArray[0] = -513.2;"); 12.625 - assertArrayEquals(new float[] { -513.2f, 5.12345f, -60.03f }, SharedObject.publicStaticFloatArray, 1e-10f); 12.626 - } 12.627 - 12.628 - @Test 12.629 - public void accessFinalFloat() throws ScriptException { 12.630 - e.eval("var pf_float = o.publicFinalFloat;"); 12.631 - assertEquals((double)o.publicFinalFloat, ((Number)e.get("pf_float")).doubleValue()); 12.632 - e.eval("o.publicFinalFloat = 20.0;"); 12.633 - assertEquals(7.72e8f, o.publicFinalFloat, 1e-10); 12.634 - } 12.635 - 12.636 - @Test 12.637 - public void accessFinalFloatArray() throws ScriptException { 12.638 - e.eval("var pf_float_array = o.publicFinalFloatArray;"); 12.639 - assertEquals(o.publicFinalFloatArray[0], e.eval("o.publicFinalFloatArray[0];")); 12.640 - assertArrayEquals(o.publicFinalFloatArray, (float[])e.get("pf_float_array"), 1e-10f); 12.641 - e.eval("var tf_float_arr = new (Java.type(\"float[]\"))(3);" + 12.642 - "tf_float_arr[0] = 9.0;" + 12.643 - "tf_float_arr[1] = 5.12345;" + 12.644 - "tf_float_arr[2] = -60.03;" + 12.645 - "o.publicFinalFloatArray = tf_float_arr;"); 12.646 - assertArrayEquals(new float[] { -131.012f, 189.32f, -31.32e8f, 3.72f }, o.publicFinalFloatArray, 1e-10f); 12.647 - e.eval("o.publicFinalFloatArray[0] = -513.2;"); 12.648 - assertEquals(-513.2f, o.publicFinalFloatArray[0], 1e-10f); 12.649 - } 12.650 - 12.651 - @Test 12.652 - public void accessStaticFinalFieldFloat() throws ScriptException { 12.653 - e.eval("var psf_float = SharedObject.publicStaticFinalFloat;"); 12.654 - assertEquals((double)SharedObject.publicStaticFinalFloat, ((Number)e.get("psf_float")).doubleValue()); 12.655 - e.eval("SharedObject.publicStaticFinalFloat = 20.0;"); 12.656 - assertEquals(0.72e8f, SharedObject.publicStaticFinalFloat, 1e-10); 12.657 - } 12.658 - 12.659 - @Test 12.660 - public void accessStaticFinalFieldFloatArray() throws ScriptException { 12.661 - e.eval("var psf_float_array = SharedObject.publicStaticFinalFloatArray;"); 12.662 - assertEquals(SharedObject.publicStaticFinalFloatArray[0], e.eval("SharedObject.publicStaticFinalFloatArray[0];")); 12.663 - assertArrayEquals(SharedObject.publicStaticFinalFloatArray, (float[])e.get("psf_float_array"), 1e-10f); 12.664 - e.eval("var tsf_float_arr = new (Java.type(\"float[]\"))(3);" + 12.665 - "tsf_float_arr[0] = 9.0;" + 12.666 - "tsf_float_arr[1] = 5.12345;" + 12.667 - "tsf_float_arr[2] = -60.03;" + 12.668 - "SharedObject.publicStaticFinalFloatArray = tsf_float_arr;"); 12.669 - assertArrayEquals(new float[] { -8131.012f, 9.32f, -138.32e8f, 0.72f }, SharedObject.publicStaticFinalFloatArray, 1e-10f); 12.670 - e.eval("SharedObject.publicStaticFinalFloatArray[0] = -513.2;"); 12.671 - assertEquals(-513.2f, SharedObject.publicStaticFinalFloatArray[0], 1e-10f); 12.672 - } 12.673 - 12.674 - // --------------------------------double 12.675 - // tests------------------------------------ 12.676 - @Test 12.677 - public void accessFieldDouble() throws ScriptException { 12.678 - e.eval("var p_double = o.publicDouble;"); 12.679 - assertEquals(o.publicDouble, e.get("p_double")); 12.680 - o.publicDouble = 0.0 / 0.0; 12.681 - assertEquals(true, e.eval("isNaN(o.publicDouble)")); 12.682 - o.publicDouble = 1.0 / 0.0; 12.683 - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicDouble")); 12.684 - o.publicDouble = -1.0 / 0.0; 12.685 - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicDouble")); 12.686 - e.eval("o.publicDouble = 30;"); 12.687 - assertEquals(Double.doubleToLongBits(30.0), Double.doubleToLongBits(o.publicDouble)); 12.688 - e.eval("o.publicDouble = 0.0/0.0;"); 12.689 - assertTrue(Double.isNaN(o.publicDouble)); 12.690 - e.eval("o.publicDouble = 1.0/0.0;"); 12.691 - assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(o.publicDouble)); 12.692 - e.eval("o.publicDouble = -1.0/0.0;"); 12.693 - assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(o.publicDouble)); 12.694 - } 12.695 - 12.696 - @Test 12.697 - public void accessFieldDoubleArrayRead() throws ScriptException { 12.698 - e.eval("var p_double_array = o.publicDoubleArray;"); 12.699 - assertEquals(o.publicDoubleArray[0], e.eval("o.publicDoubleArray[0];")); 12.700 - assertArrayEquals(o.publicDoubleArray, (double[])e.get("p_double_array"), 1e-10); 12.701 - e.eval("var t_double_arr = new (Java.type(\"double[]\"))(3);" + 12.702 - "t_double_arr[0] = 9e10;" + 12.703 - "t_double_arr[1] = 0.677777;" + 12.704 - "t_double_arr[2] = -0.0000001;" + 12.705 - "o.publicDoubleArray = t_double_arr;"); 12.706 - assertArrayEquals(new double[] { 9e10, 0.677777, -0.0000001 }, o.publicDoubleArray, 1e-10f); 12.707 - e.eval("o.publicDoubleArray[0] = -5.2e10;"); 12.708 - assertEquals(-5.2e10, o.publicDoubleArray[0], 1e-10f); 12.709 - } 12.710 - 12.711 - @Test 12.712 - public void accessStaticFieldDouble() throws ScriptException { 12.713 - e.eval("var ps_double = SharedObject.publicStaticDouble;"); 12.714 - assertEquals(SharedObject.publicStaticDouble, e.get("ps_double")); 12.715 - SharedObject.publicStaticDouble = 0.0 / 0.0; 12.716 - assertEquals(true, e.eval("isNaN(SharedObject.publicStaticDouble)")); 12.717 - SharedObject.publicStaticDouble = 1.0 / 0.0; 12.718 - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticDouble")); 12.719 - SharedObject.publicStaticDouble = -1.0 / 0.0; 12.720 - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticDouble")); 12.721 - e.eval("SharedObject.publicStaticDouble = 40.0;"); 12.722 - assertEquals(Double.doubleToLongBits(40.0), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 12.723 - e.eval("SharedObject.publicStaticDouble = 0.0/0.0;"); 12.724 - assertTrue(Double.isNaN(SharedObject.publicStaticDouble)); 12.725 - e.eval("SharedObject.publicStaticDouble = 1.0/0.0;"); 12.726 - assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 12.727 - e.eval("SharedObject.publicStaticDouble = -1.0/0.0;"); 12.728 - assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 12.729 - } 12.730 - 12.731 - @Test 12.732 - public void accessStaticFieldDoubleArrayRead() throws ScriptException { 12.733 - e.eval("var ps_double_array = SharedObject.publicStaticDoubleArray;"); 12.734 - assertEquals(SharedObject.publicStaticDoubleArray[0], e.eval("SharedObject.publicStaticDoubleArray[0];")); 12.735 - assertArrayEquals(SharedObject.publicStaticDoubleArray, (double[])e.get("ps_double_array"), 1e-10); 12.736 - e.eval("var ts_double_arr = new (Java.type(\"double[]\"))(3);" + 12.737 - "ts_double_arr[0] = 9e10;" + 12.738 - "ts_double_arr[1] = 0.677777;" + 12.739 - "ts_double_arr[2] = -0.0000001;" + 12.740 - "SharedObject.publicStaticDoubleArray = ts_double_arr;"); 12.741 - assertArrayEquals(new double[] { 9e10, 0.677777, -0.0000001 }, SharedObject.publicStaticDoubleArray, 1e-10f); 12.742 - e.eval("SharedObject.publicStaticDoubleArray[0] = -5.2e10;"); 12.743 - assertEquals(-5.2e10, SharedObject.publicStaticDoubleArray[0], 1e-10f); 12.744 - } 12.745 - 12.746 - @Test 12.747 - public void accessFinalFieldDouble() throws ScriptException { 12.748 - e.eval("var pf_double = o.publicFinalDouble;"); 12.749 - assertEquals(o.publicFinalDouble, e.get("pf_double")); 12.750 - e.eval("o.publicFinalDouble = 30.0;"); 12.751 - assertEquals(Double.doubleToLongBits(1.3412e20), Double.doubleToLongBits(o.publicFinalDouble)); 12.752 - } 12.753 - 12.754 - @Test 12.755 - public void accessFinalFieldDoubleArrayRead() throws ScriptException { 12.756 - e.eval("var pf_double_array = o.publicFinalDoubleArray;"); 12.757 - assertEquals(o.publicFinalDoubleArray[0], e.eval("o.publicFinalDoubleArray[0];")); 12.758 - assertArrayEquals(o.publicFinalDoubleArray, (double[])e.get("pf_double_array"), 1e-10); 12.759 - e.eval("var tf_double_arr = new (Java.type(\"double[]\"))(3);" + 12.760 - "tf_double_arr[0] = 9e10;" + 12.761 - "tf_double_arr[1] = 0.677777;" + 12.762 - "tf_double_arr[2] = -0.0000001;" + 12.763 - "o.publicFinalDoubleArray = tf_double_arr;"); 12.764 - assertArrayEquals(new double[] { 0.725e80, 0.12e10, 8e-3, 1.00077 }, o.publicFinalDoubleArray, 1e-10f); 12.765 - e.eval("o.publicFinalDoubleArray[0] = -5.2e10;"); 12.766 - assertEquals(-5.2e10, o.publicFinalDoubleArray[0], 1e-10f); 12.767 - } 12.768 - 12.769 - @Test 12.770 - public void accessStaticFinalFieldDouble() throws ScriptException { 12.771 - e.eval("var psf_double = SharedObject.publicStaticFinalDouble;"); 12.772 - assertEquals(SharedObject.publicStaticFinalDouble, e.get("psf_double")); 12.773 - e.eval("SharedObject.publicStaticFinalDouble = 40.0;"); 12.774 - assertEquals(Double.doubleToLongBits(1.8e12), Double.doubleToLongBits(SharedObject.publicStaticFinalDouble)); 12.775 - } 12.776 - 12.777 - @Test 12.778 - public void accessStaticFinalFieldDoubleArrayRead() throws ScriptException { 12.779 - e.eval("var psf_double_array = SharedObject.publicStaticFinalDoubleArray;"); 12.780 - assertEquals(SharedObject.publicStaticFinalDoubleArray[0], e.eval("SharedObject.publicStaticFinalDoubleArray[0];")); 12.781 - assertArrayEquals(SharedObject.publicStaticFinalDoubleArray, (double[])e.get("psf_double_array"), 1e-10); 12.782 - e.eval("var tsf_double_arr = new (Java.type(\"double[]\"))(3);" + 12.783 - "tsf_double_arr[0] = 9e10;" + 12.784 - "tsf_double_arr[1] = 0.677777;" + 12.785 - "tsf_double_arr[2] = -0.0000001;" + 12.786 - "SharedObject.publicStaticFinalDoubleArray = tsf_double_arr;"); 12.787 - assertArrayEquals(new double[] { 8.725e80, 0.82e10, 18e-3, 1.08077 }, SharedObject.publicStaticFinalDoubleArray, 1e-10f); 12.788 - e.eval("SharedObject.publicStaticFinalDoubleArray[0] = -5.2e10;"); 12.789 - assertEquals(-5.2e10, SharedObject.publicStaticFinalDoubleArray[0], 1e-10f); 12.790 - } 12.791 - 12.792 -}
13.1 --- a/test/src/jdk/nashorn/api/javaaccess/NumberBoxingTest.java Wed Apr 01 13:22:52 2015 -0700 13.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 13.3 @@ -1,365 +0,0 @@ 13.4 -/* 13.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 13.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 13.7 - * 13.8 - * This code is free software; you can redistribute it and/or modify it 13.9 - * under the terms of the GNU General Public License version 2 only, as 13.10 - * published by the Free Software Foundation. Oracle designates this 13.11 - * particular file as subject to the "Classpath" exception as provided 13.12 - * by Oracle in the LICENSE file that accompanied this code. 13.13 - * 13.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 13.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13.17 - * version 2 for more details (a copy is included in the LICENSE file that 13.18 - * accompanied this code). 13.19 - * 13.20 - * You should have received a copy of the GNU General Public License version 13.21 - * 2 along with this work; if not, write to the Free Software Foundation, 13.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 13.23 - * 13.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 13.25 - * or visit www.oracle.com if you need additional information or have any 13.26 - * questions. 13.27 - */ 13.28 - 13.29 -package jdk.nashorn.api.javaaccess; 13.30 - 13.31 -import static org.testng.AssertJUnit.assertEquals; 13.32 -import static org.testng.AssertJUnit.assertTrue; 13.33 -import javax.script.ScriptEngine; 13.34 -import javax.script.ScriptEngineManager; 13.35 -import javax.script.ScriptException; 13.36 -import org.testng.TestNG; 13.37 -import org.testng.annotations.AfterClass; 13.38 -import org.testng.annotations.BeforeClass; 13.39 -import org.testng.annotations.Test; 13.40 - 13.41 -/** 13.42 - * @test 13.43 - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.NumberBoxingTest 13.44 - * @run testng/othervm jdk.nashorn.api.javaaccess.NumberBoxingTest 13.45 - */ 13.46 -@SuppressWarnings("javadoc") 13.47 -public class NumberBoxingTest { 13.48 - 13.49 - private static ScriptEngine e; 13.50 - private static SharedObject o; 13.51 - 13.52 - public static void main(final String[] args) { 13.53 - TestNG.main(args); 13.54 - } 13.55 - 13.56 - @BeforeClass 13.57 - public static void setUpClass() throws ScriptException { 13.58 - final ScriptEngineManager m = new ScriptEngineManager(); 13.59 - e = m.getEngineByName("nashorn"); 13.60 - o = new SharedObject(); 13.61 - e.put("o", o); 13.62 - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 13.63 - } 13.64 - 13.65 - @AfterClass 13.66 - public static void tearDownClass() { 13.67 - e = null; 13.68 - o = null; 13.69 - } 13.70 - 13.71 - // --------------------------------long 13.72 - // tests------------------------------------ 13.73 - @Test 13.74 - public void accessFieldLongBoxing() throws ScriptException { 13.75 - e.eval("var p_long = o.publicLongBox;"); 13.76 - assertEquals(o.publicLongBox, e.get("p_long")); 13.77 - e.eval("o.publicLongBox = 12;"); 13.78 - assertEquals(Long.valueOf(12), o.publicLongBox); 13.79 - } 13.80 - 13.81 - @Test 13.82 - public void accessStaticFieldLongBoxing() throws ScriptException { 13.83 - e.eval("var ps_long = SharedObject.publicStaticLongBox;"); 13.84 - assertEquals(SharedObject.publicStaticLongBox, e.get("ps_long")); 13.85 - e.eval("SharedObject.publicStaticLongBox = 120;"); 13.86 - assertEquals(120L, SharedObject.publicStaticLongBox.longValue()); 13.87 - } 13.88 - 13.89 - @Test 13.90 - public void accessFinalFieldLongBoxing() throws ScriptException { 13.91 - e.eval("var pf_long = o.publicFinalLongBox;"); 13.92 - assertEquals(o.publicFinalLongBox, e.get("pf_long")); 13.93 - e.eval("o.publicFinalLongBox = 120;"); 13.94 - assertEquals(Long.valueOf(9377333334L), o.publicFinalLongBox); 13.95 - } 13.96 - 13.97 - @Test 13.98 - public void accessStaticFinalFieldLongBoxing() throws ScriptException { 13.99 - e.eval("var psf_long = SharedObject.publicStaticFinalLong;"); 13.100 - assertEquals(SharedObject.publicStaticFinalLong, e.get("psf_long")); 13.101 - e.eval("SharedObject.publicStaticFinalLong = 120;"); 13.102 - assertEquals(8333333333333L, SharedObject.publicStaticFinalLong); 13.103 - } 13.104 - 13.105 - // --------------------------------int 13.106 - // tests------------------------------------ 13.107 - @Test 13.108 - public void accessFieldIntBoxing() throws ScriptException { 13.109 - e.eval("var p_int = o.publicIntBox;"); 13.110 - assertEquals(o.publicIntBox, e.get("p_int")); 13.111 - e.eval("o.publicIntBox = 14;"); 13.112 - assertEquals(Integer.valueOf(14), o.publicIntBox); 13.113 - } 13.114 - 13.115 - @Test 13.116 - public void accessStaticFieldIntBoxing() throws ScriptException { 13.117 - e.eval("var ps_int = SharedObject.publicStaticInt;"); 13.118 - assertEquals(SharedObject.publicStaticInt, e.get("ps_int")); 13.119 - e.eval("SharedObject.publicStaticInt = 140;"); 13.120 - assertEquals(140, SharedObject.publicStaticInt); 13.121 - } 13.122 - 13.123 - @Test 13.124 - public void accessFinalFieldIntBoxing() throws ScriptException { 13.125 - e.eval("var pf_int = o.publicFinalIntBox;"); 13.126 - assertEquals(o.publicFinalIntBox, e.get("pf_int")); 13.127 - e.eval("o.publicFinalIntBox = 10;"); 13.128 - assertEquals(Integer.valueOf(207512301), o.publicFinalIntBox); 13.129 - } 13.130 - 13.131 - @Test 13.132 - public void accessStaticFinalFieldIntBoxing() throws ScriptException { 13.133 - e.eval("var psf_int = SharedObject.publicStaticFinalInt;"); 13.134 - assertEquals(SharedObject.publicStaticFinalInt, e.get("psf_int")); 13.135 - e.eval("SharedObject.publicStaticFinalInt = 140;"); 13.136 - assertEquals(207182023, SharedObject.publicStaticFinalInt); 13.137 - } 13.138 - 13.139 - // --------------------------------byte 13.140 - // tests------------------------------------ 13.141 - @Test 13.142 - public void accessFieldByteBoxing() throws ScriptException { 13.143 - e.eval("var p_byte = o.publicByteBox;"); 13.144 - assertEqualsDouble(o.publicByteBox, "p_byte"); 13.145 - e.eval("o.publicByteBox = 16;"); 13.146 - assertEquals(Byte.valueOf((byte)16), o.publicByteBox); 13.147 - } 13.148 - 13.149 - @Test 13.150 - public void accessStaticFieldByteBoxing() throws ScriptException { 13.151 - e.eval("var ps_byte = SharedObject.publicStaticByte;"); 13.152 - assertEqualsDouble(SharedObject.publicStaticByte, "ps_byte"); 13.153 - e.eval("SharedObject.publicStaticByte = 16;"); 13.154 - assertEquals(16, SharedObject.publicStaticByte); 13.155 - } 13.156 - 13.157 - @Test 13.158 - public void accessFinalFieldByteBoxing() throws ScriptException { 13.159 - e.eval("var pf_byte = o.publicFinalByteBox;"); 13.160 - assertEqualsDouble(o.publicFinalByteBox, "pf_byte"); 13.161 - e.eval("o.publicFinalByteBox = 16;"); 13.162 - assertEquals(Byte.valueOf((byte)19), o.publicFinalByteBox); 13.163 - } 13.164 - 13.165 - @Test 13.166 - public void accessStaticFinalFieldByteBoxing() throws ScriptException { 13.167 - e.eval("var psf_byte = SharedObject.publicStaticFinalByte;"); 13.168 - assertEqualsDouble(SharedObject.publicStaticFinalByte, "psf_byte"); 13.169 - e.eval("SharedObject.publicStaticFinalByte = 16;"); 13.170 - assertEquals(-70, SharedObject.publicStaticFinalByte); 13.171 - } 13.172 - 13.173 - // --------------------------------short 13.174 - // tests------------------------------------ 13.175 - @Test 13.176 - public void accessFieldShortBoxing() throws ScriptException { 13.177 - e.eval("var p_short = o.publicShortBox;"); 13.178 - assertEqualsDouble(o.publicShortBox, "p_short"); 13.179 - e.eval("o.publicShortBox = 18;"); 13.180 - assertEquals(Short.valueOf((short)18), o.publicShortBox); 13.181 - } 13.182 - 13.183 - private static void assertEqualsDouble(final Number n, final String name) { 13.184 - assertEquals(n.doubleValue(), ((Number)e.get(name)).doubleValue()); 13.185 - } 13.186 - 13.187 - @Test 13.188 - public void accessStaticFieldShortBoxing() throws ScriptException { 13.189 - e.eval("var ps_short = SharedObject.publicStaticShort;"); 13.190 - assertEqualsDouble(SharedObject.publicStaticShort, "ps_short"); 13.191 - e.eval("SharedObject.publicStaticShort = 180;"); 13.192 - assertEquals(180, SharedObject.publicStaticShort); 13.193 - } 13.194 - 13.195 - @Test 13.196 - public void accessFinalFieldShortBoxing() throws ScriptException { 13.197 - e.eval("var pf_short = o.publicFinalShortBox;"); 13.198 - assertEqualsDouble(o.publicFinalShortBox, "pf_short"); 13.199 - e.eval("o.publicFinalShortBox = 180;"); 13.200 - assertEquals(Short.valueOf((short)-26777), o.publicFinalShortBox); 13.201 - } 13.202 - 13.203 - @Test 13.204 - public void accessStaticFinalFieldShortBoxing() throws ScriptException { 13.205 - e.eval("var psf_short = SharedObject.publicStaticFinalShort;"); 13.206 - assertEqualsDouble(SharedObject.publicStaticFinalShort, "psf_short"); 13.207 - e.eval("SharedObject.publicStaticFinalShort = 180;"); 13.208 - assertEquals(8888, SharedObject.publicStaticFinalShort); 13.209 - } 13.210 - 13.211 - // --------------------------------char 13.212 - // tests------------------------------------ 13.213 - @Test 13.214 - public void accessFieldCharBoxing() throws ScriptException { 13.215 - e.eval("var p_char = o.publicCharBox;"); 13.216 - assertEquals(o.publicCharBox, e.get("p_char")); 13.217 - e.eval("o.publicCharBox = 'S';"); 13.218 - assertEquals(Character.valueOf('S'), o.publicCharBox); 13.219 - e.eval("try {" + 13.220 - " o.publicCharBox = 'Big string';" + 13.221 - "} catch(e) {" + 13.222 - " var isThrown = true;" + 13.223 - "}"); 13.224 - assertEquals("Exception thrown", true, e.get("isThrown")); 13.225 - assertEquals(Character.valueOf('S'), o.publicCharBox); 13.226 - } 13.227 - 13.228 - @Test 13.229 - public void accessStaticFieldCharBoxing() throws ScriptException { 13.230 - e.eval("var ps_char = SharedObject.publicStaticChar;"); 13.231 - assertEquals(SharedObject.publicStaticChar, e.get("ps_char")); 13.232 - e.eval("SharedObject.publicStaticChar = 'Z';"); 13.233 - assertEquals('Z', SharedObject.publicStaticChar); 13.234 - } 13.235 - 13.236 - @Test 13.237 - public void accessFinalFieldCharBoxing() throws ScriptException { 13.238 - e.eval("var pf_char = o.publicFinalCharBox;"); 13.239 - assertEquals(o.publicFinalCharBox, e.get("pf_char")); 13.240 - e.eval("o.publicFinalCharBox = 'S';"); 13.241 - assertEquals(Character.valueOf('F'), o.publicFinalCharBox); 13.242 - } 13.243 - 13.244 - @Test 13.245 - public void accessStaticFinalFieldCharBoxing() throws ScriptException { 13.246 - e.eval("var psf_char = SharedObject.publicStaticFinalChar;"); 13.247 - assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char")); 13.248 - e.eval("SharedObject.publicStaticFinalChar = 'Z';"); 13.249 - assertEquals('K', SharedObject.publicStaticFinalChar); 13.250 - } 13.251 - 13.252 - // --------------------------------float 13.253 - // tests------------------------------------ 13.254 - @Test 13.255 - public void accessFieldFloatBoxing() throws ScriptException { 13.256 - e.eval("var p_float = o.publicFloatBox;"); 13.257 - assertEqualsDouble(o.publicFloatBox, "p_float"); 13.258 - o.publicFloatBox = 0.0f / 0.0f; 13.259 - assertEquals(true, e.eval("isNaN(o.publicFloatBox)")); 13.260 - o.publicFloatBox = 1.0f / 0.0f; 13.261 - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicFloatBox")); 13.262 - o.publicFloatBox = -1.0f / 0.0f; 13.263 - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicFloatBox")); 13.264 - e.eval("o.publicFloatBox = 20;"); 13.265 - assertEquals(20, o.publicFloatBox, 1e-10); 13.266 - e.eval("o.publicFloatBox = 0.0/0.0;"); 13.267 - assertTrue(Float.isNaN(o.publicFloatBox)); 13.268 - e.eval("o.publicFloatBox = 1.0/0.0;"); 13.269 - assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(o.publicFloatBox)); 13.270 - e.eval("o.publicFloatBox = -1.0/0.0;"); 13.271 - assertEquals(Float.NEGATIVE_INFINITY, o.publicFloatBox, 1e-10); 13.272 - } 13.273 - 13.274 - @Test 13.275 - public void accessStaticFieldFloatBoxing() throws ScriptException { 13.276 - e.eval("var ps_float = SharedObject.publicStaticFloat;"); 13.277 - assertEqualsDouble(SharedObject.publicStaticFloat, "ps_float"); 13.278 - SharedObject.publicStaticFloat = 0.0f / 0.0f; 13.279 - assertEquals(true, e.eval("isNaN(SharedObject.publicStaticFloat)")); 13.280 - SharedObject.publicStaticFloat = 1.0f / 0.0f; 13.281 - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticFloat")); 13.282 - SharedObject.publicStaticFloat = -1.0f / 0.0f; 13.283 - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticFloat")); 13.284 - e.eval("SharedObject.publicStaticFloat = 20.0;"); 13.285 - assertEquals(20.0f, SharedObject.publicStaticFloat, 1e-10); 13.286 - e.eval("SharedObject.publicStaticFloat = 0.0/0.0;"); 13.287 - assertTrue(Float.isNaN(SharedObject.publicStaticFloat)); 13.288 - e.eval("SharedObject.publicStaticFloat = 1.0/0.0;"); 13.289 - assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); 13.290 - e.eval("SharedObject.publicStaticFloat = -1.0/0.0;"); 13.291 - assertEquals(Float.floatToIntBits(Float.NEGATIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); 13.292 - } 13.293 - 13.294 - @Test 13.295 - public void accessFinalFloatBoxing() throws ScriptException { 13.296 - e.eval("var pf_float = o.publicFinalFloatBox;"); 13.297 - assertEqualsDouble(o.publicFinalFloatBox, "pf_float"); 13.298 - e.eval("o.publicFinalFloatBox = 20.0;"); 13.299 - assertEquals(1.372e4f, o.publicFinalFloatBox, 1e-10); 13.300 - } 13.301 - 13.302 - @Test 13.303 - public void accessStaticFinalFieldFloatBoxing() throws ScriptException { 13.304 - e.eval("var psf_float = SharedObject.publicStaticFinalFloat;"); 13.305 - assertEqualsDouble(SharedObject.publicStaticFinalFloat, "psf_float"); 13.306 - e.eval("SharedObject.publicStaticFinalFloat = 20.0;"); 13.307 - assertEquals(0.72e8f, SharedObject.publicStaticFinalFloat, 1e-10); 13.308 - } 13.309 - 13.310 - // --------------------------------double 13.311 - // tests------------------------------------ 13.312 - @Test 13.313 - public void accessFieldDoubleBoxing() throws ScriptException { 13.314 - e.eval("var p_double = o.publicDoubleBox;"); 13.315 - assertEquals(o.publicDoubleBox, e.get("p_double")); 13.316 - o.publicDoubleBox = 0.0 / 0.0; 13.317 - assertEquals(true, e.eval("isNaN(o.publicDoubleBox)")); 13.318 - o.publicDoubleBox = 1.0 / 0.0; 13.319 - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicDoubleBox")); 13.320 - o.publicDoubleBox = -1.0 / 0.0; 13.321 - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicDoubleBox")); 13.322 - e.eval("o.publicDoubleBox = 30;"); 13.323 - assertEquals(Double.doubleToLongBits(30.0), Double.doubleToLongBits(o.publicDoubleBox)); 13.324 - e.eval("o.publicDoubleBox = 0.0/0.0;"); 13.325 - assertTrue(Double.isNaN(o.publicDoubleBox)); 13.326 - e.eval("o.publicDoubleBox = 1.0/0.0;"); 13.327 - assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(o.publicDoubleBox)); 13.328 - e.eval("o.publicDoubleBox = -1.0/0.0;"); 13.329 - assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(o.publicDoubleBox)); 13.330 - } 13.331 - 13.332 - @Test 13.333 - public void accessStaticFieldDoubleBoxing() throws ScriptException { 13.334 - e.eval("var ps_double = SharedObject.publicStaticDouble;"); 13.335 - assertEquals(SharedObject.publicStaticDouble, e.get("ps_double")); 13.336 - SharedObject.publicStaticDouble = 0.0 / 0.0; 13.337 - assertEquals(true, e.eval("isNaN(SharedObject.publicStaticDouble)")); 13.338 - SharedObject.publicStaticDouble = 1.0 / 0.0; 13.339 - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticDouble")); 13.340 - SharedObject.publicStaticDouble = -1.0 / 0.0; 13.341 - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticDouble")); 13.342 - e.eval("SharedObject.publicStaticDouble = 40.0;"); 13.343 - assertEquals(Double.doubleToLongBits(40.0), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 13.344 - e.eval("SharedObject.publicStaticDouble = 0.0/0.0;"); 13.345 - assertTrue(Double.isNaN(SharedObject.publicStaticDouble)); 13.346 - e.eval("SharedObject.publicStaticDouble = 1.0/0.0;"); 13.347 - assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 13.348 - e.eval("SharedObject.publicStaticDouble = -1.0/0.0;"); 13.349 - assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 13.350 - } 13.351 - 13.352 - @Test 13.353 - public void accessFinalFieldDoubleBoxing() throws ScriptException { 13.354 - e.eval("var pf_double = o.publicFinalDoubleBox;"); 13.355 - assertEquals(o.publicFinalDoubleBox, e.get("pf_double")); 13.356 - e.eval("o.publicFinalDoubleBox = 30.0;"); 13.357 - assertEquals(Double.doubleToLongBits(1.412e-12), Double.doubleToLongBits(o.publicFinalDoubleBox)); 13.358 - } 13.359 - 13.360 - @Test 13.361 - public void accessStaticFinalFieldDoubleBoxing() throws ScriptException { 13.362 - e.eval("var psf_double = SharedObject.publicStaticFinalDouble;"); 13.363 - assertEquals(SharedObject.publicStaticFinalDouble, e.get("psf_double")); 13.364 - e.eval("SharedObject.publicStaticFinalDouble = 40.0;"); 13.365 - assertEquals(Double.doubleToLongBits(1.8e12), Double.doubleToLongBits(SharedObject.publicStaticFinalDouble)); 13.366 - } 13.367 - 13.368 -}
14.1 --- a/test/src/jdk/nashorn/api/javaaccess/ObjectAccessTest.java Wed Apr 01 13:22:52 2015 -0700 14.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 14.3 @@ -1,165 +0,0 @@ 14.4 -/* 14.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 14.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 14.7 - * 14.8 - * This code is free software; you can redistribute it and/or modify it 14.9 - * under the terms of the GNU General Public License version 2 only, as 14.10 - * published by the Free Software Foundation. Oracle designates this 14.11 - * particular file as subject to the "Classpath" exception as provided 14.12 - * by Oracle in the LICENSE file that accompanied this code. 14.13 - * 14.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 14.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14.17 - * version 2 for more details (a copy is included in the LICENSE file that 14.18 - * accompanied this code). 14.19 - * 14.20 - * You should have received a copy of the GNU General Public License version 14.21 - * 2 along with this work; if not, write to the Free Software Foundation, 14.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 14.23 - * 14.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 14.25 - * or visit www.oracle.com if you need additional information or have any 14.26 - * questions. 14.27 - */ 14.28 - 14.29 -package jdk.nashorn.api.javaaccess; 14.30 - 14.31 -import static org.testng.AssertJUnit.assertEquals; 14.32 -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; 14.33 -import javax.script.ScriptEngine; 14.34 -import javax.script.ScriptEngineManager; 14.35 -import javax.script.ScriptException; 14.36 -import org.testng.TestNG; 14.37 -import org.testng.annotations.AfterClass; 14.38 -import org.testng.annotations.BeforeClass; 14.39 -import org.testng.annotations.Test; 14.40 - 14.41 -/** 14.42 - * @test 14.43 - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.ObjectAccessTest 14.44 - * @run testng/othervm jdk.nashorn.api.javaaccess.ObjectAccessTest 14.45 - */ 14.46 -@SuppressWarnings("javadoc") 14.47 -public class ObjectAccessTest { 14.48 - 14.49 - private static ScriptEngine e = null; 14.50 - private static SharedObject o = null; 14.51 - 14.52 - public static void main(final String[] args) { 14.53 - TestNG.main(args); 14.54 - } 14.55 - 14.56 - @BeforeClass 14.57 - public static void setUpClass() throws ScriptException { 14.58 - final ScriptEngineManager m = new ScriptEngineManager(); 14.59 - e = m.getEngineByName("nashorn"); 14.60 - o = new SharedObject(); 14.61 - e.put("o", o); 14.62 - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 14.63 - e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;"); 14.64 - } 14.65 - 14.66 - @AfterClass 14.67 - public static void tearDownClass() { 14.68 - e = null; 14.69 - o = null; 14.70 - } 14.71 - 14.72 - @Test 14.73 - public void accessFieldObject() throws ScriptException { 14.74 - e.eval("var p_object = o.publicObject;"); 14.75 - assertEquals(o.publicObject, e.get("p_object")); 14.76 - assertEquals("object", e.eval("typeof p_object;")); 14.77 - e.eval("o.publicObject = new Person(14);"); 14.78 - assertEquals(new Person(14), o.publicObject); 14.79 - } 14.80 - 14.81 - @Test 14.82 - public void accessFieldObjectArray() throws ScriptException { 14.83 - e.eval("var p_object_array = o.publicObjectArray;"); 14.84 - assertEquals(o.publicObjectArray[0], e.eval("o.publicObjectArray[0]")); 14.85 - assertArrayEquals(o.publicObjectArray, (Object[])e.get("p_object_array")); 14.86 - e.eval("var t_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + 14.87 - "t_object_arr[0] = new Person(100);" + 14.88 - "t_object_arr[1] = new Person(120);" + 14.89 - "t_object_arr[2] = new Person(140);" + 14.90 - "o.publicObjectArray = t_object_arr;"); 14.91 - assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, o.publicObjectArray); 14.92 - e.eval("o.publicObjectArray[0] = new Person(10);"); 14.93 - assertEquals(new Person(10), o.publicObjectArray[0]); 14.94 - } 14.95 - 14.96 - @Test 14.97 - public void accessStaticFieldObject() throws ScriptException { 14.98 - e.eval("var ps_object = SharedObject.publicStaticObject;"); 14.99 - assertEquals(SharedObject.publicStaticObject, e.get("ps_object")); 14.100 - assertEquals("object", e.eval("typeof ps_object;")); 14.101 - e.eval("SharedObject.publicStaticObject = new Person(16);"); 14.102 - assertEquals(new Person(16), SharedObject.publicStaticObject); 14.103 - } 14.104 - 14.105 - @Test 14.106 - public void accessStaticFieldObjectArray() throws ScriptException { 14.107 - e.eval("var ps_object_array = SharedObject.publicStaticObjectArray;"); 14.108 - assertEquals(SharedObject.publicStaticObjectArray[0], e.eval("SharedObject.publicStaticObjectArray[0]")); 14.109 - assertArrayEquals(SharedObject.publicStaticObjectArray, (Object[])e.get("ps_object_array")); 14.110 - e.eval("var ts_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + 14.111 - "ts_object_arr[0] = new Person(100);" + 14.112 - "ts_object_arr[1] = new Person(120);" + 14.113 - "ts_object_arr[2] = new Person(140);" + 14.114 - "SharedObject.publicStaticObjectArray = ts_object_arr;"); 14.115 - assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, SharedObject.publicStaticObjectArray); 14.116 - e.eval("SharedObject.publicStaticObjectArray[0] = new Person(10);"); 14.117 - assertEquals(new Person(10), SharedObject.publicStaticObjectArray[0]); 14.118 - } 14.119 - 14.120 - @Test 14.121 - public void accessFinalFieldObject() throws ScriptException { 14.122 - e.eval("var pf_object = o.publicFinalObject;"); 14.123 - assertEquals(o.publicFinalObject, e.get("pf_object")); 14.124 - assertEquals("object", e.eval("typeof pf_object;")); 14.125 - e.eval("o.publicFinalObject = new Person(-999);"); 14.126 - assertEquals(new Person(1024), o.publicFinalObject); 14.127 - } 14.128 - 14.129 - @Test 14.130 - public void accessFinalFieldObjectArray() throws ScriptException { 14.131 - e.eval("var pf_object_array = o.publicFinalObjectArray;"); 14.132 - assertEquals(o.publicFinalObjectArray[0], e.eval("o.publicFinalObjectArray[0]")); 14.133 - assertArrayEquals(o.publicFinalObjectArray, (Object[])e.get("pf_object_array")); 14.134 - e.eval("var tf_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + 14.135 - "tf_object_arr[0] = new Person(100);" + 14.136 - "tf_object_arr[1] = new Person(120);" + 14.137 - "tf_object_arr[2] = new Person(140);" + 14.138 - "o.publicOFinalbjectArray = tf_object_arr;"); 14.139 - assertArrayEquals(new Person[] { new Person(-900), new Person(1000), new Person(180) }, o.publicFinalObjectArray); 14.140 - e.eval("o.publicFinalObjectArray[0] = new Person(10);"); 14.141 - assertEquals(new Person(10), o.publicFinalObjectArray[0]); 14.142 - } 14.143 - 14.144 - @Test 14.145 - public void accessStaticFinalFieldObject() throws ScriptException { 14.146 - e.eval("var psf_object = SharedObject.publicStaticFinalObject;"); 14.147 - assertEquals(SharedObject.publicStaticFinalObject, e.get("psf_object")); 14.148 - assertEquals("object", e.eval("typeof psf_object;")); 14.149 - e.eval("SharedObject.publicStaticFinalObject = new Person(6);"); 14.150 - assertEquals(new Person(2048), SharedObject.publicStaticFinalObject); 14.151 - } 14.152 - 14.153 - @Test 14.154 - public void accessStaticFinalFieldObjectArray() throws ScriptException { 14.155 - e.eval("var psf_object_array = SharedObject.publicStaticFinalObjectArray;"); 14.156 - assertEquals(SharedObject.publicStaticFinalObjectArray[0], e.eval("SharedObject.publicStaticFinalObjectArray[0]")); 14.157 - assertArrayEquals(SharedObject.publicStaticFinalObjectArray, (Object[])e.get("psf_object_array")); 14.158 - e.eval("var tsf_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + 14.159 - "tsf_object_arr[0] = new Person(100);" + 14.160 - "tsf_object_arr[1] = new Person(120);" + 14.161 - "tsf_object_arr[2] = new Person(140);" + 14.162 - "SharedObject.publicStaticFinalObjectArray = tsf_object_arr;"); 14.163 - assertArrayEquals(new Person[] { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }, SharedObject.publicStaticFinalObjectArray); 14.164 - e.eval("SharedObject.publicStaticFinalObjectArray[0] = new Person(90);"); 14.165 - assertEquals(new Person(90), SharedObject.publicStaticFinalObjectArray[0]); 14.166 - } 14.167 - 14.168 -}
15.1 --- a/test/src/jdk/nashorn/api/javaaccess/Person.java Wed Apr 01 13:22:52 2015 -0700 15.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 15.3 @@ -1,59 +0,0 @@ 15.4 -/* 15.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 15.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 15.7 - * 15.8 - * This code is free software; you can redistribute it and/or modify it 15.9 - * under the terms of the GNU General Public License version 2 only, as 15.10 - * published by the Free Software Foundation. Oracle designates this 15.11 - * particular file as subject to the "Classpath" exception as provided 15.12 - * by Oracle in the LICENSE file that accompanied this code. 15.13 - * 15.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 15.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15.17 - * version 2 for more details (a copy is included in the LICENSE file that 15.18 - * accompanied this code). 15.19 - * 15.20 - * You should have received a copy of the GNU General Public License version 15.21 - * 2 along with this work; if not, write to the Free Software Foundation, 15.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 15.23 - * 15.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 15.25 - * or visit www.oracle.com if you need additional information or have any 15.26 - * questions. 15.27 - */ 15.28 - 15.29 -package jdk.nashorn.api.javaaccess; 15.30 - 15.31 -@SuppressWarnings("javadoc") 15.32 -public class Person { 15.33 - 15.34 - public int id = 0; 15.35 - 15.36 - public Person() { 15.37 - } 15.38 - 15.39 - public Person(final int code) { 15.40 - this.id = code; 15.41 - } 15.42 - 15.43 - @Override 15.44 - public boolean equals(final Object obj) { 15.45 - if (obj != null && obj instanceof Person) { 15.46 - final Person o = (Person)obj; 15.47 - return this.id == o.id; 15.48 - } 15.49 - return false; 15.50 - } 15.51 - 15.52 - @Override 15.53 - public int hashCode() { 15.54 - return id; 15.55 - } 15.56 - 15.57 - @Override 15.58 - public String toString() { 15.59 - return "Person(" + id + ")"; 15.60 - } 15.61 - 15.62 -}
16.1 --- a/test/src/jdk/nashorn/api/javaaccess/SharedObject.java Wed Apr 01 13:22:52 2015 -0700 16.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 16.3 @@ -1,467 +0,0 @@ 16.4 -/* 16.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 16.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 16.7 - * 16.8 - * This code is free software; you can redistribute it and/or modify it 16.9 - * under the terms of the GNU General Public License version 2 only, as 16.10 - * published by the Free Software Foundation. Oracle designates this 16.11 - * particular file as subject to the "Classpath" exception as provided 16.12 - * by Oracle in the LICENSE file that accompanied this code. 16.13 - * 16.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 16.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16.17 - * version 2 for more details (a copy is included in the LICENSE file that 16.18 - * accompanied this code). 16.19 - * 16.20 - * You should have received a copy of the GNU General Public License version 16.21 - * 2 along with this work; if not, write to the Free Software Foundation, 16.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 16.23 - * 16.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 16.25 - * or visit www.oracle.com if you need additional information or have any 16.26 - * questions. 16.27 - */ 16.28 - 16.29 -package jdk.nashorn.api.javaaccess; 16.30 - 16.31 -import javax.script.Invocable; 16.32 -import javax.script.ScriptEngine; 16.33 -import javax.script.ScriptException; 16.34 - 16.35 -@SuppressWarnings("javadoc") 16.36 -public class SharedObject { 16.37 - 16.38 - // Public fields 16.39 - public String publicString = "PublicString"; 16.40 - public String[] publicStringArray = { "ArrayString[0]", "ArrayString[1]", "ArrayString[2]", "ArrayString[3]" }; 16.41 - public Person publicObject = new Person(256); 16.42 - public Person[] publicObjectArray = { new Person(4), new Person(-422), new Person(14) }; 16.43 - public boolean publicBoolean = true; 16.44 - public boolean[] publicBooleanArray = { true, false, false, true }; 16.45 - public Boolean publicBooleanBox = true; 16.46 - public long publicLong = 933333333333333333L; 16.47 - public long[] publicLongArray = { 99012333333333L, -124355555L, 89777777777L }; 16.48 - public Long publicLongBox = 9333333333L; 16.49 - public int publicInt = 2076543123; 16.50 - public int[] publicIntArray = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; 16.51 - public Integer publicIntBox = 20765123; 16.52 - public byte publicByte = -128; 16.53 - public byte[] publicByteArray = { 1, 2, 4, 8, 16, 32, 64, 127, -128 }; 16.54 - public Byte publicByteBox = 127; 16.55 - public short publicShort = 32000; 16.56 - public short[] publicShortArray = { 3240, 8900, -16789, 1, 12 }; 16.57 - public Short publicShortBox = Short.MIN_VALUE; 16.58 - public float publicFloat = 0.7f; 16.59 - public float[] publicFloatArray = { -32.01f, 89.3f, -1.3e8f, 3.1f }; 16.60 - public Float publicFloatBox = 1.377e4f; 16.61 - public double publicDouble = 1.34e20; 16.62 - public double[] publicDoubleArray = { 0.75e80, 8e-43, 1.000077, 0.123e10 }; 16.63 - public Double publicDoubleBox = 1.4e-19; 16.64 - public char publicChar = 'A'; 16.65 - public char[] publicCharArray = "Hello Nashorn".toCharArray(); 16.66 - public Character publicCharBox = 'B'; 16.67 - // Public static fields 16.68 - public static String publicStaticString = "PublicStaticString"; 16.69 - public static String[] publicStaticStringArray = { "StaticArrayString[0]", "StaticArrayString[1]", "StaticArrayString[2]", "StaticArrayString[3]" }; 16.70 - public static Person publicStaticObject = new Person(512); 16.71 - public static Person[] publicStaticObjectArray = { new Person(40), new Person(-22), new Person(18) }; 16.72 - public static boolean publicStaticBoolean = true; 16.73 - public static boolean[] publicStaticBooleanArray = { false, false, false, true }; 16.74 - public static Boolean publicStaticBooleanBox = true; 16.75 - public static long publicStaticLong = 13333333333333333L; 16.76 - public static long[] publicStaticLongArray = { 19012333333333L, -224355555L, 39777777777L }; 16.77 - public static Long publicStaticLongBox = 9333333334L; 16.78 - public static int publicStaticInt = 207654323; 16.79 - public static int[] publicStaticIntArray = { 5, 8, 13, 21, 34 }; 16.80 - public static Integer publicStaticIntBox = 2075123; 16.81 - public static byte publicStaticByte = -12; 16.82 - public static byte[] publicStaticByteArray = { 16, 32, 64, 127, -128 }; 16.83 - public static Byte publicStaticByteBox = 17; 16.84 - public static short publicStaticShort = 320; 16.85 - public static short[] publicStaticShortArray = { 1240, 900, -1789, 100, 12 }; 16.86 - public static Short publicStaticShortBox = -16777; 16.87 - public static float publicStaticFloat = 7.7e8f; 16.88 - public static float[] publicStaticFloatArray = { -131.01f, 189.3f, -31.3e8f, 3.7f }; 16.89 - public static Float publicStaticFloatBox = 1.37e4f; 16.90 - public static double publicStaticDouble = 1.341e20; 16.91 - public static double[] publicStaticDoubleArray = { 0.75e80, 0.123e10, 8e-43, 1.000077 }; 16.92 - public static Double publicStaticDoubleBox = 1.41e-12; 16.93 - public static char publicStaticChar = 'C'; 16.94 - public static char[] publicStaticCharArray = "Nashorn".toCharArray(); 16.95 - public static Character publicStaticCharBox = 'D'; 16.96 - // Public final fields 16.97 - public final String publicFinalString = "PublicFinalString"; 16.98 - public final String[] publicFinalStringArray = { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" }; 16.99 - public final Person publicFinalObject = new Person(1024); 16.100 - public final Person[] publicFinalObjectArray = { new Person(-900), new Person(1000), new Person(180) }; 16.101 - public final boolean publicFinalBoolean = true; 16.102 - public final boolean[] publicFinalBooleanArray = { false, false, true, false }; 16.103 - public final Boolean publicFinalBooleanBox = true; 16.104 - public final long publicFinalLong = 13353333333333333L; 16.105 - public final long[] publicFinalLongArray = { 1901733333333L, -2247355555L, 3977377777L }; 16.106 - public final Long publicFinalLongBox = 9377333334L; 16.107 - public final int publicFinalInt = 20712023; 16.108 - public final int[] publicFinalIntArray = { 50, 80, 130, 210, 340 }; 16.109 - public final Integer publicFinalIntBox = 207512301; 16.110 - public final byte publicFinalByte = -7; 16.111 - public final byte[] publicFinalByteArray = { 1, 3, 6, 17, -128 }; 16.112 - public final Byte publicFinalByteBox = 19; 16.113 - public final short publicFinalShort = 31220; 16.114 - public final short[] publicFinalShortArray = { 12240, 9200, -17289, 1200, 12 }; 16.115 - public final Short publicFinalShortBox = -26777; 16.116 - public final float publicFinalFloat = 7.72e8f; 16.117 - public final float[] publicFinalFloatArray = { -131.012f, 189.32f, -31.32e8f, 3.72f }; 16.118 - public final Float publicFinalFloatBox = 1.372e4f; 16.119 - public final double publicFinalDouble = 1.3412e20; 16.120 - public final double[] publicFinalDoubleArray = { 0.725e80, 0.12e10, 8e-3, 1.00077 }; 16.121 - public final Double publicFinalDoubleBox = 1.412e-12; 16.122 - public final char publicFinalChar = 'E'; 16.123 - public final char[] publicFinalCharArray = "Nashorn hello".toCharArray(); 16.124 - public final Character publicFinalCharBox = 'F'; 16.125 - // Public static final fields 16.126 - public static final String publicStaticFinalString = "PublicStaticFinalString"; 16.127 - public static final String[] publicStaticFinalStringArray = { "StaticFinalArrayString[0]", "StaticFinalArrayString[1]", "StaticFinalArrayString[2]", "StaticFinalArrayString[3]" }; 16.128 - public static final Person publicStaticFinalObject = new Person(2048); 16.129 - public static final Person[] publicStaticFinalObjectArray = { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }; 16.130 - public static final boolean publicStaticFinalBoolean = true; 16.131 - public static final boolean[] publicStaticFinalBooleanArray = { false, true, false, false }; 16.132 - public static final Boolean publicStaticFinalBooleanBox = true; 16.133 - public static final long publicStaticFinalLong = 8333333333333L; 16.134 - public static final long[] publicStaticFinalLongArray = { 19017383333L, -2247358L, 39773787L }; 16.135 - public static final Long publicStaticFinalLongBox = 9377388334L; 16.136 - public static final int publicStaticFinalInt = 207182023; 16.137 - public static final int[] publicStaticFinalIntArray = { 1308, 210, 340 }; 16.138 - public static final Integer publicStaticFinalIntBox = 2078301; 16.139 - public static final byte publicStaticFinalByte = -70; 16.140 - public static final byte[] publicStaticFinalByteArray = { 17, -128, 81 }; 16.141 - public static final Byte publicStaticFinalByteBox = 91; 16.142 - public static final short publicStaticFinalShort = 8888; 16.143 - public static final short[] publicStaticFinalShortArray = { 8240, 9280, -1289, 120, 812 }; 16.144 - public static final Short publicStaticFinalShortBox = -26; 16.145 - public static final float publicStaticFinalFloat = 0.72e8f; 16.146 - public static final float[] publicStaticFinalFloatArray = { -8131.012f, 9.32f, -138.32e8f, 0.72f }; 16.147 - public static final Float publicStaticFinalFloatBox = 1.2e4f; 16.148 - public static final double publicStaticFinalDouble = 1.8e12; 16.149 - public static final double[] publicStaticFinalDoubleArray = { 8.725e80, 0.82e10, 18e-3, 1.08077 }; 16.150 - public static final Double publicStaticFinalDoubleBox = 1.5612e-13; 16.151 - public static final char publicStaticFinalChar = 'K'; 16.152 - public static final char[] publicStaticFinalCharArray = "StaticString".toCharArray(); 16.153 - public static final Character publicStaticFinalCharBox = 'L'; 16.154 - 16.155 - // Special vars 16.156 - public volatile boolean volatileBoolean = true; 16.157 - public transient boolean transientBoolean = true; 16.158 - 16.159 - // For methods testing 16.160 - public boolean isAccessed = false; 16.161 - public volatile boolean isFinished = false; 16.162 - 16.163 - private ScriptEngine engine; 16.164 - 16.165 - public ScriptEngine getEngine() { 16.166 - return engine; 16.167 - } 16.168 - 16.169 - public void setEngine(final ScriptEngine engine) { 16.170 - this.engine = engine; 16.171 - } 16.172 - 16.173 - public void voidMethod() { 16.174 - isAccessed = true; 16.175 - } 16.176 - 16.177 - public boolean booleanMethod(final boolean arg) { 16.178 - return !arg; 16.179 - } 16.180 - 16.181 - public Boolean booleanBoxingMethod(final Boolean arg) { 16.182 - return !arg.booleanValue(); 16.183 - } 16.184 - 16.185 - public boolean[] booleanArrayMethod(final boolean arg[]) { 16.186 - final boolean[] res = new boolean[arg.length]; 16.187 - for (int i = 0; i < arg.length; i++) { 16.188 - res[i] = !arg[i]; 16.189 - } 16.190 - return res; 16.191 - } 16.192 - 16.193 - public int intMethod(final int arg) { 16.194 - return arg + arg; 16.195 - } 16.196 - 16.197 - public Integer intBoxingMethod(final Integer arg) { 16.198 - return arg + arg; 16.199 - } 16.200 - 16.201 - public int[] intArrayMethod(final int arg[]) { 16.202 - final int[] res = new int[arg.length]; 16.203 - for (int i = 0; i < arg.length; i++) { 16.204 - res[i] = arg[i] * 2; 16.205 - } 16.206 - return res; 16.207 - } 16.208 - 16.209 - public long longMethod(final long arg) { 16.210 - return arg + arg; 16.211 - } 16.212 - 16.213 - public Long longBoxingMethod(final Long arg) { 16.214 - return arg + arg; 16.215 - } 16.216 - 16.217 - public long[] longArrayMethod(final long[] arg) { 16.218 - final long[] res = new long[arg.length]; 16.219 - for (int i = 0; i < arg.length; i++) { 16.220 - res[i] = arg[i] * 2; 16.221 - } 16.222 - return res; 16.223 - } 16.224 - 16.225 - public byte byteMethod(final byte arg) { 16.226 - return (byte)(arg + arg); 16.227 - } 16.228 - 16.229 - public Byte byteBoxingMethod(final Byte arg) { 16.230 - return (byte)(arg + arg); 16.231 - } 16.232 - 16.233 - public byte[] byteArrayMethod(final byte[] arg) { 16.234 - final byte[] res = new byte[arg.length]; 16.235 - for (int i = 0; i < arg.length; i++) { 16.236 - res[i] = (byte)(arg[i] * 2); 16.237 - } 16.238 - return res; 16.239 - } 16.240 - 16.241 - public char charMethod(final char arg) { 16.242 - return Character.toUpperCase(arg); 16.243 - } 16.244 - 16.245 - public Character charBoxingMethod(final Character arg) { 16.246 - return Character.toUpperCase(arg); 16.247 - } 16.248 - 16.249 - public char[] charArrayMethod(final char[] arg) { 16.250 - final char[] res = new char[arg.length]; 16.251 - for (int i = 0; i < arg.length; i++) { 16.252 - res[i] = Character.toUpperCase(arg[i]); 16.253 - } 16.254 - return res; 16.255 - } 16.256 - 16.257 - public short shortMethod(final short arg) { 16.258 - return (short)(arg + arg); 16.259 - } 16.260 - 16.261 - public Short shortBoxingMethod(final Short arg) { 16.262 - return (short)(arg + arg); 16.263 - } 16.264 - 16.265 - public short[] shortArrayMethod(final short[] arg) { 16.266 - final short[] res = new short[arg.length]; 16.267 - for (int i = 0; i < arg.length; i++) { 16.268 - res[i] = (short)(arg[i] * 2); 16.269 - } 16.270 - return res; 16.271 - } 16.272 - 16.273 - public float floatMethod(final float arg) { 16.274 - return arg + arg; 16.275 - } 16.276 - 16.277 - public Float floatBoxingMethod(final Float arg) { 16.278 - return arg + arg; 16.279 - } 16.280 - 16.281 - public float[] floatArrayMethod(final float[] arg) { 16.282 - final float[] res = new float[arg.length]; 16.283 - for (int i = 0; i < arg.length; i++) { 16.284 - res[i] = arg[i] * 2; 16.285 - } 16.286 - return res; 16.287 - } 16.288 - 16.289 - public double doubleMethod(final double arg) { 16.290 - return arg + arg; 16.291 - } 16.292 - 16.293 - public Double doubleBoxingMethod(final Double arg) { 16.294 - return arg + arg; 16.295 - } 16.296 - 16.297 - public double[] doubleArrayMethod(final double[] arg) { 16.298 - final double[] res = new double[arg.length]; 16.299 - for (int i = 0; i < arg.length; i++) { 16.300 - res[i] = arg[i] * 2; 16.301 - } 16.302 - return res; 16.303 - } 16.304 - 16.305 - public String stringMethod(final String str) { 16.306 - return str + str; 16.307 - } 16.308 - 16.309 - public String[] stringArrayMethod(final String[] arr) { 16.310 - final int l = arr.length; 16.311 - final String[] res = new String[l]; 16.312 - for (int i = 0; i < l; i++) { 16.313 - res[i] = arr[l - i - 1]; 16.314 - } 16.315 - return res; 16.316 - } 16.317 - 16.318 - public Person[] objectArrayMethod(final Person[] arr) { 16.319 - final Person[] res = new Person[arr.length]; 16.320 - for (int i = 0; i < arr.length; i++) { 16.321 - res[i] = new Person(i + 100); 16.322 - } 16.323 - return res; 16.324 - } 16.325 - 16.326 - public Person objectMethod(final Person t) { 16.327 - t.id *= 2; 16.328 - return t; 16.329 - } 16.330 - 16.331 - public int twoParamMethod(final long l, final double d) { 16.332 - return (int)(l + d); 16.333 - } 16.334 - 16.335 - public int threeParamMethod(final short s, final long l, final char c) { 16.336 - return (int)(s + l + c); 16.337 - } 16.338 - 16.339 - public Person[] twoObjectParamMethod(final Person arg1, final Person arg2) { 16.340 - return new Person[] { arg2, arg1 }; 16.341 - } 16.342 - 16.343 - public Person[] threeObjectParamMethod(final Person arg1, final Person arg2, final Person arg3) { 16.344 - return new Person[] { arg3, arg2, arg1 }; 16.345 - } 16.346 - 16.347 - public Person[] eightObjectParamMethod(final Person arg1, final Person arg2, final Person arg3, final Person arg4, final Person arg5, final Person arg6, final Person arg7, final Person arg8) { 16.348 - return new Person[] { arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 }; 16.349 - } 16.350 - 16.351 - public Person[] nineObjectParamMethod(final Person arg1, final Person arg2, final Person arg3, final Person arg4, final Person arg5, final Person arg6, final Person arg7, final Person arg8, final Person arg9) { 16.352 - return new Person[] { arg9, arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 }; 16.353 - } 16.354 - 16.355 - public Person[] methodObjectEllipsis(final Person... args) { 16.356 - final int l = args.length; 16.357 - final Person[] res = new Person[l]; 16.358 - for (int i = 0; i < l; i++) { 16.359 - res[i] = args[l - i - 1]; 16.360 - } 16.361 - return res; 16.362 - } 16.363 - 16.364 - public Person[] methodPrimitiveEllipsis(final int... args) { 16.365 - final int l = args.length; 16.366 - final Person[] res = new Person[l]; 16.367 - for (int i = 0; i < l; i++) { 16.368 - res[i] = new Person(args[i]); 16.369 - } 16.370 - return res; 16.371 - } 16.372 - 16.373 - public Object[] methodMixedEllipsis(final Object... args) { 16.374 - return args; 16.375 - } 16.376 - 16.377 - public Object[] methodObjectWithEllipsis(final String arg, final int... args) { 16.378 - final Object[] res = new Object[args.length + 1]; 16.379 - res[0] = arg; 16.380 - for (int i = 0; i < args.length; i++) { 16.381 - res[i + 1] = args[i]; 16.382 - } 16.383 - return res; 16.384 - } 16.385 - 16.386 - public Object[] methodPrimitiveWithEllipsis(final int arg, final long... args) { 16.387 - final Object[] res = new Object[args.length + 1]; 16.388 - res[0] = arg; 16.389 - for (int i = 0; i < args.length; i++) { 16.390 - res[i + 1] = args[i]; 16.391 - } 16.392 - return res; 16.393 - } 16.394 - 16.395 - public Object[] methodMixedWithEllipsis(final String arg1, final int arg2, final Object... args) { 16.396 - final Object[] res = new Object[args.length + 2]; 16.397 - res[0] = arg1; 16.398 - res[1] = arg2; 16.399 - System.arraycopy(args, 0, res, 2, args.length); 16.400 - return res; 16.401 - } 16.402 - 16.403 - public void methodStartsThread() { 16.404 - isFinished = false; 16.405 - 16.406 - final Thread t = new Thread(new Runnable() { 16.407 - @Override 16.408 - public void run() { 16.409 - try { 16.410 - Thread.sleep(1000); 16.411 - isFinished = true; 16.412 - } catch (final InterruptedException e) { 16.413 - e.printStackTrace(); 16.414 - } 16.415 - } 16.416 - }); 16.417 - 16.418 - t.start(); 16.419 - } 16.420 - 16.421 - public String overloadedMethodDoubleVSint(final int arg) { 16.422 - return "int"; 16.423 - } 16.424 - 16.425 - public String overloadedMethodDoubleVSint(final double arg) { 16.426 - return "double"; 16.427 - } 16.428 - 16.429 - public int overloadedMethod(final int arg) { 16.430 - return arg*2; 16.431 - } 16.432 - 16.433 - public int overloadedMethod(final String arg) { 16.434 - return arg.length(); 16.435 - } 16.436 - 16.437 - public int overloadedMethod(final boolean arg) { 16.438 - return (arg) ? 1 : 0; 16.439 - } 16.440 - 16.441 - public int overloadedMethod(final Person arg) { 16.442 - return arg.id*2; 16.443 - } 16.444 - 16.445 - public int firstLevelMethodInt(final int arg) throws ScriptException, NoSuchMethodException { 16.446 - return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInt", arg); 16.447 - } 16.448 - 16.449 - public int thirdLevelMethodInt(final int arg) { 16.450 - return arg*5; 16.451 - } 16.452 - 16.453 - public int firstLevelMethodInteger(final Integer arg) throws ScriptException, NoSuchMethodException { 16.454 - return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInteger", arg); 16.455 - } 16.456 - 16.457 - public int thirdLevelMethodInteger(final Integer arg) { 16.458 - return arg*10; 16.459 - } 16.460 - 16.461 - public Person firstLevelMethodObject(final Person p) throws ScriptException, NoSuchMethodException { 16.462 - return (Person) ((Invocable)engine).invokeFunction("secondLevelMethodObject", p); 16.463 - } 16.464 - 16.465 - public Person thirdLevelMethodObject(final Person p) { 16.466 - p.id *= 10; 16.467 - return p; 16.468 - } 16.469 - 16.470 -}
17.1 --- a/test/src/jdk/nashorn/api/javaaccess/StringAccessTest.java Wed Apr 01 13:22:52 2015 -0700 17.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 17.3 @@ -1,168 +0,0 @@ 17.4 -/* 17.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 17.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 17.7 - * 17.8 - * This code is free software; you can redistribute it and/or modify it 17.9 - * under the terms of the GNU General Public License version 2 only, as 17.10 - * published by the Free Software Foundation. Oracle designates this 17.11 - * particular file as subject to the "Classpath" exception as provided 17.12 - * by Oracle in the LICENSE file that accompanied this code. 17.13 - * 17.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 17.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17.17 - * version 2 for more details (a copy is included in the LICENSE file that 17.18 - * accompanied this code). 17.19 - * 17.20 - * You should have received a copy of the GNU General Public License version 17.21 - * 2 along with this work; if not, write to the Free Software Foundation, 17.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17.23 - * 17.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 17.25 - * or visit www.oracle.com if you need additional information or have any 17.26 - * questions. 17.27 - */ 17.28 - 17.29 -package jdk.nashorn.api.javaaccess; 17.30 - 17.31 -import static org.testng.AssertJUnit.assertEquals; 17.32 -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; 17.33 -import javax.script.ScriptEngine; 17.34 -import javax.script.ScriptEngineManager; 17.35 -import javax.script.ScriptException; 17.36 -import org.testng.TestNG; 17.37 -import org.testng.annotations.AfterClass; 17.38 -import org.testng.annotations.BeforeClass; 17.39 -import org.testng.annotations.Test; 17.40 - 17.41 -/** 17.42 - * @test 17.43 - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.StringAccessTest 17.44 - * @run testng/othervm jdk.nashorn.api.javaaccess.StringAccessTest 17.45 - */ 17.46 -@SuppressWarnings("javadoc") 17.47 -public class StringAccessTest { 17.48 - 17.49 - private static ScriptEngine e = null; 17.50 - private static SharedObject o = null; 17.51 - 17.52 - public static void main(final String[] args) { 17.53 - TestNG.main(args); 17.54 - } 17.55 - 17.56 - @BeforeClass 17.57 - public static void setUpClass() throws ScriptException { 17.58 - final ScriptEngineManager m = new ScriptEngineManager(); 17.59 - e = m.getEngineByName("nashorn"); 17.60 - o = new SharedObject(); 17.61 - e.put("o", o); 17.62 - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 17.63 - } 17.64 - 17.65 - @AfterClass 17.66 - public static void tearDownClass() { 17.67 - e = null; 17.68 - o = null; 17.69 - } 17.70 - 17.71 - @Test 17.72 - public void accessFieldString() throws ScriptException { 17.73 - e.eval("var p_string = o.publicString;"); 17.74 - assertEquals(o.publicString, e.get("p_string")); 17.75 - assertEquals("string", e.eval("typeof p_string;")); 17.76 - e.eval("o.publicString = 'changedString';"); 17.77 - assertEquals("changedString", o.publicString); 17.78 - } 17.79 - 17.80 - @Test 17.81 - public void accessFieldStringArray() throws ScriptException { 17.82 - e.eval("var p_string_array = o.publicStringArray;"); 17.83 - assertEquals(o.publicStringArray[0], e.eval("o.publicStringArray[0]")); 17.84 - assertArrayEquals(o.publicStringArray, (String[])e.get("p_string_array")); 17.85 - e.eval("var t_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + 17.86 - "t_string_arr[0] = 'abc';" + 17.87 - "t_string_arr[1] = '123';" + 17.88 - "t_string_arr[2] = 'xyzzzz';" + 17.89 - "o.publicStringArray = t_string_arr;"); 17.90 - assertArrayEquals(new String[] { "abc", "123", "xyzzzz" }, o.publicStringArray); 17.91 - e.eval("o.publicStringArray[0] = 'nashorn';"); 17.92 - assertEquals("nashorn", o.publicStringArray[0]); 17.93 - } 17.94 - 17.95 - @Test 17.96 - public void accessStaticFieldString() throws ScriptException { 17.97 - e.eval("var ps_string = SharedObject.publicStaticString;"); 17.98 - assertEquals(SharedObject.publicStaticString, e.get("ps_string")); 17.99 - assertEquals("string", e.eval("typeof ps_string;")); 17.100 - e.eval("SharedObject.publicStaticString = 'changedString';"); 17.101 - assertEquals("changedString", SharedObject.publicStaticString); 17.102 - } 17.103 - 17.104 - @Test 17.105 - public void accessStaticFieldStringArray() throws ScriptException { 17.106 - e.eval("var ps_string_array = SharedObject.publicStaticStringArray;"); 17.107 - assertEquals(SharedObject.publicStaticStringArray[0], e.eval("SharedObject.publicStaticStringArray[0]")); 17.108 - assertArrayEquals(SharedObject.publicStaticStringArray, (String[])e.get("ps_string_array")); 17.109 - e.eval("var ts_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + 17.110 - "ts_string_arr[0] = 'abc';" + 17.111 - "ts_string_arr[1] = '123';" + 17.112 - "ts_string_arr[2] = 'xyzzzz';" + 17.113 - "SharedObject.publicStaticStringArray = ts_string_arr;"); 17.114 - assertArrayEquals(new String[] { "abc", "123", "xyzzzz" }, SharedObject.publicStaticStringArray); 17.115 - e.eval("SharedObject.publicStaticStringArray[0] = 'nashorn';"); 17.116 - assertEquals("nashorn", SharedObject.publicStaticStringArray[0]); 17.117 - } 17.118 - 17.119 - @Test 17.120 - public void accessFinalFieldString() throws ScriptException { 17.121 - e.eval("var pf_string = o.publicFinalString;"); 17.122 - assertEquals(o.publicFinalString, e.get("pf_string")); 17.123 - assertEquals("string", e.eval("typeof pf_string;")); 17.124 - e.eval("o.publicFinalString = 'changedString';"); 17.125 - assertEquals("PublicFinalString", o.publicFinalString); 17.126 - } 17.127 - 17.128 - @Test 17.129 - public void accessFinalFieldStringArray() throws ScriptException { 17.130 - e.eval("var pf_string_array = o.publicFinalStringArray;"); 17.131 - assertEquals(o.publicFinalStringArray[0], e.eval("o.publicFinalStringArray[0]")); 17.132 - assertArrayEquals(o.publicFinalStringArray, (String[])e.get("pf_string_array")); 17.133 - e.eval("var tf_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + 17.134 - "tf_string_arr[0] = 'abc';" + 17.135 - "tf_string_arr[1] = '123';" + 17.136 - "tf_string_arr[2] = 'xyzzzz';" + 17.137 - "o.publicFinalStringArray = tf_string_arr;"); 17.138 - assertArrayEquals(new String[] { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" }, o.publicFinalStringArray); 17.139 - e.eval("o.publicFinalStringArray[0] = 'nashorn';"); 17.140 - assertEquals("nashorn", o.publicFinalStringArray[0]); 17.141 - } 17.142 - 17.143 - @Test 17.144 - public void accessStaticFinalFieldString() throws ScriptException { 17.145 - e.eval("var psf_string = SharedObject.publicStaticFinalString;"); 17.146 - assertEquals(SharedObject.publicStaticFinalString, e.get("psf_string")); 17.147 - assertEquals("string", e.eval("typeof psf_string;")); 17.148 - e.eval("SharedObject.publicStaticFinalString = 'changedString';"); 17.149 - assertEquals("PublicStaticFinalString", SharedObject.publicStaticFinalString); 17.150 - } 17.151 - 17.152 - @Test 17.153 - public void accessStaticFinalFieldStringArray() throws ScriptException { 17.154 - e.eval("var psf_string_array = SharedObject.publicStaticFinalStringArray;"); 17.155 - assertEquals(SharedObject.publicStaticFinalStringArray[0], e.eval("SharedObject.publicStaticFinalStringArray[0]")); 17.156 - assertArrayEquals(SharedObject.publicStaticFinalStringArray, (String[])e.get("psf_string_array")); 17.157 - e.eval("var tsf_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + 17.158 - "tsf_string_arr[0] = 'abc';" + 17.159 - "tsf_string_arr[1] = '123';" + 17.160 - "tsf_string_arr[2] = 'xyzzzz';" + 17.161 - "SharedObject.publicStaticFinalStringArray = tsf_string_arr;"); 17.162 - assertArrayEquals(new String[] { "StaticFinalArrayString[0]", 17.163 - "StaticFinalArrayString[1]", 17.164 - "StaticFinalArrayString[2]", 17.165 - "StaticFinalArrayString[3]" }, 17.166 - SharedObject.publicStaticFinalStringArray); 17.167 - e.eval("SharedObject.publicStaticFinalStringArray[0] = 'nashorn';"); 17.168 - assertEquals("nashorn", SharedObject.publicStaticFinalStringArray[0]); 17.169 - } 17.170 - 17.171 -}
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/ArrayConversionTest.java Mon Apr 06 16:18:54 2015 +0530 18.3 @@ -0,0 +1,235 @@ 18.4 +/* 18.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 18.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 18.7 + * 18.8 + * This code is free software; you can redistribute it and/or modify it 18.9 + * under the terms of the GNU General Public License version 2 only, as 18.10 + * published by the Free Software Foundation. Oracle designates this 18.11 + * particular file as subject to the "Classpath" exception as provided 18.12 + * by Oracle in the LICENSE file that accompanied this code. 18.13 + * 18.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 18.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 18.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 18.17 + * version 2 for more details (a copy is included in the LICENSE file that 18.18 + * accompanied this code). 18.19 + * 18.20 + * You should have received a copy of the GNU General Public License version 18.21 + * 2 along with this work; if not, write to the Free Software Foundation, 18.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18.23 + * 18.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 18.25 + * or visit www.oracle.com if you need additional information or have any 18.26 + * questions. 18.27 + */ 18.28 + 18.29 +package jdk.nashorn.api.javaaccess.test; 18.30 + 18.31 +import static org.testng.AssertJUnit.assertEquals; 18.32 +import static org.testng.AssertJUnit.assertFalse; 18.33 +import static org.testng.AssertJUnit.assertNull; 18.34 +import static org.testng.AssertJUnit.assertTrue; 18.35 +import java.util.Arrays; 18.36 +import java.util.List; 18.37 +import javax.script.ScriptContext; 18.38 +import javax.script.ScriptEngine; 18.39 +import javax.script.ScriptEngineManager; 18.40 +import javax.script.ScriptException; 18.41 +import org.testng.TestNG; 18.42 +import org.testng.annotations.AfterClass; 18.43 +import org.testng.annotations.BeforeClass; 18.44 +import org.testng.annotations.Test; 18.45 + 18.46 +@SuppressWarnings("javadoc") 18.47 +public class ArrayConversionTest { 18.48 + private static ScriptEngine e = null; 18.49 + 18.50 + public static void main(final String[] args) { 18.51 + TestNG.main(args); 18.52 + } 18.53 + 18.54 + @BeforeClass 18.55 + public static void setUpClass() { 18.56 + e = new ScriptEngineManager().getEngineByName("nashorn"); 18.57 + } 18.58 + 18.59 + @AfterClass 18.60 + public static void tearDownClass() { 18.61 + e = null; 18.62 + } 18.63 + 18.64 + @Test 18.65 + public void testIntArrays() throws ScriptException { 18.66 + runTest("assertNullIntArray", "null"); 18.67 + runTest("assertEmptyIntArray", "[]"); 18.68 + runTest("assertSingle42IntArray", "[42]"); 18.69 + runTest("assertSingle42IntArray", "['42']"); 18.70 + runTest("assertIntArrayConversions", "[false, true, NaN, Infinity, -Infinity, 0.4, 0.6, null, undefined, [], {}, [1], [1, 2]]"); 18.71 + } 18.72 + 18.73 + @Test 18.74 + public void testIntIntArrays() throws ScriptException { 18.75 + runTest("assertNullIntIntArray", "null"); 18.76 + runTest("assertEmptyIntIntArray", "[]"); 18.77 + runTest("assertSingleEmptyIntIntArray", "[[]]"); 18.78 + runTest("assertSingleNullIntIntArray", "[null]"); 18.79 + runTest("assertLargeIntIntArray", "[[false], [1], [2, 3], [4, 5, 6], ['7', {valueOf: function() { return 8 }}]]"); 18.80 + } 18.81 + 18.82 + @Test 18.83 + public void testObjectObjectArrays() throws ScriptException { 18.84 + runTest("assertLargeObjectObjectArray", "[[false], [1], ['foo', 42.3], [{x: 17}]]"); 18.85 + } 18.86 + 18.87 + @Test 18.88 + public void testBooleanArrays() throws ScriptException { 18.89 + runTest("assertBooleanArrayConversions", "[false, true, '', 'false', 0, 1, 0.4, 0.6, {}, [], [false], [true], NaN, Infinity, null, undefined]"); 18.90 + } 18.91 + 18.92 + @Test 18.93 + public void testArrayAmbiguity() throws ScriptException { 18.94 + runTest("x", "'abc'"); 18.95 + runTest("x", "['foo', 'bar']"); 18.96 + } 18.97 + 18.98 + @Test 18.99 + public void testListArrays() throws ScriptException { 18.100 + runTest("assertListArray", "[['foo', 'bar'], ['apple', 'orange']]"); 18.101 + } 18.102 + 18.103 + @Test 18.104 + public void testVarArgs() throws ScriptException { 18.105 + // Sole NativeArray in vararg position becomes vararg array itself 18.106 + runTest("assertVarArg_42_17", "[42, 17]"); 18.107 + // NativeArray in vararg position becomes an argument if there are more arguments 18.108 + runTest("assertVarArg_array_17", "[42], 18"); 18.109 + // Only NativeArray is converted to vararg array, other objects (e.g. a function) aren't 18.110 + runTest("assertVarArg_function", "function() { return 'Hello' }"); 18.111 + } 18.112 + 18.113 + private static void runTest(final String testMethodName, final String argument) throws ScriptException { 18.114 + e.eval("Java.type('" + ArrayConversionTest.class.getName() + "')." + testMethodName + "(" + argument + ")"); 18.115 + } 18.116 + 18.117 + public static void assertNullIntArray(final int[] array) { 18.118 + assertNull(array); 18.119 + } 18.120 + 18.121 + public static void assertNullIntIntArray(final int[][] array) { 18.122 + assertNull(array); 18.123 + } 18.124 + 18.125 + public static void assertEmptyIntArray(final int[] array) { 18.126 + assertEquals(0, array.length); 18.127 + } 18.128 + 18.129 + public static void assertSingle42IntArray(final int[] array) { 18.130 + assertEquals(1, array.length); 18.131 + assertEquals(42, array[0]); 18.132 + } 18.133 + 18.134 + 18.135 + public static void assertIntArrayConversions(final int[] array) { 18.136 + assertEquals(13, array.length); 18.137 + assertEquals(0, array[0]); // false 18.138 + assertEquals(1, array[1]); // true 18.139 + assertEquals(0, array[2]); // NaN 18.140 + assertEquals(0, array[3]); // Infinity 18.141 + assertEquals(0, array[4]); // -Infinity 18.142 + assertEquals(0, array[5]); // 0.4 18.143 + assertEquals(0, array[6]); // 0.6 - floor, not round 18.144 + assertEquals(0, array[7]); // null 18.145 + assertEquals(0, array[8]); // undefined 18.146 + assertEquals(0, array[9]); // [] 18.147 + assertEquals(0, array[10]); // {} 18.148 + assertEquals(1, array[11]); // [1] 18.149 + assertEquals(0, array[12]); // [1, 2] 18.150 + } 18.151 + 18.152 + public static void assertEmptyIntIntArray(final int[][] array) { 18.153 + assertEquals(0, array.length); 18.154 + } 18.155 + 18.156 + public static void assertSingleEmptyIntIntArray(final int[][] array) { 18.157 + assertEquals(1, array.length); 18.158 + assertTrue(Arrays.equals(new int[0], array[0])); 18.159 + } 18.160 + 18.161 + public static void assertSingleNullIntIntArray(final int[][] array) { 18.162 + assertEquals(1, array.length); 18.163 + assertNull(null, array[0]); 18.164 + } 18.165 + 18.166 + public static void assertLargeIntIntArray(final int[][] array) { 18.167 + assertEquals(5, array.length); 18.168 + assertTrue(Arrays.equals(new int[] { 0 }, array[0])); 18.169 + assertTrue(Arrays.equals(new int[] { 1 }, array[1])); 18.170 + assertTrue(Arrays.equals(new int[] { 2, 3 }, array[2])); 18.171 + assertTrue(Arrays.equals(new int[] { 4, 5, 6 }, array[3])); 18.172 + assertTrue(Arrays.equals(new int[] { 7, 8 }, array[4])); 18.173 + } 18.174 + 18.175 + public static void assertLargeObjectObjectArray(final Object[][] array) throws ScriptException { 18.176 + assertEquals(4, array.length); 18.177 + assertTrue(Arrays.equals(new Object[] { Boolean.FALSE }, array[0])); 18.178 + assertTrue(Arrays.equals(new Object[] { 1 }, array[1])); 18.179 + assertTrue(Arrays.equals(new Object[] { "foo", 42.3d }, array[2])); 18.180 + assertEquals(1, array[3].length); 18.181 + e.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", array[3][0]); 18.182 + assertEquals(17, e.eval("obj.x")); 18.183 + } 18.184 + 18.185 + public static void assertBooleanArrayConversions(final boolean[] array) { 18.186 + assertEquals(16, array.length); 18.187 + assertFalse(array[0]); // false 18.188 + assertTrue(array[1]); // true 18.189 + assertFalse(array[2]); // '' 18.190 + assertTrue(array[3]); // 'false' (yep, every non-empty string converts to true) 18.191 + assertFalse(array[4]); // 0 18.192 + assertTrue(array[5]); // 1 18.193 + assertTrue(array[6]); // 0.4 18.194 + assertTrue(array[7]); // 0.6 18.195 + assertTrue(array[8]); // {} 18.196 + assertTrue(array[9]); // [] 18.197 + assertTrue(array[10]); // [false] 18.198 + assertTrue(array[11]); // [true] 18.199 + assertFalse(array[12]); // NaN 18.200 + assertTrue(array[13]); // Infinity 18.201 + assertFalse(array[14]); // null 18.202 + assertFalse(array[15]); // undefined 18.203 + } 18.204 + 18.205 + public static void assertListArray(final List<?>[] array) { 18.206 + assertEquals(2, array.length); 18.207 + assertEquals(Arrays.asList("foo", "bar"), array[0]); 18.208 + assertEquals(Arrays.asList("apple", "orange"), array[1]); 18.209 + } 18.210 + 18.211 + public static void assertVarArg_42_17(final Object... args) { 18.212 + assertEquals(2, args.length); 18.213 + assertEquals(42, ((Number)args[0]).intValue()); 18.214 + assertEquals(17, ((Number)args[1]).intValue()); 18.215 + } 18.216 + 18.217 + public static void assertVarArg_array_17(final Object... args) throws ScriptException { 18.218 + assertEquals(2, args.length); 18.219 + e.getBindings(ScriptContext.ENGINE_SCOPE).put("arr", args[0]); 18.220 + assertTrue((Boolean)e.eval("arr instanceof Array && arr.length == 1 && arr[0] == 42")); 18.221 + assertEquals(18, ((Number)args[1]).intValue()); 18.222 + } 18.223 + 18.224 + public static void assertVarArg_function(final Object... args) throws ScriptException { 18.225 + assertEquals(1, args.length); 18.226 + e.getBindings(ScriptContext.ENGINE_SCOPE).put("fn", args[0]); 18.227 + assertEquals("Hello", e.eval("fn()")); 18.228 + } 18.229 + 18.230 + 18.231 + 18.232 + public static void x(final String y) { 18.233 + assertEquals("abc", y); 18.234 + } 18.235 + public static void x(final String[] y) { 18.236 + assertTrue(Arrays.equals(new String[] { "foo", "bar"}, y)); 18.237 + } 18.238 +}
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/BooleanAccessTest.java Mon Apr 06 16:18:54 2015 +0530 19.3 @@ -0,0 +1,219 @@ 19.4 +/* 19.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 19.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 19.7 + * 19.8 + * This code is free software; you can redistribute it and/or modify it 19.9 + * under the terms of the GNU General Public License version 2 only, as 19.10 + * published by the Free Software Foundation. Oracle designates this 19.11 + * particular file as subject to the "Classpath" exception as provided 19.12 + * by Oracle in the LICENSE file that accompanied this code. 19.13 + * 19.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 19.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 19.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 19.17 + * version 2 for more details (a copy is included in the LICENSE file that 19.18 + * accompanied this code). 19.19 + * 19.20 + * You should have received a copy of the GNU General Public License version 19.21 + * 2 along with this work; if not, write to the Free Software Foundation, 19.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19.23 + * 19.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19.25 + * or visit www.oracle.com if you need additional information or have any 19.26 + * questions. 19.27 + */ 19.28 + 19.29 +package jdk.nashorn.api.javaaccess.test; 19.30 + 19.31 +import static org.testng.AssertJUnit.assertEquals; 19.32 +import static org.testng.AssertJUnit.assertTrue; 19.33 +import java.util.Arrays; 19.34 +import javax.script.ScriptEngine; 19.35 +import javax.script.ScriptEngineManager; 19.36 +import javax.script.ScriptException; 19.37 +import org.testng.TestNG; 19.38 +import org.testng.annotations.AfterClass; 19.39 +import org.testng.annotations.BeforeClass; 19.40 +import org.testng.annotations.Test; 19.41 + 19.42 +/** 19.43 + * @test 19.44 + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.BooleanAccessTest 19.45 + * @run testng/othervm jdk.nashorn.api.javaaccess.BooleanAccessTest 19.46 + */ 19.47 +@SuppressWarnings("javadoc") 19.48 +public class BooleanAccessTest { 19.49 + 19.50 + private static ScriptEngine e = null; 19.51 + private static SharedObject o = null; 19.52 + 19.53 + public static void main(final String[] args) { 19.54 + TestNG.main(args); 19.55 + } 19.56 + 19.57 + @BeforeClass 19.58 + public static void setUpClass() throws ScriptException { 19.59 + final ScriptEngineManager m = new ScriptEngineManager(); 19.60 + e = m.getEngineByName("nashorn"); 19.61 + o = new SharedObject(); 19.62 + e.put("o", o); 19.63 + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 19.64 + } 19.65 + 19.66 + @AfterClass 19.67 + public static void tearDownClass() { 19.68 + e = null; 19.69 + o = null; 19.70 + } 19.71 + 19.72 + @Test 19.73 + public void accessFieldBoolean() throws ScriptException { 19.74 + e.eval("var p_boolean = o.publicBoolean;"); 19.75 + assertEquals(o.publicBoolean, e.get("p_boolean")); 19.76 + assertEquals("boolean", e.eval("typeof p_boolean;")); 19.77 + e.eval("o.publicBoolean = false;"); 19.78 + assertEquals(false, o.publicBoolean); 19.79 + } 19.80 + 19.81 + @Test 19.82 + public void accessFieldBooleanArray() throws ScriptException { 19.83 + e.eval("var p_boolean_array = o.publicBooleanArray;"); 19.84 + assertEquals(o.publicBooleanArray[0], e.eval("o.publicBooleanArray[0]")); 19.85 + assertTrue(Arrays.equals(o.publicBooleanArray, (boolean[])e.get("p_boolean_array"))); 19.86 + e.eval("var t_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + 19.87 + "t_boolean_arr[0] = true;" + 19.88 + "t_boolean_arr[1] = false;" + 19.89 + "t_boolean_arr[2] = false;" + 19.90 + "o.publicBooleanArray = t_boolean_arr;"); 19.91 + assertTrue(Arrays.equals(new boolean[] { true, false, false }, o.publicBooleanArray)); 19.92 + e.eval("o.publicBooleanArray[0] = false;"); 19.93 + assertEquals(false, o.publicBooleanArray[0]); 19.94 + } 19.95 + 19.96 + @Test 19.97 + public void accessStaticFieldBoolean() throws ScriptException { 19.98 + e.eval("var ps_boolean = SharedObject.publicStaticBoolean;"); 19.99 + assertEquals(SharedObject.publicStaticBoolean, e.get("ps_boolean")); 19.100 + assertEquals("boolean", e.eval("typeof ps_boolean;")); 19.101 + e.eval("SharedObject.publicStaticBoolean = false;"); 19.102 + assertEquals(false, SharedObject.publicStaticBoolean); 19.103 + } 19.104 + 19.105 + @Test 19.106 + public void accessStaticFieldBooleanArray() throws ScriptException { 19.107 + e.eval("var ps_boolean_array = SharedObject.publicStaticBooleanArray;"); 19.108 + assertEquals(SharedObject.publicStaticBooleanArray[0], e.eval("SharedObject.publicStaticBooleanArray[0]")); 19.109 + assertTrue(Arrays.equals(SharedObject.publicStaticBooleanArray, (boolean[])e.get("ps_boolean_array"))); 19.110 + e.eval("var ts_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + 19.111 + "ts_boolean_arr[0] = true;" + 19.112 + "ts_boolean_arr[1] = false;" + 19.113 + "ts_boolean_arr[2] = true;" + 19.114 + "SharedObject.publicStaticBooleanArray = ts_boolean_arr;"); 19.115 + assertTrue(Arrays.equals(new boolean[] { true, false, true }, SharedObject.publicStaticBooleanArray)); 19.116 + e.eval("SharedObject.publicStaticBooleanArray[0] = false;"); 19.117 + assertEquals(false, SharedObject.publicStaticBooleanArray[0]); 19.118 + } 19.119 + 19.120 + @Test 19.121 + public void accessFinalFieldBoolean() throws ScriptException { 19.122 + e.eval("var pf_boolean = o.publicFinalBoolean;"); 19.123 + assertEquals(o.publicFinalBoolean, e.get("pf_boolean")); 19.124 + assertEquals("boolean", e.eval("typeof pf_boolean;")); 19.125 + e.eval("o.publicFinalBoolean = false;"); 19.126 + assertEquals(true, o.publicFinalBoolean); 19.127 + } 19.128 + 19.129 + @Test 19.130 + public void accessFinalFieldBooleanArray() throws ScriptException { 19.131 + e.eval("var pf_boolean_array = o.publicFinalBooleanArray;"); 19.132 + assertEquals(o.publicFinalBooleanArray[0], e.eval("o.publicFinalBooleanArray[0]")); 19.133 + assertTrue(Arrays.equals(o.publicFinalBooleanArray, (boolean[])e.get("pf_boolean_array"))); 19.134 + e.eval("var tf_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + 19.135 + "tf_boolean_arr[0] = false;" + 19.136 + "tf_boolean_arr[1] = false;" + 19.137 + "tf_boolean_arr[2] = true;" + 19.138 + "o.publicOFinalbjectArray = tf_boolean_arr;"); 19.139 + assertTrue(Arrays.equals(new boolean[] { false, false, true, false }, o.publicFinalBooleanArray)); 19.140 + e.eval("o.publicFinalBooleanArray[0] = true;"); 19.141 + assertEquals(true, o.publicFinalBooleanArray[0]); 19.142 + } 19.143 + 19.144 + @Test 19.145 + public void accessStaticFinalFieldBoolean() throws ScriptException { 19.146 + e.eval("var psf_boolean = SharedObject.publicStaticFinalBoolean;"); 19.147 + assertEquals(SharedObject.publicStaticFinalBoolean, e.get("psf_boolean")); 19.148 + assertEquals("boolean", e.eval("typeof psf_boolean;")); 19.149 + e.eval("SharedObject.publicStaticFinalBoolean = false;"); 19.150 + assertEquals(true, SharedObject.publicStaticFinalBoolean); 19.151 + } 19.152 + 19.153 + @Test 19.154 + public void accessStaticFinalFieldBooleanArray() throws ScriptException { 19.155 + e.eval("var psf_boolean_array = SharedObject.publicStaticFinalBooleanArray;"); 19.156 + assertEquals(SharedObject.publicStaticFinalBooleanArray[0], e.eval("SharedObject.publicStaticFinalBooleanArray[0]")); 19.157 + assertTrue(Arrays.equals(SharedObject.publicStaticFinalBooleanArray, (boolean[])e.get("psf_boolean_array"))); 19.158 + e.eval("var tsf_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + 19.159 + "tsf_boolean_arr[0] = false;" + 19.160 + "tsf_boolean_arr[1] = true;" + 19.161 + "tsf_boolean_arr[2] = false;" + 19.162 + "SharedObject.publicStaticFinalBooleanArray = tsf_boolean_arr;"); 19.163 + assertTrue(Arrays.equals(new boolean[] { false, true, false, false }, SharedObject.publicStaticFinalBooleanArray)); 19.164 + e.eval("SharedObject.publicStaticFinalBooleanArray[0] = true;"); 19.165 + assertEquals(true, SharedObject.publicStaticFinalBooleanArray[0]); 19.166 + } 19.167 + 19.168 + @Test 19.169 + public void accessFieldBooleanBoxing() throws ScriptException { 19.170 + e.eval("var p_boolean_box = o.publicBooleanBox;"); 19.171 + assertEquals(o.publicBooleanBox, e.get("p_boolean_box")); 19.172 + assertEquals("boolean", e.eval("typeof p_boolean_box;")); 19.173 + e.eval("o.publicBooleanBox = false;"); 19.174 + assertEquals(false, (boolean)o.publicBooleanBox); 19.175 + } 19.176 + 19.177 + @Test 19.178 + public void accessStaticFieldBooleanBoxing() throws ScriptException { 19.179 + e.eval("var ps_boolean_box = SharedObject.publicStaticBooleanBox;"); 19.180 + assertEquals(SharedObject.publicStaticBooleanBox, e.get("ps_boolean_box")); 19.181 + assertEquals("boolean", e.eval("typeof ps_boolean_box;")); 19.182 + e.eval("SharedObject.publicStaticBooleanBox = false;"); 19.183 + assertEquals(false, (boolean)SharedObject.publicStaticBooleanBox); 19.184 + } 19.185 + 19.186 + @Test 19.187 + public void accessFinalFieldBooleanBoxing() throws ScriptException { 19.188 + e.eval("var pf_boolean_box = o.publicFinalBooleanBox;"); 19.189 + assertEquals(o.publicFinalBooleanBox, e.get("pf_boolean_box")); 19.190 + assertEquals("boolean", e.eval("typeof pf_boolean_box;")); 19.191 + e.eval("o.publicFinalBooleanBox = false;"); 19.192 + assertEquals(true, (boolean)o.publicFinalBooleanBox); 19.193 + } 19.194 + 19.195 + @Test 19.196 + public void accessStaticFinalFieldBooleanBoxing() throws ScriptException { 19.197 + e.eval("var psf_boolean_box = SharedObject.publicStaticFinalBooleanBox;"); 19.198 + assertEquals(SharedObject.publicStaticFinalBooleanBox, e.get("psf_boolean_box")); 19.199 + assertEquals("boolean", e.eval("typeof psf_boolean_box;")); 19.200 + e.eval("SharedObject.publicStaticFinalBooleanBox = false;"); 19.201 + assertEquals(true, (boolean)SharedObject.publicStaticFinalBooleanBox); 19.202 + } 19.203 + 19.204 + @Test 19.205 + public void accessVolatileField() throws ScriptException { 19.206 + e.eval("var pv_boolean = o.volatileBoolean;"); 19.207 + assertEquals(o.volatileBoolean, e.get("pv_boolean")); 19.208 + assertEquals("boolean", e.eval("typeof pv_boolean;")); 19.209 + e.eval("o.volatileBoolean = false;"); 19.210 + assertEquals(false, o.volatileBoolean); 19.211 + } 19.212 + 19.213 + @Test 19.214 + public void accessTransientField() throws ScriptException { 19.215 + e.eval("var pt_boolean = o.transientBoolean;"); 19.216 + assertEquals(o.transientBoolean, e.get("pt_boolean")); 19.217 + assertEquals("boolean", e.eval("typeof pt_boolean;")); 19.218 + e.eval("o.transientBoolean = false;"); 19.219 + assertEquals(false, o.transientBoolean); 19.220 + } 19.221 + 19.222 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/ConsStringTest.java Mon Apr 06 16:18:54 2015 +0530 20.3 @@ -0,0 +1,99 @@ 20.4 +/* 20.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 20.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 20.7 + * 20.8 + * This code is free software; you can redistribute it and/or modify it 20.9 + * under the terms of the GNU General Public License version 2 only, as 20.10 + * published by the Free Software Foundation. Oracle designates this 20.11 + * particular file as subject to the "Classpath" exception as provided 20.12 + * by Oracle in the LICENSE file that accompanied this code. 20.13 + * 20.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 20.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 20.17 + * version 2 for more details (a copy is included in the LICENSE file that 20.18 + * accompanied this code). 20.19 + * 20.20 + * You should have received a copy of the GNU General Public License version 20.21 + * 2 along with this work; if not, write to the Free Software Foundation, 20.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20.23 + * 20.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20.25 + * or visit www.oracle.com if you need additional information or have any 20.26 + * questions. 20.27 + */ 20.28 + 20.29 +package jdk.nashorn.api.javaaccess.test; 20.30 + 20.31 +import static org.testng.AssertJUnit.assertEquals; 20.32 +import java.util.HashMap; 20.33 +import java.util.Map; 20.34 +import javax.script.Bindings; 20.35 +import javax.script.ScriptContext; 20.36 +import javax.script.ScriptEngine; 20.37 +import javax.script.ScriptEngineManager; 20.38 +import javax.script.ScriptException; 20.39 +import jdk.nashorn.api.scripting.JSObject; 20.40 +import org.testng.TestNG; 20.41 +import org.testng.annotations.AfterClass; 20.42 +import org.testng.annotations.BeforeClass; 20.43 +import org.testng.annotations.Test; 20.44 + 20.45 +@SuppressWarnings("javadoc") 20.46 +public class ConsStringTest { 20.47 + private static ScriptEngine e = null; 20.48 + 20.49 + public static void main(final String[] args) { 20.50 + TestNG.main(args); 20.51 + } 20.52 + 20.53 + @BeforeClass 20.54 + public static void setUpClass() { 20.55 + e = new ScriptEngineManager().getEngineByName("nashorn"); 20.56 + } 20.57 + 20.58 + @AfterClass 20.59 + public static void tearDownClass() { 20.60 + e = null; 20.61 + } 20.62 + 20.63 + @Test 20.64 + public void testConsStringFlattening() throws ScriptException { 20.65 + final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 20.66 + final Map<Object, Object> m = new HashMap<>(); 20.67 + b.put("m", m); 20.68 + e.eval("var x = 'f'; x += 'oo'; var y = 'b'; y += 'ar'; m.put(x, y)"); 20.69 + assertEquals("bar", m.get("foo")); 20.70 + } 20.71 + 20.72 + @Test 20.73 + public void testConsStringFromMirror() throws ScriptException { 20.74 + final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 20.75 + //final Map<Object, Object> m = new HashMap<>(); 20.76 + e.eval("var x = 'f'; x += 'oo'; var obj = {x: x};"); 20.77 + assertEquals("foo", ((JSObject)b.get("obj")).getMember("x")); 20.78 + } 20.79 + 20.80 + @Test 20.81 + public void testArrayConsString() throws ScriptException { 20.82 + final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 20.83 + final ArrayHolder h = new ArrayHolder(); 20.84 + b.put("h", h); 20.85 + e.eval("var x = 'f'; x += 'oo'; h.array = [x];"); 20.86 + assertEquals(1, h.array.length); 20.87 + assertEquals("foo", h.array[0]); 20.88 + } 20.89 + 20.90 + 20.91 + public static class ArrayHolder { 20.92 + private Object[] array; 20.93 + 20.94 + public void setArray(final Object[] array) { 20.95 + this.array = array; 20.96 + } 20.97 + 20.98 + public Object[] getArray() { 20.99 + return array; 20.100 + } 20.101 + } 20.102 +}
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 21.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/MethodAccessTest.java Mon Apr 06 16:18:54 2015 +0530 21.3 @@ -0,0 +1,466 @@ 21.4 +/* 21.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 21.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 21.7 + * 21.8 + * This code is free software; you can redistribute it and/or modify it 21.9 + * under the terms of the GNU General Public License version 2 only, as 21.10 + * published by the Free Software Foundation. Oracle designates this 21.11 + * particular file as subject to the "Classpath" exception as provided 21.12 + * by Oracle in the LICENSE file that accompanied this code. 21.13 + * 21.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 21.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 21.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 21.17 + * version 2 for more details (a copy is included in the LICENSE file that 21.18 + * accompanied this code). 21.19 + * 21.20 + * You should have received a copy of the GNU General Public License version 21.21 + * 2 along with this work; if not, write to the Free Software Foundation, 21.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21.23 + * 21.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21.25 + * or visit www.oracle.com if you need additional information or have any 21.26 + * questions. 21.27 + */ 21.28 + 21.29 +package jdk.nashorn.api.javaaccess.test; 21.30 + 21.31 +import static org.testng.AssertJUnit.assertEquals; 21.32 +import static org.testng.AssertJUnit.assertTrue; 21.33 +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; 21.34 +import java.util.Arrays; 21.35 +import java.util.Calendar; 21.36 +import java.util.Locale; 21.37 +import javax.script.ScriptEngine; 21.38 +import javax.script.ScriptEngineManager; 21.39 +import javax.script.ScriptException; 21.40 +import org.testng.TestNG; 21.41 +import org.testng.annotations.AfterClass; 21.42 +import org.testng.annotations.BeforeClass; 21.43 +import org.testng.annotations.Test; 21.44 + 21.45 +/** 21.46 + * @test 21.47 + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.MethodAccessTest 21.48 + * @run testng/othervm jdk.nashorn.api.javaaccess.MethodAccessTest 21.49 + */ 21.50 +@SuppressWarnings("javadoc") 21.51 +public class MethodAccessTest { 21.52 + 21.53 + private static ScriptEngine e = null; 21.54 + private static SharedObject o = null; 21.55 + 21.56 + public static void main(final String[] args) { 21.57 + TestNG.main(args); 21.58 + } 21.59 + 21.60 + @BeforeClass 21.61 + public static void setUpClass() throws ScriptException { 21.62 + final ScriptEngineManager m = new ScriptEngineManager(); 21.63 + e = m.getEngineByName("nashorn"); 21.64 + o = new SharedObject(); 21.65 + o.setEngine(e); 21.66 + e.put("o", o); 21.67 + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 21.68 + e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;"); 21.69 + } 21.70 + 21.71 + @AfterClass 21.72 + public static void tearDownClass() { 21.73 + e = null; 21.74 + o = null; 21.75 + } 21.76 + 21.77 + @Test 21.78 + public void accessMethodthrowsCheckedException() throws ScriptException { 21.79 + e.eval("try {" + 21.80 + " var a = java.lang.Long.parseLong('foo');" + 21.81 + "} catch(e) {" + 21.82 + " var isThrown = true;" + 21.83 + " var isNumberException = e instanceof java.lang.NumberFormatException;" + 21.84 + "} finally {" + 21.85 + " var isFinalized = true;" + 21.86 + "}"); 21.87 + assertEquals("Exception thrown", true, e.get("isThrown")); 21.88 + assertEquals("Finally called", true, e.get("isFinalized")); 21.89 + assertEquals("Type is NumberFormatException", true, e.get("isNumberException")); 21.90 + } 21.91 + 21.92 + @Test 21.93 + public void accessMethodthrowsUnCheckedException() throws ScriptException { 21.94 + e.eval("try {" + 21.95 + " var a = java.lang.String.valueOf(null);" + 21.96 + "} catch(e) {" + 21.97 + " var isThrown = true;" + 21.98 + " var isNumberException = e instanceof java.lang.NullPointerException;" + 21.99 + "} finally {" + 21.100 + " var isFinalized = true;" + 21.101 + "}"); 21.102 + assertEquals(true, e.get("isThrown")); 21.103 + assertEquals(true, e.get("isFinalized")); 21.104 + assertEquals(true, e.get("isNumberException")); 21.105 + } 21.106 + 21.107 + @Test 21.108 + public void accessMethodStartsThread() throws ScriptException { 21.109 + e.eval("o.methodStartsThread();"); 21.110 + assertEquals(false, o.isFinished); 21.111 + } 21.112 + 21.113 + @Test 21.114 + public void accessStaticMethod() throws ScriptException { 21.115 + assertEquals(10, e.eval("java.lang.Math.abs(-10);")); 21.116 + } 21.117 + 21.118 + @Test 21.119 + public void accessSynchronousMethod() throws ScriptException { 21.120 + e.eval("var v = new java.util.Vector();" + "v.add(10);" + "v.add(20);" + "v.add(30);"); 21.121 + assertEquals(10, e.eval("v[0]")); 21.122 + assertEquals(20, e.eval("v[1]")); 21.123 + assertEquals(30, e.eval("v[2]")); 21.124 + assertEquals(3, e.eval("v.size()")); 21.125 + } 21.126 + 21.127 + @Test 21.128 + public void accessStaticSynchronousMethod() throws ScriptException { 21.129 + e.eval("var locales = java.util.Calendar.getAvailableLocales();"); 21.130 + final Locale[] locales = (Locale[])e.get("locales"); 21.131 + assertEquals(locales.length, Calendar.getAvailableLocales().length); 21.132 + } 21.133 + 21.134 + @Test 21.135 + public void accessNativeMethod() throws ScriptException { 21.136 + assertEquals(4.0, e.eval("java.lang.StrictMath.log10(10000);")); 21.137 + } 21.138 + 21.139 + @Test 21.140 + public void accessConstructorOfAbstractClass() throws ScriptException { 21.141 + e.eval("try {" + 21.142 + " var a = new java.util.AbstractList();" + 21.143 + " print('fail');" + 21.144 + "} catch(e) {" + 21.145 + " var isThrown = true;" + 21.146 + "}"); 21.147 + assertEquals(true, e.get("isThrown")); 21.148 + } 21.149 + 21.150 + @Test 21.151 + public void accessMethodVoid() throws ScriptException { 21.152 + o.isAccessed = false; 21.153 + e.eval("o.voidMethod();"); 21.154 + assertTrue(o.isAccessed); 21.155 + } 21.156 + 21.157 + @Test 21.158 + public void accessMethodBoolean() throws ScriptException { 21.159 + assertEquals(true, e.eval("o.booleanMethod(false);")); 21.160 + assertEquals(false, e.eval("o.booleanMethod(true);")); 21.161 + assertEquals(false, e.eval("o.booleanMethod('false');")); 21.162 + assertEquals(true, e.eval("o.booleanMethod('');")); 21.163 + assertEquals(true, e.eval("o.booleanMethod(0);")); 21.164 + } 21.165 + 21.166 + @Test 21.167 + public void accessMethodInt() throws ScriptException { 21.168 + assertEquals(0, e.eval("o.intMethod(0);")); 21.169 + assertEquals(-200, e.eval("o.intMethod(-100);")); 21.170 + assertEquals(0, e.eval("o.intMethod('0');")); 21.171 + assertEquals(-200, e.eval("o.intMethod('-100');")); 21.172 + } 21.173 + 21.174 + @Test 21.175 + public void accessMethodLong() throws ScriptException { 21.176 + assertEquals((long)0, e.eval("o.longMethod(0);")); 21.177 + assertEquals((long)400, e.eval("o.longMethod(200);")); 21.178 + assertEquals((long) 0, e.eval("o.longMethod('0');")); 21.179 + assertEquals((long) 400, e.eval("o.longMethod('200');")); 21.180 + } 21.181 + 21.182 + @Test 21.183 + public void accessMethodByte() throws ScriptException { 21.184 + assertEquals((byte) 0, e.eval("o.byteMethod(0);")); 21.185 + assertEquals((byte) 10, e.eval("o.byteMethod(5);")); 21.186 + assertEquals((byte) 0, e.eval("o.byteMethod('0');")); 21.187 + assertEquals((byte) 10, e.eval("o.byteMethod('5');")); 21.188 + } 21.189 + 21.190 + @Test 21.191 + public void accessMethodShort() throws ScriptException { 21.192 + assertEquals((short)0, e.eval("o.shortMethod(0);")); 21.193 + assertEquals((short)8000, e.eval("o.shortMethod(4000);")); 21.194 + assertEquals((short) 0, e.eval("o.shortMethod('0');")); 21.195 + assertEquals((short) 8000, e.eval("o.shortMethod('4000');")); 21.196 + } 21.197 + 21.198 + @Test 21.199 + public void accessMethodChar() throws ScriptException { 21.200 + assertEquals('A', e.eval("o.charMethod('a');")); 21.201 + assertEquals('Z', e.eval("o.charMethod('z');")); 21.202 + assertEquals(o.charMethod((char)0), e.eval("o.charMethod(0);")); 21.203 + assertEquals(o.charMethod((char)3150), e.eval("o.charMethod(3150);")); 21.204 + } 21.205 + 21.206 + @Test 21.207 + public void accessMethodFloat() throws ScriptException { 21.208 + assertEquals(0.0f, e.eval("o.floatMethod(0.0);")); 21.209 + assertEquals(4.2f, e.eval("o.floatMethod(2.1);")); 21.210 + assertEquals(0.0f, e.eval("o.floatMethod('0.0');")); 21.211 + assertEquals(4.2f, e.eval("o.floatMethod('2.1');")); 21.212 + } 21.213 + 21.214 + @Test 21.215 + public void accessMethodDouble() throws ScriptException { 21.216 + assertEquals(0.0, e.eval("o.doubleMethod(0.0);")); 21.217 + assertEquals(14.0, e.eval("o.doubleMethod(7.0);")); 21.218 + assertEquals(0.0, e.eval("o.doubleMethod('0.0');")); 21.219 + assertEquals(14.0, e.eval("o.doubleMethod('7.0');")); 21.220 + } 21.221 + 21.222 + @Test 21.223 + public void accessMethodBooleanBoxing() throws ScriptException { 21.224 + assertEquals(Boolean.TRUE, e.eval("o.booleanBoxingMethod(java.lang.Boolean.FALSE);")); 21.225 + assertEquals(Boolean.FALSE, e.eval("o.booleanBoxingMethod(java.lang.Boolean.TRUE);")); 21.226 + assertEquals(Boolean.TRUE, e.eval("o.booleanBoxingMethod('');")); 21.227 + assertEquals(Boolean.FALSE, e.eval("o.booleanBoxingMethod('false');")); 21.228 + } 21.229 + 21.230 + @Test 21.231 + public void accessMethodIntBoxing() throws ScriptException { 21.232 + assertEquals(0, e.eval("o.intBoxingMethod(0);")); 21.233 + assertEquals(-200, e.eval("o.intBoxingMethod(-100);")); 21.234 + assertTrue((int)e.eval("(new java.lang.Integer(2)).compareTo(10.0)") < 0); 21.235 + } 21.236 + 21.237 + @Test 21.238 + public void accessMethodLongBoxing() throws ScriptException { 21.239 + assertEquals((long) 0, e.eval("o.longBoxingMethod(0);")); 21.240 + assertEquals((long) 400, e.eval("o.longBoxingMethod(200);")); 21.241 + assertTrue((int)e.eval("(new java.lang.Long(2)).compareTo(10.0)") < 0); 21.242 + } 21.243 + 21.244 + @Test 21.245 + public void accessMethodByteBoxing() throws ScriptException { 21.246 + assertEquals((byte) 0, e.eval("o.byteBoxingMethod(0);")); 21.247 + assertEquals((byte) 10, e.eval("o.byteBoxingMethod(5);")); 21.248 + assertTrue((int)e.eval("(new java.lang.Byte(2)).compareTo(10.0)") < 0); 21.249 + } 21.250 + 21.251 + @Test 21.252 + public void accessMethodShortBoxing() throws ScriptException { 21.253 + assertEquals((short) 0, e.eval("o.shortBoxingMethod(0);")); 21.254 + assertEquals((short) 8000, e.eval("o.shortBoxingMethod(4000);")); 21.255 + assertTrue((int)e.eval("(new java.lang.Short(2)).compareTo(10.0)") < 0); 21.256 + } 21.257 + 21.258 + @Test 21.259 + public void accessMethodCharBoxing() throws ScriptException { 21.260 + assertEquals('A', e.eval("o.charBoxingMethod('a');")); 21.261 + assertEquals('Z', e.eval("o.charBoxingMethod('z');")); 21.262 + assertTrue((int)e.eval("(new java.lang.Character(2)).compareTo(10)") < 0); 21.263 + } 21.264 + 21.265 + @Test 21.266 + public void accessMethodFloatBoxing() throws ScriptException { 21.267 + assertEquals(0.0f, e.eval("o.floatBoxingMethod(0.0);")); 21.268 + assertEquals(4.2f, e.eval("o.floatBoxingMethod(2.1);")); 21.269 + assertTrue((int)e.eval("(new java.lang.Float(2.0)).compareTo(10.0)") < 0); 21.270 + } 21.271 + 21.272 + @Test 21.273 + public void accessMethodDoubleBoxing() throws ScriptException { 21.274 + assertEquals(0.0, e.eval("o.doubleBoxingMethod(0.0);")); 21.275 + assertEquals(14.0, e.eval("o.doubleBoxingMethod(7.0);")); 21.276 + assertTrue((int)e.eval("(new java.lang.Double(2)).compareTo(10.0)") < 0); 21.277 + } 21.278 + 21.279 + @Test 21.280 + public void accessMethodString() throws ScriptException { 21.281 + assertEquals("", e.eval("o.stringMethod('');")); 21.282 + assertEquals("abcabc", e.eval("o.stringMethod('abc');")); 21.283 + } 21.284 + 21.285 + @Test 21.286 + public void accessMethodObject() throws ScriptException { 21.287 + e.put("so", new Person(5)); 21.288 + e.eval("var rso = o.objectMethod(so);"); 21.289 + assertEquals(new Person(10), e.get("rso")); 21.290 + } 21.291 + 21.292 + @Test 21.293 + public void accessMethodBooleanArray() throws ScriptException { 21.294 + assertTrue(Arrays.equals(o.booleanArrayMethod(o.publicBooleanArray), (boolean[])e.eval("o.booleanArrayMethod(o.publicBooleanArray);"))); 21.295 + } 21.296 + 21.297 + @Test 21.298 + public void accessMethodIntArray() throws ScriptException { 21.299 + assertArrayEquals(o.intArrayMethod(o.publicIntArray), (int[])e.eval("o.intArrayMethod(o.publicIntArray);")); 21.300 + } 21.301 + 21.302 + @Test 21.303 + public void accessMethodLongArray() throws ScriptException { 21.304 + assertArrayEquals(o.longArrayMethod(o.publicLongArray), (long[])e.eval("o.longArrayMethod(o.publicLongArray);")); 21.305 + } 21.306 + 21.307 + @Test 21.308 + public void accessMethodByteArray() throws ScriptException { 21.309 + assertArrayEquals(o.byteArrayMethod(o.publicByteArray), (byte[])e.eval("o.byteArrayMethod(o.publicByteArray);")); 21.310 + } 21.311 + 21.312 + @Test 21.313 + public void accessMethodShortArray() throws ScriptException { 21.314 + assertArrayEquals(o.shortArrayMethod(o.publicShortArray), (short[])e.eval("o.shortArrayMethod(o.publicShortArray);")); 21.315 + } 21.316 + 21.317 + @Test 21.318 + public void accessMethodCharArray() throws ScriptException { 21.319 + assertArrayEquals(o.charArrayMethod(o.publicCharArray), (char[])e.eval("o.charArrayMethod(o.publicCharArray);")); 21.320 + } 21.321 + 21.322 + @Test 21.323 + public void accessMethodFloatArray() throws ScriptException { 21.324 + assertArrayEquals(o.floatArrayMethod(o.publicFloatArray), (float[])e.eval("o.floatArrayMethod(o.publicFloatArray);"), 1e-10f); 21.325 + } 21.326 + 21.327 + @Test 21.328 + public void accessMethodDoubleArray() throws ScriptException { 21.329 + assertArrayEquals(o.doubleArrayMethod(o.publicDoubleArray), (double[])e.eval("o.doubleArrayMethod(o.publicDoubleArray);"), 1e-10); 21.330 + } 21.331 + 21.332 + @Test 21.333 + public void accessMethodStringArray() throws ScriptException { 21.334 + assertArrayEquals(o.stringArrayMethod(o.publicStringArray), (String[])e.eval("o.stringArrayMethod(o.publicStringArray);")); 21.335 + } 21.336 + 21.337 + @Test 21.338 + public void accessMethodObjectArray() throws ScriptException { 21.339 + assertArrayEquals(o.objectArrayMethod(o.publicObjectArray), (Person[])e.eval("o.objectArrayMethod(o.publicObjectArray);")); 21.340 + } 21.341 + 21.342 + @Test 21.343 + public void accessDefaultConstructor() throws ScriptException { 21.344 + e.eval("var dc = new Packages.jdk.nashorn.api.javaaccess.Person()"); 21.345 + assertEquals(new Person(), e.get("dc")); 21.346 + } 21.347 + 21.348 + @Test 21.349 + public void accessCustomConstructor() throws ScriptException { 21.350 + e.eval("var cc = new Packages.jdk.nashorn.api.javaaccess.Person(17)"); 21.351 + assertEquals(new Person(17), e.get("cc")); 21.352 + } 21.353 + 21.354 + @Test 21.355 + public void accessMethod2PrimitiveParams() throws ScriptException { 21.356 + assertEquals(o.twoParamMethod(50, 40.0), e.eval("o.twoParamMethod(50,40);")); 21.357 + } 21.358 + 21.359 + @Test 21.360 + public void accessMethod3PrimitiveParams() throws ScriptException { 21.361 + assertEquals(o.threeParamMethod((short)10, 20L, 'b'), e.eval("o.threeParamMethod(10,20,'b');")); 21.362 + } 21.363 + 21.364 + @Test 21.365 + public void accessMethod2ObjectParams() throws ScriptException { 21.366 + assertArrayEquals(new Person[] { new Person(200), new Person(300) }, (Person[])e.eval("o.twoObjectParamMethod(new Person(300),new Person(200));")); 21.367 + } 21.368 + 21.369 + @Test 21.370 + public void accessMethod3ObjectParams() throws ScriptException { 21.371 + assertArrayEquals(new Person[] { new Person(3), new Person(2), new Person(1) }, (Person[])e.eval("o.threeObjectParamMethod(new Person(1),new Person(2),new Person(3));")); 21.372 + } 21.373 + 21.374 + @Test 21.375 + public void accessMethod8ObjectParams() throws ScriptException { 21.376 + assertArrayEquals(new Person[] { new Person(8), new Person(7), new Person(6), new Person(5), new Person(4), new Person(3), new Person(2), new Person(1) }, (Person[])e.eval("o.eightObjectParamMethod(new Person(1),new Person(2),new Person(3)," + "new Person(4),new Person(5),new Person(6),new Person(7),new Person(8));")); 21.377 + } 21.378 + 21.379 + @Test 21.380 + public void accessMethod9ObjectParams() throws ScriptException { 21.381 + assertArrayEquals(new Person[] { new Person(9), new Person(8), new Person(7), new Person(6), new Person(5), new Person(4), new Person(3), new Person(2), new Person(1) }, (Person[])e.eval("o.nineObjectParamMethod(new Person(1),new Person(2),new Person(3)," + "new Person(4),new Person(5),new Person(6)," + "new Person(7),new Person(8),new Person(9));")); 21.382 + } 21.383 + 21.384 + @Test 21.385 + public void accessMethodObjectEllipsis() throws ScriptException { 21.386 + assertArrayEquals(new Person[] { new Person(9), new Person(8), new Person(7), new Person(6), new Person(5), new Person(4), new Person(3), new Person(2), new Person(1) }, (Person[])e.eval("o.methodObjectEllipsis(new Person(1),new Person(2),new Person(3)," + "new Person(4),new Person(5),new Person(6)," + "new Person(7),new Person(8),new Person(9));")); 21.387 + assertArrayEquals(new Person[] {}, (Person[])e.eval("o.methodObjectEllipsis()")); 21.388 + assertArrayEquals(new Person[] { new Person(9) }, (Person[])e.eval("o.methodObjectEllipsis(new Person(9))")); 21.389 + } 21.390 + 21.391 + @Test 21.392 + public void accessMethodPrimitiveEllipsis() throws ScriptException { 21.393 + assertArrayEquals(new Person[] { new Person(1), new Person(3), new Person(2) }, (Person[])e.eval("o.methodPrimitiveEllipsis(1,3,2);")); 21.394 + assertArrayEquals(new Person[] {}, (Person[])e.eval("o.methodPrimitiveEllipsis();")); 21.395 + assertArrayEquals(o.methodPrimitiveEllipsis(9, 8, 7, 6, 5, 4, 3, 2, 1), (Person[])e.eval("o.methodPrimitiveEllipsis(9,8,7,6,5,4,3,2,1);")); 21.396 + } 21.397 + 21.398 + @Test 21.399 + public void accessMethodMixedEllipsis() throws ScriptException { 21.400 + assertArrayEquals(new Object[] { new Person(1), 12, "hello", true }, (Object[])e.eval("o.methodMixedEllipsis(new Person(1),12,'hello',true);")); 21.401 + assertArrayEquals(new Object[] {}, (Object[])e.eval("o.methodMixedEllipsis();")); 21.402 + } 21.403 + 21.404 + @Test 21.405 + public void accessMethodObjectWithEllipsis() throws ScriptException { 21.406 + assertArrayEquals(new Object[] { "hello", 12, 15, 16 }, (Object[])e.eval("o.methodObjectWithEllipsis('hello',12,15,16);")); 21.407 + assertArrayEquals(new Object[] { "hello" }, (Object[])e.eval("o.methodObjectWithEllipsis('hello');")); 21.408 + } 21.409 + 21.410 + @Test 21.411 + public void accessMethodPrimitiveWithEllipsis() throws ScriptException { 21.412 + assertArrayEquals(new Object[] { 14, 12L, 15L, 16L }, (Object[])e.eval("o.methodPrimitiveWithEllipsis(14,12,15,16);")); 21.413 + assertArrayEquals(new Object[] { 12 }, (Object[])e.eval("o.methodPrimitiveWithEllipsis(12);")); 21.414 + } 21.415 + 21.416 + @Test 21.417 + public void accessMethodMixedWithEllipsis() throws ScriptException { 21.418 + assertArrayEquals(new Object[] { "Hello", 10, true, -100500, 80d }, (Object[])e.eval("o.methodMixedWithEllipsis('Hello', 10, true, -100500,80.0);")); 21.419 + assertArrayEquals(new Object[] { "Nashorn", 15 }, (Object[])e.eval("o.methodMixedWithEllipsis('Nashorn',15);")); 21.420 + } 21.421 + 21.422 + @Test 21.423 + public void accessMethodOverloaded() throws ScriptException { 21.424 + assertEquals(0, e.eval("o.overloadedMethod(0);")); 21.425 + assertEquals(2000, e.eval("o.overloadedMethod(1000);")); 21.426 + assertEquals(2, e.eval("o.overloadedMethod('10');")); 21.427 + assertEquals(7, e.eval("o.overloadedMethod('Nashorn');")); 21.428 + assertEquals(4, e.eval("o.overloadedMethod('true');")); 21.429 + assertEquals(1, e.eval("o.overloadedMethod(true);")); 21.430 + assertEquals(0, e.eval("o.overloadedMethod(false);")); 21.431 + assertEquals(44, e.eval("o.overloadedMethod(new Person(22));")); 21.432 + assertEquals(0, e.eval("o.overloadedMethod(new Person());")); 21.433 + } 21.434 + 21.435 + @Test 21.436 + public void accessMethodDoubleVSintOverloaded() throws ScriptException { 21.437 + assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.0);")); 21.438 + assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(1000.0);")); 21.439 + assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.01);")); 21.440 + assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(100.02);")); 21.441 + assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(0);")); 21.442 + assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(1000);")); 21.443 + } 21.444 + 21.445 + @Test 21.446 + public void accessJavaMethodIntFromJSFromJavaFromJS() throws ScriptException { 21.447 + e.eval("function secondLevelMethodInt(a) {" 21.448 + + "return o.thirdLevelMethodInt(a);" 21.449 + + "}"); 21.450 + assertEquals(50, e.eval("o.firstLevelMethodInt(10);")); 21.451 + } 21.452 + 21.453 + @Test 21.454 + public void accessJavaMethodIntegerFromJSFromJavaFromJS() throws ScriptException { 21.455 + e.eval("function secondLevelMethodInteger(a) {" 21.456 + + "return o.thirdLevelMethodInteger(a);" 21.457 + + "}"); 21.458 + assertEquals(100, e.eval("o.firstLevelMethodInteger(10);")); 21.459 + } 21.460 + 21.461 + @Test 21.462 + public void accessJavaMethodObjectFromJSFromJavaFromJS() throws ScriptException { 21.463 + e.eval("function secondLevelMethodObject(p) {" 21.464 + + "return o.thirdLevelMethodObject(p);" 21.465 + + "}"); 21.466 + assertEquals(new Person(100), e.eval("o.firstLevelMethodObject(new Person(10));")); 21.467 + } 21.468 + 21.469 +}
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 22.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/NumberAccessTest.java Mon Apr 06 16:18:54 2015 +0530 22.3 @@ -0,0 +1,789 @@ 22.4 +/* 22.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 22.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 22.7 + * 22.8 + * This code is free software; you can redistribute it and/or modify it 22.9 + * under the terms of the GNU General Public License version 2 only, as 22.10 + * published by the Free Software Foundation. Oracle designates this 22.11 + * particular file as subject to the "Classpath" exception as provided 22.12 + * by Oracle in the LICENSE file that accompanied this code. 22.13 + * 22.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 22.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 22.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 22.17 + * version 2 for more details (a copy is included in the LICENSE file that 22.18 + * accompanied this code). 22.19 + * 22.20 + * You should have received a copy of the GNU General Public License version 22.21 + * 2 along with this work; if not, write to the Free Software Foundation, 22.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22.23 + * 22.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22.25 + * or visit www.oracle.com if you need additional information or have any 22.26 + * questions. 22.27 + */ 22.28 + 22.29 +package jdk.nashorn.api.javaaccess.test; 22.30 + 22.31 +import static org.testng.AssertJUnit.assertEquals; 22.32 +import static org.testng.AssertJUnit.assertTrue; 22.33 +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; 22.34 +import javax.script.ScriptEngine; 22.35 +import javax.script.ScriptEngineManager; 22.36 +import javax.script.ScriptException; 22.37 +import org.testng.TestNG; 22.38 +import org.testng.annotations.AfterClass; 22.39 +import org.testng.annotations.BeforeClass; 22.40 +import org.testng.annotations.Test; 22.41 + 22.42 +/** 22.43 + * @test 22.44 + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.NumberAccessTest 22.45 + * @run testng/othervm jdk.nashorn.api.javaaccess.NumberAccessTest 22.46 + */ 22.47 +@SuppressWarnings("javadoc") 22.48 +public class NumberAccessTest { 22.49 + 22.50 + private static ScriptEngine e; 22.51 + private static SharedObject o; 22.52 + 22.53 + public static void main(final String[] args) { 22.54 + TestNG.main(args); 22.55 + } 22.56 + 22.57 + @BeforeClass 22.58 + public static void setUpClass() throws ScriptException { 22.59 + final ScriptEngineManager m = new ScriptEngineManager(); 22.60 + e = m.getEngineByName("nashorn"); 22.61 + o = new SharedObject(); 22.62 + e.put("o", o); 22.63 + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 22.64 + } 22.65 + 22.66 + @AfterClass 22.67 + public static void tearDownClass() { 22.68 + e = null; 22.69 + o = null; 22.70 + } 22.71 + 22.72 + // --------------------------------long 22.73 + // tests------------------------------------ 22.74 + @Test 22.75 + public void accessFieldLong() throws ScriptException { 22.76 + e.eval("var p_long = o.publicLong;"); 22.77 + assertEquals(o.publicLong, e.get("p_long")); 22.78 + e.eval("o.publicLong = 12;"); 22.79 + assertEquals(12, o.publicLong); 22.80 + } 22.81 + 22.82 + @Test 22.83 + public void accessFieldLongArray() throws ScriptException { 22.84 + e.eval("var p_long_array = o.publicLongArray;"); 22.85 + assertEquals(o.publicLongArray[0], e.eval("o.publicLongArray[0];")); 22.86 + assertArrayEquals(o.publicLongArray, (long[])e.get("p_long_array")); 22.87 + e.eval("var t_long_arr = new (Java.type(\"long[]\"))(3);" + 22.88 + "t_long_arr[0] = -189009;" + 22.89 + "t_long_arr[1] = 456;" + 22.90 + "t_long_arr[2] = 600000001;" + 22.91 + "o.publicLongArray = t_long_arr;"); 22.92 + // e.eval("o.publicIntArray = [-189009,456,600000001];"); 22.93 + assertArrayEquals(new long[] { -189009, 456, 600000001 }, o.publicLongArray); 22.94 + e.eval("o.publicLongArray[0] = 10;"); 22.95 + assertEquals(10, o.publicLongArray[0]); 22.96 + } 22.97 + 22.98 + @Test 22.99 + public void accessStaticFieldLong() throws ScriptException { 22.100 + e.eval("var ps_long = SharedObject.publicStaticLong;"); 22.101 + assertEquals(SharedObject.publicStaticLong, e.get("ps_long")); 22.102 + e.eval("SharedObject.publicStaticLong = 120;"); 22.103 + assertEquals(120, SharedObject.publicStaticLong); 22.104 + } 22.105 + 22.106 + @Test 22.107 + public void accessStaticFieldLongArray() throws ScriptException { 22.108 + e.eval("var ps_long_array = SharedObject.publicStaticLongArray;"); 22.109 + assertEquals(SharedObject.publicStaticLongArray[0], e.eval("SharedObject.publicStaticLongArray[0];")); 22.110 + assertArrayEquals(SharedObject.publicStaticLongArray, (long[])e.get("ps_long_array")); 22.111 + e.eval("var ts_long_arr = new (Java.type(\"long[]\"))(3);" + 22.112 + "ts_long_arr[0] = -189009;" + 22.113 + "ts_long_arr[1] = 456;" + 22.114 + "ts_long_arr[2] = 600000001;" + 22.115 + "SharedObject.publicStaticLongArray = ts_long_arr;"); 22.116 + // e.eval("o.publicIntArray = [-189009,456,600000001];"); 22.117 + assertArrayEquals(new long[] { -189009, 456, 600000001 }, SharedObject.publicStaticLongArray); 22.118 + e.eval("SharedObject.publicStaticLongArray[0] = 10;"); 22.119 + assertEquals(10, SharedObject.publicStaticLongArray[0]); 22.120 + } 22.121 + 22.122 + @Test 22.123 + public void accessFinalFieldLong() throws ScriptException { 22.124 + e.eval("var pf_long = o.publicFinalLong;"); 22.125 + assertEquals(o.publicFinalLong, e.get("pf_long")); 22.126 + e.eval("o.publicFinalLong = 120;"); 22.127 + assertEquals(13353333333333333L, o.publicFinalLong); 22.128 + } 22.129 + 22.130 + @Test 22.131 + public void accessFinalFieldLongArray() throws ScriptException { 22.132 + e.eval("var pf_long_array = o.publicFinalLongArray;"); 22.133 + assertEquals(o.publicFinalLongArray[0], e.eval("o.publicFinalLongArray[0];")); 22.134 + assertArrayEquals(o.publicFinalLongArray, (long[])e.get("pf_long_array")); 22.135 + e.eval("var tf_long_arr = new (Java.type(\"long[]\"))(3);" + 22.136 + "tf_long_arr[0] = -189009;" + 22.137 + "tf_long_arr[1] = 456;" + 22.138 + "tf_long_arr[2] = 600000001;" + 22.139 + "o.publicFinalLongArray = tf_long_arr;"); 22.140 + // e.eval("o.publicIntArray = [-189009,456,600000001];"); 22.141 + assertArrayEquals(new long[] { 1901733333333L, -2247355555L, 3977377777L }, o.publicFinalLongArray); 22.142 + e.eval("o.publicFinalLongArray[0] = 10;"); 22.143 + assertEquals(10, o.publicFinalLongArray[0]); 22.144 + } 22.145 + 22.146 + @Test 22.147 + public void accessStaticFinalFieldLong() throws ScriptException { 22.148 + e.eval("var psf_long = SharedObject.publicStaticFinalLong;"); 22.149 + assertEquals(SharedObject.publicStaticFinalLong, e.get("psf_long")); 22.150 + e.eval("SharedObject.publicStaticFinalLong = 120;"); 22.151 + assertEquals(8333333333333L, SharedObject.publicStaticFinalLong); 22.152 + } 22.153 + 22.154 + @Test 22.155 + public void accessStaticFinalFieldLongArray() throws ScriptException { 22.156 + e.eval("var psf_long_array = SharedObject.publicStaticFinalLongArray;"); 22.157 + assertEquals(SharedObject.publicStaticFinalLongArray[0], e.eval("SharedObject.publicStaticFinalLongArray[0];")); 22.158 + assertArrayEquals(SharedObject.publicStaticFinalLongArray, (long[])e.get("psf_long_array")); 22.159 + e.eval("var tsf_long_arr = new (Java.type(\"long[]\"))(3);" + 22.160 + "tsf_long_arr[0] = -189009;" + 22.161 + "tsf_long_arr[1] = 456;" + 22.162 + "tsf_long_arr[2] = 600000001;" + 22.163 + "SharedObject.publicStaticFinalLongArray = tsf_long_arr;"); 22.164 + // e.eval("o.publicIntArray = [-189009,456,600000001];"); 22.165 + assertArrayEquals(new long[] { 19017383333L, -2247358L, 39773787L }, SharedObject.publicStaticFinalLongArray); 22.166 + e.eval("SharedObject.publicStaticFinalLongArray[0] = 10;"); 22.167 + assertEquals(10, SharedObject.publicStaticFinalLongArray[0]); 22.168 + } 22.169 + 22.170 + // --------------------------------int 22.171 + // tests------------------------------------ 22.172 + @Test 22.173 + public void accessFieldInt() throws ScriptException { 22.174 + e.eval("var p_int = o.publicInt;"); 22.175 + assertEquals(o.publicInt, e.get("p_int")); 22.176 + e.eval("o.publicInt = 14;"); 22.177 + assertEquals(14, o.publicInt); 22.178 + } 22.179 + 22.180 + @Test 22.181 + public void accessFieldIntArray() throws ScriptException { 22.182 + e.eval("var p_int_array = o.publicIntArray;"); 22.183 + assertEquals(o.publicIntArray[0], e.eval("o.publicIntArray[0];")); 22.184 + assertArrayEquals(o.publicIntArray, (int[])e.get("p_int_array")); 22.185 + e.eval("var t_int_arr = new (Java.type(\"int[]\"))(3);" + 22.186 + "t_int_arr[0] = 4;" + 22.187 + "t_int_arr[1] = 5;" + 22.188 + "t_int_arr[2] = 6;" + 22.189 + "o.publicIntArray = t_int_arr;"); 22.190 + assertArrayEquals(new int[] { 4, 5, 6 }, o.publicIntArray); 22.191 + e.eval("o.publicIntArray[0] = 100;"); 22.192 + assertEquals(100, o.publicIntArray[0]); 22.193 + } 22.194 + 22.195 + @Test 22.196 + public void accessStaticFieldInt() throws ScriptException { 22.197 + e.eval("var ps_int = SharedObject.publicStaticInt;"); 22.198 + assertEquals(SharedObject.publicStaticInt, e.get("ps_int")); 22.199 + e.eval("SharedObject.publicStaticInt = 140;"); 22.200 + assertEquals(140, SharedObject.publicStaticInt); 22.201 + } 22.202 + 22.203 + @Test 22.204 + public void accessStaticFieldIntArray() throws ScriptException { 22.205 + e.eval("var ps_int_array = SharedObject.publicStaticIntArray;"); 22.206 + assertEquals(SharedObject.publicStaticIntArray[0], e.eval("SharedObject.publicStaticIntArray[0];")); 22.207 + assertArrayEquals(SharedObject.publicStaticIntArray, (int[])e.get("ps_int_array")); 22.208 + e.eval("var ts_int_arr = new (Java.type(\"int[]\"))(3);" + 22.209 + "ts_int_arr[0] = 4;" + 22.210 + "ts_int_arr[1] = 5;" + 22.211 + "ts_int_arr[2] = 6;" + 22.212 + "SharedObject.publicStaticIntArray = ts_int_arr;"); 22.213 + assertArrayEquals(new int[] { 4, 5, 6 }, SharedObject.publicStaticIntArray); 22.214 + e.eval("SharedObject.publicStaticIntArray[0] = 100;"); 22.215 + assertEquals(100, SharedObject.publicStaticIntArray[0]); 22.216 + } 22.217 + 22.218 + @Test 22.219 + public void accessFinalFieldInt() throws ScriptException { 22.220 + e.eval("var pf_int = o.publicFinalInt;"); 22.221 + assertEquals(o.publicFinalInt, e.get("pf_int")); 22.222 + 22.223 + e.eval("o.publicFinalInt = 10;"); 22.224 + assertEquals(20712023, o.publicFinalInt); 22.225 + } 22.226 + 22.227 + @Test 22.228 + public void accessFinalFieldIntArray() throws ScriptException { 22.229 + e.eval("var pf_int_array = o.publicFinalIntArray;"); 22.230 + assertEquals(o.publicFinalIntArray[0], e.eval("o.publicFinalIntArray[0];")); 22.231 + assertArrayEquals(o.publicFinalIntArray, (int[])e.get("pf_int_array")); 22.232 + e.eval("var tf_int_arr = new (Java.type(\"int[]\"))(3);" + 22.233 + "tf_int_arr[0] = 4;" + 22.234 + "tf_int_arr[1] = 5;" + 22.235 + "tf_int_arr[2] = 6;" + 22.236 + "o.publicFinalIntArray = tf_int_arr;"); 22.237 + assertArrayEquals(new int[] { 50, 80, 130, 210, 340 }, o.publicFinalIntArray); 22.238 + e.eval("o.publicFinalIntArray[0] = 100;"); 22.239 + assertEquals(100, o.publicFinalIntArray[0]); 22.240 + } 22.241 + 22.242 + @Test 22.243 + public void accessStaticFinalFieldInt() throws ScriptException { 22.244 + e.eval("var psf_int = SharedObject.publicStaticFinalInt;"); 22.245 + assertEquals(SharedObject.publicStaticFinalInt, e.get("psf_int")); 22.246 + e.eval("SharedObject.publicStaticFinalInt = 140;"); 22.247 + assertEquals(207182023, SharedObject.publicStaticFinalInt); 22.248 + } 22.249 + 22.250 + @Test 22.251 + public void accessStaticFinalFieldIntArray() throws ScriptException { 22.252 + e.eval("var psf_int_array = SharedObject.publicStaticFinalIntArray;"); 22.253 + assertEquals(SharedObject.publicStaticFinalIntArray[0], e.eval("SharedObject.publicStaticFinalIntArray[0];")); 22.254 + assertArrayEquals(SharedObject.publicStaticFinalIntArray, (int[])e.get("psf_int_array")); 22.255 + e.eval("var tsf_int_arr = new (Java.type(\"int[]\"))(3);" + 22.256 + "tsf_int_arr[0] = 4;" + 22.257 + "tsf_int_arr[1] = 5;" + 22.258 + "tsf_int_arr[2] = 6;" + 22.259 + "SharedObject.publicStaticFinalIntArray = tsf_int_arr;"); 22.260 + assertArrayEquals(new int[] { 1308, 210, 340 }, SharedObject.publicStaticFinalIntArray); 22.261 + e.eval("SharedObject.publicStaticFinalIntArray[0] = 100;"); 22.262 + assertEquals(100, SharedObject.publicStaticFinalIntArray[0]); 22.263 + } 22.264 + 22.265 + // --------------------------------byte 22.266 + // tests------------------------------------ 22.267 + @Test 22.268 + public void accessFieldByte() throws ScriptException { 22.269 + e.eval("var p_byte = o.publicByte;"); 22.270 + assertEquals((double)o.publicByte, ((Number)e.get("p_byte")).doubleValue()); 22.271 + e.eval("o.publicByte = 16;"); 22.272 + assertEquals(16, o.publicByte); 22.273 + } 22.274 + 22.275 + @Test 22.276 + public void accessFieldByteArray() throws ScriptException { 22.277 + e.eval("var p_byte_array = o.publicByteArray;"); 22.278 + assertEquals(o.publicByteArray[0], e.eval("o.publicByteArray[0];")); 22.279 + assertArrayEquals(o.publicByteArray, (byte[])e.get("p_byte_array")); 22.280 + e.eval("var t_byte_arr = new (Java.type(\"byte[]\"))(3);" + 22.281 + "t_byte_arr[0] = -18;" + 22.282 + "t_byte_arr[1] = 56;" + 22.283 + "t_byte_arr[2] = 60;" + 22.284 + "o.publicByteArray = t_byte_arr;"); 22.285 + assertArrayEquals(new byte[] { -18, 56, 60 }, o.publicByteArray); 22.286 + e.eval("o.publicByteArray[0] = 100;"); 22.287 + assertEquals(100, o.publicByteArray[0]); 22.288 + } 22.289 + 22.290 + @Test 22.291 + public void accessStaticFieldByte() throws ScriptException { 22.292 + e.eval("var ps_byte = SharedObject.publicStaticByte;"); 22.293 + assertEquals((double)SharedObject.publicStaticByte, ((Number)e.get("ps_byte")).doubleValue()); 22.294 + e.eval("SharedObject.publicStaticByte = 16;"); 22.295 + assertEquals(16, SharedObject.publicStaticByte); 22.296 + } 22.297 + 22.298 + @Test 22.299 + public void accessStaticFieldByteArray() throws ScriptException { 22.300 + e.eval("var ps_byte_array = SharedObject.publicStaticByteArray;"); 22.301 + assertEquals(SharedObject.publicStaticByteArray[0], e.eval("SharedObject.publicStaticByteArray[0];")); 22.302 + assertArrayEquals(SharedObject.publicStaticByteArray, (byte[])e.get("ps_byte_array")); 22.303 + e.eval("var ts_byte_arr = new (Java.type(\"byte[]\"))(3);" + 22.304 + "ts_byte_arr[0] = -18;" + 22.305 + "ts_byte_arr[1] = 56;" + 22.306 + "ts_byte_arr[2] = 60;" + 22.307 + "SharedObject.publicStaticByteArray = ts_byte_arr;"); 22.308 + assertArrayEquals(new byte[] { -18, 56, 60 }, SharedObject.publicStaticByteArray); 22.309 + e.eval("SharedObject.publicStaticByteArray[0] = -90;"); 22.310 + assertEquals(-90, SharedObject.publicStaticByteArray[0]); 22.311 + } 22.312 + 22.313 + @Test 22.314 + public void accessFinalFieldByte() throws ScriptException { 22.315 + e.eval("var pf_byte = o.publicFinalByte;"); 22.316 + assertEquals((double)o.publicFinalByte, ((Number)e.get("pf_byte")).doubleValue()); 22.317 + e.eval("o.publicFinalByte = 16;"); 22.318 + assertEquals(-7, o.publicFinalByte); 22.319 + } 22.320 + 22.321 + @Test 22.322 + public void accessFinalFieldByteArray() throws ScriptException { 22.323 + e.eval("var pf_byte_array = o.publicFinalByteArray;"); 22.324 + assertEquals(o.publicFinalByteArray[0], e.eval("o.publicFinalByteArray[0];")); 22.325 + assertArrayEquals(o.publicFinalByteArray, (byte[])e.get("pf_byte_array")); 22.326 + e.eval("var tf_byte_arr = new (Java.type(\"byte[]\"))(3);" + 22.327 + "tf_byte_arr[0] = -18;" + 22.328 + "tf_byte_arr[1] = 56;" + 22.329 + "tf_byte_arr[2] = 60;" + 22.330 + "o.publicFinalByteArray = tf_byte_arr;"); 22.331 + assertArrayEquals(new byte[] { 1, 3, 6, 17, -128 }, o.publicFinalByteArray); 22.332 + e.eval("o.publicFinalByteArray[0] = -90;"); 22.333 + assertEquals(-90, o.publicFinalByteArray[0]); 22.334 + } 22.335 + 22.336 + @Test 22.337 + public void accessStaticFinalFieldByte() throws ScriptException { 22.338 + e.eval("var psf_byte = SharedObject.publicStaticFinalByte;"); 22.339 + assertEquals((double)SharedObject.publicStaticFinalByte, ((Number)e.get("psf_byte")).doubleValue()); 22.340 + e.eval("SharedObject.publicStaticFinalByte = 16;"); 22.341 + assertEquals(-70, SharedObject.publicStaticFinalByte); 22.342 + } 22.343 + 22.344 + @Test 22.345 + public void accessStaticFinalFieldByteArray() throws ScriptException { 22.346 + e.eval("var psf_byte_array = SharedObject.publicStaticFinalByteArray;"); 22.347 + assertEquals(SharedObject.publicStaticFinalByteArray[0], e.eval("SharedObject.publicStaticFinalByteArray[0];")); 22.348 + assertArrayEquals(SharedObject.publicStaticFinalByteArray, (byte[])e.get("psf_byte_array")); 22.349 + e.eval("var tsf_byte_arr = new (Java.type(\"byte[]\"))(3);" + 22.350 + "tsf_byte_arr[0] = -18;" + 22.351 + "tsf_byte_arr[1] = 56;" + 22.352 + "tsf_byte_arr[2] = 60;" + 22.353 + "SharedObject.publicStaticFinalByteArray = tsf_byte_arr;"); 22.354 + assertArrayEquals(new byte[] { 17, -128, 81 }, SharedObject.publicStaticFinalByteArray); 22.355 + e.eval("SharedObject.publicStaticFinalByteArray[0] = -90;"); 22.356 + assertEquals(-90, SharedObject.publicStaticFinalByteArray[0]); 22.357 + } 22.358 + 22.359 + // --------------------------------short 22.360 + // tests------------------------------------ 22.361 + @Test 22.362 + public void accessFieldShort() throws ScriptException { 22.363 + e.eval("var p_short = o.publicShort;"); 22.364 + assertEquals((double)o.publicShort, ((Number)e.get("p_short")).doubleValue()); 22.365 + e.eval("o.publicShort = 18;"); 22.366 + assertEquals(18, o.publicShort); 22.367 + } 22.368 + 22.369 + @Test 22.370 + public void accessFieldShortArray() throws ScriptException { 22.371 + e.eval("var p_short_array = o.publicShortArray;"); 22.372 + assertEquals(o.publicShortArray[0], e.eval("o.publicShortArray[0];")); 22.373 + assertArrayEquals(o.publicShortArray, (short[])e.get("p_short_array")); 22.374 + e.eval("var t_short_arr = new (Java.type(\"short[]\"))(3);" + 22.375 + "t_short_arr[0] = 90;" + 22.376 + "t_short_arr[1] = 5;" + 22.377 + "t_short_arr[2] = -6000;" + 22.378 + "o.publicShortArray = t_short_arr;"); 22.379 + assertArrayEquals(new short[] { 90, 5, -6000 }, o.publicShortArray); 22.380 + e.eval("o.publicShortArray[0] = -1000;"); 22.381 + assertEquals(-1000, o.publicShortArray[0]); 22.382 + } 22.383 + 22.384 + @Test 22.385 + public void accessStaticFieldShort() throws ScriptException { 22.386 + e.eval("var ps_short = SharedObject.publicStaticShort;"); 22.387 + assertEquals((double)SharedObject.publicStaticShort, ((Number)e.get("ps_short")).doubleValue()); 22.388 + e.eval("SharedObject.publicStaticShort = 180;"); 22.389 + assertEquals(180, SharedObject.publicStaticShort); 22.390 + } 22.391 + 22.392 + @Test 22.393 + public void accessStaticFieldShortArray() throws ScriptException { 22.394 + e.eval("var ps_short_array = SharedObject.publicStaticShortArray;"); 22.395 + assertEquals(SharedObject.publicStaticShortArray[0], e.eval("SharedObject.publicStaticShortArray[0];")); 22.396 + assertArrayEquals(SharedObject.publicStaticShortArray, (short[])e.get("ps_short_array")); 22.397 + e.eval("var ts_short_arr = new (Java.type(\"short[]\"))(3);" + 22.398 + "ts_short_arr[0] = 90;" + 22.399 + "ts_short_arr[1] = 5;" + 22.400 + "ts_short_arr[2] = -6000;" + 22.401 + "SharedObject.publicStaticShortArray = ts_short_arr;"); 22.402 + assertArrayEquals(new short[] { 90, 5, -6000 }, SharedObject.publicStaticShortArray); 22.403 + e.eval("SharedObject.publicStaticShortArray[0] = -1000;"); 22.404 + assertEquals(-1000, SharedObject.publicStaticShortArray[0]); 22.405 + } 22.406 + 22.407 + @Test 22.408 + public void accessFinalFieldShort() throws ScriptException { 22.409 + e.eval("var pf_short = o.publicFinalShort;"); 22.410 + assertEquals((double)o.publicFinalShort, ((Number)e.get("pf_short")).doubleValue()); 22.411 + e.eval("o.publicFinalShort = 180;"); 22.412 + assertEquals(31220, o.publicFinalShort); 22.413 + } 22.414 + 22.415 + @Test 22.416 + public void accessFinalFieldShortArray() throws ScriptException { 22.417 + e.eval("var pf_short_array = o.publicFinalShortArray;"); 22.418 + assertEquals(o.publicFinalShortArray[0], e.eval("o.publicFinalShortArray[0];")); 22.419 + assertArrayEquals(o.publicFinalShortArray, (short[])e.get("pf_short_array")); 22.420 + e.eval("var tf_short_arr = new (Java.type(\"short[]\"))(3);" + 22.421 + "tf_short_arr[0] = 90;" + 22.422 + "tf_short_arr[1] = 5;" + 22.423 + "tf_short_arr[2] = -6000;" + 22.424 + "o.publicFinalShortArray = tf_short_arr;"); 22.425 + assertArrayEquals(new short[] { 12240, 9200, -17289, 1200, 12 }, o.publicFinalShortArray); 22.426 + e.eval("o.publicFinalShortArray[0] = -1000;"); 22.427 + assertEquals(-1000, o.publicFinalShortArray[0]); 22.428 + } 22.429 + 22.430 + @Test 22.431 + public void accessStaticFinalFieldShort() throws ScriptException { 22.432 + e.eval("var psf_short = SharedObject.publicStaticFinalShort;"); 22.433 + assertEquals((double)SharedObject.publicStaticFinalShort, ((Number)e.get("psf_short")).doubleValue()); 22.434 + e.eval("SharedObject.publicStaticFinalShort = 180;"); 22.435 + assertEquals(8888, SharedObject.publicStaticFinalShort); 22.436 + } 22.437 + 22.438 + @Test 22.439 + public void accessStaticFinalFieldShortArray() throws ScriptException { 22.440 + e.eval("var psf_short_array = SharedObject.publicStaticFinalShortArray;"); 22.441 + assertEquals(SharedObject.publicStaticFinalShortArray[0], e.eval("SharedObject.publicStaticFinalShortArray[0];")); 22.442 + assertArrayEquals(SharedObject.publicStaticFinalShortArray, (short[])e.get("psf_short_array")); 22.443 + e.eval("var tsf_short_arr = new (Java.type(\"short[]\"))(3);" + 22.444 + "tsf_short_arr[0] = 90;" + 22.445 + "tsf_short_arr[1] = 5;" + 22.446 + "tsf_short_arr[2] = -6000;" + 22.447 + "SharedObject.publicStaticFinalShortArray = tsf_short_arr;"); 22.448 + assertArrayEquals(new short[] { 8240, 9280, -1289, 120, 812 }, SharedObject.publicStaticFinalShortArray); 22.449 + e.eval("SharedObject.publicStaticFinalShortArray[0] = -1000;"); 22.450 + assertEquals(-1000, SharedObject.publicStaticFinalShortArray[0]); 22.451 + } 22.452 + 22.453 + // --------------------------------char 22.454 + // tests------------------------------------ 22.455 + @Test 22.456 + public void accessFieldChar() throws ScriptException { 22.457 + e.eval("var p_char = o.publicChar;"); 22.458 + assertEquals(o.publicChar, e.get("p_char")); 22.459 + e.eval("o.publicChar = 'S';"); 22.460 + assertEquals('S', o.publicChar); 22.461 + e.eval("o.publicChar = 10;"); 22.462 + assertEquals(10, o.publicChar); 22.463 + e.eval("try {" 22.464 + + " o.publicChar = 'Big string';" + 22.465 + "} catch(e) {" + 22.466 + " var isThrown = true;" + 22.467 + "}"); 22.468 + assertEquals("Exception thrown", true, e.get("isThrown")); 22.469 + assertEquals(10, o.publicChar); 22.470 + } 22.471 + 22.472 + @Test 22.473 + public void accessFieldCharArray() throws ScriptException { 22.474 + e.eval("var p_char_array = o.publicCharArray;"); 22.475 + assertEquals(o.publicCharArray[0], e.eval("o.publicCharArray[0];")); 22.476 + assertArrayEquals(o.publicCharArray, (char[])e.get("p_char_array")); 22.477 + e.eval("var t_char_arr = new (Java.type(\"char[]\"))(3);" + 22.478 + "t_char_arr[0] = 'F';" + 22.479 + "t_char_arr[1] = 'o';" + 22.480 + "t_char_arr[2] = 'o';" + 22.481 + "o.publicCharArray = t_char_arr;"); 22.482 + assertArrayEquals("Foo".toCharArray(), o.publicCharArray); 22.483 + e.eval("o.publicCharArray[0] = 'Z';"); 22.484 + assertEquals('Z', o.publicCharArray[0]); 22.485 + } 22.486 + 22.487 + @Test 22.488 + public void accessStaticFieldChar() throws ScriptException { 22.489 + e.eval("var ps_char = SharedObject.publicStaticChar;"); 22.490 + assertEquals(SharedObject.publicStaticChar, e.get("ps_char")); 22.491 + e.eval("SharedObject.publicStaticChar = 'Z';"); 22.492 + assertEquals('Z', SharedObject.publicStaticChar); 22.493 + } 22.494 + 22.495 + @Test 22.496 + public void accessStaticFieldCharArray() throws ScriptException { 22.497 + e.eval("var ps_char_array = SharedObject.publicStaticCharArray;"); 22.498 + assertEquals(SharedObject.publicStaticCharArray[0], e.eval("SharedObject.publicStaticCharArray[0];")); 22.499 + assertArrayEquals(SharedObject.publicStaticCharArray, (char[])e.get("ps_char_array")); 22.500 + e.eval("var ts_char_arr = new (Java.type(\"char[]\"))(3);" + 22.501 + "ts_char_arr[0] = 'G';" + 22.502 + "ts_char_arr[1] = 'o';" + 22.503 + "ts_char_arr[2] = 'o';" + 22.504 + "SharedObject.publicStaticCharArray = ts_char_arr;"); 22.505 + assertArrayEquals("Goo".toCharArray(), SharedObject.publicStaticCharArray); 22.506 + e.eval("SharedObject.publicStaticCharArray[0] = 'Z';"); 22.507 + assertEquals('Z', SharedObject.publicStaticCharArray[0]); 22.508 + } 22.509 + 22.510 + @Test 22.511 + public void accessFinalFieldChar() throws ScriptException { 22.512 + e.eval("var pf_char = o.publicFinalChar;"); 22.513 + assertEquals(o.publicFinalChar, e.get("pf_char")); 22.514 + e.eval("o.publicFinalChar = 'S';"); 22.515 + assertEquals('E', o.publicFinalChar); 22.516 + } 22.517 + 22.518 + @Test 22.519 + public void accessFinalCharArray() throws ScriptException { 22.520 + e.eval("var pf_char_array = o.publicFinalCharArray;"); 22.521 + assertEquals(o.publicFinalCharArray[0], e.eval("o.publicFinalCharArray[0];")); 22.522 + assertArrayEquals(o.publicFinalCharArray, (char[])e.get("pf_char_array")); 22.523 + e.eval("var tf_char_arr = new (Java.type(\"char[]\"))(3);" + 22.524 + "tf_char_arr[0] = 'F';" + 22.525 + "tf_char_arr[1] = 'o';" + 22.526 + "tf_char_arr[2] = 'o';" + 22.527 + "o.publicFinalCharArray = tf_char_arr;"); 22.528 + assertArrayEquals("Nashorn hello".toCharArray(), o.publicFinalCharArray); 22.529 + e.eval("o.publicFinalCharArray[0] = 'Z';"); 22.530 + assertEquals('Z', o.publicFinalCharArray[0]); 22.531 + } 22.532 + 22.533 + @Test 22.534 + public void accessStaticFinalFieldChar() throws ScriptException { 22.535 + e.eval("var psf_char = SharedObject.publicStaticFinalChar;"); 22.536 + assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char")); 22.537 + e.eval("SharedObject.publicStaticFinalChar = 'Z';"); 22.538 + assertEquals('K', SharedObject.publicStaticFinalChar); 22.539 + } 22.540 + 22.541 + @Test 22.542 + public void accessStaticFinalFieldCharArray() throws ScriptException { 22.543 + e.eval("var psf_char_array = SharedObject.publicStaticFinalCharArray;"); 22.544 + assertEquals(SharedObject.publicStaticFinalCharArray[0], e.eval("SharedObject.publicStaticFinalCharArray[0];")); 22.545 + assertArrayEquals(SharedObject.publicStaticFinalCharArray, (char[])e.get("psf_char_array")); 22.546 + e.eval("var tsf_char_arr = new (Java.type(\"char[]\"))(3);" + 22.547 + "tsf_char_arr[0] = 'Z';" + 22.548 + "tsf_char_arr[1] = 'o';" + 22.549 + "tsf_char_arr[2] = 'o';" + 22.550 + "SharedObject.publicStaticFinalCharArray = tsf_char_arr;"); 22.551 + assertArrayEquals("StaticString".toCharArray(), SharedObject.publicStaticFinalCharArray); 22.552 + e.eval("SharedObject.publicStaticFinalCharArray[0] = 'Z';"); 22.553 + assertEquals('Z', SharedObject.publicStaticFinalCharArray[0]); 22.554 + } 22.555 + 22.556 + // --------------------------------float 22.557 + // tests------------------------------------ 22.558 + @Test 22.559 + public void accessFieldFloat() throws ScriptException { 22.560 + e.eval("var p_float = o.publicFloat;"); 22.561 + assertEquals((double)o.publicFloat, ((Number)e.get("p_float")).doubleValue()); 22.562 + o.publicFloat = 0.0f / 0.0f; 22.563 + assertEquals(true, e.eval("isNaN(o.publicFloat)")); 22.564 + o.publicFloat = 1.0f / 0.0f; 22.565 + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicFloat")); 22.566 + o.publicFloat = -1.0f / 0.0f; 22.567 + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicFloat")); 22.568 + e.eval("o.publicFloat = 20;"); 22.569 + assertEquals(20, o.publicFloat, 1e-10); 22.570 + e.eval("o.publicFloat = 0.0/0.0;"); 22.571 + assertTrue(Float.isNaN(o.publicFloat)); 22.572 + e.eval("o.publicFloat = 1.0/0.0;"); 22.573 + assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(o.publicFloat)); 22.574 + e.eval("o.publicFloat = -1.0/0.0;"); 22.575 + assertEquals(Float.NEGATIVE_INFINITY, o.publicFloat, 1e-10); 22.576 + } 22.577 + 22.578 + @Test 22.579 + public void accessFieldFloatArray() throws ScriptException { 22.580 + e.eval("var p_float_array = o.publicFloatArray;"); 22.581 + assertEquals(o.publicFloatArray[0], e.eval("o.publicFloatArray[0];")); 22.582 + assertArrayEquals(o.publicFloatArray, (float[])e.get("p_float_array"), 1e-10f); 22.583 + e.eval("var t_float_arr = new (Java.type(\"float[]\"))(3);" + 22.584 + "t_float_arr[0] = 9.0;" + 22.585 + "t_float_arr[1] = 5.12345;" + 22.586 + "t_float_arr[2] = -60.03;" + 22.587 + "o.publicFloatArray = t_float_arr;"); 22.588 + assertArrayEquals(new float[] { 9.0f, 5.12345f, -60.03f }, o.publicFloatArray, 1e-10f); 22.589 + e.eval("o.publicFloatArray[0] = -513.2;"); 22.590 + assertArrayEquals(new float[] { -513.2f, 5.12345f, -60.03f }, o.publicFloatArray, 1e-10f); 22.591 + } 22.592 + 22.593 + @Test 22.594 + public void accessStaticFieldFloat() throws ScriptException { 22.595 + e.eval("var ps_float = SharedObject.publicStaticFloat;"); 22.596 + assertEquals((double)SharedObject.publicStaticFloat, ((Number)e.get("ps_float")).doubleValue()); 22.597 + SharedObject.publicStaticFloat = 0.0f / 0.0f; 22.598 + assertEquals(true, e.eval("isNaN(SharedObject.publicStaticFloat)")); 22.599 + SharedObject.publicStaticFloat = 1.0f / 0.0f; 22.600 + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticFloat")); 22.601 + SharedObject.publicStaticFloat = -1.0f / 0.0f; 22.602 + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticFloat")); 22.603 + e.eval("SharedObject.publicStaticFloat = 20.0;"); 22.604 + assertEquals(20.0f, SharedObject.publicStaticFloat, 1e-10); 22.605 + e.eval("SharedObject.publicStaticFloat = 0.0/0.0;"); 22.606 + assertTrue(Float.isNaN(SharedObject.publicStaticFloat)); 22.607 + e.eval("SharedObject.publicStaticFloat = 1.0/0.0;"); 22.608 + assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); 22.609 + e.eval("SharedObject.publicStaticFloat = -1.0/0.0;"); 22.610 + assertEquals(Float.floatToIntBits(Float.NEGATIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); 22.611 + } 22.612 + 22.613 + @Test 22.614 + public void accessStaticFieldFloatArray() throws ScriptException { 22.615 + e.eval("var ps_float_array = SharedObject.publicStaticFloatArray;"); 22.616 + assertEquals(SharedObject.publicStaticFloatArray[0], e.eval("SharedObject.publicStaticFloatArray[0];")); 22.617 + assertArrayEquals(SharedObject.publicStaticFloatArray, (float[])e.get("ps_float_array"), 1e-10f); 22.618 + e.eval("var ts_float_arr = new (Java.type(\"float[]\"))(3);" + 22.619 + "ts_float_arr[0] = 9.0;" + 22.620 + "ts_float_arr[1] = 5.12345;" + 22.621 + "ts_float_arr[2] = -60.03;" + 22.622 + "SharedObject.publicStaticFloatArray = ts_float_arr;"); 22.623 + assertArrayEquals(new float[] { 9.0f, 5.12345f, -60.03f }, SharedObject.publicStaticFloatArray, 1e-10f); 22.624 + e.eval("SharedObject.publicStaticFloatArray[0] = -513.2;"); 22.625 + assertArrayEquals(new float[] { -513.2f, 5.12345f, -60.03f }, SharedObject.publicStaticFloatArray, 1e-10f); 22.626 + } 22.627 + 22.628 + @Test 22.629 + public void accessFinalFloat() throws ScriptException { 22.630 + e.eval("var pf_float = o.publicFinalFloat;"); 22.631 + assertEquals((double)o.publicFinalFloat, ((Number)e.get("pf_float")).doubleValue()); 22.632 + e.eval("o.publicFinalFloat = 20.0;"); 22.633 + assertEquals(7.72e8f, o.publicFinalFloat, 1e-10); 22.634 + } 22.635 + 22.636 + @Test 22.637 + public void accessFinalFloatArray() throws ScriptException { 22.638 + e.eval("var pf_float_array = o.publicFinalFloatArray;"); 22.639 + assertEquals(o.publicFinalFloatArray[0], e.eval("o.publicFinalFloatArray[0];")); 22.640 + assertArrayEquals(o.publicFinalFloatArray, (float[])e.get("pf_float_array"), 1e-10f); 22.641 + e.eval("var tf_float_arr = new (Java.type(\"float[]\"))(3);" + 22.642 + "tf_float_arr[0] = 9.0;" + 22.643 + "tf_float_arr[1] = 5.12345;" + 22.644 + "tf_float_arr[2] = -60.03;" + 22.645 + "o.publicFinalFloatArray = tf_float_arr;"); 22.646 + assertArrayEquals(new float[] { -131.012f, 189.32f, -31.32e8f, 3.72f }, o.publicFinalFloatArray, 1e-10f); 22.647 + e.eval("o.publicFinalFloatArray[0] = -513.2;"); 22.648 + assertEquals(-513.2f, o.publicFinalFloatArray[0], 1e-10f); 22.649 + } 22.650 + 22.651 + @Test 22.652 + public void accessStaticFinalFieldFloat() throws ScriptException { 22.653 + e.eval("var psf_float = SharedObject.publicStaticFinalFloat;"); 22.654 + assertEquals((double)SharedObject.publicStaticFinalFloat, ((Number)e.get("psf_float")).doubleValue()); 22.655 + e.eval("SharedObject.publicStaticFinalFloat = 20.0;"); 22.656 + assertEquals(0.72e8f, SharedObject.publicStaticFinalFloat, 1e-10); 22.657 + } 22.658 + 22.659 + @Test 22.660 + public void accessStaticFinalFieldFloatArray() throws ScriptException { 22.661 + e.eval("var psf_float_array = SharedObject.publicStaticFinalFloatArray;"); 22.662 + assertEquals(SharedObject.publicStaticFinalFloatArray[0], e.eval("SharedObject.publicStaticFinalFloatArray[0];")); 22.663 + assertArrayEquals(SharedObject.publicStaticFinalFloatArray, (float[])e.get("psf_float_array"), 1e-10f); 22.664 + e.eval("var tsf_float_arr = new (Java.type(\"float[]\"))(3);" + 22.665 + "tsf_float_arr[0] = 9.0;" + 22.666 + "tsf_float_arr[1] = 5.12345;" + 22.667 + "tsf_float_arr[2] = -60.03;" + 22.668 + "SharedObject.publicStaticFinalFloatArray = tsf_float_arr;"); 22.669 + assertArrayEquals(new float[] { -8131.012f, 9.32f, -138.32e8f, 0.72f }, SharedObject.publicStaticFinalFloatArray, 1e-10f); 22.670 + e.eval("SharedObject.publicStaticFinalFloatArray[0] = -513.2;"); 22.671 + assertEquals(-513.2f, SharedObject.publicStaticFinalFloatArray[0], 1e-10f); 22.672 + } 22.673 + 22.674 + // --------------------------------double 22.675 + // tests------------------------------------ 22.676 + @Test 22.677 + public void accessFieldDouble() throws ScriptException { 22.678 + e.eval("var p_double = o.publicDouble;"); 22.679 + assertEquals(o.publicDouble, e.get("p_double")); 22.680 + o.publicDouble = 0.0 / 0.0; 22.681 + assertEquals(true, e.eval("isNaN(o.publicDouble)")); 22.682 + o.publicDouble = 1.0 / 0.0; 22.683 + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicDouble")); 22.684 + o.publicDouble = -1.0 / 0.0; 22.685 + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicDouble")); 22.686 + e.eval("o.publicDouble = 30;"); 22.687 + assertEquals(Double.doubleToLongBits(30.0), Double.doubleToLongBits(o.publicDouble)); 22.688 + e.eval("o.publicDouble = 0.0/0.0;"); 22.689 + assertTrue(Double.isNaN(o.publicDouble)); 22.690 + e.eval("o.publicDouble = 1.0/0.0;"); 22.691 + assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(o.publicDouble)); 22.692 + e.eval("o.publicDouble = -1.0/0.0;"); 22.693 + assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(o.publicDouble)); 22.694 + } 22.695 + 22.696 + @Test 22.697 + public void accessFieldDoubleArrayRead() throws ScriptException { 22.698 + e.eval("var p_double_array = o.publicDoubleArray;"); 22.699 + assertEquals(o.publicDoubleArray[0], e.eval("o.publicDoubleArray[0];")); 22.700 + assertArrayEquals(o.publicDoubleArray, (double[])e.get("p_double_array"), 1e-10); 22.701 + e.eval("var t_double_arr = new (Java.type(\"double[]\"))(3);" + 22.702 + "t_double_arr[0] = 9e10;" + 22.703 + "t_double_arr[1] = 0.677777;" + 22.704 + "t_double_arr[2] = -0.0000001;" + 22.705 + "o.publicDoubleArray = t_double_arr;"); 22.706 + assertArrayEquals(new double[] { 9e10, 0.677777, -0.0000001 }, o.publicDoubleArray, 1e-10f); 22.707 + e.eval("o.publicDoubleArray[0] = -5.2e10;"); 22.708 + assertEquals(-5.2e10, o.publicDoubleArray[0], 1e-10f); 22.709 + } 22.710 + 22.711 + @Test 22.712 + public void accessStaticFieldDouble() throws ScriptException { 22.713 + e.eval("var ps_double = SharedObject.publicStaticDouble;"); 22.714 + assertEquals(SharedObject.publicStaticDouble, e.get("ps_double")); 22.715 + SharedObject.publicStaticDouble = 0.0 / 0.0; 22.716 + assertEquals(true, e.eval("isNaN(SharedObject.publicStaticDouble)")); 22.717 + SharedObject.publicStaticDouble = 1.0 / 0.0; 22.718 + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticDouble")); 22.719 + SharedObject.publicStaticDouble = -1.0 / 0.0; 22.720 + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticDouble")); 22.721 + e.eval("SharedObject.publicStaticDouble = 40.0;"); 22.722 + assertEquals(Double.doubleToLongBits(40.0), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 22.723 + e.eval("SharedObject.publicStaticDouble = 0.0/0.0;"); 22.724 + assertTrue(Double.isNaN(SharedObject.publicStaticDouble)); 22.725 + e.eval("SharedObject.publicStaticDouble = 1.0/0.0;"); 22.726 + assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 22.727 + e.eval("SharedObject.publicStaticDouble = -1.0/0.0;"); 22.728 + assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 22.729 + } 22.730 + 22.731 + @Test 22.732 + public void accessStaticFieldDoubleArrayRead() throws ScriptException { 22.733 + e.eval("var ps_double_array = SharedObject.publicStaticDoubleArray;"); 22.734 + assertEquals(SharedObject.publicStaticDoubleArray[0], e.eval("SharedObject.publicStaticDoubleArray[0];")); 22.735 + assertArrayEquals(SharedObject.publicStaticDoubleArray, (double[])e.get("ps_double_array"), 1e-10); 22.736 + e.eval("var ts_double_arr = new (Java.type(\"double[]\"))(3);" + 22.737 + "ts_double_arr[0] = 9e10;" + 22.738 + "ts_double_arr[1] = 0.677777;" + 22.739 + "ts_double_arr[2] = -0.0000001;" + 22.740 + "SharedObject.publicStaticDoubleArray = ts_double_arr;"); 22.741 + assertArrayEquals(new double[] { 9e10, 0.677777, -0.0000001 }, SharedObject.publicStaticDoubleArray, 1e-10f); 22.742 + e.eval("SharedObject.publicStaticDoubleArray[0] = -5.2e10;"); 22.743 + assertEquals(-5.2e10, SharedObject.publicStaticDoubleArray[0], 1e-10f); 22.744 + } 22.745 + 22.746 + @Test 22.747 + public void accessFinalFieldDouble() throws ScriptException { 22.748 + e.eval("var pf_double = o.publicFinalDouble;"); 22.749 + assertEquals(o.publicFinalDouble, e.get("pf_double")); 22.750 + e.eval("o.publicFinalDouble = 30.0;"); 22.751 + assertEquals(Double.doubleToLongBits(1.3412e20), Double.doubleToLongBits(o.publicFinalDouble)); 22.752 + } 22.753 + 22.754 + @Test 22.755 + public void accessFinalFieldDoubleArrayRead() throws ScriptException { 22.756 + e.eval("var pf_double_array = o.publicFinalDoubleArray;"); 22.757 + assertEquals(o.publicFinalDoubleArray[0], e.eval("o.publicFinalDoubleArray[0];")); 22.758 + assertArrayEquals(o.publicFinalDoubleArray, (double[])e.get("pf_double_array"), 1e-10); 22.759 + e.eval("var tf_double_arr = new (Java.type(\"double[]\"))(3);" + 22.760 + "tf_double_arr[0] = 9e10;" + 22.761 + "tf_double_arr[1] = 0.677777;" + 22.762 + "tf_double_arr[2] = -0.0000001;" + 22.763 + "o.publicFinalDoubleArray = tf_double_arr;"); 22.764 + assertArrayEquals(new double[] { 0.725e80, 0.12e10, 8e-3, 1.00077 }, o.publicFinalDoubleArray, 1e-10f); 22.765 + e.eval("o.publicFinalDoubleArray[0] = -5.2e10;"); 22.766 + assertEquals(-5.2e10, o.publicFinalDoubleArray[0], 1e-10f); 22.767 + } 22.768 + 22.769 + @Test 22.770 + public void accessStaticFinalFieldDouble() throws ScriptException { 22.771 + e.eval("var psf_double = SharedObject.publicStaticFinalDouble;"); 22.772 + assertEquals(SharedObject.publicStaticFinalDouble, e.get("psf_double")); 22.773 + e.eval("SharedObject.publicStaticFinalDouble = 40.0;"); 22.774 + assertEquals(Double.doubleToLongBits(1.8e12), Double.doubleToLongBits(SharedObject.publicStaticFinalDouble)); 22.775 + } 22.776 + 22.777 + @Test 22.778 + public void accessStaticFinalFieldDoubleArrayRead() throws ScriptException { 22.779 + e.eval("var psf_double_array = SharedObject.publicStaticFinalDoubleArray;"); 22.780 + assertEquals(SharedObject.publicStaticFinalDoubleArray[0], e.eval("SharedObject.publicStaticFinalDoubleArray[0];")); 22.781 + assertArrayEquals(SharedObject.publicStaticFinalDoubleArray, (double[])e.get("psf_double_array"), 1e-10); 22.782 + e.eval("var tsf_double_arr = new (Java.type(\"double[]\"))(3);" + 22.783 + "tsf_double_arr[0] = 9e10;" + 22.784 + "tsf_double_arr[1] = 0.677777;" + 22.785 + "tsf_double_arr[2] = -0.0000001;" + 22.786 + "SharedObject.publicStaticFinalDoubleArray = tsf_double_arr;"); 22.787 + assertArrayEquals(new double[] { 8.725e80, 0.82e10, 18e-3, 1.08077 }, SharedObject.publicStaticFinalDoubleArray, 1e-10f); 22.788 + e.eval("SharedObject.publicStaticFinalDoubleArray[0] = -5.2e10;"); 22.789 + assertEquals(-5.2e10, SharedObject.publicStaticFinalDoubleArray[0], 1e-10f); 22.790 + } 22.791 + 22.792 +}
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 23.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/NumberBoxingTest.java Mon Apr 06 16:18:54 2015 +0530 23.3 @@ -0,0 +1,365 @@ 23.4 +/* 23.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 23.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 23.7 + * 23.8 + * This code is free software; you can redistribute it and/or modify it 23.9 + * under the terms of the GNU General Public License version 2 only, as 23.10 + * published by the Free Software Foundation. Oracle designates this 23.11 + * particular file as subject to the "Classpath" exception as provided 23.12 + * by Oracle in the LICENSE file that accompanied this code. 23.13 + * 23.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 23.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 23.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 23.17 + * version 2 for more details (a copy is included in the LICENSE file that 23.18 + * accompanied this code). 23.19 + * 23.20 + * You should have received a copy of the GNU General Public License version 23.21 + * 2 along with this work; if not, write to the Free Software Foundation, 23.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 23.23 + * 23.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23.25 + * or visit www.oracle.com if you need additional information or have any 23.26 + * questions. 23.27 + */ 23.28 + 23.29 +package jdk.nashorn.api.javaaccess.test; 23.30 + 23.31 +import static org.testng.AssertJUnit.assertEquals; 23.32 +import static org.testng.AssertJUnit.assertTrue; 23.33 +import javax.script.ScriptEngine; 23.34 +import javax.script.ScriptEngineManager; 23.35 +import javax.script.ScriptException; 23.36 +import org.testng.TestNG; 23.37 +import org.testng.annotations.AfterClass; 23.38 +import org.testng.annotations.BeforeClass; 23.39 +import org.testng.annotations.Test; 23.40 + 23.41 +/** 23.42 + * @test 23.43 + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.NumberBoxingTest 23.44 + * @run testng/othervm jdk.nashorn.api.javaaccess.NumberBoxingTest 23.45 + */ 23.46 +@SuppressWarnings("javadoc") 23.47 +public class NumberBoxingTest { 23.48 + 23.49 + private static ScriptEngine e; 23.50 + private static SharedObject o; 23.51 + 23.52 + public static void main(final String[] args) { 23.53 + TestNG.main(args); 23.54 + } 23.55 + 23.56 + @BeforeClass 23.57 + public static void setUpClass() throws ScriptException { 23.58 + final ScriptEngineManager m = new ScriptEngineManager(); 23.59 + e = m.getEngineByName("nashorn"); 23.60 + o = new SharedObject(); 23.61 + e.put("o", o); 23.62 + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 23.63 + } 23.64 + 23.65 + @AfterClass 23.66 + public static void tearDownClass() { 23.67 + e = null; 23.68 + o = null; 23.69 + } 23.70 + 23.71 + // --------------------------------long 23.72 + // tests------------------------------------ 23.73 + @Test 23.74 + public void accessFieldLongBoxing() throws ScriptException { 23.75 + e.eval("var p_long = o.publicLongBox;"); 23.76 + assertEquals(o.publicLongBox, e.get("p_long")); 23.77 + e.eval("o.publicLongBox = 12;"); 23.78 + assertEquals(Long.valueOf(12), o.publicLongBox); 23.79 + } 23.80 + 23.81 + @Test 23.82 + public void accessStaticFieldLongBoxing() throws ScriptException { 23.83 + e.eval("var ps_long = SharedObject.publicStaticLongBox;"); 23.84 + assertEquals(SharedObject.publicStaticLongBox, e.get("ps_long")); 23.85 + e.eval("SharedObject.publicStaticLongBox = 120;"); 23.86 + assertEquals(120L, SharedObject.publicStaticLongBox.longValue()); 23.87 + } 23.88 + 23.89 + @Test 23.90 + public void accessFinalFieldLongBoxing() throws ScriptException { 23.91 + e.eval("var pf_long = o.publicFinalLongBox;"); 23.92 + assertEquals(o.publicFinalLongBox, e.get("pf_long")); 23.93 + e.eval("o.publicFinalLongBox = 120;"); 23.94 + assertEquals(Long.valueOf(9377333334L), o.publicFinalLongBox); 23.95 + } 23.96 + 23.97 + @Test 23.98 + public void accessStaticFinalFieldLongBoxing() throws ScriptException { 23.99 + e.eval("var psf_long = SharedObject.publicStaticFinalLong;"); 23.100 + assertEquals(SharedObject.publicStaticFinalLong, e.get("psf_long")); 23.101 + e.eval("SharedObject.publicStaticFinalLong = 120;"); 23.102 + assertEquals(8333333333333L, SharedObject.publicStaticFinalLong); 23.103 + } 23.104 + 23.105 + // --------------------------------int 23.106 + // tests------------------------------------ 23.107 + @Test 23.108 + public void accessFieldIntBoxing() throws ScriptException { 23.109 + e.eval("var p_int = o.publicIntBox;"); 23.110 + assertEquals(o.publicIntBox, e.get("p_int")); 23.111 + e.eval("o.publicIntBox = 14;"); 23.112 + assertEquals(Integer.valueOf(14), o.publicIntBox); 23.113 + } 23.114 + 23.115 + @Test 23.116 + public void accessStaticFieldIntBoxing() throws ScriptException { 23.117 + e.eval("var ps_int = SharedObject.publicStaticInt;"); 23.118 + assertEquals(SharedObject.publicStaticInt, e.get("ps_int")); 23.119 + e.eval("SharedObject.publicStaticInt = 140;"); 23.120 + assertEquals(140, SharedObject.publicStaticInt); 23.121 + } 23.122 + 23.123 + @Test 23.124 + public void accessFinalFieldIntBoxing() throws ScriptException { 23.125 + e.eval("var pf_int = o.publicFinalIntBox;"); 23.126 + assertEquals(o.publicFinalIntBox, e.get("pf_int")); 23.127 + e.eval("o.publicFinalIntBox = 10;"); 23.128 + assertEquals(Integer.valueOf(207512301), o.publicFinalIntBox); 23.129 + } 23.130 + 23.131 + @Test 23.132 + public void accessStaticFinalFieldIntBoxing() throws ScriptException { 23.133 + e.eval("var psf_int = SharedObject.publicStaticFinalInt;"); 23.134 + assertEquals(SharedObject.publicStaticFinalInt, e.get("psf_int")); 23.135 + e.eval("SharedObject.publicStaticFinalInt = 140;"); 23.136 + assertEquals(207182023, SharedObject.publicStaticFinalInt); 23.137 + } 23.138 + 23.139 + // --------------------------------byte 23.140 + // tests------------------------------------ 23.141 + @Test 23.142 + public void accessFieldByteBoxing() throws ScriptException { 23.143 + e.eval("var p_byte = o.publicByteBox;"); 23.144 + assertEqualsDouble(o.publicByteBox, "p_byte"); 23.145 + e.eval("o.publicByteBox = 16;"); 23.146 + assertEquals(Byte.valueOf((byte)16), o.publicByteBox); 23.147 + } 23.148 + 23.149 + @Test 23.150 + public void accessStaticFieldByteBoxing() throws ScriptException { 23.151 + e.eval("var ps_byte = SharedObject.publicStaticByte;"); 23.152 + assertEqualsDouble(SharedObject.publicStaticByte, "ps_byte"); 23.153 + e.eval("SharedObject.publicStaticByte = 16;"); 23.154 + assertEquals(16, SharedObject.publicStaticByte); 23.155 + } 23.156 + 23.157 + @Test 23.158 + public void accessFinalFieldByteBoxing() throws ScriptException { 23.159 + e.eval("var pf_byte = o.publicFinalByteBox;"); 23.160 + assertEqualsDouble(o.publicFinalByteBox, "pf_byte"); 23.161 + e.eval("o.publicFinalByteBox = 16;"); 23.162 + assertEquals(Byte.valueOf((byte)19), o.publicFinalByteBox); 23.163 + } 23.164 + 23.165 + @Test 23.166 + public void accessStaticFinalFieldByteBoxing() throws ScriptException { 23.167 + e.eval("var psf_byte = SharedObject.publicStaticFinalByte;"); 23.168 + assertEqualsDouble(SharedObject.publicStaticFinalByte, "psf_byte"); 23.169 + e.eval("SharedObject.publicStaticFinalByte = 16;"); 23.170 + assertEquals(-70, SharedObject.publicStaticFinalByte); 23.171 + } 23.172 + 23.173 + // --------------------------------short 23.174 + // tests------------------------------------ 23.175 + @Test 23.176 + public void accessFieldShortBoxing() throws ScriptException { 23.177 + e.eval("var p_short = o.publicShortBox;"); 23.178 + assertEqualsDouble(o.publicShortBox, "p_short"); 23.179 + e.eval("o.publicShortBox = 18;"); 23.180 + assertEquals(Short.valueOf((short)18), o.publicShortBox); 23.181 + } 23.182 + 23.183 + private static void assertEqualsDouble(final Number n, final String name) { 23.184 + assertEquals(n.doubleValue(), ((Number)e.get(name)).doubleValue()); 23.185 + } 23.186 + 23.187 + @Test 23.188 + public void accessStaticFieldShortBoxing() throws ScriptException { 23.189 + e.eval("var ps_short = SharedObject.publicStaticShort;"); 23.190 + assertEqualsDouble(SharedObject.publicStaticShort, "ps_short"); 23.191 + e.eval("SharedObject.publicStaticShort = 180;"); 23.192 + assertEquals(180, SharedObject.publicStaticShort); 23.193 + } 23.194 + 23.195 + @Test 23.196 + public void accessFinalFieldShortBoxing() throws ScriptException { 23.197 + e.eval("var pf_short = o.publicFinalShortBox;"); 23.198 + assertEqualsDouble(o.publicFinalShortBox, "pf_short"); 23.199 + e.eval("o.publicFinalShortBox = 180;"); 23.200 + assertEquals(Short.valueOf((short)-26777), o.publicFinalShortBox); 23.201 + } 23.202 + 23.203 + @Test 23.204 + public void accessStaticFinalFieldShortBoxing() throws ScriptException { 23.205 + e.eval("var psf_short = SharedObject.publicStaticFinalShort;"); 23.206 + assertEqualsDouble(SharedObject.publicStaticFinalShort, "psf_short"); 23.207 + e.eval("SharedObject.publicStaticFinalShort = 180;"); 23.208 + assertEquals(8888, SharedObject.publicStaticFinalShort); 23.209 + } 23.210 + 23.211 + // --------------------------------char 23.212 + // tests------------------------------------ 23.213 + @Test 23.214 + public void accessFieldCharBoxing() throws ScriptException { 23.215 + e.eval("var p_char = o.publicCharBox;"); 23.216 + assertEquals(o.publicCharBox, e.get("p_char")); 23.217 + e.eval("o.publicCharBox = 'S';"); 23.218 + assertEquals(Character.valueOf('S'), o.publicCharBox); 23.219 + e.eval("try {" + 23.220 + " o.publicCharBox = 'Big string';" + 23.221 + "} catch(e) {" + 23.222 + " var isThrown = true;" + 23.223 + "}"); 23.224 + assertEquals("Exception thrown", true, e.get("isThrown")); 23.225 + assertEquals(Character.valueOf('S'), o.publicCharBox); 23.226 + } 23.227 + 23.228 + @Test 23.229 + public void accessStaticFieldCharBoxing() throws ScriptException { 23.230 + e.eval("var ps_char = SharedObject.publicStaticChar;"); 23.231 + assertEquals(SharedObject.publicStaticChar, e.get("ps_char")); 23.232 + e.eval("SharedObject.publicStaticChar = 'Z';"); 23.233 + assertEquals('Z', SharedObject.publicStaticChar); 23.234 + } 23.235 + 23.236 + @Test 23.237 + public void accessFinalFieldCharBoxing() throws ScriptException { 23.238 + e.eval("var pf_char = o.publicFinalCharBox;"); 23.239 + assertEquals(o.publicFinalCharBox, e.get("pf_char")); 23.240 + e.eval("o.publicFinalCharBox = 'S';"); 23.241 + assertEquals(Character.valueOf('F'), o.publicFinalCharBox); 23.242 + } 23.243 + 23.244 + @Test 23.245 + public void accessStaticFinalFieldCharBoxing() throws ScriptException { 23.246 + e.eval("var psf_char = SharedObject.publicStaticFinalChar;"); 23.247 + assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char")); 23.248 + e.eval("SharedObject.publicStaticFinalChar = 'Z';"); 23.249 + assertEquals('K', SharedObject.publicStaticFinalChar); 23.250 + } 23.251 + 23.252 + // --------------------------------float 23.253 + // tests------------------------------------ 23.254 + @Test 23.255 + public void accessFieldFloatBoxing() throws ScriptException { 23.256 + e.eval("var p_float = o.publicFloatBox;"); 23.257 + assertEqualsDouble(o.publicFloatBox, "p_float"); 23.258 + o.publicFloatBox = 0.0f / 0.0f; 23.259 + assertEquals(true, e.eval("isNaN(o.publicFloatBox)")); 23.260 + o.publicFloatBox = 1.0f / 0.0f; 23.261 + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicFloatBox")); 23.262 + o.publicFloatBox = -1.0f / 0.0f; 23.263 + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicFloatBox")); 23.264 + e.eval("o.publicFloatBox = 20;"); 23.265 + assertEquals(20, o.publicFloatBox, 1e-10); 23.266 + e.eval("o.publicFloatBox = 0.0/0.0;"); 23.267 + assertTrue(Float.isNaN(o.publicFloatBox)); 23.268 + e.eval("o.publicFloatBox = 1.0/0.0;"); 23.269 + assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(o.publicFloatBox)); 23.270 + e.eval("o.publicFloatBox = -1.0/0.0;"); 23.271 + assertEquals(Float.NEGATIVE_INFINITY, o.publicFloatBox, 1e-10); 23.272 + } 23.273 + 23.274 + @Test 23.275 + public void accessStaticFieldFloatBoxing() throws ScriptException { 23.276 + e.eval("var ps_float = SharedObject.publicStaticFloat;"); 23.277 + assertEqualsDouble(SharedObject.publicStaticFloat, "ps_float"); 23.278 + SharedObject.publicStaticFloat = 0.0f / 0.0f; 23.279 + assertEquals(true, e.eval("isNaN(SharedObject.publicStaticFloat)")); 23.280 + SharedObject.publicStaticFloat = 1.0f / 0.0f; 23.281 + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticFloat")); 23.282 + SharedObject.publicStaticFloat = -1.0f / 0.0f; 23.283 + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticFloat")); 23.284 + e.eval("SharedObject.publicStaticFloat = 20.0;"); 23.285 + assertEquals(20.0f, SharedObject.publicStaticFloat, 1e-10); 23.286 + e.eval("SharedObject.publicStaticFloat = 0.0/0.0;"); 23.287 + assertTrue(Float.isNaN(SharedObject.publicStaticFloat)); 23.288 + e.eval("SharedObject.publicStaticFloat = 1.0/0.0;"); 23.289 + assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); 23.290 + e.eval("SharedObject.publicStaticFloat = -1.0/0.0;"); 23.291 + assertEquals(Float.floatToIntBits(Float.NEGATIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); 23.292 + } 23.293 + 23.294 + @Test 23.295 + public void accessFinalFloatBoxing() throws ScriptException { 23.296 + e.eval("var pf_float = o.publicFinalFloatBox;"); 23.297 + assertEqualsDouble(o.publicFinalFloatBox, "pf_float"); 23.298 + e.eval("o.publicFinalFloatBox = 20.0;"); 23.299 + assertEquals(1.372e4f, o.publicFinalFloatBox, 1e-10); 23.300 + } 23.301 + 23.302 + @Test 23.303 + public void accessStaticFinalFieldFloatBoxing() throws ScriptException { 23.304 + e.eval("var psf_float = SharedObject.publicStaticFinalFloat;"); 23.305 + assertEqualsDouble(SharedObject.publicStaticFinalFloat, "psf_float"); 23.306 + e.eval("SharedObject.publicStaticFinalFloat = 20.0;"); 23.307 + assertEquals(0.72e8f, SharedObject.publicStaticFinalFloat, 1e-10); 23.308 + } 23.309 + 23.310 + // --------------------------------double 23.311 + // tests------------------------------------ 23.312 + @Test 23.313 + public void accessFieldDoubleBoxing() throws ScriptException { 23.314 + e.eval("var p_double = o.publicDoubleBox;"); 23.315 + assertEquals(o.publicDoubleBox, e.get("p_double")); 23.316 + o.publicDoubleBox = 0.0 / 0.0; 23.317 + assertEquals(true, e.eval("isNaN(o.publicDoubleBox)")); 23.318 + o.publicDoubleBox = 1.0 / 0.0; 23.319 + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicDoubleBox")); 23.320 + o.publicDoubleBox = -1.0 / 0.0; 23.321 + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicDoubleBox")); 23.322 + e.eval("o.publicDoubleBox = 30;"); 23.323 + assertEquals(Double.doubleToLongBits(30.0), Double.doubleToLongBits(o.publicDoubleBox)); 23.324 + e.eval("o.publicDoubleBox = 0.0/0.0;"); 23.325 + assertTrue(Double.isNaN(o.publicDoubleBox)); 23.326 + e.eval("o.publicDoubleBox = 1.0/0.0;"); 23.327 + assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(o.publicDoubleBox)); 23.328 + e.eval("o.publicDoubleBox = -1.0/0.0;"); 23.329 + assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(o.publicDoubleBox)); 23.330 + } 23.331 + 23.332 + @Test 23.333 + public void accessStaticFieldDoubleBoxing() throws ScriptException { 23.334 + e.eval("var ps_double = SharedObject.publicStaticDouble;"); 23.335 + assertEquals(SharedObject.publicStaticDouble, e.get("ps_double")); 23.336 + SharedObject.publicStaticDouble = 0.0 / 0.0; 23.337 + assertEquals(true, e.eval("isNaN(SharedObject.publicStaticDouble)")); 23.338 + SharedObject.publicStaticDouble = 1.0 / 0.0; 23.339 + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticDouble")); 23.340 + SharedObject.publicStaticDouble = -1.0 / 0.0; 23.341 + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticDouble")); 23.342 + e.eval("SharedObject.publicStaticDouble = 40.0;"); 23.343 + assertEquals(Double.doubleToLongBits(40.0), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 23.344 + e.eval("SharedObject.publicStaticDouble = 0.0/0.0;"); 23.345 + assertTrue(Double.isNaN(SharedObject.publicStaticDouble)); 23.346 + e.eval("SharedObject.publicStaticDouble = 1.0/0.0;"); 23.347 + assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 23.348 + e.eval("SharedObject.publicStaticDouble = -1.0/0.0;"); 23.349 + assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); 23.350 + } 23.351 + 23.352 + @Test 23.353 + public void accessFinalFieldDoubleBoxing() throws ScriptException { 23.354 + e.eval("var pf_double = o.publicFinalDoubleBox;"); 23.355 + assertEquals(o.publicFinalDoubleBox, e.get("pf_double")); 23.356 + e.eval("o.publicFinalDoubleBox = 30.0;"); 23.357 + assertEquals(Double.doubleToLongBits(1.412e-12), Double.doubleToLongBits(o.publicFinalDoubleBox)); 23.358 + } 23.359 + 23.360 + @Test 23.361 + public void accessStaticFinalFieldDoubleBoxing() throws ScriptException { 23.362 + e.eval("var psf_double = SharedObject.publicStaticFinalDouble;"); 23.363 + assertEquals(SharedObject.publicStaticFinalDouble, e.get("psf_double")); 23.364 + e.eval("SharedObject.publicStaticFinalDouble = 40.0;"); 23.365 + assertEquals(Double.doubleToLongBits(1.8e12), Double.doubleToLongBits(SharedObject.publicStaticFinalDouble)); 23.366 + } 23.367 + 23.368 +}
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 24.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/ObjectAccessTest.java Mon Apr 06 16:18:54 2015 +0530 24.3 @@ -0,0 +1,165 @@ 24.4 +/* 24.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 24.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 24.7 + * 24.8 + * This code is free software; you can redistribute it and/or modify it 24.9 + * under the terms of the GNU General Public License version 2 only, as 24.10 + * published by the Free Software Foundation. Oracle designates this 24.11 + * particular file as subject to the "Classpath" exception as provided 24.12 + * by Oracle in the LICENSE file that accompanied this code. 24.13 + * 24.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 24.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 24.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 24.17 + * version 2 for more details (a copy is included in the LICENSE file that 24.18 + * accompanied this code). 24.19 + * 24.20 + * You should have received a copy of the GNU General Public License version 24.21 + * 2 along with this work; if not, write to the Free Software Foundation, 24.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24.23 + * 24.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24.25 + * or visit www.oracle.com if you need additional information or have any 24.26 + * questions. 24.27 + */ 24.28 + 24.29 +package jdk.nashorn.api.javaaccess.test; 24.30 + 24.31 +import static org.testng.AssertJUnit.assertEquals; 24.32 +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; 24.33 +import javax.script.ScriptEngine; 24.34 +import javax.script.ScriptEngineManager; 24.35 +import javax.script.ScriptException; 24.36 +import org.testng.TestNG; 24.37 +import org.testng.annotations.AfterClass; 24.38 +import org.testng.annotations.BeforeClass; 24.39 +import org.testng.annotations.Test; 24.40 + 24.41 +/** 24.42 + * @test 24.43 + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.ObjectAccessTest 24.44 + * @run testng/othervm jdk.nashorn.api.javaaccess.ObjectAccessTest 24.45 + */ 24.46 +@SuppressWarnings("javadoc") 24.47 +public class ObjectAccessTest { 24.48 + 24.49 + private static ScriptEngine e = null; 24.50 + private static SharedObject o = null; 24.51 + 24.52 + public static void main(final String[] args) { 24.53 + TestNG.main(args); 24.54 + } 24.55 + 24.56 + @BeforeClass 24.57 + public static void setUpClass() throws ScriptException { 24.58 + final ScriptEngineManager m = new ScriptEngineManager(); 24.59 + e = m.getEngineByName("nashorn"); 24.60 + o = new SharedObject(); 24.61 + e.put("o", o); 24.62 + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 24.63 + e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;"); 24.64 + } 24.65 + 24.66 + @AfterClass 24.67 + public static void tearDownClass() { 24.68 + e = null; 24.69 + o = null; 24.70 + } 24.71 + 24.72 + @Test 24.73 + public void accessFieldObject() throws ScriptException { 24.74 + e.eval("var p_object = o.publicObject;"); 24.75 + assertEquals(o.publicObject, e.get("p_object")); 24.76 + assertEquals("object", e.eval("typeof p_object;")); 24.77 + e.eval("o.publicObject = new Person(14);"); 24.78 + assertEquals(new Person(14), o.publicObject); 24.79 + } 24.80 + 24.81 + @Test 24.82 + public void accessFieldObjectArray() throws ScriptException { 24.83 + e.eval("var p_object_array = o.publicObjectArray;"); 24.84 + assertEquals(o.publicObjectArray[0], e.eval("o.publicObjectArray[0]")); 24.85 + assertArrayEquals(o.publicObjectArray, (Object[])e.get("p_object_array")); 24.86 + e.eval("var t_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + 24.87 + "t_object_arr[0] = new Person(100);" + 24.88 + "t_object_arr[1] = new Person(120);" + 24.89 + "t_object_arr[2] = new Person(140);" + 24.90 + "o.publicObjectArray = t_object_arr;"); 24.91 + assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, o.publicObjectArray); 24.92 + e.eval("o.publicObjectArray[0] = new Person(10);"); 24.93 + assertEquals(new Person(10), o.publicObjectArray[0]); 24.94 + } 24.95 + 24.96 + @Test 24.97 + public void accessStaticFieldObject() throws ScriptException { 24.98 + e.eval("var ps_object = SharedObject.publicStaticObject;"); 24.99 + assertEquals(SharedObject.publicStaticObject, e.get("ps_object")); 24.100 + assertEquals("object", e.eval("typeof ps_object;")); 24.101 + e.eval("SharedObject.publicStaticObject = new Person(16);"); 24.102 + assertEquals(new Person(16), SharedObject.publicStaticObject); 24.103 + } 24.104 + 24.105 + @Test 24.106 + public void accessStaticFieldObjectArray() throws ScriptException { 24.107 + e.eval("var ps_object_array = SharedObject.publicStaticObjectArray;"); 24.108 + assertEquals(SharedObject.publicStaticObjectArray[0], e.eval("SharedObject.publicStaticObjectArray[0]")); 24.109 + assertArrayEquals(SharedObject.publicStaticObjectArray, (Object[])e.get("ps_object_array")); 24.110 + e.eval("var ts_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + 24.111 + "ts_object_arr[0] = new Person(100);" + 24.112 + "ts_object_arr[1] = new Person(120);" + 24.113 + "ts_object_arr[2] = new Person(140);" + 24.114 + "SharedObject.publicStaticObjectArray = ts_object_arr;"); 24.115 + assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, SharedObject.publicStaticObjectArray); 24.116 + e.eval("SharedObject.publicStaticObjectArray[0] = new Person(10);"); 24.117 + assertEquals(new Person(10), SharedObject.publicStaticObjectArray[0]); 24.118 + } 24.119 + 24.120 + @Test 24.121 + public void accessFinalFieldObject() throws ScriptException { 24.122 + e.eval("var pf_object = o.publicFinalObject;"); 24.123 + assertEquals(o.publicFinalObject, e.get("pf_object")); 24.124 + assertEquals("object", e.eval("typeof pf_object;")); 24.125 + e.eval("o.publicFinalObject = new Person(-999);"); 24.126 + assertEquals(new Person(1024), o.publicFinalObject); 24.127 + } 24.128 + 24.129 + @Test 24.130 + public void accessFinalFieldObjectArray() throws ScriptException { 24.131 + e.eval("var pf_object_array = o.publicFinalObjectArray;"); 24.132 + assertEquals(o.publicFinalObjectArray[0], e.eval("o.publicFinalObjectArray[0]")); 24.133 + assertArrayEquals(o.publicFinalObjectArray, (Object[])e.get("pf_object_array")); 24.134 + e.eval("var tf_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + 24.135 + "tf_object_arr[0] = new Person(100);" + 24.136 + "tf_object_arr[1] = new Person(120);" + 24.137 + "tf_object_arr[2] = new Person(140);" + 24.138 + "o.publicOFinalbjectArray = tf_object_arr;"); 24.139 + assertArrayEquals(new Person[] { new Person(-900), new Person(1000), new Person(180) }, o.publicFinalObjectArray); 24.140 + e.eval("o.publicFinalObjectArray[0] = new Person(10);"); 24.141 + assertEquals(new Person(10), o.publicFinalObjectArray[0]); 24.142 + } 24.143 + 24.144 + @Test 24.145 + public void accessStaticFinalFieldObject() throws ScriptException { 24.146 + e.eval("var psf_object = SharedObject.publicStaticFinalObject;"); 24.147 + assertEquals(SharedObject.publicStaticFinalObject, e.get("psf_object")); 24.148 + assertEquals("object", e.eval("typeof psf_object;")); 24.149 + e.eval("SharedObject.publicStaticFinalObject = new Person(6);"); 24.150 + assertEquals(new Person(2048), SharedObject.publicStaticFinalObject); 24.151 + } 24.152 + 24.153 + @Test 24.154 + public void accessStaticFinalFieldObjectArray() throws ScriptException { 24.155 + e.eval("var psf_object_array = SharedObject.publicStaticFinalObjectArray;"); 24.156 + assertEquals(SharedObject.publicStaticFinalObjectArray[0], e.eval("SharedObject.publicStaticFinalObjectArray[0]")); 24.157 + assertArrayEquals(SharedObject.publicStaticFinalObjectArray, (Object[])e.get("psf_object_array")); 24.158 + e.eval("var tsf_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + 24.159 + "tsf_object_arr[0] = new Person(100);" + 24.160 + "tsf_object_arr[1] = new Person(120);" + 24.161 + "tsf_object_arr[2] = new Person(140);" + 24.162 + "SharedObject.publicStaticFinalObjectArray = tsf_object_arr;"); 24.163 + assertArrayEquals(new Person[] { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }, SharedObject.publicStaticFinalObjectArray); 24.164 + e.eval("SharedObject.publicStaticFinalObjectArray[0] = new Person(90);"); 24.165 + assertEquals(new Person(90), SharedObject.publicStaticFinalObjectArray[0]); 24.166 + } 24.167 + 24.168 +}
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 25.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/Person.java Mon Apr 06 16:18:54 2015 +0530 25.3 @@ -0,0 +1,59 @@ 25.4 +/* 25.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 25.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 25.7 + * 25.8 + * This code is free software; you can redistribute it and/or modify it 25.9 + * under the terms of the GNU General Public License version 2 only, as 25.10 + * published by the Free Software Foundation. Oracle designates this 25.11 + * particular file as subject to the "Classpath" exception as provided 25.12 + * by Oracle in the LICENSE file that accompanied this code. 25.13 + * 25.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 25.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 25.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 25.17 + * version 2 for more details (a copy is included in the LICENSE file that 25.18 + * accompanied this code). 25.19 + * 25.20 + * You should have received a copy of the GNU General Public License version 25.21 + * 2 along with this work; if not, write to the Free Software Foundation, 25.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 25.23 + * 25.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 25.25 + * or visit www.oracle.com if you need additional information or have any 25.26 + * questions. 25.27 + */ 25.28 + 25.29 +package jdk.nashorn.api.javaaccess.test; 25.30 + 25.31 +@SuppressWarnings("javadoc") 25.32 +public class Person { 25.33 + 25.34 + public int id = 0; 25.35 + 25.36 + public Person() { 25.37 + } 25.38 + 25.39 + public Person(final int code) { 25.40 + this.id = code; 25.41 + } 25.42 + 25.43 + @Override 25.44 + public boolean equals(final Object obj) { 25.45 + if (obj != null && obj instanceof Person) { 25.46 + final Person o = (Person)obj; 25.47 + return this.id == o.id; 25.48 + } 25.49 + return false; 25.50 + } 25.51 + 25.52 + @Override 25.53 + public int hashCode() { 25.54 + return id; 25.55 + } 25.56 + 25.57 + @Override 25.58 + public String toString() { 25.59 + return "Person(" + id + ")"; 25.60 + } 25.61 + 25.62 +}
26.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 26.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/SharedObject.java Mon Apr 06 16:18:54 2015 +0530 26.3 @@ -0,0 +1,467 @@ 26.4 +/* 26.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 26.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 26.7 + * 26.8 + * This code is free software; you can redistribute it and/or modify it 26.9 + * under the terms of the GNU General Public License version 2 only, as 26.10 + * published by the Free Software Foundation. Oracle designates this 26.11 + * particular file as subject to the "Classpath" exception as provided 26.12 + * by Oracle in the LICENSE file that accompanied this code. 26.13 + * 26.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 26.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 26.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 26.17 + * version 2 for more details (a copy is included in the LICENSE file that 26.18 + * accompanied this code). 26.19 + * 26.20 + * You should have received a copy of the GNU General Public License version 26.21 + * 2 along with this work; if not, write to the Free Software Foundation, 26.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 26.23 + * 26.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 26.25 + * or visit www.oracle.com if you need additional information or have any 26.26 + * questions. 26.27 + */ 26.28 + 26.29 +package jdk.nashorn.api.javaaccess.test; 26.30 + 26.31 +import javax.script.Invocable; 26.32 +import javax.script.ScriptEngine; 26.33 +import javax.script.ScriptException; 26.34 + 26.35 +@SuppressWarnings("javadoc") 26.36 +public class SharedObject { 26.37 + 26.38 + // Public fields 26.39 + public String publicString = "PublicString"; 26.40 + public String[] publicStringArray = { "ArrayString[0]", "ArrayString[1]", "ArrayString[2]", "ArrayString[3]" }; 26.41 + public Person publicObject = new Person(256); 26.42 + public Person[] publicObjectArray = { new Person(4), new Person(-422), new Person(14) }; 26.43 + public boolean publicBoolean = true; 26.44 + public boolean[] publicBooleanArray = { true, false, false, true }; 26.45 + public Boolean publicBooleanBox = true; 26.46 + public long publicLong = 933333333333333333L; 26.47 + public long[] publicLongArray = { 99012333333333L, -124355555L, 89777777777L }; 26.48 + public Long publicLongBox = 9333333333L; 26.49 + public int publicInt = 2076543123; 26.50 + public int[] publicIntArray = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; 26.51 + public Integer publicIntBox = 20765123; 26.52 + public byte publicByte = -128; 26.53 + public byte[] publicByteArray = { 1, 2, 4, 8, 16, 32, 64, 127, -128 }; 26.54 + public Byte publicByteBox = 127; 26.55 + public short publicShort = 32000; 26.56 + public short[] publicShortArray = { 3240, 8900, -16789, 1, 12 }; 26.57 + public Short publicShortBox = Short.MIN_VALUE; 26.58 + public float publicFloat = 0.7f; 26.59 + public float[] publicFloatArray = { -32.01f, 89.3f, -1.3e8f, 3.1f }; 26.60 + public Float publicFloatBox = 1.377e4f; 26.61 + public double publicDouble = 1.34e20; 26.62 + public double[] publicDoubleArray = { 0.75e80, 8e-43, 1.000077, 0.123e10 }; 26.63 + public Double publicDoubleBox = 1.4e-19; 26.64 + public char publicChar = 'A'; 26.65 + public char[] publicCharArray = "Hello Nashorn".toCharArray(); 26.66 + public Character publicCharBox = 'B'; 26.67 + // Public static fields 26.68 + public static String publicStaticString = "PublicStaticString"; 26.69 + public static String[] publicStaticStringArray = { "StaticArrayString[0]", "StaticArrayString[1]", "StaticArrayString[2]", "StaticArrayString[3]" }; 26.70 + public static Person publicStaticObject = new Person(512); 26.71 + public static Person[] publicStaticObjectArray = { new Person(40), new Person(-22), new Person(18) }; 26.72 + public static boolean publicStaticBoolean = true; 26.73 + public static boolean[] publicStaticBooleanArray = { false, false, false, true }; 26.74 + public static Boolean publicStaticBooleanBox = true; 26.75 + public static long publicStaticLong = 13333333333333333L; 26.76 + public static long[] publicStaticLongArray = { 19012333333333L, -224355555L, 39777777777L }; 26.77 + public static Long publicStaticLongBox = 9333333334L; 26.78 + public static int publicStaticInt = 207654323; 26.79 + public static int[] publicStaticIntArray = { 5, 8, 13, 21, 34 }; 26.80 + public static Integer publicStaticIntBox = 2075123; 26.81 + public static byte publicStaticByte = -12; 26.82 + public static byte[] publicStaticByteArray = { 16, 32, 64, 127, -128 }; 26.83 + public static Byte publicStaticByteBox = 17; 26.84 + public static short publicStaticShort = 320; 26.85 + public static short[] publicStaticShortArray = { 1240, 900, -1789, 100, 12 }; 26.86 + public static Short publicStaticShortBox = -16777; 26.87 + public static float publicStaticFloat = 7.7e8f; 26.88 + public static float[] publicStaticFloatArray = { -131.01f, 189.3f, -31.3e8f, 3.7f }; 26.89 + public static Float publicStaticFloatBox = 1.37e4f; 26.90 + public static double publicStaticDouble = 1.341e20; 26.91 + public static double[] publicStaticDoubleArray = { 0.75e80, 0.123e10, 8e-43, 1.000077 }; 26.92 + public static Double publicStaticDoubleBox = 1.41e-12; 26.93 + public static char publicStaticChar = 'C'; 26.94 + public static char[] publicStaticCharArray = "Nashorn".toCharArray(); 26.95 + public static Character publicStaticCharBox = 'D'; 26.96 + // Public final fields 26.97 + public final String publicFinalString = "PublicFinalString"; 26.98 + public final String[] publicFinalStringArray = { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" }; 26.99 + public final Person publicFinalObject = new Person(1024); 26.100 + public final Person[] publicFinalObjectArray = { new Person(-900), new Person(1000), new Person(180) }; 26.101 + public final boolean publicFinalBoolean = true; 26.102 + public final boolean[] publicFinalBooleanArray = { false, false, true, false }; 26.103 + public final Boolean publicFinalBooleanBox = true; 26.104 + public final long publicFinalLong = 13353333333333333L; 26.105 + public final long[] publicFinalLongArray = { 1901733333333L, -2247355555L, 3977377777L }; 26.106 + public final Long publicFinalLongBox = 9377333334L; 26.107 + public final int publicFinalInt = 20712023; 26.108 + public final int[] publicFinalIntArray = { 50, 80, 130, 210, 340 }; 26.109 + public final Integer publicFinalIntBox = 207512301; 26.110 + public final byte publicFinalByte = -7; 26.111 + public final byte[] publicFinalByteArray = { 1, 3, 6, 17, -128 }; 26.112 + public final Byte publicFinalByteBox = 19; 26.113 + public final short publicFinalShort = 31220; 26.114 + public final short[] publicFinalShortArray = { 12240, 9200, -17289, 1200, 12 }; 26.115 + public final Short publicFinalShortBox = -26777; 26.116 + public final float publicFinalFloat = 7.72e8f; 26.117 + public final float[] publicFinalFloatArray = { -131.012f, 189.32f, -31.32e8f, 3.72f }; 26.118 + public final Float publicFinalFloatBox = 1.372e4f; 26.119 + public final double publicFinalDouble = 1.3412e20; 26.120 + public final double[] publicFinalDoubleArray = { 0.725e80, 0.12e10, 8e-3, 1.00077 }; 26.121 + public final Double publicFinalDoubleBox = 1.412e-12; 26.122 + public final char publicFinalChar = 'E'; 26.123 + public final char[] publicFinalCharArray = "Nashorn hello".toCharArray(); 26.124 + public final Character publicFinalCharBox = 'F'; 26.125 + // Public static final fields 26.126 + public static final String publicStaticFinalString = "PublicStaticFinalString"; 26.127 + public static final String[] publicStaticFinalStringArray = { "StaticFinalArrayString[0]", "StaticFinalArrayString[1]", "StaticFinalArrayString[2]", "StaticFinalArrayString[3]" }; 26.128 + public static final Person publicStaticFinalObject = new Person(2048); 26.129 + public static final Person[] publicStaticFinalObjectArray = { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }; 26.130 + public static final boolean publicStaticFinalBoolean = true; 26.131 + public static final boolean[] publicStaticFinalBooleanArray = { false, true, false, false }; 26.132 + public static final Boolean publicStaticFinalBooleanBox = true; 26.133 + public static final long publicStaticFinalLong = 8333333333333L; 26.134 + public static final long[] publicStaticFinalLongArray = { 19017383333L, -2247358L, 39773787L }; 26.135 + public static final Long publicStaticFinalLongBox = 9377388334L; 26.136 + public static final int publicStaticFinalInt = 207182023; 26.137 + public static final int[] publicStaticFinalIntArray = { 1308, 210, 340 }; 26.138 + public static final Integer publicStaticFinalIntBox = 2078301; 26.139 + public static final byte publicStaticFinalByte = -70; 26.140 + public static final byte[] publicStaticFinalByteArray = { 17, -128, 81 }; 26.141 + public static final Byte publicStaticFinalByteBox = 91; 26.142 + public static final short publicStaticFinalShort = 8888; 26.143 + public static final short[] publicStaticFinalShortArray = { 8240, 9280, -1289, 120, 812 }; 26.144 + public static final Short publicStaticFinalShortBox = -26; 26.145 + public static final float publicStaticFinalFloat = 0.72e8f; 26.146 + public static final float[] publicStaticFinalFloatArray = { -8131.012f, 9.32f, -138.32e8f, 0.72f }; 26.147 + public static final Float publicStaticFinalFloatBox = 1.2e4f; 26.148 + public static final double publicStaticFinalDouble = 1.8e12; 26.149 + public static final double[] publicStaticFinalDoubleArray = { 8.725e80, 0.82e10, 18e-3, 1.08077 }; 26.150 + public static final Double publicStaticFinalDoubleBox = 1.5612e-13; 26.151 + public static final char publicStaticFinalChar = 'K'; 26.152 + public static final char[] publicStaticFinalCharArray = "StaticString".toCharArray(); 26.153 + public static final Character publicStaticFinalCharBox = 'L'; 26.154 + 26.155 + // Special vars 26.156 + public volatile boolean volatileBoolean = true; 26.157 + public transient boolean transientBoolean = true; 26.158 + 26.159 + // For methods testing 26.160 + public boolean isAccessed = false; 26.161 + public volatile boolean isFinished = false; 26.162 + 26.163 + private ScriptEngine engine; 26.164 + 26.165 + public ScriptEngine getEngine() { 26.166 + return engine; 26.167 + } 26.168 + 26.169 + public void setEngine(final ScriptEngine engine) { 26.170 + this.engine = engine; 26.171 + } 26.172 + 26.173 + public void voidMethod() { 26.174 + isAccessed = true; 26.175 + } 26.176 + 26.177 + public boolean booleanMethod(final boolean arg) { 26.178 + return !arg; 26.179 + } 26.180 + 26.181 + public Boolean booleanBoxingMethod(final Boolean arg) { 26.182 + return !arg.booleanValue(); 26.183 + } 26.184 + 26.185 + public boolean[] booleanArrayMethod(final boolean arg[]) { 26.186 + final boolean[] res = new boolean[arg.length]; 26.187 + for (int i = 0; i < arg.length; i++) { 26.188 + res[i] = !arg[i]; 26.189 + } 26.190 + return res; 26.191 + } 26.192 + 26.193 + public int intMethod(final int arg) { 26.194 + return arg + arg; 26.195 + } 26.196 + 26.197 + public Integer intBoxingMethod(final Integer arg) { 26.198 + return arg + arg; 26.199 + } 26.200 + 26.201 + public int[] intArrayMethod(final int arg[]) { 26.202 + final int[] res = new int[arg.length]; 26.203 + for (int i = 0; i < arg.length; i++) { 26.204 + res[i] = arg[i] * 2; 26.205 + } 26.206 + return res; 26.207 + } 26.208 + 26.209 + public long longMethod(final long arg) { 26.210 + return arg + arg; 26.211 + } 26.212 + 26.213 + public Long longBoxingMethod(final Long arg) { 26.214 + return arg + arg; 26.215 + } 26.216 + 26.217 + public long[] longArrayMethod(final long[] arg) { 26.218 + final long[] res = new long[arg.length]; 26.219 + for (int i = 0; i < arg.length; i++) { 26.220 + res[i] = arg[i] * 2; 26.221 + } 26.222 + return res; 26.223 + } 26.224 + 26.225 + public byte byteMethod(final byte arg) { 26.226 + return (byte)(arg + arg); 26.227 + } 26.228 + 26.229 + public Byte byteBoxingMethod(final Byte arg) { 26.230 + return (byte)(arg + arg); 26.231 + } 26.232 + 26.233 + public byte[] byteArrayMethod(final byte[] arg) { 26.234 + final byte[] res = new byte[arg.length]; 26.235 + for (int i = 0; i < arg.length; i++) { 26.236 + res[i] = (byte)(arg[i] * 2); 26.237 + } 26.238 + return res; 26.239 + } 26.240 + 26.241 + public char charMethod(final char arg) { 26.242 + return Character.toUpperCase(arg); 26.243 + } 26.244 + 26.245 + public Character charBoxingMethod(final Character arg) { 26.246 + return Character.toUpperCase(arg); 26.247 + } 26.248 + 26.249 + public char[] charArrayMethod(final char[] arg) { 26.250 + final char[] res = new char[arg.length]; 26.251 + for (int i = 0; i < arg.length; i++) { 26.252 + res[i] = Character.toUpperCase(arg[i]); 26.253 + } 26.254 + return res; 26.255 + } 26.256 + 26.257 + public short shortMethod(final short arg) { 26.258 + return (short)(arg + arg); 26.259 + } 26.260 + 26.261 + public Short shortBoxingMethod(final Short arg) { 26.262 + return (short)(arg + arg); 26.263 + } 26.264 + 26.265 + public short[] shortArrayMethod(final short[] arg) { 26.266 + final short[] res = new short[arg.length]; 26.267 + for (int i = 0; i < arg.length; i++) { 26.268 + res[i] = (short)(arg[i] * 2); 26.269 + } 26.270 + return res; 26.271 + } 26.272 + 26.273 + public float floatMethod(final float arg) { 26.274 + return arg + arg; 26.275 + } 26.276 + 26.277 + public Float floatBoxingMethod(final Float arg) { 26.278 + return arg + arg; 26.279 + } 26.280 + 26.281 + public float[] floatArrayMethod(final float[] arg) { 26.282 + final float[] res = new float[arg.length]; 26.283 + for (int i = 0; i < arg.length; i++) { 26.284 + res[i] = arg[i] * 2; 26.285 + } 26.286 + return res; 26.287 + } 26.288 + 26.289 + public double doubleMethod(final double arg) { 26.290 + return arg + arg; 26.291 + } 26.292 + 26.293 + public Double doubleBoxingMethod(final Double arg) { 26.294 + return arg + arg; 26.295 + } 26.296 + 26.297 + public double[] doubleArrayMethod(final double[] arg) { 26.298 + final double[] res = new double[arg.length]; 26.299 + for (int i = 0; i < arg.length; i++) { 26.300 + res[i] = arg[i] * 2; 26.301 + } 26.302 + return res; 26.303 + } 26.304 + 26.305 + public String stringMethod(final String str) { 26.306 + return str + str; 26.307 + } 26.308 + 26.309 + public String[] stringArrayMethod(final String[] arr) { 26.310 + final int l = arr.length; 26.311 + final String[] res = new String[l]; 26.312 + for (int i = 0; i < l; i++) { 26.313 + res[i] = arr[l - i - 1]; 26.314 + } 26.315 + return res; 26.316 + } 26.317 + 26.318 + public Person[] objectArrayMethod(final Person[] arr) { 26.319 + final Person[] res = new Person[arr.length]; 26.320 + for (int i = 0; i < arr.length; i++) { 26.321 + res[i] = new Person(i + 100); 26.322 + } 26.323 + return res; 26.324 + } 26.325 + 26.326 + public Person objectMethod(final Person t) { 26.327 + t.id *= 2; 26.328 + return t; 26.329 + } 26.330 + 26.331 + public int twoParamMethod(final long l, final double d) { 26.332 + return (int)(l + d); 26.333 + } 26.334 + 26.335 + public int threeParamMethod(final short s, final long l, final char c) { 26.336 + return (int)(s + l + c); 26.337 + } 26.338 + 26.339 + public Person[] twoObjectParamMethod(final Person arg1, final Person arg2) { 26.340 + return new Person[] { arg2, arg1 }; 26.341 + } 26.342 + 26.343 + public Person[] threeObjectParamMethod(final Person arg1, final Person arg2, final Person arg3) { 26.344 + return new Person[] { arg3, arg2, arg1 }; 26.345 + } 26.346 + 26.347 + public Person[] eightObjectParamMethod(final Person arg1, final Person arg2, final Person arg3, final Person arg4, final Person arg5, final Person arg6, final Person arg7, final Person arg8) { 26.348 + return new Person[] { arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 }; 26.349 + } 26.350 + 26.351 + public Person[] nineObjectParamMethod(final Person arg1, final Person arg2, final Person arg3, final Person arg4, final Person arg5, final Person arg6, final Person arg7, final Person arg8, final Person arg9) { 26.352 + return new Person[] { arg9, arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 }; 26.353 + } 26.354 + 26.355 + public Person[] methodObjectEllipsis(final Person... args) { 26.356 + final int l = args.length; 26.357 + final Person[] res = new Person[l]; 26.358 + for (int i = 0; i < l; i++) { 26.359 + res[i] = args[l - i - 1]; 26.360 + } 26.361 + return res; 26.362 + } 26.363 + 26.364 + public Person[] methodPrimitiveEllipsis(final int... args) { 26.365 + final int l = args.length; 26.366 + final Person[] res = new Person[l]; 26.367 + for (int i = 0; i < l; i++) { 26.368 + res[i] = new Person(args[i]); 26.369 + } 26.370 + return res; 26.371 + } 26.372 + 26.373 + public Object[] methodMixedEllipsis(final Object... args) { 26.374 + return args; 26.375 + } 26.376 + 26.377 + public Object[] methodObjectWithEllipsis(final String arg, final int... args) { 26.378 + final Object[] res = new Object[args.length + 1]; 26.379 + res[0] = arg; 26.380 + for (int i = 0; i < args.length; i++) { 26.381 + res[i + 1] = args[i]; 26.382 + } 26.383 + return res; 26.384 + } 26.385 + 26.386 + public Object[] methodPrimitiveWithEllipsis(final int arg, final long... args) { 26.387 + final Object[] res = new Object[args.length + 1]; 26.388 + res[0] = arg; 26.389 + for (int i = 0; i < args.length; i++) { 26.390 + res[i + 1] = args[i]; 26.391 + } 26.392 + return res; 26.393 + } 26.394 + 26.395 + public Object[] methodMixedWithEllipsis(final String arg1, final int arg2, final Object... args) { 26.396 + final Object[] res = new Object[args.length + 2]; 26.397 + res[0] = arg1; 26.398 + res[1] = arg2; 26.399 + System.arraycopy(args, 0, res, 2, args.length); 26.400 + return res; 26.401 + } 26.402 + 26.403 + public void methodStartsThread() { 26.404 + isFinished = false; 26.405 + 26.406 + final Thread t = new Thread(new Runnable() { 26.407 + @Override 26.408 + public void run() { 26.409 + try { 26.410 + Thread.sleep(1000); 26.411 + isFinished = true; 26.412 + } catch (final InterruptedException e) { 26.413 + e.printStackTrace(); 26.414 + } 26.415 + } 26.416 + }); 26.417 + 26.418 + t.start(); 26.419 + } 26.420 + 26.421 + public String overloadedMethodDoubleVSint(final int arg) { 26.422 + return "int"; 26.423 + } 26.424 + 26.425 + public String overloadedMethodDoubleVSint(final double arg) { 26.426 + return "double"; 26.427 + } 26.428 + 26.429 + public int overloadedMethod(final int arg) { 26.430 + return arg*2; 26.431 + } 26.432 + 26.433 + public int overloadedMethod(final String arg) { 26.434 + return arg.length(); 26.435 + } 26.436 + 26.437 + public int overloadedMethod(final boolean arg) { 26.438 + return (arg) ? 1 : 0; 26.439 + } 26.440 + 26.441 + public int overloadedMethod(final Person arg) { 26.442 + return arg.id*2; 26.443 + } 26.444 + 26.445 + public int firstLevelMethodInt(final int arg) throws ScriptException, NoSuchMethodException { 26.446 + return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInt", arg); 26.447 + } 26.448 + 26.449 + public int thirdLevelMethodInt(final int arg) { 26.450 + return arg*5; 26.451 + } 26.452 + 26.453 + public int firstLevelMethodInteger(final Integer arg) throws ScriptException, NoSuchMethodException { 26.454 + return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInteger", arg); 26.455 + } 26.456 + 26.457 + public int thirdLevelMethodInteger(final Integer arg) { 26.458 + return arg*10; 26.459 + } 26.460 + 26.461 + public Person firstLevelMethodObject(final Person p) throws ScriptException, NoSuchMethodException { 26.462 + return (Person) ((Invocable)engine).invokeFunction("secondLevelMethodObject", p); 26.463 + } 26.464 + 26.465 + public Person thirdLevelMethodObject(final Person p) { 26.466 + p.id *= 10; 26.467 + return p; 26.468 + } 26.469 + 26.470 +}
27.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 27.2 +++ b/test/src/jdk/nashorn/api/javaaccess/test/StringAccessTest.java Mon Apr 06 16:18:54 2015 +0530 27.3 @@ -0,0 +1,168 @@ 27.4 +/* 27.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 27.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 27.7 + * 27.8 + * This code is free software; you can redistribute it and/or modify it 27.9 + * under the terms of the GNU General Public License version 2 only, as 27.10 + * published by the Free Software Foundation. Oracle designates this 27.11 + * particular file as subject to the "Classpath" exception as provided 27.12 + * by Oracle in the LICENSE file that accompanied this code. 27.13 + * 27.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 27.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 27.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 27.17 + * version 2 for more details (a copy is included in the LICENSE file that 27.18 + * accompanied this code). 27.19 + * 27.20 + * You should have received a copy of the GNU General Public License version 27.21 + * 2 along with this work; if not, write to the Free Software Foundation, 27.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 27.23 + * 27.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 27.25 + * or visit www.oracle.com if you need additional information or have any 27.26 + * questions. 27.27 + */ 27.28 + 27.29 +package jdk.nashorn.api.javaaccess.test; 27.30 + 27.31 +import static org.testng.AssertJUnit.assertEquals; 27.32 +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; 27.33 +import javax.script.ScriptEngine; 27.34 +import javax.script.ScriptEngineManager; 27.35 +import javax.script.ScriptException; 27.36 +import org.testng.TestNG; 27.37 +import org.testng.annotations.AfterClass; 27.38 +import org.testng.annotations.BeforeClass; 27.39 +import org.testng.annotations.Test; 27.40 + 27.41 +/** 27.42 + * @test 27.43 + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.StringAccessTest 27.44 + * @run testng/othervm jdk.nashorn.api.javaaccess.StringAccessTest 27.45 + */ 27.46 +@SuppressWarnings("javadoc") 27.47 +public class StringAccessTest { 27.48 + 27.49 + private static ScriptEngine e = null; 27.50 + private static SharedObject o = null; 27.51 + 27.52 + public static void main(final String[] args) { 27.53 + TestNG.main(args); 27.54 + } 27.55 + 27.56 + @BeforeClass 27.57 + public static void setUpClass() throws ScriptException { 27.58 + final ScriptEngineManager m = new ScriptEngineManager(); 27.59 + e = m.getEngineByName("nashorn"); 27.60 + o = new SharedObject(); 27.61 + e.put("o", o); 27.62 + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); 27.63 + } 27.64 + 27.65 + @AfterClass 27.66 + public static void tearDownClass() { 27.67 + e = null; 27.68 + o = null; 27.69 + } 27.70 + 27.71 + @Test 27.72 + public void accessFieldString() throws ScriptException { 27.73 + e.eval("var p_string = o.publicString;"); 27.74 + assertEquals(o.publicString, e.get("p_string")); 27.75 + assertEquals("string", e.eval("typeof p_string;")); 27.76 + e.eval("o.publicString = 'changedString';"); 27.77 + assertEquals("changedString", o.publicString); 27.78 + } 27.79 + 27.80 + @Test 27.81 + public void accessFieldStringArray() throws ScriptException { 27.82 + e.eval("var p_string_array = o.publicStringArray;"); 27.83 + assertEquals(o.publicStringArray[0], e.eval("o.publicStringArray[0]")); 27.84 + assertArrayEquals(o.publicStringArray, (String[])e.get("p_string_array")); 27.85 + e.eval("var t_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + 27.86 + "t_string_arr[0] = 'abc';" + 27.87 + "t_string_arr[1] = '123';" + 27.88 + "t_string_arr[2] = 'xyzzzz';" + 27.89 + "o.publicStringArray = t_string_arr;"); 27.90 + assertArrayEquals(new String[] { "abc", "123", "xyzzzz" }, o.publicStringArray); 27.91 + e.eval("o.publicStringArray[0] = 'nashorn';"); 27.92 + assertEquals("nashorn", o.publicStringArray[0]); 27.93 + } 27.94 + 27.95 + @Test 27.96 + public void accessStaticFieldString() throws ScriptException { 27.97 + e.eval("var ps_string = SharedObject.publicStaticString;"); 27.98 + assertEquals(SharedObject.publicStaticString, e.get("ps_string")); 27.99 + assertEquals("string", e.eval("typeof ps_string;")); 27.100 + e.eval("SharedObject.publicStaticString = 'changedString';"); 27.101 + assertEquals("changedString", SharedObject.publicStaticString); 27.102 + } 27.103 + 27.104 + @Test 27.105 + public void accessStaticFieldStringArray() throws ScriptException { 27.106 + e.eval("var ps_string_array = SharedObject.publicStaticStringArray;"); 27.107 + assertEquals(SharedObject.publicStaticStringArray[0], e.eval("SharedObject.publicStaticStringArray[0]")); 27.108 + assertArrayEquals(SharedObject.publicStaticStringArray, (String[])e.get("ps_string_array")); 27.109 + e.eval("var ts_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + 27.110 + "ts_string_arr[0] = 'abc';" + 27.111 + "ts_string_arr[1] = '123';" + 27.112 + "ts_string_arr[2] = 'xyzzzz';" + 27.113 + "SharedObject.publicStaticStringArray = ts_string_arr;"); 27.114 + assertArrayEquals(new String[] { "abc", "123", "xyzzzz" }, SharedObject.publicStaticStringArray); 27.115 + e.eval("SharedObject.publicStaticStringArray[0] = 'nashorn';"); 27.116 + assertEquals("nashorn", SharedObject.publicStaticStringArray[0]); 27.117 + } 27.118 + 27.119 + @Test 27.120 + public void accessFinalFieldString() throws ScriptException { 27.121 + e.eval("var pf_string = o.publicFinalString;"); 27.122 + assertEquals(o.publicFinalString, e.get("pf_string")); 27.123 + assertEquals("string", e.eval("typeof pf_string;")); 27.124 + e.eval("o.publicFinalString = 'changedString';"); 27.125 + assertEquals("PublicFinalString", o.publicFinalString); 27.126 + } 27.127 + 27.128 + @Test 27.129 + public void accessFinalFieldStringArray() throws ScriptException { 27.130 + e.eval("var pf_string_array = o.publicFinalStringArray;"); 27.131 + assertEquals(o.publicFinalStringArray[0], e.eval("o.publicFinalStringArray[0]")); 27.132 + assertArrayEquals(o.publicFinalStringArray, (String[])e.get("pf_string_array")); 27.133 + e.eval("var tf_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + 27.134 + "tf_string_arr[0] = 'abc';" + 27.135 + "tf_string_arr[1] = '123';" + 27.136 + "tf_string_arr[2] = 'xyzzzz';" + 27.137 + "o.publicFinalStringArray = tf_string_arr;"); 27.138 + assertArrayEquals(new String[] { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" }, o.publicFinalStringArray); 27.139 + e.eval("o.publicFinalStringArray[0] = 'nashorn';"); 27.140 + assertEquals("nashorn", o.publicFinalStringArray[0]); 27.141 + } 27.142 + 27.143 + @Test 27.144 + public void accessStaticFinalFieldString() throws ScriptException { 27.145 + e.eval("var psf_string = SharedObject.publicStaticFinalString;"); 27.146 + assertEquals(SharedObject.publicStaticFinalString, e.get("psf_string")); 27.147 + assertEquals("string", e.eval("typeof psf_string;")); 27.148 + e.eval("SharedObject.publicStaticFinalString = 'changedString';"); 27.149 + assertEquals("PublicStaticFinalString", SharedObject.publicStaticFinalString); 27.150 + } 27.151 + 27.152 + @Test 27.153 + public void accessStaticFinalFieldStringArray() throws ScriptException { 27.154 + e.eval("var psf_string_array = SharedObject.publicStaticFinalStringArray;"); 27.155 + assertEquals(SharedObject.publicStaticFinalStringArray[0], e.eval("SharedObject.publicStaticFinalStringArray[0]")); 27.156 + assertArrayEquals(SharedObject.publicStaticFinalStringArray, (String[])e.get("psf_string_array")); 27.157 + e.eval("var tsf_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + 27.158 + "tsf_string_arr[0] = 'abc';" + 27.159 + "tsf_string_arr[1] = '123';" + 27.160 + "tsf_string_arr[2] = 'xyzzzz';" + 27.161 + "SharedObject.publicStaticFinalStringArray = tsf_string_arr;"); 27.162 + assertArrayEquals(new String[] { "StaticFinalArrayString[0]", 27.163 + "StaticFinalArrayString[1]", 27.164 + "StaticFinalArrayString[2]", 27.165 + "StaticFinalArrayString[3]" }, 27.166 + SharedObject.publicStaticFinalStringArray); 27.167 + e.eval("SharedObject.publicStaticFinalStringArray[0] = 'nashorn';"); 27.168 + assertEquals("nashorn", SharedObject.publicStaticFinalStringArray[0]); 27.169 + } 27.170 + 27.171 +}
28.1 --- a/test/src/jdk/nashorn/api/scripting/InvocableTest.java Wed Apr 01 13:22:52 2015 -0700 28.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 28.3 @@ -1,539 +0,0 @@ 28.4 -/* 28.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 28.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 28.7 - * 28.8 - * This code is free software; you can redistribute it and/or modify it 28.9 - * under the terms of the GNU General Public License version 2 only, as 28.10 - * published by the Free Software Foundation. Oracle designates this 28.11 - * particular file as subject to the "Classpath" exception as provided 28.12 - * by Oracle in the LICENSE file that accompanied this code. 28.13 - * 28.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 28.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 28.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 28.17 - * version 2 for more details (a copy is included in the LICENSE file that 28.18 - * accompanied this code). 28.19 - * 28.20 - * You should have received a copy of the GNU General Public License version 28.21 - * 2 along with this work; if not, write to the Free Software Foundation, 28.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 28.23 - * 28.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 28.25 - * or visit www.oracle.com if you need additional information or have any 28.26 - * questions. 28.27 - */ 28.28 - 28.29 -package jdk.nashorn.api.scripting; 28.30 - 28.31 -import static org.testng.Assert.assertEquals; 28.32 -import static org.testng.Assert.fail; 28.33 -import java.util.Objects; 28.34 -import java.util.function.Function; 28.35 -import javax.script.Invocable; 28.36 -import javax.script.ScriptContext; 28.37 -import javax.script.ScriptEngine; 28.38 -import javax.script.ScriptEngineManager; 28.39 -import javax.script.ScriptException; 28.40 -import javax.script.SimpleScriptContext; 28.41 -import org.testng.Assert; 28.42 -import org.testng.annotations.Test; 28.43 - 28.44 -/** 28.45 - * Tests for javax.script.Invocable implementation of nashorn. 28.46 - */ 28.47 -@SuppressWarnings("javadoc") 28.48 -public class InvocableTest { 28.49 - 28.50 - private static void log(final String msg) { 28.51 - org.testng.Reporter.log(msg, true); 28.52 - } 28.53 - 28.54 - @Test 28.55 - public void invokeMethodTest() { 28.56 - final ScriptEngineManager m = new ScriptEngineManager(); 28.57 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.58 - 28.59 - try { 28.60 - e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();"); 28.61 - final Object obj = e.get("myExample"); 28.62 - final Object res = ((Invocable) e).invokeMethod(obj, "hello"); 28.63 - assertEquals(res, "Hello World!"); 28.64 - } catch (final Exception exp) { 28.65 - exp.printStackTrace(); 28.66 - fail(exp.getMessage()); 28.67 - } 28.68 - } 28.69 - 28.70 - @Test 28.71 - /** 28.72 - * Check that we can call invokeMethod on an object that we got by 28.73 - * evaluating script with different Context set. 28.74 - */ 28.75 - public void invokeMethodDifferentContextTest() { 28.76 - final ScriptEngineManager m = new ScriptEngineManager(); 28.77 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.78 - 28.79 - try { 28.80 - // define an object with method on it 28.81 - final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); 28.82 - 28.83 - final ScriptContext ctxt = new SimpleScriptContext(); 28.84 - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 28.85 - e.setContext(ctxt); 28.86 - 28.87 - // invoke 'func' on obj - but with current script context changed 28.88 - final Object res = ((Invocable) e).invokeMethod(obj, "hello"); 28.89 - assertEquals(res, "Hello World!"); 28.90 - } catch (final Exception exp) { 28.91 - exp.printStackTrace(); 28.92 - fail(exp.getMessage()); 28.93 - } 28.94 - } 28.95 - 28.96 - @Test 28.97 - /** 28.98 - * Check that invokeMethod throws NPE on null method name. 28.99 - */ 28.100 - public void invokeMethodNullNameTest() { 28.101 - final ScriptEngineManager m = new ScriptEngineManager(); 28.102 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.103 - 28.104 - try { 28.105 - final Object obj = e.eval("({})"); 28.106 - ((Invocable) e).invokeMethod(obj, null); 28.107 - fail("should have thrown NPE"); 28.108 - } catch (final Exception exp) { 28.109 - if (!(exp instanceof NullPointerException)) { 28.110 - exp.printStackTrace(); 28.111 - fail(exp.getMessage()); 28.112 - } 28.113 - } 28.114 - } 28.115 - 28.116 - @Test 28.117 - /** 28.118 - * Check that invokeMethod throws NoSuchMethodException on missing method. 28.119 - */ 28.120 - public void invokeMethodMissingTest() { 28.121 - final ScriptEngineManager m = new ScriptEngineManager(); 28.122 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.123 - 28.124 - try { 28.125 - final Object obj = e.eval("({})"); 28.126 - ((Invocable) e).invokeMethod(obj, "nonExistentMethod"); 28.127 - fail("should have thrown NoSuchMethodException"); 28.128 - } catch (final Exception exp) { 28.129 - if (!(exp instanceof NoSuchMethodException)) { 28.130 - exp.printStackTrace(); 28.131 - fail(exp.getMessage()); 28.132 - } 28.133 - } 28.134 - } 28.135 - 28.136 - @Test 28.137 - /** 28.138 - * Check that calling method on non-script object 'thiz' results in 28.139 - * IllegalArgumentException. 28.140 - */ 28.141 - public void invokeMethodNonScriptObjectThizTest() { 28.142 - final ScriptEngineManager m = new ScriptEngineManager(); 28.143 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.144 - 28.145 - try { 28.146 - ((Invocable) e).invokeMethod(new Object(), "toString"); 28.147 - fail("should have thrown IllegalArgumentException"); 28.148 - } catch (final Exception exp) { 28.149 - if (!(exp instanceof IllegalArgumentException)) { 28.150 - exp.printStackTrace(); 28.151 - fail(exp.getMessage()); 28.152 - } 28.153 - } 28.154 - } 28.155 - 28.156 - @Test 28.157 - /** 28.158 - * Check that calling method on null 'thiz' results in 28.159 - * IllegalArgumentException. 28.160 - */ 28.161 - public void invokeMethodNullThizTest() { 28.162 - final ScriptEngineManager m = new ScriptEngineManager(); 28.163 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.164 - 28.165 - try { 28.166 - ((Invocable) e).invokeMethod(null, "toString"); 28.167 - fail("should have thrown IllegalArgumentException"); 28.168 - } catch (final Exception exp) { 28.169 - if (!(exp instanceof IllegalArgumentException)) { 28.170 - exp.printStackTrace(); 28.171 - fail(exp.getMessage()); 28.172 - } 28.173 - } 28.174 - } 28.175 - 28.176 - @Test 28.177 - /** 28.178 - * Check that calling method on mirror created by another engine results in 28.179 - * IllegalArgumentException. 28.180 - */ 28.181 - public void invokeMethodMixEnginesTest() { 28.182 - final ScriptEngineManager m = new ScriptEngineManager(); 28.183 - final ScriptEngine engine1 = m.getEngineByName("nashorn"); 28.184 - final ScriptEngine engine2 = m.getEngineByName("nashorn"); 28.185 - 28.186 - try { 28.187 - final Object obj = engine1.eval("({ run: function() {} })"); 28.188 - // pass object from engine1 to engine2 as 'thiz' for invokeMethod 28.189 - ((Invocable) engine2).invokeMethod(obj, "run"); 28.190 - fail("should have thrown IllegalArgumentException"); 28.191 - } catch (final Exception exp) { 28.192 - if (!(exp instanceof IllegalArgumentException)) { 28.193 - exp.printStackTrace(); 28.194 - fail(exp.getMessage()); 28.195 - } 28.196 - } 28.197 - } 28.198 - 28.199 - @Test 28.200 - public void getInterfaceTest() { 28.201 - final ScriptEngineManager m = new ScriptEngineManager(); 28.202 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.203 - final Invocable inv = (Invocable) e; 28.204 - 28.205 - // try to get interface from global functions 28.206 - try { 28.207 - e.eval("function run() { print('run'); };"); 28.208 - final Runnable runnable = inv.getInterface(Runnable.class); 28.209 - runnable.run(); 28.210 - } catch (final Exception exp) { 28.211 - exp.printStackTrace(); 28.212 - fail(exp.getMessage()); 28.213 - } 28.214 - 28.215 - // try interface on specific script object 28.216 - try { 28.217 - e.eval("var obj = { run: function() { print('run from obj'); } };"); 28.218 - final Object obj = e.get("obj"); 28.219 - final Runnable runnable = inv.getInterface(obj, Runnable.class); 28.220 - runnable.run(); 28.221 - } catch (final Exception exp) { 28.222 - exp.printStackTrace(); 28.223 - fail(exp.getMessage()); 28.224 - } 28.225 - } 28.226 - 28.227 - public interface Foo { 28.228 - 28.229 - public void bar(); 28.230 - } 28.231 - 28.232 - public interface Foo2 extends Foo { 28.233 - 28.234 - public void bar2(); 28.235 - } 28.236 - 28.237 - @Test 28.238 - public void getInterfaceMissingTest() { 28.239 - final ScriptEngineManager manager = new ScriptEngineManager(); 28.240 - final ScriptEngine engine = manager.getEngineByName("nashorn"); 28.241 - 28.242 - // don't define any function. 28.243 - try { 28.244 - engine.eval(""); 28.245 - } catch (final Exception exp) { 28.246 - exp.printStackTrace(); 28.247 - fail(exp.getMessage()); 28.248 - } 28.249 - 28.250 - Runnable runnable = ((Invocable) engine).getInterface(Runnable.class); 28.251 - if (runnable != null) { 28.252 - fail("runnable is not null!"); 28.253 - } 28.254 - 28.255 - // now define "run" 28.256 - try { 28.257 - engine.eval("function run() { print('this is run function'); }"); 28.258 - } catch (final Exception exp) { 28.259 - exp.printStackTrace(); 28.260 - fail(exp.getMessage()); 28.261 - } 28.262 - runnable = ((Invocable) engine).getInterface(Runnable.class); 28.263 - // should not return null now! 28.264 - runnable.run(); 28.265 - 28.266 - // define only one method of "Foo2" 28.267 - try { 28.268 - engine.eval("function bar() { print('bar function'); }"); 28.269 - } catch (final Exception exp) { 28.270 - exp.printStackTrace(); 28.271 - fail(exp.getMessage()); 28.272 - } 28.273 - 28.274 - Foo2 foo2 = ((Invocable) engine).getInterface(Foo2.class); 28.275 - if (foo2 != null) { 28.276 - throw new RuntimeException("foo2 is not null!"); 28.277 - } 28.278 - 28.279 - // now define other method of "Foo2" 28.280 - try { 28.281 - engine.eval("function bar2() { print('bar2 function'); }"); 28.282 - } catch (final Exception exp) { 28.283 - exp.printStackTrace(); 28.284 - fail(exp.getMessage()); 28.285 - } 28.286 - foo2 = ((Invocable) engine).getInterface(Foo2.class); 28.287 - foo2.bar(); 28.288 - foo2.bar2(); 28.289 - } 28.290 - 28.291 - @Test 28.292 - /** 28.293 - * Try passing non-interface Class object for interface implementation. 28.294 - */ 28.295 - public void getNonInterfaceGetInterfaceTest() { 28.296 - final ScriptEngineManager manager = new ScriptEngineManager(); 28.297 - final ScriptEngine engine = manager.getEngineByName("nashorn"); 28.298 - try { 28.299 - log(Objects.toString(((Invocable) engine).getInterface(Object.class))); 28.300 - fail("Should have thrown IllegalArgumentException"); 28.301 - } catch (final Exception exp) { 28.302 - if (!(exp instanceof IllegalArgumentException)) { 28.303 - fail("IllegalArgumentException expected, got " + exp); 28.304 - } 28.305 - } 28.306 - } 28.307 - 28.308 - @Test 28.309 - /** 28.310 - * Check that we can get interface out of a script object even after 28.311 - * switching to use different ScriptContext. 28.312 - */ 28.313 - public void getInterfaceDifferentContext() { 28.314 - final ScriptEngineManager m = new ScriptEngineManager(); 28.315 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.316 - try { 28.317 - final Object obj = e.eval("({ run: function() { } })"); 28.318 - 28.319 - // change script context 28.320 - final ScriptContext ctxt = new SimpleScriptContext(); 28.321 - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 28.322 - e.setContext(ctxt); 28.323 - 28.324 - final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); 28.325 - r.run(); 28.326 - } catch (final Exception exp) { 28.327 - exp.printStackTrace(); 28.328 - fail(exp.getMessage()); 28.329 - } 28.330 - } 28.331 - 28.332 - @Test 28.333 - /** 28.334 - * Check that getInterface on non-script object 'thiz' results in 28.335 - * IllegalArgumentException. 28.336 - */ 28.337 - public void getInterfaceNonScriptObjectThizTest() { 28.338 - final ScriptEngineManager m = new ScriptEngineManager(); 28.339 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.340 - 28.341 - try { 28.342 - ((Invocable) e).getInterface(new Object(), Runnable.class); 28.343 - fail("should have thrown IllegalArgumentException"); 28.344 - } catch (final Exception exp) { 28.345 - if (!(exp instanceof IllegalArgumentException)) { 28.346 - exp.printStackTrace(); 28.347 - fail(exp.getMessage()); 28.348 - } 28.349 - } 28.350 - } 28.351 - 28.352 - @Test 28.353 - /** 28.354 - * Check that getInterface on null 'thiz' results in 28.355 - * IllegalArgumentException. 28.356 - */ 28.357 - public void getInterfaceNullThizTest() { 28.358 - final ScriptEngineManager m = new ScriptEngineManager(); 28.359 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.360 - 28.361 - try { 28.362 - ((Invocable) e).getInterface(null, Runnable.class); 28.363 - fail("should have thrown IllegalArgumentException"); 28.364 - } catch (final Exception exp) { 28.365 - if (!(exp instanceof IllegalArgumentException)) { 28.366 - exp.printStackTrace(); 28.367 - fail(exp.getMessage()); 28.368 - } 28.369 - } 28.370 - } 28.371 - 28.372 - @Test 28.373 - /** 28.374 - * Check that calling getInterface on mirror created by another engine 28.375 - * results in IllegalArgumentException. 28.376 - */ 28.377 - public void getInterfaceMixEnginesTest() { 28.378 - final ScriptEngineManager m = new ScriptEngineManager(); 28.379 - final ScriptEngine engine1 = m.getEngineByName("nashorn"); 28.380 - final ScriptEngine engine2 = m.getEngineByName("nashorn"); 28.381 - 28.382 - try { 28.383 - final Object obj = engine1.eval("({ run: function() {} })"); 28.384 - // pass object from engine1 to engine2 as 'thiz' for getInterface 28.385 - ((Invocable) engine2).getInterface(obj, Runnable.class); 28.386 - fail("should have thrown IllegalArgumentException"); 28.387 - } catch (final Exception exp) { 28.388 - if (!(exp instanceof IllegalArgumentException)) { 28.389 - exp.printStackTrace(); 28.390 - fail(exp.getMessage()); 28.391 - } 28.392 - } 28.393 - } 28.394 - 28.395 - @Test 28.396 - /** 28.397 - * check that null function name results in NPE. 28.398 - */ 28.399 - public void invokeFunctionNullNameTest() { 28.400 - final ScriptEngineManager m = new ScriptEngineManager(); 28.401 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.402 - 28.403 - try { 28.404 - ((Invocable)e).invokeFunction(null); 28.405 - fail("should have thrown NPE"); 28.406 - } catch (final Exception exp) { 28.407 - if (!(exp instanceof NullPointerException)) { 28.408 - exp.printStackTrace(); 28.409 - fail(exp.getMessage()); 28.410 - } 28.411 - } 28.412 - } 28.413 - 28.414 - @Test 28.415 - /** 28.416 - * Check that attempt to call missing function results in 28.417 - * NoSuchMethodException. 28.418 - */ 28.419 - public void invokeFunctionMissingTest() { 28.420 - final ScriptEngineManager m = new ScriptEngineManager(); 28.421 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.422 - 28.423 - try { 28.424 - ((Invocable)e).invokeFunction("NonExistentFunc"); 28.425 - fail("should have thrown NoSuchMethodException"); 28.426 - } catch (final Exception exp) { 28.427 - if (!(exp instanceof NoSuchMethodException)) { 28.428 - exp.printStackTrace(); 28.429 - fail(exp.getMessage()); 28.430 - } 28.431 - } 28.432 - } 28.433 - 28.434 - @Test 28.435 - /** 28.436 - * Check that invokeFunction calls functions only from current context's 28.437 - * Bindings. 28.438 - */ 28.439 - public void invokeFunctionDifferentContextTest() { 28.440 - final ScriptEngineManager m = new ScriptEngineManager(); 28.441 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.442 - 28.443 - try { 28.444 - // define an object with method on it 28.445 - e.eval("function hello() { return 'Hello World!'; }"); 28.446 - final ScriptContext ctxt = new SimpleScriptContext(); 28.447 - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 28.448 - // change engine's current context 28.449 - e.setContext(ctxt); 28.450 - 28.451 - ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context! 28.452 - fail("should have thrown NoSuchMethodException"); 28.453 - } catch (final Exception exp) { 28.454 - if (!(exp instanceof NoSuchMethodException)) { 28.455 - exp.printStackTrace(); 28.456 - fail(exp.getMessage()); 28.457 - } 28.458 - } 28.459 - } 28.460 - 28.461 - @Test 28.462 - public void invokeFunctionExceptionTest() { 28.463 - final ScriptEngineManager m = new ScriptEngineManager(); 28.464 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.465 - try { 28.466 - e.eval("function func() { throw new TypeError(); }"); 28.467 - } catch (final Throwable t) { 28.468 - t.printStackTrace(); 28.469 - fail(t.getMessage()); 28.470 - } 28.471 - 28.472 - try { 28.473 - ((Invocable) e).invokeFunction("func"); 28.474 - fail("should have thrown exception"); 28.475 - } catch (final ScriptException se) { 28.476 - // ECMA TypeError property wrapped as a ScriptException 28.477 - log("got " + se + " as expected"); 28.478 - } catch (final Throwable t) { 28.479 - t.printStackTrace(); 28.480 - fail(t.getMessage()); 28.481 - } 28.482 - } 28.483 - 28.484 - @Test 28.485 - public void invokeMethodExceptionTest() { 28.486 - final ScriptEngineManager m = new ScriptEngineManager(); 28.487 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.488 - try { 28.489 - e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }"); 28.490 - } catch (final Throwable t) { 28.491 - t.printStackTrace(); 28.492 - fail(t.getMessage()); 28.493 - } 28.494 - 28.495 - try { 28.496 - final Object sobj = e.get("sobj"); 28.497 - ((Invocable) e).invokeMethod(sobj, "foo"); 28.498 - fail("should have thrown exception"); 28.499 - } catch (final ScriptException se) { 28.500 - // ECMA TypeError property wrapped as a ScriptException 28.501 - log("got " + se + " as expected"); 28.502 - } catch (final Throwable t) { 28.503 - t.printStackTrace(); 28.504 - fail(t.getMessage()); 28.505 - } 28.506 - } 28.507 - 28.508 - @Test 28.509 - /** 28.510 - * Tests whether invocation of a JavaScript method through a variable arity 28.511 - * Java method will pass the vararg array. Both non-vararg and vararg 28.512 - * JavaScript methods are tested. 28.513 - * 28.514 - * @throws ScriptException 28.515 - */ 28.516 - public void variableArityInterfaceTest() throws ScriptException { 28.517 - final ScriptEngineManager m = new ScriptEngineManager(); 28.518 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.519 - e.eval( 28.520 - "function test1(i, strings) {" 28.521 - + " return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)" 28.522 - + "}" 28.523 - + "function test2() {" 28.524 - + " return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])" 28.525 - + "}"); 28.526 - final VariableArityTestInterface itf = ((Invocable) e).getInterface(VariableArityTestInterface.class); 28.527 - Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]"); 28.528 - Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]"); 28.529 - } 28.530 - 28.531 - @Test 28.532 - public void defaultMethodTest() throws ScriptException { 28.533 - final ScriptEngineManager m = new ScriptEngineManager(); 28.534 - final ScriptEngine e = m.getEngineByName("nashorn"); 28.535 - final Invocable inv = (Invocable) e; 28.536 - 28.537 - final Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})"); 28.538 - @SuppressWarnings("unchecked") 28.539 - final Function<String, String> func = inv.getInterface(obj, Function.class); 28.540 - assertEquals(func.apply("hello"), "HELLO"); 28.541 - } 28.542 -}
29.1 --- a/test/src/jdk/nashorn/api/scripting/MultipleEngineTest.java Wed Apr 01 13:22:52 2015 -0700 29.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 29.3 @@ -1,52 +0,0 @@ 29.4 -/* 29.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 29.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 29.7 - * 29.8 - * This code is free software; you can redistribute it and/or modify it 29.9 - * under the terms of the GNU General Public License version 2 only, as 29.10 - * published by the Free Software Foundation. Oracle designates this 29.11 - * particular file as subject to the "Classpath" exception as provided 29.12 - * by Oracle in the LICENSE file that accompanied this code. 29.13 - * 29.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 29.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 29.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 29.17 - * version 2 for more details (a copy is included in the LICENSE file that 29.18 - * accompanied this code). 29.19 - * 29.20 - * You should have received a copy of the GNU General Public License version 29.21 - * 2 along with this work; if not, write to the Free Software Foundation, 29.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 29.23 - * 29.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 29.25 - * or visit www.oracle.com if you need additional information or have any 29.26 - * questions. 29.27 - */ 29.28 - 29.29 -package jdk.nashorn.api.scripting; 29.30 - 29.31 -import javax.script.ScriptEngine; 29.32 -import javax.script.ScriptEngineManager; 29.33 -import javax.script.ScriptException; 29.34 -import org.testng.annotations.Test; 29.35 - 29.36 -/** 29.37 - * Test that we can create multiple, independent script engines and use those 29.38 - * independently. 29.39 - * 29.40 - * @test 29.41 - * @run testng jdk.nashorn.api.scripting.MultipleEngineTest 29.42 - */ 29.43 -@SuppressWarnings("javadoc") 29.44 -public class MultipleEngineTest { 29.45 - @Test 29.46 - public void createAndUseManyEngine() throws ScriptException { 29.47 - final ScriptEngineManager m = new ScriptEngineManager(); 29.48 - 29.49 - final ScriptEngine e1 = m.getEngineByName("nashorn"); 29.50 - e1.eval("var x = 33; print(x);"); 29.51 - 29.52 - final ScriptEngine e2 = m.getEngineByName("nashorn"); 29.53 - e2.eval("try { print(x) } catch(e) { print(e); }"); 29.54 - } 29.55 -}
30.1 --- a/test/src/jdk/nashorn/api/scripting/PluggableJSObjectTest.java Wed Apr 01 13:22:52 2015 -0700 30.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 30.3 @@ -1,288 +0,0 @@ 30.4 -/* 30.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 30.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 30.7 - * 30.8 - * This code is free software; you can redistribute it and/or modify it 30.9 - * under the terms of the GNU General Public License version 2 only, as 30.10 - * published by the Free Software Foundation. Oracle designates this 30.11 - * particular file as subject to the "Classpath" exception as provided 30.12 - * by Oracle in the LICENSE file that accompanied this code. 30.13 - * 30.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 30.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 30.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 30.17 - * version 2 for more details (a copy is included in the LICENSE file that 30.18 - * accompanied this code). 30.19 - * 30.20 - * You should have received a copy of the GNU General Public License version 30.21 - * 2 along with this work; if not, write to the Free Software Foundation, 30.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 30.23 - * 30.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 30.25 - * or visit www.oracle.com if you need additional information or have any 30.26 - * questions. 30.27 - */ 30.28 - 30.29 -package jdk.nashorn.api.scripting; 30.30 - 30.31 -import static org.testng.Assert.assertEquals; 30.32 -import static org.testng.Assert.assertFalse; 30.33 -import static org.testng.Assert.fail; 30.34 - 30.35 -import java.nio.IntBuffer; 30.36 -import java.util.Collection; 30.37 -import java.util.HashMap; 30.38 -import java.util.LinkedHashMap; 30.39 -import java.util.Set; 30.40 -import javax.script.ScriptEngine; 30.41 -import javax.script.ScriptEngineManager; 30.42 -import org.testng.annotations.Test; 30.43 - 30.44 -/** 30.45 - * Tests for pluggable external impls. of jdk.nashorn.api.scripting.JSObject. 30.46 - * 30.47 - * JDK-8024615: Refactor ScriptObjectMirror and JSObject to support external 30.48 - * JSObject implementations. 30.49 - */ 30.50 -@SuppressWarnings("javadoc") 30.51 -public class PluggableJSObjectTest { 30.52 - public static class MapWrapperObject extends AbstractJSObject { 30.53 - private final HashMap<String, Object> map = new LinkedHashMap<>(); 30.54 - 30.55 - public HashMap<String, Object> getMap() { 30.56 - return map; 30.57 - } 30.58 - 30.59 - @Override 30.60 - public Object getMember(final String name) { 30.61 - return map.get(name); 30.62 - } 30.63 - 30.64 - @Override 30.65 - public void setMember(final String name, final Object value) { 30.66 - map.put(name, value); 30.67 - } 30.68 - 30.69 - @Override 30.70 - public boolean hasMember(final String name) { 30.71 - return map.containsKey(name); 30.72 - } 30.73 - 30.74 - @Override 30.75 - public void removeMember(final String name) { 30.76 - map.remove(name); 30.77 - } 30.78 - 30.79 - @Override 30.80 - public Set<String> keySet() { 30.81 - return map.keySet(); 30.82 - } 30.83 - 30.84 - @Override 30.85 - public Collection<Object> values() { 30.86 - return map.values(); 30.87 - } 30.88 - } 30.89 - 30.90 - @Test 30.91 - // Named property access on a JSObject 30.92 - public void namedAccessTest() { 30.93 - final ScriptEngineManager m = new ScriptEngineManager(); 30.94 - final ScriptEngine e = m.getEngineByName("nashorn"); 30.95 - try { 30.96 - final MapWrapperObject obj = new MapWrapperObject(); 30.97 - e.put("obj", obj); 30.98 - obj.getMap().put("foo", "bar"); 30.99 - 30.100 - // property-like access on MapWrapperObject objects 30.101 - assertEquals(e.eval("obj.foo"), "bar"); 30.102 - e.eval("obj.foo = 'hello'"); 30.103 - assertEquals(e.eval("'foo' in obj"), Boolean.TRUE); 30.104 - assertEquals(e.eval("obj.foo"), "hello"); 30.105 - assertEquals(obj.getMap().get("foo"), "hello"); 30.106 - e.eval("delete obj.foo"); 30.107 - assertFalse(obj.getMap().containsKey("foo")); 30.108 - assertEquals(e.eval("'foo' in obj"), Boolean.FALSE); 30.109 - } catch (final Exception exp) { 30.110 - exp.printStackTrace(); 30.111 - fail(exp.getMessage()); 30.112 - } 30.113 - } 30.114 - 30.115 - // @bug 8062030: Nashorn bug retrieving array property after key string concatenation 30.116 - @Test 30.117 - // ConsString attribute access on a JSObject 30.118 - public void consStringTest() { 30.119 - final ScriptEngineManager m = new ScriptEngineManager(); 30.120 - final ScriptEngine e = m.getEngineByName("nashorn"); 30.121 - try { 30.122 - final MapWrapperObject obj = new MapWrapperObject(); 30.123 - e.put("obj", obj); 30.124 - e.put("f", "f"); 30.125 - e.eval("obj[f + 'oo'] = 'bar';"); 30.126 - 30.127 - assertEquals(obj.getMap().get("foo"), "bar"); 30.128 - assertEquals(e.eval("obj[f + 'oo']"), "bar"); 30.129 - assertEquals(e.eval("obj['foo']"), "bar"); 30.130 - assertEquals(e.eval("f + 'oo' in obj"), Boolean.TRUE); 30.131 - assertEquals(e.eval("'foo' in obj"), Boolean.TRUE); 30.132 - e.eval("delete obj[f + 'oo']"); 30.133 - assertFalse(obj.getMap().containsKey("foo")); 30.134 - assertEquals(e.eval("obj[f + 'oo']"), null); 30.135 - assertEquals(e.eval("obj['foo']"), null); 30.136 - assertEquals(e.eval("f + 'oo' in obj"), Boolean.FALSE); 30.137 - assertEquals(e.eval("'foo' in obj"), Boolean.FALSE); 30.138 - } catch (final Exception exp) { 30.139 - exp.printStackTrace(); 30.140 - fail(exp.getMessage()); 30.141 - } 30.142 - } 30.143 - 30.144 - public static class BufferObject extends AbstractJSObject { 30.145 - private final IntBuffer buf; 30.146 - 30.147 - public BufferObject(final int size) { 30.148 - buf = IntBuffer.allocate(size); 30.149 - } 30.150 - 30.151 - public IntBuffer getBuffer() { 30.152 - return buf; 30.153 - } 30.154 - 30.155 - @Override 30.156 - public Object getMember(final String name) { 30.157 - return name.equals("length")? buf.capacity() : null; 30.158 - } 30.159 - 30.160 - @Override 30.161 - public boolean hasSlot(final int i) { 30.162 - return i > -1 && i < buf.capacity(); 30.163 - } 30.164 - 30.165 - @Override 30.166 - public Object getSlot(final int i) { 30.167 - return buf.get(i); 30.168 - } 30.169 - 30.170 - @Override 30.171 - public void setSlot(final int i, final Object value) { 30.172 - buf.put(i, ((Number)value).intValue()); 30.173 - } 30.174 - 30.175 - @Override 30.176 - public boolean isArray() { 30.177 - return true; 30.178 - } 30.179 - } 30.180 - 30.181 - @Test 30.182 - // array-like indexed access for a JSObject 30.183 - public void indexedAccessTest() { 30.184 - final ScriptEngineManager m = new ScriptEngineManager(); 30.185 - final ScriptEngine e = m.getEngineByName("nashorn"); 30.186 - try { 30.187 - final BufferObject buf = new BufferObject(2); 30.188 - e.put("buf", buf); 30.189 - 30.190 - // array-like access on BufferObject objects 30.191 - assertEquals(e.eval("buf.length"), buf.getBuffer().capacity()); 30.192 - e.eval("buf[0] = 23"); 30.193 - assertEquals(buf.getBuffer().get(0), 23); 30.194 - assertEquals(e.eval("buf[0]"), 23); 30.195 - assertEquals(e.eval("buf[1]"), 0); 30.196 - buf.getBuffer().put(1, 42); 30.197 - assertEquals(e.eval("buf[1]"), 42); 30.198 - assertEquals(e.eval("Array.isArray(buf)"), Boolean.TRUE); 30.199 - } catch (final Exception exp) { 30.200 - exp.printStackTrace(); 30.201 - fail(exp.getMessage()); 30.202 - } 30.203 - } 30.204 - 30.205 - public static class Adder extends AbstractJSObject { 30.206 - @Override 30.207 - public Object call(final Object thiz, final Object... args) { 30.208 - double res = 0.0; 30.209 - for (final Object arg : args) { 30.210 - res += ((Number)arg).doubleValue(); 30.211 - } 30.212 - return res; 30.213 - } 30.214 - 30.215 - @Override 30.216 - public boolean isFunction() { 30.217 - return true; 30.218 - } 30.219 - } 30.220 - 30.221 - @Test 30.222 - // a callable JSObject 30.223 - public void callableJSObjectTest() { 30.224 - final ScriptEngineManager m = new ScriptEngineManager(); 30.225 - final ScriptEngine e = m.getEngineByName("nashorn"); 30.226 - try { 30.227 - e.put("sum", new Adder()); 30.228 - // check callability of Adder objects 30.229 - assertEquals(e.eval("typeof sum"), "function"); 30.230 - assertEquals(((Number)e.eval("sum(1, 2, 3, 4, 5)")).intValue(), 15); 30.231 - } catch (final Exception exp) { 30.232 - exp.printStackTrace(); 30.233 - fail(exp.getMessage()); 30.234 - } 30.235 - } 30.236 - 30.237 - public static class Factory extends AbstractJSObject { 30.238 - @SuppressWarnings("unused") 30.239 - @Override 30.240 - public Object newObject(final Object... args) { 30.241 - return new HashMap<Object, Object>(); 30.242 - } 30.243 - 30.244 - @Override 30.245 - public boolean isFunction() { 30.246 - return true; 30.247 - } 30.248 - } 30.249 - 30.250 - @Test 30.251 - // a factory JSObject 30.252 - public void factoryJSObjectTest() { 30.253 - final ScriptEngineManager m = new ScriptEngineManager(); 30.254 - final ScriptEngine e = m.getEngineByName("nashorn"); 30.255 - try { 30.256 - e.put("Factory", new Factory()); 30.257 - 30.258 - // check new on Factory 30.259 - assertEquals(e.eval("typeof Factory"), "function"); 30.260 - assertEquals(e.eval("typeof new Factory()"), "object"); 30.261 - assertEquals(e.eval("(new Factory()) instanceof java.util.Map"), Boolean.TRUE); 30.262 - } catch (final Exception exp) { 30.263 - exp.printStackTrace(); 30.264 - fail(exp.getMessage()); 30.265 - } 30.266 - } 30.267 - 30.268 - @Test 30.269 - // iteration tests 30.270 - public void iteratingJSObjectTest() { 30.271 - final ScriptEngineManager m = new ScriptEngineManager(); 30.272 - final ScriptEngine e = m.getEngineByName("nashorn"); 30.273 - try { 30.274 - final MapWrapperObject obj = new MapWrapperObject(); 30.275 - obj.setMember("foo", "hello"); 30.276 - obj.setMember("bar", "world"); 30.277 - e.put("obj", obj); 30.278 - 30.279 - // check for..in 30.280 - Object val = e.eval("var str = ''; for (i in obj) str += i; str"); 30.281 - assertEquals(val.toString(), "foobar"); 30.282 - 30.283 - // check for..each..in 30.284 - val = e.eval("var str = ''; for each (i in obj) str += i; str"); 30.285 - assertEquals(val.toString(), "helloworld"); 30.286 - } catch (final Exception exp) { 30.287 - exp.printStackTrace(); 30.288 - fail(exp.getMessage()); 30.289 - } 30.290 - } 30.291 -}
31.1 --- a/test/src/jdk/nashorn/api/scripting/ScopeTest.java Wed Apr 01 13:22:52 2015 -0700 31.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 31.3 @@ -1,779 +0,0 @@ 31.4 -/* 31.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 31.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 31.7 - * 31.8 - * This code is free software; you can redistribute it and/or modify it 31.9 - * under the terms of the GNU General Public License version 2 only, as 31.10 - * published by the Free Software Foundation. Oracle designates this 31.11 - * particular file as subject to the "Classpath" exception as provided 31.12 - * by Oracle in the LICENSE file that accompanied this code. 31.13 - * 31.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 31.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 31.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 31.17 - * version 2 for more details (a copy is included in the LICENSE file that 31.18 - * accompanied this code). 31.19 - * 31.20 - * You should have received a copy of the GNU General Public License version 31.21 - * 2 along with this work; if not, write to the Free Software Foundation, 31.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 31.23 - * 31.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 31.25 - * or visit www.oracle.com if you need additional information or have any 31.26 - * questions. 31.27 - */ 31.28 -package jdk.nashorn.api.scripting; 31.29 - 31.30 -import static org.testng.Assert.assertEquals; 31.31 -import static org.testng.Assert.assertNotNull; 31.32 -import static org.testng.Assert.assertTrue; 31.33 -import static org.testng.Assert.fail; 31.34 -import javax.script.Bindings; 31.35 -import javax.script.ScriptContext; 31.36 -import javax.script.ScriptEngine; 31.37 -import javax.script.ScriptEngineManager; 31.38 -import javax.script.ScriptException; 31.39 -import javax.script.SimpleBindings; 31.40 -import javax.script.SimpleScriptContext; 31.41 -import org.testng.Assert; 31.42 -import org.testng.annotations.Test; 31.43 - 31.44 -/** 31.45 - * Tests for jsr223 Bindings "scope" (engine, global scopes) 31.46 - */ 31.47 -@SuppressWarnings("javadoc") 31.48 -public class ScopeTest { 31.49 - 31.50 - @Test 31.51 - public void createBindingsTest() { 31.52 - final ScriptEngineManager m = new ScriptEngineManager(); 31.53 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.54 - final Bindings b = e.createBindings(); 31.55 - b.put("foo", 42.0); 31.56 - Object res = null; 31.57 - try { 31.58 - res = e.eval("foo == 42.0", b); 31.59 - } catch (final ScriptException | NullPointerException se) { 31.60 - se.printStackTrace(); 31.61 - fail(se.getMessage()); 31.62 - } 31.63 - 31.64 - assertEquals(res, Boolean.TRUE); 31.65 - } 31.66 - 31.67 - @Test 31.68 - public void engineScopeTest() { 31.69 - final ScriptEngineManager m = new ScriptEngineManager(); 31.70 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.71 - final Bindings engineScope = e.getBindings(ScriptContext.ENGINE_SCOPE); 31.72 - 31.73 - // check few ECMA standard built-in global properties 31.74 - assertNotNull(engineScope.get("Object")); 31.75 - assertNotNull(engineScope.get("TypeError")); 31.76 - assertNotNull(engineScope.get("eval")); 31.77 - 31.78 - // can access via ScriptEngine.get as well 31.79 - assertNotNull(e.get("Object")); 31.80 - assertNotNull(e.get("TypeError")); 31.81 - assertNotNull(e.get("eval")); 31.82 - 31.83 - // Access by either way should return same object 31.84 - assertEquals(engineScope.get("Array"), e.get("Array")); 31.85 - assertEquals(engineScope.get("EvalError"), e.get("EvalError")); 31.86 - assertEquals(engineScope.get("undefined"), e.get("undefined")); 31.87 - 31.88 - // try exposing a new variable from scope 31.89 - engineScope.put("myVar", "foo"); 31.90 - try { 31.91 - assertEquals(e.eval("myVar"), "foo"); 31.92 - } catch (final ScriptException se) { 31.93 - se.printStackTrace(); 31.94 - fail(se.getMessage()); 31.95 - } 31.96 - 31.97 - // update "myVar" in script an check the value from scope 31.98 - try { 31.99 - e.eval("myVar = 'nashorn';"); 31.100 - } catch (final ScriptException se) { 31.101 - se.printStackTrace(); 31.102 - fail(se.getMessage()); 31.103 - } 31.104 - 31.105 - // now check modified value from scope and engine 31.106 - assertEquals(engineScope.get("myVar"), "nashorn"); 31.107 - assertEquals(e.get("myVar"), "nashorn"); 31.108 - } 31.109 - 31.110 - @Test 31.111 - public void multiGlobalTest() { 31.112 - final ScriptEngineManager m = new ScriptEngineManager(); 31.113 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.114 - final Bindings b = e.createBindings(); 31.115 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.116 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.117 - 31.118 - try { 31.119 - final Object obj1 = e.eval("Object"); 31.120 - final Object obj2 = e.eval("Object", newCtxt); 31.121 - Assert.assertNotEquals(obj1, obj2); 31.122 - Assert.assertNotNull(obj1); 31.123 - Assert.assertNotNull(obj2); 31.124 - Assert.assertEquals(obj1.toString(), obj2.toString()); 31.125 - 31.126 - e.eval("x = 'hello'"); 31.127 - e.eval("x = 'world'", newCtxt); 31.128 - Object x1 = e.getContext().getAttribute("x"); 31.129 - Object x2 = newCtxt.getAttribute("x"); 31.130 - Assert.assertNotEquals(x1, x2); 31.131 - Assert.assertEquals(x1, "hello"); 31.132 - Assert.assertEquals(x2, "world"); 31.133 - 31.134 - x1 = e.eval("x"); 31.135 - x2 = e.eval("x", newCtxt); 31.136 - Assert.assertNotEquals(x1, x2); 31.137 - Assert.assertEquals(x1, "hello"); 31.138 - Assert.assertEquals(x2, "world"); 31.139 - 31.140 - final ScriptContext origCtxt = e.getContext(); 31.141 - e.setContext(newCtxt); 31.142 - e.eval("y = new Object()"); 31.143 - e.eval("y = new Object()", origCtxt); 31.144 - 31.145 - final Object y1 = origCtxt.getAttribute("y"); 31.146 - final Object y2 = newCtxt.getAttribute("y"); 31.147 - Assert.assertNotEquals(y1, y2); 31.148 - final Object yeval1 = e.eval("y"); 31.149 - final Object yeval2 = e.eval("y", origCtxt); 31.150 - Assert.assertNotEquals(yeval1, yeval2); 31.151 - Assert.assertEquals("[object Object]", y1.toString()); 31.152 - Assert.assertEquals("[object Object]", y2.toString()); 31.153 - } catch (final ScriptException se) { 31.154 - se.printStackTrace(); 31.155 - fail(se.getMessage()); 31.156 - } 31.157 - } 31.158 - 31.159 - @Test 31.160 - public void userEngineScopeBindingsTest() throws ScriptException { 31.161 - final ScriptEngineManager m = new ScriptEngineManager(); 31.162 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.163 - e.eval("function func() {}"); 31.164 - 31.165 - final ScriptContext newContext = new SimpleScriptContext(); 31.166 - newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 31.167 - // we are using a new bindings - so it should have 'func' defined 31.168 - final Object value = e.eval("typeof func", newContext); 31.169 - assertTrue(value.equals("undefined")); 31.170 - } 31.171 - 31.172 - @Test 31.173 - public void userEngineScopeBindingsNoLeakTest() throws ScriptException { 31.174 - final ScriptEngineManager m = new ScriptEngineManager(); 31.175 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.176 - final ScriptContext newContext = new SimpleScriptContext(); 31.177 - newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 31.178 - e.eval("function foo() {}", newContext); 31.179 - 31.180 - // in the default context's ENGINE_SCOPE, 'foo' shouldn't exist 31.181 - assertTrue(e.eval("typeof foo").equals("undefined")); 31.182 - } 31.183 - 31.184 - @Test 31.185 - public void userEngineScopeBindingsRetentionTest() throws ScriptException { 31.186 - final ScriptEngineManager m = new ScriptEngineManager(); 31.187 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.188 - final ScriptContext newContext = new SimpleScriptContext(); 31.189 - newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 31.190 - e.eval("function foo() {}", newContext); 31.191 - 31.192 - // definition retained with user's ENGINE_SCOPE Binding 31.193 - assertTrue(e.eval("typeof foo", newContext).equals("function")); 31.194 - 31.195 - final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE); 31.196 - // but not in another ENGINE_SCOPE binding 31.197 - newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 31.198 - assertTrue(e.eval("typeof foo", newContext).equals("undefined")); 31.199 - 31.200 - // restore ENGINE_SCOPE and check again 31.201 - newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE); 31.202 - assertTrue(e.eval("typeof foo", newContext).equals("function")); 31.203 - } 31.204 - 31.205 - @Test 31.206 - // check that engine.js definitions are visible in all new global instances 31.207 - public void checkBuiltinsInNewBindingsTest() throws ScriptException { 31.208 - final ScriptEngineManager m = new ScriptEngineManager(); 31.209 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.210 - 31.211 - // check default global instance has engine.js definitions 31.212 - final Bindings g = (Bindings) e.eval("this"); 31.213 - Object value = g.get("__noSuchProperty__"); 31.214 - assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 31.215 - value = g.get("print"); 31.216 - assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 31.217 - 31.218 - // check new global instance created has engine.js definitions 31.219 - final Bindings b = e.createBindings(); 31.220 - value = b.get("__noSuchProperty__"); 31.221 - assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 31.222 - value = b.get("print"); 31.223 - assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 31.224 - 31.225 - // put a mapping into GLOBAL_SCOPE 31.226 - final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); 31.227 - globalScope.put("x", "hello"); 31.228 - 31.229 - // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval 31.230 - assertTrue(e.eval("x").equals("hello")); 31.231 - 31.232 - final ScriptContext ctx = new SimpleScriptContext(); 31.233 - ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); 31.234 - ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.235 - 31.236 - // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 31.237 - assertTrue(e.eval("x", ctx).equals("hello")); 31.238 - 31.239 - // try some arbitray Bindings for ENGINE_SCOPE 31.240 - final Bindings sb = new SimpleBindings(); 31.241 - ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); 31.242 - 31.243 - // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 31.244 - assertTrue(e.eval("x", ctx).equals("hello")); 31.245 - 31.246 - // engine.js builtins are still defined even with arbitrary Bindings 31.247 - assertTrue(e.eval("typeof print", ctx).equals("function")); 31.248 - assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); 31.249 - 31.250 - // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition 31.251 - sb.put("x", "newX"); 31.252 - assertTrue(e.eval("x", ctx).equals("newX")); 31.253 - } 31.254 - 31.255 - /** 31.256 - * Test multi-threaded access to defined global variables for shared script classes with multiple globals. 31.257 - */ 31.258 - @Test 31.259 - public static void multiThreadedVarTest() throws ScriptException, InterruptedException { 31.260 - final ScriptEngineManager m = new ScriptEngineManager(); 31.261 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.262 - final Bindings b = e.createBindings(); 31.263 - final ScriptContext origContext = e.getContext(); 31.264 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.265 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.266 - final String sharedScript = "foo"; 31.267 - 31.268 - assertEquals(e.eval("var foo = 'original context';", origContext), null); 31.269 - assertEquals(e.eval("var foo = 'new context';", newCtxt), null); 31.270 - 31.271 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.272 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 31.273 - t1.start(); 31.274 - t2.start(); 31.275 - t1.join(); 31.276 - t2.join(); 31.277 - 31.278 - assertEquals(e.eval("var foo = 'newer context';", newCtxt), null); 31.279 - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.280 - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 31.281 - 31.282 - t3.start(); 31.283 - t4.start(); 31.284 - t3.join(); 31.285 - t4.join(); 31.286 - 31.287 - assertEquals(e.eval(sharedScript), "original context"); 31.288 - assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 31.289 - } 31.290 - 31.291 - /** 31.292 - * Test multi-threaded access to undefined global variables for shared script classes with multiple globals. 31.293 - */ 31.294 - @Test 31.295 - public static void multiThreadedGlobalTest() throws ScriptException, InterruptedException { 31.296 - final ScriptEngineManager m = new ScriptEngineManager(); 31.297 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.298 - final Bindings b = e.createBindings(); 31.299 - final ScriptContext origContext = e.getContext(); 31.300 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.301 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.302 - 31.303 - assertEquals(e.eval("foo = 'original context';", origContext), "original context"); 31.304 - assertEquals(e.eval("foo = 'new context';", newCtxt), "new context"); 31.305 - final String sharedScript = "foo"; 31.306 - 31.307 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.308 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 31.309 - t1.start(); 31.310 - t2.start(); 31.311 - t1.join(); 31.312 - t2.join(); 31.313 - 31.314 - final Object obj3 = e.eval("delete foo; foo = 'newer context';", newCtxt); 31.315 - assertEquals(obj3, "newer context"); 31.316 - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.317 - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 31.318 - 31.319 - t3.start(); 31.320 - t4.start(); 31.321 - t3.join(); 31.322 - t4.join(); 31.323 - 31.324 - Assert.assertEquals(e.eval(sharedScript), "original context"); 31.325 - Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 31.326 - } 31.327 - 31.328 - /** 31.329 - * Test multi-threaded access using the postfix ++ operator for shared script classes with multiple globals. 31.330 - */ 31.331 - @Test 31.332 - public static void multiThreadedIncTest() throws ScriptException, InterruptedException { 31.333 - final ScriptEngineManager m = new ScriptEngineManager(); 31.334 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.335 - final Bindings b = e.createBindings(); 31.336 - final ScriptContext origContext = e.getContext(); 31.337 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.338 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.339 - 31.340 - assertEquals(e.eval("var x = 0;", origContext), null); 31.341 - assertEquals(e.eval("var x = 2;", newCtxt), null); 31.342 - final String sharedScript = "x++;"; 31.343 - 31.344 - final Thread t1 = new Thread(new Runnable() { 31.345 - @Override 31.346 - public void run() { 31.347 - try { 31.348 - for (int i = 0; i < 1000; i++) { 31.349 - assertEquals(e.eval(sharedScript, origContext), (double)i); 31.350 - } 31.351 - } catch (final ScriptException se) { 31.352 - fail(se.toString()); 31.353 - } 31.354 - } 31.355 - }); 31.356 - final Thread t2 = new Thread(new Runnable() { 31.357 - @Override 31.358 - public void run() { 31.359 - try { 31.360 - for (int i = 2; i < 1000; i++) { 31.361 - assertEquals(e.eval(sharedScript, newCtxt), (double)i); 31.362 - } 31.363 - } catch (final ScriptException se) { 31.364 - fail(se.toString()); 31.365 - } 31.366 - } 31.367 - }); 31.368 - t1.start(); 31.369 - t2.start(); 31.370 - t1.join(); 31.371 - t2.join(); 31.372 - } 31.373 - 31.374 - /** 31.375 - * Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals. 31.376 - */ 31.377 - @Test 31.378 - public static void multiThreadedPrimitiveTest() throws ScriptException, InterruptedException { 31.379 - final ScriptEngineManager m = new ScriptEngineManager(); 31.380 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.381 - final Bindings b = e.createBindings(); 31.382 - final ScriptContext origContext = e.getContext(); 31.383 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.384 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.385 - 31.386 - final Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext); 31.387 - final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt); 31.388 - assertEquals(obj1, "original context"); 31.389 - assertEquals(obj2, "new context"); 31.390 - final String sharedScript = "''.foo"; 31.391 - 31.392 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.393 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 31.394 - t1.start(); 31.395 - t2.start(); 31.396 - t1.join(); 31.397 - t2.join(); 31.398 - 31.399 - final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); 31.400 - assertEquals(obj3, "newer context"); 31.401 - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.402 - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 31.403 - 31.404 - t3.start(); 31.405 - t4.start(); 31.406 - t3.join(); 31.407 - t4.join(); 31.408 - 31.409 - Assert.assertEquals(e.eval(sharedScript), "original context"); 31.410 - Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 31.411 - } 31.412 - 31.413 - 31.414 - /** 31.415 - * Test multi-threaded access to prototype user accessor properties for shared script classes with multiple globals. 31.416 - */ 31.417 - @Test 31.418 - public static void multiThreadedAccessorTest() throws ScriptException, InterruptedException { 31.419 - final ScriptEngineManager m = new ScriptEngineManager(); 31.420 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.421 - final Bindings b = e.createBindings(); 31.422 - final ScriptContext origContext = e.getContext(); 31.423 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.424 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.425 - 31.426 - e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'original context' })", origContext); 31.427 - e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'new context', configurable: true })", newCtxt); 31.428 - final String sharedScript = "({}).foo"; 31.429 - 31.430 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.431 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 31.432 - t1.start(); 31.433 - t2.start(); 31.434 - t1.join(); 31.435 - t2.join(); 31.436 - 31.437 - final Object obj3 = e.eval("delete Object.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); 31.438 - assertEquals(obj3, "newer context"); 31.439 - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.440 - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 31.441 - 31.442 - t3.start(); 31.443 - t4.start(); 31.444 - t3.join(); 31.445 - t4.join(); 31.446 - } 31.447 - 31.448 - /** 31.449 - * Test multi-threaded access to primitive prototype user accessor properties for shared script classes with multiple globals. 31.450 - */ 31.451 - @Test 31.452 - public static void multiThreadedPrimitiveAccessorTest() throws ScriptException, InterruptedException { 31.453 - final ScriptEngineManager m = new ScriptEngineManager(); 31.454 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.455 - final Bindings b = e.createBindings(); 31.456 - final ScriptContext origContext = e.getContext(); 31.457 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.458 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.459 - 31.460 - e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'original context' })", origContext); 31.461 - e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'new context' })", newCtxt); 31.462 - final String sharedScript = "''.foo"; 31.463 - 31.464 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.465 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 31.466 - t1.start(); 31.467 - t2.start(); 31.468 - t1.join(); 31.469 - t2.join(); 31.470 - 31.471 - final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); 31.472 - assertEquals(obj3, "newer context"); 31.473 - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 31.474 - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 31.475 - 31.476 - t3.start(); 31.477 - t4.start(); 31.478 - t3.join(); 31.479 - t4.join(); 31.480 - } 31.481 - 31.482 - /** 31.483 - * Test multi-threaded scope function invocation for shared script classes with multiple globals. 31.484 - */ 31.485 - @Test 31.486 - public static void multiThreadedFunctionTest() throws ScriptException, InterruptedException { 31.487 - final ScriptEngineManager m = new ScriptEngineManager(); 31.488 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.489 - final Bindings b = e.createBindings(); 31.490 - final ScriptContext origContext = e.getContext(); 31.491 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.492 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.493 - 31.494 - e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext); 31.495 - assertEquals(origContext.getAttribute("scopeVar"), 1); 31.496 - assertEquals(e.eval("scopeTest()"), 1); 31.497 - 31.498 - e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt); 31.499 - assertEquals(newCtxt.getAttribute("scopeVar"), 1); 31.500 - assertEquals(e.eval("scopeTest();", newCtxt), 1); 31.501 - 31.502 - assertEquals(e.eval("scopeVar = 3;", newCtxt), 3); 31.503 - assertEquals(newCtxt.getAttribute("scopeVar"), 3); 31.504 - 31.505 - 31.506 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000)); 31.507 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000)); 31.508 - 31.509 - t1.start(); 31.510 - t2.start(); 31.511 - t1.join(); 31.512 - t2.join(); 31.513 - 31.514 - } 31.515 - 31.516 - /** 31.517 - * Test multi-threaded access to global getters and setters for shared script classes with multiple globals. 31.518 - */ 31.519 - @Test 31.520 - public static void getterSetterTest() throws ScriptException, InterruptedException { 31.521 - final ScriptEngineManager m = new ScriptEngineManager(); 31.522 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.523 - final Bindings b = e.createBindings(); 31.524 - final ScriptContext origContext = e.getContext(); 31.525 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.526 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.527 - final String sharedScript = "accessor1"; 31.528 - 31.529 - e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); 31.530 - assertEquals(e.eval("accessor1 = 1;"), 1); 31.531 - assertEquals(e.eval(sharedScript), 1); 31.532 - 31.533 - e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); 31.534 - assertEquals(e.eval("accessor1 = 2;", newCtxt), 2); 31.535 - assertEquals(e.eval(sharedScript, newCtxt), 2); 31.536 - 31.537 - 31.538 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); 31.539 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); 31.540 - 31.541 - t1.start(); 31.542 - t2.start(); 31.543 - t1.join(); 31.544 - t2.join(); 31.545 - 31.546 - assertEquals(e.eval(sharedScript), 1); 31.547 - assertEquals(e.eval(sharedScript, newCtxt), 2); 31.548 - assertEquals(e.eval("v"), 1); 31.549 - assertEquals(e.eval("v", newCtxt), 2); 31.550 - } 31.551 - 31.552 - /** 31.553 - * Test multi-threaded access to global getters and setters for shared script classes with multiple globals. 31.554 - */ 31.555 - @Test 31.556 - public static void getterSetter2Test() throws ScriptException, InterruptedException { 31.557 - final ScriptEngineManager m = new ScriptEngineManager(); 31.558 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.559 - final Bindings b = e.createBindings(); 31.560 - final ScriptContext origContext = e.getContext(); 31.561 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.562 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.563 - final String sharedScript = "accessor2"; 31.564 - 31.565 - e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); 31.566 - assertEquals(e.eval("accessor2 = 1;"), 1); 31.567 - assertEquals(e.eval(sharedScript), 1); 31.568 - 31.569 - e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); 31.570 - assertEquals(e.eval("accessor2 = 2;", newCtxt), 2); 31.571 - assertEquals(e.eval(sharedScript, newCtxt), 2); 31.572 - 31.573 - 31.574 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); 31.575 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); 31.576 - 31.577 - t1.start(); 31.578 - t2.start(); 31.579 - t1.join(); 31.580 - t2.join(); 31.581 - 31.582 - assertEquals(e.eval(sharedScript), 1); 31.583 - assertEquals(e.eval(sharedScript, newCtxt), 2); 31.584 - assertEquals(e.eval("x"), 1); 31.585 - assertEquals(e.eval("x", newCtxt), 2); 31.586 - } 31.587 - 31.588 - // @bug 8058422: Users should be able to overwrite "context" and "engine" variables 31.589 - @Test 31.590 - public static void contextOverwriteTest() throws ScriptException { 31.591 - final ScriptEngineManager m = new ScriptEngineManager(); 31.592 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.593 - final Bindings b = new SimpleBindings(); 31.594 - b.put("context", "hello"); 31.595 - b.put("foo", 32); 31.596 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.597 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.598 - e.setContext(newCtxt); 31.599 - assertEquals(e.eval("context"), "hello"); 31.600 - assertEquals(((Number)e.eval("foo")).intValue(), 32); 31.601 - } 31.602 - 31.603 - // @bug 8058422: Users should be able to overwrite "context" and "engine" variables 31.604 - @Test 31.605 - public static void contextOverwriteInScriptTest() throws ScriptException { 31.606 - final ScriptEngineManager m = new ScriptEngineManager(); 31.607 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.608 - e.put("foo", 32); 31.609 - 31.610 - assertEquals(((Number)e.eval("foo")).intValue(), 32); 31.611 - assertEquals(e.eval("context = 'bar'"), "bar"); 31.612 - assertEquals(((Number)e.eval("foo")).intValue(), 32); 31.613 - } 31.614 - 31.615 - // @bug 8058422: Users should be able to overwrite "context" and "engine" variables 31.616 - @Test 31.617 - public static void engineOverwriteTest() throws ScriptException { 31.618 - final ScriptEngineManager m = new ScriptEngineManager(); 31.619 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.620 - final Bindings b = new SimpleBindings(); 31.621 - b.put("engine", "hello"); 31.622 - b.put("foo", 32); 31.623 - final ScriptContext newCtxt = new SimpleScriptContext(); 31.624 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.625 - e.setContext(newCtxt); 31.626 - assertEquals(e.eval("engine"), "hello"); 31.627 - assertEquals(((Number)e.eval("foo")).intValue(), 32); 31.628 - } 31.629 - 31.630 - // @bug 8058422: Users should be able to overwrite "context" and "engine" variables 31.631 - @Test 31.632 - public static void engineOverwriteInScriptTest() throws ScriptException { 31.633 - final ScriptEngineManager m = new ScriptEngineManager(); 31.634 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.635 - e.put("foo", 32); 31.636 - 31.637 - assertEquals(((Number)e.eval("foo")).intValue(), 32); 31.638 - assertEquals(e.eval("engine = 'bar'"), "bar"); 31.639 - assertEquals(((Number)e.eval("foo")).intValue(), 32); 31.640 - } 31.641 - 31.642 - // @bug 8044750: megamorphic getter for scope objects does not call __noSuchProperty__ hook 31.643 - @Test 31.644 - public static void testMegamorphicGetInGlobal() throws Exception { 31.645 - final ScriptEngineManager m = new ScriptEngineManager(); 31.646 - final ScriptEngine engine = m.getEngineByName("nashorn"); 31.647 - final String script = "foo"; 31.648 - // "foo" is megamorphic because of different global scopes. 31.649 - // Make sure ScriptContext variable search works even after 31.650 - // it becomes megamorphic. 31.651 - for (int index = 0; index < 25; index++) { 31.652 - final Bindings bindings = new SimpleBindings(); 31.653 - bindings.put("foo", index); 31.654 - final Number value = (Number)engine.eval(script, bindings); 31.655 - assertEquals(index, value.intValue()); 31.656 - } 31.657 - } 31.658 - 31.659 - /** 31.660 - * Test "slow" scopes involving {@code with} and {@code eval} statements for shared script classes with multiple globals. 31.661 - * @throws ScriptException 31.662 - * @throws InterruptedException 31.663 - */ 31.664 - @Test 31.665 - public static void testSlowScope() throws ScriptException, InterruptedException { 31.666 - final ScriptEngineManager m = new ScriptEngineManager(); 31.667 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.668 - 31.669 - for (int i = 0; i < 100; i++) { 31.670 - final Bindings b = e.createBindings(); 31.671 - final ScriptContext ctxt = new SimpleScriptContext(); 31.672 - ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 31.673 - 31.674 - e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt); 31.675 - assertEquals(e.eval("a", ctxt), 1); 31.676 - assertEquals(b.get("a"), 1); 31.677 - assertEquals(e.eval("b", ctxt), 3); 31.678 - assertEquals(b.get("b"), 3); 31.679 - assertEquals(e.eval("c", ctxt), 10); 31.680 - assertEquals(b.get("c"), 10); 31.681 - } 31.682 - } 31.683 - 31.684 - private static class ScriptRunner implements Runnable { 31.685 - 31.686 - final ScriptEngine engine; 31.687 - final ScriptContext context; 31.688 - final String source; 31.689 - final Object expected; 31.690 - final int iterations; 31.691 - 31.692 - ScriptRunner(final ScriptEngine engine, final ScriptContext context, final String source, final Object expected, final int iterations) { 31.693 - this.engine = engine; 31.694 - this.context = context; 31.695 - this.source = source; 31.696 - this.expected = expected; 31.697 - this.iterations = iterations; 31.698 - } 31.699 - 31.700 - @Override 31.701 - public void run() { 31.702 - try { 31.703 - for (int i = 0; i < iterations; i++) { 31.704 - assertEquals(engine.eval(source, context), expected); 31.705 - } 31.706 - } catch (final ScriptException se) { 31.707 - throw new RuntimeException(se); 31.708 - } 31.709 - } 31.710 - } 31.711 - 31.712 - // @bug 8071678: NashornScriptEngine returns javax.script.ScriptContext instance 31.713 - // with get/setAttribute methods insonsistent for GLOBAL_SCOPE 31.714 - @Test 31.715 - public void testGlobalScopeSearch() throws Exception { 31.716 - final ScriptEngineManager m = new ScriptEngineManager(); 31.717 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.718 - final ScriptContext c = e.getContext(); 31.719 - c.setAttribute("name1234", "value", ScriptContext.GLOBAL_SCOPE); 31.720 - assertEquals(c.getAttribute("name1234"), "value"); 31.721 - assertEquals(c.getAttributesScope("name1234"), 31.722 - ScriptContext.GLOBAL_SCOPE); 31.723 - } 31.724 - 31.725 - // @bug 8071594: NashornScriptEngine returns javax.script.ScriptContext instance 31.726 - // which doesn't completely conform to the spec regarding exceptions throwing 31.727 - @Test 31.728 - public void testScriptContext_NPE_IAE() throws Exception { 31.729 - final ScriptEngineManager m = new ScriptEngineManager(); 31.730 - final ScriptEngine e = m.getEngineByName("nashorn"); 31.731 - final ScriptContext c = e.getContext(); 31.732 - try { 31.733 - c.getAttribute(""); 31.734 - throw new AssertionError("should have thrown IAE"); 31.735 - } catch (IllegalArgumentException iae1) {} 31.736 - 31.737 - try { 31.738 - c.getAttribute(null); 31.739 - throw new AssertionError("should have thrown NPE"); 31.740 - } catch (NullPointerException npe1) {} 31.741 - 31.742 - try { 31.743 - c.getAttribute("", ScriptContext.ENGINE_SCOPE); 31.744 - throw new AssertionError("should have thrown IAE"); 31.745 - } catch (IllegalArgumentException iae2) {} 31.746 - 31.747 - try { 31.748 - c.getAttribute(null, ScriptContext.ENGINE_SCOPE); 31.749 - throw new AssertionError("should have thrown NPE"); 31.750 - } catch (NullPointerException npe2) {} 31.751 - 31.752 - try { 31.753 - c.removeAttribute("", ScriptContext.ENGINE_SCOPE); 31.754 - throw new AssertionError("should have thrown IAE"); 31.755 - } catch (IllegalArgumentException iae3) {} 31.756 - 31.757 - try { 31.758 - c.removeAttribute(null, ScriptContext.ENGINE_SCOPE); 31.759 - throw new AssertionError("should have thrown NPE"); 31.760 - } catch (NullPointerException npe3) {} 31.761 - 31.762 - try { 31.763 - c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE); 31.764 - throw new AssertionError("should have thrown IAE"); 31.765 - } catch (IllegalArgumentException iae4) {} 31.766 - 31.767 - try { 31.768 - c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE); 31.769 - throw new AssertionError("should have thrown NPE"); 31.770 - } catch (NullPointerException npe4) {} 31.771 - 31.772 - try { 31.773 - c.getAttributesScope(""); 31.774 - throw new AssertionError("should have thrown IAE"); 31.775 - } catch (IllegalArgumentException iae5) {} 31.776 - 31.777 - try { 31.778 - c.getAttributesScope(null); 31.779 - throw new AssertionError("should have thrown NPE"); 31.780 - } catch (NullPointerException npe5) {} 31.781 - } 31.782 -}
32.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptEngineSecurityTest.java Wed Apr 01 13:22:52 2015 -0700 32.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 32.3 @@ -1,311 +0,0 @@ 32.4 -/* 32.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 32.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 32.7 - * 32.8 - * This code is free software; you can redistribute it and/or modify it 32.9 - * under the terms of the GNU General Public License version 2 only, as 32.10 - * published by the Free Software Foundation. Oracle designates this 32.11 - * particular file as subject to the "Classpath" exception as provided 32.12 - * by Oracle in the LICENSE file that accompanied this code. 32.13 - * 32.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 32.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 32.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 32.17 - * version 2 for more details (a copy is included in the LICENSE file that 32.18 - * accompanied this code). 32.19 - * 32.20 - * You should have received a copy of the GNU General Public License version 32.21 - * 2 along with this work; if not, write to the Free Software Foundation, 32.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 32.23 - * 32.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 32.25 - * or visit www.oracle.com if you need additional information or have any 32.26 - * questions. 32.27 - */ 32.28 - 32.29 -package jdk.nashorn.api.scripting; 32.30 - 32.31 -import static org.testng.Assert.fail; 32.32 -import java.lang.reflect.InvocationHandler; 32.33 -import java.lang.reflect.Method; 32.34 -import java.lang.reflect.Proxy; 32.35 -import javax.script.ScriptEngine; 32.36 -import javax.script.ScriptEngineManager; 32.37 -import javax.script.ScriptException; 32.38 -import org.testng.annotations.Test; 32.39 - 32.40 -/** 32.41 - * jsr223 tests for security access checks. 32.42 - */ 32.43 -@SuppressWarnings("javadoc") 32.44 -public class ScriptEngineSecurityTest { 32.45 - 32.46 - private static void log(final String msg) { 32.47 - org.testng.Reporter.log(msg, true); 32.48 - } 32.49 - 32.50 - @Test 32.51 - public void securityPackagesTest() { 32.52 - if (System.getSecurityManager() == null) { 32.53 - // pass vacuously 32.54 - return; 32.55 - } 32.56 - 32.57 - final ScriptEngineManager m = new ScriptEngineManager(); 32.58 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.59 - try { 32.60 - e.eval("var v = Packages.sun.misc.Unsafe;"); 32.61 - fail("should have thrown SecurityException"); 32.62 - } catch (final Exception exp) { 32.63 - if (exp instanceof SecurityException) { 32.64 - log("got " + exp + " as expected"); 32.65 - } else { 32.66 - fail(exp.getMessage()); 32.67 - } 32.68 - } 32.69 - } 32.70 - 32.71 - @Test 32.72 - public void securityJavaTypeTest() { 32.73 - if (System.getSecurityManager() == null) { 32.74 - // pass vacuously 32.75 - return; 32.76 - } 32.77 - 32.78 - final ScriptEngineManager m = new ScriptEngineManager(); 32.79 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.80 - try { 32.81 - e.eval("var v = Java.type('sun.misc.Unsafe');"); 32.82 - fail("should have thrown SecurityException"); 32.83 - } catch (final Exception exp) { 32.84 - if (exp instanceof SecurityException) { 32.85 - log("got " + exp + " as expected"); 32.86 - } else { 32.87 - fail(exp.getMessage()); 32.88 - } 32.89 - } 32.90 - } 32.91 - 32.92 - @Test 32.93 - public void securityClassForNameTest() { 32.94 - if (System.getSecurityManager() == null) { 32.95 - // pass vacuously 32.96 - return; 32.97 - } 32.98 - 32.99 - final ScriptEngineManager m = new ScriptEngineManager(); 32.100 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.101 - try { 32.102 - e.eval("var v = java.lang.Class.forName('sun.misc.Unsafe');"); 32.103 - fail("should have thrown SecurityException"); 32.104 - } catch (final Exception exp) { 32.105 - if (exp instanceof SecurityException) { 32.106 - log("got " + exp + " as expected"); 32.107 - } else { 32.108 - fail(exp.getMessage()); 32.109 - } 32.110 - } 32.111 - } 32.112 - 32.113 - @Test 32.114 - public void securitySystemExit() { 32.115 - if (System.getSecurityManager() == null) { 32.116 - // pass vacuously 32.117 - return; 32.118 - } 32.119 - 32.120 - final ScriptEngineManager m = new ScriptEngineManager(); 32.121 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.122 - try { 32.123 - e.eval("java.lang.System.exit(0);"); 32.124 - fail("should have thrown SecurityException"); 32.125 - } catch (final Exception exp) { 32.126 - if (exp instanceof SecurityException) { 32.127 - log("got " + exp + " as expected"); 32.128 - } else { 32.129 - fail(exp.getMessage()); 32.130 - } 32.131 - } 32.132 - } 32.133 - 32.134 - 32.135 - @Test 32.136 - public void securitySystemExitFromFinalizerThread() throws ScriptException { 32.137 - if (System.getSecurityManager() == null) { 32.138 - // pass vacuously 32.139 - return; 32.140 - } 32.141 - 32.142 - final ScriptEngineManager m = new ScriptEngineManager(); 32.143 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.144 - e.eval("var o = Java.extend(Java.type('javax.imageio.spi.ServiceRegistry'), { deregisterAll: this.exit.bind(null, 1234)});\n" + 32.145 - "new o(new java.util.ArrayList().iterator())"); 32.146 - System.gc(); 32.147 - System.runFinalization(); 32.148 - // NOTE: this test just exits the VM if it fails. 32.149 - } 32.150 - 32.151 - @Test 32.152 - public void securitySystemLoadLibrary() { 32.153 - if (System.getSecurityManager() == null) { 32.154 - // pass vacuously 32.155 - return; 32.156 - } 32.157 - 32.158 - final ScriptEngineManager m = new ScriptEngineManager(); 32.159 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.160 - try { 32.161 - e.eval("java.lang.System.loadLibrary('foo');"); 32.162 - fail("should have thrown SecurityException"); 32.163 - } catch (final Exception exp) { 32.164 - if (exp instanceof SecurityException) { 32.165 - log("got " + exp + " as expected"); 32.166 - } else { 32.167 - fail(exp.getMessage()); 32.168 - } 32.169 - } 32.170 - } 32.171 - 32.172 - // @bug 8032948: Nashorn linkages awry 32.173 - @SuppressWarnings("serial") 32.174 - public static class FakeProxy extends Proxy { 32.175 - public FakeProxy(final InvocationHandler ih) { 32.176 - super(ih); 32.177 - } 32.178 - 32.179 - public static Class<?> makeProxyClass(final ClassLoader cl, final Class<?>... ifaces) { 32.180 - return Proxy.getProxyClass(cl, ifaces); 32.181 - } 32.182 - } 32.183 - 32.184 - @Test 32.185 - public void fakeProxySubclassAccessCheckTest() { 32.186 - if (System.getSecurityManager() == null) { 32.187 - // pass vacuously 32.188 - return; 32.189 - } 32.190 - 32.191 - final ScriptEngineManager m = new ScriptEngineManager(); 32.192 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.193 - 32.194 - e.put("name", ScriptEngineSecurityTest.class.getName()); 32.195 - e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); 32.196 - e.put("intfs", new Class[] { Runnable.class }); 32.197 - 32.198 - final String getClass = "Java.type(name + '$FakeProxy').getProxyClass(cl, intfs);"; 32.199 - 32.200 - // Should not be able to call static methods of Proxy via fake subclass 32.201 - try { 32.202 - e.eval(getClass); 32.203 - fail("should have thrown SecurityException"); 32.204 - } catch (final Exception exp) { 32.205 - if (! (exp instanceof SecurityException)) { 32.206 - fail("SecurityException expected, got " + exp); 32.207 - } 32.208 - } 32.209 - } 32.210 - 32.211 - @Test 32.212 - public void fakeProxySubclassAccessCheckTest2() { 32.213 - if (System.getSecurityManager() == null) { 32.214 - // pass vacuously 32.215 - return; 32.216 - } 32.217 - 32.218 - final ScriptEngineManager m = new ScriptEngineManager(); 32.219 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.220 - 32.221 - e.put("name", ScriptEngineSecurityTest.class.getName()); 32.222 - e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); 32.223 - e.put("intfs", new Class[] { Runnable.class }); 32.224 - 32.225 - final String getClass = "Java.type(name + '$FakeProxy').makeProxyClass(cl, intfs);"; 32.226 - 32.227 - // Should not be able to call static methods of Proxy via fake subclass 32.228 - try { 32.229 - e.eval(getClass); 32.230 - fail("should have thrown SecurityException"); 32.231 - } catch (final Exception exp) { 32.232 - if (! (exp instanceof SecurityException)) { 32.233 - fail("SecurityException expected, got " + exp); 32.234 - } 32.235 - } 32.236 - } 32.237 - 32.238 - @Test 32.239 - public static void proxyStaticAccessCheckTest() { 32.240 - if (System.getSecurityManager() == null) { 32.241 - // pass vacuously 32.242 - return; 32.243 - } 32.244 - 32.245 - final ScriptEngineManager m = new ScriptEngineManager(); 32.246 - final ScriptEngine e = m.getEngineByName("nashorn"); 32.247 - final Runnable r = (Runnable)Proxy.newProxyInstance( 32.248 - ScriptEngineTest.class.getClassLoader(), 32.249 - new Class[] { Runnable.class }, 32.250 - new InvocationHandler() { 32.251 - @Override 32.252 - public Object invoke(final Object p, final Method mtd, final Object[] a) { 32.253 - return null; 32.254 - } 32.255 - }); 32.256 - 32.257 - e.put("rc", r.getClass()); 32.258 - e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); 32.259 - e.put("intfs", new Class[] { Runnable.class }); 32.260 - 32.261 - // make sure static methods of Proxy is not accessible via subclass 32.262 - try { 32.263 - e.eval("rc.static.getProxyClass(cl, intfs)"); 32.264 - fail("Should have thrown SecurityException"); 32.265 - } catch (final Exception exp) { 32.266 - if (! (exp instanceof SecurityException)) { 32.267 - fail("SecurityException expected, got " + exp); 32.268 - } 32.269 - } 32.270 - } 32.271 - 32.272 - 32.273 - @Test 32.274 - public void nashornConfigSecurityTest() { 32.275 - if (System.getSecurityManager() == null) { 32.276 - // pass vacuously 32.277 - return; 32.278 - } 32.279 - 32.280 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 32.281 - try { 32.282 - fac.getScriptEngine(new ClassFilter() { 32.283 - @Override 32.284 - public boolean exposeToScripts(final String name) { 32.285 - return true; 32.286 - } 32.287 - }); 32.288 - fail("SecurityException should have been thrown"); 32.289 - } catch (final SecurityException e) { 32.290 - //empty 32.291 - } 32.292 - } 32.293 - 32.294 - @Test 32.295 - public void nashornConfigSecurityTest2() { 32.296 - if (System.getSecurityManager() == null) { 32.297 - // pass vacuously 32.298 - return; 32.299 - } 32.300 - 32.301 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 32.302 - try { 32.303 - fac.getScriptEngine(new String[0], null, new ClassFilter() { 32.304 - @Override 32.305 - public boolean exposeToScripts(final String name) { 32.306 - return true; 32.307 - } 32.308 - }); 32.309 - fail("SecurityException should have been thrown"); 32.310 - } catch (final SecurityException e) { 32.311 - //empty 32.312 - } 32.313 - } 32.314 -}
33.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Wed Apr 01 13:22:52 2015 -0700 33.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 33.3 @@ -1,880 +0,0 @@ 33.4 -/* 33.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 33.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 33.7 - * 33.8 - * This code is free software; you can redistribute it and/or modify it 33.9 - * under the terms of the GNU General Public License version 2 only, as 33.10 - * published by the Free Software Foundation. Oracle designates this 33.11 - * particular file as subject to the "Classpath" exception as provided 33.12 - * by Oracle in the LICENSE file that accompanied this code. 33.13 - * 33.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 33.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 33.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 33.17 - * version 2 for more details (a copy is included in the LICENSE file that 33.18 - * accompanied this code). 33.19 - * 33.20 - * You should have received a copy of the GNU General Public License version 33.21 - * 2 along with this work; if not, write to the Free Software Foundation, 33.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 33.23 - * 33.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 33.25 - * or visit www.oracle.com if you need additional information or have any 33.26 - * questions. 33.27 - */ 33.28 - 33.29 -package jdk.nashorn.api.scripting; 33.30 - 33.31 -import static org.testng.Assert.assertEquals; 33.32 -import static org.testng.Assert.assertNotNull; 33.33 -import static org.testng.Assert.assertNull; 33.34 -import static org.testng.Assert.assertTrue; 33.35 -import static org.testng.Assert.fail; 33.36 - 33.37 -import java.io.StringReader; 33.38 -import java.io.StringWriter; 33.39 -import java.lang.reflect.InvocationHandler; 33.40 -import java.lang.reflect.Method; 33.41 -import java.lang.reflect.Proxy; 33.42 -import java.util.Collections; 33.43 -import java.util.concurrent.Callable; 33.44 -import java.util.concurrent.atomic.AtomicBoolean; 33.45 -import java.util.function.Consumer; 33.46 -import java.util.function.Function; 33.47 -import javax.script.Bindings; 33.48 -import javax.script.Compilable; 33.49 -import javax.script.CompiledScript; 33.50 -import javax.script.Invocable; 33.51 -import javax.script.ScriptContext; 33.52 -import javax.script.ScriptEngine; 33.53 -import javax.script.ScriptEngineFactory; 33.54 -import javax.script.ScriptEngineManager; 33.55 -import javax.script.ScriptException; 33.56 -import javax.script.SimpleScriptContext; 33.57 -import org.testng.annotations.Test; 33.58 - 33.59 -/** 33.60 - * Tests for JSR-223 script engine for Nashorn. 33.61 - * 33.62 - * @test 33.63 - * @build jdk.nashorn.api.scripting.Window jdk.nashorn.api.scripting.WindowEventHandler jdk.nashorn.api.scripting.VariableArityTestInterface jdk.nashorn.api.scripting.ScriptEngineTest 33.64 - * @run testng/othervm jdk.nashorn.api.scripting.ScriptEngineTest 33.65 - */ 33.66 -@SuppressWarnings("javadoc") 33.67 -public class ScriptEngineTest { 33.68 - 33.69 - private static void log(final String msg) { 33.70 - org.testng.Reporter.log(msg, true); 33.71 - } 33.72 - 33.73 - @Test 33.74 - public void argumentsTest() { 33.75 - final ScriptEngineManager m = new ScriptEngineManager(); 33.76 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.77 - 33.78 - final String[] args = new String[] { "hello", "world" }; 33.79 - try { 33.80 - e.put("arguments", args); 33.81 - final Object arg0 = e.eval("arguments[0]"); 33.82 - final Object arg1 = e.eval("arguments[1]"); 33.83 - assertEquals(args[0], arg0); 33.84 - assertEquals(args[1], arg1); 33.85 - } catch (final Exception exp) { 33.86 - exp.printStackTrace(); 33.87 - fail(exp.getMessage()); 33.88 - } 33.89 - } 33.90 - 33.91 - @Test 33.92 - public void argumentsWithTest() { 33.93 - final ScriptEngineManager m = new ScriptEngineManager(); 33.94 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.95 - 33.96 - final String[] args = new String[] { "hello", "world" }; 33.97 - try { 33.98 - e.put("arguments", args); 33.99 - final Object arg0 = e.eval("var imports = new JavaImporter(java.io); " + 33.100 - " with(imports) { arguments[0] }"); 33.101 - final Object arg1 = e.eval("var imports = new JavaImporter(java.util, java.io); " + 33.102 - " with(imports) { arguments[1] }"); 33.103 - assertEquals(args[0], arg0); 33.104 - assertEquals(args[1], arg1); 33.105 - } catch (final Exception exp) { 33.106 - exp.printStackTrace(); 33.107 - fail(exp.getMessage()); 33.108 - } 33.109 - } 33.110 - 33.111 - @Test 33.112 - public void argumentsEmptyTest() { 33.113 - final ScriptEngineManager m = new ScriptEngineManager(); 33.114 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.115 - 33.116 - try { 33.117 - assertEquals(e.eval("arguments instanceof Array"), true); 33.118 - assertEquals(e.eval("arguments.length == 0"), true); 33.119 - } catch (final Exception exp) { 33.120 - exp.printStackTrace(); 33.121 - fail(exp.getMessage()); 33.122 - } 33.123 - } 33.124 - 33.125 - @Test 33.126 - public void factoryTests() { 33.127 - final ScriptEngineManager m = new ScriptEngineManager(); 33.128 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.129 - assertNotNull(e); 33.130 - 33.131 - final ScriptEngineFactory fac = e.getFactory(); 33.132 - 33.133 - assertEquals(fac.getLanguageName(), "ECMAScript"); 33.134 - assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); 33.135 - assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1"); 33.136 - assertEquals(fac.getEngineName(), "Oracle Nashorn"); 33.137 - assertEquals(fac.getOutputStatement("context"), "print(context)"); 33.138 - assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');"); 33.139 - assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); 33.140 - 33.141 - boolean seenJS = false; 33.142 - for (final String ext : fac.getExtensions()) { 33.143 - if (ext.equals("js")) { 33.144 - seenJS = true; 33.145 - } 33.146 - } 33.147 - 33.148 - assertEquals(seenJS, true); 33.149 - final String str = fac.getMethodCallSyntax("obj", "foo", "x"); 33.150 - assertEquals(str, "obj.foo(x)"); 33.151 - 33.152 - boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false; 33.153 - for (final String name : fac.getNames()) { 33.154 - switch (name) { 33.155 - case "nashorn": seenNashorn = true; break; 33.156 - case "javascript": seenJavaScript = true; break; 33.157 - case "ECMAScript": seenECMAScript = true; break; 33.158 - default: 33.159 - break; 33.160 - } 33.161 - } 33.162 - 33.163 - assertTrue(seenNashorn); 33.164 - assertTrue(seenJavaScript); 33.165 - assertTrue(seenECMAScript); 33.166 - 33.167 - boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false; 33.168 - for (final String mime : fac.getMimeTypes()) { 33.169 - switch (mime) { 33.170 - case "application/javascript": seenAppJS = true; break; 33.171 - case "application/ecmascript": seenAppECMA = true; break; 33.172 - case "text/javascript": seenTextJS = true; break; 33.173 - case "text/ecmascript": seenTextECMA = true; break; 33.174 - default: 33.175 - break; 33.176 - } 33.177 - } 33.178 - 33.179 - assertTrue(seenAppJS); 33.180 - assertTrue(seenAppECMA); 33.181 - assertTrue(seenTextJS); 33.182 - assertTrue(seenTextECMA); 33.183 - } 33.184 - 33.185 - @Test 33.186 - public void evalTests() { 33.187 - final ScriptEngineManager m = new ScriptEngineManager(); 33.188 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.189 - e.put(ScriptEngine.FILENAME, "myfile.js"); 33.190 - 33.191 - try { 33.192 - e.eval("print('hello')"); 33.193 - } catch (final ScriptException se) { 33.194 - fail(se.getMessage()); 33.195 - } 33.196 - try { 33.197 - e.eval("print('hello)"); 33.198 - fail("script exception expected"); 33.199 - } catch (final ScriptException se) { 33.200 - assertEquals(se.getLineNumber(), 1); 33.201 - assertEquals(se.getColumnNumber(), 13); 33.202 - assertEquals(se.getFileName(), "myfile.js"); 33.203 - // se.printStackTrace(); 33.204 - } 33.205 - 33.206 - try { 33.207 - Object obj = e.eval("34 + 41"); 33.208 - assertTrue(34.0 + 41.0 == ((Number)obj).doubleValue()); 33.209 - obj = e.eval("x = 5"); 33.210 - assertTrue(5.0 == ((Number)obj).doubleValue()); 33.211 - } catch (final ScriptException se) { 33.212 - se.printStackTrace(); 33.213 - fail(se.getMessage()); 33.214 - } 33.215 - } 33.216 - 33.217 - @Test 33.218 - public void compileTests() { 33.219 - final ScriptEngineManager m = new ScriptEngineManager(); 33.220 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.221 - CompiledScript script = null; 33.222 - 33.223 - try { 33.224 - script = ((Compilable)e).compile("print('hello')"); 33.225 - } catch (final ScriptException se) { 33.226 - fail(se.getMessage()); 33.227 - } 33.228 - 33.229 - try { 33.230 - script.eval(); 33.231 - } catch (final ScriptException | NullPointerException se) { 33.232 - se.printStackTrace(); 33.233 - fail(se.getMessage()); 33.234 - } 33.235 - 33.236 - // try to compile from a Reader 33.237 - try { 33.238 - script = ((Compilable)e).compile(new StringReader("print('world')")); 33.239 - } catch (final ScriptException se) { 33.240 - fail(se.getMessage()); 33.241 - } 33.242 - 33.243 - try { 33.244 - script.eval(); 33.245 - } catch (final ScriptException | NullPointerException se) { 33.246 - se.printStackTrace(); 33.247 - fail(se.getMessage()); 33.248 - } 33.249 - } 33.250 - 33.251 - @Test 33.252 - public void compileAndEvalInDiffContextTest() throws ScriptException { 33.253 - final ScriptEngineManager m = new ScriptEngineManager(); 33.254 - final ScriptEngine engine = m.getEngineByName("js"); 33.255 - final Compilable compilable = (Compilable) engine; 33.256 - final CompiledScript compiledScript = compilable.compile("foo"); 33.257 - final ScriptContext ctxt = new SimpleScriptContext(); 33.258 - ctxt.setAttribute("foo", "hello", ScriptContext.ENGINE_SCOPE); 33.259 - assertEquals(compiledScript.eval(ctxt), "hello"); 33.260 - } 33.261 - 33.262 - @Test 33.263 - public void accessGlobalTest() { 33.264 - final ScriptEngineManager m = new ScriptEngineManager(); 33.265 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.266 - 33.267 - try { 33.268 - e.eval("var x = 'hello'"); 33.269 - assertEquals(e.get("x"), "hello"); 33.270 - } catch (final ScriptException exp) { 33.271 - exp.printStackTrace(); 33.272 - fail(exp.getMessage()); 33.273 - } 33.274 - } 33.275 - 33.276 - @Test 33.277 - public void exposeGlobalTest() { 33.278 - final ScriptEngineManager m = new ScriptEngineManager(); 33.279 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.280 - 33.281 - try { 33.282 - e.put("y", "foo"); 33.283 - e.eval("print(y)"); 33.284 - } catch (final ScriptException exp) { 33.285 - exp.printStackTrace(); 33.286 - fail(exp.getMessage()); 33.287 - } 33.288 - } 33.289 - 33.290 - @Test 33.291 - public void putGlobalFunctionTest() { 33.292 - final ScriptEngineManager m = new ScriptEngineManager(); 33.293 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.294 - 33.295 - e.put("callable", new Callable<String>() { 33.296 - @Override 33.297 - public String call() throws Exception { 33.298 - return "callable was called"; 33.299 - } 33.300 - }); 33.301 - 33.302 - try { 33.303 - e.eval("print(callable.call())"); 33.304 - } catch (final ScriptException exp) { 33.305 - exp.printStackTrace(); 33.306 - fail(exp.getMessage()); 33.307 - } 33.308 - } 33.309 - 33.310 - @Test 33.311 - public void windowAlertTest() { 33.312 - final ScriptEngineManager m = new ScriptEngineManager(); 33.313 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.314 - final Window window = new Window(); 33.315 - 33.316 - try { 33.317 - e.put("window", window); 33.318 - e.eval("print(window.alert)"); 33.319 - e.eval("window.alert('calling window.alert...')"); 33.320 - } catch (final Exception exp) { 33.321 - exp.printStackTrace(); 33.322 - fail(exp.getMessage()); 33.323 - } 33.324 - } 33.325 - 33.326 - @Test 33.327 - public void windowLocationTest() { 33.328 - final ScriptEngineManager m = new ScriptEngineManager(); 33.329 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.330 - final Window window = new Window(); 33.331 - 33.332 - try { 33.333 - e.put("window", window); 33.334 - e.eval("print(window.location)"); 33.335 - final Object locationValue = e.eval("window.getLocation()"); 33.336 - assertEquals(locationValue, "http://localhost:8080/window"); 33.337 - } catch (final Exception exp) { 33.338 - exp.printStackTrace(); 33.339 - fail(exp.getMessage()); 33.340 - } 33.341 - } 33.342 - 33.343 - @Test 33.344 - public void windowItemTest() { 33.345 - final ScriptEngineManager m = new ScriptEngineManager(); 33.346 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.347 - final Window window = new Window(); 33.348 - 33.349 - try { 33.350 - e.put("window", window); 33.351 - final String item1 = (String)e.eval("window.item(65535)"); 33.352 - assertEquals(item1, "ffff"); 33.353 - final String item2 = (String)e.eval("window.item(255)"); 33.354 - assertEquals(item2, "ff"); 33.355 - } catch (final Exception exp) { 33.356 - exp.printStackTrace(); 33.357 - fail(exp.getMessage()); 33.358 - } 33.359 - } 33.360 - 33.361 - @Test 33.362 - public void windowEventTest() { 33.363 - final ScriptEngineManager m = new ScriptEngineManager(); 33.364 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.365 - final Window window = new Window(); 33.366 - 33.367 - try { 33.368 - e.put("window", window); 33.369 - e.eval("window.onload = function() { print('window load event fired'); return true }"); 33.370 - assertTrue((Boolean)e.eval("window.onload.loaded()")); 33.371 - final WindowEventHandler handler = window.getOnload(); 33.372 - assertNotNull(handler); 33.373 - assertTrue(handler.loaded()); 33.374 - } catch (final Exception exp) { 33.375 - exp.printStackTrace(); 33.376 - fail(exp.getMessage()); 33.377 - } 33.378 - } 33.379 - 33.380 - @Test 33.381 - public void throwTest() { 33.382 - final ScriptEngineManager m = new ScriptEngineManager(); 33.383 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.384 - e.put(ScriptEngine.FILENAME, "throwtest.js"); 33.385 - 33.386 - try { 33.387 - e.eval("throw 'foo'"); 33.388 - } catch (final ScriptException exp) { 33.389 - log(exp.getMessage()); 33.390 - assertEquals(exp.getMessage(), "foo in throwtest.js at line number 1 at column number 0"); 33.391 - assertEquals(exp.getFileName(), "throwtest.js"); 33.392 - assertEquals(exp.getLineNumber(), 1); 33.393 - } 33.394 - } 33.395 - 33.396 - @Test 33.397 - public void setTimeoutTest() { 33.398 - final ScriptEngineManager m = new ScriptEngineManager(); 33.399 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.400 - final Window window = new Window(); 33.401 - 33.402 - try { 33.403 - final Class<?> setTimeoutParamTypes[] = { Window.class, String.class, int.class }; 33.404 - final Method setTimeout = Window.class.getDeclaredMethod("setTimeout", setTimeoutParamTypes); 33.405 - assertNotNull(setTimeout); 33.406 - e.put("window", window); 33.407 - e.eval("window.setTimeout('foo()', 100)"); 33.408 - 33.409 - // try to make setTimeout global 33.410 - e.put("setTimeout", setTimeout); 33.411 - // TODO: java.lang.ClassCastException: required class 33.412 - // java.lang.Integer but encountered class java.lang.Double 33.413 - // e.eval("setTimeout('foo2()', 200)"); 33.414 - } catch (final Exception exp) { 33.415 - exp.printStackTrace(); 33.416 - fail(exp.getMessage()); 33.417 - } 33.418 - } 33.419 - 33.420 - @Test 33.421 - public void setWriterTest() { 33.422 - final ScriptEngineManager m = new ScriptEngineManager(); 33.423 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.424 - final StringWriter sw = new StringWriter(); 33.425 - e.getContext().setWriter(sw); 33.426 - 33.427 - try { 33.428 - e.eval("print('hello world')"); 33.429 - } catch (final Exception exp) { 33.430 - exp.printStackTrace(); 33.431 - fail(exp.getMessage()); 33.432 - } 33.433 - assertEquals(sw.toString(), println("hello world")); 33.434 - } 33.435 - 33.436 - @Test 33.437 - public void redefineEchoTest() { 33.438 - final ScriptEngineManager m = new ScriptEngineManager(); 33.439 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.440 - 33.441 - try { 33.442 - e.eval("var echo = {}; if (typeof echo !== 'object') { throw 'echo is a '+typeof echo; }"); 33.443 - } catch (final Exception exp) { 33.444 - exp.printStackTrace(); 33.445 - fail(exp.getMessage()); 33.446 - } 33.447 - } 33.448 - @Test 33.449 - public void noEnumerablePropertiesTest() { 33.450 - final ScriptEngineManager m = new ScriptEngineManager(); 33.451 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.452 - try { 33.453 - e.eval("for (i in this) { throw 'found property: ' + i }"); 33.454 - } catch (final Exception exp) { 33.455 - exp.printStackTrace(); 33.456 - fail(exp.getMessage()); 33.457 - } 33.458 - } 33.459 - 33.460 - @Test 33.461 - public void noRefErrorForGlobalThisAccessTest() { 33.462 - final ScriptEngineManager m = new ScriptEngineManager(); 33.463 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.464 - try { 33.465 - e.eval("this.foo"); 33.466 - } catch (final Exception exp) { 33.467 - exp.printStackTrace(); 33.468 - fail(exp.getMessage()); 33.469 - } 33.470 - } 33.471 - 33.472 - @Test 33.473 - public void refErrorForUndeclaredAccessTest() { 33.474 - final ScriptEngineManager m = new ScriptEngineManager(); 33.475 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.476 - try { 33.477 - e.eval("try { print(foo); throw 'no ref error' } catch (e) { if (!(e instanceof ReferenceError)) throw e; }"); 33.478 - } catch (final Exception exp) { 33.479 - exp.printStackTrace(); 33.480 - fail(exp.getMessage()); 33.481 - } 33.482 - } 33.483 - 33.484 - @Test 33.485 - public void typeErrorForGlobalThisCallTest() { 33.486 - final ScriptEngineManager m = new ScriptEngineManager(); 33.487 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.488 - try { 33.489 - e.eval("try { this.foo() } catch(e) { if (! (e instanceof TypeError)) throw 'no type error' }"); 33.490 - } catch (final Exception exp) { 33.491 - exp.printStackTrace(); 33.492 - fail(exp.getMessage()); 33.493 - } 33.494 - } 33.495 - 33.496 - @Test 33.497 - public void refErrorForUndeclaredCallTest() { 33.498 - final ScriptEngineManager m = new ScriptEngineManager(); 33.499 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.500 - try { 33.501 - e.eval("try { foo() } catch(e) { if (! (e instanceof ReferenceError)) throw 'no ref error' }"); 33.502 - } catch (final Exception exp) { 33.503 - exp.printStackTrace(); 33.504 - fail(exp.getMessage()); 33.505 - } 33.506 - } 33.507 - 33.508 - @Test 33.509 - // check that print function prints arg followed by newline char 33.510 - public void printTest() { 33.511 - final ScriptEngineManager m = new ScriptEngineManager(); 33.512 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.513 - final StringWriter sw = new StringWriter(); 33.514 - e.getContext().setWriter(sw); 33.515 - try { 33.516 - e.eval("print('hello')"); 33.517 - } catch (final Throwable t) { 33.518 - t.printStackTrace(); 33.519 - fail(t.getMessage()); 33.520 - } 33.521 - 33.522 - assertEquals(sw.toString(), println("hello")); 33.523 - } 33.524 - 33.525 - @Test 33.526 - // check that print prints all arguments (more than one) 33.527 - public void printManyTest() { 33.528 - final ScriptEngineManager m = new ScriptEngineManager(); 33.529 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.530 - final StringWriter sw = new StringWriter(); 33.531 - e.getContext().setWriter(sw); 33.532 - try { 33.533 - e.eval("print(34, true, 'hello')"); 33.534 - } catch (final Throwable t) { 33.535 - t.printStackTrace(); 33.536 - fail(t.getMessage()); 33.537 - } 33.538 - 33.539 - assertEquals(sw.toString(), println("34 true hello")); 33.540 - } 33.541 - 33.542 - @Test 33.543 - public void scriptObjectAutoConversionTest() throws ScriptException { 33.544 - final ScriptEngineManager m = new ScriptEngineManager(); 33.545 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.546 - e.eval("obj = { foo: 'hello' }"); 33.547 - e.put("Window", e.eval("Packages.jdk.nashorn.api.scripting.Window")); 33.548 - assertEquals(e.eval("Window.funcJSObject(obj)"), "hello"); 33.549 - assertEquals(e.eval("Window.funcScriptObjectMirror(obj)"), "hello"); 33.550 - assertEquals(e.eval("Window.funcMap(obj)"), "hello"); 33.551 - assertEquals(e.eval("Window.funcJSObject(obj)"), "hello"); 33.552 - } 33.553 - 33.554 - // @bug 8032948: Nashorn linkages awry 33.555 - @Test 33.556 - public void checkProxyAccess() throws ScriptException { 33.557 - final ScriptEngineManager m = new ScriptEngineManager(); 33.558 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.559 - final boolean[] reached = new boolean[1]; 33.560 - final Runnable r = (Runnable)Proxy.newProxyInstance( 33.561 - ScriptEngineTest.class.getClassLoader(), 33.562 - new Class[] { Runnable.class }, 33.563 - new InvocationHandler() { 33.564 - @Override 33.565 - public Object invoke(final Object p, final Method mtd, final Object[] a) { 33.566 - reached[0] = true; 33.567 - return null; 33.568 - } 33.569 - }); 33.570 - 33.571 - e.put("r", r); 33.572 - e.eval("r.run()"); 33.573 - 33.574 - assertTrue(reached[0]); 33.575 - } 33.576 - 33.577 - // properties that can be read by any code 33.578 - private static String[] propNames = { 33.579 - "java.version", 33.580 - "java.vendor", 33.581 - "java.vendor.url", 33.582 - "java.class.version", 33.583 - "os.name", 33.584 - "os.version", 33.585 - "os.arch", 33.586 - "file.separator", 33.587 - "path.separator", 33.588 - "line.separator", 33.589 - "java.specification.version", 33.590 - "java.specification.vendor", 33.591 - "java.specification.name", 33.592 - "java.vm.specification.version", 33.593 - "java.vm.specification.vendor", 33.594 - "java.vm.specification.name", 33.595 - "java.vm.version", 33.596 - "java.vm.vendor", 33.597 - "java.vm.name" 33.598 - }; 33.599 - 33.600 - // @bug 8033924: Default permissions are not given for eval code 33.601 - @Test 33.602 - public void checkPropertyReadPermissions() throws ScriptException { 33.603 - final ScriptEngineManager m = new ScriptEngineManager(); 33.604 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.605 - 33.606 - for (final String name : propNames) { 33.607 - checkProperty(e, name); 33.608 - } 33.609 - } 33.610 - 33.611 - // @bug 8046013: TypeError: Cannot apply "with" to non script object 33.612 - @Test 33.613 - public void withOnMirrorTest() throws ScriptException { 33.614 - final ScriptEngineManager m = new ScriptEngineManager(); 33.615 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.616 - 33.617 - final Object obj = e.eval("({ foo: 'hello'})"); 33.618 - final Object[] arr = new Object[1]; 33.619 - arr[0] = obj; 33.620 - e.put("arr", arr); 33.621 - final Object res = e.eval("var res; with(arr[0]) { res = foo; }; res"); 33.622 - assertEquals(res, "hello"); 33.623 - } 33.624 - 33.625 - // @bug 8054223: Nashorn: AssertionError when use __DIR__ and ScriptEngine.eval() 33.626 - @Test 33.627 - public void check__DIR__Test() throws ScriptException { 33.628 - final ScriptEngineManager m = new ScriptEngineManager(); 33.629 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.630 - e.eval("__DIR__"); 33.631 - } 33.632 - 33.633 - // @bug 8050432:javax.script.filename variable should not be enumerable 33.634 - // with nashorn engine's ENGINE_SCOPE bindings 33.635 - @Test 33.636 - public void enumerableGlobalsTest() throws ScriptException { 33.637 - final ScriptEngineManager m = new ScriptEngineManager(); 33.638 - final ScriptEngine e = m.getEngineByName("nashorn"); 33.639 - 33.640 - e.put(ScriptEngine.FILENAME, "test"); 33.641 - final Object enumerable = e.eval( 33.642 - "Object.getOwnPropertyDescriptor(this, " + 33.643 - " 'javax.script.filename').enumerable"); 33.644 - assertEquals(enumerable, Boolean.FALSE); 33.645 - } 33.646 - 33.647 - public static class Context { 33.648 - private Object myobj; 33.649 - 33.650 - public void set(final Object o) { 33.651 - myobj = o; 33.652 - } 33.653 - 33.654 - public Object get() { 33.655 - return myobj; 33.656 - } 33.657 - } 33.658 - 33.659 - // @bug 8050977: Java8 Javascript Nashorn exception: 33.660 - // no current Global instance for nashorn 33.661 - @Test 33.662 - public void currentGlobalMissingTest() throws Exception { 33.663 - final ScriptEngineManager manager = new ScriptEngineManager(); 33.664 - final ScriptEngine e = manager.getEngineByName("nashorn"); 33.665 - 33.666 - final Context ctx = new Context(); 33.667 - e.put("ctx", ctx); 33.668 - e.eval("var obj = { foo: function(str) { return str.toUpperCase() } }"); 33.669 - e.eval("ctx.set(obj)"); 33.670 - final Invocable inv = (Invocable)e; 33.671 - assertEquals("HELLO", inv.invokeMethod(ctx.get(), "foo", "hello")); 33.672 - // try object literal 33.673 - e.eval("ctx.set({ bar: function(str) { return str.toLowerCase() } })"); 33.674 - assertEquals("hello", inv.invokeMethod(ctx.get(), "bar", "HELLO")); 33.675 - // try array literal 33.676 - e.eval("var arr = [ 'hello', 'world' ]"); 33.677 - e.eval("ctx.set(arr)"); 33.678 - assertEquals("helloworld", inv.invokeMethod(ctx.get(), "join", "")); 33.679 - } 33.680 - 33.681 - // @bug 8068524: NashornScriptEngineFactory.getParameter() throws IAE 33.682 - // for an unknown key, doesn't conform to the general spec 33.683 - @Test 33.684 - public void getParameterInvalidKeyTest() throws Exception { 33.685 - final ScriptEngineManager manager = new ScriptEngineManager(); 33.686 - final ScriptEngine e = manager.getEngineByName("nashorn"); 33.687 - // no exception expected here! 33.688 - Object value = e.getFactory().getParameter("no value assigned to this key"); 33.689 - assertNull(value); 33.690 - } 33.691 - 33.692 - // @bug JDK-8068889: ConsString arguments to a functional interface wasn't converted to string. 33.693 - @Test 33.694 - public void functionalInterfaceStringTest() throws Exception { 33.695 - final ScriptEngineManager manager = new ScriptEngineManager(); 33.696 - final ScriptEngine e = manager.getEngineByName("nashorn"); 33.697 - final AtomicBoolean invoked = new AtomicBoolean(false); 33.698 - e.put("f", new Function<String, String>() { 33.699 - @Override 33.700 - public String apply(String t) { 33.701 - invoked.set(true); 33.702 - return t; 33.703 - } 33.704 - }); 33.705 - assertEquals(e.eval("var x = 'a'; x += 'b'; f(x)"), "ab"); 33.706 - assertTrue(invoked.get()); 33.707 - } 33.708 - 33.709 - // @bug JDK-8068889: ScriptObject arguments to a functional interface wasn't converted to a mirror. 33.710 - @Test 33.711 - public void functionalInterfaceObjectTest() throws Exception { 33.712 - final ScriptEngineManager manager = new ScriptEngineManager(); 33.713 - final ScriptEngine e = manager.getEngineByName("nashorn"); 33.714 - final AtomicBoolean invoked = new AtomicBoolean(false); 33.715 - e.put("c", new Consumer<Object>() { 33.716 - @Override 33.717 - public void accept(Object t) { 33.718 - assertTrue(t instanceof ScriptObjectMirror); 33.719 - assertEquals(((ScriptObjectMirror)t).get("a"), "xyz"); 33.720 - invoked.set(true); 33.721 - } 33.722 - }); 33.723 - e.eval("var x = 'xy'; x += 'z';c({a:x})"); 33.724 - assertTrue(invoked.get()); 33.725 - } 33.726 - 33.727 - @Test 33.728 - public void testLengthOnArrayLikeObjects() throws Exception { 33.729 - final ScriptEngine e = new ScriptEngineManager().getEngineByName("nashorn"); 33.730 - final Object val = e.eval("var arr = { length: 1, 0: 1}; arr.length"); 33.731 - 33.732 - assertTrue(Number.class.isAssignableFrom(val.getClass())); 33.733 - assertTrue(((Number)val).intValue() == 1); 33.734 - } 33.735 - 33.736 - // @bug JDK-8068603: NashornScriptEngine.put/get() impls don't conform to NPE, IAE spec assertions 33.737 - @Test 33.738 - public void illegalBindingsValuesTest() throws Exception { 33.739 - final ScriptEngineManager manager = new ScriptEngineManager(); 33.740 - final ScriptEngine e = manager.getEngineByName("nashorn"); 33.741 - 33.742 - try { 33.743 - e.put(null, "null-value"); 33.744 - fail(); 33.745 - } catch (NullPointerException x) { 33.746 - // expected 33.747 - } 33.748 - 33.749 - try { 33.750 - e.put("", "empty-value"); 33.751 - fail(); 33.752 - } catch (IllegalArgumentException x) { 33.753 - // expected 33.754 - } 33.755 - 33.756 - final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 33.757 - assertTrue(b instanceof ScriptObjectMirror); 33.758 - 33.759 - try { 33.760 - b.put(null, "null-value"); 33.761 - fail(); 33.762 - } catch (NullPointerException x) { 33.763 - // expected 33.764 - } 33.765 - 33.766 - try { 33.767 - b.put("", "empty-value"); 33.768 - fail(); 33.769 - } catch (IllegalArgumentException x) { 33.770 - // expected 33.771 - } 33.772 - 33.773 - try { 33.774 - b.get(null); 33.775 - fail(); 33.776 - } catch (NullPointerException x) { 33.777 - // expected 33.778 - } 33.779 - 33.780 - try { 33.781 - b.get(""); 33.782 - fail(); 33.783 - } catch (IllegalArgumentException x) { 33.784 - // expected 33.785 - } 33.786 - 33.787 - try { 33.788 - b.get(1); 33.789 - fail(); 33.790 - } catch (ClassCastException x) { 33.791 - // expected 33.792 - } 33.793 - 33.794 - try { 33.795 - b.remove(null); 33.796 - fail(); 33.797 - } catch (NullPointerException x) { 33.798 - // expected 33.799 - } 33.800 - 33.801 - try { 33.802 - b.remove(""); 33.803 - fail(); 33.804 - } catch (IllegalArgumentException x) { 33.805 - // expected 33.806 - } 33.807 - 33.808 - try { 33.809 - b.remove(1); 33.810 - fail(); 33.811 - } catch (ClassCastException x) { 33.812 - // expected 33.813 - } 33.814 - 33.815 - try { 33.816 - b.containsKey(null); 33.817 - fail(); 33.818 - } catch (NullPointerException x) { 33.819 - // expected 33.820 - } 33.821 - 33.822 - try { 33.823 - b.containsKey(""); 33.824 - fail(); 33.825 - } catch (IllegalArgumentException x) { 33.826 - // expected 33.827 - } 33.828 - 33.829 - try { 33.830 - b.containsKey(1); 33.831 - fail(); 33.832 - } catch (ClassCastException x) { 33.833 - // expected 33.834 - } 33.835 - 33.836 - try { 33.837 - b.putAll(null); 33.838 - fail(); 33.839 - } catch (NullPointerException x) { 33.840 - // expected 33.841 - } 33.842 - 33.843 - try { 33.844 - b.putAll(Collections.singletonMap((String)null, "null-value")); 33.845 - fail(); 33.846 - } catch (NullPointerException x) { 33.847 - // expected 33.848 - } 33.849 - 33.850 - try { 33.851 - b.putAll(Collections.singletonMap("", "empty-value")); 33.852 - fail(); 33.853 - } catch (IllegalArgumentException x) { 33.854 - // expected 33.855 - } 33.856 - } 33.857 - 33.858 - // @bug 8071989: NashornScriptEngine returns javax.script.ScriptContext instance 33.859 - // with insonsistent get/remove methods behavior for undefined attributes 33.860 - @Test 33.861 - public void testScriptContextGetRemoveUndefined() throws Exception { 33.862 - final ScriptEngineManager manager = new ScriptEngineManager(); 33.863 - final ScriptEngine e = manager.getEngineByName("nashorn"); 33.864 - final ScriptContext ctx = e.getContext(); 33.865 - assertNull(ctx.getAttribute("undefinedname", ScriptContext.ENGINE_SCOPE)); 33.866 - assertNull(ctx.removeAttribute("undefinedname", ScriptContext.ENGINE_SCOPE)); 33.867 - } 33.868 - 33.869 - private static void checkProperty(final ScriptEngine e, final String name) 33.870 - throws ScriptException { 33.871 - final String value = System.getProperty(name); 33.872 - e.put("name", name); 33.873 - assertEquals(value, e.eval("java.lang.System.getProperty(name)")); 33.874 - } 33.875 - 33.876 - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 33.877 - 33.878 - // Returns String that would be the result of calling PrintWriter.println 33.879 - // of the given String. (This is to handle platform specific newline). 33.880 - private static String println(final String str) { 33.881 - return str + LINE_SEPARATOR; 33.882 - } 33.883 -}
34.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptObjectMirrorTest.java Wed Apr 01 13:22:52 2015 -0700 34.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 34.3 @@ -1,387 +0,0 @@ 34.4 -/* 34.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 34.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 34.7 - * 34.8 - * This code is free software; you can redistribute it and/or modify it 34.9 - * under the terms of the GNU General Public License version 2 only, as 34.10 - * published by the Free Software Foundation. Oracle designates this 34.11 - * particular file as subject to the "Classpath" exception as provided 34.12 - * by Oracle in the LICENSE file that accompanied this code. 34.13 - * 34.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 34.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 34.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 34.17 - * version 2 for more details (a copy is included in the LICENSE file that 34.18 - * accompanied this code). 34.19 - * 34.20 - * You should have received a copy of the GNU General Public License version 34.21 - * 2 along with this work; if not, write to the Free Software Foundation, 34.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 34.23 - * 34.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 34.25 - * or visit www.oracle.com if you need additional information or have any 34.26 - * questions. 34.27 - */ 34.28 - 34.29 -package jdk.nashorn.api.scripting; 34.30 - 34.31 -import static org.testng.Assert.assertEquals; 34.32 -import static org.testng.Assert.assertFalse; 34.33 -import static org.testng.Assert.assertTrue; 34.34 -import static org.testng.Assert.fail; 34.35 - 34.36 -import java.nio.ByteBuffer; 34.37 -import java.util.HashMap; 34.38 -import java.util.List; 34.39 -import java.util.Map; 34.40 -import java.util.function.Function; 34.41 -import javax.script.Bindings; 34.42 -import javax.script.Invocable; 34.43 -import javax.script.ScriptContext; 34.44 -import javax.script.ScriptEngine; 34.45 -import javax.script.ScriptEngineManager; 34.46 -import javax.script.ScriptException; 34.47 -import org.testng.annotations.Test; 34.48 - 34.49 -/** 34.50 - * Tests to check jdk.nashorn.api.scripting.ScriptObjectMirror API. 34.51 - */ 34.52 -@SuppressWarnings("javadoc") 34.53 -public class ScriptObjectMirrorTest { 34.54 - 34.55 - @SuppressWarnings("unchecked") 34.56 - @Test 34.57 - public void reflectionTest() throws ScriptException { 34.58 - final ScriptEngineManager m = new ScriptEngineManager(); 34.59 - final ScriptEngine e = m.getEngineByName("nashorn"); 34.60 - 34.61 - e.eval("var obj = { x: 344, y: 'nashorn' }"); 34.62 - 34.63 - int count = 0; 34.64 - Map<Object, Object> map = (Map<Object, Object>) e.get("obj"); 34.65 - assertFalse(map.isEmpty()); 34.66 - assertTrue(map.keySet().contains("x")); 34.67 - assertTrue(map.containsKey("x")); 34.68 - assertTrue(map.values().contains("nashorn")); 34.69 - assertTrue(map.containsValue("nashorn")); 34.70 - for (final Map.Entry<?, ?> ex : map.entrySet()) { 34.71 - final Object key = ex.getKey(); 34.72 - if (key.equals("x")) { 34.73 - assertTrue(344 == ((Number) ex.getValue()).doubleValue()); 34.74 - count++; 34.75 - } else if (key.equals("y")) { 34.76 - assertEquals(ex.getValue(), "nashorn"); 34.77 - count++; 34.78 - } 34.79 - } 34.80 - assertEquals(2, count); 34.81 - assertEquals(2, map.size()); 34.82 - 34.83 - // add property 34.84 - map.put("z", "hello"); 34.85 - assertEquals(e.eval("obj.z"), "hello"); 34.86 - assertEquals(map.get("z"), "hello"); 34.87 - assertTrue(map.keySet().contains("z")); 34.88 - assertTrue(map.containsKey("z")); 34.89 - assertTrue(map.values().contains("hello")); 34.90 - assertTrue(map.containsValue("hello")); 34.91 - assertEquals(map.size(), 3); 34.92 - 34.93 - final Map<Object, Object> newMap = new HashMap<>(); 34.94 - newMap.put("foo", 23.0); 34.95 - newMap.put("bar", true); 34.96 - map.putAll(newMap); 34.97 - 34.98 - assertEquals(e.eval("obj.foo"), 23.0); 34.99 - assertEquals(e.eval("obj.bar"), true); 34.100 - 34.101 - // remove using map method 34.102 - map.remove("foo"); 34.103 - assertEquals(e.eval("typeof obj.foo"), "undefined"); 34.104 - 34.105 - count = 0; 34.106 - e.eval("var arr = [ true, 'hello' ]"); 34.107 - map = (Map<Object, Object>) e.get("arr"); 34.108 - assertFalse(map.isEmpty()); 34.109 - assertTrue(map.containsKey("length")); 34.110 - assertTrue(map.containsValue("hello")); 34.111 - for (final Map.Entry<?, ?> ex : map.entrySet()) { 34.112 - final Object key = ex.getKey(); 34.113 - if (key.equals("0")) { 34.114 - assertEquals(ex.getValue(), Boolean.TRUE); 34.115 - count++; 34.116 - } else if (key.equals("1")) { 34.117 - assertEquals(ex.getValue(), "hello"); 34.118 - count++; 34.119 - } 34.120 - } 34.121 - assertEquals(count, 2); 34.122 - assertEquals(map.size(), 2); 34.123 - 34.124 - // add element 34.125 - map.put("2", "world"); 34.126 - assertEquals(map.get("2"), "world"); 34.127 - assertEquals(map.size(), 3); 34.128 - 34.129 - // remove all 34.130 - map.clear(); 34.131 - assertTrue(map.isEmpty()); 34.132 - assertEquals(e.eval("typeof arr[0]"), "undefined"); 34.133 - assertEquals(e.eval("typeof arr[1]"), "undefined"); 34.134 - assertEquals(e.eval("typeof arr[2]"), "undefined"); 34.135 - } 34.136 - 34.137 - @Test 34.138 - public void jsobjectTest() { 34.139 - final ScriptEngineManager m = new ScriptEngineManager(); 34.140 - final ScriptEngine e = m.getEngineByName("nashorn"); 34.141 - try { 34.142 - e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }"); 34.143 - final ScriptObjectMirror obj = (ScriptObjectMirror) e.get("obj"); 34.144 - 34.145 - // try basic get on existing properties 34.146 - if (!obj.getMember("bar").equals("hello")) { 34.147 - fail("obj.bar != 'hello'"); 34.148 - } 34.149 - 34.150 - if (!obj.getSlot(1).equals("world")) { 34.151 - fail("obj[1] != 'world'"); 34.152 - } 34.153 - 34.154 - if (!obj.callMember("func", new Object[0]).equals("hello")) { 34.155 - fail("obj.func() != 'hello'"); 34.156 - } 34.157 - 34.158 - // try setting properties 34.159 - obj.setMember("bar", "new-bar"); 34.160 - obj.setSlot(1, "new-element-1"); 34.161 - if (!obj.getMember("bar").equals("new-bar")) { 34.162 - fail("obj.bar != 'new-bar'"); 34.163 - } 34.164 - 34.165 - if (!obj.getSlot(1).equals("new-element-1")) { 34.166 - fail("obj[1] != 'new-element-1'"); 34.167 - } 34.168 - 34.169 - // try adding properties 34.170 - obj.setMember("prop", "prop-value"); 34.171 - obj.setSlot(12, "element-12"); 34.172 - if (!obj.getMember("prop").equals("prop-value")) { 34.173 - fail("obj.prop != 'prop-value'"); 34.174 - } 34.175 - 34.176 - if (!obj.getSlot(12).equals("element-12")) { 34.177 - fail("obj[12] != 'element-12'"); 34.178 - } 34.179 - 34.180 - // delete properties 34.181 - obj.removeMember("prop"); 34.182 - if ("prop-value".equals(obj.getMember("prop"))) { 34.183 - fail("obj.prop is not deleted!"); 34.184 - } 34.185 - 34.186 - // Simple eval tests 34.187 - assertEquals(obj.eval("typeof Object"), "function"); 34.188 - assertEquals(obj.eval("'nashorn'.substring(3)"), "horn"); 34.189 - } catch (final Exception exp) { 34.190 - exp.printStackTrace(); 34.191 - fail(exp.getMessage()); 34.192 - } 34.193 - } 34.194 - 34.195 - @Test 34.196 - public void scriptObjectMirrorToStringTest() { 34.197 - final ScriptEngineManager m = new ScriptEngineManager(); 34.198 - final ScriptEngine e = m.getEngineByName("nashorn"); 34.199 - try { 34.200 - final Object obj = e.eval("new TypeError('wrong type')"); 34.201 - assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value"); 34.202 - } catch (final Throwable t) { 34.203 - t.printStackTrace(); 34.204 - fail(t.getMessage()); 34.205 - } 34.206 - 34.207 - try { 34.208 - final Object obj = e.eval("function func() { print('hello'); }"); 34.209 - assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value"); 34.210 - } catch (final Throwable t) { 34.211 - t.printStackTrace(); 34.212 - fail(t.getMessage()); 34.213 - } 34.214 - } 34.215 - 34.216 - @Test 34.217 - public void mirrorNewObjectGlobalFunctionTest() throws ScriptException { 34.218 - final ScriptEngineManager m = new ScriptEngineManager(); 34.219 - final ScriptEngine e = m.getEngineByName("nashorn"); 34.220 - final ScriptEngine e2 = m.getEngineByName("nashorn"); 34.221 - 34.222 - e.eval("function func() {}"); 34.223 - e2.put("foo", e.get("func")); 34.224 - final ScriptObjectMirror e2global = (ScriptObjectMirror)e2.eval("this"); 34.225 - final Object newObj = ((ScriptObjectMirror)e2global.getMember("foo")).newObject(); 34.226 - assertTrue(newObj instanceof ScriptObjectMirror); 34.227 - } 34.228 - 34.229 - @Test 34.230 - public void mirrorNewObjectInstanceFunctionTest() throws ScriptException { 34.231 - final ScriptEngineManager m = new ScriptEngineManager(); 34.232 - final ScriptEngine e = m.getEngineByName("nashorn"); 34.233 - final ScriptEngine e2 = m.getEngineByName("nashorn"); 34.234 - 34.235 - e.eval("function func() {}"); 34.236 - e2.put("func", e.get("func")); 34.237 - final ScriptObjectMirror e2obj = (ScriptObjectMirror)e2.eval("({ foo: func })"); 34.238 - final Object newObj = ((ScriptObjectMirror)e2obj.getMember("foo")).newObject(); 34.239 - assertTrue(newObj instanceof ScriptObjectMirror); 34.240 - } 34.241 - 34.242 - @Test 34.243 - public void indexPropertiesExternalBufferTest() throws ScriptException { 34.244 - final ScriptEngineManager m = new ScriptEngineManager(); 34.245 - final ScriptEngine e = m.getEngineByName("nashorn"); 34.246 - final ScriptObjectMirror obj = (ScriptObjectMirror)e.eval("var obj = {}; obj"); 34.247 - final ByteBuffer buf = ByteBuffer.allocate(5); 34.248 - int i; 34.249 - for (i = 0; i < 5; i++) { 34.250 - buf.put(i, (byte)(i+10)); 34.251 - } 34.252 - obj.setIndexedPropertiesToExternalArrayData(buf); 34.253 - 34.254 - for (i = 0; i < 5; i++) { 34.255 - assertEquals((byte)(i+10), ((Number)e.eval("obj[" + i + "]")).byteValue()); 34.256 - } 34.257 - 34.258 - e.eval("for (i = 0; i < 5; i++) obj[i] = 0"); 34.259 - for (i = 0; i < 5; i++) { 34.260 - assertEquals((byte)0, ((Number)e.eval("obj[" + i + "]")).byteValue()); 34.261 - assertEquals((byte)0, buf.get(i)); 34.262 - } 34.263 - } 34.264 - 34.265 - @Test 34.266 - public void conversionTest() throws ScriptException { 34.267 - final ScriptEngineManager m = new ScriptEngineManager(); 34.268 - final ScriptEngine e = m.getEngineByName("nashorn"); 34.269 - final ScriptObjectMirror arr = (ScriptObjectMirror)e.eval("[33, 45, 23]"); 34.270 - final int[] intArr = arr.to(int[].class); 34.271 - assertEquals(intArr[0], 33); 34.272 - assertEquals(intArr[1], 45); 34.273 - assertEquals(intArr[2], 23); 34.274 - 34.275 - final List<?> list = arr.to(List.class); 34.276 - assertEquals(list.get(0), 33); 34.277 - assertEquals(list.get(1), 45); 34.278 - assertEquals(list.get(2), 23); 34.279 - 34.280 - ScriptObjectMirror obj = (ScriptObjectMirror)e.eval( 34.281 - "({ valueOf: function() { return 42 } })"); 34.282 - assertEquals(Double.valueOf(42.0), obj.to(Double.class)); 34.283 - 34.284 - obj = (ScriptObjectMirror)e.eval( 34.285 - "({ toString: function() { return 'foo' } })"); 34.286 - assertEquals("foo", obj.to(String.class)); 34.287 - } 34.288 - 34.289 - // @bug 8044000: Access to undefined property yields "null" instead of "undefined" 34.290 - @Test 34.291 - public void mapScriptObjectMirrorCallsiteTest() throws ScriptException { 34.292 - final ScriptEngineManager m = new ScriptEngineManager(); 34.293 - final ScriptEngine engine = m.getEngineByName("nashorn"); 34.294 - final String TEST_SCRIPT = "typeof obj.foo"; 34.295 - 34.296 - final Bindings global = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE); 34.297 - engine.eval("var obj = java.util.Collections.emptyMap()"); 34.298 - // this will drive callsite "obj.foo" of TEST_SCRIPT 34.299 - // to use "obj instanceof Map" as it's guard 34.300 - engine.eval(TEST_SCRIPT, global); 34.301 - // redefine 'obj' to be a script object 34.302 - engine.eval("obj = {}"); 34.303 - 34.304 - final Bindings newGlobal = engine.createBindings(); 34.305 - // transfer 'obj' from default global to new global 34.306 - // new global will get a ScriptObjectMirror wrapping 'obj' 34.307 - newGlobal.put("obj", global.get("obj")); 34.308 - 34.309 - // Every ScriptObjectMirror is a Map! If callsite "obj.foo" 34.310 - // does not see the new 'obj' is a ScriptObjectMirror, it'll 34.311 - // continue to use Map's get("obj.foo") instead of ScriptObjectMirror's 34.312 - // getMember("obj.foo") - thereby getting null instead of undefined 34.313 - assertEquals("undefined", engine.eval(TEST_SCRIPT, newGlobal)); 34.314 - } 34.315 - 34.316 - public interface MirrorCheckExample { 34.317 - Object test1(Object arg); 34.318 - Object test2(Object arg); 34.319 - boolean compare(Object o1, Object o2); 34.320 - } 34.321 - 34.322 - // @bug 8053910: ScriptObjectMirror causing havoc with Invocation interface 34.323 - @Test 34.324 - public void checkMirrorToObject() throws Exception { 34.325 - final ScriptEngineManager engineManager = new ScriptEngineManager(); 34.326 - final ScriptEngine engine = engineManager.getEngineByName("nashorn"); 34.327 - final Invocable invocable = (Invocable)engine; 34.328 - 34.329 - engine.eval("function test1(arg) { return { arg: arg }; }"); 34.330 - engine.eval("function test2(arg) { return arg; }"); 34.331 - engine.eval("function compare(arg1, arg2) { return arg1 == arg2; }"); 34.332 - 34.333 - final Map<String, Object> map = new HashMap<>(); 34.334 - map.put("option", true); 34.335 - 34.336 - final MirrorCheckExample example = invocable.getInterface(MirrorCheckExample.class); 34.337 - 34.338 - final Object value1 = invocable.invokeFunction("test1", map); 34.339 - final Object value2 = example.test1(map); 34.340 - final Object value3 = invocable.invokeFunction("test2", value2); 34.341 - final Object value4 = example.test2(value2); 34.342 - 34.343 - // check that Object type argument receives a ScriptObjectMirror 34.344 - // when ScriptObject is passed 34.345 - assertEquals(ScriptObjectMirror.class, value1.getClass()); 34.346 - assertEquals(ScriptObjectMirror.class, value2.getClass()); 34.347 - assertEquals(ScriptObjectMirror.class, value3.getClass()); 34.348 - assertEquals(ScriptObjectMirror.class, value4.getClass()); 34.349 - assertTrue((boolean)invocable.invokeFunction("compare", value1, value1)); 34.350 - assertTrue(example.compare(value1, value1)); 34.351 - assertTrue((boolean)invocable.invokeFunction("compare", value3, value4)); 34.352 - assertTrue(example.compare(value3, value4)); 34.353 - } 34.354 - 34.355 - // @bug 8053910: ScriptObjectMirror causing havoc with Invocation interface 34.356 - @Test 34.357 - public void mirrorUnwrapInterfaceMethod() throws Exception { 34.358 - final ScriptEngineManager engineManager = new ScriptEngineManager(); 34.359 - final ScriptEngine engine = engineManager.getEngineByName("nashorn"); 34.360 - final Invocable invocable = (Invocable)engine; 34.361 - engine.eval("function apply(obj) { " + 34.362 - " return obj instanceof Packages.jdk.nashorn.api.scripting.ScriptObjectMirror; " + 34.363 - "}"); 34.364 - @SuppressWarnings("unchecked") 34.365 - final Function<Object,Object> func = invocable.getInterface(Function.class); 34.366 - assertFalse((boolean)func.apply(engine.eval("({ x: 2 })"))); 34.367 - } 34.368 - 34.369 - // @bug 8055687: Wrong "this" passed to JSObject.eval call 34.370 - @Test 34.371 - public void checkThisForJSObjectEval() throws Exception { 34.372 - final ScriptEngineManager engineManager = new ScriptEngineManager(); 34.373 - final ScriptEngine e = engineManager.getEngineByName("nashorn"); 34.374 - final JSObject jsobj = (JSObject)e.eval("({foo: 23, bar: 'hello' })"); 34.375 - assertEquals(((Number)jsobj.eval("this.foo")).intValue(), 23); 34.376 - assertEquals(jsobj.eval("this.bar"), "hello"); 34.377 - assertEquals(jsobj.eval("String(this)"), "[object Object]"); 34.378 - final Object global = e.eval("this"); 34.379 - assertFalse(global.equals(jsobj.eval("this"))); 34.380 - } 34.381 - 34.382 - @Test 34.383 - public void topLevelAnonFuncStatement() throws Exception { 34.384 - final ScriptEngineManager engineManager = new ScriptEngineManager(); 34.385 - final ScriptEngine e = engineManager.getEngineByName("nashorn"); 34.386 - final JSObject func = (JSObject)e.eval("function(x) { return x + ' world' }"); 34.387 - assertTrue(func.isFunction()); 34.388 - assertEquals(func.call(e.eval("this"), "hello"), "hello world"); 34.389 - } 34.390 -}
35.1 --- a/test/src/jdk/nashorn/api/scripting/VariableArityTestInterface.java Wed Apr 01 13:22:52 2015 -0700 35.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 35.3 @@ -1,32 +0,0 @@ 35.4 -/* 35.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 35.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 35.7 - * 35.8 - * This code is free software; you can redistribute it and/or modify it 35.9 - * under the terms of the GNU General Public License version 2 only, as 35.10 - * published by the Free Software Foundation. Oracle designates this 35.11 - * particular file as subject to the "Classpath" exception as provided 35.12 - * by Oracle in the LICENSE file that accompanied this code. 35.13 - * 35.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 35.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 35.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 35.17 - * version 2 for more details (a copy is included in the LICENSE file that 35.18 - * accompanied this code). 35.19 - * 35.20 - * You should have received a copy of the GNU General Public License version 35.21 - * 2 along with this work; if not, write to the Free Software Foundation, 35.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 35.23 - * 35.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 35.25 - * or visit www.oracle.com if you need additional information or have any 35.26 - * questions. 35.27 - */ 35.28 - 35.29 -package jdk.nashorn.api.scripting; 35.30 - 35.31 -@SuppressWarnings("javadoc") 35.32 -public interface VariableArityTestInterface { 35.33 - public String test1(int i, String... strings); 35.34 - public String test2(int i, String... strings); 35.35 -}
36.1 --- a/test/src/jdk/nashorn/api/scripting/Window.java Wed Apr 01 13:22:52 2015 -0700 36.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 36.3 @@ -1,86 +0,0 @@ 36.4 -/* 36.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 36.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 36.7 - * 36.8 - * This code is free software; you can redistribute it and/or modify it 36.9 - * under the terms of the GNU General Public License version 2 only, as 36.10 - * published by the Free Software Foundation. Oracle designates this 36.11 - * particular file as subject to the "Classpath" exception as provided 36.12 - * by Oracle in the LICENSE file that accompanied this code. 36.13 - * 36.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 36.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 36.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 36.17 - * version 2 for more details (a copy is included in the LICENSE file that 36.18 - * accompanied this code). 36.19 - * 36.20 - * You should have received a copy of the GNU General Public License version 36.21 - * 2 along with this work; if not, write to the Free Software Foundation, 36.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 36.23 - * 36.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 36.25 - * or visit www.oracle.com if you need additional information or have any 36.26 - * questions. 36.27 - */ 36.28 - 36.29 -package jdk.nashorn.api.scripting; 36.30 - 36.31 -import java.util.Map; 36.32 -import javax.script.Bindings; 36.33 - 36.34 -@SuppressWarnings("javadoc") 36.35 -public class Window { 36.36 - 36.37 - private String location = "http://localhost:8080/window"; 36.38 - 36.39 - private WindowEventHandler onload = null; 36.40 - 36.41 - public void alert(final String message) { 36.42 - System.out.println("alert: " + message); 36.43 - } 36.44 - 36.45 - public String getLocation() { 36.46 - return location; 36.47 - } 36.48 - 36.49 - public void setLocation(final String location) { 36.50 - this.location = location; 36.51 - } 36.52 - 36.53 - public String item(final int index) { 36.54 - return Integer.toHexString(index); 36.55 - } 36.56 - 36.57 - public WindowEventHandler getOnload() { 36.58 - return onload; 36.59 - } 36.60 - 36.61 - public void setOnload(final WindowEventHandler onload) { 36.62 - this.onload = onload; 36.63 - } 36.64 - 36.65 - public static int setTimeout(final Window self, final String code, final int delay) { 36.66 - return self.setTimeout(code, delay); 36.67 - } 36.68 - 36.69 - public int setTimeout(final String code, final int delay) { 36.70 - System.out.println("window.setTimeout: " + delay + ", code: " + code); 36.71 - return 0; 36.72 - } 36.73 - 36.74 - public static Object funcJSObject(final JSObject jsobj) { 36.75 - return jsobj.getMember("foo"); 36.76 - } 36.77 - 36.78 - public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) { 36.79 - return sobj.get("foo"); 36.80 - } 36.81 - 36.82 - public static Object funcMap(final Map<?,?> map) { 36.83 - return map.get("foo"); 36.84 - } 36.85 - 36.86 - public static Object funcBindings(final Bindings bindings) { 36.87 - return bindings.get("foo"); 36.88 - } 36.89 -}
37.1 --- a/test/src/jdk/nashorn/api/scripting/WindowEventHandler.java Wed Apr 01 13:22:52 2015 -0700 37.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 37.3 @@ -1,31 +0,0 @@ 37.4 -/* 37.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 37.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 37.7 - * 37.8 - * This code is free software; you can redistribute it and/or modify it 37.9 - * under the terms of the GNU General Public License version 2 only, as 37.10 - * published by the Free Software Foundation. Oracle designates this 37.11 - * particular file as subject to the "Classpath" exception as provided 37.12 - * by Oracle in the LICENSE file that accompanied this code. 37.13 - * 37.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 37.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 37.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 37.17 - * version 2 for more details (a copy is included in the LICENSE file that 37.18 - * accompanied this code). 37.19 - * 37.20 - * You should have received a copy of the GNU General Public License version 37.21 - * 2 along with this work; if not, write to the Free Software Foundation, 37.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 37.23 - * 37.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 37.25 - * or visit www.oracle.com if you need additional information or have any 37.26 - * questions. 37.27 - */ 37.28 - 37.29 -package jdk.nashorn.api.scripting; 37.30 - 37.31 -@SuppressWarnings("javadoc") 37.32 -public interface WindowEventHandler { 37.33 - public boolean loaded(); 37.34 -}
38.1 --- a/test/src/jdk/nashorn/api/scripting/resources/func.js Wed Apr 01 13:22:52 2015 -0700 38.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 38.3 @@ -1,42 +0,0 @@ 38.4 -/* 38.5 - * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 38.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 38.7 - * 38.8 - * This code is free software; you can redistribute it and/or modify it 38.9 - * under the terms of the GNU General Public License version 2 only, as 38.10 - * published by the Free Software Foundation. Oracle designates this 38.11 - * particular file as subject to the "Classpath" exception as provided 38.12 - * by Oracle in the LICENSE file that accompanied this code. 38.13 - * 38.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 38.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 38.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 38.17 - * version 2 for more details (a copy is included in the LICENSE file that 38.18 - * accompanied this code). 38.19 - * 38.20 - * You should have received a copy of the GNU General Public License version 38.21 - * 2 along with this work; if not, write to the Free Software Foundation, 38.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 38.23 - * 38.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 38.25 - * or visit www.oracle.com if you need additional information or have any 38.26 - * questions. 38.27 - */ 38.28 - 38.29 -// This script is loaded from jdk.nashorn.api.scripting.ScopeTest to test script class sharing and reuse. 38.30 - 38.31 -var scopeVar = 1; 38.32 -var global = this; 38.33 -undefGlobal = this; 38.34 - 38.35 -function scopeTest() { 38.36 - if (this !== global) { 38.37 - throw new Error("this !== global"); 38.38 - } 38.39 - if (this !== undefGlobal) { 38.40 - throw new Error("this !== undefinedGlobal") 38.41 - } 38.42 - return scopeVar; 38.43 -} 38.44 - 38.45 -scopeTest();
39.1 --- a/test/src/jdk/nashorn/api/scripting/resources/gettersetter.js Wed Apr 01 13:22:52 2015 -0700 39.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 39.3 @@ -1,38 +0,0 @@ 39.4 -/* 39.5 - * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 39.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 39.7 - * 39.8 - * This code is free software; you can redistribute it and/or modify it 39.9 - * under the terms of the GNU General Public License version 2 only, as 39.10 - * published by the Free Software Foundation. Oracle designates this 39.11 - * particular file as subject to the "Classpath" exception as provided 39.12 - * by Oracle in the LICENSE file that accompanied this code. 39.13 - * 39.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 39.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 39.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 39.17 - * version 2 for more details (a copy is included in the LICENSE file that 39.18 - * accompanied this code). 39.19 - * 39.20 - * You should have received a copy of the GNU General Public License version 39.21 - * 2 along with this work; if not, write to the Free Software Foundation, 39.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 39.23 - * 39.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 39.25 - * or visit www.oracle.com if you need additional information or have any 39.26 - * questions. 39.27 - */ 39.28 - 39.29 -// This script is loaded from jdk.nashorn.api.scripting.ScopeTest to test script class sharing and reuse. 39.30 - 39.31 -var v; 39.32 - 39.33 -Object.defineProperty(this, "accessor1", { 39.34 - get: function() { return v; }, 39.35 - set: function(n) { v = n; } 39.36 -}); 39.37 - 39.38 -Object.defineProperty(this, "accessor2", { 39.39 - get: function() { return x; }, 39.40 - set: function(n) { x = n; } 39.41 -});
40.1 --- a/test/src/jdk/nashorn/api/scripting/resources/witheval.js Wed Apr 01 13:22:52 2015 -0700 40.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 40.3 @@ -1,60 +0,0 @@ 40.4 -/* 40.5 - * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 40.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 40.7 - * 40.8 - * This code is free software; you can redistribute it and/or modify it 40.9 - * under the terms of the GNU General Public License version 2 only, as 40.10 - * published by the Free Software Foundation. Oracle designates this 40.11 - * particular file as subject to the "Classpath" exception as provided 40.12 - * by Oracle in the LICENSE file that accompanied this code. 40.13 - * 40.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 40.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 40.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 40.17 - * version 2 for more details (a copy is included in the LICENSE file that 40.18 - * accompanied this code). 40.19 - * 40.20 - * You should have received a copy of the GNU General Public License version 40.21 - * 2 along with this work; if not, write to the Free Software Foundation, 40.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 40.23 - * 40.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 40.25 - * or visit www.oracle.com if you need additional information or have any 40.26 - * questions. 40.27 - */ 40.28 - 40.29 -// This script is loaded from jdk.nashorn.api.scripting.ScopeTest to test script class sharing and reuse. 40.30 - 40.31 -var a; 40.32 - 40.33 -function outer(p, e) { 40.34 - eval(e); 40.35 - with(p) { 40.36 - function inner() { 40.37 - a = 1; 40.38 - c = 10; 40.39 - if (a !== 1) { 40.40 - throw new Error("a !== 1"); 40.41 - } 40.42 - if (b !== 3) { 40.43 - throw new Error("b !== 3"); 40.44 - } 40.45 - if (c !== 10) { 40.46 - throw new Error("c !== 10"); 40.47 - } 40.48 - } 40.49 - inner(); 40.50 - } 40.51 -} 40.52 - 40.53 -outer({}, "b = 3;"); 40.54 - 40.55 -if (a !== 1) { 40.56 - throw new Error("a !== 1"); 40.57 -} 40.58 -if (b !== 3) { 40.59 - throw new Error("b !== 3"); 40.60 -} 40.61 -if (c !== 10) { 40.62 - throw new Error("c !== 10"); 40.63 -}
41.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 41.2 +++ b/test/src/jdk/nashorn/api/scripting/test/InvocableTest.java Mon Apr 06 16:18:54 2015 +0530 41.3 @@ -0,0 +1,539 @@ 41.4 +/* 41.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 41.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41.7 + * 41.8 + * This code is free software; you can redistribute it and/or modify it 41.9 + * under the terms of the GNU General Public License version 2 only, as 41.10 + * published by the Free Software Foundation. Oracle designates this 41.11 + * particular file as subject to the "Classpath" exception as provided 41.12 + * by Oracle in the LICENSE file that accompanied this code. 41.13 + * 41.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 41.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 41.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 41.17 + * version 2 for more details (a copy is included in the LICENSE file that 41.18 + * accompanied this code). 41.19 + * 41.20 + * You should have received a copy of the GNU General Public License version 41.21 + * 2 along with this work; if not, write to the Free Software Foundation, 41.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 41.23 + * 41.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 41.25 + * or visit www.oracle.com if you need additional information or have any 41.26 + * questions. 41.27 + */ 41.28 + 41.29 +package jdk.nashorn.api.scripting.test; 41.30 + 41.31 +import static org.testng.Assert.assertEquals; 41.32 +import static org.testng.Assert.fail; 41.33 +import java.util.Objects; 41.34 +import java.util.function.Function; 41.35 +import javax.script.Invocable; 41.36 +import javax.script.ScriptContext; 41.37 +import javax.script.ScriptEngine; 41.38 +import javax.script.ScriptEngineManager; 41.39 +import javax.script.ScriptException; 41.40 +import javax.script.SimpleScriptContext; 41.41 +import org.testng.Assert; 41.42 +import org.testng.annotations.Test; 41.43 + 41.44 +/** 41.45 + * Tests for javax.script.Invocable implementation of nashorn. 41.46 + */ 41.47 +@SuppressWarnings("javadoc") 41.48 +public class InvocableTest { 41.49 + 41.50 + private static void log(final String msg) { 41.51 + org.testng.Reporter.log(msg, true); 41.52 + } 41.53 + 41.54 + @Test 41.55 + public void invokeMethodTest() { 41.56 + final ScriptEngineManager m = new ScriptEngineManager(); 41.57 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.58 + 41.59 + try { 41.60 + e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();"); 41.61 + final Object obj = e.get("myExample"); 41.62 + final Object res = ((Invocable) e).invokeMethod(obj, "hello"); 41.63 + assertEquals(res, "Hello World!"); 41.64 + } catch (final Exception exp) { 41.65 + exp.printStackTrace(); 41.66 + fail(exp.getMessage()); 41.67 + } 41.68 + } 41.69 + 41.70 + @Test 41.71 + /** 41.72 + * Check that we can call invokeMethod on an object that we got by 41.73 + * evaluating script with different Context set. 41.74 + */ 41.75 + public void invokeMethodDifferentContextTest() { 41.76 + final ScriptEngineManager m = new ScriptEngineManager(); 41.77 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.78 + 41.79 + try { 41.80 + // define an object with method on it 41.81 + final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); 41.82 + 41.83 + final ScriptContext ctxt = new SimpleScriptContext(); 41.84 + ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 41.85 + e.setContext(ctxt); 41.86 + 41.87 + // invoke 'func' on obj - but with current script context changed 41.88 + final Object res = ((Invocable) e).invokeMethod(obj, "hello"); 41.89 + assertEquals(res, "Hello World!"); 41.90 + } catch (final Exception exp) { 41.91 + exp.printStackTrace(); 41.92 + fail(exp.getMessage()); 41.93 + } 41.94 + } 41.95 + 41.96 + @Test 41.97 + /** 41.98 + * Check that invokeMethod throws NPE on null method name. 41.99 + */ 41.100 + public void invokeMethodNullNameTest() { 41.101 + final ScriptEngineManager m = new ScriptEngineManager(); 41.102 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.103 + 41.104 + try { 41.105 + final Object obj = e.eval("({})"); 41.106 + ((Invocable) e).invokeMethod(obj, null); 41.107 + fail("should have thrown NPE"); 41.108 + } catch (final Exception exp) { 41.109 + if (!(exp instanceof NullPointerException)) { 41.110 + exp.printStackTrace(); 41.111 + fail(exp.getMessage()); 41.112 + } 41.113 + } 41.114 + } 41.115 + 41.116 + @Test 41.117 + /** 41.118 + * Check that invokeMethod throws NoSuchMethodException on missing method. 41.119 + */ 41.120 + public void invokeMethodMissingTest() { 41.121 + final ScriptEngineManager m = new ScriptEngineManager(); 41.122 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.123 + 41.124 + try { 41.125 + final Object obj = e.eval("({})"); 41.126 + ((Invocable) e).invokeMethod(obj, "nonExistentMethod"); 41.127 + fail("should have thrown NoSuchMethodException"); 41.128 + } catch (final Exception exp) { 41.129 + if (!(exp instanceof NoSuchMethodException)) { 41.130 + exp.printStackTrace(); 41.131 + fail(exp.getMessage()); 41.132 + } 41.133 + } 41.134 + } 41.135 + 41.136 + @Test 41.137 + /** 41.138 + * Check that calling method on non-script object 'thiz' results in 41.139 + * IllegalArgumentException. 41.140 + */ 41.141 + public void invokeMethodNonScriptObjectThizTest() { 41.142 + final ScriptEngineManager m = new ScriptEngineManager(); 41.143 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.144 + 41.145 + try { 41.146 + ((Invocable) e).invokeMethod(new Object(), "toString"); 41.147 + fail("should have thrown IllegalArgumentException"); 41.148 + } catch (final Exception exp) { 41.149 + if (!(exp instanceof IllegalArgumentException)) { 41.150 + exp.printStackTrace(); 41.151 + fail(exp.getMessage()); 41.152 + } 41.153 + } 41.154 + } 41.155 + 41.156 + @Test 41.157 + /** 41.158 + * Check that calling method on null 'thiz' results in 41.159 + * IllegalArgumentException. 41.160 + */ 41.161 + public void invokeMethodNullThizTest() { 41.162 + final ScriptEngineManager m = new ScriptEngineManager(); 41.163 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.164 + 41.165 + try { 41.166 + ((Invocable) e).invokeMethod(null, "toString"); 41.167 + fail("should have thrown IllegalArgumentException"); 41.168 + } catch (final Exception exp) { 41.169 + if (!(exp instanceof IllegalArgumentException)) { 41.170 + exp.printStackTrace(); 41.171 + fail(exp.getMessage()); 41.172 + } 41.173 + } 41.174 + } 41.175 + 41.176 + @Test 41.177 + /** 41.178 + * Check that calling method on mirror created by another engine results in 41.179 + * IllegalArgumentException. 41.180 + */ 41.181 + public void invokeMethodMixEnginesTest() { 41.182 + final ScriptEngineManager m = new ScriptEngineManager(); 41.183 + final ScriptEngine engine1 = m.getEngineByName("nashorn"); 41.184 + final ScriptEngine engine2 = m.getEngineByName("nashorn"); 41.185 + 41.186 + try { 41.187 + final Object obj = engine1.eval("({ run: function() {} })"); 41.188 + // pass object from engine1 to engine2 as 'thiz' for invokeMethod 41.189 + ((Invocable) engine2).invokeMethod(obj, "run"); 41.190 + fail("should have thrown IllegalArgumentException"); 41.191 + } catch (final Exception exp) { 41.192 + if (!(exp instanceof IllegalArgumentException)) { 41.193 + exp.printStackTrace(); 41.194 + fail(exp.getMessage()); 41.195 + } 41.196 + } 41.197 + } 41.198 + 41.199 + @Test 41.200 + public void getInterfaceTest() { 41.201 + final ScriptEngineManager m = new ScriptEngineManager(); 41.202 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.203 + final Invocable inv = (Invocable) e; 41.204 + 41.205 + // try to get interface from global functions 41.206 + try { 41.207 + e.eval("function run() { print('run'); };"); 41.208 + final Runnable runnable = inv.getInterface(Runnable.class); 41.209 + runnable.run(); 41.210 + } catch (final Exception exp) { 41.211 + exp.printStackTrace(); 41.212 + fail(exp.getMessage()); 41.213 + } 41.214 + 41.215 + // try interface on specific script object 41.216 + try { 41.217 + e.eval("var obj = { run: function() { print('run from obj'); } };"); 41.218 + final Object obj = e.get("obj"); 41.219 + final Runnable runnable = inv.getInterface(obj, Runnable.class); 41.220 + runnable.run(); 41.221 + } catch (final Exception exp) { 41.222 + exp.printStackTrace(); 41.223 + fail(exp.getMessage()); 41.224 + } 41.225 + } 41.226 + 41.227 + public interface Foo { 41.228 + 41.229 + public void bar(); 41.230 + } 41.231 + 41.232 + public interface Foo2 extends Foo { 41.233 + 41.234 + public void bar2(); 41.235 + } 41.236 + 41.237 + @Test 41.238 + public void getInterfaceMissingTest() { 41.239 + final ScriptEngineManager manager = new ScriptEngineManager(); 41.240 + final ScriptEngine engine = manager.getEngineByName("nashorn"); 41.241 + 41.242 + // don't define any function. 41.243 + try { 41.244 + engine.eval(""); 41.245 + } catch (final Exception exp) { 41.246 + exp.printStackTrace(); 41.247 + fail(exp.getMessage()); 41.248 + } 41.249 + 41.250 + Runnable runnable = ((Invocable) engine).getInterface(Runnable.class); 41.251 + if (runnable != null) { 41.252 + fail("runnable is not null!"); 41.253 + } 41.254 + 41.255 + // now define "run" 41.256 + try { 41.257 + engine.eval("function run() { print('this is run function'); }"); 41.258 + } catch (final Exception exp) { 41.259 + exp.printStackTrace(); 41.260 + fail(exp.getMessage()); 41.261 + } 41.262 + runnable = ((Invocable) engine).getInterface(Runnable.class); 41.263 + // should not return null now! 41.264 + runnable.run(); 41.265 + 41.266 + // define only one method of "Foo2" 41.267 + try { 41.268 + engine.eval("function bar() { print('bar function'); }"); 41.269 + } catch (final Exception exp) { 41.270 + exp.printStackTrace(); 41.271 + fail(exp.getMessage()); 41.272 + } 41.273 + 41.274 + Foo2 foo2 = ((Invocable) engine).getInterface(Foo2.class); 41.275 + if (foo2 != null) { 41.276 + throw new RuntimeException("foo2 is not null!"); 41.277 + } 41.278 + 41.279 + // now define other method of "Foo2" 41.280 + try { 41.281 + engine.eval("function bar2() { print('bar2 function'); }"); 41.282 + } catch (final Exception exp) { 41.283 + exp.printStackTrace(); 41.284 + fail(exp.getMessage()); 41.285 + } 41.286 + foo2 = ((Invocable) engine).getInterface(Foo2.class); 41.287 + foo2.bar(); 41.288 + foo2.bar2(); 41.289 + } 41.290 + 41.291 + @Test 41.292 + /** 41.293 + * Try passing non-interface Class object for interface implementation. 41.294 + */ 41.295 + public void getNonInterfaceGetInterfaceTest() { 41.296 + final ScriptEngineManager manager = new ScriptEngineManager(); 41.297 + final ScriptEngine engine = manager.getEngineByName("nashorn"); 41.298 + try { 41.299 + log(Objects.toString(((Invocable) engine).getInterface(Object.class))); 41.300 + fail("Should have thrown IllegalArgumentException"); 41.301 + } catch (final Exception exp) { 41.302 + if (!(exp instanceof IllegalArgumentException)) { 41.303 + fail("IllegalArgumentException expected, got " + exp); 41.304 + } 41.305 + } 41.306 + } 41.307 + 41.308 + @Test 41.309 + /** 41.310 + * Check that we can get interface out of a script object even after 41.311 + * switching to use different ScriptContext. 41.312 + */ 41.313 + public void getInterfaceDifferentContext() { 41.314 + final ScriptEngineManager m = new ScriptEngineManager(); 41.315 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.316 + try { 41.317 + final Object obj = e.eval("({ run: function() { } })"); 41.318 + 41.319 + // change script context 41.320 + final ScriptContext ctxt = new SimpleScriptContext(); 41.321 + ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 41.322 + e.setContext(ctxt); 41.323 + 41.324 + final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); 41.325 + r.run(); 41.326 + } catch (final Exception exp) { 41.327 + exp.printStackTrace(); 41.328 + fail(exp.getMessage()); 41.329 + } 41.330 + } 41.331 + 41.332 + @Test 41.333 + /** 41.334 + * Check that getInterface on non-script object 'thiz' results in 41.335 + * IllegalArgumentException. 41.336 + */ 41.337 + public void getInterfaceNonScriptObjectThizTest() { 41.338 + final ScriptEngineManager m = new ScriptEngineManager(); 41.339 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.340 + 41.341 + try { 41.342 + ((Invocable) e).getInterface(new Object(), Runnable.class); 41.343 + fail("should have thrown IllegalArgumentException"); 41.344 + } catch (final Exception exp) { 41.345 + if (!(exp instanceof IllegalArgumentException)) { 41.346 + exp.printStackTrace(); 41.347 + fail(exp.getMessage()); 41.348 + } 41.349 + } 41.350 + } 41.351 + 41.352 + @Test 41.353 + /** 41.354 + * Check that getInterface on null 'thiz' results in 41.355 + * IllegalArgumentException. 41.356 + */ 41.357 + public void getInterfaceNullThizTest() { 41.358 + final ScriptEngineManager m = new ScriptEngineManager(); 41.359 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.360 + 41.361 + try { 41.362 + ((Invocable) e).getInterface(null, Runnable.class); 41.363 + fail("should have thrown IllegalArgumentException"); 41.364 + } catch (final Exception exp) { 41.365 + if (!(exp instanceof IllegalArgumentException)) { 41.366 + exp.printStackTrace(); 41.367 + fail(exp.getMessage()); 41.368 + } 41.369 + } 41.370 + } 41.371 + 41.372 + @Test 41.373 + /** 41.374 + * Check that calling getInterface on mirror created by another engine 41.375 + * results in IllegalArgumentException. 41.376 + */ 41.377 + public void getInterfaceMixEnginesTest() { 41.378 + final ScriptEngineManager m = new ScriptEngineManager(); 41.379 + final ScriptEngine engine1 = m.getEngineByName("nashorn"); 41.380 + final ScriptEngine engine2 = m.getEngineByName("nashorn"); 41.381 + 41.382 + try { 41.383 + final Object obj = engine1.eval("({ run: function() {} })"); 41.384 + // pass object from engine1 to engine2 as 'thiz' for getInterface 41.385 + ((Invocable) engine2).getInterface(obj, Runnable.class); 41.386 + fail("should have thrown IllegalArgumentException"); 41.387 + } catch (final Exception exp) { 41.388 + if (!(exp instanceof IllegalArgumentException)) { 41.389 + exp.printStackTrace(); 41.390 + fail(exp.getMessage()); 41.391 + } 41.392 + } 41.393 + } 41.394 + 41.395 + @Test 41.396 + /** 41.397 + * check that null function name results in NPE. 41.398 + */ 41.399 + public void invokeFunctionNullNameTest() { 41.400 + final ScriptEngineManager m = new ScriptEngineManager(); 41.401 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.402 + 41.403 + try { 41.404 + ((Invocable)e).invokeFunction(null); 41.405 + fail("should have thrown NPE"); 41.406 + } catch (final Exception exp) { 41.407 + if (!(exp instanceof NullPointerException)) { 41.408 + exp.printStackTrace(); 41.409 + fail(exp.getMessage()); 41.410 + } 41.411 + } 41.412 + } 41.413 + 41.414 + @Test 41.415 + /** 41.416 + * Check that attempt to call missing function results in 41.417 + * NoSuchMethodException. 41.418 + */ 41.419 + public void invokeFunctionMissingTest() { 41.420 + final ScriptEngineManager m = new ScriptEngineManager(); 41.421 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.422 + 41.423 + try { 41.424 + ((Invocable)e).invokeFunction("NonExistentFunc"); 41.425 + fail("should have thrown NoSuchMethodException"); 41.426 + } catch (final Exception exp) { 41.427 + if (!(exp instanceof NoSuchMethodException)) { 41.428 + exp.printStackTrace(); 41.429 + fail(exp.getMessage()); 41.430 + } 41.431 + } 41.432 + } 41.433 + 41.434 + @Test 41.435 + /** 41.436 + * Check that invokeFunction calls functions only from current context's 41.437 + * Bindings. 41.438 + */ 41.439 + public void invokeFunctionDifferentContextTest() { 41.440 + final ScriptEngineManager m = new ScriptEngineManager(); 41.441 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.442 + 41.443 + try { 41.444 + // define an object with method on it 41.445 + e.eval("function hello() { return 'Hello World!'; }"); 41.446 + final ScriptContext ctxt = new SimpleScriptContext(); 41.447 + ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 41.448 + // change engine's current context 41.449 + e.setContext(ctxt); 41.450 + 41.451 + ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context! 41.452 + fail("should have thrown NoSuchMethodException"); 41.453 + } catch (final Exception exp) { 41.454 + if (!(exp instanceof NoSuchMethodException)) { 41.455 + exp.printStackTrace(); 41.456 + fail(exp.getMessage()); 41.457 + } 41.458 + } 41.459 + } 41.460 + 41.461 + @Test 41.462 + public void invokeFunctionExceptionTest() { 41.463 + final ScriptEngineManager m = new ScriptEngineManager(); 41.464 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.465 + try { 41.466 + e.eval("function func() { throw new TypeError(); }"); 41.467 + } catch (final Throwable t) { 41.468 + t.printStackTrace(); 41.469 + fail(t.getMessage()); 41.470 + } 41.471 + 41.472 + try { 41.473 + ((Invocable) e).invokeFunction("func"); 41.474 + fail("should have thrown exception"); 41.475 + } catch (final ScriptException se) { 41.476 + // ECMA TypeError property wrapped as a ScriptException 41.477 + log("got " + se + " as expected"); 41.478 + } catch (final Throwable t) { 41.479 + t.printStackTrace(); 41.480 + fail(t.getMessage()); 41.481 + } 41.482 + } 41.483 + 41.484 + @Test 41.485 + public void invokeMethodExceptionTest() { 41.486 + final ScriptEngineManager m = new ScriptEngineManager(); 41.487 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.488 + try { 41.489 + e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }"); 41.490 + } catch (final Throwable t) { 41.491 + t.printStackTrace(); 41.492 + fail(t.getMessage()); 41.493 + } 41.494 + 41.495 + try { 41.496 + final Object sobj = e.get("sobj"); 41.497 + ((Invocable) e).invokeMethod(sobj, "foo"); 41.498 + fail("should have thrown exception"); 41.499 + } catch (final ScriptException se) { 41.500 + // ECMA TypeError property wrapped as a ScriptException 41.501 + log("got " + se + " as expected"); 41.502 + } catch (final Throwable t) { 41.503 + t.printStackTrace(); 41.504 + fail(t.getMessage()); 41.505 + } 41.506 + } 41.507 + 41.508 + @Test 41.509 + /** 41.510 + * Tests whether invocation of a JavaScript method through a variable arity 41.511 + * Java method will pass the vararg array. Both non-vararg and vararg 41.512 + * JavaScript methods are tested. 41.513 + * 41.514 + * @throws ScriptException 41.515 + */ 41.516 + public void variableArityInterfaceTest() throws ScriptException { 41.517 + final ScriptEngineManager m = new ScriptEngineManager(); 41.518 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.519 + e.eval( 41.520 + "function test1(i, strings) {" 41.521 + + " return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)" 41.522 + + "}" 41.523 + + "function test2() {" 41.524 + + " return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])" 41.525 + + "}"); 41.526 + final VariableArityTestInterface itf = ((Invocable) e).getInterface(VariableArityTestInterface.class); 41.527 + Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]"); 41.528 + Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]"); 41.529 + } 41.530 + 41.531 + @Test 41.532 + public void defaultMethodTest() throws ScriptException { 41.533 + final ScriptEngineManager m = new ScriptEngineManager(); 41.534 + final ScriptEngine e = m.getEngineByName("nashorn"); 41.535 + final Invocable inv = (Invocable) e; 41.536 + 41.537 + final Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})"); 41.538 + @SuppressWarnings("unchecked") 41.539 + final Function<String, String> func = inv.getInterface(obj, Function.class); 41.540 + assertEquals(func.apply("hello"), "HELLO"); 41.541 + } 41.542 +}
42.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 42.2 +++ b/test/src/jdk/nashorn/api/scripting/test/MultipleEngineTest.java Mon Apr 06 16:18:54 2015 +0530 42.3 @@ -0,0 +1,52 @@ 42.4 +/* 42.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 42.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 42.7 + * 42.8 + * This code is free software; you can redistribute it and/or modify it 42.9 + * under the terms of the GNU General Public License version 2 only, as 42.10 + * published by the Free Software Foundation. Oracle designates this 42.11 + * particular file as subject to the "Classpath" exception as provided 42.12 + * by Oracle in the LICENSE file that accompanied this code. 42.13 + * 42.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 42.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 42.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 42.17 + * version 2 for more details (a copy is included in the LICENSE file that 42.18 + * accompanied this code). 42.19 + * 42.20 + * You should have received a copy of the GNU General Public License version 42.21 + * 2 along with this work; if not, write to the Free Software Foundation, 42.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 42.23 + * 42.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 42.25 + * or visit www.oracle.com if you need additional information or have any 42.26 + * questions. 42.27 + */ 42.28 + 42.29 +package jdk.nashorn.api.scripting.test; 42.30 + 42.31 +import javax.script.ScriptEngine; 42.32 +import javax.script.ScriptEngineManager; 42.33 +import javax.script.ScriptException; 42.34 +import org.testng.annotations.Test; 42.35 + 42.36 +/** 42.37 + * Test that we can create multiple, independent script engines and use those 42.38 + * independently. 42.39 + * 42.40 + * @test 42.41 + * @run testng jdk.nashorn.api.scripting.MultipleEngineTest 42.42 + */ 42.43 +@SuppressWarnings("javadoc") 42.44 +public class MultipleEngineTest { 42.45 + @Test 42.46 + public void createAndUseManyEngine() throws ScriptException { 42.47 + final ScriptEngineManager m = new ScriptEngineManager(); 42.48 + 42.49 + final ScriptEngine e1 = m.getEngineByName("nashorn"); 42.50 + e1.eval("var x = 33; print(x);"); 42.51 + 42.52 + final ScriptEngine e2 = m.getEngineByName("nashorn"); 42.53 + e2.eval("try { print(x) } catch(e) { print(e); }"); 42.54 + } 42.55 +}
43.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 43.2 +++ b/test/src/jdk/nashorn/api/scripting/test/PluggableJSObjectTest.java Mon Apr 06 16:18:54 2015 +0530 43.3 @@ -0,0 +1,289 @@ 43.4 +/* 43.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 43.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 43.7 + * 43.8 + * This code is free software; you can redistribute it and/or modify it 43.9 + * under the terms of the GNU General Public License version 2 only, as 43.10 + * published by the Free Software Foundation. Oracle designates this 43.11 + * particular file as subject to the "Classpath" exception as provided 43.12 + * by Oracle in the LICENSE file that accompanied this code. 43.13 + * 43.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 43.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 43.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 43.17 + * version 2 for more details (a copy is included in the LICENSE file that 43.18 + * accompanied this code). 43.19 + * 43.20 + * You should have received a copy of the GNU General Public License version 43.21 + * 2 along with this work; if not, write to the Free Software Foundation, 43.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 43.23 + * 43.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 43.25 + * or visit www.oracle.com if you need additional information or have any 43.26 + * questions. 43.27 + */ 43.28 + 43.29 +package jdk.nashorn.api.scripting.test; 43.30 + 43.31 +import static org.testng.Assert.assertEquals; 43.32 +import static org.testng.Assert.assertFalse; 43.33 +import static org.testng.Assert.fail; 43.34 + 43.35 +import java.nio.IntBuffer; 43.36 +import java.util.Collection; 43.37 +import java.util.HashMap; 43.38 +import java.util.LinkedHashMap; 43.39 +import java.util.Set; 43.40 +import javax.script.ScriptEngine; 43.41 +import javax.script.ScriptEngineManager; 43.42 +import jdk.nashorn.api.scripting.AbstractJSObject; 43.43 +import org.testng.annotations.Test; 43.44 + 43.45 +/** 43.46 + * Tests for pluggable external impls. of jdk.nashorn.api.scripting.JSObject. 43.47 + * 43.48 + * JDK-8024615: Refactor ScriptObjectMirror and JSObject to support external 43.49 + * JSObject implementations. 43.50 + */ 43.51 +@SuppressWarnings("javadoc") 43.52 +public class PluggableJSObjectTest { 43.53 + public static class MapWrapperObject extends AbstractJSObject { 43.54 + private final HashMap<String, Object> map = new LinkedHashMap<>(); 43.55 + 43.56 + public HashMap<String, Object> getMap() { 43.57 + return map; 43.58 + } 43.59 + 43.60 + @Override 43.61 + public Object getMember(final String name) { 43.62 + return map.get(name); 43.63 + } 43.64 + 43.65 + @Override 43.66 + public void setMember(final String name, final Object value) { 43.67 + map.put(name, value); 43.68 + } 43.69 + 43.70 + @Override 43.71 + public boolean hasMember(final String name) { 43.72 + return map.containsKey(name); 43.73 + } 43.74 + 43.75 + @Override 43.76 + public void removeMember(final String name) { 43.77 + map.remove(name); 43.78 + } 43.79 + 43.80 + @Override 43.81 + public Set<String> keySet() { 43.82 + return map.keySet(); 43.83 + } 43.84 + 43.85 + @Override 43.86 + public Collection<Object> values() { 43.87 + return map.values(); 43.88 + } 43.89 + } 43.90 + 43.91 + @Test 43.92 + // Named property access on a JSObject 43.93 + public void namedAccessTest() { 43.94 + final ScriptEngineManager m = new ScriptEngineManager(); 43.95 + final ScriptEngine e = m.getEngineByName("nashorn"); 43.96 + try { 43.97 + final MapWrapperObject obj = new MapWrapperObject(); 43.98 + e.put("obj", obj); 43.99 + obj.getMap().put("foo", "bar"); 43.100 + 43.101 + // property-like access on MapWrapperObject objects 43.102 + assertEquals(e.eval("obj.foo"), "bar"); 43.103 + e.eval("obj.foo = 'hello'"); 43.104 + assertEquals(e.eval("'foo' in obj"), Boolean.TRUE); 43.105 + assertEquals(e.eval("obj.foo"), "hello"); 43.106 + assertEquals(obj.getMap().get("foo"), "hello"); 43.107 + e.eval("delete obj.foo"); 43.108 + assertFalse(obj.getMap().containsKey("foo")); 43.109 + assertEquals(e.eval("'foo' in obj"), Boolean.FALSE); 43.110 + } catch (final Exception exp) { 43.111 + exp.printStackTrace(); 43.112 + fail(exp.getMessage()); 43.113 + } 43.114 + } 43.115 + 43.116 + // @bug 8062030: Nashorn bug retrieving array property after key string concatenation 43.117 + @Test 43.118 + // ConsString attribute access on a JSObject 43.119 + public void consStringTest() { 43.120 + final ScriptEngineManager m = new ScriptEngineManager(); 43.121 + final ScriptEngine e = m.getEngineByName("nashorn"); 43.122 + try { 43.123 + final MapWrapperObject obj = new MapWrapperObject(); 43.124 + e.put("obj", obj); 43.125 + e.put("f", "f"); 43.126 + e.eval("obj[f + 'oo'] = 'bar';"); 43.127 + 43.128 + assertEquals(obj.getMap().get("foo"), "bar"); 43.129 + assertEquals(e.eval("obj[f + 'oo']"), "bar"); 43.130 + assertEquals(e.eval("obj['foo']"), "bar"); 43.131 + assertEquals(e.eval("f + 'oo' in obj"), Boolean.TRUE); 43.132 + assertEquals(e.eval("'foo' in obj"), Boolean.TRUE); 43.133 + e.eval("delete obj[f + 'oo']"); 43.134 + assertFalse(obj.getMap().containsKey("foo")); 43.135 + assertEquals(e.eval("obj[f + 'oo']"), null); 43.136 + assertEquals(e.eval("obj['foo']"), null); 43.137 + assertEquals(e.eval("f + 'oo' in obj"), Boolean.FALSE); 43.138 + assertEquals(e.eval("'foo' in obj"), Boolean.FALSE); 43.139 + } catch (final Exception exp) { 43.140 + exp.printStackTrace(); 43.141 + fail(exp.getMessage()); 43.142 + } 43.143 + } 43.144 + 43.145 + public static class BufferObject extends AbstractJSObject { 43.146 + private final IntBuffer buf; 43.147 + 43.148 + public BufferObject(final int size) { 43.149 + buf = IntBuffer.allocate(size); 43.150 + } 43.151 + 43.152 + public IntBuffer getBuffer() { 43.153 + return buf; 43.154 + } 43.155 + 43.156 + @Override 43.157 + public Object getMember(final String name) { 43.158 + return name.equals("length")? buf.capacity() : null; 43.159 + } 43.160 + 43.161 + @Override 43.162 + public boolean hasSlot(final int i) { 43.163 + return i > -1 && i < buf.capacity(); 43.164 + } 43.165 + 43.166 + @Override 43.167 + public Object getSlot(final int i) { 43.168 + return buf.get(i); 43.169 + } 43.170 + 43.171 + @Override 43.172 + public void setSlot(final int i, final Object value) { 43.173 + buf.put(i, ((Number)value).intValue()); 43.174 + } 43.175 + 43.176 + @Override 43.177 + public boolean isArray() { 43.178 + return true; 43.179 + } 43.180 + } 43.181 + 43.182 + @Test 43.183 + // array-like indexed access for a JSObject 43.184 + public void indexedAccessTest() { 43.185 + final ScriptEngineManager m = new ScriptEngineManager(); 43.186 + final ScriptEngine e = m.getEngineByName("nashorn"); 43.187 + try { 43.188 + final BufferObject buf = new BufferObject(2); 43.189 + e.put("buf", buf); 43.190 + 43.191 + // array-like access on BufferObject objects 43.192 + assertEquals(e.eval("buf.length"), buf.getBuffer().capacity()); 43.193 + e.eval("buf[0] = 23"); 43.194 + assertEquals(buf.getBuffer().get(0), 23); 43.195 + assertEquals(e.eval("buf[0]"), 23); 43.196 + assertEquals(e.eval("buf[1]"), 0); 43.197 + buf.getBuffer().put(1, 42); 43.198 + assertEquals(e.eval("buf[1]"), 42); 43.199 + assertEquals(e.eval("Array.isArray(buf)"), Boolean.TRUE); 43.200 + } catch (final Exception exp) { 43.201 + exp.printStackTrace(); 43.202 + fail(exp.getMessage()); 43.203 + } 43.204 + } 43.205 + 43.206 + public static class Adder extends AbstractJSObject { 43.207 + @Override 43.208 + public Object call(final Object thiz, final Object... args) { 43.209 + double res = 0.0; 43.210 + for (final Object arg : args) { 43.211 + res += ((Number)arg).doubleValue(); 43.212 + } 43.213 + return res; 43.214 + } 43.215 + 43.216 + @Override 43.217 + public boolean isFunction() { 43.218 + return true; 43.219 + } 43.220 + } 43.221 + 43.222 + @Test 43.223 + // a callable JSObject 43.224 + public void callableJSObjectTest() { 43.225 + final ScriptEngineManager m = new ScriptEngineManager(); 43.226 + final ScriptEngine e = m.getEngineByName("nashorn"); 43.227 + try { 43.228 + e.put("sum", new Adder()); 43.229 + // check callability of Adder objects 43.230 + assertEquals(e.eval("typeof sum"), "function"); 43.231 + assertEquals(((Number)e.eval("sum(1, 2, 3, 4, 5)")).intValue(), 15); 43.232 + } catch (final Exception exp) { 43.233 + exp.printStackTrace(); 43.234 + fail(exp.getMessage()); 43.235 + } 43.236 + } 43.237 + 43.238 + public static class Factory extends AbstractJSObject { 43.239 + @SuppressWarnings("unused") 43.240 + @Override 43.241 + public Object newObject(final Object... args) { 43.242 + return new HashMap<Object, Object>(); 43.243 + } 43.244 + 43.245 + @Override 43.246 + public boolean isFunction() { 43.247 + return true; 43.248 + } 43.249 + } 43.250 + 43.251 + @Test 43.252 + // a factory JSObject 43.253 + public void factoryJSObjectTest() { 43.254 + final ScriptEngineManager m = new ScriptEngineManager(); 43.255 + final ScriptEngine e = m.getEngineByName("nashorn"); 43.256 + try { 43.257 + e.put("Factory", new Factory()); 43.258 + 43.259 + // check new on Factory 43.260 + assertEquals(e.eval("typeof Factory"), "function"); 43.261 + assertEquals(e.eval("typeof new Factory()"), "object"); 43.262 + assertEquals(e.eval("(new Factory()) instanceof java.util.Map"), Boolean.TRUE); 43.263 + } catch (final Exception exp) { 43.264 + exp.printStackTrace(); 43.265 + fail(exp.getMessage()); 43.266 + } 43.267 + } 43.268 + 43.269 + @Test 43.270 + // iteration tests 43.271 + public void iteratingJSObjectTest() { 43.272 + final ScriptEngineManager m = new ScriptEngineManager(); 43.273 + final ScriptEngine e = m.getEngineByName("nashorn"); 43.274 + try { 43.275 + final MapWrapperObject obj = new MapWrapperObject(); 43.276 + obj.setMember("foo", "hello"); 43.277 + obj.setMember("bar", "world"); 43.278 + e.put("obj", obj); 43.279 + 43.280 + // check for..in 43.281 + Object val = e.eval("var str = ''; for (i in obj) str += i; str"); 43.282 + assertEquals(val.toString(), "foobar"); 43.283 + 43.284 + // check for..each..in 43.285 + val = e.eval("var str = ''; for each (i in obj) str += i; str"); 43.286 + assertEquals(val.toString(), "helloworld"); 43.287 + } catch (final Exception exp) { 43.288 + exp.printStackTrace(); 43.289 + fail(exp.getMessage()); 43.290 + } 43.291 + } 43.292 +}
44.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 44.2 +++ b/test/src/jdk/nashorn/api/scripting/test/ScopeTest.java Mon Apr 06 16:18:54 2015 +0530 44.3 @@ -0,0 +1,781 @@ 44.4 +/* 44.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 44.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 44.7 + * 44.8 + * This code is free software; you can redistribute it and/or modify it 44.9 + * under the terms of the GNU General Public License version 2 only, as 44.10 + * published by the Free Software Foundation. Oracle designates this 44.11 + * particular file as subject to the "Classpath" exception as provided 44.12 + * by Oracle in the LICENSE file that accompanied this code. 44.13 + * 44.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 44.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 44.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 44.17 + * version 2 for more details (a copy is included in the LICENSE file that 44.18 + * accompanied this code). 44.19 + * 44.20 + * You should have received a copy of the GNU General Public License version 44.21 + * 2 along with this work; if not, write to the Free Software Foundation, 44.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 44.23 + * 44.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 44.25 + * or visit www.oracle.com if you need additional information or have any 44.26 + * questions. 44.27 + */ 44.28 +package jdk.nashorn.api.scripting.test; 44.29 + 44.30 +import static org.testng.Assert.assertEquals; 44.31 +import static org.testng.Assert.assertNotNull; 44.32 +import static org.testng.Assert.assertTrue; 44.33 +import static org.testng.Assert.fail; 44.34 +import javax.script.Bindings; 44.35 +import javax.script.ScriptContext; 44.36 +import javax.script.ScriptEngine; 44.37 +import javax.script.ScriptEngineManager; 44.38 +import javax.script.ScriptException; 44.39 +import javax.script.SimpleBindings; 44.40 +import javax.script.SimpleScriptContext; 44.41 +import jdk.nashorn.api.scripting.ScriptObjectMirror; 44.42 +import jdk.nashorn.api.scripting.URLReader; 44.43 +import org.testng.Assert; 44.44 +import org.testng.annotations.Test; 44.45 + 44.46 +/** 44.47 + * Tests for jsr223 Bindings "scope" (engine, global scopes) 44.48 + */ 44.49 +@SuppressWarnings("javadoc") 44.50 +public class ScopeTest { 44.51 + 44.52 + @Test 44.53 + public void createBindingsTest() { 44.54 + final ScriptEngineManager m = new ScriptEngineManager(); 44.55 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.56 + final Bindings b = e.createBindings(); 44.57 + b.put("foo", 42.0); 44.58 + Object res = null; 44.59 + try { 44.60 + res = e.eval("foo == 42.0", b); 44.61 + } catch (final ScriptException | NullPointerException se) { 44.62 + se.printStackTrace(); 44.63 + fail(se.getMessage()); 44.64 + } 44.65 + 44.66 + assertEquals(res, Boolean.TRUE); 44.67 + } 44.68 + 44.69 + @Test 44.70 + public void engineScopeTest() { 44.71 + final ScriptEngineManager m = new ScriptEngineManager(); 44.72 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.73 + final Bindings engineScope = e.getBindings(ScriptContext.ENGINE_SCOPE); 44.74 + 44.75 + // check few ECMA standard built-in global properties 44.76 + assertNotNull(engineScope.get("Object")); 44.77 + assertNotNull(engineScope.get("TypeError")); 44.78 + assertNotNull(engineScope.get("eval")); 44.79 + 44.80 + // can access via ScriptEngine.get as well 44.81 + assertNotNull(e.get("Object")); 44.82 + assertNotNull(e.get("TypeError")); 44.83 + assertNotNull(e.get("eval")); 44.84 + 44.85 + // Access by either way should return same object 44.86 + assertEquals(engineScope.get("Array"), e.get("Array")); 44.87 + assertEquals(engineScope.get("EvalError"), e.get("EvalError")); 44.88 + assertEquals(engineScope.get("undefined"), e.get("undefined")); 44.89 + 44.90 + // try exposing a new variable from scope 44.91 + engineScope.put("myVar", "foo"); 44.92 + try { 44.93 + assertEquals(e.eval("myVar"), "foo"); 44.94 + } catch (final ScriptException se) { 44.95 + se.printStackTrace(); 44.96 + fail(se.getMessage()); 44.97 + } 44.98 + 44.99 + // update "myVar" in script an check the value from scope 44.100 + try { 44.101 + e.eval("myVar = 'nashorn';"); 44.102 + } catch (final ScriptException se) { 44.103 + se.printStackTrace(); 44.104 + fail(se.getMessage()); 44.105 + } 44.106 + 44.107 + // now check modified value from scope and engine 44.108 + assertEquals(engineScope.get("myVar"), "nashorn"); 44.109 + assertEquals(e.get("myVar"), "nashorn"); 44.110 + } 44.111 + 44.112 + @Test 44.113 + public void multiGlobalTest() { 44.114 + final ScriptEngineManager m = new ScriptEngineManager(); 44.115 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.116 + final Bindings b = e.createBindings(); 44.117 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.118 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.119 + 44.120 + try { 44.121 + final Object obj1 = e.eval("Object"); 44.122 + final Object obj2 = e.eval("Object", newCtxt); 44.123 + Assert.assertNotEquals(obj1, obj2); 44.124 + Assert.assertNotNull(obj1); 44.125 + Assert.assertNotNull(obj2); 44.126 + Assert.assertEquals(obj1.toString(), obj2.toString()); 44.127 + 44.128 + e.eval("x = 'hello'"); 44.129 + e.eval("x = 'world'", newCtxt); 44.130 + Object x1 = e.getContext().getAttribute("x"); 44.131 + Object x2 = newCtxt.getAttribute("x"); 44.132 + Assert.assertNotEquals(x1, x2); 44.133 + Assert.assertEquals(x1, "hello"); 44.134 + Assert.assertEquals(x2, "world"); 44.135 + 44.136 + x1 = e.eval("x"); 44.137 + x2 = e.eval("x", newCtxt); 44.138 + Assert.assertNotEquals(x1, x2); 44.139 + Assert.assertEquals(x1, "hello"); 44.140 + Assert.assertEquals(x2, "world"); 44.141 + 44.142 + final ScriptContext origCtxt = e.getContext(); 44.143 + e.setContext(newCtxt); 44.144 + e.eval("y = new Object()"); 44.145 + e.eval("y = new Object()", origCtxt); 44.146 + 44.147 + final Object y1 = origCtxt.getAttribute("y"); 44.148 + final Object y2 = newCtxt.getAttribute("y"); 44.149 + Assert.assertNotEquals(y1, y2); 44.150 + final Object yeval1 = e.eval("y"); 44.151 + final Object yeval2 = e.eval("y", origCtxt); 44.152 + Assert.assertNotEquals(yeval1, yeval2); 44.153 + Assert.assertEquals("[object Object]", y1.toString()); 44.154 + Assert.assertEquals("[object Object]", y2.toString()); 44.155 + } catch (final ScriptException se) { 44.156 + se.printStackTrace(); 44.157 + fail(se.getMessage()); 44.158 + } 44.159 + } 44.160 + 44.161 + @Test 44.162 + public void userEngineScopeBindingsTest() throws ScriptException { 44.163 + final ScriptEngineManager m = new ScriptEngineManager(); 44.164 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.165 + e.eval("function func() {}"); 44.166 + 44.167 + final ScriptContext newContext = new SimpleScriptContext(); 44.168 + newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 44.169 + // we are using a new bindings - so it should have 'func' defined 44.170 + final Object value = e.eval("typeof func", newContext); 44.171 + assertTrue(value.equals("undefined")); 44.172 + } 44.173 + 44.174 + @Test 44.175 + public void userEngineScopeBindingsNoLeakTest() throws ScriptException { 44.176 + final ScriptEngineManager m = new ScriptEngineManager(); 44.177 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.178 + final ScriptContext newContext = new SimpleScriptContext(); 44.179 + newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 44.180 + e.eval("function foo() {}", newContext); 44.181 + 44.182 + // in the default context's ENGINE_SCOPE, 'foo' shouldn't exist 44.183 + assertTrue(e.eval("typeof foo").equals("undefined")); 44.184 + } 44.185 + 44.186 + @Test 44.187 + public void userEngineScopeBindingsRetentionTest() throws ScriptException { 44.188 + final ScriptEngineManager m = new ScriptEngineManager(); 44.189 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.190 + final ScriptContext newContext = new SimpleScriptContext(); 44.191 + newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 44.192 + e.eval("function foo() {}", newContext); 44.193 + 44.194 + // definition retained with user's ENGINE_SCOPE Binding 44.195 + assertTrue(e.eval("typeof foo", newContext).equals("function")); 44.196 + 44.197 + final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE); 44.198 + // but not in another ENGINE_SCOPE binding 44.199 + newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); 44.200 + assertTrue(e.eval("typeof foo", newContext).equals("undefined")); 44.201 + 44.202 + // restore ENGINE_SCOPE and check again 44.203 + newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE); 44.204 + assertTrue(e.eval("typeof foo", newContext).equals("function")); 44.205 + } 44.206 + 44.207 + @Test 44.208 + // check that engine.js definitions are visible in all new global instances 44.209 + public void checkBuiltinsInNewBindingsTest() throws ScriptException { 44.210 + final ScriptEngineManager m = new ScriptEngineManager(); 44.211 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.212 + 44.213 + // check default global instance has engine.js definitions 44.214 + final Bindings g = (Bindings) e.eval("this"); 44.215 + Object value = g.get("__noSuchProperty__"); 44.216 + assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 44.217 + value = g.get("print"); 44.218 + assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 44.219 + 44.220 + // check new global instance created has engine.js definitions 44.221 + final Bindings b = e.createBindings(); 44.222 + value = b.get("__noSuchProperty__"); 44.223 + assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 44.224 + value = b.get("print"); 44.225 + assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); 44.226 + 44.227 + // put a mapping into GLOBAL_SCOPE 44.228 + final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); 44.229 + globalScope.put("x", "hello"); 44.230 + 44.231 + // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval 44.232 + assertTrue(e.eval("x").equals("hello")); 44.233 + 44.234 + final ScriptContext ctx = new SimpleScriptContext(); 44.235 + ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); 44.236 + ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.237 + 44.238 + // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 44.239 + assertTrue(e.eval("x", ctx).equals("hello")); 44.240 + 44.241 + // try some arbitray Bindings for ENGINE_SCOPE 44.242 + final Bindings sb = new SimpleBindings(); 44.243 + ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); 44.244 + 44.245 + // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 44.246 + assertTrue(e.eval("x", ctx).equals("hello")); 44.247 + 44.248 + // engine.js builtins are still defined even with arbitrary Bindings 44.249 + assertTrue(e.eval("typeof print", ctx).equals("function")); 44.250 + assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); 44.251 + 44.252 + // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition 44.253 + sb.put("x", "newX"); 44.254 + assertTrue(e.eval("x", ctx).equals("newX")); 44.255 + } 44.256 + 44.257 + /** 44.258 + * Test multi-threaded access to defined global variables for shared script classes with multiple globals. 44.259 + */ 44.260 + @Test 44.261 + public static void multiThreadedVarTest() throws ScriptException, InterruptedException { 44.262 + final ScriptEngineManager m = new ScriptEngineManager(); 44.263 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.264 + final Bindings b = e.createBindings(); 44.265 + final ScriptContext origContext = e.getContext(); 44.266 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.267 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.268 + final String sharedScript = "foo"; 44.269 + 44.270 + assertEquals(e.eval("var foo = 'original context';", origContext), null); 44.271 + assertEquals(e.eval("var foo = 'new context';", newCtxt), null); 44.272 + 44.273 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.274 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 44.275 + t1.start(); 44.276 + t2.start(); 44.277 + t1.join(); 44.278 + t2.join(); 44.279 + 44.280 + assertEquals(e.eval("var foo = 'newer context';", newCtxt), null); 44.281 + final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.282 + final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 44.283 + 44.284 + t3.start(); 44.285 + t4.start(); 44.286 + t3.join(); 44.287 + t4.join(); 44.288 + 44.289 + assertEquals(e.eval(sharedScript), "original context"); 44.290 + assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 44.291 + } 44.292 + 44.293 + /** 44.294 + * Test multi-threaded access to undefined global variables for shared script classes with multiple globals. 44.295 + */ 44.296 + @Test 44.297 + public static void multiThreadedGlobalTest() throws ScriptException, InterruptedException { 44.298 + final ScriptEngineManager m = new ScriptEngineManager(); 44.299 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.300 + final Bindings b = e.createBindings(); 44.301 + final ScriptContext origContext = e.getContext(); 44.302 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.303 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.304 + 44.305 + assertEquals(e.eval("foo = 'original context';", origContext), "original context"); 44.306 + assertEquals(e.eval("foo = 'new context';", newCtxt), "new context"); 44.307 + final String sharedScript = "foo"; 44.308 + 44.309 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.310 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 44.311 + t1.start(); 44.312 + t2.start(); 44.313 + t1.join(); 44.314 + t2.join(); 44.315 + 44.316 + final Object obj3 = e.eval("delete foo; foo = 'newer context';", newCtxt); 44.317 + assertEquals(obj3, "newer context"); 44.318 + final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.319 + final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 44.320 + 44.321 + t3.start(); 44.322 + t4.start(); 44.323 + t3.join(); 44.324 + t4.join(); 44.325 + 44.326 + Assert.assertEquals(e.eval(sharedScript), "original context"); 44.327 + Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 44.328 + } 44.329 + 44.330 + /** 44.331 + * Test multi-threaded access using the postfix ++ operator for shared script classes with multiple globals. 44.332 + */ 44.333 + @Test 44.334 + public static void multiThreadedIncTest() throws ScriptException, InterruptedException { 44.335 + final ScriptEngineManager m = new ScriptEngineManager(); 44.336 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.337 + final Bindings b = e.createBindings(); 44.338 + final ScriptContext origContext = e.getContext(); 44.339 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.340 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.341 + 44.342 + assertEquals(e.eval("var x = 0;", origContext), null); 44.343 + assertEquals(e.eval("var x = 2;", newCtxt), null); 44.344 + final String sharedScript = "x++;"; 44.345 + 44.346 + final Thread t1 = new Thread(new Runnable() { 44.347 + @Override 44.348 + public void run() { 44.349 + try { 44.350 + for (int i = 0; i < 1000; i++) { 44.351 + assertEquals(e.eval(sharedScript, origContext), (double)i); 44.352 + } 44.353 + } catch (final ScriptException se) { 44.354 + fail(se.toString()); 44.355 + } 44.356 + } 44.357 + }); 44.358 + final Thread t2 = new Thread(new Runnable() { 44.359 + @Override 44.360 + public void run() { 44.361 + try { 44.362 + for (int i = 2; i < 1000; i++) { 44.363 + assertEquals(e.eval(sharedScript, newCtxt), (double)i); 44.364 + } 44.365 + } catch (final ScriptException se) { 44.366 + fail(se.toString()); 44.367 + } 44.368 + } 44.369 + }); 44.370 + t1.start(); 44.371 + t2.start(); 44.372 + t1.join(); 44.373 + t2.join(); 44.374 + } 44.375 + 44.376 + /** 44.377 + * Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals. 44.378 + */ 44.379 + @Test 44.380 + public static void multiThreadedPrimitiveTest() throws ScriptException, InterruptedException { 44.381 + final ScriptEngineManager m = new ScriptEngineManager(); 44.382 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.383 + final Bindings b = e.createBindings(); 44.384 + final ScriptContext origContext = e.getContext(); 44.385 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.386 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.387 + 44.388 + final Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext); 44.389 + final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt); 44.390 + assertEquals(obj1, "original context"); 44.391 + assertEquals(obj2, "new context"); 44.392 + final String sharedScript = "''.foo"; 44.393 + 44.394 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.395 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 44.396 + t1.start(); 44.397 + t2.start(); 44.398 + t1.join(); 44.399 + t2.join(); 44.400 + 44.401 + final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); 44.402 + assertEquals(obj3, "newer context"); 44.403 + final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.404 + final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 44.405 + 44.406 + t3.start(); 44.407 + t4.start(); 44.408 + t3.join(); 44.409 + t4.join(); 44.410 + 44.411 + Assert.assertEquals(e.eval(sharedScript), "original context"); 44.412 + Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 44.413 + } 44.414 + 44.415 + 44.416 + /** 44.417 + * Test multi-threaded access to prototype user accessor properties for shared script classes with multiple globals. 44.418 + */ 44.419 + @Test 44.420 + public static void multiThreadedAccessorTest() throws ScriptException, InterruptedException { 44.421 + final ScriptEngineManager m = new ScriptEngineManager(); 44.422 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.423 + final Bindings b = e.createBindings(); 44.424 + final ScriptContext origContext = e.getContext(); 44.425 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.426 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.427 + 44.428 + e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'original context' })", origContext); 44.429 + e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'new context', configurable: true })", newCtxt); 44.430 + final String sharedScript = "({}).foo"; 44.431 + 44.432 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.433 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 44.434 + t1.start(); 44.435 + t2.start(); 44.436 + t1.join(); 44.437 + t2.join(); 44.438 + 44.439 + final Object obj3 = e.eval("delete Object.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); 44.440 + assertEquals(obj3, "newer context"); 44.441 + final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.442 + final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 44.443 + 44.444 + t3.start(); 44.445 + t4.start(); 44.446 + t3.join(); 44.447 + t4.join(); 44.448 + } 44.449 + 44.450 + /** 44.451 + * Test multi-threaded access to primitive prototype user accessor properties for shared script classes with multiple globals. 44.452 + */ 44.453 + @Test 44.454 + public static void multiThreadedPrimitiveAccessorTest() throws ScriptException, InterruptedException { 44.455 + final ScriptEngineManager m = new ScriptEngineManager(); 44.456 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.457 + final Bindings b = e.createBindings(); 44.458 + final ScriptContext origContext = e.getContext(); 44.459 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.460 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.461 + 44.462 + e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'original context' })", origContext); 44.463 + e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'new context' })", newCtxt); 44.464 + final String sharedScript = "''.foo"; 44.465 + 44.466 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.467 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 44.468 + t1.start(); 44.469 + t2.start(); 44.470 + t1.join(); 44.471 + t2.join(); 44.472 + 44.473 + final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); 44.474 + assertEquals(obj3, "newer context"); 44.475 + final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 44.476 + final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 44.477 + 44.478 + t3.start(); 44.479 + t4.start(); 44.480 + t3.join(); 44.481 + t4.join(); 44.482 + } 44.483 + 44.484 + /** 44.485 + * Test multi-threaded scope function invocation for shared script classes with multiple globals. 44.486 + */ 44.487 + @Test 44.488 + public static void multiThreadedFunctionTest() throws ScriptException, InterruptedException { 44.489 + final ScriptEngineManager m = new ScriptEngineManager(); 44.490 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.491 + final Bindings b = e.createBindings(); 44.492 + final ScriptContext origContext = e.getContext(); 44.493 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.494 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.495 + 44.496 + e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext); 44.497 + assertEquals(origContext.getAttribute("scopeVar"), 1); 44.498 + assertEquals(e.eval("scopeTest()"), 1); 44.499 + 44.500 + e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt); 44.501 + assertEquals(newCtxt.getAttribute("scopeVar"), 1); 44.502 + assertEquals(e.eval("scopeTest();", newCtxt), 1); 44.503 + 44.504 + assertEquals(e.eval("scopeVar = 3;", newCtxt), 3); 44.505 + assertEquals(newCtxt.getAttribute("scopeVar"), 3); 44.506 + 44.507 + 44.508 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000)); 44.509 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000)); 44.510 + 44.511 + t1.start(); 44.512 + t2.start(); 44.513 + t1.join(); 44.514 + t2.join(); 44.515 + 44.516 + } 44.517 + 44.518 + /** 44.519 + * Test multi-threaded access to global getters and setters for shared script classes with multiple globals. 44.520 + */ 44.521 + @Test 44.522 + public static void getterSetterTest() throws ScriptException, InterruptedException { 44.523 + final ScriptEngineManager m = new ScriptEngineManager(); 44.524 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.525 + final Bindings b = e.createBindings(); 44.526 + final ScriptContext origContext = e.getContext(); 44.527 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.528 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.529 + final String sharedScript = "accessor1"; 44.530 + 44.531 + e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); 44.532 + assertEquals(e.eval("accessor1 = 1;"), 1); 44.533 + assertEquals(e.eval(sharedScript), 1); 44.534 + 44.535 + e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); 44.536 + assertEquals(e.eval("accessor1 = 2;", newCtxt), 2); 44.537 + assertEquals(e.eval(sharedScript, newCtxt), 2); 44.538 + 44.539 + 44.540 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); 44.541 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); 44.542 + 44.543 + t1.start(); 44.544 + t2.start(); 44.545 + t1.join(); 44.546 + t2.join(); 44.547 + 44.548 + assertEquals(e.eval(sharedScript), 1); 44.549 + assertEquals(e.eval(sharedScript, newCtxt), 2); 44.550 + assertEquals(e.eval("v"), 1); 44.551 + assertEquals(e.eval("v", newCtxt), 2); 44.552 + } 44.553 + 44.554 + /** 44.555 + * Test multi-threaded access to global getters and setters for shared script classes with multiple globals. 44.556 + */ 44.557 + @Test 44.558 + public static void getterSetter2Test() throws ScriptException, InterruptedException { 44.559 + final ScriptEngineManager m = new ScriptEngineManager(); 44.560 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.561 + final Bindings b = e.createBindings(); 44.562 + final ScriptContext origContext = e.getContext(); 44.563 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.564 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.565 + final String sharedScript = "accessor2"; 44.566 + 44.567 + e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); 44.568 + assertEquals(e.eval("accessor2 = 1;"), 1); 44.569 + assertEquals(e.eval(sharedScript), 1); 44.570 + 44.571 + e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); 44.572 + assertEquals(e.eval("accessor2 = 2;", newCtxt), 2); 44.573 + assertEquals(e.eval(sharedScript, newCtxt), 2); 44.574 + 44.575 + 44.576 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); 44.577 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); 44.578 + 44.579 + t1.start(); 44.580 + t2.start(); 44.581 + t1.join(); 44.582 + t2.join(); 44.583 + 44.584 + assertEquals(e.eval(sharedScript), 1); 44.585 + assertEquals(e.eval(sharedScript, newCtxt), 2); 44.586 + assertEquals(e.eval("x"), 1); 44.587 + assertEquals(e.eval("x", newCtxt), 2); 44.588 + } 44.589 + 44.590 + // @bug 8058422: Users should be able to overwrite "context" and "engine" variables 44.591 + @Test 44.592 + public static void contextOverwriteTest() throws ScriptException { 44.593 + final ScriptEngineManager m = new ScriptEngineManager(); 44.594 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.595 + final Bindings b = new SimpleBindings(); 44.596 + b.put("context", "hello"); 44.597 + b.put("foo", 32); 44.598 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.599 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.600 + e.setContext(newCtxt); 44.601 + assertEquals(e.eval("context"), "hello"); 44.602 + assertEquals(((Number)e.eval("foo")).intValue(), 32); 44.603 + } 44.604 + 44.605 + // @bug 8058422: Users should be able to overwrite "context" and "engine" variables 44.606 + @Test 44.607 + public static void contextOverwriteInScriptTest() throws ScriptException { 44.608 + final ScriptEngineManager m = new ScriptEngineManager(); 44.609 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.610 + e.put("foo", 32); 44.611 + 44.612 + assertEquals(((Number)e.eval("foo")).intValue(), 32); 44.613 + assertEquals(e.eval("context = 'bar'"), "bar"); 44.614 + assertEquals(((Number)e.eval("foo")).intValue(), 32); 44.615 + } 44.616 + 44.617 + // @bug 8058422: Users should be able to overwrite "context" and "engine" variables 44.618 + @Test 44.619 + public static void engineOverwriteTest() throws ScriptException { 44.620 + final ScriptEngineManager m = new ScriptEngineManager(); 44.621 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.622 + final Bindings b = new SimpleBindings(); 44.623 + b.put("engine", "hello"); 44.624 + b.put("foo", 32); 44.625 + final ScriptContext newCtxt = new SimpleScriptContext(); 44.626 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.627 + e.setContext(newCtxt); 44.628 + assertEquals(e.eval("engine"), "hello"); 44.629 + assertEquals(((Number)e.eval("foo")).intValue(), 32); 44.630 + } 44.631 + 44.632 + // @bug 8058422: Users should be able to overwrite "context" and "engine" variables 44.633 + @Test 44.634 + public static void engineOverwriteInScriptTest() throws ScriptException { 44.635 + final ScriptEngineManager m = new ScriptEngineManager(); 44.636 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.637 + e.put("foo", 32); 44.638 + 44.639 + assertEquals(((Number)e.eval("foo")).intValue(), 32); 44.640 + assertEquals(e.eval("engine = 'bar'"), "bar"); 44.641 + assertEquals(((Number)e.eval("foo")).intValue(), 32); 44.642 + } 44.643 + 44.644 + // @bug 8044750: megamorphic getter for scope objects does not call __noSuchProperty__ hook 44.645 + @Test 44.646 + public static void testMegamorphicGetInGlobal() throws Exception { 44.647 + final ScriptEngineManager m = new ScriptEngineManager(); 44.648 + final ScriptEngine engine = m.getEngineByName("nashorn"); 44.649 + final String script = "foo"; 44.650 + // "foo" is megamorphic because of different global scopes. 44.651 + // Make sure ScriptContext variable search works even after 44.652 + // it becomes megamorphic. 44.653 + for (int index = 0; index < 25; index++) { 44.654 + final Bindings bindings = new SimpleBindings(); 44.655 + bindings.put("foo", index); 44.656 + final Number value = (Number)engine.eval(script, bindings); 44.657 + assertEquals(index, value.intValue()); 44.658 + } 44.659 + } 44.660 + 44.661 + /** 44.662 + * Test "slow" scopes involving {@code with} and {@code eval} statements for shared script classes with multiple globals. 44.663 + * @throws ScriptException 44.664 + * @throws InterruptedException 44.665 + */ 44.666 + @Test 44.667 + public static void testSlowScope() throws ScriptException, InterruptedException { 44.668 + final ScriptEngineManager m = new ScriptEngineManager(); 44.669 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.670 + 44.671 + for (int i = 0; i < 100; i++) { 44.672 + final Bindings b = e.createBindings(); 44.673 + final ScriptContext ctxt = new SimpleScriptContext(); 44.674 + ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 44.675 + 44.676 + e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt); 44.677 + assertEquals(e.eval("a", ctxt), 1); 44.678 + assertEquals(b.get("a"), 1); 44.679 + assertEquals(e.eval("b", ctxt), 3); 44.680 + assertEquals(b.get("b"), 3); 44.681 + assertEquals(e.eval("c", ctxt), 10); 44.682 + assertEquals(b.get("c"), 10); 44.683 + } 44.684 + } 44.685 + 44.686 + private static class ScriptRunner implements Runnable { 44.687 + 44.688 + final ScriptEngine engine; 44.689 + final ScriptContext context; 44.690 + final String source; 44.691 + final Object expected; 44.692 + final int iterations; 44.693 + 44.694 + ScriptRunner(final ScriptEngine engine, final ScriptContext context, final String source, final Object expected, final int iterations) { 44.695 + this.engine = engine; 44.696 + this.context = context; 44.697 + this.source = source; 44.698 + this.expected = expected; 44.699 + this.iterations = iterations; 44.700 + } 44.701 + 44.702 + @Override 44.703 + public void run() { 44.704 + try { 44.705 + for (int i = 0; i < iterations; i++) { 44.706 + assertEquals(engine.eval(source, context), expected); 44.707 + } 44.708 + } catch (final ScriptException se) { 44.709 + throw new RuntimeException(se); 44.710 + } 44.711 + } 44.712 + } 44.713 + 44.714 + // @bug 8071678: NashornScriptEngine returns javax.script.ScriptContext instance 44.715 + // with get/setAttribute methods insonsistent for GLOBAL_SCOPE 44.716 + @Test 44.717 + public void testGlobalScopeSearch() throws Exception { 44.718 + final ScriptEngineManager m = new ScriptEngineManager(); 44.719 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.720 + final ScriptContext c = e.getContext(); 44.721 + c.setAttribute("name1234", "value", ScriptContext.GLOBAL_SCOPE); 44.722 + assertEquals(c.getAttribute("name1234"), "value"); 44.723 + assertEquals(c.getAttributesScope("name1234"), 44.724 + ScriptContext.GLOBAL_SCOPE); 44.725 + } 44.726 + 44.727 + // @bug 8071594: NashornScriptEngine returns javax.script.ScriptContext instance 44.728 + // which doesn't completely conform to the spec regarding exceptions throwing 44.729 + @Test 44.730 + public void testScriptContext_NPE_IAE() throws Exception { 44.731 + final ScriptEngineManager m = new ScriptEngineManager(); 44.732 + final ScriptEngine e = m.getEngineByName("nashorn"); 44.733 + final ScriptContext c = e.getContext(); 44.734 + try { 44.735 + c.getAttribute(""); 44.736 + throw new AssertionError("should have thrown IAE"); 44.737 + } catch (IllegalArgumentException iae1) {} 44.738 + 44.739 + try { 44.740 + c.getAttribute(null); 44.741 + throw new AssertionError("should have thrown NPE"); 44.742 + } catch (NullPointerException npe1) {} 44.743 + 44.744 + try { 44.745 + c.getAttribute("", ScriptContext.ENGINE_SCOPE); 44.746 + throw new AssertionError("should have thrown IAE"); 44.747 + } catch (IllegalArgumentException iae2) {} 44.748 + 44.749 + try { 44.750 + c.getAttribute(null, ScriptContext.ENGINE_SCOPE); 44.751 + throw new AssertionError("should have thrown NPE"); 44.752 + } catch (NullPointerException npe2) {} 44.753 + 44.754 + try { 44.755 + c.removeAttribute("", ScriptContext.ENGINE_SCOPE); 44.756 + throw new AssertionError("should have thrown IAE"); 44.757 + } catch (IllegalArgumentException iae3) {} 44.758 + 44.759 + try { 44.760 + c.removeAttribute(null, ScriptContext.ENGINE_SCOPE); 44.761 + throw new AssertionError("should have thrown NPE"); 44.762 + } catch (NullPointerException npe3) {} 44.763 + 44.764 + try { 44.765 + c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE); 44.766 + throw new AssertionError("should have thrown IAE"); 44.767 + } catch (IllegalArgumentException iae4) {} 44.768 + 44.769 + try { 44.770 + c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE); 44.771 + throw new AssertionError("should have thrown NPE"); 44.772 + } catch (NullPointerException npe4) {} 44.773 + 44.774 + try { 44.775 + c.getAttributesScope(""); 44.776 + throw new AssertionError("should have thrown IAE"); 44.777 + } catch (IllegalArgumentException iae5) {} 44.778 + 44.779 + try { 44.780 + c.getAttributesScope(null); 44.781 + throw new AssertionError("should have thrown NPE"); 44.782 + } catch (NullPointerException npe5) {} 44.783 + } 44.784 +}
45.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 45.2 +++ b/test/src/jdk/nashorn/api/scripting/test/ScriptEngineSecurityTest.java Mon Apr 06 16:18:54 2015 +0530 45.3 @@ -0,0 +1,313 @@ 45.4 +/* 45.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 45.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 45.7 + * 45.8 + * This code is free software; you can redistribute it and/or modify it 45.9 + * under the terms of the GNU General Public License version 2 only, as 45.10 + * published by the Free Software Foundation. Oracle designates this 45.11 + * particular file as subject to the "Classpath" exception as provided 45.12 + * by Oracle in the LICENSE file that accompanied this code. 45.13 + * 45.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 45.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 45.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 45.17 + * version 2 for more details (a copy is included in the LICENSE file that 45.18 + * accompanied this code). 45.19 + * 45.20 + * You should have received a copy of the GNU General Public License version 45.21 + * 2 along with this work; if not, write to the Free Software Foundation, 45.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 45.23 + * 45.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 45.25 + * or visit www.oracle.com if you need additional information or have any 45.26 + * questions. 45.27 + */ 45.28 + 45.29 +package jdk.nashorn.api.scripting.test; 45.30 + 45.31 +import static org.testng.Assert.fail; 45.32 +import java.lang.reflect.InvocationHandler; 45.33 +import java.lang.reflect.Method; 45.34 +import java.lang.reflect.Proxy; 45.35 +import javax.script.ScriptEngine; 45.36 +import javax.script.ScriptEngineManager; 45.37 +import javax.script.ScriptException; 45.38 +import jdk.nashorn.api.scripting.ClassFilter; 45.39 +import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 45.40 +import org.testng.annotations.Test; 45.41 + 45.42 +/** 45.43 + * jsr223 tests for security access checks. 45.44 + */ 45.45 +@SuppressWarnings("javadoc") 45.46 +public class ScriptEngineSecurityTest { 45.47 + 45.48 + private static void log(final String msg) { 45.49 + org.testng.Reporter.log(msg, true); 45.50 + } 45.51 + 45.52 + @Test 45.53 + public void securityPackagesTest() { 45.54 + if (System.getSecurityManager() == null) { 45.55 + // pass vacuously 45.56 + return; 45.57 + } 45.58 + 45.59 + final ScriptEngineManager m = new ScriptEngineManager(); 45.60 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.61 + try { 45.62 + e.eval("var v = Packages.sun.misc.Unsafe;"); 45.63 + fail("should have thrown SecurityException"); 45.64 + } catch (final Exception exp) { 45.65 + if (exp instanceof SecurityException) { 45.66 + log("got " + exp + " as expected"); 45.67 + } else { 45.68 + fail(exp.getMessage()); 45.69 + } 45.70 + } 45.71 + } 45.72 + 45.73 + @Test 45.74 + public void securityJavaTypeTest() { 45.75 + if (System.getSecurityManager() == null) { 45.76 + // pass vacuously 45.77 + return; 45.78 + } 45.79 + 45.80 + final ScriptEngineManager m = new ScriptEngineManager(); 45.81 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.82 + try { 45.83 + e.eval("var v = Java.type('sun.misc.Unsafe');"); 45.84 + fail("should have thrown SecurityException"); 45.85 + } catch (final Exception exp) { 45.86 + if (exp instanceof SecurityException) { 45.87 + log("got " + exp + " as expected"); 45.88 + } else { 45.89 + fail(exp.getMessage()); 45.90 + } 45.91 + } 45.92 + } 45.93 + 45.94 + @Test 45.95 + public void securityClassForNameTest() { 45.96 + if (System.getSecurityManager() == null) { 45.97 + // pass vacuously 45.98 + return; 45.99 + } 45.100 + 45.101 + final ScriptEngineManager m = new ScriptEngineManager(); 45.102 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.103 + try { 45.104 + e.eval("var v = java.lang.Class.forName('sun.misc.Unsafe');"); 45.105 + fail("should have thrown SecurityException"); 45.106 + } catch (final Exception exp) { 45.107 + if (exp instanceof SecurityException) { 45.108 + log("got " + exp + " as expected"); 45.109 + } else { 45.110 + fail(exp.getMessage()); 45.111 + } 45.112 + } 45.113 + } 45.114 + 45.115 + @Test 45.116 + public void securitySystemExit() { 45.117 + if (System.getSecurityManager() == null) { 45.118 + // pass vacuously 45.119 + return; 45.120 + } 45.121 + 45.122 + final ScriptEngineManager m = new ScriptEngineManager(); 45.123 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.124 + try { 45.125 + e.eval("java.lang.System.exit(0);"); 45.126 + fail("should have thrown SecurityException"); 45.127 + } catch (final Exception exp) { 45.128 + if (exp instanceof SecurityException) { 45.129 + log("got " + exp + " as expected"); 45.130 + } else { 45.131 + fail(exp.getMessage()); 45.132 + } 45.133 + } 45.134 + } 45.135 + 45.136 + 45.137 + @Test 45.138 + public void securitySystemExitFromFinalizerThread() throws ScriptException { 45.139 + if (System.getSecurityManager() == null) { 45.140 + // pass vacuously 45.141 + return; 45.142 + } 45.143 + 45.144 + final ScriptEngineManager m = new ScriptEngineManager(); 45.145 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.146 + e.eval("var o = Java.extend(Java.type('javax.imageio.spi.ServiceRegistry'), { deregisterAll: this.exit.bind(null, 1234)});\n" + 45.147 + "new o(new java.util.ArrayList().iterator())"); 45.148 + System.gc(); 45.149 + System.runFinalization(); 45.150 + // NOTE: this test just exits the VM if it fails. 45.151 + } 45.152 + 45.153 + @Test 45.154 + public void securitySystemLoadLibrary() { 45.155 + if (System.getSecurityManager() == null) { 45.156 + // pass vacuously 45.157 + return; 45.158 + } 45.159 + 45.160 + final ScriptEngineManager m = new ScriptEngineManager(); 45.161 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.162 + try { 45.163 + e.eval("java.lang.System.loadLibrary('foo');"); 45.164 + fail("should have thrown SecurityException"); 45.165 + } catch (final Exception exp) { 45.166 + if (exp instanceof SecurityException) { 45.167 + log("got " + exp + " as expected"); 45.168 + } else { 45.169 + fail(exp.getMessage()); 45.170 + } 45.171 + } 45.172 + } 45.173 + 45.174 + // @bug 8032948: Nashorn linkages awry 45.175 + @SuppressWarnings("serial") 45.176 + public static class FakeProxy extends Proxy { 45.177 + public FakeProxy(final InvocationHandler ih) { 45.178 + super(ih); 45.179 + } 45.180 + 45.181 + public static Class<?> makeProxyClass(final ClassLoader cl, final Class<?>... ifaces) { 45.182 + return Proxy.getProxyClass(cl, ifaces); 45.183 + } 45.184 + } 45.185 + 45.186 + @Test 45.187 + public void fakeProxySubclassAccessCheckTest() { 45.188 + if (System.getSecurityManager() == null) { 45.189 + // pass vacuously 45.190 + return; 45.191 + } 45.192 + 45.193 + final ScriptEngineManager m = new ScriptEngineManager(); 45.194 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.195 + 45.196 + e.put("name", ScriptEngineSecurityTest.class.getName()); 45.197 + e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); 45.198 + e.put("intfs", new Class[] { Runnable.class }); 45.199 + 45.200 + final String getClass = "Java.type(name + '$FakeProxy').getProxyClass(cl, intfs);"; 45.201 + 45.202 + // Should not be able to call static methods of Proxy via fake subclass 45.203 + try { 45.204 + e.eval(getClass); 45.205 + fail("should have thrown SecurityException"); 45.206 + } catch (final Exception exp) { 45.207 + if (! (exp instanceof SecurityException)) { 45.208 + fail("SecurityException expected, got " + exp); 45.209 + } 45.210 + } 45.211 + } 45.212 + 45.213 + @Test 45.214 + public void fakeProxySubclassAccessCheckTest2() { 45.215 + if (System.getSecurityManager() == null) { 45.216 + // pass vacuously 45.217 + return; 45.218 + } 45.219 + 45.220 + final ScriptEngineManager m = new ScriptEngineManager(); 45.221 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.222 + 45.223 + e.put("name", ScriptEngineSecurityTest.class.getName()); 45.224 + e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); 45.225 + e.put("intfs", new Class[] { Runnable.class }); 45.226 + 45.227 + final String getClass = "Java.type(name + '$FakeProxy').makeProxyClass(cl, intfs);"; 45.228 + 45.229 + // Should not be able to call static methods of Proxy via fake subclass 45.230 + try { 45.231 + e.eval(getClass); 45.232 + fail("should have thrown SecurityException"); 45.233 + } catch (final Exception exp) { 45.234 + if (! (exp instanceof SecurityException)) { 45.235 + fail("SecurityException expected, got " + exp); 45.236 + } 45.237 + } 45.238 + } 45.239 + 45.240 + @Test 45.241 + public static void proxyStaticAccessCheckTest() { 45.242 + if (System.getSecurityManager() == null) { 45.243 + // pass vacuously 45.244 + return; 45.245 + } 45.246 + 45.247 + final ScriptEngineManager m = new ScriptEngineManager(); 45.248 + final ScriptEngine e = m.getEngineByName("nashorn"); 45.249 + final Runnable r = (Runnable)Proxy.newProxyInstance( 45.250 + ScriptEngineTest.class.getClassLoader(), 45.251 + new Class[] { Runnable.class }, 45.252 + new InvocationHandler() { 45.253 + @Override 45.254 + public Object invoke(final Object p, final Method mtd, final Object[] a) { 45.255 + return null; 45.256 + } 45.257 + }); 45.258 + 45.259 + e.put("rc", r.getClass()); 45.260 + e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); 45.261 + e.put("intfs", new Class[] { Runnable.class }); 45.262 + 45.263 + // make sure static methods of Proxy is not accessible via subclass 45.264 + try { 45.265 + e.eval("rc.static.getProxyClass(cl, intfs)"); 45.266 + fail("Should have thrown SecurityException"); 45.267 + } catch (final Exception exp) { 45.268 + if (! (exp instanceof SecurityException)) { 45.269 + fail("SecurityException expected, got " + exp); 45.270 + } 45.271 + } 45.272 + } 45.273 + 45.274 + 45.275 + @Test 45.276 + public void nashornConfigSecurityTest() { 45.277 + if (System.getSecurityManager() == null) { 45.278 + // pass vacuously 45.279 + return; 45.280 + } 45.281 + 45.282 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 45.283 + try { 45.284 + fac.getScriptEngine(new ClassFilter() { 45.285 + @Override 45.286 + public boolean exposeToScripts(final String name) { 45.287 + return true; 45.288 + } 45.289 + }); 45.290 + fail("SecurityException should have been thrown"); 45.291 + } catch (final SecurityException e) { 45.292 + //empty 45.293 + } 45.294 + } 45.295 + 45.296 + @Test 45.297 + public void nashornConfigSecurityTest2() { 45.298 + if (System.getSecurityManager() == null) { 45.299 + // pass vacuously 45.300 + return; 45.301 + } 45.302 + 45.303 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 45.304 + try { 45.305 + fac.getScriptEngine(new String[0], null, new ClassFilter() { 45.306 + @Override 45.307 + public boolean exposeToScripts(final String name) { 45.308 + return true; 45.309 + } 45.310 + }); 45.311 + fail("SecurityException should have been thrown"); 45.312 + } catch (final SecurityException e) { 45.313 + //empty 45.314 + } 45.315 + } 45.316 +}
46.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 46.2 +++ b/test/src/jdk/nashorn/api/scripting/test/ScriptEngineTest.java Mon Apr 06 16:18:54 2015 +0530 46.3 @@ -0,0 +1,881 @@ 46.4 +/* 46.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 46.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 46.7 + * 46.8 + * This code is free software; you can redistribute it and/or modify it 46.9 + * under the terms of the GNU General Public License version 2 only, as 46.10 + * published by the Free Software Foundation. Oracle designates this 46.11 + * particular file as subject to the "Classpath" exception as provided 46.12 + * by Oracle in the LICENSE file that accompanied this code. 46.13 + * 46.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 46.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 46.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 46.17 + * version 2 for more details (a copy is included in the LICENSE file that 46.18 + * accompanied this code). 46.19 + * 46.20 + * You should have received a copy of the GNU General Public License version 46.21 + * 2 along with this work; if not, write to the Free Software Foundation, 46.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 46.23 + * 46.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 46.25 + * or visit www.oracle.com if you need additional information or have any 46.26 + * questions. 46.27 + */ 46.28 + 46.29 +package jdk.nashorn.api.scripting.test; 46.30 + 46.31 +import static org.testng.Assert.assertEquals; 46.32 +import static org.testng.Assert.assertNotNull; 46.33 +import static org.testng.Assert.assertNull; 46.34 +import static org.testng.Assert.assertTrue; 46.35 +import static org.testng.Assert.fail; 46.36 + 46.37 +import java.io.StringReader; 46.38 +import java.io.StringWriter; 46.39 +import java.lang.reflect.InvocationHandler; 46.40 +import java.lang.reflect.Method; 46.41 +import java.lang.reflect.Proxy; 46.42 +import java.util.Collections; 46.43 +import java.util.concurrent.Callable; 46.44 +import java.util.concurrent.atomic.AtomicBoolean; 46.45 +import java.util.function.Consumer; 46.46 +import java.util.function.Function; 46.47 +import javax.script.Bindings; 46.48 +import javax.script.Compilable; 46.49 +import javax.script.CompiledScript; 46.50 +import javax.script.Invocable; 46.51 +import javax.script.ScriptContext; 46.52 +import javax.script.ScriptEngine; 46.53 +import javax.script.ScriptEngineFactory; 46.54 +import javax.script.ScriptEngineManager; 46.55 +import javax.script.ScriptException; 46.56 +import javax.script.SimpleScriptContext; 46.57 +import jdk.nashorn.api.scripting.ScriptObjectMirror; 46.58 +import org.testng.annotations.Test; 46.59 + 46.60 +/** 46.61 + * Tests for JSR-223 script engine for Nashorn. 46.62 + * 46.63 + * @test 46.64 + * @build jdk.nashorn.api.scripting.Window jdk.nashorn.api.scripting.WindowEventHandler jdk.nashorn.api.scripting.VariableArityTestInterface jdk.nashorn.api.scripting.ScriptEngineTest 46.65 + * @run testng/othervm jdk.nashorn.api.scripting.ScriptEngineTest 46.66 + */ 46.67 +@SuppressWarnings("javadoc") 46.68 +public class ScriptEngineTest { 46.69 + 46.70 + private static void log(final String msg) { 46.71 + org.testng.Reporter.log(msg, true); 46.72 + } 46.73 + 46.74 + @Test 46.75 + public void argumentsTest() { 46.76 + final ScriptEngineManager m = new ScriptEngineManager(); 46.77 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.78 + 46.79 + final String[] args = new String[] { "hello", "world" }; 46.80 + try { 46.81 + e.put("arguments", args); 46.82 + final Object arg0 = e.eval("arguments[0]"); 46.83 + final Object arg1 = e.eval("arguments[1]"); 46.84 + assertEquals(args[0], arg0); 46.85 + assertEquals(args[1], arg1); 46.86 + } catch (final Exception exp) { 46.87 + exp.printStackTrace(); 46.88 + fail(exp.getMessage()); 46.89 + } 46.90 + } 46.91 + 46.92 + @Test 46.93 + public void argumentsWithTest() { 46.94 + final ScriptEngineManager m = new ScriptEngineManager(); 46.95 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.96 + 46.97 + final String[] args = new String[] { "hello", "world" }; 46.98 + try { 46.99 + e.put("arguments", args); 46.100 + final Object arg0 = e.eval("var imports = new JavaImporter(java.io); " + 46.101 + " with(imports) { arguments[0] }"); 46.102 + final Object arg1 = e.eval("var imports = new JavaImporter(java.util, java.io); " + 46.103 + " with(imports) { arguments[1] }"); 46.104 + assertEquals(args[0], arg0); 46.105 + assertEquals(args[1], arg1); 46.106 + } catch (final Exception exp) { 46.107 + exp.printStackTrace(); 46.108 + fail(exp.getMessage()); 46.109 + } 46.110 + } 46.111 + 46.112 + @Test 46.113 + public void argumentsEmptyTest() { 46.114 + final ScriptEngineManager m = new ScriptEngineManager(); 46.115 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.116 + 46.117 + try { 46.118 + assertEquals(e.eval("arguments instanceof Array"), true); 46.119 + assertEquals(e.eval("arguments.length == 0"), true); 46.120 + } catch (final Exception exp) { 46.121 + exp.printStackTrace(); 46.122 + fail(exp.getMessage()); 46.123 + } 46.124 + } 46.125 + 46.126 + @Test 46.127 + public void factoryTests() { 46.128 + final ScriptEngineManager m = new ScriptEngineManager(); 46.129 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.130 + assertNotNull(e); 46.131 + 46.132 + final ScriptEngineFactory fac = e.getFactory(); 46.133 + 46.134 + assertEquals(fac.getLanguageName(), "ECMAScript"); 46.135 + assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); 46.136 + assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1"); 46.137 + assertEquals(fac.getEngineName(), "Oracle Nashorn"); 46.138 + assertEquals(fac.getOutputStatement("context"), "print(context)"); 46.139 + assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');"); 46.140 + assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); 46.141 + 46.142 + boolean seenJS = false; 46.143 + for (final String ext : fac.getExtensions()) { 46.144 + if (ext.equals("js")) { 46.145 + seenJS = true; 46.146 + } 46.147 + } 46.148 + 46.149 + assertEquals(seenJS, true); 46.150 + final String str = fac.getMethodCallSyntax("obj", "foo", "x"); 46.151 + assertEquals(str, "obj.foo(x)"); 46.152 + 46.153 + boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false; 46.154 + for (final String name : fac.getNames()) { 46.155 + switch (name) { 46.156 + case "nashorn": seenNashorn = true; break; 46.157 + case "javascript": seenJavaScript = true; break; 46.158 + case "ECMAScript": seenECMAScript = true; break; 46.159 + default: 46.160 + break; 46.161 + } 46.162 + } 46.163 + 46.164 + assertTrue(seenNashorn); 46.165 + assertTrue(seenJavaScript); 46.166 + assertTrue(seenECMAScript); 46.167 + 46.168 + boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false; 46.169 + for (final String mime : fac.getMimeTypes()) { 46.170 + switch (mime) { 46.171 + case "application/javascript": seenAppJS = true; break; 46.172 + case "application/ecmascript": seenAppECMA = true; break; 46.173 + case "text/javascript": seenTextJS = true; break; 46.174 + case "text/ecmascript": seenTextECMA = true; break; 46.175 + default: 46.176 + break; 46.177 + } 46.178 + } 46.179 + 46.180 + assertTrue(seenAppJS); 46.181 + assertTrue(seenAppECMA); 46.182 + assertTrue(seenTextJS); 46.183 + assertTrue(seenTextECMA); 46.184 + } 46.185 + 46.186 + @Test 46.187 + public void evalTests() { 46.188 + final ScriptEngineManager m = new ScriptEngineManager(); 46.189 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.190 + e.put(ScriptEngine.FILENAME, "myfile.js"); 46.191 + 46.192 + try { 46.193 + e.eval("print('hello')"); 46.194 + } catch (final ScriptException se) { 46.195 + fail(se.getMessage()); 46.196 + } 46.197 + try { 46.198 + e.eval("print('hello)"); 46.199 + fail("script exception expected"); 46.200 + } catch (final ScriptException se) { 46.201 + assertEquals(se.getLineNumber(), 1); 46.202 + assertEquals(se.getColumnNumber(), 13); 46.203 + assertEquals(se.getFileName(), "myfile.js"); 46.204 + // se.printStackTrace(); 46.205 + } 46.206 + 46.207 + try { 46.208 + Object obj = e.eval("34 + 41"); 46.209 + assertTrue(34.0 + 41.0 == ((Number)obj).doubleValue()); 46.210 + obj = e.eval("x = 5"); 46.211 + assertTrue(5.0 == ((Number)obj).doubleValue()); 46.212 + } catch (final ScriptException se) { 46.213 + se.printStackTrace(); 46.214 + fail(se.getMessage()); 46.215 + } 46.216 + } 46.217 + 46.218 + @Test 46.219 + public void compileTests() { 46.220 + final ScriptEngineManager m = new ScriptEngineManager(); 46.221 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.222 + CompiledScript script = null; 46.223 + 46.224 + try { 46.225 + script = ((Compilable)e).compile("print('hello')"); 46.226 + } catch (final ScriptException se) { 46.227 + fail(se.getMessage()); 46.228 + } 46.229 + 46.230 + try { 46.231 + script.eval(); 46.232 + } catch (final ScriptException | NullPointerException se) { 46.233 + se.printStackTrace(); 46.234 + fail(se.getMessage()); 46.235 + } 46.236 + 46.237 + // try to compile from a Reader 46.238 + try { 46.239 + script = ((Compilable)e).compile(new StringReader("print('world')")); 46.240 + } catch (final ScriptException se) { 46.241 + fail(se.getMessage()); 46.242 + } 46.243 + 46.244 + try { 46.245 + script.eval(); 46.246 + } catch (final ScriptException | NullPointerException se) { 46.247 + se.printStackTrace(); 46.248 + fail(se.getMessage()); 46.249 + } 46.250 + } 46.251 + 46.252 + @Test 46.253 + public void compileAndEvalInDiffContextTest() throws ScriptException { 46.254 + final ScriptEngineManager m = new ScriptEngineManager(); 46.255 + final ScriptEngine engine = m.getEngineByName("js"); 46.256 + final Compilable compilable = (Compilable) engine; 46.257 + final CompiledScript compiledScript = compilable.compile("foo"); 46.258 + final ScriptContext ctxt = new SimpleScriptContext(); 46.259 + ctxt.setAttribute("foo", "hello", ScriptContext.ENGINE_SCOPE); 46.260 + assertEquals(compiledScript.eval(ctxt), "hello"); 46.261 + } 46.262 + 46.263 + @Test 46.264 + public void accessGlobalTest() { 46.265 + final ScriptEngineManager m = new ScriptEngineManager(); 46.266 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.267 + 46.268 + try { 46.269 + e.eval("var x = 'hello'"); 46.270 + assertEquals(e.get("x"), "hello"); 46.271 + } catch (final ScriptException exp) { 46.272 + exp.printStackTrace(); 46.273 + fail(exp.getMessage()); 46.274 + } 46.275 + } 46.276 + 46.277 + @Test 46.278 + public void exposeGlobalTest() { 46.279 + final ScriptEngineManager m = new ScriptEngineManager(); 46.280 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.281 + 46.282 + try { 46.283 + e.put("y", "foo"); 46.284 + e.eval("print(y)"); 46.285 + } catch (final ScriptException exp) { 46.286 + exp.printStackTrace(); 46.287 + fail(exp.getMessage()); 46.288 + } 46.289 + } 46.290 + 46.291 + @Test 46.292 + public void putGlobalFunctionTest() { 46.293 + final ScriptEngineManager m = new ScriptEngineManager(); 46.294 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.295 + 46.296 + e.put("callable", new Callable<String>() { 46.297 + @Override 46.298 + public String call() throws Exception { 46.299 + return "callable was called"; 46.300 + } 46.301 + }); 46.302 + 46.303 + try { 46.304 + e.eval("print(callable.call())"); 46.305 + } catch (final ScriptException exp) { 46.306 + exp.printStackTrace(); 46.307 + fail(exp.getMessage()); 46.308 + } 46.309 + } 46.310 + 46.311 + @Test 46.312 + public void windowAlertTest() { 46.313 + final ScriptEngineManager m = new ScriptEngineManager(); 46.314 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.315 + final Window window = new Window(); 46.316 + 46.317 + try { 46.318 + e.put("window", window); 46.319 + e.eval("print(window.alert)"); 46.320 + e.eval("window.alert('calling window.alert...')"); 46.321 + } catch (final Exception exp) { 46.322 + exp.printStackTrace(); 46.323 + fail(exp.getMessage()); 46.324 + } 46.325 + } 46.326 + 46.327 + @Test 46.328 + public void windowLocationTest() { 46.329 + final ScriptEngineManager m = new ScriptEngineManager(); 46.330 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.331 + final Window window = new Window(); 46.332 + 46.333 + try { 46.334 + e.put("window", window); 46.335 + e.eval("print(window.location)"); 46.336 + final Object locationValue = e.eval("window.getLocation()"); 46.337 + assertEquals(locationValue, "http://localhost:8080/window"); 46.338 + } catch (final Exception exp) { 46.339 + exp.printStackTrace(); 46.340 + fail(exp.getMessage()); 46.341 + } 46.342 + } 46.343 + 46.344 + @Test 46.345 + public void windowItemTest() { 46.346 + final ScriptEngineManager m = new ScriptEngineManager(); 46.347 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.348 + final Window window = new Window(); 46.349 + 46.350 + try { 46.351 + e.put("window", window); 46.352 + final String item1 = (String)e.eval("window.item(65535)"); 46.353 + assertEquals(item1, "ffff"); 46.354 + final String item2 = (String)e.eval("window.item(255)"); 46.355 + assertEquals(item2, "ff"); 46.356 + } catch (final Exception exp) { 46.357 + exp.printStackTrace(); 46.358 + fail(exp.getMessage()); 46.359 + } 46.360 + } 46.361 + 46.362 + @Test 46.363 + public void windowEventTest() { 46.364 + final ScriptEngineManager m = new ScriptEngineManager(); 46.365 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.366 + final Window window = new Window(); 46.367 + 46.368 + try { 46.369 + e.put("window", window); 46.370 + e.eval("window.onload = function() { print('window load event fired'); return true }"); 46.371 + assertTrue((Boolean)e.eval("window.onload.loaded()")); 46.372 + final WindowEventHandler handler = window.getOnload(); 46.373 + assertNotNull(handler); 46.374 + assertTrue(handler.loaded()); 46.375 + } catch (final Exception exp) { 46.376 + exp.printStackTrace(); 46.377 + fail(exp.getMessage()); 46.378 + } 46.379 + } 46.380 + 46.381 + @Test 46.382 + public void throwTest() { 46.383 + final ScriptEngineManager m = new ScriptEngineManager(); 46.384 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.385 + e.put(ScriptEngine.FILENAME, "throwtest.js"); 46.386 + 46.387 + try { 46.388 + e.eval("throw 'foo'"); 46.389 + } catch (final ScriptException exp) { 46.390 + log(exp.getMessage()); 46.391 + assertEquals(exp.getMessage(), "foo in throwtest.js at line number 1 at column number 0"); 46.392 + assertEquals(exp.getFileName(), "throwtest.js"); 46.393 + assertEquals(exp.getLineNumber(), 1); 46.394 + } 46.395 + } 46.396 + 46.397 + @Test 46.398 + public void setTimeoutTest() { 46.399 + final ScriptEngineManager m = new ScriptEngineManager(); 46.400 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.401 + final Window window = new Window(); 46.402 + 46.403 + try { 46.404 + final Class<?> setTimeoutParamTypes[] = { Window.class, String.class, int.class }; 46.405 + final Method setTimeout = Window.class.getDeclaredMethod("setTimeout", setTimeoutParamTypes); 46.406 + assertNotNull(setTimeout); 46.407 + e.put("window", window); 46.408 + e.eval("window.setTimeout('foo()', 100)"); 46.409 + 46.410 + // try to make setTimeout global 46.411 + e.put("setTimeout", setTimeout); 46.412 + // TODO: java.lang.ClassCastException: required class 46.413 + // java.lang.Integer but encountered class java.lang.Double 46.414 + // e.eval("setTimeout('foo2()', 200)"); 46.415 + } catch (final Exception exp) { 46.416 + exp.printStackTrace(); 46.417 + fail(exp.getMessage()); 46.418 + } 46.419 + } 46.420 + 46.421 + @Test 46.422 + public void setWriterTest() { 46.423 + final ScriptEngineManager m = new ScriptEngineManager(); 46.424 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.425 + final StringWriter sw = new StringWriter(); 46.426 + e.getContext().setWriter(sw); 46.427 + 46.428 + try { 46.429 + e.eval("print('hello world')"); 46.430 + } catch (final Exception exp) { 46.431 + exp.printStackTrace(); 46.432 + fail(exp.getMessage()); 46.433 + } 46.434 + assertEquals(sw.toString(), println("hello world")); 46.435 + } 46.436 + 46.437 + @Test 46.438 + public void redefineEchoTest() { 46.439 + final ScriptEngineManager m = new ScriptEngineManager(); 46.440 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.441 + 46.442 + try { 46.443 + e.eval("var echo = {}; if (typeof echo !== 'object') { throw 'echo is a '+typeof echo; }"); 46.444 + } catch (final Exception exp) { 46.445 + exp.printStackTrace(); 46.446 + fail(exp.getMessage()); 46.447 + } 46.448 + } 46.449 + @Test 46.450 + public void noEnumerablePropertiesTest() { 46.451 + final ScriptEngineManager m = new ScriptEngineManager(); 46.452 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.453 + try { 46.454 + e.eval("for (i in this) { throw 'found property: ' + i }"); 46.455 + } catch (final Exception exp) { 46.456 + exp.printStackTrace(); 46.457 + fail(exp.getMessage()); 46.458 + } 46.459 + } 46.460 + 46.461 + @Test 46.462 + public void noRefErrorForGlobalThisAccessTest() { 46.463 + final ScriptEngineManager m = new ScriptEngineManager(); 46.464 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.465 + try { 46.466 + e.eval("this.foo"); 46.467 + } catch (final Exception exp) { 46.468 + exp.printStackTrace(); 46.469 + fail(exp.getMessage()); 46.470 + } 46.471 + } 46.472 + 46.473 + @Test 46.474 + public void refErrorForUndeclaredAccessTest() { 46.475 + final ScriptEngineManager m = new ScriptEngineManager(); 46.476 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.477 + try { 46.478 + e.eval("try { print(foo); throw 'no ref error' } catch (e) { if (!(e instanceof ReferenceError)) throw e; }"); 46.479 + } catch (final Exception exp) { 46.480 + exp.printStackTrace(); 46.481 + fail(exp.getMessage()); 46.482 + } 46.483 + } 46.484 + 46.485 + @Test 46.486 + public void typeErrorForGlobalThisCallTest() { 46.487 + final ScriptEngineManager m = new ScriptEngineManager(); 46.488 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.489 + try { 46.490 + e.eval("try { this.foo() } catch(e) { if (! (e instanceof TypeError)) throw 'no type error' }"); 46.491 + } catch (final Exception exp) { 46.492 + exp.printStackTrace(); 46.493 + fail(exp.getMessage()); 46.494 + } 46.495 + } 46.496 + 46.497 + @Test 46.498 + public void refErrorForUndeclaredCallTest() { 46.499 + final ScriptEngineManager m = new ScriptEngineManager(); 46.500 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.501 + try { 46.502 + e.eval("try { foo() } catch(e) { if (! (e instanceof ReferenceError)) throw 'no ref error' }"); 46.503 + } catch (final Exception exp) { 46.504 + exp.printStackTrace(); 46.505 + fail(exp.getMessage()); 46.506 + } 46.507 + } 46.508 + 46.509 + @Test 46.510 + // check that print function prints arg followed by newline char 46.511 + public void printTest() { 46.512 + final ScriptEngineManager m = new ScriptEngineManager(); 46.513 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.514 + final StringWriter sw = new StringWriter(); 46.515 + e.getContext().setWriter(sw); 46.516 + try { 46.517 + e.eval("print('hello')"); 46.518 + } catch (final Throwable t) { 46.519 + t.printStackTrace(); 46.520 + fail(t.getMessage()); 46.521 + } 46.522 + 46.523 + assertEquals(sw.toString(), println("hello")); 46.524 + } 46.525 + 46.526 + @Test 46.527 + // check that print prints all arguments (more than one) 46.528 + public void printManyTest() { 46.529 + final ScriptEngineManager m = new ScriptEngineManager(); 46.530 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.531 + final StringWriter sw = new StringWriter(); 46.532 + e.getContext().setWriter(sw); 46.533 + try { 46.534 + e.eval("print(34, true, 'hello')"); 46.535 + } catch (final Throwable t) { 46.536 + t.printStackTrace(); 46.537 + fail(t.getMessage()); 46.538 + } 46.539 + 46.540 + assertEquals(sw.toString(), println("34 true hello")); 46.541 + } 46.542 + 46.543 + @Test 46.544 + public void scriptObjectAutoConversionTest() throws ScriptException { 46.545 + final ScriptEngineManager m = new ScriptEngineManager(); 46.546 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.547 + e.eval("obj = { foo: 'hello' }"); 46.548 + e.put("Window", e.eval("Packages.jdk.nashorn.api.scripting.Window")); 46.549 + assertEquals(e.eval("Window.funcJSObject(obj)"), "hello"); 46.550 + assertEquals(e.eval("Window.funcScriptObjectMirror(obj)"), "hello"); 46.551 + assertEquals(e.eval("Window.funcMap(obj)"), "hello"); 46.552 + assertEquals(e.eval("Window.funcJSObject(obj)"), "hello"); 46.553 + } 46.554 + 46.555 + // @bug 8032948: Nashorn linkages awry 46.556 + @Test 46.557 + public void checkProxyAccess() throws ScriptException { 46.558 + final ScriptEngineManager m = new ScriptEngineManager(); 46.559 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.560 + final boolean[] reached = new boolean[1]; 46.561 + final Runnable r = (Runnable)Proxy.newProxyInstance( 46.562 + ScriptEngineTest.class.getClassLoader(), 46.563 + new Class[] { Runnable.class }, 46.564 + new InvocationHandler() { 46.565 + @Override 46.566 + public Object invoke(final Object p, final Method mtd, final Object[] a) { 46.567 + reached[0] = true; 46.568 + return null; 46.569 + } 46.570 + }); 46.571 + 46.572 + e.put("r", r); 46.573 + e.eval("r.run()"); 46.574 + 46.575 + assertTrue(reached[0]); 46.576 + } 46.577 + 46.578 + // properties that can be read by any code 46.579 + private static String[] propNames = { 46.580 + "java.version", 46.581 + "java.vendor", 46.582 + "java.vendor.url", 46.583 + "java.class.version", 46.584 + "os.name", 46.585 + "os.version", 46.586 + "os.arch", 46.587 + "file.separator", 46.588 + "path.separator", 46.589 + "line.separator", 46.590 + "java.specification.version", 46.591 + "java.specification.vendor", 46.592 + "java.specification.name", 46.593 + "java.vm.specification.version", 46.594 + "java.vm.specification.vendor", 46.595 + "java.vm.specification.name", 46.596 + "java.vm.version", 46.597 + "java.vm.vendor", 46.598 + "java.vm.name" 46.599 + }; 46.600 + 46.601 + // @bug 8033924: Default permissions are not given for eval code 46.602 + @Test 46.603 + public void checkPropertyReadPermissions() throws ScriptException { 46.604 + final ScriptEngineManager m = new ScriptEngineManager(); 46.605 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.606 + 46.607 + for (final String name : propNames) { 46.608 + checkProperty(e, name); 46.609 + } 46.610 + } 46.611 + 46.612 + // @bug 8046013: TypeError: Cannot apply "with" to non script object 46.613 + @Test 46.614 + public void withOnMirrorTest() throws ScriptException { 46.615 + final ScriptEngineManager m = new ScriptEngineManager(); 46.616 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.617 + 46.618 + final Object obj = e.eval("({ foo: 'hello'})"); 46.619 + final Object[] arr = new Object[1]; 46.620 + arr[0] = obj; 46.621 + e.put("arr", arr); 46.622 + final Object res = e.eval("var res; with(arr[0]) { res = foo; }; res"); 46.623 + assertEquals(res, "hello"); 46.624 + } 46.625 + 46.626 + // @bug 8054223: Nashorn: AssertionError when use __DIR__ and ScriptEngine.eval() 46.627 + @Test 46.628 + public void check__DIR__Test() throws ScriptException { 46.629 + final ScriptEngineManager m = new ScriptEngineManager(); 46.630 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.631 + e.eval("__DIR__"); 46.632 + } 46.633 + 46.634 + // @bug 8050432:javax.script.filename variable should not be enumerable 46.635 + // with nashorn engine's ENGINE_SCOPE bindings 46.636 + @Test 46.637 + public void enumerableGlobalsTest() throws ScriptException { 46.638 + final ScriptEngineManager m = new ScriptEngineManager(); 46.639 + final ScriptEngine e = m.getEngineByName("nashorn"); 46.640 + 46.641 + e.put(ScriptEngine.FILENAME, "test"); 46.642 + final Object enumerable = e.eval( 46.643 + "Object.getOwnPropertyDescriptor(this, " + 46.644 + " 'javax.script.filename').enumerable"); 46.645 + assertEquals(enumerable, Boolean.FALSE); 46.646 + } 46.647 + 46.648 + public static class Context { 46.649 + private Object myobj; 46.650 + 46.651 + public void set(final Object o) { 46.652 + myobj = o; 46.653 + } 46.654 + 46.655 + public Object get() { 46.656 + return myobj; 46.657 + } 46.658 + } 46.659 + 46.660 + // @bug 8050977: Java8 Javascript Nashorn exception: 46.661 + // no current Global instance for nashorn 46.662 + @Test 46.663 + public void currentGlobalMissingTest() throws Exception { 46.664 + final ScriptEngineManager manager = new ScriptEngineManager(); 46.665 + final ScriptEngine e = manager.getEngineByName("nashorn"); 46.666 + 46.667 + final Context ctx = new Context(); 46.668 + e.put("ctx", ctx); 46.669 + e.eval("var obj = { foo: function(str) { return str.toUpperCase() } }"); 46.670 + e.eval("ctx.set(obj)"); 46.671 + final Invocable inv = (Invocable)e; 46.672 + assertEquals("HELLO", inv.invokeMethod(ctx.get(), "foo", "hello")); 46.673 + // try object literal 46.674 + e.eval("ctx.set({ bar: function(str) { return str.toLowerCase() } })"); 46.675 + assertEquals("hello", inv.invokeMethod(ctx.get(), "bar", "HELLO")); 46.676 + // try array literal 46.677 + e.eval("var arr = [ 'hello', 'world' ]"); 46.678 + e.eval("ctx.set(arr)"); 46.679 + assertEquals("helloworld", inv.invokeMethod(ctx.get(), "join", "")); 46.680 + } 46.681 + 46.682 + // @bug 8068524: NashornScriptEngineFactory.getParameter() throws IAE 46.683 + // for an unknown key, doesn't conform to the general spec 46.684 + @Test 46.685 + public void getParameterInvalidKeyTest() throws Exception { 46.686 + final ScriptEngineManager manager = new ScriptEngineManager(); 46.687 + final ScriptEngine e = manager.getEngineByName("nashorn"); 46.688 + // no exception expected here! 46.689 + Object value = e.getFactory().getParameter("no value assigned to this key"); 46.690 + assertNull(value); 46.691 + } 46.692 + 46.693 + // @bug JDK-8068889: ConsString arguments to a functional interface wasn't converted to string. 46.694 + @Test 46.695 + public void functionalInterfaceStringTest() throws Exception { 46.696 + final ScriptEngineManager manager = new ScriptEngineManager(); 46.697 + final ScriptEngine e = manager.getEngineByName("nashorn"); 46.698 + final AtomicBoolean invoked = new AtomicBoolean(false); 46.699 + e.put("f", new Function<String, String>() { 46.700 + @Override 46.701 + public String apply(String t) { 46.702 + invoked.set(true); 46.703 + return t; 46.704 + } 46.705 + }); 46.706 + assertEquals(e.eval("var x = 'a'; x += 'b'; f(x)"), "ab"); 46.707 + assertTrue(invoked.get()); 46.708 + } 46.709 + 46.710 + // @bug JDK-8068889: ScriptObject arguments to a functional interface wasn't converted to a mirror. 46.711 + @Test 46.712 + public void functionalInterfaceObjectTest() throws Exception { 46.713 + final ScriptEngineManager manager = new ScriptEngineManager(); 46.714 + final ScriptEngine e = manager.getEngineByName("nashorn"); 46.715 + final AtomicBoolean invoked = new AtomicBoolean(false); 46.716 + e.put("c", new Consumer<Object>() { 46.717 + @Override 46.718 + public void accept(Object t) { 46.719 + assertTrue(t instanceof ScriptObjectMirror); 46.720 + assertEquals(((ScriptObjectMirror)t).get("a"), "xyz"); 46.721 + invoked.set(true); 46.722 + } 46.723 + }); 46.724 + e.eval("var x = 'xy'; x += 'z';c({a:x})"); 46.725 + assertTrue(invoked.get()); 46.726 + } 46.727 + 46.728 + @Test 46.729 + public void testLengthOnArrayLikeObjects() throws Exception { 46.730 + final ScriptEngine e = new ScriptEngineManager().getEngineByName("nashorn"); 46.731 + final Object val = e.eval("var arr = { length: 1, 0: 1}; arr.length"); 46.732 + 46.733 + assertTrue(Number.class.isAssignableFrom(val.getClass())); 46.734 + assertTrue(((Number)val).intValue() == 1); 46.735 + } 46.736 + 46.737 + // @bug JDK-8068603: NashornScriptEngine.put/get() impls don't conform to NPE, IAE spec assertions 46.738 + @Test 46.739 + public void illegalBindingsValuesTest() throws Exception { 46.740 + final ScriptEngineManager manager = new ScriptEngineManager(); 46.741 + final ScriptEngine e = manager.getEngineByName("nashorn"); 46.742 + 46.743 + try { 46.744 + e.put(null, "null-value"); 46.745 + fail(); 46.746 + } catch (NullPointerException x) { 46.747 + // expected 46.748 + } 46.749 + 46.750 + try { 46.751 + e.put("", "empty-value"); 46.752 + fail(); 46.753 + } catch (IllegalArgumentException x) { 46.754 + // expected 46.755 + } 46.756 + 46.757 + final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 46.758 + assertTrue(b instanceof ScriptObjectMirror); 46.759 + 46.760 + try { 46.761 + b.put(null, "null-value"); 46.762 + fail(); 46.763 + } catch (NullPointerException x) { 46.764 + // expected 46.765 + } 46.766 + 46.767 + try { 46.768 + b.put("", "empty-value"); 46.769 + fail(); 46.770 + } catch (IllegalArgumentException x) { 46.771 + // expected 46.772 + } 46.773 + 46.774 + try { 46.775 + b.get(null); 46.776 + fail(); 46.777 + } catch (NullPointerException x) { 46.778 + // expected 46.779 + } 46.780 + 46.781 + try { 46.782 + b.get(""); 46.783 + fail(); 46.784 + } catch (IllegalArgumentException x) { 46.785 + // expected 46.786 + } 46.787 + 46.788 + try { 46.789 + b.get(1); 46.790 + fail(); 46.791 + } catch (ClassCastException x) { 46.792 + // expected 46.793 + } 46.794 + 46.795 + try { 46.796 + b.remove(null); 46.797 + fail(); 46.798 + } catch (NullPointerException x) { 46.799 + // expected 46.800 + } 46.801 + 46.802 + try { 46.803 + b.remove(""); 46.804 + fail(); 46.805 + } catch (IllegalArgumentException x) { 46.806 + // expected 46.807 + } 46.808 + 46.809 + try { 46.810 + b.remove(1); 46.811 + fail(); 46.812 + } catch (ClassCastException x) { 46.813 + // expected 46.814 + } 46.815 + 46.816 + try { 46.817 + b.containsKey(null); 46.818 + fail(); 46.819 + } catch (NullPointerException x) { 46.820 + // expected 46.821 + } 46.822 + 46.823 + try { 46.824 + b.containsKey(""); 46.825 + fail(); 46.826 + } catch (IllegalArgumentException x) { 46.827 + // expected 46.828 + } 46.829 + 46.830 + try { 46.831 + b.containsKey(1); 46.832 + fail(); 46.833 + } catch (ClassCastException x) { 46.834 + // expected 46.835 + } 46.836 + 46.837 + try { 46.838 + b.putAll(null); 46.839 + fail(); 46.840 + } catch (NullPointerException x) { 46.841 + // expected 46.842 + } 46.843 + 46.844 + try { 46.845 + b.putAll(Collections.singletonMap((String)null, "null-value")); 46.846 + fail(); 46.847 + } catch (NullPointerException x) { 46.848 + // expected 46.849 + } 46.850 + 46.851 + try { 46.852 + b.putAll(Collections.singletonMap("", "empty-value")); 46.853 + fail(); 46.854 + } catch (IllegalArgumentException x) { 46.855 + // expected 46.856 + } 46.857 + } 46.858 + 46.859 + // @bug 8071989: NashornScriptEngine returns javax.script.ScriptContext instance 46.860 + // with insonsistent get/remove methods behavior for undefined attributes 46.861 + @Test 46.862 + public void testScriptContextGetRemoveUndefined() throws Exception { 46.863 + final ScriptEngineManager manager = new ScriptEngineManager(); 46.864 + final ScriptEngine e = manager.getEngineByName("nashorn"); 46.865 + final ScriptContext ctx = e.getContext(); 46.866 + assertNull(ctx.getAttribute("undefinedname", ScriptContext.ENGINE_SCOPE)); 46.867 + assertNull(ctx.removeAttribute("undefinedname", ScriptContext.ENGINE_SCOPE)); 46.868 + } 46.869 + 46.870 + private static void checkProperty(final ScriptEngine e, final String name) 46.871 + throws ScriptException { 46.872 + final String value = System.getProperty(name); 46.873 + e.put("name", name); 46.874 + assertEquals(value, e.eval("java.lang.System.getProperty(name)")); 46.875 + } 46.876 + 46.877 + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 46.878 + 46.879 + // Returns String that would be the result of calling PrintWriter.println 46.880 + // of the given String. (This is to handle platform specific newline). 46.881 + private static String println(final String str) { 46.882 + return str + LINE_SEPARATOR; 46.883 + } 46.884 +}
47.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 47.2 +++ b/test/src/jdk/nashorn/api/scripting/test/ScriptObjectMirrorTest.java Mon Apr 06 16:18:54 2015 +0530 47.3 @@ -0,0 +1,389 @@ 47.4 +/* 47.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 47.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 47.7 + * 47.8 + * This code is free software; you can redistribute it and/or modify it 47.9 + * under the terms of the GNU General Public License version 2 only, as 47.10 + * published by the Free Software Foundation. Oracle designates this 47.11 + * particular file as subject to the "Classpath" exception as provided 47.12 + * by Oracle in the LICENSE file that accompanied this code. 47.13 + * 47.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 47.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 47.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 47.17 + * version 2 for more details (a copy is included in the LICENSE file that 47.18 + * accompanied this code). 47.19 + * 47.20 + * You should have received a copy of the GNU General Public License version 47.21 + * 2 along with this work; if not, write to the Free Software Foundation, 47.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 47.23 + * 47.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 47.25 + * or visit www.oracle.com if you need additional information or have any 47.26 + * questions. 47.27 + */ 47.28 + 47.29 +package jdk.nashorn.api.scripting.test; 47.30 + 47.31 +import static org.testng.Assert.assertEquals; 47.32 +import static org.testng.Assert.assertFalse; 47.33 +import static org.testng.Assert.assertTrue; 47.34 +import static org.testng.Assert.fail; 47.35 + 47.36 +import java.nio.ByteBuffer; 47.37 +import java.util.HashMap; 47.38 +import java.util.List; 47.39 +import java.util.Map; 47.40 +import java.util.function.Function; 47.41 +import javax.script.Bindings; 47.42 +import javax.script.Invocable; 47.43 +import javax.script.ScriptContext; 47.44 +import javax.script.ScriptEngine; 47.45 +import javax.script.ScriptEngineManager; 47.46 +import javax.script.ScriptException; 47.47 +import jdk.nashorn.api.scripting.JSObject; 47.48 +import jdk.nashorn.api.scripting.ScriptObjectMirror; 47.49 +import org.testng.annotations.Test; 47.50 + 47.51 +/** 47.52 + * Tests to check jdk.nashorn.api.scripting.ScriptObjectMirror API. 47.53 + */ 47.54 +@SuppressWarnings("javadoc") 47.55 +public class ScriptObjectMirrorTest { 47.56 + 47.57 + @SuppressWarnings("unchecked") 47.58 + @Test 47.59 + public void reflectionTest() throws ScriptException { 47.60 + final ScriptEngineManager m = new ScriptEngineManager(); 47.61 + final ScriptEngine e = m.getEngineByName("nashorn"); 47.62 + 47.63 + e.eval("var obj = { x: 344, y: 'nashorn' }"); 47.64 + 47.65 + int count = 0; 47.66 + Map<Object, Object> map = (Map<Object, Object>) e.get("obj"); 47.67 + assertFalse(map.isEmpty()); 47.68 + assertTrue(map.keySet().contains("x")); 47.69 + assertTrue(map.containsKey("x")); 47.70 + assertTrue(map.values().contains("nashorn")); 47.71 + assertTrue(map.containsValue("nashorn")); 47.72 + for (final Map.Entry<?, ?> ex : map.entrySet()) { 47.73 + final Object key = ex.getKey(); 47.74 + if (key.equals("x")) { 47.75 + assertTrue(344 == ((Number) ex.getValue()).doubleValue()); 47.76 + count++; 47.77 + } else if (key.equals("y")) { 47.78 + assertEquals(ex.getValue(), "nashorn"); 47.79 + count++; 47.80 + } 47.81 + } 47.82 + assertEquals(2, count); 47.83 + assertEquals(2, map.size()); 47.84 + 47.85 + // add property 47.86 + map.put("z", "hello"); 47.87 + assertEquals(e.eval("obj.z"), "hello"); 47.88 + assertEquals(map.get("z"), "hello"); 47.89 + assertTrue(map.keySet().contains("z")); 47.90 + assertTrue(map.containsKey("z")); 47.91 + assertTrue(map.values().contains("hello")); 47.92 + assertTrue(map.containsValue("hello")); 47.93 + assertEquals(map.size(), 3); 47.94 + 47.95 + final Map<Object, Object> newMap = new HashMap<>(); 47.96 + newMap.put("foo", 23.0); 47.97 + newMap.put("bar", true); 47.98 + map.putAll(newMap); 47.99 + 47.100 + assertEquals(e.eval("obj.foo"), 23.0); 47.101 + assertEquals(e.eval("obj.bar"), true); 47.102 + 47.103 + // remove using map method 47.104 + map.remove("foo"); 47.105 + assertEquals(e.eval("typeof obj.foo"), "undefined"); 47.106 + 47.107 + count = 0; 47.108 + e.eval("var arr = [ true, 'hello' ]"); 47.109 + map = (Map<Object, Object>) e.get("arr"); 47.110 + assertFalse(map.isEmpty()); 47.111 + assertTrue(map.containsKey("length")); 47.112 + assertTrue(map.containsValue("hello")); 47.113 + for (final Map.Entry<?, ?> ex : map.entrySet()) { 47.114 + final Object key = ex.getKey(); 47.115 + if (key.equals("0")) { 47.116 + assertEquals(ex.getValue(), Boolean.TRUE); 47.117 + count++; 47.118 + } else if (key.equals("1")) { 47.119 + assertEquals(ex.getValue(), "hello"); 47.120 + count++; 47.121 + } 47.122 + } 47.123 + assertEquals(count, 2); 47.124 + assertEquals(map.size(), 2); 47.125 + 47.126 + // add element 47.127 + map.put("2", "world"); 47.128 + assertEquals(map.get("2"), "world"); 47.129 + assertEquals(map.size(), 3); 47.130 + 47.131 + // remove all 47.132 + map.clear(); 47.133 + assertTrue(map.isEmpty()); 47.134 + assertEquals(e.eval("typeof arr[0]"), "undefined"); 47.135 + assertEquals(e.eval("typeof arr[1]"), "undefined"); 47.136 + assertEquals(e.eval("typeof arr[2]"), "undefined"); 47.137 + } 47.138 + 47.139 + @Test 47.140 + public void jsobjectTest() { 47.141 + final ScriptEngineManager m = new ScriptEngineManager(); 47.142 + final ScriptEngine e = m.getEngineByName("nashorn"); 47.143 + try { 47.144 + e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }"); 47.145 + final ScriptObjectMirror obj = (ScriptObjectMirror) e.get("obj"); 47.146 + 47.147 + // try basic get on existing properties 47.148 + if (!obj.getMember("bar").equals("hello")) { 47.149 + fail("obj.bar != 'hello'"); 47.150 + } 47.151 + 47.152 + if (!obj.getSlot(1).equals("world")) { 47.153 + fail("obj[1] != 'world'"); 47.154 + } 47.155 + 47.156 + if (!obj.callMember("func", new Object[0]).equals("hello")) { 47.157 + fail("obj.func() != 'hello'"); 47.158 + } 47.159 + 47.160 + // try setting properties 47.161 + obj.setMember("bar", "new-bar"); 47.162 + obj.setSlot(1, "new-element-1"); 47.163 + if (!obj.getMember("bar").equals("new-bar")) { 47.164 + fail("obj.bar != 'new-bar'"); 47.165 + } 47.166 + 47.167 + if (!obj.getSlot(1).equals("new-element-1")) { 47.168 + fail("obj[1] != 'new-element-1'"); 47.169 + } 47.170 + 47.171 + // try adding properties 47.172 + obj.setMember("prop", "prop-value"); 47.173 + obj.setSlot(12, "element-12"); 47.174 + if (!obj.getMember("prop").equals("prop-value")) { 47.175 + fail("obj.prop != 'prop-value'"); 47.176 + } 47.177 + 47.178 + if (!obj.getSlot(12).equals("element-12")) { 47.179 + fail("obj[12] != 'element-12'"); 47.180 + } 47.181 + 47.182 + // delete properties 47.183 + obj.removeMember("prop"); 47.184 + if ("prop-value".equals(obj.getMember("prop"))) { 47.185 + fail("obj.prop is not deleted!"); 47.186 + } 47.187 + 47.188 + // Simple eval tests 47.189 + assertEquals(obj.eval("typeof Object"), "function"); 47.190 + assertEquals(obj.eval("'nashorn'.substring(3)"), "horn"); 47.191 + } catch (final Exception exp) { 47.192 + exp.printStackTrace(); 47.193 + fail(exp.getMessage()); 47.194 + } 47.195 + } 47.196 + 47.197 + @Test 47.198 + public void scriptObjectMirrorToStringTest() { 47.199 + final ScriptEngineManager m = new ScriptEngineManager(); 47.200 + final ScriptEngine e = m.getEngineByName("nashorn"); 47.201 + try { 47.202 + final Object obj = e.eval("new TypeError('wrong type')"); 47.203 + assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value"); 47.204 + } catch (final Throwable t) { 47.205 + t.printStackTrace(); 47.206 + fail(t.getMessage()); 47.207 + } 47.208 + 47.209 + try { 47.210 + final Object obj = e.eval("function func() { print('hello'); }"); 47.211 + assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value"); 47.212 + } catch (final Throwable t) { 47.213 + t.printStackTrace(); 47.214 + fail(t.getMessage()); 47.215 + } 47.216 + } 47.217 + 47.218 + @Test 47.219 + public void mirrorNewObjectGlobalFunctionTest() throws ScriptException { 47.220 + final ScriptEngineManager m = new ScriptEngineManager(); 47.221 + final ScriptEngine e = m.getEngineByName("nashorn"); 47.222 + final ScriptEngine e2 = m.getEngineByName("nashorn"); 47.223 + 47.224 + e.eval("function func() {}"); 47.225 + e2.put("foo", e.get("func")); 47.226 + final ScriptObjectMirror e2global = (ScriptObjectMirror)e2.eval("this"); 47.227 + final Object newObj = ((ScriptObjectMirror)e2global.getMember("foo")).newObject(); 47.228 + assertTrue(newObj instanceof ScriptObjectMirror); 47.229 + } 47.230 + 47.231 + @Test 47.232 + public void mirrorNewObjectInstanceFunctionTest() throws ScriptException { 47.233 + final ScriptEngineManager m = new ScriptEngineManager(); 47.234 + final ScriptEngine e = m.getEngineByName("nashorn"); 47.235 + final ScriptEngine e2 = m.getEngineByName("nashorn"); 47.236 + 47.237 + e.eval("function func() {}"); 47.238 + e2.put("func", e.get("func")); 47.239 + final ScriptObjectMirror e2obj = (ScriptObjectMirror)e2.eval("({ foo: func })"); 47.240 + final Object newObj = ((ScriptObjectMirror)e2obj.getMember("foo")).newObject(); 47.241 + assertTrue(newObj instanceof ScriptObjectMirror); 47.242 + } 47.243 + 47.244 + @Test 47.245 + public void indexPropertiesExternalBufferTest() throws ScriptException { 47.246 + final ScriptEngineManager m = new ScriptEngineManager(); 47.247 + final ScriptEngine e = m.getEngineByName("nashorn"); 47.248 + final ScriptObjectMirror obj = (ScriptObjectMirror)e.eval("var obj = {}; obj"); 47.249 + final ByteBuffer buf = ByteBuffer.allocate(5); 47.250 + int i; 47.251 + for (i = 0; i < 5; i++) { 47.252 + buf.put(i, (byte)(i+10)); 47.253 + } 47.254 + obj.setIndexedPropertiesToExternalArrayData(buf); 47.255 + 47.256 + for (i = 0; i < 5; i++) { 47.257 + assertEquals((byte)(i+10), ((Number)e.eval("obj[" + i + "]")).byteValue()); 47.258 + } 47.259 + 47.260 + e.eval("for (i = 0; i < 5; i++) obj[i] = 0"); 47.261 + for (i = 0; i < 5; i++) { 47.262 + assertEquals((byte)0, ((Number)e.eval("obj[" + i + "]")).byteValue()); 47.263 + assertEquals((byte)0, buf.get(i)); 47.264 + } 47.265 + } 47.266 + 47.267 + @Test 47.268 + public void conversionTest() throws ScriptException { 47.269 + final ScriptEngineManager m = new ScriptEngineManager(); 47.270 + final ScriptEngine e = m.getEngineByName("nashorn"); 47.271 + final ScriptObjectMirror arr = (ScriptObjectMirror)e.eval("[33, 45, 23]"); 47.272 + final int[] intArr = arr.to(int[].class); 47.273 + assertEquals(intArr[0], 33); 47.274 + assertEquals(intArr[1], 45); 47.275 + assertEquals(intArr[2], 23); 47.276 + 47.277 + final List<?> list = arr.to(List.class); 47.278 + assertEquals(list.get(0), 33); 47.279 + assertEquals(list.get(1), 45); 47.280 + assertEquals(list.get(2), 23); 47.281 + 47.282 + ScriptObjectMirror obj = (ScriptObjectMirror)e.eval( 47.283 + "({ valueOf: function() { return 42 } })"); 47.284 + assertEquals(Double.valueOf(42.0), obj.to(Double.class)); 47.285 + 47.286 + obj = (ScriptObjectMirror)e.eval( 47.287 + "({ toString: function() { return 'foo' } })"); 47.288 + assertEquals("foo", obj.to(String.class)); 47.289 + } 47.290 + 47.291 + // @bug 8044000: Access to undefined property yields "null" instead of "undefined" 47.292 + @Test 47.293 + public void mapScriptObjectMirrorCallsiteTest() throws ScriptException { 47.294 + final ScriptEngineManager m = new ScriptEngineManager(); 47.295 + final ScriptEngine engine = m.getEngineByName("nashorn"); 47.296 + final String TEST_SCRIPT = "typeof obj.foo"; 47.297 + 47.298 + final Bindings global = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE); 47.299 + engine.eval("var obj = java.util.Collections.emptyMap()"); 47.300 + // this will drive callsite "obj.foo" of TEST_SCRIPT 47.301 + // to use "obj instanceof Map" as it's guard 47.302 + engine.eval(TEST_SCRIPT, global); 47.303 + // redefine 'obj' to be a script object 47.304 + engine.eval("obj = {}"); 47.305 + 47.306 + final Bindings newGlobal = engine.createBindings(); 47.307 + // transfer 'obj' from default global to new global 47.308 + // new global will get a ScriptObjectMirror wrapping 'obj' 47.309 + newGlobal.put("obj", global.get("obj")); 47.310 + 47.311 + // Every ScriptObjectMirror is a Map! If callsite "obj.foo" 47.312 + // does not see the new 'obj' is a ScriptObjectMirror, it'll 47.313 + // continue to use Map's get("obj.foo") instead of ScriptObjectMirror's 47.314 + // getMember("obj.foo") - thereby getting null instead of undefined 47.315 + assertEquals("undefined", engine.eval(TEST_SCRIPT, newGlobal)); 47.316 + } 47.317 + 47.318 + public interface MirrorCheckExample { 47.319 + Object test1(Object arg); 47.320 + Object test2(Object arg); 47.321 + boolean compare(Object o1, Object o2); 47.322 + } 47.323 + 47.324 + // @bug 8053910: ScriptObjectMirror causing havoc with Invocation interface 47.325 + @Test 47.326 + public void checkMirrorToObject() throws Exception { 47.327 + final ScriptEngineManager engineManager = new ScriptEngineManager(); 47.328 + final ScriptEngine engine = engineManager.getEngineByName("nashorn"); 47.329 + final Invocable invocable = (Invocable)engine; 47.330 + 47.331 + engine.eval("function test1(arg) { return { arg: arg }; }"); 47.332 + engine.eval("function test2(arg) { return arg; }"); 47.333 + engine.eval("function compare(arg1, arg2) { return arg1 == arg2; }"); 47.334 + 47.335 + final Map<String, Object> map = new HashMap<>(); 47.336 + map.put("option", true); 47.337 + 47.338 + final MirrorCheckExample example = invocable.getInterface(MirrorCheckExample.class); 47.339 + 47.340 + final Object value1 = invocable.invokeFunction("test1", map); 47.341 + final Object value2 = example.test1(map); 47.342 + final Object value3 = invocable.invokeFunction("test2", value2); 47.343 + final Object value4 = example.test2(value2); 47.344 + 47.345 + // check that Object type argument receives a ScriptObjectMirror 47.346 + // when ScriptObject is passed 47.347 + assertEquals(ScriptObjectMirror.class, value1.getClass()); 47.348 + assertEquals(ScriptObjectMirror.class, value2.getClass()); 47.349 + assertEquals(ScriptObjectMirror.class, value3.getClass()); 47.350 + assertEquals(ScriptObjectMirror.class, value4.getClass()); 47.351 + assertTrue((boolean)invocable.invokeFunction("compare", value1, value1)); 47.352 + assertTrue(example.compare(value1, value1)); 47.353 + assertTrue((boolean)invocable.invokeFunction("compare", value3, value4)); 47.354 + assertTrue(example.compare(value3, value4)); 47.355 + } 47.356 + 47.357 + // @bug 8053910: ScriptObjectMirror causing havoc with Invocation interface 47.358 + @Test 47.359 + public void mirrorUnwrapInterfaceMethod() throws Exception { 47.360 + final ScriptEngineManager engineManager = new ScriptEngineManager(); 47.361 + final ScriptEngine engine = engineManager.getEngineByName("nashorn"); 47.362 + final Invocable invocable = (Invocable)engine; 47.363 + engine.eval("function apply(obj) { " + 47.364 + " return obj instanceof Packages.jdk.nashorn.api.scripting.ScriptObjectMirror; " + 47.365 + "}"); 47.366 + @SuppressWarnings("unchecked") 47.367 + final Function<Object,Object> func = invocable.getInterface(Function.class); 47.368 + assertFalse((boolean)func.apply(engine.eval("({ x: 2 })"))); 47.369 + } 47.370 + 47.371 + // @bug 8055687: Wrong "this" passed to JSObject.eval call 47.372 + @Test 47.373 + public void checkThisForJSObjectEval() throws Exception { 47.374 + final ScriptEngineManager engineManager = new ScriptEngineManager(); 47.375 + final ScriptEngine e = engineManager.getEngineByName("nashorn"); 47.376 + final JSObject jsobj = (JSObject)e.eval("({foo: 23, bar: 'hello' })"); 47.377 + assertEquals(((Number)jsobj.eval("this.foo")).intValue(), 23); 47.378 + assertEquals(jsobj.eval("this.bar"), "hello"); 47.379 + assertEquals(jsobj.eval("String(this)"), "[object Object]"); 47.380 + final Object global = e.eval("this"); 47.381 + assertFalse(global.equals(jsobj.eval("this"))); 47.382 + } 47.383 + 47.384 + @Test 47.385 + public void topLevelAnonFuncStatement() throws Exception { 47.386 + final ScriptEngineManager engineManager = new ScriptEngineManager(); 47.387 + final ScriptEngine e = engineManager.getEngineByName("nashorn"); 47.388 + final JSObject func = (JSObject)e.eval("function(x) { return x + ' world' }"); 47.389 + assertTrue(func.isFunction()); 47.390 + assertEquals(func.call(e.eval("this"), "hello"), "hello world"); 47.391 + } 47.392 +}
48.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 48.2 +++ b/test/src/jdk/nashorn/api/scripting/test/VariableArityTestInterface.java Mon Apr 06 16:18:54 2015 +0530 48.3 @@ -0,0 +1,32 @@ 48.4 +/* 48.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 48.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 48.7 + * 48.8 + * This code is free software; you can redistribute it and/or modify it 48.9 + * under the terms of the GNU General Public License version 2 only, as 48.10 + * published by the Free Software Foundation. Oracle designates this 48.11 + * particular file as subject to the "Classpath" exception as provided 48.12 + * by Oracle in the LICENSE file that accompanied this code. 48.13 + * 48.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 48.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 48.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 48.17 + * version 2 for more details (a copy is included in the LICENSE file that 48.18 + * accompanied this code). 48.19 + * 48.20 + * You should have received a copy of the GNU General Public License version 48.21 + * 2 along with this work; if not, write to the Free Software Foundation, 48.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 48.23 + * 48.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 48.25 + * or visit www.oracle.com if you need additional information or have any 48.26 + * questions. 48.27 + */ 48.28 + 48.29 +package jdk.nashorn.api.scripting.test; 48.30 + 48.31 +@SuppressWarnings("javadoc") 48.32 +public interface VariableArityTestInterface { 48.33 + public String test1(int i, String... strings); 48.34 + public String test2(int i, String... strings); 48.35 +}
49.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 49.2 +++ b/test/src/jdk/nashorn/api/scripting/test/Window.java Mon Apr 06 16:18:54 2015 +0530 49.3 @@ -0,0 +1,88 @@ 49.4 +/* 49.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 49.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 49.7 + * 49.8 + * This code is free software; you can redistribute it and/or modify it 49.9 + * under the terms of the GNU General Public License version 2 only, as 49.10 + * published by the Free Software Foundation. Oracle designates this 49.11 + * particular file as subject to the "Classpath" exception as provided 49.12 + * by Oracle in the LICENSE file that accompanied this code. 49.13 + * 49.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 49.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 49.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 49.17 + * version 2 for more details (a copy is included in the LICENSE file that 49.18 + * accompanied this code). 49.19 + * 49.20 + * You should have received a copy of the GNU General Public License version 49.21 + * 2 along with this work; if not, write to the Free Software Foundation, 49.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 49.23 + * 49.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 49.25 + * or visit www.oracle.com if you need additional information or have any 49.26 + * questions. 49.27 + */ 49.28 + 49.29 +package jdk.nashorn.api.scripting.test; 49.30 + 49.31 +import java.util.Map; 49.32 +import javax.script.Bindings; 49.33 +import jdk.nashorn.api.scripting.JSObject; 49.34 +import jdk.nashorn.api.scripting.ScriptObjectMirror; 49.35 + 49.36 +@SuppressWarnings("javadoc") 49.37 +public class Window { 49.38 + 49.39 + private String location = "http://localhost:8080/window"; 49.40 + 49.41 + private WindowEventHandler onload = null; 49.42 + 49.43 + public void alert(final String message) { 49.44 + System.out.println("alert: " + message); 49.45 + } 49.46 + 49.47 + public String getLocation() { 49.48 + return location; 49.49 + } 49.50 + 49.51 + public void setLocation(final String location) { 49.52 + this.location = location; 49.53 + } 49.54 + 49.55 + public String item(final int index) { 49.56 + return Integer.toHexString(index); 49.57 + } 49.58 + 49.59 + public WindowEventHandler getOnload() { 49.60 + return onload; 49.61 + } 49.62 + 49.63 + public void setOnload(final WindowEventHandler onload) { 49.64 + this.onload = onload; 49.65 + } 49.66 + 49.67 + public static int setTimeout(final Window self, final String code, final int delay) { 49.68 + return self.setTimeout(code, delay); 49.69 + } 49.70 + 49.71 + public int setTimeout(final String code, final int delay) { 49.72 + System.out.println("window.setTimeout: " + delay + ", code: " + code); 49.73 + return 0; 49.74 + } 49.75 + 49.76 + public static Object funcJSObject(final JSObject jsobj) { 49.77 + return jsobj.getMember("foo"); 49.78 + } 49.79 + 49.80 + public static Object funcScriptObjectMirror(final ScriptObjectMirror sobj) { 49.81 + return sobj.get("foo"); 49.82 + } 49.83 + 49.84 + public static Object funcMap(final Map<?,?> map) { 49.85 + return map.get("foo"); 49.86 + } 49.87 + 49.88 + public static Object funcBindings(final Bindings bindings) { 49.89 + return bindings.get("foo"); 49.90 + } 49.91 +}
50.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 50.2 +++ b/test/src/jdk/nashorn/api/scripting/test/WindowEventHandler.java Mon Apr 06 16:18:54 2015 +0530 50.3 @@ -0,0 +1,31 @@ 50.4 +/* 50.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 50.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 50.7 + * 50.8 + * This code is free software; you can redistribute it and/or modify it 50.9 + * under the terms of the GNU General Public License version 2 only, as 50.10 + * published by the Free Software Foundation. Oracle designates this 50.11 + * particular file as subject to the "Classpath" exception as provided 50.12 + * by Oracle in the LICENSE file that accompanied this code. 50.13 + * 50.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 50.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 50.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 50.17 + * version 2 for more details (a copy is included in the LICENSE file that 50.18 + * accompanied this code). 50.19 + * 50.20 + * You should have received a copy of the GNU General Public License version 50.21 + * 2 along with this work; if not, write to the Free Software Foundation, 50.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 50.23 + * 50.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 50.25 + * or visit www.oracle.com if you need additional information or have any 50.26 + * questions. 50.27 + */ 50.28 + 50.29 +package jdk.nashorn.api.scripting.test; 50.30 + 50.31 +@SuppressWarnings("javadoc") 50.32 +public interface WindowEventHandler { 50.33 + public boolean loaded(); 50.34 +}
51.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 51.2 +++ b/test/src/jdk/nashorn/api/scripting/test/resources/func.js Mon Apr 06 16:18:54 2015 +0530 51.3 @@ -0,0 +1,42 @@ 51.4 +/* 51.5 + * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 51.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 51.7 + * 51.8 + * This code is free software; you can redistribute it and/or modify it 51.9 + * under the terms of the GNU General Public License version 2 only, as 51.10 + * published by the Free Software Foundation. Oracle designates this 51.11 + * particular file as subject to the "Classpath" exception as provided 51.12 + * by Oracle in the LICENSE file that accompanied this code. 51.13 + * 51.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 51.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 51.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 51.17 + * version 2 for more details (a copy is included in the LICENSE file that 51.18 + * accompanied this code). 51.19 + * 51.20 + * You should have received a copy of the GNU General Public License version 51.21 + * 2 along with this work; if not, write to the Free Software Foundation, 51.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 51.23 + * 51.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 51.25 + * or visit www.oracle.com if you need additional information or have any 51.26 + * questions. 51.27 + */ 51.28 + 51.29 +// This script is loaded from jdk.nashorn.api.scripting.ScopeTest to test script class sharing and reuse. 51.30 + 51.31 +var scopeVar = 1; 51.32 +var global = this; 51.33 +undefGlobal = this; 51.34 + 51.35 +function scopeTest() { 51.36 + if (this !== global) { 51.37 + throw new Error("this !== global"); 51.38 + } 51.39 + if (this !== undefGlobal) { 51.40 + throw new Error("this !== undefinedGlobal") 51.41 + } 51.42 + return scopeVar; 51.43 +} 51.44 + 51.45 +scopeTest();
52.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 52.2 +++ b/test/src/jdk/nashorn/api/scripting/test/resources/gettersetter.js Mon Apr 06 16:18:54 2015 +0530 52.3 @@ -0,0 +1,38 @@ 52.4 +/* 52.5 + * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 52.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 52.7 + * 52.8 + * This code is free software; you can redistribute it and/or modify it 52.9 + * under the terms of the GNU General Public License version 2 only, as 52.10 + * published by the Free Software Foundation. Oracle designates this 52.11 + * particular file as subject to the "Classpath" exception as provided 52.12 + * by Oracle in the LICENSE file that accompanied this code. 52.13 + * 52.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 52.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 52.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 52.17 + * version 2 for more details (a copy is included in the LICENSE file that 52.18 + * accompanied this code). 52.19 + * 52.20 + * You should have received a copy of the GNU General Public License version 52.21 + * 2 along with this work; if not, write to the Free Software Foundation, 52.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 52.23 + * 52.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 52.25 + * or visit www.oracle.com if you need additional information or have any 52.26 + * questions. 52.27 + */ 52.28 + 52.29 +// This script is loaded from jdk.nashorn.api.scripting.ScopeTest to test script class sharing and reuse. 52.30 + 52.31 +var v; 52.32 + 52.33 +Object.defineProperty(this, "accessor1", { 52.34 + get: function() { return v; }, 52.35 + set: function(n) { v = n; } 52.36 +}); 52.37 + 52.38 +Object.defineProperty(this, "accessor2", { 52.39 + get: function() { return x; }, 52.40 + set: function(n) { x = n; } 52.41 +});
53.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 53.2 +++ b/test/src/jdk/nashorn/api/scripting/test/resources/witheval.js Mon Apr 06 16:18:54 2015 +0530 53.3 @@ -0,0 +1,60 @@ 53.4 +/* 53.5 + * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 53.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 53.7 + * 53.8 + * This code is free software; you can redistribute it and/or modify it 53.9 + * under the terms of the GNU General Public License version 2 only, as 53.10 + * published by the Free Software Foundation. Oracle designates this 53.11 + * particular file as subject to the "Classpath" exception as provided 53.12 + * by Oracle in the LICENSE file that accompanied this code. 53.13 + * 53.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 53.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 53.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 53.17 + * version 2 for more details (a copy is included in the LICENSE file that 53.18 + * accompanied this code). 53.19 + * 53.20 + * You should have received a copy of the GNU General Public License version 53.21 + * 2 along with this work; if not, write to the Free Software Foundation, 53.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 53.23 + * 53.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 53.25 + * or visit www.oracle.com if you need additional information or have any 53.26 + * questions. 53.27 + */ 53.28 + 53.29 +// This script is loaded from jdk.nashorn.api.scripting.ScopeTest to test script class sharing and reuse. 53.30 + 53.31 +var a; 53.32 + 53.33 +function outer(p, e) { 53.34 + eval(e); 53.35 + with(p) { 53.36 + function inner() { 53.37 + a = 1; 53.38 + c = 10; 53.39 + if (a !== 1) { 53.40 + throw new Error("a !== 1"); 53.41 + } 53.42 + if (b !== 3) { 53.43 + throw new Error("b !== 3"); 53.44 + } 53.45 + if (c !== 10) { 53.46 + throw new Error("c !== 10"); 53.47 + } 53.48 + } 53.49 + inner(); 53.50 + } 53.51 +} 53.52 + 53.53 +outer({}, "b = 3;"); 53.54 + 53.55 +if (a !== 1) { 53.56 + throw new Error("a !== 1"); 53.57 +} 53.58 +if (b !== 3) { 53.59 + throw new Error("b !== 3"); 53.60 +} 53.61 +if (c !== 10) { 53.62 + throw new Error("c !== 10"); 53.63 +}
54.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 54.2 +++ b/test/src/jdk/nashorn/api/test/NashornSQLDriver.java Mon Apr 06 16:18:54 2015 +0530 54.3 @@ -0,0 +1,84 @@ 54.4 +/* 54.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 54.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 54.7 + * 54.8 + * This code is free software; you can redistribute it and/or modify it 54.9 + * under the terms of the GNU General Public License version 2 only, as 54.10 + * published by the Free Software Foundation. Oracle designates this 54.11 + * particular file as subject to the "Classpath" exception as provided 54.12 + * by Oracle in the LICENSE file that accompanied this code. 54.13 + * 54.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 54.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 54.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 54.17 + * version 2 for more details (a copy is included in the LICENSE file that 54.18 + * accompanied this code). 54.19 + * 54.20 + * You should have received a copy of the GNU General Public License version 54.21 + * 2 along with this work; if not, write to the Free Software Foundation, 54.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 54.23 + * 54.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 54.25 + * or visit www.oracle.com if you need additional information or have any 54.26 + * questions. 54.27 + */ 54.28 + 54.29 +package jdk.nashorn.api.test; 54.30 + 54.31 +import java.sql.Connection; 54.32 +import java.sql.Driver; 54.33 +import java.sql.DriverManager; 54.34 +import java.sql.DriverPropertyInfo; 54.35 +import java.sql.SQLException; 54.36 +import java.sql.SQLFeatureNotSupportedException; 54.37 +import java.util.Properties; 54.38 +import java.util.logging.Logger; 54.39 + 54.40 +/** 54.41 + * A dummy SQL driver for testing purpose. 54.42 + */ 54.43 +public final class NashornSQLDriver implements Driver { 54.44 + static { 54.45 + try { 54.46 + DriverManager.registerDriver(new NashornSQLDriver(), null); 54.47 + } catch (final SQLException se) { 54.48 + throw new RuntimeException(se); 54.49 + } 54.50 + } 54.51 + 54.52 + @Override 54.53 + public boolean acceptsURL(final String url) { 54.54 + return url.startsWith("jdbc:nashorn:"); 54.55 + } 54.56 + 54.57 + @Override 54.58 + public Connection connect(final String url, final Properties info) { 54.59 + throw new UnsupportedOperationException("I am a dummy!!"); 54.60 + } 54.61 + 54.62 + @Override 54.63 + public int getMajorVersion() { 54.64 + return -1; 54.65 + } 54.66 + 54.67 + @Override 54.68 + public int getMinorVersion() { 54.69 + return -1; 54.70 + } 54.71 + 54.72 + @Override 54.73 + public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) { 54.74 + return new DriverPropertyInfo[0]; 54.75 + } 54.76 + 54.77 + @Override 54.78 + public boolean jdbcCompliant() { 54.79 + // no way! 54.80 + return false; 54.81 + } 54.82 + 54.83 + @Override 54.84 + public Logger getParentLogger() throws SQLFeatureNotSupportedException { 54.85 + throw new SQLFeatureNotSupportedException(); 54.86 + } 54.87 +}
55.1 --- a/test/src/jdk/nashorn/internal/codegen/CompilerTest.java Wed Apr 01 13:22:52 2015 -0700 55.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 55.3 @@ -1,209 +0,0 @@ 55.4 -/* 55.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 55.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 55.7 - * 55.8 - * This code is free software; you can redistribute it and/or modify it 55.9 - * under the terms of the GNU General Public License version 2 only, as 55.10 - * published by the Free Software Foundation. Oracle designates this 55.11 - * particular file as subject to the "Classpath" exception as provided 55.12 - * by Oracle in the LICENSE file that accompanied this code. 55.13 - * 55.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 55.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 55.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 55.17 - * version 2 for more details (a copy is included in the LICENSE file that 55.18 - * accompanied this code). 55.19 - * 55.20 - * You should have received a copy of the GNU General Public License version 55.21 - * 2 along with this work; if not, write to the Free Software Foundation, 55.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 55.23 - * 55.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 55.25 - * or visit www.oracle.com if you need additional information or have any 55.26 - * questions. 55.27 - */ 55.28 - 55.29 -package jdk.nashorn.internal.codegen; 55.30 - 55.31 -import static jdk.nashorn.internal.runtime.Source.readFully; 55.32 -import static jdk.nashorn.internal.runtime.Source.sourceFor; 55.33 -import java.io.File; 55.34 -import java.io.PrintWriter; 55.35 -import java.io.StringWriter; 55.36 -import jdk.nashorn.internal.objects.Global; 55.37 -import jdk.nashorn.internal.runtime.Context; 55.38 -import jdk.nashorn.internal.runtime.ErrorManager; 55.39 -import jdk.nashorn.internal.runtime.ScriptFunction; 55.40 -import jdk.nashorn.internal.runtime.Source; 55.41 -import jdk.nashorn.internal.runtime.options.Options; 55.42 -import org.testng.Assert; 55.43 -import org.testng.annotations.AfterClass; 55.44 -import org.testng.annotations.BeforeClass; 55.45 -import org.testng.annotations.Test; 55.46 - 55.47 -/** 55.48 - * Tests to check Nashorn JS compiler - just compiler and not execution of scripts. 55.49 - */ 55.50 -@SuppressWarnings("javadoc") 55.51 -public class CompilerTest { 55.52 - private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("compilertest.verbose")); 55.53 - private static final boolean TEST262 = Boolean.valueOf(System.getProperty("compilertest.test262")); 55.54 - private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); 55.55 - private static final String TEST_NODE_DIR = System.getProperty("test.node.dir"); 55.56 - private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); 55.57 - 55.58 - interface TestFilter { 55.59 - public boolean exclude(File file, String content); 55.60 - } 55.61 - 55.62 - private static void log(final String msg) { 55.63 - org.testng.Reporter.log(msg, true); 55.64 - } 55.65 - 55.66 - private Context context; 55.67 - private Global global; 55.68 - 55.69 - @BeforeClass 55.70 - public void setupTest() { 55.71 - final Options options = new Options("nashorn"); 55.72 - options.set("anon.functions", true); 55.73 - options.set("compile.only", true); 55.74 - options.set("print.ast", true); 55.75 - options.set("print.parse", true); 55.76 - options.set("scripting", true); 55.77 - options.set("const.as.var", true); 55.78 - options.set("verify.code", true); 55.79 - 55.80 - final ErrorManager errors = new ErrorManager() { 55.81 - @Override 55.82 - public void error(final String msg) { 55.83 - log(msg); 55.84 - } 55.85 - }; 55.86 - 55.87 - final StringWriter sw = new StringWriter(); 55.88 - final PrintWriter pw = new PrintWriter(sw); 55.89 - this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader()); 55.90 - this.global = context.createGlobal(); 55.91 - } 55.92 - 55.93 - @AfterClass 55.94 - public void tearDownTest() { 55.95 - this.context = null; 55.96 - this.global = null; 55.97 - } 55.98 - 55.99 - @Test 55.100 - public void compileAllTests() { 55.101 - if (TEST262) { 55.102 - compileTestSet(new File(TEST262_SUITE_DIR), new TestFilter() { 55.103 - @Override 55.104 - public boolean exclude(final File file, final String content) { 55.105 - return content != null && content.contains("@negative"); 55.106 - } 55.107 - }); 55.108 - } 55.109 - compileTestSet(new File(TEST_BASIC_DIR), new TestFilter() { 55.110 - @Override 55.111 - public boolean exclude(final File file, final String content) { 55.112 - return file.getName().equals("es6"); 55.113 - } 55.114 - }); 55.115 - compileTestSet(new File(TEST_NODE_DIR, "node"), null); 55.116 - compileTestSet(new File(TEST_NODE_DIR, "src"), null); 55.117 - } 55.118 - 55.119 - private void compileTestSet(final File testSetDir, final TestFilter filter) { 55.120 - passed = 0; 55.121 - failed = 0; 55.122 - skipped = 0; 55.123 - if (! testSetDir.isDirectory()) { 55.124 - log("WARNING: " + testSetDir + " not found or not a directory"); 55.125 - return; 55.126 - } 55.127 - log(testSetDir.getAbsolutePath()); 55.128 - compileJSDirectory(testSetDir, filter); 55.129 - 55.130 - log(testSetDir + " compile done!"); 55.131 - log("compile ok: " + passed); 55.132 - log("compile failed: " + failed); 55.133 - log("compile skipped: " + skipped); 55.134 - if (failed != 0) { 55.135 - Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath()); 55.136 - } 55.137 - } 55.138 - 55.139 - // number of scripts that compiled fine 55.140 - private int passed; 55.141 - // number of scripts resulting in compile failure 55.142 - private int failed; 55.143 - // scripts that were skipped - all tests with @negative are 55.144 - // skipped for now. 55.145 - private int skipped; 55.146 - 55.147 - private void compileJSDirectory(final File dir, final TestFilter filter) { 55.148 - if (filter != null && filter.exclude(dir, null)) { 55.149 - return; 55.150 - } 55.151 - for (final File f : dir.listFiles()) { 55.152 - if (f.isDirectory()) { 55.153 - compileJSDirectory(f, filter); 55.154 - } else if (f.getName().endsWith(".js")) { 55.155 - compileJSFile(f, filter); 55.156 - } 55.157 - } 55.158 - } 55.159 - 55.160 - private void compileJSFile(final File file, final TestFilter filter) { 55.161 - if (VERBOSE) { 55.162 - log("Begin compiling " + file.getAbsolutePath()); 55.163 - } 55.164 - 55.165 - final Global oldGlobal = Context.getGlobal(); 55.166 - final boolean globalChanged = (oldGlobal != global); 55.167 - 55.168 - try { 55.169 - final char[] buffer = readFully(file); 55.170 - boolean excluded = false; 55.171 - 55.172 - if (filter != null) { 55.173 - final String content = new String(buffer); 55.174 - excluded = filter.exclude(file, content); 55.175 - } 55.176 - 55.177 - if (excluded) { 55.178 - if (VERBOSE) { 55.179 - log("Skipping " + file.getAbsolutePath()); 55.180 - } 55.181 - skipped++; 55.182 - return; 55.183 - } 55.184 - 55.185 - if (globalChanged) { 55.186 - Context.setGlobal(global); 55.187 - } 55.188 - final Source source = sourceFor(file.getAbsolutePath(), buffer); 55.189 - final ScriptFunction script = context.compileScript(source, global); 55.190 - if (script == null || context.getErrorManager().getNumberOfErrors() > 0) { 55.191 - log("Compile failed: " + file.getAbsolutePath()); 55.192 - failed++; 55.193 - } else { 55.194 - passed++; 55.195 - } 55.196 - } catch (final Throwable t) { 55.197 - log("Compile failed: " + file.getAbsolutePath() + " : " + t); 55.198 - if (VERBOSE) { 55.199 - t.printStackTrace(System.out); 55.200 - } 55.201 - failed++; 55.202 - } finally { 55.203 - if (globalChanged) { 55.204 - Context.setGlobal(oldGlobal); 55.205 - } 55.206 - } 55.207 - 55.208 - if (VERBOSE) { 55.209 - log("Done compiling " + file.getAbsolutePath()); 55.210 - } 55.211 - } 55.212 -}
56.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 56.2 +++ b/test/src/jdk/nashorn/internal/codegen/test/CompilerTest.java Mon Apr 06 16:18:54 2015 +0530 56.3 @@ -0,0 +1,209 @@ 56.4 +/* 56.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 56.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 56.7 + * 56.8 + * This code is free software; you can redistribute it and/or modify it 56.9 + * under the terms of the GNU General Public License version 2 only, as 56.10 + * published by the Free Software Foundation. Oracle designates this 56.11 + * particular file as subject to the "Classpath" exception as provided 56.12 + * by Oracle in the LICENSE file that accompanied this code. 56.13 + * 56.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 56.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 56.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 56.17 + * version 2 for more details (a copy is included in the LICENSE file that 56.18 + * accompanied this code). 56.19 + * 56.20 + * You should have received a copy of the GNU General Public License version 56.21 + * 2 along with this work; if not, write to the Free Software Foundation, 56.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 56.23 + * 56.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 56.25 + * or visit www.oracle.com if you need additional information or have any 56.26 + * questions. 56.27 + */ 56.28 + 56.29 +package jdk.nashorn.internal.codegen.test; 56.30 + 56.31 +import static jdk.nashorn.internal.runtime.Source.readFully; 56.32 +import static jdk.nashorn.internal.runtime.Source.sourceFor; 56.33 +import java.io.File; 56.34 +import java.io.PrintWriter; 56.35 +import java.io.StringWriter; 56.36 +import jdk.nashorn.internal.objects.Global; 56.37 +import jdk.nashorn.internal.runtime.Context; 56.38 +import jdk.nashorn.internal.runtime.ErrorManager; 56.39 +import jdk.nashorn.internal.runtime.ScriptFunction; 56.40 +import jdk.nashorn.internal.runtime.Source; 56.41 +import jdk.nashorn.internal.runtime.options.Options; 56.42 +import org.testng.Assert; 56.43 +import org.testng.annotations.AfterClass; 56.44 +import org.testng.annotations.BeforeClass; 56.45 +import org.testng.annotations.Test; 56.46 + 56.47 +/** 56.48 + * Tests to check Nashorn JS compiler - just compiler and not execution of scripts. 56.49 + */ 56.50 +@SuppressWarnings("javadoc") 56.51 +public class CompilerTest { 56.52 + private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("compilertest.verbose")); 56.53 + private static final boolean TEST262 = Boolean.valueOf(System.getProperty("compilertest.test262")); 56.54 + private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); 56.55 + private static final String TEST_NODE_DIR = System.getProperty("test.node.dir"); 56.56 + private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); 56.57 + 56.58 + interface TestFilter { 56.59 + public boolean exclude(File file, String content); 56.60 + } 56.61 + 56.62 + private static void log(final String msg) { 56.63 + org.testng.Reporter.log(msg, true); 56.64 + } 56.65 + 56.66 + private Context context; 56.67 + private Global global; 56.68 + 56.69 + @BeforeClass 56.70 + public void setupTest() { 56.71 + final Options options = new Options("nashorn"); 56.72 + options.set("anon.functions", true); 56.73 + options.set("compile.only", true); 56.74 + options.set("print.ast", true); 56.75 + options.set("print.parse", true); 56.76 + options.set("scripting", true); 56.77 + options.set("const.as.var", true); 56.78 + options.set("verify.code", true); 56.79 + 56.80 + final ErrorManager errors = new ErrorManager() { 56.81 + @Override 56.82 + public void error(final String msg) { 56.83 + log(msg); 56.84 + } 56.85 + }; 56.86 + 56.87 + final StringWriter sw = new StringWriter(); 56.88 + final PrintWriter pw = new PrintWriter(sw); 56.89 + this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader()); 56.90 + this.global = context.createGlobal(); 56.91 + } 56.92 + 56.93 + @AfterClass 56.94 + public void tearDownTest() { 56.95 + this.context = null; 56.96 + this.global = null; 56.97 + } 56.98 + 56.99 + @Test 56.100 + public void compileAllTests() { 56.101 + if (TEST262) { 56.102 + compileTestSet(new File(TEST262_SUITE_DIR), new TestFilter() { 56.103 + @Override 56.104 + public boolean exclude(final File file, final String content) { 56.105 + return content != null && content.contains("@negative"); 56.106 + } 56.107 + }); 56.108 + } 56.109 + compileTestSet(new File(TEST_BASIC_DIR), new TestFilter() { 56.110 + @Override 56.111 + public boolean exclude(final File file, final String content) { 56.112 + return file.getName().equals("es6"); 56.113 + } 56.114 + }); 56.115 + compileTestSet(new File(TEST_NODE_DIR, "node"), null); 56.116 + compileTestSet(new File(TEST_NODE_DIR, "src"), null); 56.117 + } 56.118 + 56.119 + private void compileTestSet(final File testSetDir, final TestFilter filter) { 56.120 + passed = 0; 56.121 + failed = 0; 56.122 + skipped = 0; 56.123 + if (! testSetDir.isDirectory()) { 56.124 + log("WARNING: " + testSetDir + " not found or not a directory"); 56.125 + return; 56.126 + } 56.127 + log(testSetDir.getAbsolutePath()); 56.128 + compileJSDirectory(testSetDir, filter); 56.129 + 56.130 + log(testSetDir + " compile done!"); 56.131 + log("compile ok: " + passed); 56.132 + log("compile failed: " + failed); 56.133 + log("compile skipped: " + skipped); 56.134 + if (failed != 0) { 56.135 + Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath()); 56.136 + } 56.137 + } 56.138 + 56.139 + // number of scripts that compiled fine 56.140 + private int passed; 56.141 + // number of scripts resulting in compile failure 56.142 + private int failed; 56.143 + // scripts that were skipped - all tests with @negative are 56.144 + // skipped for now. 56.145 + private int skipped; 56.146 + 56.147 + private void compileJSDirectory(final File dir, final TestFilter filter) { 56.148 + if (filter != null && filter.exclude(dir, null)) { 56.149 + return; 56.150 + } 56.151 + for (final File f : dir.listFiles()) { 56.152 + if (f.isDirectory()) { 56.153 + compileJSDirectory(f, filter); 56.154 + } else if (f.getName().endsWith(".js")) { 56.155 + compileJSFile(f, filter); 56.156 + } 56.157 + } 56.158 + } 56.159 + 56.160 + private void compileJSFile(final File file, final TestFilter filter) { 56.161 + if (VERBOSE) { 56.162 + log("Begin compiling " + file.getAbsolutePath()); 56.163 + } 56.164 + 56.165 + final Global oldGlobal = Context.getGlobal(); 56.166 + final boolean globalChanged = (oldGlobal != global); 56.167 + 56.168 + try { 56.169 + final char[] buffer = readFully(file); 56.170 + boolean excluded = false; 56.171 + 56.172 + if (filter != null) { 56.173 + final String content = new String(buffer); 56.174 + excluded = filter.exclude(file, content); 56.175 + } 56.176 + 56.177 + if (excluded) { 56.178 + if (VERBOSE) { 56.179 + log("Skipping " + file.getAbsolutePath()); 56.180 + } 56.181 + skipped++; 56.182 + return; 56.183 + } 56.184 + 56.185 + if (globalChanged) { 56.186 + Context.setGlobal(global); 56.187 + } 56.188 + final Source source = sourceFor(file.getAbsolutePath(), buffer); 56.189 + final ScriptFunction script = context.compileScript(source, global); 56.190 + if (script == null || context.getErrorManager().getNumberOfErrors() > 0) { 56.191 + log("Compile failed: " + file.getAbsolutePath()); 56.192 + failed++; 56.193 + } else { 56.194 + passed++; 56.195 + } 56.196 + } catch (final Throwable t) { 56.197 + log("Compile failed: " + file.getAbsolutePath() + " : " + t); 56.198 + if (VERBOSE) { 56.199 + t.printStackTrace(System.out); 56.200 + } 56.201 + failed++; 56.202 + } finally { 56.203 + if (globalChanged) { 56.204 + Context.setGlobal(oldGlobal); 56.205 + } 56.206 + } 56.207 + 56.208 + if (VERBOSE) { 56.209 + log("Done compiling " + file.getAbsolutePath()); 56.210 + } 56.211 + } 56.212 +}
57.1 --- a/test/src/jdk/nashorn/internal/parser/ParserTest.java Wed Apr 01 13:22:52 2015 -0700 57.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 57.3 @@ -1,189 +0,0 @@ 57.4 -/* 57.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 57.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 57.7 - * 57.8 - * This code is free software; you can redistribute it and/or modify it 57.9 - * under the terms of the GNU General Public License version 2 only, as 57.10 - * published by the Free Software Foundation. Oracle designates this 57.11 - * particular file as subject to the "Classpath" exception as provided 57.12 - * by Oracle in the LICENSE file that accompanied this code. 57.13 - * 57.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 57.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 57.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 57.17 - * version 2 for more details (a copy is included in the LICENSE file that 57.18 - * accompanied this code). 57.19 - * 57.20 - * You should have received a copy of the GNU General Public License version 57.21 - * 2 along with this work; if not, write to the Free Software Foundation, 57.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 57.23 - * 57.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 57.25 - * or visit www.oracle.com if you need additional information or have any 57.26 - * questions. 57.27 - */ 57.28 - 57.29 -package jdk.nashorn.internal.parser; 57.30 - 57.31 -import static jdk.nashorn.internal.runtime.Source.readFully; 57.32 -import static jdk.nashorn.internal.runtime.Source.sourceFor; 57.33 -import java.io.File; 57.34 -import jdk.nashorn.internal.runtime.Context; 57.35 -import jdk.nashorn.internal.runtime.ErrorManager; 57.36 -import jdk.nashorn.internal.runtime.Source; 57.37 -import jdk.nashorn.internal.runtime.options.Options; 57.38 -import org.testng.Assert; 57.39 -import org.testng.annotations.AfterClass; 57.40 -import org.testng.annotations.BeforeClass; 57.41 -import org.testng.annotations.Test; 57.42 - 57.43 -/** 57.44 - * Run tests to check Nashorn's parser. 57.45 - */ 57.46 -@SuppressWarnings("javadoc") 57.47 -public class ParserTest { 57.48 - private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("parsertest.verbose")); 57.49 - private static final boolean TEST262 = Boolean.valueOf(System.getProperty("parsertest.test262")); 57.50 - 57.51 - private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); 57.52 - private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); 57.53 - 57.54 - 57.55 - interface TestFilter { 57.56 - public boolean exclude(File file, String content); 57.57 - } 57.58 - 57.59 - private static void log(final String msg) { 57.60 - org.testng.Reporter.log(msg, true); 57.61 - } 57.62 - 57.63 - private Context context; 57.64 - 57.65 - @BeforeClass 57.66 - public void setupTest() { 57.67 - final Options options = new Options("nashorn"); 57.68 - options.set("anon.functions", true); 57.69 - options.set("parse.only", true); 57.70 - options.set("scripting", true); 57.71 - options.set("const.as.var", true); 57.72 - 57.73 - final ErrorManager errors = new ErrorManager(); 57.74 - this.context = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 57.75 - } 57.76 - 57.77 - @AfterClass 57.78 - public void tearDownTest() { 57.79 - this.context = null; 57.80 - } 57.81 - 57.82 - @Test 57.83 - public void parseAllTests() { 57.84 - if (TEST262) { 57.85 - parseTestSet(TEST262_SUITE_DIR, new TestFilter() { 57.86 - @Override 57.87 - public boolean exclude(final File file, final String content) { 57.88 - return content != null && content.contains("@negative"); 57.89 - } 57.90 - }); 57.91 - } 57.92 - parseTestSet(TEST_BASIC_DIR, new TestFilter() { 57.93 - @Override 57.94 - public boolean exclude(final File file, final String content) { 57.95 - return file.getName().equals("es6"); 57.96 - } 57.97 - }); 57.98 - } 57.99 - 57.100 - private void parseTestSet(final String testSet, final TestFilter filter) { 57.101 - passed = 0; 57.102 - failed = 0; 57.103 - skipped = 0; 57.104 - 57.105 - final File testSetDir = new File(testSet); 57.106 - if (! testSetDir.isDirectory()) { 57.107 - log("WARNING: " + testSetDir + " not found or not a directory"); 57.108 - return; 57.109 - } 57.110 - log(testSetDir.getAbsolutePath()); 57.111 - parseJSDirectory(testSetDir, filter); 57.112 - 57.113 - log(testSet + " parse done!"); 57.114 - log("parse ok: " + passed); 57.115 - log("parse failed: " + failed); 57.116 - log("parse skipped: " + skipped); 57.117 - if (failed != 0) { 57.118 - Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath()); 57.119 - } 57.120 - } 57.121 - 57.122 - // number of scripts that parsed fine 57.123 - private int passed; 57.124 - // number of scripts resulting in parse failure 57.125 - private int failed; 57.126 - // scripts that were skipped - all tests with @negative are 57.127 - // skipped for now. 57.128 - private int skipped; 57.129 - 57.130 - private void parseJSDirectory(final File dir, final TestFilter filter) { 57.131 - if (filter != null && filter.exclude(dir, null)) { 57.132 - return; 57.133 - } 57.134 - for (final File f : dir.listFiles()) { 57.135 - if (f.isDirectory()) { 57.136 - parseJSDirectory(f, filter); 57.137 - } else if (f.getName().endsWith(".js")) { 57.138 - parseJSFile(f, filter); 57.139 - } 57.140 - } 57.141 - } 57.142 - 57.143 - private void parseJSFile(final File file, final TestFilter filter) { 57.144 - if (VERBOSE) { 57.145 - log("Begin parsing " + file.getAbsolutePath()); 57.146 - } 57.147 - 57.148 - try { 57.149 - final char[] buffer = readFully(file); 57.150 - boolean excluded = false; 57.151 - if (filter != null) { 57.152 - final String content = new String(buffer); 57.153 - excluded = filter.exclude(file, content); 57.154 - } 57.155 - 57.156 - if (excluded) { 57.157 - if (VERBOSE) { 57.158 - log("Skipping " + file.getAbsolutePath()); 57.159 - } 57.160 - skipped++; 57.161 - return; 57.162 - } 57.163 - 57.164 - final ErrorManager errors = new ErrorManager() { 57.165 - @Override 57.166 - public void error(final String msg) { 57.167 - log(msg); 57.168 - } 57.169 - }; 57.170 - errors.setLimit(0); 57.171 - final Source source = sourceFor(file.getAbsolutePath(), buffer); 57.172 - new Parser(context.getEnv(), source, errors, context.getEnv()._strict, null).parse(); 57.173 - if (errors.getNumberOfErrors() > 0) { 57.174 - log("Parse failed: " + file.getAbsolutePath()); 57.175 - failed++; 57.176 - } else { 57.177 - passed++; 57.178 - } 57.179 - } catch (final Throwable exp) { 57.180 - exp.printStackTrace(); 57.181 - log("Parse failed: " + file.getAbsolutePath() + " : " + exp); 57.182 - if (VERBOSE) { 57.183 - exp.printStackTrace(System.out); 57.184 - } 57.185 - failed++; 57.186 - } 57.187 - 57.188 - if (VERBOSE) { 57.189 - log("Done parsing " + file.getAbsolutePath()); 57.190 - } 57.191 - } 57.192 -}
58.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 58.2 +++ b/test/src/jdk/nashorn/internal/parser/test/ParserTest.java Mon Apr 06 16:18:54 2015 +0530 58.3 @@ -0,0 +1,190 @@ 58.4 +/* 58.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 58.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 58.7 + * 58.8 + * This code is free software; you can redistribute it and/or modify it 58.9 + * under the terms of the GNU General Public License version 2 only, as 58.10 + * published by the Free Software Foundation. Oracle designates this 58.11 + * particular file as subject to the "Classpath" exception as provided 58.12 + * by Oracle in the LICENSE file that accompanied this code. 58.13 + * 58.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 58.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 58.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 58.17 + * version 2 for more details (a copy is included in the LICENSE file that 58.18 + * accompanied this code). 58.19 + * 58.20 + * You should have received a copy of the GNU General Public License version 58.21 + * 2 along with this work; if not, write to the Free Software Foundation, 58.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 58.23 + * 58.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 58.25 + * or visit www.oracle.com if you need additional information or have any 58.26 + * questions. 58.27 + */ 58.28 + 58.29 +package jdk.nashorn.internal.parser.test; 58.30 + 58.31 +import static jdk.nashorn.internal.runtime.Source.readFully; 58.32 +import static jdk.nashorn.internal.runtime.Source.sourceFor; 58.33 +import java.io.File; 58.34 +import jdk.nashorn.internal.parser.Parser; 58.35 +import jdk.nashorn.internal.runtime.Context; 58.36 +import jdk.nashorn.internal.runtime.ErrorManager; 58.37 +import jdk.nashorn.internal.runtime.Source; 58.38 +import jdk.nashorn.internal.runtime.options.Options; 58.39 +import org.testng.Assert; 58.40 +import org.testng.annotations.AfterClass; 58.41 +import org.testng.annotations.BeforeClass; 58.42 +import org.testng.annotations.Test; 58.43 + 58.44 +/** 58.45 + * Run tests to check Nashorn's parser. 58.46 + */ 58.47 +@SuppressWarnings("javadoc") 58.48 +public class ParserTest { 58.49 + private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("parsertest.verbose")); 58.50 + private static final boolean TEST262 = Boolean.valueOf(System.getProperty("parsertest.test262")); 58.51 + 58.52 + private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); 58.53 + private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); 58.54 + 58.55 + 58.56 + interface TestFilter { 58.57 + public boolean exclude(File file, String content); 58.58 + } 58.59 + 58.60 + private static void log(final String msg) { 58.61 + org.testng.Reporter.log(msg, true); 58.62 + } 58.63 + 58.64 + private Context context; 58.65 + 58.66 + @BeforeClass 58.67 + public void setupTest() { 58.68 + final Options options = new Options("nashorn"); 58.69 + options.set("anon.functions", true); 58.70 + options.set("parse.only", true); 58.71 + options.set("scripting", true); 58.72 + options.set("const.as.var", true); 58.73 + 58.74 + final ErrorManager errors = new ErrorManager(); 58.75 + this.context = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 58.76 + } 58.77 + 58.78 + @AfterClass 58.79 + public void tearDownTest() { 58.80 + this.context = null; 58.81 + } 58.82 + 58.83 + @Test 58.84 + public void parseAllTests() { 58.85 + if (TEST262) { 58.86 + parseTestSet(TEST262_SUITE_DIR, new TestFilter() { 58.87 + @Override 58.88 + public boolean exclude(final File file, final String content) { 58.89 + return content != null && content.contains("@negative"); 58.90 + } 58.91 + }); 58.92 + } 58.93 + parseTestSet(TEST_BASIC_DIR, new TestFilter() { 58.94 + @Override 58.95 + public boolean exclude(final File file, final String content) { 58.96 + return file.getName().equals("es6"); 58.97 + } 58.98 + }); 58.99 + } 58.100 + 58.101 + private void parseTestSet(final String testSet, final TestFilter filter) { 58.102 + passed = 0; 58.103 + failed = 0; 58.104 + skipped = 0; 58.105 + 58.106 + final File testSetDir = new File(testSet); 58.107 + if (! testSetDir.isDirectory()) { 58.108 + log("WARNING: " + testSetDir + " not found or not a directory"); 58.109 + return; 58.110 + } 58.111 + log(testSetDir.getAbsolutePath()); 58.112 + parseJSDirectory(testSetDir, filter); 58.113 + 58.114 + log(testSet + " parse done!"); 58.115 + log("parse ok: " + passed); 58.116 + log("parse failed: " + failed); 58.117 + log("parse skipped: " + skipped); 58.118 + if (failed != 0) { 58.119 + Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath()); 58.120 + } 58.121 + } 58.122 + 58.123 + // number of scripts that parsed fine 58.124 + private int passed; 58.125 + // number of scripts resulting in parse failure 58.126 + private int failed; 58.127 + // scripts that were skipped - all tests with @negative are 58.128 + // skipped for now. 58.129 + private int skipped; 58.130 + 58.131 + private void parseJSDirectory(final File dir, final TestFilter filter) { 58.132 + if (filter != null && filter.exclude(dir, null)) { 58.133 + return; 58.134 + } 58.135 + for (final File f : dir.listFiles()) { 58.136 + if (f.isDirectory()) { 58.137 + parseJSDirectory(f, filter); 58.138 + } else if (f.getName().endsWith(".js")) { 58.139 + parseJSFile(f, filter); 58.140 + } 58.141 + } 58.142 + } 58.143 + 58.144 + private void parseJSFile(final File file, final TestFilter filter) { 58.145 + if (VERBOSE) { 58.146 + log("Begin parsing " + file.getAbsolutePath()); 58.147 + } 58.148 + 58.149 + try { 58.150 + final char[] buffer = readFully(file); 58.151 + boolean excluded = false; 58.152 + if (filter != null) { 58.153 + final String content = new String(buffer); 58.154 + excluded = filter.exclude(file, content); 58.155 + } 58.156 + 58.157 + if (excluded) { 58.158 + if (VERBOSE) { 58.159 + log("Skipping " + file.getAbsolutePath()); 58.160 + } 58.161 + skipped++; 58.162 + return; 58.163 + } 58.164 + 58.165 + final ErrorManager errors = new ErrorManager() { 58.166 + @Override 58.167 + public void error(final String msg) { 58.168 + log(msg); 58.169 + } 58.170 + }; 58.171 + errors.setLimit(0); 58.172 + final Source source = sourceFor(file.getAbsolutePath(), buffer); 58.173 + new Parser(context.getEnv(), source, errors, context.getEnv()._strict, null).parse(); 58.174 + if (errors.getNumberOfErrors() > 0) { 58.175 + log("Parse failed: " + file.getAbsolutePath()); 58.176 + failed++; 58.177 + } else { 58.178 + passed++; 58.179 + } 58.180 + } catch (final Throwable exp) { 58.181 + exp.printStackTrace(); 58.182 + log("Parse failed: " + file.getAbsolutePath() + " : " + exp); 58.183 + if (VERBOSE) { 58.184 + exp.printStackTrace(System.out); 58.185 + } 58.186 + failed++; 58.187 + } 58.188 + 58.189 + if (VERBOSE) { 58.190 + log("Done parsing " + file.getAbsolutePath()); 58.191 + } 58.192 + } 58.193 +}
59.1 --- a/test/src/jdk/nashorn/internal/runtime/ClassFilterTest.java Wed Apr 01 13:22:52 2015 -0700 59.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 59.3 @@ -1,185 +0,0 @@ 59.4 -/* 59.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 59.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 59.7 - * 59.8 - * This code is free software; you can redistribute it and/or modify it 59.9 - * under the terms of the GNU General Public License version 2 only, as 59.10 - * published by the Free Software Foundation. Oracle designates this 59.11 - * particular file as subject to the "Classpath" exception as provided 59.12 - * by Oracle in the LICENSE file that accompanied this code. 59.13 - * 59.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 59.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 59.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 59.17 - * version 2 for more details (a copy is included in the LICENSE file that 59.18 - * accompanied this code). 59.19 - * 59.20 - * You should have received a copy of the GNU General Public License version 59.21 - * 2 along with this work; if not, write to the Free Software Foundation, 59.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 59.23 - * 59.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 59.25 - * or visit www.oracle.com if you need additional information or have any 59.26 - * questions. 59.27 - */ 59.28 - 59.29 -package jdk.nashorn.internal.runtime; 59.30 - 59.31 -import static org.testng.Assert.fail; 59.32 -import java.io.File; 59.33 -import javax.script.ScriptEngine; 59.34 -import javax.script.ScriptException; 59.35 -import jdk.nashorn.api.scripting.ClassFilter; 59.36 -import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 59.37 -import jdk.nashorn.api.scripting.URLReader; 59.38 -import jdk.nashorn.internal.test.framework.TestFinder; 59.39 -import org.testng.annotations.Test; 59.40 - 59.41 -@SuppressWarnings("javadoc") 59.42 -public class ClassFilterTest { 59.43 - private static final String NASHORN_CODE_CACHE = "nashorn.persistent.code.cache"; 59.44 - private static final String CLASSFILTER_CODE_CACHE = "build/classfilter_nashorn_code_cache"; 59.45 - 59.46 - // @Test 59.47 - // This test takes too much time for basic "ant clean test" run. 59.48 - // Given that "allow-all-java-classes" is equivalent to no java class 59.49 - // filter and external tests don't access any java, not sure if this 59.50 - // test contributes much. We need faster "ant clean test" cycle for 59.51 - // developers. 59.52 - public void runExternalJsTest() { 59.53 - final String[] paths = new String[]{ 59.54 - "test/script/basic/compile-octane.js", 59.55 - "test/script/basic/jquery.js", 59.56 - "test/script/basic/prototype.js", 59.57 - "test/script/basic/runsunspider.js", 59.58 - "test/script/basic/underscore.js", 59.59 - "test/script/basic/yui.js", 59.60 - "test/script/basic/run-octane.js" 59.61 - }; 59.62 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 59.63 - for (final String path : paths) { 59.64 - final ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter()); 59.65 - try { 59.66 - engine.eval(new URLReader(new File(path).toURI().toURL())); 59.67 - } catch (final Exception e) { 59.68 - fail("Script " + path + " fails with exception :" + e.getMessage()); 59.69 - } 59.70 - } 59.71 - } 59.72 - 59.73 - @Test 59.74 - public void noJavaOptionTest() { 59.75 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 59.76 - final ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter()); 59.77 - try { 59.78 - engine.eval("var str = Java.type('java.lang.String');"); 59.79 - fail("TypeError should have been thrown"); 59.80 - } catch (final ScriptException e) { 59.81 - //emtpy 59.82 - } 59.83 - } 59.84 - 59.85 - @Test 59.86 - public void securityTest() { 59.87 - if (System.getSecurityManager() == null) { 59.88 - return; 59.89 - } 59.90 - 59.91 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 59.92 - final ScriptEngine engine = factory.getScriptEngine(getClassFilter()); 59.93 - try { 59.94 - engine.eval("var thread = Java.type('sun.misc.Unsafe')"); 59.95 - fail("SecurityException should have been thrown"); 59.96 - } catch (final Exception e) { 59.97 - //empty 59.98 - } 59.99 - try { 59.100 - engine.eval("var thread = new sun.misc.Unsafe()"); 59.101 - fail("SecurityException should have been thrown"); 59.102 - } catch (final Exception e) { 59.103 - //empty 59.104 - } 59.105 - try { 59.106 - engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})"); 59.107 - fail("TypeError should have been thrown"); 59.108 - } catch (final Exception e) { 59.109 - //empty 59.110 - } 59.111 - try { 59.112 - engine.eval("java.lang.System.exit(0)"); 59.113 - fail("SecurityException should have been thrown"); 59.114 - } catch (final Exception e) { 59.115 - //empty 59.116 - } 59.117 - 59.118 - } 59.119 - 59.120 - @Test 59.121 - public void persistentCacheTest() { 59.122 - final String oldCodeCache = System.getProperty(NASHORN_CODE_CACHE); 59.123 - System.setProperty(NASHORN_CODE_CACHE, CLASSFILTER_CODE_CACHE); 59.124 - try { 59.125 - persistentCacheTestImpl(); 59.126 - } finally { 59.127 - if (oldCodeCache != null) { 59.128 - System.setProperty(NASHORN_CODE_CACHE, oldCodeCache); 59.129 - } 59.130 - } 59.131 - } 59.132 - 59.133 - private void persistentCacheTestImpl() { 59.134 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 59.135 - final ScriptEngine engine = factory.getScriptEngine( 59.136 - TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}), 59.137 - getClass().getClassLoader(), 59.138 - getClassFilter() 59.139 - ); 59.140 - final String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore(); 59.141 - try { 59.142 - engine.eval(testScript); 59.143 - } catch (final ScriptException exc) { 59.144 - fail(exc.getMessage()); 59.145 - } 59.146 - final ScriptEngine engineSafe = factory.getScriptEngine( 59.147 - TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}), 59.148 - getClass().getClassLoader(), 59.149 - new ClassFilter() { 59.150 - @Override 59.151 - public boolean exposeToScripts(final String s) { 59.152 - return false; 59.153 - } 59.154 - } 59.155 - ); 59.156 - try { 59.157 - engineSafe.eval(testScript); 59.158 - fail("ClassNotFoundException should have been thrown"); 59.159 - } catch (final Exception exc) { 59.160 - if (!(exc.getCause() instanceof ClassNotFoundException)) { 59.161 - fail("ClassNotFoundException expected, got " + exc.getClass()); 59.162 - } 59.163 - } 59.164 - } 59.165 - 59.166 - private static String generateCodeForPersistentStore() { 59.167 - final StringBuilder stringBuilder = new StringBuilder(); 59.168 - for (int i=0; i < 100; i++) { 59.169 - stringBuilder.append("function i") 59.170 - .append(i) 59.171 - .append("(y, z) { var x") 59.172 - .append(i) 59.173 - .append(" = ") 59.174 - .append(i) 59.175 - .append(";}"); 59.176 - } 59.177 - return stringBuilder.toString(); 59.178 - } 59.179 - 59.180 - private static ClassFilter getClassFilter() { 59.181 - return new ClassFilter() { 59.182 - @Override 59.183 - public boolean exposeToScripts(final String s) { 59.184 - return true; 59.185 - } 59.186 - }; 59.187 - } 59.188 -}
60.1 --- a/test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java Wed Apr 01 13:22:52 2015 -0700 60.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 60.3 @@ -1,188 +0,0 @@ 60.4 -/* 60.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 60.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 60.7 - * 60.8 - * This code is free software; you can redistribute it and/or modify it 60.9 - * under the terms of the GNU General Public License version 2 only, as 60.10 - * published by the Free Software Foundation. Oracle designates this 60.11 - * particular file as subject to the "Classpath" exception as provided 60.12 - * by Oracle in the LICENSE file that accompanied this code. 60.13 - * 60.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 60.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 60.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 60.17 - * version 2 for more details (a copy is included in the LICENSE file that 60.18 - * accompanied this code). 60.19 - * 60.20 - * You should have received a copy of the GNU General Public License version 60.21 - * 2 along with this work; if not, write to the Free Software Foundation, 60.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 60.23 - * 60.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 60.25 - * or visit www.oracle.com if you need additional information or have any 60.26 - * questions. 60.27 - */ 60.28 -package jdk.nashorn.internal.runtime; 60.29 - 60.30 -import static org.testng.Assert.assertEquals; 60.31 -import static org.testng.Assert.assertFalse; 60.32 -import java.io.File; 60.33 -import java.io.IOException; 60.34 -import java.nio.file.DirectoryStream; 60.35 -import java.nio.file.FileSystems; 60.36 -import java.nio.file.Files; 60.37 -import java.nio.file.Path; 60.38 -import javax.script.ScriptEngine; 60.39 -import javax.script.ScriptException; 60.40 -import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 60.41 -import org.testng.annotations.Test; 60.42 - 60.43 -/** 60.44 - * @test 60.45 - * @bug 8039185 8039403 60.46 - * @summary Test for persistent code cache and path handling 60.47 - * @run testng jdk.nashorn.internal.runtime.CodeStoreAndPathTest 60.48 - */ 60.49 -@SuppressWarnings("javadoc") 60.50 -public class CodeStoreAndPathTest { 60.51 - 60.52 - final String code1 = "var code1; var x = 'Hello Script'; var x1 = 'Hello Script'; " 60.53 - + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; " 60.54 - + "var x4 = 'Hello Script'; var x5 = 'Hello Script';" 60.55 - + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; " 60.56 - + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; " 60.57 - + "var x10 = 'Hello Script';" 60.58 - + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60.59 - + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 60.60 - + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 60.61 - + "var x10 = 'Hello Script';}" 60.62 - + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60.63 - + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 60.64 - + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 60.65 - + "var x10 = 'Hello Script';}" 60.66 - + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60.67 - + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 60.68 - + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 60.69 - + "var x10 = 'Hello Script';}" 60.70 - + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60.71 - + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 60.72 - + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 60.73 - + "var x10 = 'Hello Script';}"; 60.74 - final String code2 = "var code2; var x = 'Hello Script'; var x1 = 'Hello Script'; " 60.75 - + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; " 60.76 - + "var x4 = 'Hello Script'; var x5 = 'Hello Script';" 60.77 - + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; " 60.78 - + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; " 60.79 - + "var x10 = 'Hello Script';" 60.80 - + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60.81 - + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 60.82 - + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 60.83 - + "var x10 = 'Hello Script';}" 60.84 - + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60.85 - + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 60.86 - + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 60.87 - + "var x10 = 'Hello Script';}" 60.88 - + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60.89 - + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 60.90 - + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 60.91 - + "var x10 = 'Hello Script';}" 60.92 - + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 60.93 - + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 60.94 - + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 60.95 - + "var x10 = 'Hello Script';}"; 60.96 - // Script size < Default minimum size for storing a compiled script class 60.97 - final String code3 = "var code3; var x = 'Hello Script'; var x1 = 'Hello Script'; "; 60.98 - final String codeCache = "build/nashorn_code_cache"; 60.99 - final String oldUserDir = System.getProperty("user.dir"); 60.100 - 60.101 - private static final String[] ENGINE_OPTIONS_OPT = new String[]{"--persistent-code-cache", "--optimistic-types=true"}; 60.102 - private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"}; 60.103 - 60.104 - @Test 60.105 - public void pathHandlingTest() { 60.106 - System.setProperty("nashorn.persistent.code.cache", codeCache); 60.107 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 60.108 - 60.109 - fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 60.110 - 60.111 - final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache); 60.112 - final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty( 60.113 - "nashorn.persistent.code.cache")).toAbsolutePath(); 60.114 - // Check that nashorn code cache is created in current working directory 60.115 - assertEquals(actualCodeCachePath, expectedCodeCachePath); 60.116 - // Check that code cache dir exists and it's not empty 60.117 - final File file = new File(actualCodeCachePath.toUri()); 60.118 - assertFalse(!file.isDirectory(), "No code cache directory was created!"); 60.119 - assertFalse(file.list().length == 0, "Code cache directory is empty!"); 60.120 - } 60.121 - 60.122 - @Test 60.123 - public void changeUserDirTest() throws ScriptException, IOException { 60.124 - System.setProperty("nashorn.persistent.code.cache", codeCache); 60.125 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 60.126 - final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 60.127 - final Path codeCachePath = getCodeCachePath(false); 60.128 - final String newUserDir = "build/newUserDir"; 60.129 - // Now changing current working directory 60.130 - System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir); 60.131 - try { 60.132 - // Check that a new compiled script is stored in existing code cache 60.133 - e.eval(code1); 60.134 - final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 60.135 - checkCompiledScripts(stream, 1); 60.136 - // Setting to default current working dir 60.137 - } finally { 60.138 - System.setProperty("user.dir", oldUserDir); 60.139 - } 60.140 - } 60.141 - 60.142 - @Test 60.143 - public void codeCacheTest() throws ScriptException, IOException { 60.144 - System.setProperty("nashorn.persistent.code.cache", codeCache); 60.145 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 60.146 - final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 60.147 - final Path codeCachePath = getCodeCachePath(false); 60.148 - e.eval(code1); 60.149 - e.eval(code2); 60.150 - e.eval(code3);// less than minimum size for storing 60.151 - // adding code1 and code2. 60.152 - final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 60.153 - checkCompiledScripts(stream, 2); 60.154 - } 60.155 - 60.156 - @Test 60.157 - public void codeCacheTestOpt() throws ScriptException, IOException { 60.158 - System.setProperty("nashorn.persistent.code.cache", codeCache); 60.159 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 60.160 - final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT); 60.161 - final Path codeCachePath = getCodeCachePath(true); 60.162 - e.eval(code1); 60.163 - e.eval(code2); 60.164 - e.eval(code3);// less than minimum size for storing 60.165 - // adding code1 and code2. 60.166 - final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 60.167 - checkCompiledScripts(stream, 2); 60.168 - } 60.169 - 60.170 - private static Path getCodeCachePath(final boolean optimistic) { 60.171 - final String codeCache = System.getProperty("nashorn.persistent.code.cache"); 60.172 - final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath(); 60.173 - final String[] files = codeCachePath.toFile().list(); 60.174 - for (final String file : files) { 60.175 - if (file.endsWith("_opt") == optimistic) { 60.176 - return codeCachePath.resolve(file); 60.177 - } 60.178 - } 60.179 - throw new AssertionError("Code cache path not found"); 60.180 - } 60.181 - 60.182 - private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException { 60.183 - int n = numberOfScripts; 60.184 - for (@SuppressWarnings("unused") final Path file : stream) { 60.185 - n--; 60.186 - } 60.187 - stream.close(); 60.188 - assertEquals(n, 0); 60.189 - } 60.190 - 60.191 -}
61.1 --- a/test/src/jdk/nashorn/internal/runtime/ConsStringTest.java Wed Apr 01 13:22:52 2015 -0700 61.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 61.3 @@ -1,131 +0,0 @@ 61.4 -/* 61.5 - * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 61.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 61.7 - * 61.8 - * This code is free software; you can redistribute it and/or modify it 61.9 - * under the terms of the GNU General Public License version 2 only, as 61.10 - * published by the Free Software Foundation. Oracle designates this 61.11 - * particular file as subject to the "Classpath" exception as provided 61.12 - * by Oracle in the LICENSE file that accompanied this code. 61.13 - * 61.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 61.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 61.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 61.17 - * version 2 for more details (a copy is included in the LICENSE file that 61.18 - * accompanied this code). 61.19 - * 61.20 - * You should have received a copy of the GNU General Public License version 61.21 - * 2 along with this work; if not, write to the Free Software Foundation, 61.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 61.23 - * 61.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 61.25 - * or visit www.oracle.com if you need additional information or have any 61.26 - * questions. 61.27 - */ 61.28 - 61.29 -package jdk.nashorn.internal.runtime; 61.30 - 61.31 -import static org.testng.Assert.assertEquals; 61.32 - 61.33 -import org.testng.annotations.Test; 61.34 - 61.35 -/** 61.36 - * Tests for JSType methods. 61.37 - * 61.38 - * @test 61.39 - * @run testng jdk.nashorn.internal.runtime.ConsStringTest 61.40 - */ 61.41 -public class ConsStringTest { 61.42 - 61.43 - /** 61.44 - * Test toString conversion 61.45 - */ 61.46 - @Test 61.47 - public void testConsStringToString() { 61.48 - final ConsString cs1 = new ConsString("b", "c"); 61.49 - final ConsString cs2 = new ConsString("d", "e"); 61.50 - final ConsString cs3 = new ConsString(cs1, cs2); 61.51 - final ConsString cs4 = new ConsString(cs3, "f"); 61.52 - final ConsString cs5 = new ConsString("a", cs4); 61.53 - assertEquals(cs5.toString(), "abcdef"); 61.54 - assertEquals(cs4.toString(), "bcdef"); 61.55 - assertEquals(cs3.toString(), "bcde"); 61.56 - assertEquals(cs2.toString(), "de"); 61.57 - assertEquals(cs1.toString(), "bc"); 61.58 - // ConsStrings should be flattened now 61.59 - assertEquals(cs1.getComponents()[0], "bc"); 61.60 - assertEquals(cs1.getComponents()[1], ""); 61.61 - assertEquals(cs2.getComponents()[0], "de"); 61.62 - assertEquals(cs2.getComponents()[1], ""); 61.63 - assertEquals(cs3.getComponents()[0], "bcde"); 61.64 - assertEquals(cs3.getComponents()[1], ""); 61.65 - assertEquals(cs4.getComponents()[0], "bcdef"); 61.66 - assertEquals(cs4.getComponents()[1], ""); 61.67 - assertEquals(cs5.getComponents()[0], "abcdef"); 61.68 - assertEquals(cs5.getComponents()[1], ""); 61.69 - } 61.70 - 61.71 - /** 61.72 - * Test charAt 61.73 - */ 61.74 - @Test 61.75 - public void testConsStringCharAt() { 61.76 - final ConsString cs1 = new ConsString("b", "c"); 61.77 - final ConsString cs2 = new ConsString("d", "e"); 61.78 - final ConsString cs3 = new ConsString(cs1, cs2); 61.79 - final ConsString cs4 = new ConsString(cs3, "f"); 61.80 - final ConsString cs5 = new ConsString("a", cs4); 61.81 - assertEquals(cs1.charAt(1), 'c'); 61.82 - assertEquals(cs2.charAt(0), 'd'); 61.83 - assertEquals(cs3.charAt(3), 'e'); 61.84 - assertEquals(cs4.charAt(1), 'c'); 61.85 - assertEquals(cs5.charAt(2), 'c'); 61.86 - // ConsStrings should be flattened now 61.87 - assertEquals(cs1.getComponents()[0], "bc"); 61.88 - assertEquals(cs1.getComponents()[1], ""); 61.89 - assertEquals(cs2.getComponents()[0], "de"); 61.90 - assertEquals(cs2.getComponents()[1], ""); 61.91 - assertEquals(cs3.getComponents()[0], "bcde"); 61.92 - assertEquals(cs3.getComponents()[1], ""); 61.93 - assertEquals(cs4.getComponents()[0], "bcdef"); 61.94 - assertEquals(cs4.getComponents()[1], ""); 61.95 - assertEquals(cs5.getComponents()[0], "abcdef"); 61.96 - assertEquals(cs5.getComponents()[1], ""); 61.97 - } 61.98 - 61.99 - 61.100 - /** 61.101 - * Test flattening of top-level and internal ConsStrings 61.102 - */ 61.103 - @Test 61.104 - public void testConsStringFlattening() { 61.105 - final ConsString cs1 = new ConsString("b", "c"); 61.106 - final ConsString cs2 = new ConsString("d", "e"); 61.107 - final ConsString cs3 = new ConsString(cs1, cs2); 61.108 - final ConsString cs4 = new ConsString(cs3, "f"); 61.109 - 61.110 - final ConsString cs5 = new ConsString("a", cs4); 61.111 - // top-level ConsString should not yet be flattened 61.112 - assert(cs5.getComponents()[0] == "a"); 61.113 - assert(cs5.getComponents()[1] == cs4); 61.114 - assertEquals(cs5.toString(), "abcdef"); 61.115 - // top-level ConsString should be flattened 61.116 - assertEquals(cs5.getComponents()[0], "abcdef"); 61.117 - assertEquals(cs5.getComponents()[1], ""); 61.118 - // internal ConsString should not yet be flattened after first traversal 61.119 - assertEquals(cs4.getComponents()[0], cs3); 61.120 - assertEquals(cs4.getComponents()[1], "f"); 61.121 - 61.122 - final ConsString cs6 = new ConsString("a", cs4); 61.123 - // top-level ConsString should not yet be flattened 61.124 - assertEquals(cs6.getComponents()[0], "a"); 61.125 - assertEquals(cs6.getComponents()[1], cs4); 61.126 - assertEquals(cs6.toString(), "abcdef"); 61.127 - // top-level ConsString should be flattened 61.128 - assertEquals(cs6.getComponents()[0], "abcdef"); 61.129 - assertEquals(cs6.getComponents()[1], ""); 61.130 - // internal ConsString should have been flattened after second traversal 61.131 - assertEquals(cs4.getComponents()[0], "bcdef"); 61.132 - assertEquals(cs4.getComponents()[1], ""); 61.133 - } 61.134 -}
62.1 --- a/test/src/jdk/nashorn/internal/runtime/ContextTest.java Wed Apr 01 13:22:52 2015 -0700 62.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 62.3 @@ -1,138 +0,0 @@ 62.4 -/* 62.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 62.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 62.7 - * 62.8 - * This code is free software; you can redistribute it and/or modify it 62.9 - * under the terms of the GNU General Public License version 2 only, as 62.10 - * published by the Free Software Foundation. Oracle designates this 62.11 - * particular file as subject to the "Classpath" exception as provided 62.12 - * by Oracle in the LICENSE file that accompanied this code. 62.13 - * 62.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 62.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 62.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 62.17 - * version 2 for more details (a copy is included in the LICENSE file that 62.18 - * accompanied this code). 62.19 - * 62.20 - * You should have received a copy of the GNU General Public License version 62.21 - * 2 along with this work; if not, write to the Free Software Foundation, 62.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 62.23 - * 62.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 62.25 - * or visit www.oracle.com if you need additional information or have any 62.26 - * questions. 62.27 - */ 62.28 - 62.29 -package jdk.nashorn.internal.runtime; 62.30 - 62.31 -import static jdk.nashorn.internal.runtime.Source.sourceFor; 62.32 -import static org.testng.Assert.assertEquals; 62.33 -import static org.testng.Assert.assertTrue; 62.34 -import static org.testng.Assert.fail; 62.35 -import java.util.Map; 62.36 -import jdk.nashorn.internal.objects.Global; 62.37 -import jdk.nashorn.internal.runtime.options.Options; 62.38 -import org.testng.annotations.Test; 62.39 - 62.40 -/** 62.41 - * Basic Context API tests. 62.42 - * 62.43 - * @test 62.44 - * @run testng jdk.nashorn.internal.runtime.ContextTest 62.45 - */ 62.46 -@SuppressWarnings("javadoc") 62.47 -public class ContextTest { 62.48 - // basic context eval test 62.49 - @Test 62.50 - public void evalTest() { 62.51 - final Options options = new Options(""); 62.52 - final ErrorManager errors = new ErrorManager(); 62.53 - final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 62.54 - final Global oldGlobal = Context.getGlobal(); 62.55 - Context.setGlobal(cx.createGlobal()); 62.56 - try { 62.57 - String code = "22 + 10"; 62.58 - assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue()); 62.59 - 62.60 - code = "obj = { js: 'nashorn' }; obj.js"; 62.61 - assertEquals(eval(cx, "<evalTest2>", code), "nashorn"); 62.62 - } finally { 62.63 - Context.setGlobal(oldGlobal); 62.64 - } 62.65 - } 62.66 - 62.67 - // Make sure trying to compile an invalid script returns null - see JDK-8046215. 62.68 - @Test 62.69 - public void compileErrorTest() { 62.70 - final Options options = new Options(""); 62.71 - final ErrorManager errors = new ErrorManager(); 62.72 - final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 62.73 - final Global oldGlobal = Context.getGlobal(); 62.74 - Context.setGlobal(cx.createGlobal()); 62.75 - try { 62.76 - final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal()); 62.77 - if (script != null) { 62.78 - fail("Invalid script compiled without errors"); 62.79 - } 62.80 - if (errors.getNumberOfErrors() != 1) { 62.81 - fail("Wrong number of errors: " + errors.getNumberOfErrors()); 62.82 - } 62.83 - } finally { 62.84 - Context.setGlobal(oldGlobal); 62.85 - } 62.86 - } 62.87 - 62.88 - // basic check for JS reflection access - java.util.Map-like access on ScriptObject 62.89 - @Test 62.90 - public void reflectionTest() { 62.91 - final Options options = new Options(""); 62.92 - final ErrorManager errors = new ErrorManager(); 62.93 - final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 62.94 - final boolean strict = cx.getEnv()._strict; 62.95 - final Global oldGlobal = Context.getGlobal(); 62.96 - Context.setGlobal(cx.createGlobal()); 62.97 - 62.98 - try { 62.99 - final String code = "var obj = { x: 344, y: 42 }"; 62.100 - eval(cx, "<reflectionTest>", code); 62.101 - 62.102 - final Object obj = Context.getGlobal().get("obj"); 62.103 - 62.104 - assertTrue(obj instanceof ScriptObject); 62.105 - 62.106 - final ScriptObject sobj = (ScriptObject)obj; 62.107 - int count = 0; 62.108 - for (final Map.Entry<?, ?> ex : sobj.entrySet()) { 62.109 - final Object key = ex.getKey(); 62.110 - if (key.equals("x")) { 62.111 - assertTrue(ex.getValue() instanceof Number); 62.112 - assertTrue(344.0 == ((Number)ex.getValue()).doubleValue()); 62.113 - 62.114 - count++; 62.115 - } else if (key.equals("y")) { 62.116 - assertTrue(ex.getValue() instanceof Number); 62.117 - assertTrue(42.0 == ((Number)ex.getValue()).doubleValue()); 62.118 - 62.119 - count++; 62.120 - } 62.121 - } 62.122 - assertEquals(count, 2); 62.123 - assertEquals(sobj.size(), 2); 62.124 - 62.125 - // add property 62.126 - sobj.put("zee", "hello", strict); 62.127 - assertEquals(sobj.get("zee"), "hello"); 62.128 - assertEquals(sobj.size(), 3); 62.129 - 62.130 - } finally { 62.131 - Context.setGlobal(oldGlobal); 62.132 - } 62.133 - } 62.134 - 62.135 - private static Object eval(final Context cx, final String name, final String code) { 62.136 - final Source source = sourceFor(name, code); 62.137 - final ScriptObject global = Context.getGlobal(); 62.138 - final ScriptFunction func = cx.compileScript(source, global); 62.139 - return func != null ? ScriptRuntime.apply(func, global) : null; 62.140 - } 62.141 -}
63.1 --- a/test/src/jdk/nashorn/internal/runtime/ExceptionsNotSerializable.java Wed Apr 01 13:22:52 2015 -0700 63.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 63.3 @@ -1,77 +0,0 @@ 63.4 -/* 63.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 63.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 63.7 - * 63.8 - * This code is free software; you can redistribute it and/or modify it 63.9 - * under the terms of the GNU General Public License version 2 only, as 63.10 - * published by the Free Software Foundation. Oracle designates this 63.11 - * particular file as subject to the "Classpath" exception as provided 63.12 - * by Oracle in the LICENSE file that accompanied this code. 63.13 - * 63.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 63.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 63.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 63.17 - * version 2 for more details (a copy is included in the LICENSE file that 63.18 - * accompanied this code). 63.19 - * 63.20 - * You should have received a copy of the GNU General Public License version 63.21 - * 2 along with this work; if not, write to the Free Software Foundation, 63.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 63.23 - * 63.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 63.25 - * or visit www.oracle.com if you need additional information or have any 63.26 - * questions. 63.27 - */ 63.28 - 63.29 -package jdk.nashorn.internal.runtime; 63.30 - 63.31 -import static org.testng.Assert.assertEquals; 63.32 -import static org.testng.Assert.fail; 63.33 -import java.io.ByteArrayOutputStream; 63.34 -import java.io.IOException; 63.35 -import java.io.NotSerializableException; 63.36 -import java.io.ObjectOutputStream; 63.37 -import javax.script.ScriptEngine; 63.38 -import javax.script.ScriptException; 63.39 -import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 63.40 -import org.testng.annotations.Test; 63.41 - 63.42 -/** 63.43 - * JDK-8044518: Ensure exceptions related to optimistic recompilation are not serializable 63.44 - * 63.45 - * @test 63.46 - * @run testng jdk.nashorn.internal.runtime.ExceptionsNotSerializable 63.47 - */ 63.48 -@SuppressWarnings("javadoc") 63.49 -public class ExceptionsNotSerializable { 63.50 - @Test 63.51 - public void rewriteExceptionNotSerializable() throws ScriptException { 63.52 - // NOTE: we must create a RewriteException in a context of a Nashorn engine, as it uses Global.newIntance() 63.53 - // internally. 63.54 - final ScriptEngine e = new NashornScriptEngineFactory().getScriptEngine(); 63.55 - e.put("f", new Runnable() { 63.56 - @Override 63.57 - public void run() { 63.58 - tryToSerialize(RewriteException.create(null, new Object[0], new String[0])); 63.59 - } 63.60 - }); 63.61 - e.eval("f()"); 63.62 - } 63.63 - 63.64 - @Test 63.65 - public void unwarrantedOptimismExceptionNotSerializable() { 63.66 - tryToSerialize(new UnwarrantedOptimismException(new Double(1.0), 128)); 63.67 - } 63.68 - 63.69 - private static void tryToSerialize(final Object obj) { 63.70 - try { 63.71 - new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(obj); 63.72 - fail(); 63.73 - } catch (final NotSerializableException e) { 63.74 - assertEquals(e.getMessage(), obj.getClass().getName()); 63.75 - } catch (final IOException e) { 63.76 - fail("", e); 63.77 - } 63.78 - 63.79 - } 63.80 -}
64.1 --- a/test/src/jdk/nashorn/internal/runtime/JSTypeTest.java Wed Apr 01 13:22:52 2015 -0700 64.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 64.3 @@ -1,193 +0,0 @@ 64.4 -/* 64.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 64.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 64.7 - * 64.8 - * This code is free software; you can redistribute it and/or modify it 64.9 - * under the terms of the GNU General Public License version 2 only, as 64.10 - * published by the Free Software Foundation. Oracle designates this 64.11 - * particular file as subject to the "Classpath" exception as provided 64.12 - * by Oracle in the LICENSE file that accompanied this code. 64.13 - * 64.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 64.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 64.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 64.17 - * version 2 for more details (a copy is included in the LICENSE file that 64.18 - * accompanied this code). 64.19 - * 64.20 - * You should have received a copy of the GNU General Public License version 64.21 - * 2 along with this work; if not, write to the Free Software Foundation, 64.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 64.23 - * 64.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 64.25 - * or visit www.oracle.com if you need additional information or have any 64.26 - * questions. 64.27 - */ 64.28 - 64.29 -package jdk.nashorn.internal.runtime; 64.30 - 64.31 -import static org.testng.Assert.assertEquals; 64.32 -import static org.testng.Assert.assertFalse; 64.33 -import static org.testng.Assert.assertTrue; 64.34 - 64.35 -import org.testng.annotations.Test; 64.36 - 64.37 -/** 64.38 - * Tests for JSType methods. 64.39 - * 64.40 - * @test 64.41 - * @run testng jdk.nashorn.internal.runtime.JSTypeTest 64.42 - */ 64.43 -public class JSTypeTest { 64.44 - /** 64.45 - * Test of isPrimitive method, of class Runtime. 64.46 - */ 64.47 - @Test 64.48 - public void testIsPrimitive() { 64.49 - assertTrue(JSType.isPrimitive(null)); 64.50 - assertTrue(JSType.isPrimitive(ScriptRuntime.UNDEFINED)); 64.51 - assertTrue(JSType.isPrimitive(Double.NaN)); 64.52 - assertTrue(JSType.isPrimitive(Double.NEGATIVE_INFINITY)); 64.53 - assertTrue(JSType.isPrimitive(Double.POSITIVE_INFINITY)); 64.54 - assertTrue(JSType.isPrimitive(0.0)); 64.55 - assertTrue(JSType.isPrimitive(3.14)); 64.56 - assertTrue(JSType.isPrimitive("hello")); 64.57 - assertTrue(JSType.isPrimitive("")); 64.58 - assertFalse(JSType.isPrimitive(new Object())); 64.59 - } 64.60 - 64.61 - /** 64.62 - * Test of toBoolean method, of class Runtime. 64.63 - */ 64.64 - @Test 64.65 - public void testToBoolean() { 64.66 - assertFalse(JSType.toBoolean(ScriptRuntime.UNDEFINED)); 64.67 - assertFalse(JSType.toBoolean(null)); 64.68 - assertFalse(JSType.toBoolean(Boolean.FALSE)); 64.69 - assertTrue(JSType.toBoolean(Boolean.TRUE)); 64.70 - assertFalse(JSType.toBoolean(-0.0)); 64.71 - assertFalse(JSType.toBoolean(0.0)); 64.72 - assertFalse(JSType.toBoolean(Double.NaN)); 64.73 - assertTrue(JSType.toBoolean(3.14)); 64.74 - assertFalse(JSType.toBoolean("")); 64.75 - assertTrue(JSType.toBoolean("javascript")); 64.76 - assertTrue(JSType.toBoolean(new Object())); 64.77 - } 64.78 - 64.79 - /** 64.80 - * Test of toNumber method, of class Runtime. 64.81 - */ 64.82 - @Test 64.83 - public void testToNumber_Object() { 64.84 - assertTrue(Double.isNaN(JSType.toNumber(ScriptRuntime.UNDEFINED))); 64.85 - assertEquals(JSType.toNumber((Object)null), 0.0, 0.0); 64.86 - assertEquals(JSType.toNumber(Boolean.TRUE), 1.0, 0.0); 64.87 - assertEquals(JSType.toNumber(Boolean.FALSE), 0.0, 0.0); 64.88 - assertEquals(JSType.toNumber(3.14), 3.14, 0.0); 64.89 - // FIXME: add more assertions for specific String to number cases 64.90 - // FIXME: add case for Object type (JSObject with getDefaultValue) 64.91 - } 64.92 - 64.93 - /** 64.94 - * Test of toString method, of class Runtime. 64.95 - */ 64.96 - @Test 64.97 - public void testToString_Object() { 64.98 - assertEquals(JSType.toString(ScriptRuntime.UNDEFINED), "undefined"); 64.99 - assertEquals(JSType.toString(null), "null"); 64.100 - assertEquals(JSType.toString(Boolean.TRUE), "true"); 64.101 - assertEquals(JSType.toString(Boolean.FALSE), "false"); 64.102 - assertEquals(JSType.toString(""), ""); 64.103 - assertEquals(JSType.toString("nashorn"), "nashorn"); 64.104 - assertEquals(JSType.toString(Double.NaN), "NaN"); 64.105 - assertEquals(JSType.toString(Double.POSITIVE_INFINITY), "Infinity"); 64.106 - assertEquals(JSType.toString(Double.NEGATIVE_INFINITY), "-Infinity"); 64.107 - assertEquals(JSType.toString(0.0), "0"); 64.108 - // FIXME: add more number-to-string test cases 64.109 - // FIXME: add case for Object type (JSObject with getDefaultValue) 64.110 - } 64.111 - 64.112 - /** 64.113 - * Test of JSType.toUint32(double) 64.114 - */ 64.115 - @Test 64.116 - public void testToUint32() { 64.117 - assertEquals(JSType.toUint32(+0.0), 0); 64.118 - assertEquals(JSType.toUint32(-0.0), 0); 64.119 - assertEquals(JSType.toUint32(Double.NaN), 0); 64.120 - assertEquals(JSType.toUint32(Double.POSITIVE_INFINITY), 0); 64.121 - assertEquals(JSType.toUint32(Double.NEGATIVE_INFINITY), 0); 64.122 - assertEquals(JSType.toUint32(9223372036854775807.0d), 0); 64.123 - assertEquals(JSType.toUint32(-9223372036854775807.0d), 0); 64.124 - assertEquals(JSType.toUint32(1099511627776.0d), 0); 64.125 - assertEquals(JSType.toUint32(-1099511627776.0d), 0); 64.126 - assertEquals(JSType.toUint32(4294967295.0d), 4294967295l); 64.127 - assertEquals(JSType.toUint32(4294967296.0d), 0); 64.128 - assertEquals(JSType.toUint32(4294967297.0d), 1); 64.129 - assertEquals(JSType.toUint32(-4294967295.0d), 1); 64.130 - assertEquals(JSType.toUint32(-4294967296.0d), 0); 64.131 - assertEquals(JSType.toUint32(-4294967297.0d), 4294967295l); 64.132 - assertEquals(JSType.toUint32(4294967295.6d), 4294967295l); 64.133 - assertEquals(JSType.toUint32(4294967296.6d), 0); 64.134 - assertEquals(JSType.toUint32(4294967297.6d), 1); 64.135 - assertEquals(JSType.toUint32(-4294967295.6d), 1); 64.136 - assertEquals(JSType.toUint32(-4294967296.6d), 0); 64.137 - assertEquals(JSType.toUint32(-4294967297.6d), 4294967295l); 64.138 - } 64.139 - 64.140 - /** 64.141 - * Test of JSType.toInt32(double) 64.142 - */ 64.143 - @Test 64.144 - public void testToInt32() { 64.145 - assertEquals(JSType.toInt32(+0.0), 0); 64.146 - assertEquals(JSType.toInt32(-0.0), 0); 64.147 - assertEquals(JSType.toInt32(Double.NaN), 0); 64.148 - assertEquals(JSType.toInt32(Double.POSITIVE_INFINITY), 0); 64.149 - assertEquals(JSType.toInt32(Double.NEGATIVE_INFINITY), 0); 64.150 - assertEquals(JSType.toInt32(9223372036854775807.0d), 0); 64.151 - assertEquals(JSType.toInt32(-9223372036854775807.0d), 0); 64.152 - assertEquals(JSType.toInt32(1099511627776.0d), 0); 64.153 - assertEquals(JSType.toInt32(-1099511627776.0d), 0); 64.154 - assertEquals(JSType.toInt32(4294967295.0d), -1); 64.155 - assertEquals(JSType.toInt32(4294967296.0d), 0); 64.156 - assertEquals(JSType.toInt32(4294967297.0d), 1); 64.157 - assertEquals(JSType.toInt32(-4294967295.0d), 1); 64.158 - assertEquals(JSType.toInt32(-4294967296.0d), 0); 64.159 - assertEquals(JSType.toInt32(-4294967297.d), -1); 64.160 - assertEquals(JSType.toInt32(4294967295.6d), -1); 64.161 - assertEquals(JSType.toInt32(4294967296.6d), 0); 64.162 - assertEquals(JSType.toInt32(4294967297.6d), 1); 64.163 - assertEquals(JSType.toInt32(-4294967295.6d), 1); 64.164 - assertEquals(JSType.toInt32(-4294967296.6d), 0); 64.165 - assertEquals(JSType.toInt32(-4294967297.6d), -1); 64.166 - } 64.167 - 64.168 - /** 64.169 - * Test of JSType.toUint16(double) 64.170 - */ 64.171 - @Test 64.172 - public void testToUint16() { 64.173 - assertEquals(JSType.toUint16(+0.0), 0); 64.174 - assertEquals(JSType.toUint16(-0.0), 0); 64.175 - assertEquals(JSType.toUint16(Double.NaN), 0); 64.176 - assertEquals(JSType.toUint16(Double.POSITIVE_INFINITY), 0); 64.177 - assertEquals(JSType.toUint16(Double.NEGATIVE_INFINITY), 0); 64.178 - assertEquals(JSType.toUint16(9223372036854775807.0d), 0); 64.179 - assertEquals(JSType.toUint16(-9223372036854775807.0d), 0); 64.180 - assertEquals(JSType.toUint16(1099511627776.0d), 0); 64.181 - assertEquals(JSType.toUint16(-1099511627776.0d), 0); 64.182 - assertEquals(JSType.toUint16(4294967295.0d), 65535); 64.183 - assertEquals(JSType.toUint16(4294967296.0d), 0); 64.184 - assertEquals(JSType.toUint16(4294967297.0d), 1); 64.185 - assertEquals(JSType.toUint16(-4294967295.0d), 1); 64.186 - assertEquals(JSType.toUint16(-4294967296.0d), 0); 64.187 - assertEquals(JSType.toUint16(-4294967297.0d), 65535); 64.188 - assertEquals(JSType.toUint16(4294967295.6d), 65535); 64.189 - assertEquals(JSType.toUint16(4294967296.6d), 0); 64.190 - assertEquals(JSType.toUint16(4294967297.6d), 1); 64.191 - assertEquals(JSType.toUint16(-4294967295.6d), 1); 64.192 - assertEquals(JSType.toUint16(-4294967296.6d), 0); 64.193 - assertEquals(JSType.toUint16(-4294967297.6d), 65535); 64.194 - } 64.195 - 64.196 -}
65.1 --- a/test/src/jdk/nashorn/internal/runtime/LexicalBindingTest.java Wed Apr 01 13:22:52 2015 -0700 65.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 65.3 @@ -1,212 +0,0 @@ 65.4 -/* 65.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 65.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 65.7 - * 65.8 - * This code is free software; you can redistribute it and/or modify it 65.9 - * under the terms of the GNU General Public License version 2 only, as 65.10 - * published by the Free Software Foundation. Oracle designates this 65.11 - * particular file as subject to the "Classpath" exception as provided 65.12 - * by Oracle in the LICENSE file that accompanied this code. 65.13 - * 65.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 65.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 65.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 65.17 - * version 2 for more details (a copy is included in the LICENSE file that 65.18 - * accompanied this code). 65.19 - * 65.20 - * You should have received a copy of the GNU General Public License version 65.21 - * 2 along with this work; if not, write to the Free Software Foundation, 65.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 65.23 - * 65.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 65.25 - * or visit www.oracle.com if you need additional information or have any 65.26 - * questions. 65.27 - */ 65.28 - 65.29 -package jdk.nashorn.internal.runtime; 65.30 - 65.31 -import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 65.32 -import org.testng.annotations.Test; 65.33 - 65.34 -import javax.script.Bindings; 65.35 -import javax.script.ScriptContext; 65.36 -import javax.script.ScriptEngine; 65.37 -import javax.script.ScriptException; 65.38 -import javax.script.SimpleScriptContext; 65.39 - 65.40 -import static org.testng.Assert.assertEquals; 65.41 - 65.42 -/** 65.43 - * Top-level lexical binding tests. 65.44 - * 65.45 - * @test 65.46 - * @run testng jdk.nashorn.internal.runtime.LexicalBindingTest 65.47 - */ 65.48 -@SuppressWarnings("javadoc") 65.49 -public class LexicalBindingTest { 65.50 - 65.51 - final static String LANGUAGE_ES6 = "--language=es6"; 65.52 - final static int NUMBER_OF_CONTEXTS = 20; 65.53 - final static int MEGAMORPHIC_LOOP_COUNT = 20; 65.54 - 65.55 - /** 65.56 - * Test access to global var-declared variables for shared script classes with multiple globals. 65.57 - */ 65.58 - @Test 65.59 - public static void megamorphicVarTest() throws ScriptException, InterruptedException { 65.60 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 65.61 - final ScriptEngine e = factory.getScriptEngine(); 65.62 - final ScriptContext[] contexts = new ScriptContext[NUMBER_OF_CONTEXTS]; 65.63 - final String sharedScript = "foo"; 65.64 - 65.65 - 65.66 - for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) { 65.67 - final ScriptContext context = contexts[i] = new SimpleScriptContext(); 65.68 - final Bindings b = e.createBindings(); 65.69 - context.setBindings(b, ScriptContext.ENGINE_SCOPE); 65.70 - assertEquals(e.eval("var foo = '" + i + "';", context), null); 65.71 - } 65.72 - 65.73 - for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) { 65.74 - final ScriptContext context = contexts[i]; 65.75 - assertEquals(e.eval(sharedScript, context), String.valueOf(i)); 65.76 - } 65.77 - } 65.78 - 65.79 - /** 65.80 - * Test access to global lexically declared variables for shared script classes with multiple globals. 65.81 - */ 65.82 - @Test 65.83 - public static void megamorphicMultiGlobalLetTest() throws ScriptException, InterruptedException { 65.84 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 65.85 - final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6); 65.86 - final ScriptContext[] contexts = new ScriptContext[NUMBER_OF_CONTEXTS]; 65.87 - final String sharedScript = "foo"; 65.88 - 65.89 - 65.90 - for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) { 65.91 - final ScriptContext context = contexts[i] = new SimpleScriptContext(); 65.92 - final Bindings b = e.createBindings(); 65.93 - context.setBindings(b, ScriptContext.ENGINE_SCOPE); 65.94 - assertEquals(e.eval("let foo = '" + i + "';", context), null); 65.95 - } 65.96 - 65.97 - for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) { 65.98 - final ScriptContext context = contexts[i]; 65.99 - assertEquals(e.eval(sharedScript, context), String.valueOf(i)); 65.100 - } 65.101 - } 65.102 - 65.103 - 65.104 - /** 65.105 - * Test access to global lexically declared variables for shared script classes with single global. 65.106 - */ 65.107 - @Test 65.108 - public static void megamorphicSingleGlobalLetTest() throws ScriptException, InterruptedException { 65.109 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 65.110 - final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6); 65.111 - final String sharedGetterScript = "foo"; 65.112 - final String sharedSetterScript = "foo = 1"; 65.113 - 65.114 - for (int i = 0; i < MEGAMORPHIC_LOOP_COUNT; i++) { 65.115 - assertEquals(e.eval(sharedSetterScript), 1); 65.116 - assertEquals(e.eval(sharedGetterScript), 1); 65.117 - assertEquals(e.eval("delete foo; a" + i + " = 1; foo = " + i + ";"), i); 65.118 - assertEquals(e.eval(sharedGetterScript), i); 65.119 - } 65.120 - 65.121 - assertEquals(e.eval("let foo = 'foo';"), null); 65.122 - assertEquals(e.eval(sharedGetterScript), "foo"); 65.123 - assertEquals(e.eval(sharedSetterScript), 1); 65.124 - assertEquals(e.eval(sharedGetterScript), 1); 65.125 - assertEquals(e.eval("this.foo"), MEGAMORPHIC_LOOP_COUNT - 1); 65.126 - } 65.127 - 65.128 - /** 65.129 - * Test access to global lexically declared variables for shared script classes with single global. 65.130 - */ 65.131 - @Test 65.132 - public static void megamorphicInheritedGlobalLetTest() throws ScriptException, InterruptedException { 65.133 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 65.134 - final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6); 65.135 - final String sharedGetterScript = "foo"; 65.136 - final String sharedSetterScript = "foo = 1"; 65.137 - 65.138 - for (int i = 0; i < MEGAMORPHIC_LOOP_COUNT; i++) { 65.139 - assertEquals(e.eval(sharedSetterScript), 1); 65.140 - assertEquals(e.eval(sharedGetterScript), 1); 65.141 - assertEquals(e.eval("delete foo; a" + i + " = 1; Object.prototype.foo = " + i + ";"), i); 65.142 - assertEquals(e.eval(sharedGetterScript), i); 65.143 - } 65.144 - 65.145 - assertEquals(e.eval("let foo = 'foo';"), null); 65.146 - assertEquals(e.eval(sharedGetterScript), "foo"); 65.147 - assertEquals(e.eval(sharedSetterScript), 1); 65.148 - assertEquals(e.eval(sharedGetterScript), 1); 65.149 - assertEquals(e.eval("this.foo"), MEGAMORPHIC_LOOP_COUNT - 1); 65.150 - } 65.151 - 65.152 - /** 65.153 - * Test multi-threaded access to global lexically declared variables for shared script classes with multiple globals. 65.154 - */ 65.155 - @Test 65.156 - public static void multiThreadedLetTest() throws ScriptException, InterruptedException { 65.157 - final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 65.158 - final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6); 65.159 - final Bindings b = e.createBindings(); 65.160 - final ScriptContext origContext = e.getContext(); 65.161 - final ScriptContext newCtxt = new SimpleScriptContext(); 65.162 - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 65.163 - final String sharedScript = "foo"; 65.164 - 65.165 - assertEquals(e.eval("let foo = 'original context';", origContext), null); 65.166 - assertEquals(e.eval("let foo = 'new context';", newCtxt), null); 65.167 - 65.168 - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 65.169 - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 65.170 - t1.start(); 65.171 - t2.start(); 65.172 - t1.join(); 65.173 - t2.join(); 65.174 - 65.175 - assertEquals(e.eval("foo = 'newer context';", newCtxt), "newer context"); 65.176 - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 65.177 - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 65.178 - 65.179 - t3.start(); 65.180 - t4.start(); 65.181 - t3.join(); 65.182 - t4.join(); 65.183 - 65.184 - assertEquals(e.eval(sharedScript), "original context"); 65.185 - assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 65.186 - } 65.187 - 65.188 - private static class ScriptRunner implements Runnable { 65.189 - 65.190 - final ScriptEngine engine; 65.191 - final ScriptContext context; 65.192 - final String source; 65.193 - final Object expected; 65.194 - final int iterations; 65.195 - 65.196 - ScriptRunner(final ScriptEngine engine, final ScriptContext context, final String source, final Object expected, final int iterations) { 65.197 - this.engine = engine; 65.198 - this.context = context; 65.199 - this.source = source; 65.200 - this.expected = expected; 65.201 - this.iterations = iterations; 65.202 - } 65.203 - 65.204 - @Override 65.205 - public void run() { 65.206 - try { 65.207 - for (int i = 0; i < iterations; i++) { 65.208 - assertEquals(engine.eval(source, context), expected); 65.209 - } 65.210 - } catch (final ScriptException se) { 65.211 - throw new RuntimeException(se); 65.212 - } 65.213 - } 65.214 - } 65.215 -}
66.1 --- a/test/src/jdk/nashorn/internal/runtime/NoPersistenceCachingTest.java Wed Apr 01 13:22:52 2015 -0700 66.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 66.3 @@ -1,140 +0,0 @@ 66.4 -/* 66.5 - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 66.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 66.7 - * 66.8 - * This code is free software; you can redistribute it and/or modify it 66.9 - * under the terms of the GNU General Public License version 2 only, as 66.10 - * published by the Free Software Foundation. Oracle designates this 66.11 - * particular file as subject to the "Classpath" exception as provided 66.12 - * by Oracle in the LICENSE file that accompanied this code. 66.13 - * 66.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 66.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 66.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 66.17 - * version 2 for more details (a copy is included in the LICENSE file that 66.18 - * accompanied this code). 66.19 - * 66.20 - * You should have received a copy of the GNU General Public License version 66.21 - * 2 along with this work; if not, write to the Free Software Foundation, 66.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 66.23 - * 66.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 66.25 - * or visit www.oracle.com if you need additional information or have any 66.26 - * questions. 66.27 - */ 66.28 -package jdk.nashorn.internal.runtime; 66.29 - 66.30 -import static org.testng.Assert.fail; 66.31 -import java.io.ByteArrayOutputStream; 66.32 -import java.io.PrintStream; 66.33 -import java.util.regex.Matcher; 66.34 -import java.util.regex.Pattern; 66.35 -import javax.script.ScriptContext; 66.36 -import javax.script.ScriptEngine; 66.37 -import javax.script.ScriptEngineFactory; 66.38 -import javax.script.ScriptEngineManager; 66.39 -import javax.script.SimpleScriptContext; 66.40 -import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 66.41 -import org.testng.annotations.AfterTest; 66.42 -import org.testng.annotations.BeforeTest; 66.43 -import org.testng.annotations.Test; 66.44 - 66.45 -/** 66.46 - * @test 66.47 - * @bug 8037378 66.48 - * @summary Sanity tests for no persistence caching 66.49 - * @run testng/othervm jdk.nashorn.internal.runtime.NoPersistenceCachingTest 66.50 - */ 66.51 -@SuppressWarnings("javadoc") 66.52 -public class NoPersistenceCachingTest { 66.53 - 66.54 - private ScriptEngine engine; 66.55 - private ScriptContext context1, context2, context3; 66.56 - private ByteArrayOutputStream stderr; 66.57 - private PrintStream prevStderr; 66.58 - 66.59 - @BeforeTest 66.60 - public void setupTest() { 66.61 - stderr = new ByteArrayOutputStream(); 66.62 - prevStderr = System.err; 66.63 - System.setErr(new PrintStream(stderr)); 66.64 - NashornScriptEngineFactory nashornFactory = null; 66.65 - final ScriptEngineManager sm = new ScriptEngineManager(); 66.66 - for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 66.67 - if (fac instanceof NashornScriptEngineFactory) { 66.68 - nashornFactory = (NashornScriptEngineFactory) fac; 66.69 - break; 66.70 - } 66.71 - } 66.72 - if (nashornFactory == null) { 66.73 - fail("Cannot find nashorn factory!"); 66.74 - } 66.75 - // fine is enough for cache hits, finest produces way too much information 66.76 - // TODO this should be ported to use the RuntimeEvents instead of screen scraping 66.77 - // logs, as obviously this is very brittle 66.78 - final String[] options = new String[]{"--log=compiler:fine"}; 66.79 - engine = nashornFactory.getScriptEngine(options); 66.80 - context1 = engine.getContext(); 66.81 - context2 = new SimpleScriptContext(); 66.82 - context2.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); 66.83 - context3 = new SimpleScriptContext(); 66.84 - context3.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); 66.85 - } 66.86 - 66.87 - @AfterTest 66.88 - public void setErrTest() { 66.89 - System.setErr(prevStderr); 66.90 - } 66.91 - 66.92 - public void runTest(final int numberOfContext, final String expectedOutputPattern, 66.93 - final int expectedPatternOccurrence) { 66.94 - 66.95 - try { 66.96 - switch (numberOfContext) { 66.97 - case 2: 66.98 - final String scriptTwoContexts = "print('HelloTwoContexts')"; 66.99 - engine.eval(scriptTwoContexts, context1); 66.100 - engine.eval(scriptTwoContexts, context2); 66.101 - break; 66.102 - case 3: 66.103 - final String scriptThreeContexts = "print('HelloThreeContexts')"; 66.104 - engine.eval(scriptThreeContexts, context1); 66.105 - engine.eval(scriptThreeContexts, context2); 66.106 - engine.eval(scriptThreeContexts, context3); 66.107 - break; 66.108 - default: 66.109 - break; 66.110 - } 66.111 - } catch (final Exception se) { 66.112 - se.printStackTrace(); 66.113 - fail(se.getMessage()); 66.114 - } 66.115 - final Pattern deoptimizing = Pattern.compile(expectedOutputPattern); 66.116 - final Matcher matcher = deoptimizing.matcher(stderr.toString()); 66.117 - int matches = 0; 66.118 - while (matcher.find()) { 66.119 - matches++; 66.120 - } 66.121 - if (matches != expectedPatternOccurrence) { 66.122 - fail("Number of cache hit is not correct, expected: " 66.123 - + expectedPatternOccurrence + " and found: " + matches + "\n" 66.124 - + stderr); 66.125 - } 66.126 - stderr.reset(); 66.127 - } 66.128 - 66.129 - private static String getCodeCachePattern() { 66.130 - return ("\\[compiler\\]\\sCode\\scache\\shit\\sfor\\s<eval>\\savoiding\\srecompile."); 66.131 - } 66.132 - 66.133 - @Test 66.134 - public void twoContextTest() { 66.135 - runTest(2, getCodeCachePattern(), 1); 66.136 - 66.137 - } 66.138 - 66.139 - @Test 66.140 - public void threeContextTest() { 66.141 - runTest(3, getCodeCachePattern(), 2); 66.142 - } 66.143 -}
67.1 --- a/test/src/jdk/nashorn/internal/runtime/SourceTest.java Wed Apr 01 13:22:52 2015 -0700 67.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 67.3 @@ -1,124 +0,0 @@ 67.4 -/* 67.5 - * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 67.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 67.7 - * 67.8 - * This code is free software; you can redistribute it and/or modify it 67.9 - * under the terms of the GNU General Public License version 2 only, as 67.10 - * published by the Free Software Foundation. Oracle designates this 67.11 - * particular file as subject to the "Classpath" exception as provided 67.12 - * by Oracle in the LICENSE file that accompanied this code. 67.13 - * 67.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 67.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 67.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 67.17 - * version 2 for more details (a copy is included in the LICENSE file that 67.18 - * accompanied this code). 67.19 - * 67.20 - * You should have received a copy of the GNU General Public License version 67.21 - * 2 along with this work; if not, write to the Free Software Foundation, 67.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 67.23 - * 67.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 67.25 - * or visit www.oracle.com if you need additional information or have any 67.26 - * questions. 67.27 - */ 67.28 - 67.29 -package jdk.nashorn.internal.runtime; 67.30 - 67.31 -import static jdk.nashorn.internal.runtime.Source.sourceFor; 67.32 -import static org.testng.Assert.assertEquals; 67.33 -import static org.testng.Assert.assertTrue; 67.34 -import static org.testng.Assert.fail; 67.35 -import java.io.File; 67.36 -import java.io.IOException; 67.37 -import java.io.InputStreamReader; 67.38 -import java.io.Reader; 67.39 -import java.net.URL; 67.40 -import java.util.Arrays; 67.41 -import jdk.nashorn.api.scripting.URLReader; 67.42 -import org.testng.annotations.Test; 67.43 - 67.44 -/** 67.45 - * Tests different Source representations. 67.46 - */ 67.47 -@SuppressWarnings("javadoc") 67.48 -public class SourceTest { 67.49 - 67.50 - final private static String SOURCE_NAME = "source.js"; 67.51 - final private static String SOURCE_STRING = "var x = 1;"; 67.52 - final private static char[] SOURCE_CHARS = SOURCE_STRING.toCharArray(); 67.53 - final private static String RESOURCE_PATH = "resources/load_test.js"; 67.54 - final private static File SOURCE_FILE = new File("build/test/classes/jdk/nashorn/internal/runtime/" + RESOURCE_PATH); 67.55 - final private static URL SOURCE_URL = SourceTest.class.getResource(RESOURCE_PATH); 67.56 - 67.57 - 67.58 - @Test 67.59 - public void testStringSource() { 67.60 - testSources(sourceFor(SOURCE_NAME, SOURCE_STRING), sourceFor(SOURCE_NAME, SOURCE_STRING)); 67.61 - testSources(sourceFor(SOURCE_NAME, SOURCE_STRING), sourceFor(SOURCE_NAME, SOURCE_CHARS)); 67.62 - } 67.63 - 67.64 - @Test 67.65 - public void testCharArraySource() { 67.66 - testSources(sourceFor(SOURCE_NAME, SOURCE_CHARS), sourceFor(SOURCE_NAME, SOURCE_CHARS)); 67.67 - testSources(sourceFor(SOURCE_NAME, SOURCE_CHARS), sourceFor(SOURCE_NAME, SOURCE_STRING)); 67.68 - } 67.69 - 67.70 - @Test 67.71 - public void testURLSource() { 67.72 - try { 67.73 - testSources(sourceFor(SOURCE_NAME, SOURCE_URL), sourceFor(SOURCE_NAME, SOURCE_URL)); 67.74 - testSources(sourceFor(SOURCE_NAME, SOURCE_URL), sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL))); 67.75 - 67.76 - } catch (final IOException e) { 67.77 - fail(e.toString()); 67.78 - } 67.79 - } 67.80 - 67.81 - @Test 67.82 - public void testURLReaderSource() { 67.83 - try { 67.84 - System.err.println(SourceTest.class.getResource("")); 67.85 - testSources(sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL)), sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL))); 67.86 - testSources(sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL)), sourceFor(SOURCE_NAME, SOURCE_URL)); 67.87 - } catch (final IOException e) { 67.88 - fail(e.toString()); 67.89 - } 67.90 - } 67.91 - 67.92 - @Test 67.93 - public void testReaderSource() { 67.94 - try { 67.95 - testSources(sourceFor(SOURCE_NAME, getReader(RESOURCE_PATH)), sourceFor(SOURCE_NAME, getReader(RESOURCE_PATH))); 67.96 - } catch (final IOException e) { 67.97 - fail(e.toString()); 67.98 - } 67.99 - } 67.100 - 67.101 - @Test 67.102 - public void testFileSource() { 67.103 - try { 67.104 - testSources(sourceFor(SOURCE_NAME, SOURCE_FILE), sourceFor(SOURCE_NAME, SOURCE_FILE)); 67.105 - } catch (final IOException e) { 67.106 - fail(e.toString()); 67.107 - } 67.108 - } 67.109 - 67.110 - private static Reader getReader(final String path) { 67.111 - return new InputStreamReader(SourceTest.class.getResourceAsStream(path)); 67.112 - } 67.113 - 67.114 - private static void testSources(final Source source1, final Source source2) { 67.115 - final char[] chars1 = source1.getContent(); 67.116 - final char[] chars2 = source2.getContent(); 67.117 - final String str1 = source1.getString(); 67.118 - final String str2 = source2.getString(); 67.119 - assertTrue(Arrays.equals(chars1, chars2)); 67.120 - assertEquals(str1, str2); 67.121 - assertEquals(source1.hashCode(), source2.hashCode()); 67.122 - assertTrue(source1.equals(source2)); 67.123 - assertTrue(Arrays.equals(source1.getContent(), str1.toCharArray())); 67.124 - assertTrue(Arrays.equals(source1.getContent(), chars1)); 67.125 - assertTrue(Arrays.equals(source1.getContent(), source2.getContent())); 67.126 - } 67.127 -}
68.1 --- a/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java Wed Apr 01 13:22:52 2015 -0700 68.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 68.3 @@ -1,362 +0,0 @@ 68.4 -/* 68.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 68.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 68.7 - * 68.8 - * This code is free software; you can redistribute it and/or modify it 68.9 - * under the terms of the GNU General Public License version 2 only, as 68.10 - * published by the Free Software Foundation. Oracle designates this 68.11 - * particular file as subject to the "Classpath" exception as provided 68.12 - * by Oracle in the LICENSE file that accompanied this code. 68.13 - * 68.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 68.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 68.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 68.17 - * version 2 for more details (a copy is included in the LICENSE file that 68.18 - * accompanied this code). 68.19 - * 68.20 - * You should have received a copy of the GNU General Public License version 68.21 - * 2 along with this work; if not, write to the Free Software Foundation, 68.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 68.23 - * 68.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 68.25 - * or visit www.oracle.com if you need additional information or have any 68.26 - * questions. 68.27 - */ 68.28 - 68.29 -package jdk.nashorn.internal.runtime; 68.30 - 68.31 -import static org.testng.Assert.assertEquals; 68.32 -import static org.testng.Assert.assertTrue; 68.33 -import static org.testng.Assert.fail; 68.34 -import javax.script.ScriptContext; 68.35 -import javax.script.ScriptEngine; 68.36 -import javax.script.ScriptEngineFactory; 68.37 -import javax.script.ScriptEngineManager; 68.38 -import javax.script.ScriptException; 68.39 -import javax.script.SimpleScriptContext; 68.40 -import jdk.nashorn.api.scripting.ClassFilter; 68.41 -import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 68.42 -import org.testng.annotations.Test; 68.43 - 68.44 -/** 68.45 - * Tests for trusted client usage of nashorn script engine factory extension API 68.46 - */ 68.47 -@SuppressWarnings("javadoc") 68.48 -public class TrustedScriptEngineTest { 68.49 - @Test 68.50 - public void versionTest() { 68.51 - final ScriptEngineManager m = new ScriptEngineManager(); 68.52 - final ScriptEngine e = m.getEngineByName("nashorn"); 68.53 - assertEquals(e.getFactory().getEngineVersion(), Version.version()); 68.54 - } 68.55 - 68.56 - private static class MyClassLoader extends ClassLoader { 68.57 - // to check if script engine uses the specified class loader 68.58 - private final boolean[] reached = new boolean[1]; 68.59 - 68.60 - @Override 68.61 - protected Class<?> findClass(final String name) throws ClassNotFoundException { 68.62 - // flag that it reached here 68.63 - reached[0] = true; 68.64 - return super.findClass(name); 68.65 - } 68.66 - 68.67 - public boolean reached() { 68.68 - return reached[0]; 68.69 - } 68.70 - } 68.71 - 68.72 - // These are for "private" extension API of NashornScriptEngineFactory that 68.73 - // accepts a ClassLoader and/or command line options. 68.74 - 68.75 - @Test 68.76 - public void factoryClassLoaderTest() { 68.77 - final ScriptEngineManager sm = new ScriptEngineManager(); 68.78 - for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 68.79 - if (fac instanceof NashornScriptEngineFactory) { 68.80 - final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 68.81 - final MyClassLoader loader = new MyClassLoader(); 68.82 - // set the classloader as app class loader 68.83 - final ScriptEngine e = nfac.getScriptEngine(loader); 68.84 - try { 68.85 - e.eval("Packages.foo"); 68.86 - // check that the class loader was attempted 68.87 - assertTrue(loader.reached(), "did not reach class loader!"); 68.88 - } catch (final ScriptException se) { 68.89 - se.printStackTrace(); 68.90 - fail(se.getMessage()); 68.91 - } 68.92 - return; 68.93 - } 68.94 - } 68.95 - 68.96 - fail("Cannot find nashorn factory!"); 68.97 - } 68.98 - 68.99 - @Test 68.100 - public void factoryClassLoaderAndOptionsTest() { 68.101 - final ScriptEngineManager sm = new ScriptEngineManager(); 68.102 - for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 68.103 - if (fac instanceof NashornScriptEngineFactory) { 68.104 - final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 68.105 - final String[] options = new String[] { "-strict" }; 68.106 - final MyClassLoader loader = new MyClassLoader(); 68.107 - // set the classloader as app class loader 68.108 - final ScriptEngine e = nfac.getScriptEngine(options, loader); 68.109 - try { 68.110 - e.eval("Packages.foo"); 68.111 - // check that the class loader was attempted 68.112 - assertTrue(loader.reached(), "did not reach class loader!"); 68.113 - } catch (final ScriptException se) { 68.114 - se.printStackTrace(); 68.115 - fail(se.getMessage()); 68.116 - } 68.117 - 68.118 - try { 68.119 - // strict mode - delete of a var should throw SyntaxError 68.120 - e.eval("var d = 2; delete d;"); 68.121 - } catch (final ScriptException se) { 68.122 - // check that the error message contains "SyntaxError" 68.123 - assertTrue(se.getMessage().contains("SyntaxError")); 68.124 - } 68.125 - 68.126 - return; 68.127 - } 68.128 - } 68.129 - 68.130 - fail("Cannot find nashorn factory!"); 68.131 - } 68.132 - 68.133 - @Test 68.134 - public void factoryOptionsTest() { 68.135 - final ScriptEngineManager sm = new ScriptEngineManager(); 68.136 - for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 68.137 - if (fac instanceof NashornScriptEngineFactory) { 68.138 - final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 68.139 - // specify --no-syntax-extensions flag 68.140 - final String[] options = new String[] { "--no-syntax-extensions" }; 68.141 - final ScriptEngine e = nfac.getScriptEngine(options); 68.142 - try { 68.143 - // try nashorn specific extension 68.144 - e.eval("var f = funtion(x) 2*x;"); 68.145 - fail("should have thrown exception!"); 68.146 - } catch (final Exception ex) { 68.147 - //empty 68.148 - } 68.149 - return; 68.150 - } 68.151 - } 68.152 - 68.153 - fail("Cannot find nashorn factory!"); 68.154 - } 68.155 - 68.156 - @Test 68.157 - /** 68.158 - * Test repeated evals with --loader-per-compile=false 68.159 - * We used to get "class redefinition error". 68.160 - */ 68.161 - public void noLoaderPerCompilerTest() { 68.162 - final ScriptEngineManager sm = new ScriptEngineManager(); 68.163 - for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 68.164 - if (fac instanceof NashornScriptEngineFactory) { 68.165 - final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 68.166 - final String[] options = new String[] { "--loader-per-compile=false" }; 68.167 - final ScriptEngine e = nfac.getScriptEngine(options); 68.168 - try { 68.169 - e.eval("2 + 3"); 68.170 - e.eval("4 + 4"); 68.171 - } catch (final ScriptException se) { 68.172 - se.printStackTrace(); 68.173 - fail(se.getMessage()); 68.174 - } 68.175 - return; 68.176 - } 68.177 - } 68.178 - fail("Cannot find nashorn factory!"); 68.179 - } 68.180 - 68.181 - @Test 68.182 - /** 68.183 - * Test that we can use same script name in repeated evals with --loader-per-compile=false 68.184 - * We used to get "class redefinition error" as name was derived from script name. 68.185 - */ 68.186 - public void noLoaderPerCompilerWithSameNameTest() { 68.187 - final ScriptEngineManager sm = new ScriptEngineManager(); 68.188 - for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 68.189 - if (fac instanceof NashornScriptEngineFactory) { 68.190 - final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 68.191 - final String[] options = new String[] { "--loader-per-compile=false" }; 68.192 - final ScriptEngine e = nfac.getScriptEngine(options); 68.193 - e.put(ScriptEngine.FILENAME, "test.js"); 68.194 - try { 68.195 - e.eval("2 + 3"); 68.196 - e.eval("4 + 4"); 68.197 - } catch (final ScriptException se) { 68.198 - se.printStackTrace(); 68.199 - fail(se.getMessage()); 68.200 - } 68.201 - return; 68.202 - } 68.203 - } 68.204 - fail("Cannot find nashorn factory!"); 68.205 - } 68.206 - 68.207 - @Test 68.208 - public void globalPerEngineTest() throws ScriptException { 68.209 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.210 - final String[] options = new String[] { "--global-per-engine" }; 68.211 - final ScriptEngine e = fac.getScriptEngine(options); 68.212 - 68.213 - e.eval("function foo() {}"); 68.214 - 68.215 - final ScriptContext newCtx = new SimpleScriptContext(); 68.216 - newCtx.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 68.217 - 68.218 - // all global definitions shared and so 'foo' should be 68.219 - // visible in new Bindings as well. 68.220 - assertTrue(e.eval("typeof foo", newCtx).equals("function")); 68.221 - 68.222 - e.eval("function bar() {}", newCtx); 68.223 - 68.224 - // bar should be visible in default context 68.225 - assertTrue(e.eval("typeof bar").equals("function")); 68.226 - } 68.227 - 68.228 - @Test 68.229 - public void classFilterTest() throws ScriptException { 68.230 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.231 - final ScriptEngine e = fac.getScriptEngine(new ClassFilter() { 68.232 - @Override 68.233 - public boolean exposeToScripts(final String fullName) { 68.234 - // don't allow anything that is not "java." 68.235 - return fullName.startsWith("java."); 68.236 - } 68.237 - }); 68.238 - 68.239 - assertEquals(e.eval("typeof javax.script.ScriptEngine"), "object"); 68.240 - assertEquals(e.eval("typeof java.util.Vector"), "function"); 68.241 - 68.242 - try { 68.243 - e.eval("Java.type('javax.script.ScriptContext')"); 68.244 - fail("should not reach here"); 68.245 - } catch (final ScriptException | RuntimeException se) { 68.246 - if (! (se.getCause() instanceof ClassNotFoundException)) { 68.247 - fail("ClassNotFoundException expected"); 68.248 - } 68.249 - } 68.250 - } 68.251 - 68.252 - @Test 68.253 - public void classFilterTest2() throws ScriptException { 68.254 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.255 - final ScriptEngine e = fac.getScriptEngine(new String[0], Thread.currentThread().getContextClassLoader(), 68.256 - new ClassFilter() { 68.257 - @Override 68.258 - public boolean exposeToScripts(final String fullName) { 68.259 - // don't allow anything that is not "java." 68.260 - return fullName.startsWith("java."); 68.261 - } 68.262 - }); 68.263 - 68.264 - assertEquals(e.eval("typeof javax.script.ScriptEngine"), "object"); 68.265 - assertEquals(e.eval("typeof java.util.Vector"), "function"); 68.266 - 68.267 - try { 68.268 - e.eval("Java.type('javax.script.ScriptContext')"); 68.269 - fail("should not reach here"); 68.270 - } catch (final ScriptException | RuntimeException se) { 68.271 - if (! (se.getCause() instanceof ClassNotFoundException)) { 68.272 - fail("ClassNotFoundException expected"); 68.273 - } 68.274 - } 68.275 - } 68.276 - 68.277 - @Test 68.278 - public void nullClassFilterTest() { 68.279 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.280 - try { 68.281 - fac.getScriptEngine((ClassFilter)null); 68.282 - fail("should have thrown NPE"); 68.283 - } catch (final NullPointerException e) { 68.284 - //empty 68.285 - } 68.286 - } 68.287 - 68.288 - @Test 68.289 - public void nullClassFilterTest2() { 68.290 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.291 - try { 68.292 - fac.getScriptEngine(new String[0], null, null); 68.293 - fail("should have thrown NPE"); 68.294 - } catch (final NullPointerException e) { 68.295 - //empty 68.296 - } 68.297 - } 68.298 - 68.299 - @Test 68.300 - public void nullArgsTest() { 68.301 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.302 - try { 68.303 - fac.getScriptEngine((String[])null); 68.304 - fail("should have thrown NPE"); 68.305 - } catch (final NullPointerException e) { 68.306 - //empty 68.307 - } 68.308 - } 68.309 - 68.310 - @Test 68.311 - public void nullArgsTest2() { 68.312 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.313 - try { 68.314 - fac.getScriptEngine(null, null, new ClassFilter() { 68.315 - @Override 68.316 - public boolean exposeToScripts(final String name) { 68.317 - return true; 68.318 - } 68.319 - }); 68.320 - fail("should have thrown NPE"); 68.321 - } catch (final NullPointerException e) { 68.322 - //empty 68.323 - } 68.324 - } 68.325 - 68.326 - @Test 68.327 - public void nashornSwallowsConstKeyword() throws Exception { 68.328 - final NashornScriptEngineFactory f = new NashornScriptEngineFactory(); 68.329 - final String[] args = new String[] { "--const-as-var" }; 68.330 - final ScriptEngine engine = f.getScriptEngine(args); 68.331 - 68.332 - final Object ret = engine.eval("" 68.333 - + "(function() {\n" 68.334 - + " const x = 10;\n" 68.335 - + " return x;\n" 68.336 - + "})();" 68.337 - ); 68.338 - assertEquals(ret, 10, "Parsed and executed OK"); 68.339 - } 68.340 - 68.341 - @Test 68.342 - public void evalDefaultFileNameTest() throws ScriptException { 68.343 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.344 - final ScriptEngine engine = fac.getScriptEngine(new String[] { "--verify-code=true" }); 68.345 - // default FILENAME being "<eval>" make sure generated code bytecode verifies. 68.346 - engine.eval("var a = 3;"); 68.347 - } 68.348 - 68.349 - @Test 68.350 - public void evalFileNameWithSpecialCharsTest() throws ScriptException { 68.351 - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 68.352 - final ScriptEngine engine = fac.getScriptEngine(new String[] { "--verify-code=true" }); 68.353 - final ScriptContext ctxt = new SimpleScriptContext(); 68.354 - // use file name with "dangerous" chars. 68.355 - ctxt.setAttribute(ScriptEngine.FILENAME, "<myscript>", ScriptContext.ENGINE_SCOPE); 68.356 - engine.eval("var a = 3;"); 68.357 - ctxt.setAttribute(ScriptEngine.FILENAME, "[myscript]", ScriptContext.ENGINE_SCOPE); 68.358 - engine.eval("var h = 'hello';"); 68.359 - ctxt.setAttribute(ScriptEngine.FILENAME, ";/\\$.", ScriptContext.ENGINE_SCOPE); 68.360 - engine.eval("var foo = 'world';"); 68.361 - // name used by jjs shell tool for the interactive mode 68.362 - ctxt.setAttribute(ScriptEngine.FILENAME, "<shell>", ScriptContext.ENGINE_SCOPE); 68.363 - engine.eval("var foo = 'world';"); 68.364 - } 68.365 -}
69.1 --- a/test/src/jdk/nashorn/internal/runtime/regexp/JdkRegExpTest.java Wed Apr 01 13:22:52 2015 -0700 69.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 69.3 @@ -1,60 +0,0 @@ 69.4 -/* 69.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 69.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 69.7 - * 69.8 - * This code is free software; you can redistribute it and/or modify it 69.9 - * under the terms of the GNU General Public License version 2 only, as 69.10 - * published by the Free Software Foundation. Oracle designates this 69.11 - * particular file as subject to the "Classpath" exception as provided 69.12 - * by Oracle in the LICENSE file that accompanied this code. 69.13 - * 69.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 69.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 69.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 69.17 - * version 2 for more details (a copy is included in the LICENSE file that 69.18 - * accompanied this code). 69.19 - * 69.20 - * You should have received a copy of the GNU General Public License version 69.21 - * 2 along with this work; if not, write to the Free Software Foundation, 69.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 69.23 - * 69.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 69.25 - * or visit www.oracle.com if you need additional information or have any 69.26 - * questions. 69.27 - */ 69.28 - 69.29 -package jdk.nashorn.internal.runtime.regexp; 69.30 - 69.31 -import static org.testng.Assert.assertEquals; 69.32 -import static org.testng.Assert.assertNotNull; 69.33 -import static org.testng.Assert.assertTrue; 69.34 - 69.35 -import org.testng.annotations.Test; 69.36 - 69.37 -/** 69.38 - * Basic tests for the JDK based RegExp implementation. 69.39 - * 69.40 - * @test 69.41 - * @run testng jdk.nashorn.internal.runtime.regexp.JdkRegExpTest 69.42 - */ 69.43 -public class JdkRegExpTest { 69.44 - 69.45 - /** 69.46 - * Compile a regular expression using the JDK implementation 69.47 - */ 69.48 - @Test 69.49 - public void testMatcher() { 69.50 - final RegExp regexp = new RegExpFactory().compile("f(o)o", ""); 69.51 - final RegExpMatcher matcher = regexp.match("foo"); 69.52 - assertNotNull(matcher); 69.53 - assertTrue(matcher.search(0)); 69.54 - assertEquals(matcher.getInput(), "foo"); 69.55 - assertEquals(matcher.groupCount(), 1); 69.56 - assertEquals(matcher.group(), "foo"); 69.57 - assertEquals(matcher.start(), 0); 69.58 - assertEquals(matcher.end(), 3); 69.59 - assertEquals(matcher.group(1), "o"); 69.60 - assertEquals(matcher.start(1), 1); 69.61 - assertEquals(matcher.end(1), 2); 69.62 - } 69.63 -}
70.1 --- a/test/src/jdk/nashorn/internal/runtime/regexp/joni/JoniTest.java Wed Apr 01 13:22:52 2015 -0700 70.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 70.3 @@ -1,50 +0,0 @@ 70.4 -/* 70.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 70.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 70.7 - * 70.8 - * This code is free software; you can redistribute it and/or modify it 70.9 - * under the terms of the GNU General Public License version 2 only, as 70.10 - * published by the Free Software Foundation. Oracle designates this 70.11 - * particular file as subject to the "Classpath" exception as provided 70.12 - * by Oracle in the LICENSE file that accompanied this code. 70.13 - * 70.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 70.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 70.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 70.17 - * version 2 for more details (a copy is included in the LICENSE file that 70.18 - * accompanied this code). 70.19 - * 70.20 - * You should have received a copy of the GNU General Public License version 70.21 - * 2 along with this work; if not, write to the Free Software Foundation, 70.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 70.23 - * 70.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 70.25 - * or visit www.oracle.com if you need additional information or have any 70.26 - * questions. 70.27 - */ 70.28 - 70.29 -package jdk.nashorn.internal.runtime.regexp.joni; 70.30 - 70.31 -import org.testng.annotations.Test; 70.32 - 70.33 -/** 70.34 - * Joni coverage tests 70.35 - * 70.36 - * @test 70.37 - * @run testng jdk.nashorn.internal.runtime.regexp.joni.JoniTest 70.38 - */ 70.39 -@SuppressWarnings("javadoc") 70.40 -public class JoniTest { 70.41 - 70.42 - @Test 70.43 - public void testDump() { 70.44 - new Regex("^a{3,}(.*)[z]++\\s\\1x$").dumpTree(); 70.45 - new Regex("^a{3,}(.*)[z]++\\s\\1x$").dumpByteCode(); 70.46 - new Regex("(abc){4,}{2,5}").dumpTree(); 70.47 - new Regex("(abc){4,}{2,5}").dumpByteCode(); 70.48 - new Regex("aaa|aa|bbbb|ccc").dumpTree(); 70.49 - new Regex("aaa|aa|bbbb|ccc").dumpByteCode(); 70.50 - new Regex("(?:ZFVR.(\\d+\\.\\d+))|(?:(?:Sversbk|TenaCnenqvfb|Vprjrnfry).(\\d+\\.\\d+))|(?:Bcren.(\\d+\\.\\d+))|(?:NccyrJroXvg.(\\d+(?:\\.\\d+)?))").dumpTree(); 70.51 - new Regex("(?:ZFVR.(\\d+\\.\\d+))|(?:(?:Sversbk|TenaCnenqvfb|Vprjrnfry).(\\d+\\.\\d+))|(?:Bcren.(\\d+\\.\\d+))|(?:NccyrJroXvg.(\\d+(?:\\.\\d+)?))").dumpByteCode(); 70.52 - } 70.53 -}
71.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 71.2 +++ b/test/src/jdk/nashorn/internal/runtime/regexp/joni/test/JoniTest.java Mon Apr 06 16:18:54 2015 +0530 71.3 @@ -0,0 +1,51 @@ 71.4 +/* 71.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 71.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 71.7 + * 71.8 + * This code is free software; you can redistribute it and/or modify it 71.9 + * under the terms of the GNU General Public License version 2 only, as 71.10 + * published by the Free Software Foundation. Oracle designates this 71.11 + * particular file as subject to the "Classpath" exception as provided 71.12 + * by Oracle in the LICENSE file that accompanied this code. 71.13 + * 71.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 71.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 71.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 71.17 + * version 2 for more details (a copy is included in the LICENSE file that 71.18 + * accompanied this code). 71.19 + * 71.20 + * You should have received a copy of the GNU General Public License version 71.21 + * 2 along with this work; if not, write to the Free Software Foundation, 71.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 71.23 + * 71.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 71.25 + * or visit www.oracle.com if you need additional information or have any 71.26 + * questions. 71.27 + */ 71.28 + 71.29 +package jdk.nashorn.internal.runtime.regexp.joni.test; 71.30 + 71.31 +import jdk.nashorn.internal.runtime.regexp.joni.Regex; 71.32 +import org.testng.annotations.Test; 71.33 + 71.34 +/** 71.35 + * Joni coverage tests 71.36 + * 71.37 + * @test 71.38 + * @run testng jdk.nashorn.internal.runtime.regexp.joni.JoniTest 71.39 + */ 71.40 +@SuppressWarnings("javadoc") 71.41 +public class JoniTest { 71.42 + 71.43 + @Test 71.44 + public void testDump() { 71.45 + new Regex("^a{3,}(.*)[z]++\\s\\1x$").dumpTree(); 71.46 + new Regex("^a{3,}(.*)[z]++\\s\\1x$").dumpByteCode(); 71.47 + new Regex("(abc){4,}{2,5}").dumpTree(); 71.48 + new Regex("(abc){4,}{2,5}").dumpByteCode(); 71.49 + new Regex("aaa|aa|bbbb|ccc").dumpTree(); 71.50 + new Regex("aaa|aa|bbbb|ccc").dumpByteCode(); 71.51 + new Regex("(?:ZFVR.(\\d+\\.\\d+))|(?:(?:Sversbk|TenaCnenqvfb|Vprjrnfry).(\\d+\\.\\d+))|(?:Bcren.(\\d+\\.\\d+))|(?:NccyrJroXvg.(\\d+(?:\\.\\d+)?))").dumpTree(); 71.52 + new Regex("(?:ZFVR.(\\d+\\.\\d+))|(?:(?:Sversbk|TenaCnenqvfb|Vprjrnfry).(\\d+\\.\\d+))|(?:Bcren.(\\d+\\.\\d+))|(?:NccyrJroXvg.(\\d+(?:\\.\\d+)?))").dumpByteCode(); 71.53 + } 71.54 +}
72.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 72.2 +++ b/test/src/jdk/nashorn/internal/runtime/regexp/test/JdkRegExpTest.java Mon Apr 06 16:18:54 2015 +0530 72.3 @@ -0,0 +1,63 @@ 72.4 +/* 72.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 72.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 72.7 + * 72.8 + * This code is free software; you can redistribute it and/or modify it 72.9 + * under the terms of the GNU General Public License version 2 only, as 72.10 + * published by the Free Software Foundation. Oracle designates this 72.11 + * particular file as subject to the "Classpath" exception as provided 72.12 + * by Oracle in the LICENSE file that accompanied this code. 72.13 + * 72.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 72.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 72.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 72.17 + * version 2 for more details (a copy is included in the LICENSE file that 72.18 + * accompanied this code). 72.19 + * 72.20 + * You should have received a copy of the GNU General Public License version 72.21 + * 2 along with this work; if not, write to the Free Software Foundation, 72.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 72.23 + * 72.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 72.25 + * or visit www.oracle.com if you need additional information or have any 72.26 + * questions. 72.27 + */ 72.28 + 72.29 +package jdk.nashorn.internal.runtime.regexp.test; 72.30 + 72.31 +import jdk.nashorn.internal.runtime.regexp.RegExp; 72.32 +import jdk.nashorn.internal.runtime.regexp.RegExpFactory; 72.33 +import jdk.nashorn.internal.runtime.regexp.RegExpMatcher; 72.34 +import static org.testng.Assert.assertEquals; 72.35 +import static org.testng.Assert.assertNotNull; 72.36 +import static org.testng.Assert.assertTrue; 72.37 + 72.38 +import org.testng.annotations.Test; 72.39 + 72.40 +/** 72.41 + * Basic tests for the JDK based RegExp implementation. 72.42 + * 72.43 + * @test 72.44 + * @run testng jdk.nashorn.internal.runtime.regexp.JdkRegExpTest 72.45 + */ 72.46 +public class JdkRegExpTest { 72.47 + 72.48 + /** 72.49 + * Compile a regular expression using the JDK implementation 72.50 + */ 72.51 + @Test 72.52 + public void testMatcher() { 72.53 + final RegExp regexp = new RegExpFactory().compile("f(o)o", ""); 72.54 + final RegExpMatcher matcher = regexp.match("foo"); 72.55 + assertNotNull(matcher); 72.56 + assertTrue(matcher.search(0)); 72.57 + assertEquals(matcher.getInput(), "foo"); 72.58 + assertEquals(matcher.groupCount(), 1); 72.59 + assertEquals(matcher.group(), "foo"); 72.60 + assertEquals(matcher.start(), 0); 72.61 + assertEquals(matcher.end(), 3); 72.62 + assertEquals(matcher.group(1), "o"); 72.63 + assertEquals(matcher.start(1), 1); 72.64 + assertEquals(matcher.end(1), 2); 72.65 + } 72.66 +}
73.1 --- a/test/src/jdk/nashorn/internal/runtime/resources/load_test.js Wed Apr 01 13:22:52 2015 -0700 73.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 73.3 @@ -1,28 +0,0 @@ 73.4 -/* 73.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 73.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 73.7 - * 73.8 - * This code is free software; you can redistribute it and/or modify it 73.9 - * under the terms of the GNU General Public License version 2 only, as 73.10 - * published by the Free Software Foundation. Oracle designates this 73.11 - * particular file as subject to the "Classpath" exception as provided 73.12 - * by Oracle in the LICENSE file that accompanied this code. 73.13 - * 73.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 73.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 73.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 73.17 - * version 2 for more details (a copy is included in the LICENSE file that 73.18 - * accompanied this code). 73.19 - * 73.20 - * You should have received a copy of the GNU General Public License version 73.21 - * 2 along with this work; if not, write to the Free Software Foundation, 73.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 73.23 - * 73.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 73.25 - * or visit www.oracle.com if you need additional information or have any 73.26 - * questions. 73.27 - */ 73.28 - 73.29 -function loadedFunc(arg) { 73.30 - return arg.toUpperCase(); 73.31 -}
74.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 74.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java Mon Apr 06 16:18:54 2015 +0530 74.3 @@ -0,0 +1,185 @@ 74.4 +/* 74.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 74.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 74.7 + * 74.8 + * This code is free software; you can redistribute it and/or modify it 74.9 + * under the terms of the GNU General Public License version 2 only, as 74.10 + * published by the Free Software Foundation. Oracle designates this 74.11 + * particular file as subject to the "Classpath" exception as provided 74.12 + * by Oracle in the LICENSE file that accompanied this code. 74.13 + * 74.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 74.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 74.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 74.17 + * version 2 for more details (a copy is included in the LICENSE file that 74.18 + * accompanied this code). 74.19 + * 74.20 + * You should have received a copy of the GNU General Public License version 74.21 + * 2 along with this work; if not, write to the Free Software Foundation, 74.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 74.23 + * 74.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 74.25 + * or visit www.oracle.com if you need additional information or have any 74.26 + * questions. 74.27 + */ 74.28 + 74.29 +package jdk.nashorn.internal.runtime.test; 74.30 + 74.31 +import static org.testng.Assert.fail; 74.32 +import java.io.File; 74.33 +import javax.script.ScriptEngine; 74.34 +import javax.script.ScriptException; 74.35 +import jdk.nashorn.api.scripting.ClassFilter; 74.36 +import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 74.37 +import jdk.nashorn.api.scripting.URLReader; 74.38 +import jdk.nashorn.internal.test.framework.TestFinder; 74.39 +import org.testng.annotations.Test; 74.40 + 74.41 +@SuppressWarnings("javadoc") 74.42 +public class ClassFilterTest { 74.43 + private static final String NASHORN_CODE_CACHE = "nashorn.persistent.code.cache"; 74.44 + private static final String CLASSFILTER_CODE_CACHE = "build/classfilter_nashorn_code_cache"; 74.45 + 74.46 + // @Test 74.47 + // This test takes too much time for basic "ant clean test" run. 74.48 + // Given that "allow-all-java-classes" is equivalent to no java class 74.49 + // filter and external tests don't access any java, not sure if this 74.50 + // test contributes much. We need faster "ant clean test" cycle for 74.51 + // developers. 74.52 + public void runExternalJsTest() { 74.53 + final String[] paths = new String[]{ 74.54 + "test/script/basic/compile-octane.js", 74.55 + "test/script/basic/jquery.js", 74.56 + "test/script/basic/prototype.js", 74.57 + "test/script/basic/runsunspider.js", 74.58 + "test/script/basic/underscore.js", 74.59 + "test/script/basic/yui.js", 74.60 + "test/script/basic/run-octane.js" 74.61 + }; 74.62 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 74.63 + for (final String path : paths) { 74.64 + final ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}, getClass().getClassLoader(), getClassFilter()); 74.65 + try { 74.66 + engine.eval(new URLReader(new File(path).toURI().toURL())); 74.67 + } catch (final Exception e) { 74.68 + fail("Script " + path + " fails with exception :" + e.getMessage()); 74.69 + } 74.70 + } 74.71 + } 74.72 + 74.73 + @Test 74.74 + public void noJavaOptionTest() { 74.75 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 74.76 + final ScriptEngine engine = factory.getScriptEngine(new String[]{"--no-java"}, getClass().getClassLoader(), getClassFilter()); 74.77 + try { 74.78 + engine.eval("var str = Java.type('java.lang.String');"); 74.79 + fail("TypeError should have been thrown"); 74.80 + } catch (final ScriptException e) { 74.81 + //emtpy 74.82 + } 74.83 + } 74.84 + 74.85 + @Test 74.86 + public void securityTest() { 74.87 + if (System.getSecurityManager() == null) { 74.88 + return; 74.89 + } 74.90 + 74.91 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 74.92 + final ScriptEngine engine = factory.getScriptEngine(getClassFilter()); 74.93 + try { 74.94 + engine.eval("var thread = Java.type('sun.misc.Unsafe')"); 74.95 + fail("SecurityException should have been thrown"); 74.96 + } catch (final Exception e) { 74.97 + //empty 74.98 + } 74.99 + try { 74.100 + engine.eval("var thread = new sun.misc.Unsafe()"); 74.101 + fail("SecurityException should have been thrown"); 74.102 + } catch (final Exception e) { 74.103 + //empty 74.104 + } 74.105 + try { 74.106 + engine.eval("var thread = Java.extend(sun.misc.Unsafe, {})"); 74.107 + fail("TypeError should have been thrown"); 74.108 + } catch (final Exception e) { 74.109 + //empty 74.110 + } 74.111 + try { 74.112 + engine.eval("java.lang.System.exit(0)"); 74.113 + fail("SecurityException should have been thrown"); 74.114 + } catch (final Exception e) { 74.115 + //empty 74.116 + } 74.117 + 74.118 + } 74.119 + 74.120 + @Test 74.121 + public void persistentCacheTest() { 74.122 + final String oldCodeCache = System.getProperty(NASHORN_CODE_CACHE); 74.123 + System.setProperty(NASHORN_CODE_CACHE, CLASSFILTER_CODE_CACHE); 74.124 + try { 74.125 + persistentCacheTestImpl(); 74.126 + } finally { 74.127 + if (oldCodeCache != null) { 74.128 + System.setProperty(NASHORN_CODE_CACHE, oldCodeCache); 74.129 + } 74.130 + } 74.131 + } 74.132 + 74.133 + private void persistentCacheTestImpl() { 74.134 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 74.135 + final ScriptEngine engine = factory.getScriptEngine( 74.136 + TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}), 74.137 + getClass().getClassLoader(), 74.138 + getClassFilter() 74.139 + ); 74.140 + final String testScript = "var a = Java.type('java.lang.String');" + generateCodeForPersistentStore(); 74.141 + try { 74.142 + engine.eval(testScript); 74.143 + } catch (final ScriptException exc) { 74.144 + fail(exc.getMessage()); 74.145 + } 74.146 + final ScriptEngine engineSafe = factory.getScriptEngine( 74.147 + TestFinder.addExplicitOptimisticTypes(new String[]{"--persistent-code-cache", "--optimistic-types=true"}), 74.148 + getClass().getClassLoader(), 74.149 + new ClassFilter() { 74.150 + @Override 74.151 + public boolean exposeToScripts(final String s) { 74.152 + return false; 74.153 + } 74.154 + } 74.155 + ); 74.156 + try { 74.157 + engineSafe.eval(testScript); 74.158 + fail("ClassNotFoundException should have been thrown"); 74.159 + } catch (final Exception exc) { 74.160 + if (!(exc.getCause() instanceof ClassNotFoundException)) { 74.161 + fail("ClassNotFoundException expected, got " + exc.getClass()); 74.162 + } 74.163 + } 74.164 + } 74.165 + 74.166 + private static String generateCodeForPersistentStore() { 74.167 + final StringBuilder stringBuilder = new StringBuilder(); 74.168 + for (int i=0; i < 100; i++) { 74.169 + stringBuilder.append("function i") 74.170 + .append(i) 74.171 + .append("(y, z) { var x") 74.172 + .append(i) 74.173 + .append(" = ") 74.174 + .append(i) 74.175 + .append(";}"); 74.176 + } 74.177 + return stringBuilder.toString(); 74.178 + } 74.179 + 74.180 + private static ClassFilter getClassFilter() { 74.181 + return new ClassFilter() { 74.182 + @Override 74.183 + public boolean exposeToScripts(final String s) { 74.184 + return true; 74.185 + } 74.186 + }; 74.187 + } 74.188 +}
75.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 75.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/CodeStoreAndPathTest.java Mon Apr 06 16:18:54 2015 +0530 75.3 @@ -0,0 +1,188 @@ 75.4 +/* 75.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 75.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 75.7 + * 75.8 + * This code is free software; you can redistribute it and/or modify it 75.9 + * under the terms of the GNU General Public License version 2 only, as 75.10 + * published by the Free Software Foundation. Oracle designates this 75.11 + * particular file as subject to the "Classpath" exception as provided 75.12 + * by Oracle in the LICENSE file that accompanied this code. 75.13 + * 75.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 75.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 75.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 75.17 + * version 2 for more details (a copy is included in the LICENSE file that 75.18 + * accompanied this code). 75.19 + * 75.20 + * You should have received a copy of the GNU General Public License version 75.21 + * 2 along with this work; if not, write to the Free Software Foundation, 75.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 75.23 + * 75.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 75.25 + * or visit www.oracle.com if you need additional information or have any 75.26 + * questions. 75.27 + */ 75.28 +package jdk.nashorn.internal.runtime.test; 75.29 + 75.30 +import static org.testng.Assert.assertEquals; 75.31 +import static org.testng.Assert.assertFalse; 75.32 +import java.io.File; 75.33 +import java.io.IOException; 75.34 +import java.nio.file.DirectoryStream; 75.35 +import java.nio.file.FileSystems; 75.36 +import java.nio.file.Files; 75.37 +import java.nio.file.Path; 75.38 +import javax.script.ScriptEngine; 75.39 +import javax.script.ScriptException; 75.40 +import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 75.41 +import org.testng.annotations.Test; 75.42 + 75.43 +/** 75.44 + * @test 75.45 + * @bug 8039185 8039403 75.46 + * @summary Test for persistent code cache and path handling 75.47 + * @run testng jdk.nashorn.internal.runtime.CodeStoreAndPathTest 75.48 + */ 75.49 +@SuppressWarnings("javadoc") 75.50 +public class CodeStoreAndPathTest { 75.51 + 75.52 + final String code1 = "var code1; var x = 'Hello Script'; var x1 = 'Hello Script'; " 75.53 + + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; " 75.54 + + "var x4 = 'Hello Script'; var x5 = 'Hello Script';" 75.55 + + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; " 75.56 + + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; " 75.57 + + "var x10 = 'Hello Script';" 75.58 + + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 75.59 + + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 75.60 + + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 75.61 + + "var x10 = 'Hello Script';}" 75.62 + + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 75.63 + + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 75.64 + + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 75.65 + + "var x10 = 'Hello Script';}" 75.66 + + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 75.67 + + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 75.68 + + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 75.69 + + "var x10 = 'Hello Script';}" 75.70 + + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 75.71 + + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 75.72 + + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 75.73 + + "var x10 = 'Hello Script';}"; 75.74 + final String code2 = "var code2; var x = 'Hello Script'; var x1 = 'Hello Script'; " 75.75 + + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; " 75.76 + + "var x4 = 'Hello Script'; var x5 = 'Hello Script';" 75.77 + + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; " 75.78 + + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; " 75.79 + + "var x10 = 'Hello Script';" 75.80 + + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 75.81 + + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 75.82 + + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 75.83 + + "var x10 = 'Hello Script';}" 75.84 + + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 75.85 + + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 75.86 + + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 75.87 + + "var x10 = 'Hello Script';}" 75.88 + + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 75.89 + + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 75.90 + + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 75.91 + + "var x10 = 'Hello Script';}" 75.92 + + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';" 75.93 + + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';" 75.94 + + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; " 75.95 + + "var x10 = 'Hello Script';}"; 75.96 + // Script size < Default minimum size for storing a compiled script class 75.97 + final String code3 = "var code3; var x = 'Hello Script'; var x1 = 'Hello Script'; "; 75.98 + final String codeCache = "build/nashorn_code_cache"; 75.99 + final String oldUserDir = System.getProperty("user.dir"); 75.100 + 75.101 + private static final String[] ENGINE_OPTIONS_OPT = new String[]{"--persistent-code-cache", "--optimistic-types=true"}; 75.102 + private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"}; 75.103 + 75.104 + @Test 75.105 + public void pathHandlingTest() { 75.106 + System.setProperty("nashorn.persistent.code.cache", codeCache); 75.107 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 75.108 + 75.109 + fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 75.110 + 75.111 + final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache); 75.112 + final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty( 75.113 + "nashorn.persistent.code.cache")).toAbsolutePath(); 75.114 + // Check that nashorn code cache is created in current working directory 75.115 + assertEquals(actualCodeCachePath, expectedCodeCachePath); 75.116 + // Check that code cache dir exists and it's not empty 75.117 + final File file = new File(actualCodeCachePath.toUri()); 75.118 + assertFalse(!file.isDirectory(), "No code cache directory was created!"); 75.119 + assertFalse(file.list().length == 0, "Code cache directory is empty!"); 75.120 + } 75.121 + 75.122 + @Test 75.123 + public void changeUserDirTest() throws ScriptException, IOException { 75.124 + System.setProperty("nashorn.persistent.code.cache", codeCache); 75.125 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 75.126 + final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 75.127 + final Path codeCachePath = getCodeCachePath(false); 75.128 + final String newUserDir = "build/newUserDir"; 75.129 + // Now changing current working directory 75.130 + System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir); 75.131 + try { 75.132 + // Check that a new compiled script is stored in existing code cache 75.133 + e.eval(code1); 75.134 + final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 75.135 + checkCompiledScripts(stream, 1); 75.136 + // Setting to default current working dir 75.137 + } finally { 75.138 + System.setProperty("user.dir", oldUserDir); 75.139 + } 75.140 + } 75.141 + 75.142 + @Test 75.143 + public void codeCacheTest() throws ScriptException, IOException { 75.144 + System.setProperty("nashorn.persistent.code.cache", codeCache); 75.145 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 75.146 + final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT); 75.147 + final Path codeCachePath = getCodeCachePath(false); 75.148 + e.eval(code1); 75.149 + e.eval(code2); 75.150 + e.eval(code3);// less than minimum size for storing 75.151 + // adding code1 and code2. 75.152 + final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 75.153 + checkCompiledScripts(stream, 2); 75.154 + } 75.155 + 75.156 + @Test 75.157 + public void codeCacheTestOpt() throws ScriptException, IOException { 75.158 + System.setProperty("nashorn.persistent.code.cache", codeCache); 75.159 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 75.160 + final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT); 75.161 + final Path codeCachePath = getCodeCachePath(true); 75.162 + e.eval(code1); 75.163 + e.eval(code2); 75.164 + e.eval(code3);// less than minimum size for storing 75.165 + // adding code1 and code2. 75.166 + final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); 75.167 + checkCompiledScripts(stream, 2); 75.168 + } 75.169 + 75.170 + private static Path getCodeCachePath(final boolean optimistic) { 75.171 + final String codeCache = System.getProperty("nashorn.persistent.code.cache"); 75.172 + final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath(); 75.173 + final String[] files = codeCachePath.toFile().list(); 75.174 + for (final String file : files) { 75.175 + if (file.endsWith("_opt") == optimistic) { 75.176 + return codeCachePath.resolve(file); 75.177 + } 75.178 + } 75.179 + throw new AssertionError("Code cache path not found"); 75.180 + } 75.181 + 75.182 + private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException { 75.183 + int n = numberOfScripts; 75.184 + for (@SuppressWarnings("unused") final Path file : stream) { 75.185 + n--; 75.186 + } 75.187 + stream.close(); 75.188 + assertEquals(n, 0); 75.189 + } 75.190 + 75.191 +}
76.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 76.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/ConsStringTest.java Mon Apr 06 16:18:54 2015 +0530 76.3 @@ -0,0 +1,132 @@ 76.4 +/* 76.5 + * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 76.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 76.7 + * 76.8 + * This code is free software; you can redistribute it and/or modify it 76.9 + * under the terms of the GNU General Public License version 2 only, as 76.10 + * published by the Free Software Foundation. Oracle designates this 76.11 + * particular file as subject to the "Classpath" exception as provided 76.12 + * by Oracle in the LICENSE file that accompanied this code. 76.13 + * 76.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 76.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 76.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 76.17 + * version 2 for more details (a copy is included in the LICENSE file that 76.18 + * accompanied this code). 76.19 + * 76.20 + * You should have received a copy of the GNU General Public License version 76.21 + * 2 along with this work; if not, write to the Free Software Foundation, 76.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 76.23 + * 76.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 76.25 + * or visit www.oracle.com if you need additional information or have any 76.26 + * questions. 76.27 + */ 76.28 + 76.29 +package jdk.nashorn.internal.runtime.test; 76.30 + 76.31 +import jdk.nashorn.internal.runtime.ConsString; 76.32 +import static org.testng.Assert.assertEquals; 76.33 + 76.34 +import org.testng.annotations.Test; 76.35 + 76.36 +/** 76.37 + * Tests for JSType methods. 76.38 + * 76.39 + * @test 76.40 + * @run testng jdk.nashorn.internal.runtime.ConsStringTest 76.41 + */ 76.42 +public class ConsStringTest { 76.43 + 76.44 + /** 76.45 + * Test toString conversion 76.46 + */ 76.47 + @Test 76.48 + public void testConsStringToString() { 76.49 + final ConsString cs1 = new ConsString("b", "c"); 76.50 + final ConsString cs2 = new ConsString("d", "e"); 76.51 + final ConsString cs3 = new ConsString(cs1, cs2); 76.52 + final ConsString cs4 = new ConsString(cs3, "f"); 76.53 + final ConsString cs5 = new ConsString("a", cs4); 76.54 + assertEquals(cs5.toString(), "abcdef"); 76.55 + assertEquals(cs4.toString(), "bcdef"); 76.56 + assertEquals(cs3.toString(), "bcde"); 76.57 + assertEquals(cs2.toString(), "de"); 76.58 + assertEquals(cs1.toString(), "bc"); 76.59 + // ConsStrings should be flattened now 76.60 + assertEquals(cs1.getComponents()[0], "bc"); 76.61 + assertEquals(cs1.getComponents()[1], ""); 76.62 + assertEquals(cs2.getComponents()[0], "de"); 76.63 + assertEquals(cs2.getComponents()[1], ""); 76.64 + assertEquals(cs3.getComponents()[0], "bcde"); 76.65 + assertEquals(cs3.getComponents()[1], ""); 76.66 + assertEquals(cs4.getComponents()[0], "bcdef"); 76.67 + assertEquals(cs4.getComponents()[1], ""); 76.68 + assertEquals(cs5.getComponents()[0], "abcdef"); 76.69 + assertEquals(cs5.getComponents()[1], ""); 76.70 + } 76.71 + 76.72 + /** 76.73 + * Test charAt 76.74 + */ 76.75 + @Test 76.76 + public void testConsStringCharAt() { 76.77 + final ConsString cs1 = new ConsString("b", "c"); 76.78 + final ConsString cs2 = new ConsString("d", "e"); 76.79 + final ConsString cs3 = new ConsString(cs1, cs2); 76.80 + final ConsString cs4 = new ConsString(cs3, "f"); 76.81 + final ConsString cs5 = new ConsString("a", cs4); 76.82 + assertEquals(cs1.charAt(1), 'c'); 76.83 + assertEquals(cs2.charAt(0), 'd'); 76.84 + assertEquals(cs3.charAt(3), 'e'); 76.85 + assertEquals(cs4.charAt(1), 'c'); 76.86 + assertEquals(cs5.charAt(2), 'c'); 76.87 + // ConsStrings should be flattened now 76.88 + assertEquals(cs1.getComponents()[0], "bc"); 76.89 + assertEquals(cs1.getComponents()[1], ""); 76.90 + assertEquals(cs2.getComponents()[0], "de"); 76.91 + assertEquals(cs2.getComponents()[1], ""); 76.92 + assertEquals(cs3.getComponents()[0], "bcde"); 76.93 + assertEquals(cs3.getComponents()[1], ""); 76.94 + assertEquals(cs4.getComponents()[0], "bcdef"); 76.95 + assertEquals(cs4.getComponents()[1], ""); 76.96 + assertEquals(cs5.getComponents()[0], "abcdef"); 76.97 + assertEquals(cs5.getComponents()[1], ""); 76.98 + } 76.99 + 76.100 + 76.101 + /** 76.102 + * Test flattening of top-level and internal ConsStrings 76.103 + */ 76.104 + @Test 76.105 + public void testConsStringFlattening() { 76.106 + final ConsString cs1 = new ConsString("b", "c"); 76.107 + final ConsString cs2 = new ConsString("d", "e"); 76.108 + final ConsString cs3 = new ConsString(cs1, cs2); 76.109 + final ConsString cs4 = new ConsString(cs3, "f"); 76.110 + 76.111 + final ConsString cs5 = new ConsString("a", cs4); 76.112 + // top-level ConsString should not yet be flattened 76.113 + assert(cs5.getComponents()[0] == "a"); 76.114 + assert(cs5.getComponents()[1] == cs4); 76.115 + assertEquals(cs5.toString(), "abcdef"); 76.116 + // top-level ConsString should be flattened 76.117 + assertEquals(cs5.getComponents()[0], "abcdef"); 76.118 + assertEquals(cs5.getComponents()[1], ""); 76.119 + // internal ConsString should not yet be flattened after first traversal 76.120 + assertEquals(cs4.getComponents()[0], cs3); 76.121 + assertEquals(cs4.getComponents()[1], "f"); 76.122 + 76.123 + final ConsString cs6 = new ConsString("a", cs4); 76.124 + // top-level ConsString should not yet be flattened 76.125 + assertEquals(cs6.getComponents()[0], "a"); 76.126 + assertEquals(cs6.getComponents()[1], cs4); 76.127 + assertEquals(cs6.toString(), "abcdef"); 76.128 + // top-level ConsString should be flattened 76.129 + assertEquals(cs6.getComponents()[0], "abcdef"); 76.130 + assertEquals(cs6.getComponents()[1], ""); 76.131 + // internal ConsString should have been flattened after second traversal 76.132 + assertEquals(cs4.getComponents()[0], "bcdef"); 76.133 + assertEquals(cs4.getComponents()[1], ""); 76.134 + } 76.135 +}
77.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 77.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/ContextTest.java Mon Apr 06 16:18:54 2015 +0530 77.3 @@ -0,0 +1,144 @@ 77.4 +/* 77.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 77.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 77.7 + * 77.8 + * This code is free software; you can redistribute it and/or modify it 77.9 + * under the terms of the GNU General Public License version 2 only, as 77.10 + * published by the Free Software Foundation. Oracle designates this 77.11 + * particular file as subject to the "Classpath" exception as provided 77.12 + * by Oracle in the LICENSE file that accompanied this code. 77.13 + * 77.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 77.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 77.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 77.17 + * version 2 for more details (a copy is included in the LICENSE file that 77.18 + * accompanied this code). 77.19 + * 77.20 + * You should have received a copy of the GNU General Public License version 77.21 + * 2 along with this work; if not, write to the Free Software Foundation, 77.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 77.23 + * 77.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 77.25 + * or visit www.oracle.com if you need additional information or have any 77.26 + * questions. 77.27 + */ 77.28 + 77.29 +package jdk.nashorn.internal.runtime.test; 77.30 + 77.31 +import static jdk.nashorn.internal.runtime.Source.sourceFor; 77.32 +import static org.testng.Assert.assertEquals; 77.33 +import static org.testng.Assert.assertTrue; 77.34 +import static org.testng.Assert.fail; 77.35 +import java.util.Map; 77.36 +import jdk.nashorn.internal.objects.Global; 77.37 +import jdk.nashorn.internal.runtime.Context; 77.38 +import jdk.nashorn.internal.runtime.ErrorManager; 77.39 +import jdk.nashorn.internal.runtime.ScriptFunction; 77.40 +import jdk.nashorn.internal.runtime.ScriptObject; 77.41 +import jdk.nashorn.internal.runtime.ScriptRuntime; 77.42 +import jdk.nashorn.internal.runtime.Source; 77.43 +import jdk.nashorn.internal.runtime.options.Options; 77.44 +import org.testng.annotations.Test; 77.45 + 77.46 +/** 77.47 + * Basic Context API tests. 77.48 + * 77.49 + * @test 77.50 + * @run testng jdk.nashorn.internal.runtime.ContextTest 77.51 + */ 77.52 +@SuppressWarnings("javadoc") 77.53 +public class ContextTest { 77.54 + // basic context eval test 77.55 + @Test 77.56 + public void evalTest() { 77.57 + final Options options = new Options(""); 77.58 + final ErrorManager errors = new ErrorManager(); 77.59 + final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 77.60 + final Global oldGlobal = Context.getGlobal(); 77.61 + Context.setGlobal(cx.createGlobal()); 77.62 + try { 77.63 + String code = "22 + 10"; 77.64 + assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue()); 77.65 + 77.66 + code = "obj = { js: 'nashorn' }; obj.js"; 77.67 + assertEquals(eval(cx, "<evalTest2>", code), "nashorn"); 77.68 + } finally { 77.69 + Context.setGlobal(oldGlobal); 77.70 + } 77.71 + } 77.72 + 77.73 + // Make sure trying to compile an invalid script returns null - see JDK-8046215. 77.74 + @Test 77.75 + public void compileErrorTest() { 77.76 + final Options options = new Options(""); 77.77 + final ErrorManager errors = new ErrorManager(); 77.78 + final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 77.79 + final Global oldGlobal = Context.getGlobal(); 77.80 + Context.setGlobal(cx.createGlobal()); 77.81 + try { 77.82 + final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal()); 77.83 + if (script != null) { 77.84 + fail("Invalid script compiled without errors"); 77.85 + } 77.86 + if (errors.getNumberOfErrors() != 1) { 77.87 + fail("Wrong number of errors: " + errors.getNumberOfErrors()); 77.88 + } 77.89 + } finally { 77.90 + Context.setGlobal(oldGlobal); 77.91 + } 77.92 + } 77.93 + 77.94 + // basic check for JS reflection access - java.util.Map-like access on ScriptObject 77.95 + @Test 77.96 + public void reflectionTest() { 77.97 + final Options options = new Options(""); 77.98 + final ErrorManager errors = new ErrorManager(); 77.99 + final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 77.100 + final boolean strict = cx.getEnv()._strict; 77.101 + final Global oldGlobal = Context.getGlobal(); 77.102 + Context.setGlobal(cx.createGlobal()); 77.103 + 77.104 + try { 77.105 + final String code = "var obj = { x: 344, y: 42 }"; 77.106 + eval(cx, "<reflectionTest>", code); 77.107 + 77.108 + final Object obj = Context.getGlobal().get("obj"); 77.109 + 77.110 + assertTrue(obj instanceof ScriptObject); 77.111 + 77.112 + final ScriptObject sobj = (ScriptObject)obj; 77.113 + int count = 0; 77.114 + for (final Map.Entry<?, ?> ex : sobj.entrySet()) { 77.115 + final Object key = ex.getKey(); 77.116 + if (key.equals("x")) { 77.117 + assertTrue(ex.getValue() instanceof Number); 77.118 + assertTrue(344.0 == ((Number)ex.getValue()).doubleValue()); 77.119 + 77.120 + count++; 77.121 + } else if (key.equals("y")) { 77.122 + assertTrue(ex.getValue() instanceof Number); 77.123 + assertTrue(42.0 == ((Number)ex.getValue()).doubleValue()); 77.124 + 77.125 + count++; 77.126 + } 77.127 + } 77.128 + assertEquals(count, 2); 77.129 + assertEquals(sobj.size(), 2); 77.130 + 77.131 + // add property 77.132 + sobj.put("zee", "hello", strict); 77.133 + assertEquals(sobj.get("zee"), "hello"); 77.134 + assertEquals(sobj.size(), 3); 77.135 + 77.136 + } finally { 77.137 + Context.setGlobal(oldGlobal); 77.138 + } 77.139 + } 77.140 + 77.141 + private static Object eval(final Context cx, final String name, final String code) { 77.142 + final Source source = sourceFor(name, code); 77.143 + final ScriptObject global = Context.getGlobal(); 77.144 + final ScriptFunction func = cx.compileScript(source, global); 77.145 + return func != null ? ScriptRuntime.apply(func, global) : null; 77.146 + } 77.147 +}
78.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 78.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/ExceptionsNotSerializable.java Mon Apr 06 16:18:54 2015 +0530 78.3 @@ -0,0 +1,79 @@ 78.4 +/* 78.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 78.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 78.7 + * 78.8 + * This code is free software; you can redistribute it and/or modify it 78.9 + * under the terms of the GNU General Public License version 2 only, as 78.10 + * published by the Free Software Foundation. Oracle designates this 78.11 + * particular file as subject to the "Classpath" exception as provided 78.12 + * by Oracle in the LICENSE file that accompanied this code. 78.13 + * 78.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 78.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 78.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 78.17 + * version 2 for more details (a copy is included in the LICENSE file that 78.18 + * accompanied this code). 78.19 + * 78.20 + * You should have received a copy of the GNU General Public License version 78.21 + * 2 along with this work; if not, write to the Free Software Foundation, 78.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 78.23 + * 78.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 78.25 + * or visit www.oracle.com if you need additional information or have any 78.26 + * questions. 78.27 + */ 78.28 + 78.29 +package jdk.nashorn.internal.runtime.test; 78.30 + 78.31 +import static org.testng.Assert.assertEquals; 78.32 +import static org.testng.Assert.fail; 78.33 +import java.io.ByteArrayOutputStream; 78.34 +import java.io.IOException; 78.35 +import java.io.NotSerializableException; 78.36 +import java.io.ObjectOutputStream; 78.37 +import javax.script.ScriptEngine; 78.38 +import javax.script.ScriptException; 78.39 +import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 78.40 +import jdk.nashorn.internal.runtime.RewriteException; 78.41 +import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; 78.42 +import org.testng.annotations.Test; 78.43 + 78.44 +/** 78.45 + * JDK-8044518: Ensure exceptions related to optimistic recompilation are not serializable 78.46 + * 78.47 + * @test 78.48 + * @run testng jdk.nashorn.internal.runtime.ExceptionsNotSerializable 78.49 + */ 78.50 +@SuppressWarnings("javadoc") 78.51 +public class ExceptionsNotSerializable { 78.52 + @Test 78.53 + public void rewriteExceptionNotSerializable() throws ScriptException { 78.54 + // NOTE: we must create a RewriteException in a context of a Nashorn engine, as it uses Global.newIntance() 78.55 + // internally. 78.56 + final ScriptEngine e = new NashornScriptEngineFactory().getScriptEngine(); 78.57 + e.put("f", new Runnable() { 78.58 + @Override 78.59 + public void run() { 78.60 + tryToSerialize(RewriteException.create(null, new Object[0], new String[0])); 78.61 + } 78.62 + }); 78.63 + e.eval("f()"); 78.64 + } 78.65 + 78.66 + @Test 78.67 + public void unwarrantedOptimismExceptionNotSerializable() { 78.68 + tryToSerialize(new UnwarrantedOptimismException(new Double(1.0), 128)); 78.69 + } 78.70 + 78.71 + private static void tryToSerialize(final Object obj) { 78.72 + try { 78.73 + new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(obj); 78.74 + fail(); 78.75 + } catch (final NotSerializableException e) { 78.76 + assertEquals(e.getMessage(), obj.getClass().getName()); 78.77 + } catch (final IOException e) { 78.78 + fail("", e); 78.79 + } 78.80 + 78.81 + } 78.82 +}
79.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 79.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/JSTypeTest.java Mon Apr 06 16:18:54 2015 +0530 79.3 @@ -0,0 +1,195 @@ 79.4 +/* 79.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 79.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 79.7 + * 79.8 + * This code is free software; you can redistribute it and/or modify it 79.9 + * under the terms of the GNU General Public License version 2 only, as 79.10 + * published by the Free Software Foundation. Oracle designates this 79.11 + * particular file as subject to the "Classpath" exception as provided 79.12 + * by Oracle in the LICENSE file that accompanied this code. 79.13 + * 79.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 79.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 79.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 79.17 + * version 2 for more details (a copy is included in the LICENSE file that 79.18 + * accompanied this code). 79.19 + * 79.20 + * You should have received a copy of the GNU General Public License version 79.21 + * 2 along with this work; if not, write to the Free Software Foundation, 79.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 79.23 + * 79.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 79.25 + * or visit www.oracle.com if you need additional information or have any 79.26 + * questions. 79.27 + */ 79.28 + 79.29 +package jdk.nashorn.internal.runtime.test; 79.30 + 79.31 +import jdk.nashorn.internal.runtime.JSType; 79.32 +import jdk.nashorn.internal.runtime.ScriptRuntime; 79.33 +import static org.testng.Assert.assertEquals; 79.34 +import static org.testng.Assert.assertFalse; 79.35 +import static org.testng.Assert.assertTrue; 79.36 + 79.37 +import org.testng.annotations.Test; 79.38 + 79.39 +/** 79.40 + * Tests for JSType methods. 79.41 + * 79.42 + * @test 79.43 + * @run testng jdk.nashorn.internal.runtime.JSTypeTest 79.44 + */ 79.45 +public class JSTypeTest { 79.46 + /** 79.47 + * Test of isPrimitive method, of class Runtime. 79.48 + */ 79.49 + @Test 79.50 + public void testIsPrimitive() { 79.51 + assertTrue(JSType.isPrimitive(null)); 79.52 + assertTrue(JSType.isPrimitive(ScriptRuntime.UNDEFINED)); 79.53 + assertTrue(JSType.isPrimitive(Double.NaN)); 79.54 + assertTrue(JSType.isPrimitive(Double.NEGATIVE_INFINITY)); 79.55 + assertTrue(JSType.isPrimitive(Double.POSITIVE_INFINITY)); 79.56 + assertTrue(JSType.isPrimitive(0.0)); 79.57 + assertTrue(JSType.isPrimitive(3.14)); 79.58 + assertTrue(JSType.isPrimitive("hello")); 79.59 + assertTrue(JSType.isPrimitive("")); 79.60 + assertFalse(JSType.isPrimitive(new Object())); 79.61 + } 79.62 + 79.63 + /** 79.64 + * Test of toBoolean method, of class Runtime. 79.65 + */ 79.66 + @Test 79.67 + public void testToBoolean() { 79.68 + assertFalse(JSType.toBoolean(ScriptRuntime.UNDEFINED)); 79.69 + assertFalse(JSType.toBoolean(null)); 79.70 + assertFalse(JSType.toBoolean(Boolean.FALSE)); 79.71 + assertTrue(JSType.toBoolean(Boolean.TRUE)); 79.72 + assertFalse(JSType.toBoolean(-0.0)); 79.73 + assertFalse(JSType.toBoolean(0.0)); 79.74 + assertFalse(JSType.toBoolean(Double.NaN)); 79.75 + assertTrue(JSType.toBoolean(3.14)); 79.76 + assertFalse(JSType.toBoolean("")); 79.77 + assertTrue(JSType.toBoolean("javascript")); 79.78 + assertTrue(JSType.toBoolean(new Object())); 79.79 + } 79.80 + 79.81 + /** 79.82 + * Test of toNumber method, of class Runtime. 79.83 + */ 79.84 + @Test 79.85 + public void testToNumber_Object() { 79.86 + assertTrue(Double.isNaN(JSType.toNumber(ScriptRuntime.UNDEFINED))); 79.87 + assertEquals(JSType.toNumber((Object)null), 0.0, 0.0); 79.88 + assertEquals(JSType.toNumber(Boolean.TRUE), 1.0, 0.0); 79.89 + assertEquals(JSType.toNumber(Boolean.FALSE), 0.0, 0.0); 79.90 + assertEquals(JSType.toNumber(3.14), 3.14, 0.0); 79.91 + // FIXME: add more assertions for specific String to number cases 79.92 + // FIXME: add case for Object type (JSObject with getDefaultValue) 79.93 + } 79.94 + 79.95 + /** 79.96 + * Test of toString method, of class Runtime. 79.97 + */ 79.98 + @Test 79.99 + public void testToString_Object() { 79.100 + assertEquals(JSType.toString(ScriptRuntime.UNDEFINED), "undefined"); 79.101 + assertEquals(JSType.toString(null), "null"); 79.102 + assertEquals(JSType.toString(Boolean.TRUE), "true"); 79.103 + assertEquals(JSType.toString(Boolean.FALSE), "false"); 79.104 + assertEquals(JSType.toString(""), ""); 79.105 + assertEquals(JSType.toString("nashorn"), "nashorn"); 79.106 + assertEquals(JSType.toString(Double.NaN), "NaN"); 79.107 + assertEquals(JSType.toString(Double.POSITIVE_INFINITY), "Infinity"); 79.108 + assertEquals(JSType.toString(Double.NEGATIVE_INFINITY), "-Infinity"); 79.109 + assertEquals(JSType.toString(0.0), "0"); 79.110 + // FIXME: add more number-to-string test cases 79.111 + // FIXME: add case for Object type (JSObject with getDefaultValue) 79.112 + } 79.113 + 79.114 + /** 79.115 + * Test of JSType.toUint32(double) 79.116 + */ 79.117 + @Test 79.118 + public void testToUint32() { 79.119 + assertEquals(JSType.toUint32(+0.0), 0); 79.120 + assertEquals(JSType.toUint32(-0.0), 0); 79.121 + assertEquals(JSType.toUint32(Double.NaN), 0); 79.122 + assertEquals(JSType.toUint32(Double.POSITIVE_INFINITY), 0); 79.123 + assertEquals(JSType.toUint32(Double.NEGATIVE_INFINITY), 0); 79.124 + assertEquals(JSType.toUint32(9223372036854775807.0d), 0); 79.125 + assertEquals(JSType.toUint32(-9223372036854775807.0d), 0); 79.126 + assertEquals(JSType.toUint32(1099511627776.0d), 0); 79.127 + assertEquals(JSType.toUint32(-1099511627776.0d), 0); 79.128 + assertEquals(JSType.toUint32(4294967295.0d), 4294967295l); 79.129 + assertEquals(JSType.toUint32(4294967296.0d), 0); 79.130 + assertEquals(JSType.toUint32(4294967297.0d), 1); 79.131 + assertEquals(JSType.toUint32(-4294967295.0d), 1); 79.132 + assertEquals(JSType.toUint32(-4294967296.0d), 0); 79.133 + assertEquals(JSType.toUint32(-4294967297.0d), 4294967295l); 79.134 + assertEquals(JSType.toUint32(4294967295.6d), 4294967295l); 79.135 + assertEquals(JSType.toUint32(4294967296.6d), 0); 79.136 + assertEquals(JSType.toUint32(4294967297.6d), 1); 79.137 + assertEquals(JSType.toUint32(-4294967295.6d), 1); 79.138 + assertEquals(JSType.toUint32(-4294967296.6d), 0); 79.139 + assertEquals(JSType.toUint32(-4294967297.6d), 4294967295l); 79.140 + } 79.141 + 79.142 + /** 79.143 + * Test of JSType.toInt32(double) 79.144 + */ 79.145 + @Test 79.146 + public void testToInt32() { 79.147 + assertEquals(JSType.toInt32(+0.0), 0); 79.148 + assertEquals(JSType.toInt32(-0.0), 0); 79.149 + assertEquals(JSType.toInt32(Double.NaN), 0); 79.150 + assertEquals(JSType.toInt32(Double.POSITIVE_INFINITY), 0); 79.151 + assertEquals(JSType.toInt32(Double.NEGATIVE_INFINITY), 0); 79.152 + assertEquals(JSType.toInt32(9223372036854775807.0d), 0); 79.153 + assertEquals(JSType.toInt32(-9223372036854775807.0d), 0); 79.154 + assertEquals(JSType.toInt32(1099511627776.0d), 0); 79.155 + assertEquals(JSType.toInt32(-1099511627776.0d), 0); 79.156 + assertEquals(JSType.toInt32(4294967295.0d), -1); 79.157 + assertEquals(JSType.toInt32(4294967296.0d), 0); 79.158 + assertEquals(JSType.toInt32(4294967297.0d), 1); 79.159 + assertEquals(JSType.toInt32(-4294967295.0d), 1); 79.160 + assertEquals(JSType.toInt32(-4294967296.0d), 0); 79.161 + assertEquals(JSType.toInt32(-4294967297.d), -1); 79.162 + assertEquals(JSType.toInt32(4294967295.6d), -1); 79.163 + assertEquals(JSType.toInt32(4294967296.6d), 0); 79.164 + assertEquals(JSType.toInt32(4294967297.6d), 1); 79.165 + assertEquals(JSType.toInt32(-4294967295.6d), 1); 79.166 + assertEquals(JSType.toInt32(-4294967296.6d), 0); 79.167 + assertEquals(JSType.toInt32(-4294967297.6d), -1); 79.168 + } 79.169 + 79.170 + /** 79.171 + * Test of JSType.toUint16(double) 79.172 + */ 79.173 + @Test 79.174 + public void testToUint16() { 79.175 + assertEquals(JSType.toUint16(+0.0), 0); 79.176 + assertEquals(JSType.toUint16(-0.0), 0); 79.177 + assertEquals(JSType.toUint16(Double.NaN), 0); 79.178 + assertEquals(JSType.toUint16(Double.POSITIVE_INFINITY), 0); 79.179 + assertEquals(JSType.toUint16(Double.NEGATIVE_INFINITY), 0); 79.180 + assertEquals(JSType.toUint16(9223372036854775807.0d), 0); 79.181 + assertEquals(JSType.toUint16(-9223372036854775807.0d), 0); 79.182 + assertEquals(JSType.toUint16(1099511627776.0d), 0); 79.183 + assertEquals(JSType.toUint16(-1099511627776.0d), 0); 79.184 + assertEquals(JSType.toUint16(4294967295.0d), 65535); 79.185 + assertEquals(JSType.toUint16(4294967296.0d), 0); 79.186 + assertEquals(JSType.toUint16(4294967297.0d), 1); 79.187 + assertEquals(JSType.toUint16(-4294967295.0d), 1); 79.188 + assertEquals(JSType.toUint16(-4294967296.0d), 0); 79.189 + assertEquals(JSType.toUint16(-4294967297.0d), 65535); 79.190 + assertEquals(JSType.toUint16(4294967295.6d), 65535); 79.191 + assertEquals(JSType.toUint16(4294967296.6d), 0); 79.192 + assertEquals(JSType.toUint16(4294967297.6d), 1); 79.193 + assertEquals(JSType.toUint16(-4294967295.6d), 1); 79.194 + assertEquals(JSType.toUint16(-4294967296.6d), 0); 79.195 + assertEquals(JSType.toUint16(-4294967297.6d), 65535); 79.196 + } 79.197 + 79.198 +}
80.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 80.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/LexicalBindingTest.java Mon Apr 06 16:18:54 2015 +0530 80.3 @@ -0,0 +1,212 @@ 80.4 +/* 80.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 80.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 80.7 + * 80.8 + * This code is free software; you can redistribute it and/or modify it 80.9 + * under the terms of the GNU General Public License version 2 only, as 80.10 + * published by the Free Software Foundation. Oracle designates this 80.11 + * particular file as subject to the "Classpath" exception as provided 80.12 + * by Oracle in the LICENSE file that accompanied this code. 80.13 + * 80.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 80.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 80.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 80.17 + * version 2 for more details (a copy is included in the LICENSE file that 80.18 + * accompanied this code). 80.19 + * 80.20 + * You should have received a copy of the GNU General Public License version 80.21 + * 2 along with this work; if not, write to the Free Software Foundation, 80.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 80.23 + * 80.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 80.25 + * or visit www.oracle.com if you need additional information or have any 80.26 + * questions. 80.27 + */ 80.28 + 80.29 +package jdk.nashorn.internal.runtime.test; 80.30 + 80.31 +import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 80.32 +import org.testng.annotations.Test; 80.33 + 80.34 +import javax.script.Bindings; 80.35 +import javax.script.ScriptContext; 80.36 +import javax.script.ScriptEngine; 80.37 +import javax.script.ScriptException; 80.38 +import javax.script.SimpleScriptContext; 80.39 + 80.40 +import static org.testng.Assert.assertEquals; 80.41 + 80.42 +/** 80.43 + * Top-level lexical binding tests. 80.44 + * 80.45 + * @test 80.46 + * @run testng jdk.nashorn.internal.runtime.LexicalBindingTest 80.47 + */ 80.48 +@SuppressWarnings("javadoc") 80.49 +public class LexicalBindingTest { 80.50 + 80.51 + final static String LANGUAGE_ES6 = "--language=es6"; 80.52 + final static int NUMBER_OF_CONTEXTS = 20; 80.53 + final static int MEGAMORPHIC_LOOP_COUNT = 20; 80.54 + 80.55 + /** 80.56 + * Test access to global var-declared variables for shared script classes with multiple globals. 80.57 + */ 80.58 + @Test 80.59 + public static void megamorphicVarTest() throws ScriptException, InterruptedException { 80.60 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 80.61 + final ScriptEngine e = factory.getScriptEngine(); 80.62 + final ScriptContext[] contexts = new ScriptContext[NUMBER_OF_CONTEXTS]; 80.63 + final String sharedScript = "foo"; 80.64 + 80.65 + 80.66 + for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) { 80.67 + final ScriptContext context = contexts[i] = new SimpleScriptContext(); 80.68 + final Bindings b = e.createBindings(); 80.69 + context.setBindings(b, ScriptContext.ENGINE_SCOPE); 80.70 + assertEquals(e.eval("var foo = '" + i + "';", context), null); 80.71 + } 80.72 + 80.73 + for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) { 80.74 + final ScriptContext context = contexts[i]; 80.75 + assertEquals(e.eval(sharedScript, context), String.valueOf(i)); 80.76 + } 80.77 + } 80.78 + 80.79 + /** 80.80 + * Test access to global lexically declared variables for shared script classes with multiple globals. 80.81 + */ 80.82 + @Test 80.83 + public static void megamorphicMultiGlobalLetTest() throws ScriptException, InterruptedException { 80.84 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 80.85 + final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6); 80.86 + final ScriptContext[] contexts = new ScriptContext[NUMBER_OF_CONTEXTS]; 80.87 + final String sharedScript = "foo"; 80.88 + 80.89 + 80.90 + for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) { 80.91 + final ScriptContext context = contexts[i] = new SimpleScriptContext(); 80.92 + final Bindings b = e.createBindings(); 80.93 + context.setBindings(b, ScriptContext.ENGINE_SCOPE); 80.94 + assertEquals(e.eval("let foo = '" + i + "';", context), null); 80.95 + } 80.96 + 80.97 + for (int i = 0; i < NUMBER_OF_CONTEXTS; i++) { 80.98 + final ScriptContext context = contexts[i]; 80.99 + assertEquals(e.eval(sharedScript, context), String.valueOf(i)); 80.100 + } 80.101 + } 80.102 + 80.103 + 80.104 + /** 80.105 + * Test access to global lexically declared variables for shared script classes with single global. 80.106 + */ 80.107 + @Test 80.108 + public static void megamorphicSingleGlobalLetTest() throws ScriptException, InterruptedException { 80.109 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 80.110 + final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6); 80.111 + final String sharedGetterScript = "foo"; 80.112 + final String sharedSetterScript = "foo = 1"; 80.113 + 80.114 + for (int i = 0; i < MEGAMORPHIC_LOOP_COUNT; i++) { 80.115 + assertEquals(e.eval(sharedSetterScript), 1); 80.116 + assertEquals(e.eval(sharedGetterScript), 1); 80.117 + assertEquals(e.eval("delete foo; a" + i + " = 1; foo = " + i + ";"), i); 80.118 + assertEquals(e.eval(sharedGetterScript), i); 80.119 + } 80.120 + 80.121 + assertEquals(e.eval("let foo = 'foo';"), null); 80.122 + assertEquals(e.eval(sharedGetterScript), "foo"); 80.123 + assertEquals(e.eval(sharedSetterScript), 1); 80.124 + assertEquals(e.eval(sharedGetterScript), 1); 80.125 + assertEquals(e.eval("this.foo"), MEGAMORPHIC_LOOP_COUNT - 1); 80.126 + } 80.127 + 80.128 + /** 80.129 + * Test access to global lexically declared variables for shared script classes with single global. 80.130 + */ 80.131 + @Test 80.132 + public static void megamorphicInheritedGlobalLetTest() throws ScriptException, InterruptedException { 80.133 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 80.134 + final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6); 80.135 + final String sharedGetterScript = "foo"; 80.136 + final String sharedSetterScript = "foo = 1"; 80.137 + 80.138 + for (int i = 0; i < MEGAMORPHIC_LOOP_COUNT; i++) { 80.139 + assertEquals(e.eval(sharedSetterScript), 1); 80.140 + assertEquals(e.eval(sharedGetterScript), 1); 80.141 + assertEquals(e.eval("delete foo; a" + i + " = 1; Object.prototype.foo = " + i + ";"), i); 80.142 + assertEquals(e.eval(sharedGetterScript), i); 80.143 + } 80.144 + 80.145 + assertEquals(e.eval("let foo = 'foo';"), null); 80.146 + assertEquals(e.eval(sharedGetterScript), "foo"); 80.147 + assertEquals(e.eval(sharedSetterScript), 1); 80.148 + assertEquals(e.eval(sharedGetterScript), 1); 80.149 + assertEquals(e.eval("this.foo"), MEGAMORPHIC_LOOP_COUNT - 1); 80.150 + } 80.151 + 80.152 + /** 80.153 + * Test multi-threaded access to global lexically declared variables for shared script classes with multiple globals. 80.154 + */ 80.155 + @Test 80.156 + public static void multiThreadedLetTest() throws ScriptException, InterruptedException { 80.157 + final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); 80.158 + final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6); 80.159 + final Bindings b = e.createBindings(); 80.160 + final ScriptContext origContext = e.getContext(); 80.161 + final ScriptContext newCtxt = new SimpleScriptContext(); 80.162 + newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 80.163 + final String sharedScript = "foo"; 80.164 + 80.165 + assertEquals(e.eval("let foo = 'original context';", origContext), null); 80.166 + assertEquals(e.eval("let foo = 'new context';", newCtxt), null); 80.167 + 80.168 + final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 80.169 + final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 80.170 + t1.start(); 80.171 + t2.start(); 80.172 + t1.join(); 80.173 + t2.join(); 80.174 + 80.175 + assertEquals(e.eval("foo = 'newer context';", newCtxt), "newer context"); 80.176 + final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 80.177 + final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 80.178 + 80.179 + t3.start(); 80.180 + t4.start(); 80.181 + t3.join(); 80.182 + t4.join(); 80.183 + 80.184 + assertEquals(e.eval(sharedScript), "original context"); 80.185 + assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 80.186 + } 80.187 + 80.188 + private static class ScriptRunner implements Runnable { 80.189 + 80.190 + final ScriptEngine engine; 80.191 + final ScriptContext context; 80.192 + final String source; 80.193 + final Object expected; 80.194 + final int iterations; 80.195 + 80.196 + ScriptRunner(final ScriptEngine engine, final ScriptContext context, final String source, final Object expected, final int iterations) { 80.197 + this.engine = engine; 80.198 + this.context = context; 80.199 + this.source = source; 80.200 + this.expected = expected; 80.201 + this.iterations = iterations; 80.202 + } 80.203 + 80.204 + @Override 80.205 + public void run() { 80.206 + try { 80.207 + for (int i = 0; i < iterations; i++) { 80.208 + assertEquals(engine.eval(source, context), expected); 80.209 + } 80.210 + } catch (final ScriptException se) { 80.211 + throw new RuntimeException(se); 80.212 + } 80.213 + } 80.214 + } 80.215 +}
81.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 81.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/NoPersistenceCachingTest.java Mon Apr 06 16:18:54 2015 +0530 81.3 @@ -0,0 +1,140 @@ 81.4 +/* 81.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 81.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 81.7 + * 81.8 + * This code is free software; you can redistribute it and/or modify it 81.9 + * under the terms of the GNU General Public License version 2 only, as 81.10 + * published by the Free Software Foundation. Oracle designates this 81.11 + * particular file as subject to the "Classpath" exception as provided 81.12 + * by Oracle in the LICENSE file that accompanied this code. 81.13 + * 81.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 81.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 81.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 81.17 + * version 2 for more details (a copy is included in the LICENSE file that 81.18 + * accompanied this code). 81.19 + * 81.20 + * You should have received a copy of the GNU General Public License version 81.21 + * 2 along with this work; if not, write to the Free Software Foundation, 81.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 81.23 + * 81.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 81.25 + * or visit www.oracle.com if you need additional information or have any 81.26 + * questions. 81.27 + */ 81.28 +package jdk.nashorn.internal.runtime.test; 81.29 + 81.30 +import static org.testng.Assert.fail; 81.31 +import java.io.ByteArrayOutputStream; 81.32 +import java.io.PrintStream; 81.33 +import java.util.regex.Matcher; 81.34 +import java.util.regex.Pattern; 81.35 +import javax.script.ScriptContext; 81.36 +import javax.script.ScriptEngine; 81.37 +import javax.script.ScriptEngineFactory; 81.38 +import javax.script.ScriptEngineManager; 81.39 +import javax.script.SimpleScriptContext; 81.40 +import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 81.41 +import org.testng.annotations.AfterTest; 81.42 +import org.testng.annotations.BeforeTest; 81.43 +import org.testng.annotations.Test; 81.44 + 81.45 +/** 81.46 + * @test 81.47 + * @bug 8037378 81.48 + * @summary Sanity tests for no persistence caching 81.49 + * @run testng/othervm jdk.nashorn.internal.runtime.NoPersistenceCachingTest 81.50 + */ 81.51 +@SuppressWarnings("javadoc") 81.52 +public class NoPersistenceCachingTest { 81.53 + 81.54 + private ScriptEngine engine; 81.55 + private ScriptContext context1, context2, context3; 81.56 + private ByteArrayOutputStream stderr; 81.57 + private PrintStream prevStderr; 81.58 + 81.59 + @BeforeTest 81.60 + public void setupTest() { 81.61 + stderr = new ByteArrayOutputStream(); 81.62 + prevStderr = System.err; 81.63 + System.setErr(new PrintStream(stderr)); 81.64 + NashornScriptEngineFactory nashornFactory = null; 81.65 + final ScriptEngineManager sm = new ScriptEngineManager(); 81.66 + for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 81.67 + if (fac instanceof NashornScriptEngineFactory) { 81.68 + nashornFactory = (NashornScriptEngineFactory) fac; 81.69 + break; 81.70 + } 81.71 + } 81.72 + if (nashornFactory == null) { 81.73 + fail("Cannot find nashorn factory!"); 81.74 + } 81.75 + // fine is enough for cache hits, finest produces way too much information 81.76 + // TODO this should be ported to use the RuntimeEvents instead of screen scraping 81.77 + // logs, as obviously this is very brittle 81.78 + final String[] options = new String[]{"--log=compiler:fine"}; 81.79 + engine = nashornFactory.getScriptEngine(options); 81.80 + context1 = engine.getContext(); 81.81 + context2 = new SimpleScriptContext(); 81.82 + context2.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); 81.83 + context3 = new SimpleScriptContext(); 81.84 + context3.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); 81.85 + } 81.86 + 81.87 + @AfterTest 81.88 + public void setErrTest() { 81.89 + System.setErr(prevStderr); 81.90 + } 81.91 + 81.92 + public void runTest(final int numberOfContext, final String expectedOutputPattern, 81.93 + final int expectedPatternOccurrence) { 81.94 + 81.95 + try { 81.96 + switch (numberOfContext) { 81.97 + case 2: 81.98 + final String scriptTwoContexts = "print('HelloTwoContexts')"; 81.99 + engine.eval(scriptTwoContexts, context1); 81.100 + engine.eval(scriptTwoContexts, context2); 81.101 + break; 81.102 + case 3: 81.103 + final String scriptThreeContexts = "print('HelloThreeContexts')"; 81.104 + engine.eval(scriptThreeContexts, context1); 81.105 + engine.eval(scriptThreeContexts, context2); 81.106 + engine.eval(scriptThreeContexts, context3); 81.107 + break; 81.108 + default: 81.109 + break; 81.110 + } 81.111 + } catch (final Exception se) { 81.112 + se.printStackTrace(); 81.113 + fail(se.getMessage()); 81.114 + } 81.115 + final Pattern deoptimizing = Pattern.compile(expectedOutputPattern); 81.116 + final Matcher matcher = deoptimizing.matcher(stderr.toString()); 81.117 + int matches = 0; 81.118 + while (matcher.find()) { 81.119 + matches++; 81.120 + } 81.121 + if (matches != expectedPatternOccurrence) { 81.122 + fail("Number of cache hit is not correct, expected: " 81.123 + + expectedPatternOccurrence + " and found: " + matches + "\n" 81.124 + + stderr); 81.125 + } 81.126 + stderr.reset(); 81.127 + } 81.128 + 81.129 + private static String getCodeCachePattern() { 81.130 + return ("\\[compiler\\]\\sCode\\scache\\shit\\sfor\\s<eval>\\savoiding\\srecompile."); 81.131 + } 81.132 + 81.133 + @Test 81.134 + public void twoContextTest() { 81.135 + runTest(2, getCodeCachePattern(), 1); 81.136 + 81.137 + } 81.138 + 81.139 + @Test 81.140 + public void threeContextTest() { 81.141 + runTest(3, getCodeCachePattern(), 2); 81.142 + } 81.143 +}
82.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 82.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/SourceTest.java Mon Apr 06 16:18:54 2015 +0530 82.3 @@ -0,0 +1,125 @@ 82.4 +/* 82.5 + * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved. 82.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 82.7 + * 82.8 + * This code is free software; you can redistribute it and/or modify it 82.9 + * under the terms of the GNU General Public License version 2 only, as 82.10 + * published by the Free Software Foundation. Oracle designates this 82.11 + * particular file as subject to the "Classpath" exception as provided 82.12 + * by Oracle in the LICENSE file that accompanied this code. 82.13 + * 82.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 82.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 82.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 82.17 + * version 2 for more details (a copy is included in the LICENSE file that 82.18 + * accompanied this code). 82.19 + * 82.20 + * You should have received a copy of the GNU General Public License version 82.21 + * 2 along with this work; if not, write to the Free Software Foundation, 82.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 82.23 + * 82.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 82.25 + * or visit www.oracle.com if you need additional information or have any 82.26 + * questions. 82.27 + */ 82.28 + 82.29 +package jdk.nashorn.internal.runtime.test; 82.30 + 82.31 +import static jdk.nashorn.internal.runtime.Source.sourceFor; 82.32 +import static org.testng.Assert.assertEquals; 82.33 +import static org.testng.Assert.assertTrue; 82.34 +import static org.testng.Assert.fail; 82.35 +import java.io.File; 82.36 +import java.io.IOException; 82.37 +import java.io.InputStreamReader; 82.38 +import java.io.Reader; 82.39 +import java.net.URL; 82.40 +import java.util.Arrays; 82.41 +import jdk.nashorn.api.scripting.URLReader; 82.42 +import jdk.nashorn.internal.runtime.Source; 82.43 +import org.testng.annotations.Test; 82.44 + 82.45 +/** 82.46 + * Tests different Source representations. 82.47 + */ 82.48 +@SuppressWarnings("javadoc") 82.49 +public class SourceTest { 82.50 + 82.51 + final private static String SOURCE_NAME = "source.js"; 82.52 + final private static String SOURCE_STRING = "var x = 1;"; 82.53 + final private static char[] SOURCE_CHARS = SOURCE_STRING.toCharArray(); 82.54 + final private static String RESOURCE_PATH = "resources/load_test.js"; 82.55 + final private static File SOURCE_FILE = new File("build/test/classes/jdk/nashorn/internal/runtime/" + RESOURCE_PATH); 82.56 + final private static URL SOURCE_URL = SourceTest.class.getResource(RESOURCE_PATH); 82.57 + 82.58 + 82.59 + @Test 82.60 + public void testStringSource() { 82.61 + testSources(sourceFor(SOURCE_NAME, SOURCE_STRING), sourceFor(SOURCE_NAME, SOURCE_STRING)); 82.62 + testSources(sourceFor(SOURCE_NAME, SOURCE_STRING), sourceFor(SOURCE_NAME, SOURCE_CHARS)); 82.63 + } 82.64 + 82.65 + @Test 82.66 + public void testCharArraySource() { 82.67 + testSources(sourceFor(SOURCE_NAME, SOURCE_CHARS), sourceFor(SOURCE_NAME, SOURCE_CHARS)); 82.68 + testSources(sourceFor(SOURCE_NAME, SOURCE_CHARS), sourceFor(SOURCE_NAME, SOURCE_STRING)); 82.69 + } 82.70 + 82.71 + @Test 82.72 + public void testURLSource() { 82.73 + try { 82.74 + testSources(sourceFor(SOURCE_NAME, SOURCE_URL), sourceFor(SOURCE_NAME, SOURCE_URL)); 82.75 + testSources(sourceFor(SOURCE_NAME, SOURCE_URL), sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL))); 82.76 + 82.77 + } catch (final IOException e) { 82.78 + fail(e.toString()); 82.79 + } 82.80 + } 82.81 + 82.82 + @Test 82.83 + public void testURLReaderSource() { 82.84 + try { 82.85 + System.err.println(SourceTest.class.getResource("")); 82.86 + testSources(sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL)), sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL))); 82.87 + testSources(sourceFor(SOURCE_NAME, new URLReader(SOURCE_URL)), sourceFor(SOURCE_NAME, SOURCE_URL)); 82.88 + } catch (final IOException e) { 82.89 + fail(e.toString()); 82.90 + } 82.91 + } 82.92 + 82.93 + @Test 82.94 + public void testReaderSource() { 82.95 + try { 82.96 + testSources(sourceFor(SOURCE_NAME, getReader(RESOURCE_PATH)), sourceFor(SOURCE_NAME, getReader(RESOURCE_PATH))); 82.97 + } catch (final IOException e) { 82.98 + fail(e.toString()); 82.99 + } 82.100 + } 82.101 + 82.102 + @Test 82.103 + public void testFileSource() { 82.104 + try { 82.105 + testSources(sourceFor(SOURCE_NAME, SOURCE_FILE), sourceFor(SOURCE_NAME, SOURCE_FILE)); 82.106 + } catch (final IOException e) { 82.107 + fail(e.toString()); 82.108 + } 82.109 + } 82.110 + 82.111 + private static Reader getReader(final String path) { 82.112 + return new InputStreamReader(SourceTest.class.getResourceAsStream(path)); 82.113 + } 82.114 + 82.115 + private static void testSources(final Source source1, final Source source2) { 82.116 + final char[] chars1 = source1.getContent(); 82.117 + final char[] chars2 = source2.getContent(); 82.118 + final String str1 = source1.getString(); 82.119 + final String str2 = source2.getString(); 82.120 + assertTrue(Arrays.equals(chars1, chars2)); 82.121 + assertEquals(str1, str2); 82.122 + assertEquals(source1.hashCode(), source2.hashCode()); 82.123 + assertTrue(source1.equals(source2)); 82.124 + assertTrue(Arrays.equals(source1.getContent(), str1.toCharArray())); 82.125 + assertTrue(Arrays.equals(source1.getContent(), chars1)); 82.126 + assertTrue(Arrays.equals(source1.getContent(), source2.getContent())); 82.127 + } 82.128 +}
83.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 83.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/TrustedScriptEngineTest.java Mon Apr 06 16:18:54 2015 +0530 83.3 @@ -0,0 +1,363 @@ 83.4 +/* 83.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 83.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 83.7 + * 83.8 + * This code is free software; you can redistribute it and/or modify it 83.9 + * under the terms of the GNU General Public License version 2 only, as 83.10 + * published by the Free Software Foundation. Oracle designates this 83.11 + * particular file as subject to the "Classpath" exception as provided 83.12 + * by Oracle in the LICENSE file that accompanied this code. 83.13 + * 83.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 83.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 83.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 83.17 + * version 2 for more details (a copy is included in the LICENSE file that 83.18 + * accompanied this code). 83.19 + * 83.20 + * You should have received a copy of the GNU General Public License version 83.21 + * 2 along with this work; if not, write to the Free Software Foundation, 83.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 83.23 + * 83.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 83.25 + * or visit www.oracle.com if you need additional information or have any 83.26 + * questions. 83.27 + */ 83.28 + 83.29 +package jdk.nashorn.internal.runtime.test; 83.30 + 83.31 +import static org.testng.Assert.assertEquals; 83.32 +import static org.testng.Assert.assertTrue; 83.33 +import static org.testng.Assert.fail; 83.34 +import javax.script.ScriptContext; 83.35 +import javax.script.ScriptEngine; 83.36 +import javax.script.ScriptEngineFactory; 83.37 +import javax.script.ScriptEngineManager; 83.38 +import javax.script.ScriptException; 83.39 +import javax.script.SimpleScriptContext; 83.40 +import jdk.nashorn.api.scripting.ClassFilter; 83.41 +import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 83.42 +import jdk.nashorn.internal.runtime.Version; 83.43 +import org.testng.annotations.Test; 83.44 + 83.45 +/** 83.46 + * Tests for trusted client usage of nashorn script engine factory extension API 83.47 + */ 83.48 +@SuppressWarnings("javadoc") 83.49 +public class TrustedScriptEngineTest { 83.50 + @Test 83.51 + public void versionTest() { 83.52 + final ScriptEngineManager m = new ScriptEngineManager(); 83.53 + final ScriptEngine e = m.getEngineByName("nashorn"); 83.54 + assertEquals(e.getFactory().getEngineVersion(), Version.version()); 83.55 + } 83.56 + 83.57 + private static class MyClassLoader extends ClassLoader { 83.58 + // to check if script engine uses the specified class loader 83.59 + private final boolean[] reached = new boolean[1]; 83.60 + 83.61 + @Override 83.62 + protected Class<?> findClass(final String name) throws ClassNotFoundException { 83.63 + // flag that it reached here 83.64 + reached[0] = true; 83.65 + return super.findClass(name); 83.66 + } 83.67 + 83.68 + public boolean reached() { 83.69 + return reached[0]; 83.70 + } 83.71 + } 83.72 + 83.73 + // These are for "private" extension API of NashornScriptEngineFactory that 83.74 + // accepts a ClassLoader and/or command line options. 83.75 + 83.76 + @Test 83.77 + public void factoryClassLoaderTest() { 83.78 + final ScriptEngineManager sm = new ScriptEngineManager(); 83.79 + for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 83.80 + if (fac instanceof NashornScriptEngineFactory) { 83.81 + final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 83.82 + final MyClassLoader loader = new MyClassLoader(); 83.83 + // set the classloader as app class loader 83.84 + final ScriptEngine e = nfac.getScriptEngine(loader); 83.85 + try { 83.86 + e.eval("Packages.foo"); 83.87 + // check that the class loader was attempted 83.88 + assertTrue(loader.reached(), "did not reach class loader!"); 83.89 + } catch (final ScriptException se) { 83.90 + se.printStackTrace(); 83.91 + fail(se.getMessage()); 83.92 + } 83.93 + return; 83.94 + } 83.95 + } 83.96 + 83.97 + fail("Cannot find nashorn factory!"); 83.98 + } 83.99 + 83.100 + @Test 83.101 + public void factoryClassLoaderAndOptionsTest() { 83.102 + final ScriptEngineManager sm = new ScriptEngineManager(); 83.103 + for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 83.104 + if (fac instanceof NashornScriptEngineFactory) { 83.105 + final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 83.106 + final String[] options = new String[] { "-strict" }; 83.107 + final MyClassLoader loader = new MyClassLoader(); 83.108 + // set the classloader as app class loader 83.109 + final ScriptEngine e = nfac.getScriptEngine(options, loader); 83.110 + try { 83.111 + e.eval("Packages.foo"); 83.112 + // check that the class loader was attempted 83.113 + assertTrue(loader.reached(), "did not reach class loader!"); 83.114 + } catch (final ScriptException se) { 83.115 + se.printStackTrace(); 83.116 + fail(se.getMessage()); 83.117 + } 83.118 + 83.119 + try { 83.120 + // strict mode - delete of a var should throw SyntaxError 83.121 + e.eval("var d = 2; delete d;"); 83.122 + } catch (final ScriptException se) { 83.123 + // check that the error message contains "SyntaxError" 83.124 + assertTrue(se.getMessage().contains("SyntaxError")); 83.125 + } 83.126 + 83.127 + return; 83.128 + } 83.129 + } 83.130 + 83.131 + fail("Cannot find nashorn factory!"); 83.132 + } 83.133 + 83.134 + @Test 83.135 + public void factoryOptionsTest() { 83.136 + final ScriptEngineManager sm = new ScriptEngineManager(); 83.137 + for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 83.138 + if (fac instanceof NashornScriptEngineFactory) { 83.139 + final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 83.140 + // specify --no-syntax-extensions flag 83.141 + final String[] options = new String[] { "--no-syntax-extensions" }; 83.142 + final ScriptEngine e = nfac.getScriptEngine(options); 83.143 + try { 83.144 + // try nashorn specific extension 83.145 + e.eval("var f = funtion(x) 2*x;"); 83.146 + fail("should have thrown exception!"); 83.147 + } catch (final Exception ex) { 83.148 + //empty 83.149 + } 83.150 + return; 83.151 + } 83.152 + } 83.153 + 83.154 + fail("Cannot find nashorn factory!"); 83.155 + } 83.156 + 83.157 + @Test 83.158 + /** 83.159 + * Test repeated evals with --loader-per-compile=false 83.160 + * We used to get "class redefinition error". 83.161 + */ 83.162 + public void noLoaderPerCompilerTest() { 83.163 + final ScriptEngineManager sm = new ScriptEngineManager(); 83.164 + for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 83.165 + if (fac instanceof NashornScriptEngineFactory) { 83.166 + final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 83.167 + final String[] options = new String[] { "--loader-per-compile=false" }; 83.168 + final ScriptEngine e = nfac.getScriptEngine(options); 83.169 + try { 83.170 + e.eval("2 + 3"); 83.171 + e.eval("4 + 4"); 83.172 + } catch (final ScriptException se) { 83.173 + se.printStackTrace(); 83.174 + fail(se.getMessage()); 83.175 + } 83.176 + return; 83.177 + } 83.178 + } 83.179 + fail("Cannot find nashorn factory!"); 83.180 + } 83.181 + 83.182 + @Test 83.183 + /** 83.184 + * Test that we can use same script name in repeated evals with --loader-per-compile=false 83.185 + * We used to get "class redefinition error" as name was derived from script name. 83.186 + */ 83.187 + public void noLoaderPerCompilerWithSameNameTest() { 83.188 + final ScriptEngineManager sm = new ScriptEngineManager(); 83.189 + for (final ScriptEngineFactory fac : sm.getEngineFactories()) { 83.190 + if (fac instanceof NashornScriptEngineFactory) { 83.191 + final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac; 83.192 + final String[] options = new String[] { "--loader-per-compile=false" }; 83.193 + final ScriptEngine e = nfac.getScriptEngine(options); 83.194 + e.put(ScriptEngine.FILENAME, "test.js"); 83.195 + try { 83.196 + e.eval("2 + 3"); 83.197 + e.eval("4 + 4"); 83.198 + } catch (final ScriptException se) { 83.199 + se.printStackTrace(); 83.200 + fail(se.getMessage()); 83.201 + } 83.202 + return; 83.203 + } 83.204 + } 83.205 + fail("Cannot find nashorn factory!"); 83.206 + } 83.207 + 83.208 + @Test 83.209 + public void globalPerEngineTest() throws ScriptException { 83.210 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.211 + final String[] options = new String[] { "--global-per-engine" }; 83.212 + final ScriptEngine e = fac.getScriptEngine(options); 83.213 + 83.214 + e.eval("function foo() {}"); 83.215 + 83.216 + final ScriptContext newCtx = new SimpleScriptContext(); 83.217 + newCtx.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); 83.218 + 83.219 + // all global definitions shared and so 'foo' should be 83.220 + // visible in new Bindings as well. 83.221 + assertTrue(e.eval("typeof foo", newCtx).equals("function")); 83.222 + 83.223 + e.eval("function bar() {}", newCtx); 83.224 + 83.225 + // bar should be visible in default context 83.226 + assertTrue(e.eval("typeof bar").equals("function")); 83.227 + } 83.228 + 83.229 + @Test 83.230 + public void classFilterTest() throws ScriptException { 83.231 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.232 + final ScriptEngine e = fac.getScriptEngine(new ClassFilter() { 83.233 + @Override 83.234 + public boolean exposeToScripts(final String fullName) { 83.235 + // don't allow anything that is not "java." 83.236 + return fullName.startsWith("java."); 83.237 + } 83.238 + }); 83.239 + 83.240 + assertEquals(e.eval("typeof javax.script.ScriptEngine"), "object"); 83.241 + assertEquals(e.eval("typeof java.util.Vector"), "function"); 83.242 + 83.243 + try { 83.244 + e.eval("Java.type('javax.script.ScriptContext')"); 83.245 + fail("should not reach here"); 83.246 + } catch (final ScriptException | RuntimeException se) { 83.247 + if (! (se.getCause() instanceof ClassNotFoundException)) { 83.248 + fail("ClassNotFoundException expected"); 83.249 + } 83.250 + } 83.251 + } 83.252 + 83.253 + @Test 83.254 + public void classFilterTest2() throws ScriptException { 83.255 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.256 + final ScriptEngine e = fac.getScriptEngine(new String[0], Thread.currentThread().getContextClassLoader(), 83.257 + new ClassFilter() { 83.258 + @Override 83.259 + public boolean exposeToScripts(final String fullName) { 83.260 + // don't allow anything that is not "java." 83.261 + return fullName.startsWith("java."); 83.262 + } 83.263 + }); 83.264 + 83.265 + assertEquals(e.eval("typeof javax.script.ScriptEngine"), "object"); 83.266 + assertEquals(e.eval("typeof java.util.Vector"), "function"); 83.267 + 83.268 + try { 83.269 + e.eval("Java.type('javax.script.ScriptContext')"); 83.270 + fail("should not reach here"); 83.271 + } catch (final ScriptException | RuntimeException se) { 83.272 + if (! (se.getCause() instanceof ClassNotFoundException)) { 83.273 + fail("ClassNotFoundException expected"); 83.274 + } 83.275 + } 83.276 + } 83.277 + 83.278 + @Test 83.279 + public void nullClassFilterTest() { 83.280 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.281 + try { 83.282 + fac.getScriptEngine((ClassFilter)null); 83.283 + fail("should have thrown NPE"); 83.284 + } catch (final NullPointerException e) { 83.285 + //empty 83.286 + } 83.287 + } 83.288 + 83.289 + @Test 83.290 + public void nullClassFilterTest2() { 83.291 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.292 + try { 83.293 + fac.getScriptEngine(new String[0], null, null); 83.294 + fail("should have thrown NPE"); 83.295 + } catch (final NullPointerException e) { 83.296 + //empty 83.297 + } 83.298 + } 83.299 + 83.300 + @Test 83.301 + public void nullArgsTest() { 83.302 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.303 + try { 83.304 + fac.getScriptEngine((String[])null); 83.305 + fail("should have thrown NPE"); 83.306 + } catch (final NullPointerException e) { 83.307 + //empty 83.308 + } 83.309 + } 83.310 + 83.311 + @Test 83.312 + public void nullArgsTest2() { 83.313 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.314 + try { 83.315 + fac.getScriptEngine(null, null, new ClassFilter() { 83.316 + @Override 83.317 + public boolean exposeToScripts(final String name) { 83.318 + return true; 83.319 + } 83.320 + }); 83.321 + fail("should have thrown NPE"); 83.322 + } catch (final NullPointerException e) { 83.323 + //empty 83.324 + } 83.325 + } 83.326 + 83.327 + @Test 83.328 + public void nashornSwallowsConstKeyword() throws Exception { 83.329 + final NashornScriptEngineFactory f = new NashornScriptEngineFactory(); 83.330 + final String[] args = new String[] { "--const-as-var" }; 83.331 + final ScriptEngine engine = f.getScriptEngine(args); 83.332 + 83.333 + final Object ret = engine.eval("" 83.334 + + "(function() {\n" 83.335 + + " const x = 10;\n" 83.336 + + " return x;\n" 83.337 + + "})();" 83.338 + ); 83.339 + assertEquals(ret, 10, "Parsed and executed OK"); 83.340 + } 83.341 + 83.342 + @Test 83.343 + public void evalDefaultFileNameTest() throws ScriptException { 83.344 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.345 + final ScriptEngine engine = fac.getScriptEngine(new String[] { "--verify-code=true" }); 83.346 + // default FILENAME being "<eval>" make sure generated code bytecode verifies. 83.347 + engine.eval("var a = 3;"); 83.348 + } 83.349 + 83.350 + @Test 83.351 + public void evalFileNameWithSpecialCharsTest() throws ScriptException { 83.352 + final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 83.353 + final ScriptEngine engine = fac.getScriptEngine(new String[] { "--verify-code=true" }); 83.354 + final ScriptContext ctxt = new SimpleScriptContext(); 83.355 + // use file name with "dangerous" chars. 83.356 + ctxt.setAttribute(ScriptEngine.FILENAME, "<myscript>", ScriptContext.ENGINE_SCOPE); 83.357 + engine.eval("var a = 3;"); 83.358 + ctxt.setAttribute(ScriptEngine.FILENAME, "[myscript]", ScriptContext.ENGINE_SCOPE); 83.359 + engine.eval("var h = 'hello';"); 83.360 + ctxt.setAttribute(ScriptEngine.FILENAME, ";/\\$.", ScriptContext.ENGINE_SCOPE); 83.361 + engine.eval("var foo = 'world';"); 83.362 + // name used by jjs shell tool for the interactive mode 83.363 + ctxt.setAttribute(ScriptEngine.FILENAME, "<shell>", ScriptContext.ENGINE_SCOPE); 83.364 + engine.eval("var foo = 'world';"); 83.365 + } 83.366 +}
84.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 84.2 +++ b/test/src/jdk/nashorn/internal/runtime/test/resources/load_test.js Mon Apr 06 16:18:54 2015 +0530 84.3 @@ -0,0 +1,28 @@ 84.4 +/* 84.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 84.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 84.7 + * 84.8 + * This code is free software; you can redistribute it and/or modify it 84.9 + * under the terms of the GNU General Public License version 2 only, as 84.10 + * published by the Free Software Foundation. Oracle designates this 84.11 + * particular file as subject to the "Classpath" exception as provided 84.12 + * by Oracle in the LICENSE file that accompanied this code. 84.13 + * 84.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 84.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 84.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 84.17 + * version 2 for more details (a copy is included in the LICENSE file that 84.18 + * accompanied this code). 84.19 + * 84.20 + * You should have received a copy of the GNU General Public License version 84.21 + * 2 along with this work; if not, write to the Free Software Foundation, 84.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 84.23 + * 84.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 84.25 + * or visit www.oracle.com if you need additional information or have any 84.26 + * questions. 84.27 + */ 84.28 + 84.29 +function loadedFunc(arg) { 84.30 + return arg.toUpperCase(); 84.31 +}