Mercurial > people > rkennke > jdk9-shenandoah-final > nashorn
changeset 1241:ea4e794c3927 jdk9-b59
Merge
author | lana |
---|---|
date | Wed, 08 Apr 2015 14:31:51 -0700 |
parents | 60dd42770ea8 (current diff) 862630898880 (diff) |
children | 67b3d03b7213 |
files | make/Makefile test/src/jdk/internal/dynalink/beans/CallerSensitiveTest.java test/src/jdk/nashorn/api/NashornSQLDriver.java test/src/jdk/nashorn/api/javaaccess/ArrayConversionTest.java test/src/jdk/nashorn/api/javaaccess/BooleanAccessTest.java test/src/jdk/nashorn/api/javaaccess/ConsStringTest.java test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java test/src/jdk/nashorn/api/javaaccess/NumberAccessTest.java test/src/jdk/nashorn/api/javaaccess/NumberBoxingTest.java test/src/jdk/nashorn/api/javaaccess/ObjectAccessTest.java test/src/jdk/nashorn/api/javaaccess/Person.java test/src/jdk/nashorn/api/javaaccess/SharedObject.java test/src/jdk/nashorn/api/javaaccess/StringAccessTest.java test/src/jdk/nashorn/api/scripting/InvocableTest.java test/src/jdk/nashorn/api/scripting/MultipleEngineTest.java test/src/jdk/nashorn/api/scripting/PluggableJSObjectTest.java test/src/jdk/nashorn/api/scripting/ScopeTest.java test/src/jdk/nashorn/api/scripting/ScriptEngineSecurityTest.java test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java test/src/jdk/nashorn/api/scripting/ScriptObjectMirrorTest.java test/src/jdk/nashorn/api/scripting/VariableArityTestInterface.java test/src/jdk/nashorn/api/scripting/Window.java test/src/jdk/nashorn/api/scripting/WindowEventHandler.java test/src/jdk/nashorn/api/scripting/resources/func.js test/src/jdk/nashorn/api/scripting/resources/gettersetter.js test/src/jdk/nashorn/api/scripting/resources/witheval.js test/src/jdk/nashorn/api/tree/ParseAPITest.java test/src/jdk/nashorn/internal/codegen/CompilerTest.java test/src/jdk/nashorn/internal/parser/ParserTest.java test/src/jdk/nashorn/internal/runtime/ClassFilterTest.java test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java test/src/jdk/nashorn/internal/runtime/ConsStringTest.java test/src/jdk/nashorn/internal/runtime/ContextTest.java test/src/jdk/nashorn/internal/runtime/ExceptionsNotSerializable.java test/src/jdk/nashorn/internal/runtime/JSTypeTest.java test/src/jdk/nashorn/internal/runtime/LexicalBindingTest.java test/src/jdk/nashorn/internal/runtime/NoPersistenceCachingTest.java test/src/jdk/nashorn/internal/runtime/SourceTest.java test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java test/src/jdk/nashorn/internal/runtime/regexp/JdkRegExpTest.java test/src/jdk/nashorn/internal/runtime/regexp/joni/JoniTest.java test/src/jdk/nashorn/internal/runtime/resources/load_test.js |
diffstat | 106 files changed, 9303 insertions(+), 9399 deletions(-) [+] |
line wrap: on
line diff
--- a/make/Makefile Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -# -# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. -# -# This code is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 only, as -# published by the Free Software Foundation. Oracle designates this -# particular file as subject to the "Classpath" exception as provided -# by Oracle in the LICENSE file that accompanied this code. -# -# This code is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -# version 2 for more details (a copy is included in the LICENSE file that -# accompanied this code). -# -# You should have received a copy of the GNU General Public License version -# 2 along with this work; if not, write to the Free Software Foundation, -# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. -# -# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA -# or visit www.oracle.com if you need additional information or have any -# questions. -# - -# Locate this Makefile -ifeq ($(filter /%, $(lastword $(MAKEFILE_LIST))), ) - makefile_path := $(CURDIR)/$(lastword $(MAKEFILE_LIST)) -else - makefile_path := $(lastword $(MAKEFILE_LIST)) -endif -repo_dir := $(patsubst %/make/Makefile, %, $(makefile_path)) - -# What is the name of this subsystem (langtools, corba, etc)? -subsystem_name := $(notdir $(repo_dir)) - -# Try to locate top-level makefile -top_level_makefile := $(repo_dir)/../Makefile -ifneq ($(wildcard $(top_level_makefile)), ) - $(info Will run $(subsystem_name) target on top-level Makefile) - $(info WARNING: This is a non-recommended way of building!) - $(info ===================================================) -else - $(info Cannot locate top-level Makefile. Is this repo not checked out as part of a complete forest?) - $(error Build from top-level Makefile instead) -endif - -all: - @$(MAKE) -f $(top_level_makefile) $(subsystem_name)
--- a/make/build.xml Wed Apr 08 13:12:53 2015 -0700 +++ b/make/build.xml Wed Apr 08 14:31:51 2015 -0700 @@ -281,12 +281,12 @@ <fileset dir="${test.src.dir}/META-INF/services/"/> </copy> - <copy todir="${build.test.classes.dir}/jdk/nashorn/internal/runtime/resources"> - <fileset dir="${test.src.dir}/jdk/nashorn/internal/runtime/resources"/> + <copy todir="${build.test.classes.dir}/jdk/nashorn/internal/runtime/test/resources"> + <fileset dir="${test.src.dir}/jdk/nashorn/internal/runtime/test/resources"/> </copy> - <copy todir="${build.test.classes.dir}/jdk/nashorn/api/scripting/resources"> - <fileset dir="${test.src.dir}/jdk/nashorn/api/scripting/resources"/> + <copy todir="${build.test.classes.dir}/jdk/nashorn/api/scripting/test/resources"> + <fileset dir="${test.src.dir}/jdk/nashorn/api/scripting/test/resources"/> </copy> <!-- tests that check nashorn internals and internal API -->
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java Wed Apr 08 14:31:51 2015 -0700 @@ -60,6 +60,7 @@ import jdk.nashorn.internal.objects.annotations.Setter; import jdk.nashorn.internal.runtime.Context; import jdk.nashorn.internal.runtime.ECMAErrors; +import jdk.nashorn.internal.runtime.FindProperty; import jdk.nashorn.internal.runtime.GlobalConstants; import jdk.nashorn.internal.runtime.GlobalFunctions; import jdk.nashorn.internal.runtime.JSType; @@ -2204,6 +2205,17 @@ } @Override + protected FindProperty findProperty(final String key, final boolean deep, final ScriptObject start) { + if (lexicalScope != null && start != this && start.isScope()) { + final FindProperty find = lexicalScope.findProperty(key, false); + if (find != null) { + return find; + } + } + return super.findProperty(key, deep, start); + } + + @Override public GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) { final boolean isScope = NashornCallSiteDescriptor.isScope(desc);
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat32Array.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat32Array.java Wed Apr 08 14:31:51 2015 -0700 @@ -114,12 +114,11 @@ private void setElem(final int index, final double elem) { try { - nb.put(index, (float)elem); + if (index < nb.limit()) { + nb.put(index, (float) elem); + } } catch (final IndexOutOfBoundsException e) { - //swallow valid array indexes. it's ok. - if (index < 0) { - throw new ClassCastException(); - } + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat64Array.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFloat64Array.java Wed Apr 08 14:31:51 2015 -0700 @@ -114,12 +114,11 @@ private void setElem(final int index, final double elem) { try { - nb.put(index, elem); + if (index < nb.limit()) { + nb.put(index, elem); + } } catch (final IndexOutOfBoundsException e) { - //swallow valid array indexes. it's ok. - if (index < 0) { - throw new ClassCastException(); - } + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt16Array.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt16Array.java Wed Apr 08 14:31:51 2015 -0700 @@ -115,12 +115,11 @@ private void setElem(final int index, final int elem) { try { - nb.put(index, (short)elem); + if (index < nb.limit()) { + nb.put(index, (short) elem); + } } catch (final IndexOutOfBoundsException e) { - //swallow valid array indexes. it's ok. - if (index < 0) { - throw new ClassCastException(); - } + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt32Array.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt32Array.java Wed Apr 08 14:31:51 2015 -0700 @@ -104,11 +104,11 @@ private void setElem(final int index, final int elem) { try { - nb.put(index, elem); - } catch (final IndexOutOfBoundsException e) { - if (index < 0) { - throw new ClassCastException(); - } + if (index < nb.limit()) { + nb.put(index, elem); + } + } catch (final IndexOutOfBoundsException e) { + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt8Array.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeInt8Array.java Wed Apr 08 14:31:51 2015 -0700 @@ -113,12 +113,11 @@ private void setElem(final int index, final int elem) { try { - nb.put(index, (byte)elem); + if (index < nb.limit()) { + nb.put(index, (byte) elem); + } } catch (final IndexOutOfBoundsException e) { - //swallow valid array indexes. it's ok. - if (index < 0) { - throw new ClassCastException(); - } + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint16Array.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint16Array.java Wed Apr 08 14:31:51 2015 -0700 @@ -104,12 +104,11 @@ private void setElem(final int index, final int elem) { try { - nb.put(index, (char)elem); + if (index < nb.limit()) { + nb.put(index, (char) elem); + } } catch (final IndexOutOfBoundsException e) { - //swallow valid array indexes. it's ok. - if (index < 0) { - throw new ClassCastException(); - } + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint32Array.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint32Array.java Wed Apr 08 14:31:51 2015 -0700 @@ -113,12 +113,11 @@ private void setElem(final int index, final int elem) { try { - nb.put(index, elem); + if (index < nb.limit()) { + nb.put(index, elem); + } } catch (final IndexOutOfBoundsException e) { - //swallow valid array indexes. it's ok. - if (index < 0) { - throw new ClassCastException(); - } + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8Array.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8Array.java Wed Apr 08 14:31:51 2015 -0700 @@ -104,12 +104,11 @@ private void setElem(final int index, final int elem) { try { - nb.put(index, (byte)elem); + if (index < nb.limit()) { + nb.put(index, (byte) elem); + } } catch (final IndexOutOfBoundsException e) { - //swallow valid array indexes. it's ok. - if (index < 0) { - throw new ClassCastException(); - } + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java Wed Apr 08 14:31:51 2015 -0700 @@ -133,18 +133,17 @@ private void setElem(final int index, final int elem) { try { - final byte clamped; - if ((elem & 0xffff_ff00) == 0) { - clamped = (byte)elem; - } else { - clamped = elem < 0 ? 0 : (byte)0xff; + if (index < nb.limit()) { + final byte clamped; + if ((elem & 0xffff_ff00) == 0) { + clamped = (byte) elem; + } else { + clamped = elem < 0 ? 0 : (byte) 0xff; + } + nb.put(index, clamped); } - nb.put(index, clamped); } catch (final IndexOutOfBoundsException e) { - //swallow valid array indexes. it's ok. - if (index < 0) { - throw new ClassCastException(); - } + throw new ClassCastException(); } }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java Wed Apr 08 14:31:51 2015 -0700 @@ -812,7 +812,7 @@ * * @return FindPropertyData or null if not found. */ - FindProperty findProperty(final String key, final boolean deep, final ScriptObject start) { + protected FindProperty findProperty(final String key, final boolean deep, final ScriptObject start) { final PropertyMap selfMap = getMap(); final Property property = selfMap.findProperty(key);
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/WithObject.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/WithObject.java Wed Apr 08 14:31:51 2015 -0700 @@ -198,7 +198,7 @@ * @return FindPropertyData or null if not found. */ @Override - FindProperty findProperty(final String key, final boolean deep, final ScriptObject start) { + protected FindProperty findProperty(final String key, final boolean deep, final ScriptObject start) { // We call findProperty on 'expression' with 'expression' itself as start parameter. // This way in ScriptObject.setObject we can tell the property is from a 'with' expression // (as opposed from another non-scope object in the proto chain such as Object.prototype).
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/ArrayCompiler.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/ArrayCompiler.java Wed Apr 08 14:31:51 2015 -0700 @@ -145,9 +145,6 @@ case TargetInfo.IS_EMPTY_MEM: addOpcode(OPCode.NULL_CHECK_END_MEMST); break; - case TargetInfo.IS_EMPTY_REC: - addOpcode(OPCode.NULL_CHECK_END_MEMST_PUSH); - break; default: break; } // switch
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/ByteCodeMachine.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/ByteCodeMachine.java Wed Apr 08 14:31:51 2015 -0700 @@ -183,7 +183,6 @@ case OPCode.NULL_CHECK_START: opNullCheckStart(); continue; case OPCode.NULL_CHECK_END: opNullCheckEnd(); continue; case OPCode.NULL_CHECK_END_MEMST: opNullCheckEndMemST(); continue; - case OPCode.NULL_CHECK_END_MEMST_PUSH: opNullCheckEndMemSTPush(); continue; case OPCode.JUMP: opJump(); continue; case OPCode.PUSH: opPush(); continue; @@ -1025,29 +1024,6 @@ } } - // USE_SUBEXP_CALL - private void opNullCheckEndMemSTPush() { - final int mem = code[ip++]; /* mem: null check id */ - - int isNull; - if (Config.USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT) { - isNull = nullCheckMemStRec(mem, s); - } else { - isNull = nullCheckRec(mem, s); - } - - if (isNull != 0) { - if (Config.DEBUG_MATCH) { - Config.log.println("NULL_CHECK_END_MEMST_PUSH: skip id:" + mem + ", s:" + s); - } - - if (isNull == -1) {opFail(); return;} - nullCheckFound(); - } else { - pushNullCheckEnd(mem); - } - } - private void opJump() { ip += code[ip] + 1; }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/StackMachine.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/StackMachine.java Wed Apr 08 14:31:51 2015 -0700 @@ -19,7 +19,6 @@ */ package jdk.nashorn.internal.runtime.regexp.joni; -import static jdk.nashorn.internal.runtime.regexp.joni.BitStatus.bsAt; import java.lang.ref.WeakReference; import jdk.nashorn.internal.runtime.regexp.joni.constants.StackPopLevel; import jdk.nashorn.internal.runtime.regexp.joni.constants.StackType; @@ -369,118 +368,9 @@ } } - protected final int nullCheckRec(final int id, final int s) { - int level = 0; - int k = stk; - while (true) { - k--; - final StackEntry e = stack[k]; - - if (e.type == NULL_CHECK_START) { - if (e.getNullCheckNum() == id) { - if (level == 0) { - return e.getNullCheckPStr() == s ? 1 : 0; - } - level--; - } - } else if (e.type == NULL_CHECK_END) { - level++; - } - } - } - protected final int nullCheckMemSt(final int id, final int s) { - int k = stk; - int isNull; - while (true) { - k--; - StackEntry e = stack[k]; - - if (e.type == NULL_CHECK_START) { - if (e.getNullCheckNum() == id) { - if (e.getNullCheckPStr() != s) { - isNull = 0; - break; - } - int endp; - isNull = 1; - while (k < stk) { - if (e.type == MEM_START) { - if (e.getMemEnd() == INVALID_INDEX) { - isNull = 0; - break; - } - if (bsAt(regex.btMemEnd, e.getMemNum())) { - endp = stack[e.getMemEnd()].getMemPStr(); - } else { - endp = e.getMemEnd(); - } - if (stack[e.getMemStart()].getMemPStr() != endp) { - isNull = 0; - break; - } else if (endp != s) { - isNull = -1; /* empty, but position changed */ - } - } - k++; - e = stack[k]; // !! - } - break; - } - } - } - return isNull; - } - - protected final int nullCheckMemStRec(final int id, final int s) { - int level = 0; - int k = stk; - int isNull; - while (true) { - k--; - StackEntry e = stack[k]; - - if (e.type == NULL_CHECK_START) { - if (e.getNullCheckNum() == id) { - if (level == 0) { - if (e.getNullCheckPStr() != s) { - isNull = 0; - break; - } - int endp; - isNull = 1; - while (k < stk) { - if (e.type == MEM_START) { - if (e.getMemEnd() == INVALID_INDEX) { - isNull = 0; - break; - } - if (bsAt(regex.btMemEnd, e.getMemNum())) { - endp = stack[e.getMemEnd()].getMemPStr(); - } else { - endp = e.getMemEnd(); - } - if (stack[e.getMemStart()].getMemPStr() != endp) { - isNull = 0; - break; - } else if (endp != s) { - isNull = -1; /* empty, but position changed */ - } - } - k++; - e = stack[k]; - } - break; - } - level--; - } - } else if (e.type == NULL_CHECK_END) { - if (e.getNullCheckNum() == id) { - level++; - } - } - } - return isNull; + // Return -1 here to cause operation to fail + return -nullCheck(id, s); } protected final int getRepeat(final int id) {
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/constants/TargetInfo.java Wed Apr 08 13:12:53 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/regexp/joni/constants/TargetInfo.java Wed Apr 08 14:31:51 2015 -0700 @@ -24,5 +24,4 @@ final int ISNOT_EMPTY = 0; final int IS_EMPTY = 1; final int IS_EMPTY_MEM = 2; - final int IS_EMPTY_REC = 3; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/basic/JDK-8073868.js Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * JDK-8073868: Regex matching causes java.lang.ArrayIndexOutOfBoundsException: 64 + * + * @test + * @run + */ + +function test(input) { + var comma = input.indexOf(","); + Assert.assertEquals(/([^\s]+),(.*)+/.exec(input)[0], input.trimLeft()); + Assert.assertEquals(/([^\s]+),(.*)+/.exec(input)[1], input.substring(0, comma).trimLeft()); + Assert.assertEquals(/([^\s]+),(.*)+/.exec(input)[2], input.substring(comma + 1)); + Assert.assertEquals(/(.*)+/.exec(input)[0], input); + Assert.assertEquals(/(.*)+/.exec(input)[1], input); +} + +test(" xxxx, xxx xxxxxx xxxxxxxxx xxxxxxx, xxxx xxxxx xxxxx "); +test(" xxxx, xxx xxxxxx xxxxxxxxx xxxxxxx, xxxx xxxxx xxxxx "); +test("x, xxxxxxxxxx xxxxxxxxx xxxxxxx, xxxx xxxxx xxxxx "); + +Assert.assertEquals(/(?:\1a|())*/.exec("a")[0], "a"); +Assert.assertEquals(/(?:\1a|())*/.exec("a")[1], undefined);
--- a/test/script/basic/es6/let-eval.js Wed Apr 08 13:12:53 2015 -0700 +++ b/test/script/basic/es6/let-eval.js Wed Apr 08 14:31:51 2015 -0700 @@ -96,3 +96,9 @@ f(); print(typeof a, typeof b, typeof c, typeof x, typeof z); + +let v = 1; +eval("print('v: ' + v); v = 2; print ('v: ' + v);"); +print("this.v: " + this.v); +print("v: " + v); +
--- a/test/script/basic/es6/let-eval.js.EXPECTED Wed Apr 08 13:12:53 2015 -0700 +++ b/test/script/basic/es6/let-eval.js.EXPECTED Wed Apr 08 14:31:51 2015 -0700 @@ -14,3 +14,7 @@ 2 1 0 2 1 0 undefined undefined undefined undefined undefined undefined +v: 1 +v: 2 +this.v: undefined +v: 2
--- a/test/script/sandbox/interfaceimpl.js Wed Apr 08 13:12:53 2015 -0700 +++ b/test/script/sandbox/interfaceimpl.js Wed Apr 08 14:31:51 2015 -0700 @@ -29,8 +29,8 @@ * @security */ -var Window = Java.type("jdk.nashorn.api.scripting.Window"); -var WindowEventHandler = Java.type("jdk.nashorn.api.scripting.WindowEventHandler"); +var Window = Java.type("jdk.nashorn.api.scripting.test.Window"); +var WindowEventHandler = Java.type("jdk.nashorn.api.scripting.test.WindowEventHandler"); var w = new Window();
--- a/test/script/trusted/JDK-8025629.js Wed Apr 08 13:12:53 2015 -0700 +++ b/test/script/trusted/JDK-8025629.js Wed Apr 08 14:31:51 2015 -0700 @@ -28,6 +28,6 @@ * @run */ -load("classpath:jdk/nashorn/internal/runtime/resources/load_test.js") +load("classpath:jdk/nashorn/internal/runtime/test/resources/load_test.js") Assert.assertEquals(loadedFunc("hello"), "HELLO");
--- a/test/src/META-INF/services/java.sql.Driver Wed Apr 08 13:12:53 2015 -0700 +++ b/test/src/META-INF/services/java.sql.Driver Wed Apr 08 14:31:51 2015 -0700 @@ -1,1 +1,1 @@ -jdk.nashorn.api.NashornSQLDriver +jdk.nashorn.api.test.NashornSQLDriver
--- a/test/src/jdk/internal/dynalink/beans/CallerSensitiveTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.internal.dynalink.beans; - -import jdk.nashorn.test.models.ClassLoaderAware; -import org.testng.annotations.Test; - -@SuppressWarnings("javadoc") -public class CallerSensitiveTest { - @Test - public void testCallerSensitive() { - BeansLinker.getLinkerForClass(ClassLoaderAware.class); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/internal/dynalink/beans/test/CallerSensitiveTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.internal.dynalink.beans.test; + +import jdk.internal.dynalink.beans.BeansLinker; +import jdk.nashorn.test.models.ClassLoaderAware; +import org.testng.annotations.Test; + +@SuppressWarnings("javadoc") +public class CallerSensitiveTest { + @Test + public void testCallerSensitive() { + BeansLinker.getLinkerForClass(ClassLoaderAware.class); + } +}
--- a/test/src/jdk/nashorn/api/NashornSQLDriver.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api; - -import java.sql.Connection; -import java.sql.Driver; -import java.sql.DriverManager; -import java.sql.DriverPropertyInfo; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.util.Properties; -import java.util.logging.Logger; - -/** - * A dummy SQL driver for testing purpose. - */ -public final class NashornSQLDriver implements Driver { - static { - try { - DriverManager.registerDriver(new NashornSQLDriver(), null); - } catch (final SQLException se) { - throw new RuntimeException(se); - } - } - - @Override - public boolean acceptsURL(final String url) { - return url.startsWith("jdbc:nashorn:"); - } - - @Override - public Connection connect(final String url, final Properties info) { - throw new UnsupportedOperationException("I am a dummy!!"); - } - - @Override - public int getMajorVersion() { - return -1; - } - - @Override - public int getMinorVersion() { - return -1; - } - - @Override - public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) { - return new DriverPropertyInfo[0]; - } - - @Override - public boolean jdbcCompliant() { - // no way! - return false; - } - - @Override - public Logger getParentLogger() throws SQLFeatureNotSupportedException { - throw new SQLFeatureNotSupportedException(); - } -}
--- a/test/src/jdk/nashorn/api/javaaccess/ArrayConversionTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,235 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertFalse; -import static org.testng.AssertJUnit.assertNull; -import static org.testng.AssertJUnit.assertTrue; -import java.util.Arrays; -import java.util.List; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -@SuppressWarnings("javadoc") -public class ArrayConversionTest { - private static ScriptEngine e = null; - - public static void main(final String[] args) { - TestNG.main(args); - } - - @BeforeClass - public static void setUpClass() { - e = new ScriptEngineManager().getEngineByName("nashorn"); - } - - @AfterClass - public static void tearDownClass() { - e = null; - } - - @Test - public void testIntArrays() throws ScriptException { - runTest("assertNullIntArray", "null"); - runTest("assertEmptyIntArray", "[]"); - runTest("assertSingle42IntArray", "[42]"); - runTest("assertSingle42IntArray", "['42']"); - runTest("assertIntArrayConversions", "[false, true, NaN, Infinity, -Infinity, 0.4, 0.6, null, undefined, [], {}, [1], [1, 2]]"); - } - - @Test - public void testIntIntArrays() throws ScriptException { - runTest("assertNullIntIntArray", "null"); - runTest("assertEmptyIntIntArray", "[]"); - runTest("assertSingleEmptyIntIntArray", "[[]]"); - runTest("assertSingleNullIntIntArray", "[null]"); - runTest("assertLargeIntIntArray", "[[false], [1], [2, 3], [4, 5, 6], ['7', {valueOf: function() { return 8 }}]]"); - } - - @Test - public void testObjectObjectArrays() throws ScriptException { - runTest("assertLargeObjectObjectArray", "[[false], [1], ['foo', 42.3], [{x: 17}]]"); - } - - @Test - public void testBooleanArrays() throws ScriptException { - runTest("assertBooleanArrayConversions", "[false, true, '', 'false', 0, 1, 0.4, 0.6, {}, [], [false], [true], NaN, Infinity, null, undefined]"); - } - - @Test - public void testArrayAmbiguity() throws ScriptException { - runTest("x", "'abc'"); - runTest("x", "['foo', 'bar']"); - } - - @Test - public void testListArrays() throws ScriptException { - runTest("assertListArray", "[['foo', 'bar'], ['apple', 'orange']]"); - } - - @Test - public void testVarArgs() throws ScriptException { - // Sole NativeArray in vararg position becomes vararg array itself - runTest("assertVarArg_42_17", "[42, 17]"); - // NativeArray in vararg position becomes an argument if there are more arguments - runTest("assertVarArg_array_17", "[42], 18"); - // Only NativeArray is converted to vararg array, other objects (e.g. a function) aren't - runTest("assertVarArg_function", "function() { return 'Hello' }"); - } - - private static void runTest(final String testMethodName, final String argument) throws ScriptException { - e.eval("Java.type('" + ArrayConversionTest.class.getName() + "')." + testMethodName + "(" + argument + ")"); - } - - public static void assertNullIntArray(final int[] array) { - assertNull(array); - } - - public static void assertNullIntIntArray(final int[][] array) { - assertNull(array); - } - - public static void assertEmptyIntArray(final int[] array) { - assertEquals(0, array.length); - } - - public static void assertSingle42IntArray(final int[] array) { - assertEquals(1, array.length); - assertEquals(42, array[0]); - } - - - public static void assertIntArrayConversions(final int[] array) { - assertEquals(13, array.length); - assertEquals(0, array[0]); // false - assertEquals(1, array[1]); // true - assertEquals(0, array[2]); // NaN - assertEquals(0, array[3]); // Infinity - assertEquals(0, array[4]); // -Infinity - assertEquals(0, array[5]); // 0.4 - assertEquals(0, array[6]); // 0.6 - floor, not round - assertEquals(0, array[7]); // null - assertEquals(0, array[8]); // undefined - assertEquals(0, array[9]); // [] - assertEquals(0, array[10]); // {} - assertEquals(1, array[11]); // [1] - assertEquals(0, array[12]); // [1, 2] - } - - public static void assertEmptyIntIntArray(final int[][] array) { - assertEquals(0, array.length); - } - - public static void assertSingleEmptyIntIntArray(final int[][] array) { - assertEquals(1, array.length); - assertTrue(Arrays.equals(new int[0], array[0])); - } - - public static void assertSingleNullIntIntArray(final int[][] array) { - assertEquals(1, array.length); - assertNull(null, array[0]); - } - - public static void assertLargeIntIntArray(final int[][] array) { - assertEquals(5, array.length); - assertTrue(Arrays.equals(new int[] { 0 }, array[0])); - assertTrue(Arrays.equals(new int[] { 1 }, array[1])); - assertTrue(Arrays.equals(new int[] { 2, 3 }, array[2])); - assertTrue(Arrays.equals(new int[] { 4, 5, 6 }, array[3])); - assertTrue(Arrays.equals(new int[] { 7, 8 }, array[4])); - } - - public static void assertLargeObjectObjectArray(final Object[][] array) throws ScriptException { - assertEquals(4, array.length); - assertTrue(Arrays.equals(new Object[] { Boolean.FALSE }, array[0])); - assertTrue(Arrays.equals(new Object[] { 1 }, array[1])); - assertTrue(Arrays.equals(new Object[] { "foo", 42.3d }, array[2])); - assertEquals(1, array[3].length); - e.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", array[3][0]); - assertEquals(17, e.eval("obj.x")); - } - - public static void assertBooleanArrayConversions(final boolean[] array) { - assertEquals(16, array.length); - assertFalse(array[0]); // false - assertTrue(array[1]); // true - assertFalse(array[2]); // '' - assertTrue(array[3]); // 'false' (yep, every non-empty string converts to true) - assertFalse(array[4]); // 0 - assertTrue(array[5]); // 1 - assertTrue(array[6]); // 0.4 - assertTrue(array[7]); // 0.6 - assertTrue(array[8]); // {} - assertTrue(array[9]); // [] - assertTrue(array[10]); // [false] - assertTrue(array[11]); // [true] - assertFalse(array[12]); // NaN - assertTrue(array[13]); // Infinity - assertFalse(array[14]); // null - assertFalse(array[15]); // undefined - } - - public static void assertListArray(final List<?>[] array) { - assertEquals(2, array.length); - assertEquals(Arrays.asList("foo", "bar"), array[0]); - assertEquals(Arrays.asList("apple", "orange"), array[1]); - } - - public static void assertVarArg_42_17(final Object... args) { - assertEquals(2, args.length); - assertEquals(42, ((Number)args[0]).intValue()); - assertEquals(17, ((Number)args[1]).intValue()); - } - - public static void assertVarArg_array_17(final Object... args) throws ScriptException { - assertEquals(2, args.length); - e.getBindings(ScriptContext.ENGINE_SCOPE).put("arr", args[0]); - assertTrue((Boolean)e.eval("arr instanceof Array && arr.length == 1 && arr[0] == 42")); - assertEquals(18, ((Number)args[1]).intValue()); - } - - public static void assertVarArg_function(final Object... args) throws ScriptException { - assertEquals(1, args.length); - e.getBindings(ScriptContext.ENGINE_SCOPE).put("fn", args[0]); - assertEquals("Hello", e.eval("fn()")); - } - - - - public static void x(final String y) { - assertEquals("abc", y); - } - public static void x(final String[] y) { - assertTrue(Arrays.equals(new String[] { "foo", "bar"}, y)); - } -}
--- a/test/src/jdk/nashorn/api/javaaccess/BooleanAccessTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,219 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertTrue; -import java.util.Arrays; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -/** - * @test - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.BooleanAccessTest - * @run testng/othervm jdk.nashorn.api.javaaccess.BooleanAccessTest - */ -@SuppressWarnings("javadoc") -public class BooleanAccessTest { - - private static ScriptEngine e = null; - private static SharedObject o = null; - - public static void main(final String[] args) { - TestNG.main(args); - } - - @BeforeClass - public static void setUpClass() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - e = m.getEngineByName("nashorn"); - o = new SharedObject(); - e.put("o", o); - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); - } - - @AfterClass - public static void tearDownClass() { - e = null; - o = null; - } - - @Test - public void accessFieldBoolean() throws ScriptException { - e.eval("var p_boolean = o.publicBoolean;"); - assertEquals(o.publicBoolean, e.get("p_boolean")); - assertEquals("boolean", e.eval("typeof p_boolean;")); - e.eval("o.publicBoolean = false;"); - assertEquals(false, o.publicBoolean); - } - - @Test - public void accessFieldBooleanArray() throws ScriptException { - e.eval("var p_boolean_array = o.publicBooleanArray;"); - assertEquals(o.publicBooleanArray[0], e.eval("o.publicBooleanArray[0]")); - assertTrue(Arrays.equals(o.publicBooleanArray, (boolean[])e.get("p_boolean_array"))); - e.eval("var t_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + - "t_boolean_arr[0] = true;" + - "t_boolean_arr[1] = false;" + - "t_boolean_arr[2] = false;" + - "o.publicBooleanArray = t_boolean_arr;"); - assertTrue(Arrays.equals(new boolean[] { true, false, false }, o.publicBooleanArray)); - e.eval("o.publicBooleanArray[0] = false;"); - assertEquals(false, o.publicBooleanArray[0]); - } - - @Test - public void accessStaticFieldBoolean() throws ScriptException { - e.eval("var ps_boolean = SharedObject.publicStaticBoolean;"); - assertEquals(SharedObject.publicStaticBoolean, e.get("ps_boolean")); - assertEquals("boolean", e.eval("typeof ps_boolean;")); - e.eval("SharedObject.publicStaticBoolean = false;"); - assertEquals(false, SharedObject.publicStaticBoolean); - } - - @Test - public void accessStaticFieldBooleanArray() throws ScriptException { - e.eval("var ps_boolean_array = SharedObject.publicStaticBooleanArray;"); - assertEquals(SharedObject.publicStaticBooleanArray[0], e.eval("SharedObject.publicStaticBooleanArray[0]")); - assertTrue(Arrays.equals(SharedObject.publicStaticBooleanArray, (boolean[])e.get("ps_boolean_array"))); - e.eval("var ts_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + - "ts_boolean_arr[0] = true;" + - "ts_boolean_arr[1] = false;" + - "ts_boolean_arr[2] = true;" + - "SharedObject.publicStaticBooleanArray = ts_boolean_arr;"); - assertTrue(Arrays.equals(new boolean[] { true, false, true }, SharedObject.publicStaticBooleanArray)); - e.eval("SharedObject.publicStaticBooleanArray[0] = false;"); - assertEquals(false, SharedObject.publicStaticBooleanArray[0]); - } - - @Test - public void accessFinalFieldBoolean() throws ScriptException { - e.eval("var pf_boolean = o.publicFinalBoolean;"); - assertEquals(o.publicFinalBoolean, e.get("pf_boolean")); - assertEquals("boolean", e.eval("typeof pf_boolean;")); - e.eval("o.publicFinalBoolean = false;"); - assertEquals(true, o.publicFinalBoolean); - } - - @Test - public void accessFinalFieldBooleanArray() throws ScriptException { - e.eval("var pf_boolean_array = o.publicFinalBooleanArray;"); - assertEquals(o.publicFinalBooleanArray[0], e.eval("o.publicFinalBooleanArray[0]")); - assertTrue(Arrays.equals(o.publicFinalBooleanArray, (boolean[])e.get("pf_boolean_array"))); - e.eval("var tf_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + - "tf_boolean_arr[0] = false;" + - "tf_boolean_arr[1] = false;" + - "tf_boolean_arr[2] = true;" + - "o.publicOFinalbjectArray = tf_boolean_arr;"); - assertTrue(Arrays.equals(new boolean[] { false, false, true, false }, o.publicFinalBooleanArray)); - e.eval("o.publicFinalBooleanArray[0] = true;"); - assertEquals(true, o.publicFinalBooleanArray[0]); - } - - @Test - public void accessStaticFinalFieldBoolean() throws ScriptException { - e.eval("var psf_boolean = SharedObject.publicStaticFinalBoolean;"); - assertEquals(SharedObject.publicStaticFinalBoolean, e.get("psf_boolean")); - assertEquals("boolean", e.eval("typeof psf_boolean;")); - e.eval("SharedObject.publicStaticFinalBoolean = false;"); - assertEquals(true, SharedObject.publicStaticFinalBoolean); - } - - @Test - public void accessStaticFinalFieldBooleanArray() throws ScriptException { - e.eval("var psf_boolean_array = SharedObject.publicStaticFinalBooleanArray;"); - assertEquals(SharedObject.publicStaticFinalBooleanArray[0], e.eval("SharedObject.publicStaticFinalBooleanArray[0]")); - assertTrue(Arrays.equals(SharedObject.publicStaticFinalBooleanArray, (boolean[])e.get("psf_boolean_array"))); - e.eval("var tsf_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + - "tsf_boolean_arr[0] = false;" + - "tsf_boolean_arr[1] = true;" + - "tsf_boolean_arr[2] = false;" + - "SharedObject.publicStaticFinalBooleanArray = tsf_boolean_arr;"); - assertTrue(Arrays.equals(new boolean[] { false, true, false, false }, SharedObject.publicStaticFinalBooleanArray)); - e.eval("SharedObject.publicStaticFinalBooleanArray[0] = true;"); - assertEquals(true, SharedObject.publicStaticFinalBooleanArray[0]); - } - - @Test - public void accessFieldBooleanBoxing() throws ScriptException { - e.eval("var p_boolean_box = o.publicBooleanBox;"); - assertEquals(o.publicBooleanBox, e.get("p_boolean_box")); - assertEquals("boolean", e.eval("typeof p_boolean_box;")); - e.eval("o.publicBooleanBox = false;"); - assertEquals(false, (boolean)o.publicBooleanBox); - } - - @Test - public void accessStaticFieldBooleanBoxing() throws ScriptException { - e.eval("var ps_boolean_box = SharedObject.publicStaticBooleanBox;"); - assertEquals(SharedObject.publicStaticBooleanBox, e.get("ps_boolean_box")); - assertEquals("boolean", e.eval("typeof ps_boolean_box;")); - e.eval("SharedObject.publicStaticBooleanBox = false;"); - assertEquals(false, (boolean)SharedObject.publicStaticBooleanBox); - } - - @Test - public void accessFinalFieldBooleanBoxing() throws ScriptException { - e.eval("var pf_boolean_box = o.publicFinalBooleanBox;"); - assertEquals(o.publicFinalBooleanBox, e.get("pf_boolean_box")); - assertEquals("boolean", e.eval("typeof pf_boolean_box;")); - e.eval("o.publicFinalBooleanBox = false;"); - assertEquals(true, (boolean)o.publicFinalBooleanBox); - } - - @Test - public void accessStaticFinalFieldBooleanBoxing() throws ScriptException { - e.eval("var psf_boolean_box = SharedObject.publicStaticFinalBooleanBox;"); - assertEquals(SharedObject.publicStaticFinalBooleanBox, e.get("psf_boolean_box")); - assertEquals("boolean", e.eval("typeof psf_boolean_box;")); - e.eval("SharedObject.publicStaticFinalBooleanBox = false;"); - assertEquals(true, (boolean)SharedObject.publicStaticFinalBooleanBox); - } - - @Test - public void accessVolatileField() throws ScriptException { - e.eval("var pv_boolean = o.volatileBoolean;"); - assertEquals(o.volatileBoolean, e.get("pv_boolean")); - assertEquals("boolean", e.eval("typeof pv_boolean;")); - e.eval("o.volatileBoolean = false;"); - assertEquals(false, o.volatileBoolean); - } - - @Test - public void accessTransientField() throws ScriptException { - e.eval("var pt_boolean = o.transientBoolean;"); - assertEquals(o.transientBoolean, e.get("pt_boolean")); - assertEquals("boolean", e.eval("typeof pt_boolean;")); - e.eval("o.transientBoolean = false;"); - assertEquals(false, o.transientBoolean); - } - -}
--- a/test/src/jdk/nashorn/api/javaaccess/ConsStringTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import static org.testng.AssertJUnit.assertEquals; -import java.util.HashMap; -import java.util.Map; -import javax.script.Bindings; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import jdk.nashorn.api.scripting.JSObject; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -@SuppressWarnings("javadoc") -public class ConsStringTest { - private static ScriptEngine e = null; - - public static void main(final String[] args) { - TestNG.main(args); - } - - @BeforeClass - public static void setUpClass() { - e = new ScriptEngineManager().getEngineByName("nashorn"); - } - - @AfterClass - public static void tearDownClass() { - e = null; - } - - @Test - public void testConsStringFlattening() throws ScriptException { - final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); - final Map<Object, Object> m = new HashMap<>(); - b.put("m", m); - e.eval("var x = 'f'; x += 'oo'; var y = 'b'; y += 'ar'; m.put(x, y)"); - assertEquals("bar", m.get("foo")); - } - - @Test - public void testConsStringFromMirror() throws ScriptException { - final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); - //final Map<Object, Object> m = new HashMap<>(); - e.eval("var x = 'f'; x += 'oo'; var obj = {x: x};"); - assertEquals("foo", ((JSObject)b.get("obj")).getMember("x")); - } - - @Test - public void testArrayConsString() throws ScriptException { - final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); - final ArrayHolder h = new ArrayHolder(); - b.put("h", h); - e.eval("var x = 'f'; x += 'oo'; h.array = [x];"); - assertEquals(1, h.array.length); - assertEquals("foo", h.array[0]); - } - - - public static class ArrayHolder { - private Object[] array; - - public void setArray(final Object[] array) { - this.array = array; - } - - public Object[] getArray() { - return array; - } - } -}
--- a/test/src/jdk/nashorn/api/javaaccess/MethodAccessTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,466 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertTrue; -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Locale; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -/** - * @test - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.MethodAccessTest - * @run testng/othervm jdk.nashorn.api.javaaccess.MethodAccessTest - */ -@SuppressWarnings("javadoc") -public class MethodAccessTest { - - private static ScriptEngine e = null; - private static SharedObject o = null; - - public static void main(final String[] args) { - TestNG.main(args); - } - - @BeforeClass - public static void setUpClass() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - e = m.getEngineByName("nashorn"); - o = new SharedObject(); - o.setEngine(e); - e.put("o", o); - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); - e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;"); - } - - @AfterClass - public static void tearDownClass() { - e = null; - o = null; - } - - @Test - public void accessMethodthrowsCheckedException() throws ScriptException { - e.eval("try {" + - " var a = java.lang.Long.parseLong('foo');" + - "} catch(e) {" + - " var isThrown = true;" + - " var isNumberException = e instanceof java.lang.NumberFormatException;" + - "} finally {" + - " var isFinalized = true;" + - "}"); - assertEquals("Exception thrown", true, e.get("isThrown")); - assertEquals("Finally called", true, e.get("isFinalized")); - assertEquals("Type is NumberFormatException", true, e.get("isNumberException")); - } - - @Test - public void accessMethodthrowsUnCheckedException() throws ScriptException { - e.eval("try {" + - " var a = java.lang.String.valueOf(null);" + - "} catch(e) {" + - " var isThrown = true;" + - " var isNumberException = e instanceof java.lang.NullPointerException;" + - "} finally {" + - " var isFinalized = true;" + - "}"); - assertEquals(true, e.get("isThrown")); - assertEquals(true, e.get("isFinalized")); - assertEquals(true, e.get("isNumberException")); - } - - @Test - public void accessMethodStartsThread() throws ScriptException { - e.eval("o.methodStartsThread();"); - assertEquals(false, o.isFinished); - } - - @Test - public void accessStaticMethod() throws ScriptException { - assertEquals(10, e.eval("java.lang.Math.abs(-10);")); - } - - @Test - public void accessSynchronousMethod() throws ScriptException { - e.eval("var v = new java.util.Vector();" + "v.add(10);" + "v.add(20);" + "v.add(30);"); - assertEquals(10, e.eval("v[0]")); - assertEquals(20, e.eval("v[1]")); - assertEquals(30, e.eval("v[2]")); - assertEquals(3, e.eval("v.size()")); - } - - @Test - public void accessStaticSynchronousMethod() throws ScriptException { - e.eval("var locales = java.util.Calendar.getAvailableLocales();"); - final Locale[] locales = (Locale[])e.get("locales"); - assertEquals(locales.length, Calendar.getAvailableLocales().length); - } - - @Test - public void accessNativeMethod() throws ScriptException { - assertEquals(4.0, e.eval("java.lang.StrictMath.log10(10000);")); - } - - @Test - public void accessConstructorOfAbstractClass() throws ScriptException { - e.eval("try {" + - " var a = new java.util.AbstractList();" + - " print('fail');" + - "} catch(e) {" + - " var isThrown = true;" + - "}"); - assertEquals(true, e.get("isThrown")); - } - - @Test - public void accessMethodVoid() throws ScriptException { - o.isAccessed = false; - e.eval("o.voidMethod();"); - assertTrue(o.isAccessed); - } - - @Test - public void accessMethodBoolean() throws ScriptException { - assertEquals(true, e.eval("o.booleanMethod(false);")); - assertEquals(false, e.eval("o.booleanMethod(true);")); - assertEquals(false, e.eval("o.booleanMethod('false');")); - assertEquals(true, e.eval("o.booleanMethod('');")); - assertEquals(true, e.eval("o.booleanMethod(0);")); - } - - @Test - public void accessMethodInt() throws ScriptException { - assertEquals(0, e.eval("o.intMethod(0);")); - assertEquals(-200, e.eval("o.intMethod(-100);")); - assertEquals(0, e.eval("o.intMethod('0');")); - assertEquals(-200, e.eval("o.intMethod('-100');")); - } - - @Test - public void accessMethodLong() throws ScriptException { - assertEquals((long)0, e.eval("o.longMethod(0);")); - assertEquals((long)400, e.eval("o.longMethod(200);")); - assertEquals((long) 0, e.eval("o.longMethod('0');")); - assertEquals((long) 400, e.eval("o.longMethod('200');")); - } - - @Test - public void accessMethodByte() throws ScriptException { - assertEquals((byte) 0, e.eval("o.byteMethod(0);")); - assertEquals((byte) 10, e.eval("o.byteMethod(5);")); - assertEquals((byte) 0, e.eval("o.byteMethod('0');")); - assertEquals((byte) 10, e.eval("o.byteMethod('5');")); - } - - @Test - public void accessMethodShort() throws ScriptException { - assertEquals((short)0, e.eval("o.shortMethod(0);")); - assertEquals((short)8000, e.eval("o.shortMethod(4000);")); - assertEquals((short) 0, e.eval("o.shortMethod('0');")); - assertEquals((short) 8000, e.eval("o.shortMethod('4000');")); - } - - @Test - public void accessMethodChar() throws ScriptException { - assertEquals('A', e.eval("o.charMethod('a');")); - assertEquals('Z', e.eval("o.charMethod('z');")); - assertEquals(o.charMethod((char)0), e.eval("o.charMethod(0);")); - assertEquals(o.charMethod((char)3150), e.eval("o.charMethod(3150);")); - } - - @Test - public void accessMethodFloat() throws ScriptException { - assertEquals(0.0f, e.eval("o.floatMethod(0.0);")); - assertEquals(4.2f, e.eval("o.floatMethod(2.1);")); - assertEquals(0.0f, e.eval("o.floatMethod('0.0');")); - assertEquals(4.2f, e.eval("o.floatMethod('2.1');")); - } - - @Test - public void accessMethodDouble() throws ScriptException { - assertEquals(0.0, e.eval("o.doubleMethod(0.0);")); - assertEquals(14.0, e.eval("o.doubleMethod(7.0);")); - assertEquals(0.0, e.eval("o.doubleMethod('0.0');")); - assertEquals(14.0, e.eval("o.doubleMethod('7.0');")); - } - - @Test - public void accessMethodBooleanBoxing() throws ScriptException { - assertEquals(Boolean.TRUE, e.eval("o.booleanBoxingMethod(java.lang.Boolean.FALSE);")); - assertEquals(Boolean.FALSE, e.eval("o.booleanBoxingMethod(java.lang.Boolean.TRUE);")); - assertEquals(Boolean.TRUE, e.eval("o.booleanBoxingMethod('');")); - assertEquals(Boolean.FALSE, e.eval("o.booleanBoxingMethod('false');")); - } - - @Test - public void accessMethodIntBoxing() throws ScriptException { - assertEquals(0, e.eval("o.intBoxingMethod(0);")); - assertEquals(-200, e.eval("o.intBoxingMethod(-100);")); - assertTrue((int)e.eval("(new java.lang.Integer(2)).compareTo(10.0)") < 0); - } - - @Test - public void accessMethodLongBoxing() throws ScriptException { - assertEquals((long) 0, e.eval("o.longBoxingMethod(0);")); - assertEquals((long) 400, e.eval("o.longBoxingMethod(200);")); - assertTrue((int)e.eval("(new java.lang.Long(2)).compareTo(10.0)") < 0); - } - - @Test - public void accessMethodByteBoxing() throws ScriptException { - assertEquals((byte) 0, e.eval("o.byteBoxingMethod(0);")); - assertEquals((byte) 10, e.eval("o.byteBoxingMethod(5);")); - assertTrue((int)e.eval("(new java.lang.Byte(2)).compareTo(10.0)") < 0); - } - - @Test - public void accessMethodShortBoxing() throws ScriptException { - assertEquals((short) 0, e.eval("o.shortBoxingMethod(0);")); - assertEquals((short) 8000, e.eval("o.shortBoxingMethod(4000);")); - assertTrue((int)e.eval("(new java.lang.Short(2)).compareTo(10.0)") < 0); - } - - @Test - public void accessMethodCharBoxing() throws ScriptException { - assertEquals('A', e.eval("o.charBoxingMethod('a');")); - assertEquals('Z', e.eval("o.charBoxingMethod('z');")); - assertTrue((int)e.eval("(new java.lang.Character(2)).compareTo(10)") < 0); - } - - @Test - public void accessMethodFloatBoxing() throws ScriptException { - assertEquals(0.0f, e.eval("o.floatBoxingMethod(0.0);")); - assertEquals(4.2f, e.eval("o.floatBoxingMethod(2.1);")); - assertTrue((int)e.eval("(new java.lang.Float(2.0)).compareTo(10.0)") < 0); - } - - @Test - public void accessMethodDoubleBoxing() throws ScriptException { - assertEquals(0.0, e.eval("o.doubleBoxingMethod(0.0);")); - assertEquals(14.0, e.eval("o.doubleBoxingMethod(7.0);")); - assertTrue((int)e.eval("(new java.lang.Double(2)).compareTo(10.0)") < 0); - } - - @Test - public void accessMethodString() throws ScriptException { - assertEquals("", e.eval("o.stringMethod('');")); - assertEquals("abcabc", e.eval("o.stringMethod('abc');")); - } - - @Test - public void accessMethodObject() throws ScriptException { - e.put("so", new Person(5)); - e.eval("var rso = o.objectMethod(so);"); - assertEquals(new Person(10), e.get("rso")); - } - - @Test - public void accessMethodBooleanArray() throws ScriptException { - assertTrue(Arrays.equals(o.booleanArrayMethod(o.publicBooleanArray), (boolean[])e.eval("o.booleanArrayMethod(o.publicBooleanArray);"))); - } - - @Test - public void accessMethodIntArray() throws ScriptException { - assertArrayEquals(o.intArrayMethod(o.publicIntArray), (int[])e.eval("o.intArrayMethod(o.publicIntArray);")); - } - - @Test - public void accessMethodLongArray() throws ScriptException { - assertArrayEquals(o.longArrayMethod(o.publicLongArray), (long[])e.eval("o.longArrayMethod(o.publicLongArray);")); - } - - @Test - public void accessMethodByteArray() throws ScriptException { - assertArrayEquals(o.byteArrayMethod(o.publicByteArray), (byte[])e.eval("o.byteArrayMethod(o.publicByteArray);")); - } - - @Test - public void accessMethodShortArray() throws ScriptException { - assertArrayEquals(o.shortArrayMethod(o.publicShortArray), (short[])e.eval("o.shortArrayMethod(o.publicShortArray);")); - } - - @Test - public void accessMethodCharArray() throws ScriptException { - assertArrayEquals(o.charArrayMethod(o.publicCharArray), (char[])e.eval("o.charArrayMethod(o.publicCharArray);")); - } - - @Test - public void accessMethodFloatArray() throws ScriptException { - assertArrayEquals(o.floatArrayMethod(o.publicFloatArray), (float[])e.eval("o.floatArrayMethod(o.publicFloatArray);"), 1e-10f); - } - - @Test - public void accessMethodDoubleArray() throws ScriptException { - assertArrayEquals(o.doubleArrayMethod(o.publicDoubleArray), (double[])e.eval("o.doubleArrayMethod(o.publicDoubleArray);"), 1e-10); - } - - @Test - public void accessMethodStringArray() throws ScriptException { - assertArrayEquals(o.stringArrayMethod(o.publicStringArray), (String[])e.eval("o.stringArrayMethod(o.publicStringArray);")); - } - - @Test - public void accessMethodObjectArray() throws ScriptException { - assertArrayEquals(o.objectArrayMethod(o.publicObjectArray), (Person[])e.eval("o.objectArrayMethod(o.publicObjectArray);")); - } - - @Test - public void accessDefaultConstructor() throws ScriptException { - e.eval("var dc = new Packages.jdk.nashorn.api.javaaccess.Person()"); - assertEquals(new Person(), e.get("dc")); - } - - @Test - public void accessCustomConstructor() throws ScriptException { - e.eval("var cc = new Packages.jdk.nashorn.api.javaaccess.Person(17)"); - assertEquals(new Person(17), e.get("cc")); - } - - @Test - public void accessMethod2PrimitiveParams() throws ScriptException { - assertEquals(o.twoParamMethod(50, 40.0), e.eval("o.twoParamMethod(50,40);")); - } - - @Test - public void accessMethod3PrimitiveParams() throws ScriptException { - assertEquals(o.threeParamMethod((short)10, 20L, 'b'), e.eval("o.threeParamMethod(10,20,'b');")); - } - - @Test - public void accessMethod2ObjectParams() throws ScriptException { - assertArrayEquals(new Person[] { new Person(200), new Person(300) }, (Person[])e.eval("o.twoObjectParamMethod(new Person(300),new Person(200));")); - } - - @Test - public void accessMethod3ObjectParams() throws ScriptException { - 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));")); - } - - @Test - public void accessMethod8ObjectParams() throws ScriptException { - 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));")); - } - - @Test - public void accessMethod9ObjectParams() throws ScriptException { - 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));")); - } - - @Test - public void accessMethodObjectEllipsis() throws ScriptException { - 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));")); - assertArrayEquals(new Person[] {}, (Person[])e.eval("o.methodObjectEllipsis()")); - assertArrayEquals(new Person[] { new Person(9) }, (Person[])e.eval("o.methodObjectEllipsis(new Person(9))")); - } - - @Test - public void accessMethodPrimitiveEllipsis() throws ScriptException { - assertArrayEquals(new Person[] { new Person(1), new Person(3), new Person(2) }, (Person[])e.eval("o.methodPrimitiveEllipsis(1,3,2);")); - assertArrayEquals(new Person[] {}, (Person[])e.eval("o.methodPrimitiveEllipsis();")); - 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);")); - } - - @Test - public void accessMethodMixedEllipsis() throws ScriptException { - assertArrayEquals(new Object[] { new Person(1), 12, "hello", true }, (Object[])e.eval("o.methodMixedEllipsis(new Person(1),12,'hello',true);")); - assertArrayEquals(new Object[] {}, (Object[])e.eval("o.methodMixedEllipsis();")); - } - - @Test - public void accessMethodObjectWithEllipsis() throws ScriptException { - assertArrayEquals(new Object[] { "hello", 12, 15, 16 }, (Object[])e.eval("o.methodObjectWithEllipsis('hello',12,15,16);")); - assertArrayEquals(new Object[] { "hello" }, (Object[])e.eval("o.methodObjectWithEllipsis('hello');")); - } - - @Test - public void accessMethodPrimitiveWithEllipsis() throws ScriptException { - assertArrayEquals(new Object[] { 14, 12L, 15L, 16L }, (Object[])e.eval("o.methodPrimitiveWithEllipsis(14,12,15,16);")); - assertArrayEquals(new Object[] { 12 }, (Object[])e.eval("o.methodPrimitiveWithEllipsis(12);")); - } - - @Test - public void accessMethodMixedWithEllipsis() throws ScriptException { - assertArrayEquals(new Object[] { "Hello", 10, true, -100500, 80d }, (Object[])e.eval("o.methodMixedWithEllipsis('Hello', 10, true, -100500,80.0);")); - assertArrayEquals(new Object[] { "Nashorn", 15 }, (Object[])e.eval("o.methodMixedWithEllipsis('Nashorn',15);")); - } - - @Test - public void accessMethodOverloaded() throws ScriptException { - assertEquals(0, e.eval("o.overloadedMethod(0);")); - assertEquals(2000, e.eval("o.overloadedMethod(1000);")); - assertEquals(2, e.eval("o.overloadedMethod('10');")); - assertEquals(7, e.eval("o.overloadedMethod('Nashorn');")); - assertEquals(4, e.eval("o.overloadedMethod('true');")); - assertEquals(1, e.eval("o.overloadedMethod(true);")); - assertEquals(0, e.eval("o.overloadedMethod(false);")); - assertEquals(44, e.eval("o.overloadedMethod(new Person(22));")); - assertEquals(0, e.eval("o.overloadedMethod(new Person());")); - } - - @Test - public void accessMethodDoubleVSintOverloaded() throws ScriptException { - assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.0);")); - assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(1000.0);")); - assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.01);")); - assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(100.02);")); - assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(0);")); - assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(1000);")); - } - - @Test - public void accessJavaMethodIntFromJSFromJavaFromJS() throws ScriptException { - e.eval("function secondLevelMethodInt(a) {" - + "return o.thirdLevelMethodInt(a);" - + "}"); - assertEquals(50, e.eval("o.firstLevelMethodInt(10);")); - } - - @Test - public void accessJavaMethodIntegerFromJSFromJavaFromJS() throws ScriptException { - e.eval("function secondLevelMethodInteger(a) {" - + "return o.thirdLevelMethodInteger(a);" - + "}"); - assertEquals(100, e.eval("o.firstLevelMethodInteger(10);")); - } - - @Test - public void accessJavaMethodObjectFromJSFromJavaFromJS() throws ScriptException { - e.eval("function secondLevelMethodObject(p) {" - + "return o.thirdLevelMethodObject(p);" - + "}"); - assertEquals(new Person(100), e.eval("o.firstLevelMethodObject(new Person(10));")); - } - -}
--- a/test/src/jdk/nashorn/api/javaaccess/NumberAccessTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,789 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertTrue; -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -/** - * @test - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.NumberAccessTest - * @run testng/othervm jdk.nashorn.api.javaaccess.NumberAccessTest - */ -@SuppressWarnings("javadoc") -public class NumberAccessTest { - - private static ScriptEngine e; - private static SharedObject o; - - public static void main(final String[] args) { - TestNG.main(args); - } - - @BeforeClass - public static void setUpClass() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - e = m.getEngineByName("nashorn"); - o = new SharedObject(); - e.put("o", o); - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); - } - - @AfterClass - public static void tearDownClass() { - e = null; - o = null; - } - - // --------------------------------long - // tests------------------------------------ - @Test - public void accessFieldLong() throws ScriptException { - e.eval("var p_long = o.publicLong;"); - assertEquals(o.publicLong, e.get("p_long")); - e.eval("o.publicLong = 12;"); - assertEquals(12, o.publicLong); - } - - @Test - public void accessFieldLongArray() throws ScriptException { - e.eval("var p_long_array = o.publicLongArray;"); - assertEquals(o.publicLongArray[0], e.eval("o.publicLongArray[0];")); - assertArrayEquals(o.publicLongArray, (long[])e.get("p_long_array")); - e.eval("var t_long_arr = new (Java.type(\"long[]\"))(3);" + - "t_long_arr[0] = -189009;" + - "t_long_arr[1] = 456;" + - "t_long_arr[2] = 600000001;" + - "o.publicLongArray = t_long_arr;"); - // e.eval("o.publicIntArray = [-189009,456,600000001];"); - assertArrayEquals(new long[] { -189009, 456, 600000001 }, o.publicLongArray); - e.eval("o.publicLongArray[0] = 10;"); - assertEquals(10, o.publicLongArray[0]); - } - - @Test - public void accessStaticFieldLong() throws ScriptException { - e.eval("var ps_long = SharedObject.publicStaticLong;"); - assertEquals(SharedObject.publicStaticLong, e.get("ps_long")); - e.eval("SharedObject.publicStaticLong = 120;"); - assertEquals(120, SharedObject.publicStaticLong); - } - - @Test - public void accessStaticFieldLongArray() throws ScriptException { - e.eval("var ps_long_array = SharedObject.publicStaticLongArray;"); - assertEquals(SharedObject.publicStaticLongArray[0], e.eval("SharedObject.publicStaticLongArray[0];")); - assertArrayEquals(SharedObject.publicStaticLongArray, (long[])e.get("ps_long_array")); - e.eval("var ts_long_arr = new (Java.type(\"long[]\"))(3);" + - "ts_long_arr[0] = -189009;" + - "ts_long_arr[1] = 456;" + - "ts_long_arr[2] = 600000001;" + - "SharedObject.publicStaticLongArray = ts_long_arr;"); - // e.eval("o.publicIntArray = [-189009,456,600000001];"); - assertArrayEquals(new long[] { -189009, 456, 600000001 }, SharedObject.publicStaticLongArray); - e.eval("SharedObject.publicStaticLongArray[0] = 10;"); - assertEquals(10, SharedObject.publicStaticLongArray[0]); - } - - @Test - public void accessFinalFieldLong() throws ScriptException { - e.eval("var pf_long = o.publicFinalLong;"); - assertEquals(o.publicFinalLong, e.get("pf_long")); - e.eval("o.publicFinalLong = 120;"); - assertEquals(13353333333333333L, o.publicFinalLong); - } - - @Test - public void accessFinalFieldLongArray() throws ScriptException { - e.eval("var pf_long_array = o.publicFinalLongArray;"); - assertEquals(o.publicFinalLongArray[0], e.eval("o.publicFinalLongArray[0];")); - assertArrayEquals(o.publicFinalLongArray, (long[])e.get("pf_long_array")); - e.eval("var tf_long_arr = new (Java.type(\"long[]\"))(3);" + - "tf_long_arr[0] = -189009;" + - "tf_long_arr[1] = 456;" + - "tf_long_arr[2] = 600000001;" + - "o.publicFinalLongArray = tf_long_arr;"); - // e.eval("o.publicIntArray = [-189009,456,600000001];"); - assertArrayEquals(new long[] { 1901733333333L, -2247355555L, 3977377777L }, o.publicFinalLongArray); - e.eval("o.publicFinalLongArray[0] = 10;"); - assertEquals(10, o.publicFinalLongArray[0]); - } - - @Test - public void accessStaticFinalFieldLong() throws ScriptException { - e.eval("var psf_long = SharedObject.publicStaticFinalLong;"); - assertEquals(SharedObject.publicStaticFinalLong, e.get("psf_long")); - e.eval("SharedObject.publicStaticFinalLong = 120;"); - assertEquals(8333333333333L, SharedObject.publicStaticFinalLong); - } - - @Test - public void accessStaticFinalFieldLongArray() throws ScriptException { - e.eval("var psf_long_array = SharedObject.publicStaticFinalLongArray;"); - assertEquals(SharedObject.publicStaticFinalLongArray[0], e.eval("SharedObject.publicStaticFinalLongArray[0];")); - assertArrayEquals(SharedObject.publicStaticFinalLongArray, (long[])e.get("psf_long_array")); - e.eval("var tsf_long_arr = new (Java.type(\"long[]\"))(3);" + - "tsf_long_arr[0] = -189009;" + - "tsf_long_arr[1] = 456;" + - "tsf_long_arr[2] = 600000001;" + - "SharedObject.publicStaticFinalLongArray = tsf_long_arr;"); - // e.eval("o.publicIntArray = [-189009,456,600000001];"); - assertArrayEquals(new long[] { 19017383333L, -2247358L, 39773787L }, SharedObject.publicStaticFinalLongArray); - e.eval("SharedObject.publicStaticFinalLongArray[0] = 10;"); - assertEquals(10, SharedObject.publicStaticFinalLongArray[0]); - } - - // --------------------------------int - // tests------------------------------------ - @Test - public void accessFieldInt() throws ScriptException { - e.eval("var p_int = o.publicInt;"); - assertEquals(o.publicInt, e.get("p_int")); - e.eval("o.publicInt = 14;"); - assertEquals(14, o.publicInt); - } - - @Test - public void accessFieldIntArray() throws ScriptException { - e.eval("var p_int_array = o.publicIntArray;"); - assertEquals(o.publicIntArray[0], e.eval("o.publicIntArray[0];")); - assertArrayEquals(o.publicIntArray, (int[])e.get("p_int_array")); - e.eval("var t_int_arr = new (Java.type(\"int[]\"))(3);" + - "t_int_arr[0] = 4;" + - "t_int_arr[1] = 5;" + - "t_int_arr[2] = 6;" + - "o.publicIntArray = t_int_arr;"); - assertArrayEquals(new int[] { 4, 5, 6 }, o.publicIntArray); - e.eval("o.publicIntArray[0] = 100;"); - assertEquals(100, o.publicIntArray[0]); - } - - @Test - public void accessStaticFieldInt() throws ScriptException { - e.eval("var ps_int = SharedObject.publicStaticInt;"); - assertEquals(SharedObject.publicStaticInt, e.get("ps_int")); - e.eval("SharedObject.publicStaticInt = 140;"); - assertEquals(140, SharedObject.publicStaticInt); - } - - @Test - public void accessStaticFieldIntArray() throws ScriptException { - e.eval("var ps_int_array = SharedObject.publicStaticIntArray;"); - assertEquals(SharedObject.publicStaticIntArray[0], e.eval("SharedObject.publicStaticIntArray[0];")); - assertArrayEquals(SharedObject.publicStaticIntArray, (int[])e.get("ps_int_array")); - e.eval("var ts_int_arr = new (Java.type(\"int[]\"))(3);" + - "ts_int_arr[0] = 4;" + - "ts_int_arr[1] = 5;" + - "ts_int_arr[2] = 6;" + - "SharedObject.publicStaticIntArray = ts_int_arr;"); - assertArrayEquals(new int[] { 4, 5, 6 }, SharedObject.publicStaticIntArray); - e.eval("SharedObject.publicStaticIntArray[0] = 100;"); - assertEquals(100, SharedObject.publicStaticIntArray[0]); - } - - @Test - public void accessFinalFieldInt() throws ScriptException { - e.eval("var pf_int = o.publicFinalInt;"); - assertEquals(o.publicFinalInt, e.get("pf_int")); - - e.eval("o.publicFinalInt = 10;"); - assertEquals(20712023, o.publicFinalInt); - } - - @Test - public void accessFinalFieldIntArray() throws ScriptException { - e.eval("var pf_int_array = o.publicFinalIntArray;"); - assertEquals(o.publicFinalIntArray[0], e.eval("o.publicFinalIntArray[0];")); - assertArrayEquals(o.publicFinalIntArray, (int[])e.get("pf_int_array")); - e.eval("var tf_int_arr = new (Java.type(\"int[]\"))(3);" + - "tf_int_arr[0] = 4;" + - "tf_int_arr[1] = 5;" + - "tf_int_arr[2] = 6;" + - "o.publicFinalIntArray = tf_int_arr;"); - assertArrayEquals(new int[] { 50, 80, 130, 210, 340 }, o.publicFinalIntArray); - e.eval("o.publicFinalIntArray[0] = 100;"); - assertEquals(100, o.publicFinalIntArray[0]); - } - - @Test - public void accessStaticFinalFieldInt() throws ScriptException { - e.eval("var psf_int = SharedObject.publicStaticFinalInt;"); - assertEquals(SharedObject.publicStaticFinalInt, e.get("psf_int")); - e.eval("SharedObject.publicStaticFinalInt = 140;"); - assertEquals(207182023, SharedObject.publicStaticFinalInt); - } - - @Test - public void accessStaticFinalFieldIntArray() throws ScriptException { - e.eval("var psf_int_array = SharedObject.publicStaticFinalIntArray;"); - assertEquals(SharedObject.publicStaticFinalIntArray[0], e.eval("SharedObject.publicStaticFinalIntArray[0];")); - assertArrayEquals(SharedObject.publicStaticFinalIntArray, (int[])e.get("psf_int_array")); - e.eval("var tsf_int_arr = new (Java.type(\"int[]\"))(3);" + - "tsf_int_arr[0] = 4;" + - "tsf_int_arr[1] = 5;" + - "tsf_int_arr[2] = 6;" + - "SharedObject.publicStaticFinalIntArray = tsf_int_arr;"); - assertArrayEquals(new int[] { 1308, 210, 340 }, SharedObject.publicStaticFinalIntArray); - e.eval("SharedObject.publicStaticFinalIntArray[0] = 100;"); - assertEquals(100, SharedObject.publicStaticFinalIntArray[0]); - } - - // --------------------------------byte - // tests------------------------------------ - @Test - public void accessFieldByte() throws ScriptException { - e.eval("var p_byte = o.publicByte;"); - assertEquals((double)o.publicByte, ((Number)e.get("p_byte")).doubleValue()); - e.eval("o.publicByte = 16;"); - assertEquals(16, o.publicByte); - } - - @Test - public void accessFieldByteArray() throws ScriptException { - e.eval("var p_byte_array = o.publicByteArray;"); - assertEquals(o.publicByteArray[0], e.eval("o.publicByteArray[0];")); - assertArrayEquals(o.publicByteArray, (byte[])e.get("p_byte_array")); - e.eval("var t_byte_arr = new (Java.type(\"byte[]\"))(3);" + - "t_byte_arr[0] = -18;" + - "t_byte_arr[1] = 56;" + - "t_byte_arr[2] = 60;" + - "o.publicByteArray = t_byte_arr;"); - assertArrayEquals(new byte[] { -18, 56, 60 }, o.publicByteArray); - e.eval("o.publicByteArray[0] = 100;"); - assertEquals(100, o.publicByteArray[0]); - } - - @Test - public void accessStaticFieldByte() throws ScriptException { - e.eval("var ps_byte = SharedObject.publicStaticByte;"); - assertEquals((double)SharedObject.publicStaticByte, ((Number)e.get("ps_byte")).doubleValue()); - e.eval("SharedObject.publicStaticByte = 16;"); - assertEquals(16, SharedObject.publicStaticByte); - } - - @Test - public void accessStaticFieldByteArray() throws ScriptException { - e.eval("var ps_byte_array = SharedObject.publicStaticByteArray;"); - assertEquals(SharedObject.publicStaticByteArray[0], e.eval("SharedObject.publicStaticByteArray[0];")); - assertArrayEquals(SharedObject.publicStaticByteArray, (byte[])e.get("ps_byte_array")); - e.eval("var ts_byte_arr = new (Java.type(\"byte[]\"))(3);" + - "ts_byte_arr[0] = -18;" + - "ts_byte_arr[1] = 56;" + - "ts_byte_arr[2] = 60;" + - "SharedObject.publicStaticByteArray = ts_byte_arr;"); - assertArrayEquals(new byte[] { -18, 56, 60 }, SharedObject.publicStaticByteArray); - e.eval("SharedObject.publicStaticByteArray[0] = -90;"); - assertEquals(-90, SharedObject.publicStaticByteArray[0]); - } - - @Test - public void accessFinalFieldByte() throws ScriptException { - e.eval("var pf_byte = o.publicFinalByte;"); - assertEquals((double)o.publicFinalByte, ((Number)e.get("pf_byte")).doubleValue()); - e.eval("o.publicFinalByte = 16;"); - assertEquals(-7, o.publicFinalByte); - } - - @Test - public void accessFinalFieldByteArray() throws ScriptException { - e.eval("var pf_byte_array = o.publicFinalByteArray;"); - assertEquals(o.publicFinalByteArray[0], e.eval("o.publicFinalByteArray[0];")); - assertArrayEquals(o.publicFinalByteArray, (byte[])e.get("pf_byte_array")); - e.eval("var tf_byte_arr = new (Java.type(\"byte[]\"))(3);" + - "tf_byte_arr[0] = -18;" + - "tf_byte_arr[1] = 56;" + - "tf_byte_arr[2] = 60;" + - "o.publicFinalByteArray = tf_byte_arr;"); - assertArrayEquals(new byte[] { 1, 3, 6, 17, -128 }, o.publicFinalByteArray); - e.eval("o.publicFinalByteArray[0] = -90;"); - assertEquals(-90, o.publicFinalByteArray[0]); - } - - @Test - public void accessStaticFinalFieldByte() throws ScriptException { - e.eval("var psf_byte = SharedObject.publicStaticFinalByte;"); - assertEquals((double)SharedObject.publicStaticFinalByte, ((Number)e.get("psf_byte")).doubleValue()); - e.eval("SharedObject.publicStaticFinalByte = 16;"); - assertEquals(-70, SharedObject.publicStaticFinalByte); - } - - @Test - public void accessStaticFinalFieldByteArray() throws ScriptException { - e.eval("var psf_byte_array = SharedObject.publicStaticFinalByteArray;"); - assertEquals(SharedObject.publicStaticFinalByteArray[0], e.eval("SharedObject.publicStaticFinalByteArray[0];")); - assertArrayEquals(SharedObject.publicStaticFinalByteArray, (byte[])e.get("psf_byte_array")); - e.eval("var tsf_byte_arr = new (Java.type(\"byte[]\"))(3);" + - "tsf_byte_arr[0] = -18;" + - "tsf_byte_arr[1] = 56;" + - "tsf_byte_arr[2] = 60;" + - "SharedObject.publicStaticFinalByteArray = tsf_byte_arr;"); - assertArrayEquals(new byte[] { 17, -128, 81 }, SharedObject.publicStaticFinalByteArray); - e.eval("SharedObject.publicStaticFinalByteArray[0] = -90;"); - assertEquals(-90, SharedObject.publicStaticFinalByteArray[0]); - } - - // --------------------------------short - // tests------------------------------------ - @Test - public void accessFieldShort() throws ScriptException { - e.eval("var p_short = o.publicShort;"); - assertEquals((double)o.publicShort, ((Number)e.get("p_short")).doubleValue()); - e.eval("o.publicShort = 18;"); - assertEquals(18, o.publicShort); - } - - @Test - public void accessFieldShortArray() throws ScriptException { - e.eval("var p_short_array = o.publicShortArray;"); - assertEquals(o.publicShortArray[0], e.eval("o.publicShortArray[0];")); - assertArrayEquals(o.publicShortArray, (short[])e.get("p_short_array")); - e.eval("var t_short_arr = new (Java.type(\"short[]\"))(3);" + - "t_short_arr[0] = 90;" + - "t_short_arr[1] = 5;" + - "t_short_arr[2] = -6000;" + - "o.publicShortArray = t_short_arr;"); - assertArrayEquals(new short[] { 90, 5, -6000 }, o.publicShortArray); - e.eval("o.publicShortArray[0] = -1000;"); - assertEquals(-1000, o.publicShortArray[0]); - } - - @Test - public void accessStaticFieldShort() throws ScriptException { - e.eval("var ps_short = SharedObject.publicStaticShort;"); - assertEquals((double)SharedObject.publicStaticShort, ((Number)e.get("ps_short")).doubleValue()); - e.eval("SharedObject.publicStaticShort = 180;"); - assertEquals(180, SharedObject.publicStaticShort); - } - - @Test - public void accessStaticFieldShortArray() throws ScriptException { - e.eval("var ps_short_array = SharedObject.publicStaticShortArray;"); - assertEquals(SharedObject.publicStaticShortArray[0], e.eval("SharedObject.publicStaticShortArray[0];")); - assertArrayEquals(SharedObject.publicStaticShortArray, (short[])e.get("ps_short_array")); - e.eval("var ts_short_arr = new (Java.type(\"short[]\"))(3);" + - "ts_short_arr[0] = 90;" + - "ts_short_arr[1] = 5;" + - "ts_short_arr[2] = -6000;" + - "SharedObject.publicStaticShortArray = ts_short_arr;"); - assertArrayEquals(new short[] { 90, 5, -6000 }, SharedObject.publicStaticShortArray); - e.eval("SharedObject.publicStaticShortArray[0] = -1000;"); - assertEquals(-1000, SharedObject.publicStaticShortArray[0]); - } - - @Test - public void accessFinalFieldShort() throws ScriptException { - e.eval("var pf_short = o.publicFinalShort;"); - assertEquals((double)o.publicFinalShort, ((Number)e.get("pf_short")).doubleValue()); - e.eval("o.publicFinalShort = 180;"); - assertEquals(31220, o.publicFinalShort); - } - - @Test - public void accessFinalFieldShortArray() throws ScriptException { - e.eval("var pf_short_array = o.publicFinalShortArray;"); - assertEquals(o.publicFinalShortArray[0], e.eval("o.publicFinalShortArray[0];")); - assertArrayEquals(o.publicFinalShortArray, (short[])e.get("pf_short_array")); - e.eval("var tf_short_arr = new (Java.type(\"short[]\"))(3);" + - "tf_short_arr[0] = 90;" + - "tf_short_arr[1] = 5;" + - "tf_short_arr[2] = -6000;" + - "o.publicFinalShortArray = tf_short_arr;"); - assertArrayEquals(new short[] { 12240, 9200, -17289, 1200, 12 }, o.publicFinalShortArray); - e.eval("o.publicFinalShortArray[0] = -1000;"); - assertEquals(-1000, o.publicFinalShortArray[0]); - } - - @Test - public void accessStaticFinalFieldShort() throws ScriptException { - e.eval("var psf_short = SharedObject.publicStaticFinalShort;"); - assertEquals((double)SharedObject.publicStaticFinalShort, ((Number)e.get("psf_short")).doubleValue()); - e.eval("SharedObject.publicStaticFinalShort = 180;"); - assertEquals(8888, SharedObject.publicStaticFinalShort); - } - - @Test - public void accessStaticFinalFieldShortArray() throws ScriptException { - e.eval("var psf_short_array = SharedObject.publicStaticFinalShortArray;"); - assertEquals(SharedObject.publicStaticFinalShortArray[0], e.eval("SharedObject.publicStaticFinalShortArray[0];")); - assertArrayEquals(SharedObject.publicStaticFinalShortArray, (short[])e.get("psf_short_array")); - e.eval("var tsf_short_arr = new (Java.type(\"short[]\"))(3);" + - "tsf_short_arr[0] = 90;" + - "tsf_short_arr[1] = 5;" + - "tsf_short_arr[2] = -6000;" + - "SharedObject.publicStaticFinalShortArray = tsf_short_arr;"); - assertArrayEquals(new short[] { 8240, 9280, -1289, 120, 812 }, SharedObject.publicStaticFinalShortArray); - e.eval("SharedObject.publicStaticFinalShortArray[0] = -1000;"); - assertEquals(-1000, SharedObject.publicStaticFinalShortArray[0]); - } - - // --------------------------------char - // tests------------------------------------ - @Test - public void accessFieldChar() throws ScriptException { - e.eval("var p_char = o.publicChar;"); - assertEquals(o.publicChar, e.get("p_char")); - e.eval("o.publicChar = 'S';"); - assertEquals('S', o.publicChar); - e.eval("o.publicChar = 10;"); - assertEquals(10, o.publicChar); - e.eval("try {" - + " o.publicChar = 'Big string';" + - "} catch(e) {" + - " var isThrown = true;" + - "}"); - assertEquals("Exception thrown", true, e.get("isThrown")); - assertEquals(10, o.publicChar); - } - - @Test - public void accessFieldCharArray() throws ScriptException { - e.eval("var p_char_array = o.publicCharArray;"); - assertEquals(o.publicCharArray[0], e.eval("o.publicCharArray[0];")); - assertArrayEquals(o.publicCharArray, (char[])e.get("p_char_array")); - e.eval("var t_char_arr = new (Java.type(\"char[]\"))(3);" + - "t_char_arr[0] = 'F';" + - "t_char_arr[1] = 'o';" + - "t_char_arr[2] = 'o';" + - "o.publicCharArray = t_char_arr;"); - assertArrayEquals("Foo".toCharArray(), o.publicCharArray); - e.eval("o.publicCharArray[0] = 'Z';"); - assertEquals('Z', o.publicCharArray[0]); - } - - @Test - public void accessStaticFieldChar() throws ScriptException { - e.eval("var ps_char = SharedObject.publicStaticChar;"); - assertEquals(SharedObject.publicStaticChar, e.get("ps_char")); - e.eval("SharedObject.publicStaticChar = 'Z';"); - assertEquals('Z', SharedObject.publicStaticChar); - } - - @Test - public void accessStaticFieldCharArray() throws ScriptException { - e.eval("var ps_char_array = SharedObject.publicStaticCharArray;"); - assertEquals(SharedObject.publicStaticCharArray[0], e.eval("SharedObject.publicStaticCharArray[0];")); - assertArrayEquals(SharedObject.publicStaticCharArray, (char[])e.get("ps_char_array")); - e.eval("var ts_char_arr = new (Java.type(\"char[]\"))(3);" + - "ts_char_arr[0] = 'G';" + - "ts_char_arr[1] = 'o';" + - "ts_char_arr[2] = 'o';" + - "SharedObject.publicStaticCharArray = ts_char_arr;"); - assertArrayEquals("Goo".toCharArray(), SharedObject.publicStaticCharArray); - e.eval("SharedObject.publicStaticCharArray[0] = 'Z';"); - assertEquals('Z', SharedObject.publicStaticCharArray[0]); - } - - @Test - public void accessFinalFieldChar() throws ScriptException { - e.eval("var pf_char = o.publicFinalChar;"); - assertEquals(o.publicFinalChar, e.get("pf_char")); - e.eval("o.publicFinalChar = 'S';"); - assertEquals('E', o.publicFinalChar); - } - - @Test - public void accessFinalCharArray() throws ScriptException { - e.eval("var pf_char_array = o.publicFinalCharArray;"); - assertEquals(o.publicFinalCharArray[0], e.eval("o.publicFinalCharArray[0];")); - assertArrayEquals(o.publicFinalCharArray, (char[])e.get("pf_char_array")); - e.eval("var tf_char_arr = new (Java.type(\"char[]\"))(3);" + - "tf_char_arr[0] = 'F';" + - "tf_char_arr[1] = 'o';" + - "tf_char_arr[2] = 'o';" + - "o.publicFinalCharArray = tf_char_arr;"); - assertArrayEquals("Nashorn hello".toCharArray(), o.publicFinalCharArray); - e.eval("o.publicFinalCharArray[0] = 'Z';"); - assertEquals('Z', o.publicFinalCharArray[0]); - } - - @Test - public void accessStaticFinalFieldChar() throws ScriptException { - e.eval("var psf_char = SharedObject.publicStaticFinalChar;"); - assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char")); - e.eval("SharedObject.publicStaticFinalChar = 'Z';"); - assertEquals('K', SharedObject.publicStaticFinalChar); - } - - @Test - public void accessStaticFinalFieldCharArray() throws ScriptException { - e.eval("var psf_char_array = SharedObject.publicStaticFinalCharArray;"); - assertEquals(SharedObject.publicStaticFinalCharArray[0], e.eval("SharedObject.publicStaticFinalCharArray[0];")); - assertArrayEquals(SharedObject.publicStaticFinalCharArray, (char[])e.get("psf_char_array")); - e.eval("var tsf_char_arr = new (Java.type(\"char[]\"))(3);" + - "tsf_char_arr[0] = 'Z';" + - "tsf_char_arr[1] = 'o';" + - "tsf_char_arr[2] = 'o';" + - "SharedObject.publicStaticFinalCharArray = tsf_char_arr;"); - assertArrayEquals("StaticString".toCharArray(), SharedObject.publicStaticFinalCharArray); - e.eval("SharedObject.publicStaticFinalCharArray[0] = 'Z';"); - assertEquals('Z', SharedObject.publicStaticFinalCharArray[0]); - } - - // --------------------------------float - // tests------------------------------------ - @Test - public void accessFieldFloat() throws ScriptException { - e.eval("var p_float = o.publicFloat;"); - assertEquals((double)o.publicFloat, ((Number)e.get("p_float")).doubleValue()); - o.publicFloat = 0.0f / 0.0f; - assertEquals(true, e.eval("isNaN(o.publicFloat)")); - o.publicFloat = 1.0f / 0.0f; - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicFloat")); - o.publicFloat = -1.0f / 0.0f; - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicFloat")); - e.eval("o.publicFloat = 20;"); - assertEquals(20, o.publicFloat, 1e-10); - e.eval("o.publicFloat = 0.0/0.0;"); - assertTrue(Float.isNaN(o.publicFloat)); - e.eval("o.publicFloat = 1.0/0.0;"); - assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(o.publicFloat)); - e.eval("o.publicFloat = -1.0/0.0;"); - assertEquals(Float.NEGATIVE_INFINITY, o.publicFloat, 1e-10); - } - - @Test - public void accessFieldFloatArray() throws ScriptException { - e.eval("var p_float_array = o.publicFloatArray;"); - assertEquals(o.publicFloatArray[0], e.eval("o.publicFloatArray[0];")); - assertArrayEquals(o.publicFloatArray, (float[])e.get("p_float_array"), 1e-10f); - e.eval("var t_float_arr = new (Java.type(\"float[]\"))(3);" + - "t_float_arr[0] = 9.0;" + - "t_float_arr[1] = 5.12345;" + - "t_float_arr[2] = -60.03;" + - "o.publicFloatArray = t_float_arr;"); - assertArrayEquals(new float[] { 9.0f, 5.12345f, -60.03f }, o.publicFloatArray, 1e-10f); - e.eval("o.publicFloatArray[0] = -513.2;"); - assertArrayEquals(new float[] { -513.2f, 5.12345f, -60.03f }, o.publicFloatArray, 1e-10f); - } - - @Test - public void accessStaticFieldFloat() throws ScriptException { - e.eval("var ps_float = SharedObject.publicStaticFloat;"); - assertEquals((double)SharedObject.publicStaticFloat, ((Number)e.get("ps_float")).doubleValue()); - SharedObject.publicStaticFloat = 0.0f / 0.0f; - assertEquals(true, e.eval("isNaN(SharedObject.publicStaticFloat)")); - SharedObject.publicStaticFloat = 1.0f / 0.0f; - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticFloat")); - SharedObject.publicStaticFloat = -1.0f / 0.0f; - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticFloat")); - e.eval("SharedObject.publicStaticFloat = 20.0;"); - assertEquals(20.0f, SharedObject.publicStaticFloat, 1e-10); - e.eval("SharedObject.publicStaticFloat = 0.0/0.0;"); - assertTrue(Float.isNaN(SharedObject.publicStaticFloat)); - e.eval("SharedObject.publicStaticFloat = 1.0/0.0;"); - assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); - e.eval("SharedObject.publicStaticFloat = -1.0/0.0;"); - assertEquals(Float.floatToIntBits(Float.NEGATIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); - } - - @Test - public void accessStaticFieldFloatArray() throws ScriptException { - e.eval("var ps_float_array = SharedObject.publicStaticFloatArray;"); - assertEquals(SharedObject.publicStaticFloatArray[0], e.eval("SharedObject.publicStaticFloatArray[0];")); - assertArrayEquals(SharedObject.publicStaticFloatArray, (float[])e.get("ps_float_array"), 1e-10f); - e.eval("var ts_float_arr = new (Java.type(\"float[]\"))(3);" + - "ts_float_arr[0] = 9.0;" + - "ts_float_arr[1] = 5.12345;" + - "ts_float_arr[2] = -60.03;" + - "SharedObject.publicStaticFloatArray = ts_float_arr;"); - assertArrayEquals(new float[] { 9.0f, 5.12345f, -60.03f }, SharedObject.publicStaticFloatArray, 1e-10f); - e.eval("SharedObject.publicStaticFloatArray[0] = -513.2;"); - assertArrayEquals(new float[] { -513.2f, 5.12345f, -60.03f }, SharedObject.publicStaticFloatArray, 1e-10f); - } - - @Test - public void accessFinalFloat() throws ScriptException { - e.eval("var pf_float = o.publicFinalFloat;"); - assertEquals((double)o.publicFinalFloat, ((Number)e.get("pf_float")).doubleValue()); - e.eval("o.publicFinalFloat = 20.0;"); - assertEquals(7.72e8f, o.publicFinalFloat, 1e-10); - } - - @Test - public void accessFinalFloatArray() throws ScriptException { - e.eval("var pf_float_array = o.publicFinalFloatArray;"); - assertEquals(o.publicFinalFloatArray[0], e.eval("o.publicFinalFloatArray[0];")); - assertArrayEquals(o.publicFinalFloatArray, (float[])e.get("pf_float_array"), 1e-10f); - e.eval("var tf_float_arr = new (Java.type(\"float[]\"))(3);" + - "tf_float_arr[0] = 9.0;" + - "tf_float_arr[1] = 5.12345;" + - "tf_float_arr[2] = -60.03;" + - "o.publicFinalFloatArray = tf_float_arr;"); - assertArrayEquals(new float[] { -131.012f, 189.32f, -31.32e8f, 3.72f }, o.publicFinalFloatArray, 1e-10f); - e.eval("o.publicFinalFloatArray[0] = -513.2;"); - assertEquals(-513.2f, o.publicFinalFloatArray[0], 1e-10f); - } - - @Test - public void accessStaticFinalFieldFloat() throws ScriptException { - e.eval("var psf_float = SharedObject.publicStaticFinalFloat;"); - assertEquals((double)SharedObject.publicStaticFinalFloat, ((Number)e.get("psf_float")).doubleValue()); - e.eval("SharedObject.publicStaticFinalFloat = 20.0;"); - assertEquals(0.72e8f, SharedObject.publicStaticFinalFloat, 1e-10); - } - - @Test - public void accessStaticFinalFieldFloatArray() throws ScriptException { - e.eval("var psf_float_array = SharedObject.publicStaticFinalFloatArray;"); - assertEquals(SharedObject.publicStaticFinalFloatArray[0], e.eval("SharedObject.publicStaticFinalFloatArray[0];")); - assertArrayEquals(SharedObject.publicStaticFinalFloatArray, (float[])e.get("psf_float_array"), 1e-10f); - e.eval("var tsf_float_arr = new (Java.type(\"float[]\"))(3);" + - "tsf_float_arr[0] = 9.0;" + - "tsf_float_arr[1] = 5.12345;" + - "tsf_float_arr[2] = -60.03;" + - "SharedObject.publicStaticFinalFloatArray = tsf_float_arr;"); - assertArrayEquals(new float[] { -8131.012f, 9.32f, -138.32e8f, 0.72f }, SharedObject.publicStaticFinalFloatArray, 1e-10f); - e.eval("SharedObject.publicStaticFinalFloatArray[0] = -513.2;"); - assertEquals(-513.2f, SharedObject.publicStaticFinalFloatArray[0], 1e-10f); - } - - // --------------------------------double - // tests------------------------------------ - @Test - public void accessFieldDouble() throws ScriptException { - e.eval("var p_double = o.publicDouble;"); - assertEquals(o.publicDouble, e.get("p_double")); - o.publicDouble = 0.0 / 0.0; - assertEquals(true, e.eval("isNaN(o.publicDouble)")); - o.publicDouble = 1.0 / 0.0; - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicDouble")); - o.publicDouble = -1.0 / 0.0; - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicDouble")); - e.eval("o.publicDouble = 30;"); - assertEquals(Double.doubleToLongBits(30.0), Double.doubleToLongBits(o.publicDouble)); - e.eval("o.publicDouble = 0.0/0.0;"); - assertTrue(Double.isNaN(o.publicDouble)); - e.eval("o.publicDouble = 1.0/0.0;"); - assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(o.publicDouble)); - e.eval("o.publicDouble = -1.0/0.0;"); - assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(o.publicDouble)); - } - - @Test - public void accessFieldDoubleArrayRead() throws ScriptException { - e.eval("var p_double_array = o.publicDoubleArray;"); - assertEquals(o.publicDoubleArray[0], e.eval("o.publicDoubleArray[0];")); - assertArrayEquals(o.publicDoubleArray, (double[])e.get("p_double_array"), 1e-10); - e.eval("var t_double_arr = new (Java.type(\"double[]\"))(3);" + - "t_double_arr[0] = 9e10;" + - "t_double_arr[1] = 0.677777;" + - "t_double_arr[2] = -0.0000001;" + - "o.publicDoubleArray = t_double_arr;"); - assertArrayEquals(new double[] { 9e10, 0.677777, -0.0000001 }, o.publicDoubleArray, 1e-10f); - e.eval("o.publicDoubleArray[0] = -5.2e10;"); - assertEquals(-5.2e10, o.publicDoubleArray[0], 1e-10f); - } - - @Test - public void accessStaticFieldDouble() throws ScriptException { - e.eval("var ps_double = SharedObject.publicStaticDouble;"); - assertEquals(SharedObject.publicStaticDouble, e.get("ps_double")); - SharedObject.publicStaticDouble = 0.0 / 0.0; - assertEquals(true, e.eval("isNaN(SharedObject.publicStaticDouble)")); - SharedObject.publicStaticDouble = 1.0 / 0.0; - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticDouble")); - SharedObject.publicStaticDouble = -1.0 / 0.0; - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticDouble")); - e.eval("SharedObject.publicStaticDouble = 40.0;"); - assertEquals(Double.doubleToLongBits(40.0), Double.doubleToLongBits(SharedObject.publicStaticDouble)); - e.eval("SharedObject.publicStaticDouble = 0.0/0.0;"); - assertTrue(Double.isNaN(SharedObject.publicStaticDouble)); - e.eval("SharedObject.publicStaticDouble = 1.0/0.0;"); - assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); - e.eval("SharedObject.publicStaticDouble = -1.0/0.0;"); - assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); - } - - @Test - public void accessStaticFieldDoubleArrayRead() throws ScriptException { - e.eval("var ps_double_array = SharedObject.publicStaticDoubleArray;"); - assertEquals(SharedObject.publicStaticDoubleArray[0], e.eval("SharedObject.publicStaticDoubleArray[0];")); - assertArrayEquals(SharedObject.publicStaticDoubleArray, (double[])e.get("ps_double_array"), 1e-10); - e.eval("var ts_double_arr = new (Java.type(\"double[]\"))(3);" + - "ts_double_arr[0] = 9e10;" + - "ts_double_arr[1] = 0.677777;" + - "ts_double_arr[2] = -0.0000001;" + - "SharedObject.publicStaticDoubleArray = ts_double_arr;"); - assertArrayEquals(new double[] { 9e10, 0.677777, -0.0000001 }, SharedObject.publicStaticDoubleArray, 1e-10f); - e.eval("SharedObject.publicStaticDoubleArray[0] = -5.2e10;"); - assertEquals(-5.2e10, SharedObject.publicStaticDoubleArray[0], 1e-10f); - } - - @Test - public void accessFinalFieldDouble() throws ScriptException { - e.eval("var pf_double = o.publicFinalDouble;"); - assertEquals(o.publicFinalDouble, e.get("pf_double")); - e.eval("o.publicFinalDouble = 30.0;"); - assertEquals(Double.doubleToLongBits(1.3412e20), Double.doubleToLongBits(o.publicFinalDouble)); - } - - @Test - public void accessFinalFieldDoubleArrayRead() throws ScriptException { - e.eval("var pf_double_array = o.publicFinalDoubleArray;"); - assertEquals(o.publicFinalDoubleArray[0], e.eval("o.publicFinalDoubleArray[0];")); - assertArrayEquals(o.publicFinalDoubleArray, (double[])e.get("pf_double_array"), 1e-10); - e.eval("var tf_double_arr = new (Java.type(\"double[]\"))(3);" + - "tf_double_arr[0] = 9e10;" + - "tf_double_arr[1] = 0.677777;" + - "tf_double_arr[2] = -0.0000001;" + - "o.publicFinalDoubleArray = tf_double_arr;"); - assertArrayEquals(new double[] { 0.725e80, 0.12e10, 8e-3, 1.00077 }, o.publicFinalDoubleArray, 1e-10f); - e.eval("o.publicFinalDoubleArray[0] = -5.2e10;"); - assertEquals(-5.2e10, o.publicFinalDoubleArray[0], 1e-10f); - } - - @Test - public void accessStaticFinalFieldDouble() throws ScriptException { - e.eval("var psf_double = SharedObject.publicStaticFinalDouble;"); - assertEquals(SharedObject.publicStaticFinalDouble, e.get("psf_double")); - e.eval("SharedObject.publicStaticFinalDouble = 40.0;"); - assertEquals(Double.doubleToLongBits(1.8e12), Double.doubleToLongBits(SharedObject.publicStaticFinalDouble)); - } - - @Test - public void accessStaticFinalFieldDoubleArrayRead() throws ScriptException { - e.eval("var psf_double_array = SharedObject.publicStaticFinalDoubleArray;"); - assertEquals(SharedObject.publicStaticFinalDoubleArray[0], e.eval("SharedObject.publicStaticFinalDoubleArray[0];")); - assertArrayEquals(SharedObject.publicStaticFinalDoubleArray, (double[])e.get("psf_double_array"), 1e-10); - e.eval("var tsf_double_arr = new (Java.type(\"double[]\"))(3);" + - "tsf_double_arr[0] = 9e10;" + - "tsf_double_arr[1] = 0.677777;" + - "tsf_double_arr[2] = -0.0000001;" + - "SharedObject.publicStaticFinalDoubleArray = tsf_double_arr;"); - assertArrayEquals(new double[] { 8.725e80, 0.82e10, 18e-3, 1.08077 }, SharedObject.publicStaticFinalDoubleArray, 1e-10f); - e.eval("SharedObject.publicStaticFinalDoubleArray[0] = -5.2e10;"); - assertEquals(-5.2e10, SharedObject.publicStaticFinalDoubleArray[0], 1e-10f); - } - -}
--- a/test/src/jdk/nashorn/api/javaaccess/NumberBoxingTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,365 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertTrue; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -/** - * @test - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.NumberBoxingTest - * @run testng/othervm jdk.nashorn.api.javaaccess.NumberBoxingTest - */ -@SuppressWarnings("javadoc") -public class NumberBoxingTest { - - private static ScriptEngine e; - private static SharedObject o; - - public static void main(final String[] args) { - TestNG.main(args); - } - - @BeforeClass - public static void setUpClass() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - e = m.getEngineByName("nashorn"); - o = new SharedObject(); - e.put("o", o); - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); - } - - @AfterClass - public static void tearDownClass() { - e = null; - o = null; - } - - // --------------------------------long - // tests------------------------------------ - @Test - public void accessFieldLongBoxing() throws ScriptException { - e.eval("var p_long = o.publicLongBox;"); - assertEquals(o.publicLongBox, e.get("p_long")); - e.eval("o.publicLongBox = 12;"); - assertEquals(Long.valueOf(12), o.publicLongBox); - } - - @Test - public void accessStaticFieldLongBoxing() throws ScriptException { - e.eval("var ps_long = SharedObject.publicStaticLongBox;"); - assertEquals(SharedObject.publicStaticLongBox, e.get("ps_long")); - e.eval("SharedObject.publicStaticLongBox = 120;"); - assertEquals(120L, SharedObject.publicStaticLongBox.longValue()); - } - - @Test - public void accessFinalFieldLongBoxing() throws ScriptException { - e.eval("var pf_long = o.publicFinalLongBox;"); - assertEquals(o.publicFinalLongBox, e.get("pf_long")); - e.eval("o.publicFinalLongBox = 120;"); - assertEquals(Long.valueOf(9377333334L), o.publicFinalLongBox); - } - - @Test - public void accessStaticFinalFieldLongBoxing() throws ScriptException { - e.eval("var psf_long = SharedObject.publicStaticFinalLong;"); - assertEquals(SharedObject.publicStaticFinalLong, e.get("psf_long")); - e.eval("SharedObject.publicStaticFinalLong = 120;"); - assertEquals(8333333333333L, SharedObject.publicStaticFinalLong); - } - - // --------------------------------int - // tests------------------------------------ - @Test - public void accessFieldIntBoxing() throws ScriptException { - e.eval("var p_int = o.publicIntBox;"); - assertEquals(o.publicIntBox, e.get("p_int")); - e.eval("o.publicIntBox = 14;"); - assertEquals(Integer.valueOf(14), o.publicIntBox); - } - - @Test - public void accessStaticFieldIntBoxing() throws ScriptException { - e.eval("var ps_int = SharedObject.publicStaticInt;"); - assertEquals(SharedObject.publicStaticInt, e.get("ps_int")); - e.eval("SharedObject.publicStaticInt = 140;"); - assertEquals(140, SharedObject.publicStaticInt); - } - - @Test - public void accessFinalFieldIntBoxing() throws ScriptException { - e.eval("var pf_int = o.publicFinalIntBox;"); - assertEquals(o.publicFinalIntBox, e.get("pf_int")); - e.eval("o.publicFinalIntBox = 10;"); - assertEquals(Integer.valueOf(207512301), o.publicFinalIntBox); - } - - @Test - public void accessStaticFinalFieldIntBoxing() throws ScriptException { - e.eval("var psf_int = SharedObject.publicStaticFinalInt;"); - assertEquals(SharedObject.publicStaticFinalInt, e.get("psf_int")); - e.eval("SharedObject.publicStaticFinalInt = 140;"); - assertEquals(207182023, SharedObject.publicStaticFinalInt); - } - - // --------------------------------byte - // tests------------------------------------ - @Test - public void accessFieldByteBoxing() throws ScriptException { - e.eval("var p_byte = o.publicByteBox;"); - assertEqualsDouble(o.publicByteBox, "p_byte"); - e.eval("o.publicByteBox = 16;"); - assertEquals(Byte.valueOf((byte)16), o.publicByteBox); - } - - @Test - public void accessStaticFieldByteBoxing() throws ScriptException { - e.eval("var ps_byte = SharedObject.publicStaticByte;"); - assertEqualsDouble(SharedObject.publicStaticByte, "ps_byte"); - e.eval("SharedObject.publicStaticByte = 16;"); - assertEquals(16, SharedObject.publicStaticByte); - } - - @Test - public void accessFinalFieldByteBoxing() throws ScriptException { - e.eval("var pf_byte = o.publicFinalByteBox;"); - assertEqualsDouble(o.publicFinalByteBox, "pf_byte"); - e.eval("o.publicFinalByteBox = 16;"); - assertEquals(Byte.valueOf((byte)19), o.publicFinalByteBox); - } - - @Test - public void accessStaticFinalFieldByteBoxing() throws ScriptException { - e.eval("var psf_byte = SharedObject.publicStaticFinalByte;"); - assertEqualsDouble(SharedObject.publicStaticFinalByte, "psf_byte"); - e.eval("SharedObject.publicStaticFinalByte = 16;"); - assertEquals(-70, SharedObject.publicStaticFinalByte); - } - - // --------------------------------short - // tests------------------------------------ - @Test - public void accessFieldShortBoxing() throws ScriptException { - e.eval("var p_short = o.publicShortBox;"); - assertEqualsDouble(o.publicShortBox, "p_short"); - e.eval("o.publicShortBox = 18;"); - assertEquals(Short.valueOf((short)18), o.publicShortBox); - } - - private static void assertEqualsDouble(final Number n, final String name) { - assertEquals(n.doubleValue(), ((Number)e.get(name)).doubleValue()); - } - - @Test - public void accessStaticFieldShortBoxing() throws ScriptException { - e.eval("var ps_short = SharedObject.publicStaticShort;"); - assertEqualsDouble(SharedObject.publicStaticShort, "ps_short"); - e.eval("SharedObject.publicStaticShort = 180;"); - assertEquals(180, SharedObject.publicStaticShort); - } - - @Test - public void accessFinalFieldShortBoxing() throws ScriptException { - e.eval("var pf_short = o.publicFinalShortBox;"); - assertEqualsDouble(o.publicFinalShortBox, "pf_short"); - e.eval("o.publicFinalShortBox = 180;"); - assertEquals(Short.valueOf((short)-26777), o.publicFinalShortBox); - } - - @Test - public void accessStaticFinalFieldShortBoxing() throws ScriptException { - e.eval("var psf_short = SharedObject.publicStaticFinalShort;"); - assertEqualsDouble(SharedObject.publicStaticFinalShort, "psf_short"); - e.eval("SharedObject.publicStaticFinalShort = 180;"); - assertEquals(8888, SharedObject.publicStaticFinalShort); - } - - // --------------------------------char - // tests------------------------------------ - @Test - public void accessFieldCharBoxing() throws ScriptException { - e.eval("var p_char = o.publicCharBox;"); - assertEquals(o.publicCharBox, e.get("p_char")); - e.eval("o.publicCharBox = 'S';"); - assertEquals(Character.valueOf('S'), o.publicCharBox); - e.eval("try {" + - " o.publicCharBox = 'Big string';" + - "} catch(e) {" + - " var isThrown = true;" + - "}"); - assertEquals("Exception thrown", true, e.get("isThrown")); - assertEquals(Character.valueOf('S'), o.publicCharBox); - } - - @Test - public void accessStaticFieldCharBoxing() throws ScriptException { - e.eval("var ps_char = SharedObject.publicStaticChar;"); - assertEquals(SharedObject.publicStaticChar, e.get("ps_char")); - e.eval("SharedObject.publicStaticChar = 'Z';"); - assertEquals('Z', SharedObject.publicStaticChar); - } - - @Test - public void accessFinalFieldCharBoxing() throws ScriptException { - e.eval("var pf_char = o.publicFinalCharBox;"); - assertEquals(o.publicFinalCharBox, e.get("pf_char")); - e.eval("o.publicFinalCharBox = 'S';"); - assertEquals(Character.valueOf('F'), o.publicFinalCharBox); - } - - @Test - public void accessStaticFinalFieldCharBoxing() throws ScriptException { - e.eval("var psf_char = SharedObject.publicStaticFinalChar;"); - assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char")); - e.eval("SharedObject.publicStaticFinalChar = 'Z';"); - assertEquals('K', SharedObject.publicStaticFinalChar); - } - - // --------------------------------float - // tests------------------------------------ - @Test - public void accessFieldFloatBoxing() throws ScriptException { - e.eval("var p_float = o.publicFloatBox;"); - assertEqualsDouble(o.publicFloatBox, "p_float"); - o.publicFloatBox = 0.0f / 0.0f; - assertEquals(true, e.eval("isNaN(o.publicFloatBox)")); - o.publicFloatBox = 1.0f / 0.0f; - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicFloatBox")); - o.publicFloatBox = -1.0f / 0.0f; - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicFloatBox")); - e.eval("o.publicFloatBox = 20;"); - assertEquals(20, o.publicFloatBox, 1e-10); - e.eval("o.publicFloatBox = 0.0/0.0;"); - assertTrue(Float.isNaN(o.publicFloatBox)); - e.eval("o.publicFloatBox = 1.0/0.0;"); - assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(o.publicFloatBox)); - e.eval("o.publicFloatBox = -1.0/0.0;"); - assertEquals(Float.NEGATIVE_INFINITY, o.publicFloatBox, 1e-10); - } - - @Test - public void accessStaticFieldFloatBoxing() throws ScriptException { - e.eval("var ps_float = SharedObject.publicStaticFloat;"); - assertEqualsDouble(SharedObject.publicStaticFloat, "ps_float"); - SharedObject.publicStaticFloat = 0.0f / 0.0f; - assertEquals(true, e.eval("isNaN(SharedObject.publicStaticFloat)")); - SharedObject.publicStaticFloat = 1.0f / 0.0f; - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticFloat")); - SharedObject.publicStaticFloat = -1.0f / 0.0f; - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticFloat")); - e.eval("SharedObject.publicStaticFloat = 20.0;"); - assertEquals(20.0f, SharedObject.publicStaticFloat, 1e-10); - e.eval("SharedObject.publicStaticFloat = 0.0/0.0;"); - assertTrue(Float.isNaN(SharedObject.publicStaticFloat)); - e.eval("SharedObject.publicStaticFloat = 1.0/0.0;"); - assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); - e.eval("SharedObject.publicStaticFloat = -1.0/0.0;"); - assertEquals(Float.floatToIntBits(Float.NEGATIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); - } - - @Test - public void accessFinalFloatBoxing() throws ScriptException { - e.eval("var pf_float = o.publicFinalFloatBox;"); - assertEqualsDouble(o.publicFinalFloatBox, "pf_float"); - e.eval("o.publicFinalFloatBox = 20.0;"); - assertEquals(1.372e4f, o.publicFinalFloatBox, 1e-10); - } - - @Test - public void accessStaticFinalFieldFloatBoxing() throws ScriptException { - e.eval("var psf_float = SharedObject.publicStaticFinalFloat;"); - assertEqualsDouble(SharedObject.publicStaticFinalFloat, "psf_float"); - e.eval("SharedObject.publicStaticFinalFloat = 20.0;"); - assertEquals(0.72e8f, SharedObject.publicStaticFinalFloat, 1e-10); - } - - // --------------------------------double - // tests------------------------------------ - @Test - public void accessFieldDoubleBoxing() throws ScriptException { - e.eval("var p_double = o.publicDoubleBox;"); - assertEquals(o.publicDoubleBox, e.get("p_double")); - o.publicDoubleBox = 0.0 / 0.0; - assertEquals(true, e.eval("isNaN(o.publicDoubleBox)")); - o.publicDoubleBox = 1.0 / 0.0; - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicDoubleBox")); - o.publicDoubleBox = -1.0 / 0.0; - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicDoubleBox")); - e.eval("o.publicDoubleBox = 30;"); - assertEquals(Double.doubleToLongBits(30.0), Double.doubleToLongBits(o.publicDoubleBox)); - e.eval("o.publicDoubleBox = 0.0/0.0;"); - assertTrue(Double.isNaN(o.publicDoubleBox)); - e.eval("o.publicDoubleBox = 1.0/0.0;"); - assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(o.publicDoubleBox)); - e.eval("o.publicDoubleBox = -1.0/0.0;"); - assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(o.publicDoubleBox)); - } - - @Test - public void accessStaticFieldDoubleBoxing() throws ScriptException { - e.eval("var ps_double = SharedObject.publicStaticDouble;"); - assertEquals(SharedObject.publicStaticDouble, e.get("ps_double")); - SharedObject.publicStaticDouble = 0.0 / 0.0; - assertEquals(true, e.eval("isNaN(SharedObject.publicStaticDouble)")); - SharedObject.publicStaticDouble = 1.0 / 0.0; - assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticDouble")); - SharedObject.publicStaticDouble = -1.0 / 0.0; - assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticDouble")); - e.eval("SharedObject.publicStaticDouble = 40.0;"); - assertEquals(Double.doubleToLongBits(40.0), Double.doubleToLongBits(SharedObject.publicStaticDouble)); - e.eval("SharedObject.publicStaticDouble = 0.0/0.0;"); - assertTrue(Double.isNaN(SharedObject.publicStaticDouble)); - e.eval("SharedObject.publicStaticDouble = 1.0/0.0;"); - assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); - e.eval("SharedObject.publicStaticDouble = -1.0/0.0;"); - assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); - } - - @Test - public void accessFinalFieldDoubleBoxing() throws ScriptException { - e.eval("var pf_double = o.publicFinalDoubleBox;"); - assertEquals(o.publicFinalDoubleBox, e.get("pf_double")); - e.eval("o.publicFinalDoubleBox = 30.0;"); - assertEquals(Double.doubleToLongBits(1.412e-12), Double.doubleToLongBits(o.publicFinalDoubleBox)); - } - - @Test - public void accessStaticFinalFieldDoubleBoxing() throws ScriptException { - e.eval("var psf_double = SharedObject.publicStaticFinalDouble;"); - assertEquals(SharedObject.publicStaticFinalDouble, e.get("psf_double")); - e.eval("SharedObject.publicStaticFinalDouble = 40.0;"); - assertEquals(Double.doubleToLongBits(1.8e12), Double.doubleToLongBits(SharedObject.publicStaticFinalDouble)); - } - -}
--- a/test/src/jdk/nashorn/api/javaaccess/ObjectAccessTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,165 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -/** - * @test - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.ObjectAccessTest - * @run testng/othervm jdk.nashorn.api.javaaccess.ObjectAccessTest - */ -@SuppressWarnings("javadoc") -public class ObjectAccessTest { - - private static ScriptEngine e = null; - private static SharedObject o = null; - - public static void main(final String[] args) { - TestNG.main(args); - } - - @BeforeClass - public static void setUpClass() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - e = m.getEngineByName("nashorn"); - o = new SharedObject(); - e.put("o", o); - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); - e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;"); - } - - @AfterClass - public static void tearDownClass() { - e = null; - o = null; - } - - @Test - public void accessFieldObject() throws ScriptException { - e.eval("var p_object = o.publicObject;"); - assertEquals(o.publicObject, e.get("p_object")); - assertEquals("object", e.eval("typeof p_object;")); - e.eval("o.publicObject = new Person(14);"); - assertEquals(new Person(14), o.publicObject); - } - - @Test - public void accessFieldObjectArray() throws ScriptException { - e.eval("var p_object_array = o.publicObjectArray;"); - assertEquals(o.publicObjectArray[0], e.eval("o.publicObjectArray[0]")); - assertArrayEquals(o.publicObjectArray, (Object[])e.get("p_object_array")); - e.eval("var t_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + - "t_object_arr[0] = new Person(100);" + - "t_object_arr[1] = new Person(120);" + - "t_object_arr[2] = new Person(140);" + - "o.publicObjectArray = t_object_arr;"); - assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, o.publicObjectArray); - e.eval("o.publicObjectArray[0] = new Person(10);"); - assertEquals(new Person(10), o.publicObjectArray[0]); - } - - @Test - public void accessStaticFieldObject() throws ScriptException { - e.eval("var ps_object = SharedObject.publicStaticObject;"); - assertEquals(SharedObject.publicStaticObject, e.get("ps_object")); - assertEquals("object", e.eval("typeof ps_object;")); - e.eval("SharedObject.publicStaticObject = new Person(16);"); - assertEquals(new Person(16), SharedObject.publicStaticObject); - } - - @Test - public void accessStaticFieldObjectArray() throws ScriptException { - e.eval("var ps_object_array = SharedObject.publicStaticObjectArray;"); - assertEquals(SharedObject.publicStaticObjectArray[0], e.eval("SharedObject.publicStaticObjectArray[0]")); - assertArrayEquals(SharedObject.publicStaticObjectArray, (Object[])e.get("ps_object_array")); - e.eval("var ts_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + - "ts_object_arr[0] = new Person(100);" + - "ts_object_arr[1] = new Person(120);" + - "ts_object_arr[2] = new Person(140);" + - "SharedObject.publicStaticObjectArray = ts_object_arr;"); - assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, SharedObject.publicStaticObjectArray); - e.eval("SharedObject.publicStaticObjectArray[0] = new Person(10);"); - assertEquals(new Person(10), SharedObject.publicStaticObjectArray[0]); - } - - @Test - public void accessFinalFieldObject() throws ScriptException { - e.eval("var pf_object = o.publicFinalObject;"); - assertEquals(o.publicFinalObject, e.get("pf_object")); - assertEquals("object", e.eval("typeof pf_object;")); - e.eval("o.publicFinalObject = new Person(-999);"); - assertEquals(new Person(1024), o.publicFinalObject); - } - - @Test - public void accessFinalFieldObjectArray() throws ScriptException { - e.eval("var pf_object_array = o.publicFinalObjectArray;"); - assertEquals(o.publicFinalObjectArray[0], e.eval("o.publicFinalObjectArray[0]")); - assertArrayEquals(o.publicFinalObjectArray, (Object[])e.get("pf_object_array")); - e.eval("var tf_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + - "tf_object_arr[0] = new Person(100);" + - "tf_object_arr[1] = new Person(120);" + - "tf_object_arr[2] = new Person(140);" + - "o.publicOFinalbjectArray = tf_object_arr;"); - assertArrayEquals(new Person[] { new Person(-900), new Person(1000), new Person(180) }, o.publicFinalObjectArray); - e.eval("o.publicFinalObjectArray[0] = new Person(10);"); - assertEquals(new Person(10), o.publicFinalObjectArray[0]); - } - - @Test - public void accessStaticFinalFieldObject() throws ScriptException { - e.eval("var psf_object = SharedObject.publicStaticFinalObject;"); - assertEquals(SharedObject.publicStaticFinalObject, e.get("psf_object")); - assertEquals("object", e.eval("typeof psf_object;")); - e.eval("SharedObject.publicStaticFinalObject = new Person(6);"); - assertEquals(new Person(2048), SharedObject.publicStaticFinalObject); - } - - @Test - public void accessStaticFinalFieldObjectArray() throws ScriptException { - e.eval("var psf_object_array = SharedObject.publicStaticFinalObjectArray;"); - assertEquals(SharedObject.publicStaticFinalObjectArray[0], e.eval("SharedObject.publicStaticFinalObjectArray[0]")); - assertArrayEquals(SharedObject.publicStaticFinalObjectArray, (Object[])e.get("psf_object_array")); - e.eval("var tsf_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + - "tsf_object_arr[0] = new Person(100);" + - "tsf_object_arr[1] = new Person(120);" + - "tsf_object_arr[2] = new Person(140);" + - "SharedObject.publicStaticFinalObjectArray = tsf_object_arr;"); - assertArrayEquals(new Person[] { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }, SharedObject.publicStaticFinalObjectArray); - e.eval("SharedObject.publicStaticFinalObjectArray[0] = new Person(90);"); - assertEquals(new Person(90), SharedObject.publicStaticFinalObjectArray[0]); - } - -}
--- a/test/src/jdk/nashorn/api/javaaccess/Person.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -@SuppressWarnings("javadoc") -public class Person { - - public int id = 0; - - public Person() { - } - - public Person(final int code) { - this.id = code; - } - - @Override - public boolean equals(final Object obj) { - if (obj != null && obj instanceof Person) { - final Person o = (Person)obj; - return this.id == o.id; - } - return false; - } - - @Override - public int hashCode() { - return id; - } - - @Override - public String toString() { - return "Person(" + id + ")"; - } - -}
--- a/test/src/jdk/nashorn/api/javaaccess/SharedObject.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,467 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import javax.script.Invocable; -import javax.script.ScriptEngine; -import javax.script.ScriptException; - -@SuppressWarnings("javadoc") -public class SharedObject { - - // Public fields - public String publicString = "PublicString"; - public String[] publicStringArray = { "ArrayString[0]", "ArrayString[1]", "ArrayString[2]", "ArrayString[3]" }; - public Person publicObject = new Person(256); - public Person[] publicObjectArray = { new Person(4), new Person(-422), new Person(14) }; - public boolean publicBoolean = true; - public boolean[] publicBooleanArray = { true, false, false, true }; - public Boolean publicBooleanBox = true; - public long publicLong = 933333333333333333L; - public long[] publicLongArray = { 99012333333333L, -124355555L, 89777777777L }; - public Long publicLongBox = 9333333333L; - public int publicInt = 2076543123; - public int[] publicIntArray = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; - public Integer publicIntBox = 20765123; - public byte publicByte = -128; - public byte[] publicByteArray = { 1, 2, 4, 8, 16, 32, 64, 127, -128 }; - public Byte publicByteBox = 127; - public short publicShort = 32000; - public short[] publicShortArray = { 3240, 8900, -16789, 1, 12 }; - public Short publicShortBox = Short.MIN_VALUE; - public float publicFloat = 0.7f; - public float[] publicFloatArray = { -32.01f, 89.3f, -1.3e8f, 3.1f }; - public Float publicFloatBox = 1.377e4f; - public double publicDouble = 1.34e20; - public double[] publicDoubleArray = { 0.75e80, 8e-43, 1.000077, 0.123e10 }; - public Double publicDoubleBox = 1.4e-19; - public char publicChar = 'A'; - public char[] publicCharArray = "Hello Nashorn".toCharArray(); - public Character publicCharBox = 'B'; - // Public static fields - public static String publicStaticString = "PublicStaticString"; - public static String[] publicStaticStringArray = { "StaticArrayString[0]", "StaticArrayString[1]", "StaticArrayString[2]", "StaticArrayString[3]" }; - public static Person publicStaticObject = new Person(512); - public static Person[] publicStaticObjectArray = { new Person(40), new Person(-22), new Person(18) }; - public static boolean publicStaticBoolean = true; - public static boolean[] publicStaticBooleanArray = { false, false, false, true }; - public static Boolean publicStaticBooleanBox = true; - public static long publicStaticLong = 13333333333333333L; - public static long[] publicStaticLongArray = { 19012333333333L, -224355555L, 39777777777L }; - public static Long publicStaticLongBox = 9333333334L; - public static int publicStaticInt = 207654323; - public static int[] publicStaticIntArray = { 5, 8, 13, 21, 34 }; - public static Integer publicStaticIntBox = 2075123; - public static byte publicStaticByte = -12; - public static byte[] publicStaticByteArray = { 16, 32, 64, 127, -128 }; - public static Byte publicStaticByteBox = 17; - public static short publicStaticShort = 320; - public static short[] publicStaticShortArray = { 1240, 900, -1789, 100, 12 }; - public static Short publicStaticShortBox = -16777; - public static float publicStaticFloat = 7.7e8f; - public static float[] publicStaticFloatArray = { -131.01f, 189.3f, -31.3e8f, 3.7f }; - public static Float publicStaticFloatBox = 1.37e4f; - public static double publicStaticDouble = 1.341e20; - public static double[] publicStaticDoubleArray = { 0.75e80, 0.123e10, 8e-43, 1.000077 }; - public static Double publicStaticDoubleBox = 1.41e-12; - public static char publicStaticChar = 'C'; - public static char[] publicStaticCharArray = "Nashorn".toCharArray(); - public static Character publicStaticCharBox = 'D'; - // Public final fields - public final String publicFinalString = "PublicFinalString"; - public final String[] publicFinalStringArray = { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" }; - public final Person publicFinalObject = new Person(1024); - public final Person[] publicFinalObjectArray = { new Person(-900), new Person(1000), new Person(180) }; - public final boolean publicFinalBoolean = true; - public final boolean[] publicFinalBooleanArray = { false, false, true, false }; - public final Boolean publicFinalBooleanBox = true; - public final long publicFinalLong = 13353333333333333L; - public final long[] publicFinalLongArray = { 1901733333333L, -2247355555L, 3977377777L }; - public final Long publicFinalLongBox = 9377333334L; - public final int publicFinalInt = 20712023; - public final int[] publicFinalIntArray = { 50, 80, 130, 210, 340 }; - public final Integer publicFinalIntBox = 207512301; - public final byte publicFinalByte = -7; - public final byte[] publicFinalByteArray = { 1, 3, 6, 17, -128 }; - public final Byte publicFinalByteBox = 19; - public final short publicFinalShort = 31220; - public final short[] publicFinalShortArray = { 12240, 9200, -17289, 1200, 12 }; - public final Short publicFinalShortBox = -26777; - public final float publicFinalFloat = 7.72e8f; - public final float[] publicFinalFloatArray = { -131.012f, 189.32f, -31.32e8f, 3.72f }; - public final Float publicFinalFloatBox = 1.372e4f; - public final double publicFinalDouble = 1.3412e20; - public final double[] publicFinalDoubleArray = { 0.725e80, 0.12e10, 8e-3, 1.00077 }; - public final Double publicFinalDoubleBox = 1.412e-12; - public final char publicFinalChar = 'E'; - public final char[] publicFinalCharArray = "Nashorn hello".toCharArray(); - public final Character publicFinalCharBox = 'F'; - // Public static final fields - public static final String publicStaticFinalString = "PublicStaticFinalString"; - public static final String[] publicStaticFinalStringArray = { "StaticFinalArrayString[0]", "StaticFinalArrayString[1]", "StaticFinalArrayString[2]", "StaticFinalArrayString[3]" }; - public static final Person publicStaticFinalObject = new Person(2048); - public static final Person[] publicStaticFinalObjectArray = { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }; - public static final boolean publicStaticFinalBoolean = true; - public static final boolean[] publicStaticFinalBooleanArray = { false, true, false, false }; - public static final Boolean publicStaticFinalBooleanBox = true; - public static final long publicStaticFinalLong = 8333333333333L; - public static final long[] publicStaticFinalLongArray = { 19017383333L, -2247358L, 39773787L }; - public static final Long publicStaticFinalLongBox = 9377388334L; - public static final int publicStaticFinalInt = 207182023; - public static final int[] publicStaticFinalIntArray = { 1308, 210, 340 }; - public static final Integer publicStaticFinalIntBox = 2078301; - public static final byte publicStaticFinalByte = -70; - public static final byte[] publicStaticFinalByteArray = { 17, -128, 81 }; - public static final Byte publicStaticFinalByteBox = 91; - public static final short publicStaticFinalShort = 8888; - public static final short[] publicStaticFinalShortArray = { 8240, 9280, -1289, 120, 812 }; - public static final Short publicStaticFinalShortBox = -26; - public static final float publicStaticFinalFloat = 0.72e8f; - public static final float[] publicStaticFinalFloatArray = { -8131.012f, 9.32f, -138.32e8f, 0.72f }; - public static final Float publicStaticFinalFloatBox = 1.2e4f; - public static final double publicStaticFinalDouble = 1.8e12; - public static final double[] publicStaticFinalDoubleArray = { 8.725e80, 0.82e10, 18e-3, 1.08077 }; - public static final Double publicStaticFinalDoubleBox = 1.5612e-13; - public static final char publicStaticFinalChar = 'K'; - public static final char[] publicStaticFinalCharArray = "StaticString".toCharArray(); - public static final Character publicStaticFinalCharBox = 'L'; - - // Special vars - public volatile boolean volatileBoolean = true; - public transient boolean transientBoolean = true; - - // For methods testing - public boolean isAccessed = false; - public volatile boolean isFinished = false; - - private ScriptEngine engine; - - public ScriptEngine getEngine() { - return engine; - } - - public void setEngine(final ScriptEngine engine) { - this.engine = engine; - } - - public void voidMethod() { - isAccessed = true; - } - - public boolean booleanMethod(final boolean arg) { - return !arg; - } - - public Boolean booleanBoxingMethod(final Boolean arg) { - return !arg.booleanValue(); - } - - public boolean[] booleanArrayMethod(final boolean arg[]) { - final boolean[] res = new boolean[arg.length]; - for (int i = 0; i < arg.length; i++) { - res[i] = !arg[i]; - } - return res; - } - - public int intMethod(final int arg) { - return arg + arg; - } - - public Integer intBoxingMethod(final Integer arg) { - return arg + arg; - } - - public int[] intArrayMethod(final int arg[]) { - final int[] res = new int[arg.length]; - for (int i = 0; i < arg.length; i++) { - res[i] = arg[i] * 2; - } - return res; - } - - public long longMethod(final long arg) { - return arg + arg; - } - - public Long longBoxingMethod(final Long arg) { - return arg + arg; - } - - public long[] longArrayMethod(final long[] arg) { - final long[] res = new long[arg.length]; - for (int i = 0; i < arg.length; i++) { - res[i] = arg[i] * 2; - } - return res; - } - - public byte byteMethod(final byte arg) { - return (byte)(arg + arg); - } - - public Byte byteBoxingMethod(final Byte arg) { - return (byte)(arg + arg); - } - - public byte[] byteArrayMethod(final byte[] arg) { - final byte[] res = new byte[arg.length]; - for (int i = 0; i < arg.length; i++) { - res[i] = (byte)(arg[i] * 2); - } - return res; - } - - public char charMethod(final char arg) { - return Character.toUpperCase(arg); - } - - public Character charBoxingMethod(final Character arg) { - return Character.toUpperCase(arg); - } - - public char[] charArrayMethod(final char[] arg) { - final char[] res = new char[arg.length]; - for (int i = 0; i < arg.length; i++) { - res[i] = Character.toUpperCase(arg[i]); - } - return res; - } - - public short shortMethod(final short arg) { - return (short)(arg + arg); - } - - public Short shortBoxingMethod(final Short arg) { - return (short)(arg + arg); - } - - public short[] shortArrayMethod(final short[] arg) { - final short[] res = new short[arg.length]; - for (int i = 0; i < arg.length; i++) { - res[i] = (short)(arg[i] * 2); - } - return res; - } - - public float floatMethod(final float arg) { - return arg + arg; - } - - public Float floatBoxingMethod(final Float arg) { - return arg + arg; - } - - public float[] floatArrayMethod(final float[] arg) { - final float[] res = new float[arg.length]; - for (int i = 0; i < arg.length; i++) { - res[i] = arg[i] * 2; - } - return res; - } - - public double doubleMethod(final double arg) { - return arg + arg; - } - - public Double doubleBoxingMethod(final Double arg) { - return arg + arg; - } - - public double[] doubleArrayMethod(final double[] arg) { - final double[] res = new double[arg.length]; - for (int i = 0; i < arg.length; i++) { - res[i] = arg[i] * 2; - } - return res; - } - - public String stringMethod(final String str) { - return str + str; - } - - public String[] stringArrayMethod(final String[] arr) { - final int l = arr.length; - final String[] res = new String[l]; - for (int i = 0; i < l; i++) { - res[i] = arr[l - i - 1]; - } - return res; - } - - public Person[] objectArrayMethod(final Person[] arr) { - final Person[] res = new Person[arr.length]; - for (int i = 0; i < arr.length; i++) { - res[i] = new Person(i + 100); - } - return res; - } - - public Person objectMethod(final Person t) { - t.id *= 2; - return t; - } - - public int twoParamMethod(final long l, final double d) { - return (int)(l + d); - } - - public int threeParamMethod(final short s, final long l, final char c) { - return (int)(s + l + c); - } - - public Person[] twoObjectParamMethod(final Person arg1, final Person arg2) { - return new Person[] { arg2, arg1 }; - } - - public Person[] threeObjectParamMethod(final Person arg1, final Person arg2, final Person arg3) { - return new Person[] { arg3, arg2, arg1 }; - } - - 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) { - return new Person[] { arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 }; - } - - 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) { - return new Person[] { arg9, arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 }; - } - - public Person[] methodObjectEllipsis(final Person... args) { - final int l = args.length; - final Person[] res = new Person[l]; - for (int i = 0; i < l; i++) { - res[i] = args[l - i - 1]; - } - return res; - } - - public Person[] methodPrimitiveEllipsis(final int... args) { - final int l = args.length; - final Person[] res = new Person[l]; - for (int i = 0; i < l; i++) { - res[i] = new Person(args[i]); - } - return res; - } - - public Object[] methodMixedEllipsis(final Object... args) { - return args; - } - - public Object[] methodObjectWithEllipsis(final String arg, final int... args) { - final Object[] res = new Object[args.length + 1]; - res[0] = arg; - for (int i = 0; i < args.length; i++) { - res[i + 1] = args[i]; - } - return res; - } - - public Object[] methodPrimitiveWithEllipsis(final int arg, final long... args) { - final Object[] res = new Object[args.length + 1]; - res[0] = arg; - for (int i = 0; i < args.length; i++) { - res[i + 1] = args[i]; - } - return res; - } - - public Object[] methodMixedWithEllipsis(final String arg1, final int arg2, final Object... args) { - final Object[] res = new Object[args.length + 2]; - res[0] = arg1; - res[1] = arg2; - System.arraycopy(args, 0, res, 2, args.length); - return res; - } - - public void methodStartsThread() { - isFinished = false; - - final Thread t = new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(1000); - isFinished = true; - } catch (final InterruptedException e) { - e.printStackTrace(); - } - } - }); - - t.start(); - } - - public String overloadedMethodDoubleVSint(final int arg) { - return "int"; - } - - public String overloadedMethodDoubleVSint(final double arg) { - return "double"; - } - - public int overloadedMethod(final int arg) { - return arg*2; - } - - public int overloadedMethod(final String arg) { - return arg.length(); - } - - public int overloadedMethod(final boolean arg) { - return (arg) ? 1 : 0; - } - - public int overloadedMethod(final Person arg) { - return arg.id*2; - } - - public int firstLevelMethodInt(final int arg) throws ScriptException, NoSuchMethodException { - return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInt", arg); - } - - public int thirdLevelMethodInt(final int arg) { - return arg*5; - } - - public int firstLevelMethodInteger(final Integer arg) throws ScriptException, NoSuchMethodException { - return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInteger", arg); - } - - public int thirdLevelMethodInteger(final Integer arg) { - return arg*10; - } - - public Person firstLevelMethodObject(final Person p) throws ScriptException, NoSuchMethodException { - return (Person) ((Invocable)engine).invokeFunction("secondLevelMethodObject", p); - } - - public Person thirdLevelMethodObject(final Person p) { - p.id *= 10; - return p; - } - -}
--- a/test/src/jdk/nashorn/api/javaaccess/StringAccessTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,168 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.javaaccess; - -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -/** - * @test - * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.StringAccessTest - * @run testng/othervm jdk.nashorn.api.javaaccess.StringAccessTest - */ -@SuppressWarnings("javadoc") -public class StringAccessTest { - - private static ScriptEngine e = null; - private static SharedObject o = null; - - public static void main(final String[] args) { - TestNG.main(args); - } - - @BeforeClass - public static void setUpClass() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - e = m.getEngineByName("nashorn"); - o = new SharedObject(); - e.put("o", o); - e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); - } - - @AfterClass - public static void tearDownClass() { - e = null; - o = null; - } - - @Test - public void accessFieldString() throws ScriptException { - e.eval("var p_string = o.publicString;"); - assertEquals(o.publicString, e.get("p_string")); - assertEquals("string", e.eval("typeof p_string;")); - e.eval("o.publicString = 'changedString';"); - assertEquals("changedString", o.publicString); - } - - @Test - public void accessFieldStringArray() throws ScriptException { - e.eval("var p_string_array = o.publicStringArray;"); - assertEquals(o.publicStringArray[0], e.eval("o.publicStringArray[0]")); - assertArrayEquals(o.publicStringArray, (String[])e.get("p_string_array")); - e.eval("var t_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + - "t_string_arr[0] = 'abc';" + - "t_string_arr[1] = '123';" + - "t_string_arr[2] = 'xyzzzz';" + - "o.publicStringArray = t_string_arr;"); - assertArrayEquals(new String[] { "abc", "123", "xyzzzz" }, o.publicStringArray); - e.eval("o.publicStringArray[0] = 'nashorn';"); - assertEquals("nashorn", o.publicStringArray[0]); - } - - @Test - public void accessStaticFieldString() throws ScriptException { - e.eval("var ps_string = SharedObject.publicStaticString;"); - assertEquals(SharedObject.publicStaticString, e.get("ps_string")); - assertEquals("string", e.eval("typeof ps_string;")); - e.eval("SharedObject.publicStaticString = 'changedString';"); - assertEquals("changedString", SharedObject.publicStaticString); - } - - @Test - public void accessStaticFieldStringArray() throws ScriptException { - e.eval("var ps_string_array = SharedObject.publicStaticStringArray;"); - assertEquals(SharedObject.publicStaticStringArray[0], e.eval("SharedObject.publicStaticStringArray[0]")); - assertArrayEquals(SharedObject.publicStaticStringArray, (String[])e.get("ps_string_array")); - e.eval("var ts_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + - "ts_string_arr[0] = 'abc';" + - "ts_string_arr[1] = '123';" + - "ts_string_arr[2] = 'xyzzzz';" + - "SharedObject.publicStaticStringArray = ts_string_arr;"); - assertArrayEquals(new String[] { "abc", "123", "xyzzzz" }, SharedObject.publicStaticStringArray); - e.eval("SharedObject.publicStaticStringArray[0] = 'nashorn';"); - assertEquals("nashorn", SharedObject.publicStaticStringArray[0]); - } - - @Test - public void accessFinalFieldString() throws ScriptException { - e.eval("var pf_string = o.publicFinalString;"); - assertEquals(o.publicFinalString, e.get("pf_string")); - assertEquals("string", e.eval("typeof pf_string;")); - e.eval("o.publicFinalString = 'changedString';"); - assertEquals("PublicFinalString", o.publicFinalString); - } - - @Test - public void accessFinalFieldStringArray() throws ScriptException { - e.eval("var pf_string_array = o.publicFinalStringArray;"); - assertEquals(o.publicFinalStringArray[0], e.eval("o.publicFinalStringArray[0]")); - assertArrayEquals(o.publicFinalStringArray, (String[])e.get("pf_string_array")); - e.eval("var tf_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + - "tf_string_arr[0] = 'abc';" + - "tf_string_arr[1] = '123';" + - "tf_string_arr[2] = 'xyzzzz';" + - "o.publicFinalStringArray = tf_string_arr;"); - assertArrayEquals(new String[] { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" }, o.publicFinalStringArray); - e.eval("o.publicFinalStringArray[0] = 'nashorn';"); - assertEquals("nashorn", o.publicFinalStringArray[0]); - } - - @Test - public void accessStaticFinalFieldString() throws ScriptException { - e.eval("var psf_string = SharedObject.publicStaticFinalString;"); - assertEquals(SharedObject.publicStaticFinalString, e.get("psf_string")); - assertEquals("string", e.eval("typeof psf_string;")); - e.eval("SharedObject.publicStaticFinalString = 'changedString';"); - assertEquals("PublicStaticFinalString", SharedObject.publicStaticFinalString); - } - - @Test - public void accessStaticFinalFieldStringArray() throws ScriptException { - e.eval("var psf_string_array = SharedObject.publicStaticFinalStringArray;"); - assertEquals(SharedObject.publicStaticFinalStringArray[0], e.eval("SharedObject.publicStaticFinalStringArray[0]")); - assertArrayEquals(SharedObject.publicStaticFinalStringArray, (String[])e.get("psf_string_array")); - e.eval("var tsf_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + - "tsf_string_arr[0] = 'abc';" + - "tsf_string_arr[1] = '123';" + - "tsf_string_arr[2] = 'xyzzzz';" + - "SharedObject.publicStaticFinalStringArray = tsf_string_arr;"); - assertArrayEquals(new String[] { "StaticFinalArrayString[0]", - "StaticFinalArrayString[1]", - "StaticFinalArrayString[2]", - "StaticFinalArrayString[3]" }, - SharedObject.publicStaticFinalStringArray); - e.eval("SharedObject.publicStaticFinalStringArray[0] = 'nashorn';"); - assertEquals("nashorn", SharedObject.publicStaticFinalStringArray[0]); - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/ArrayConversionTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertNull; +import static org.testng.AssertJUnit.assertTrue; +import java.util.Arrays; +import java.util.List; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.testng.TestNG; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +@SuppressWarnings("javadoc") +public class ArrayConversionTest { + private static ScriptEngine e = null; + + public static void main(final String[] args) { + TestNG.main(args); + } + + @BeforeClass + public static void setUpClass() { + e = new ScriptEngineManager().getEngineByName("nashorn"); + } + + @AfterClass + public static void tearDownClass() { + e = null; + } + + @Test + public void testIntArrays() throws ScriptException { + runTest("assertNullIntArray", "null"); + runTest("assertEmptyIntArray", "[]"); + runTest("assertSingle42IntArray", "[42]"); + runTest("assertSingle42IntArray", "['42']"); + runTest("assertIntArrayConversions", "[false, true, NaN, Infinity, -Infinity, 0.4, 0.6, null, undefined, [], {}, [1], [1, 2]]"); + } + + @Test + public void testIntIntArrays() throws ScriptException { + runTest("assertNullIntIntArray", "null"); + runTest("assertEmptyIntIntArray", "[]"); + runTest("assertSingleEmptyIntIntArray", "[[]]"); + runTest("assertSingleNullIntIntArray", "[null]"); + runTest("assertLargeIntIntArray", "[[false], [1], [2, 3], [4, 5, 6], ['7', {valueOf: function() { return 8 }}]]"); + } + + @Test + public void testObjectObjectArrays() throws ScriptException { + runTest("assertLargeObjectObjectArray", "[[false], [1], ['foo', 42.3], [{x: 17}]]"); + } + + @Test + public void testBooleanArrays() throws ScriptException { + runTest("assertBooleanArrayConversions", "[false, true, '', 'false', 0, 1, 0.4, 0.6, {}, [], [false], [true], NaN, Infinity, null, undefined]"); + } + + @Test + public void testArrayAmbiguity() throws ScriptException { + runTest("x", "'abc'"); + runTest("x", "['foo', 'bar']"); + } + + @Test + public void testListArrays() throws ScriptException { + runTest("assertListArray", "[['foo', 'bar'], ['apple', 'orange']]"); + } + + @Test + public void testVarArgs() throws ScriptException { + // Sole NativeArray in vararg position becomes vararg array itself + runTest("assertVarArg_42_17", "[42, 17]"); + // NativeArray in vararg position becomes an argument if there are more arguments + runTest("assertVarArg_array_17", "[42], 18"); + // Only NativeArray is converted to vararg array, other objects (e.g. a function) aren't + runTest("assertVarArg_function", "function() { return 'Hello' }"); + } + + private static void runTest(final String testMethodName, final String argument) throws ScriptException { + e.eval("Java.type('" + ArrayConversionTest.class.getName() + "')." + testMethodName + "(" + argument + ")"); + } + + public static void assertNullIntArray(final int[] array) { + assertNull(array); + } + + public static void assertNullIntIntArray(final int[][] array) { + assertNull(array); + } + + public static void assertEmptyIntArray(final int[] array) { + assertEquals(0, array.length); + } + + public static void assertSingle42IntArray(final int[] array) { + assertEquals(1, array.length); + assertEquals(42, array[0]); + } + + + public static void assertIntArrayConversions(final int[] array) { + assertEquals(13, array.length); + assertEquals(0, array[0]); // false + assertEquals(1, array[1]); // true + assertEquals(0, array[2]); // NaN + assertEquals(0, array[3]); // Infinity + assertEquals(0, array[4]); // -Infinity + assertEquals(0, array[5]); // 0.4 + assertEquals(0, array[6]); // 0.6 - floor, not round + assertEquals(0, array[7]); // null + assertEquals(0, array[8]); // undefined + assertEquals(0, array[9]); // [] + assertEquals(0, array[10]); // {} + assertEquals(1, array[11]); // [1] + assertEquals(0, array[12]); // [1, 2] + } + + public static void assertEmptyIntIntArray(final int[][] array) { + assertEquals(0, array.length); + } + + public static void assertSingleEmptyIntIntArray(final int[][] array) { + assertEquals(1, array.length); + assertTrue(Arrays.equals(new int[0], array[0])); + } + + public static void assertSingleNullIntIntArray(final int[][] array) { + assertEquals(1, array.length); + assertNull(null, array[0]); + } + + public static void assertLargeIntIntArray(final int[][] array) { + assertEquals(5, array.length); + assertTrue(Arrays.equals(new int[] { 0 }, array[0])); + assertTrue(Arrays.equals(new int[] { 1 }, array[1])); + assertTrue(Arrays.equals(new int[] { 2, 3 }, array[2])); + assertTrue(Arrays.equals(new int[] { 4, 5, 6 }, array[3])); + assertTrue(Arrays.equals(new int[] { 7, 8 }, array[4])); + } + + public static void assertLargeObjectObjectArray(final Object[][] array) throws ScriptException { + assertEquals(4, array.length); + assertTrue(Arrays.equals(new Object[] { Boolean.FALSE }, array[0])); + assertTrue(Arrays.equals(new Object[] { 1 }, array[1])); + assertTrue(Arrays.equals(new Object[] { "foo", 42.3d }, array[2])); + assertEquals(1, array[3].length); + e.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", array[3][0]); + assertEquals(17, e.eval("obj.x")); + } + + public static void assertBooleanArrayConversions(final boolean[] array) { + assertEquals(16, array.length); + assertFalse(array[0]); // false + assertTrue(array[1]); // true + assertFalse(array[2]); // '' + assertTrue(array[3]); // 'false' (yep, every non-empty string converts to true) + assertFalse(array[4]); // 0 + assertTrue(array[5]); // 1 + assertTrue(array[6]); // 0.4 + assertTrue(array[7]); // 0.6 + assertTrue(array[8]); // {} + assertTrue(array[9]); // [] + assertTrue(array[10]); // [false] + assertTrue(array[11]); // [true] + assertFalse(array[12]); // NaN + assertTrue(array[13]); // Infinity + assertFalse(array[14]); // null + assertFalse(array[15]); // undefined + } + + public static void assertListArray(final List<?>[] array) { + assertEquals(2, array.length); + assertEquals(Arrays.asList("foo", "bar"), array[0]); + assertEquals(Arrays.asList("apple", "orange"), array[1]); + } + + public static void assertVarArg_42_17(final Object... args) { + assertEquals(2, args.length); + assertEquals(42, ((Number)args[0]).intValue()); + assertEquals(17, ((Number)args[1]).intValue()); + } + + public static void assertVarArg_array_17(final Object... args) throws ScriptException { + assertEquals(2, args.length); + e.getBindings(ScriptContext.ENGINE_SCOPE).put("arr", args[0]); + assertTrue((Boolean)e.eval("arr instanceof Array && arr.length == 1 && arr[0] == 42")); + assertEquals(18, ((Number)args[1]).intValue()); + } + + public static void assertVarArg_function(final Object... args) throws ScriptException { + assertEquals(1, args.length); + e.getBindings(ScriptContext.ENGINE_SCOPE).put("fn", args[0]); + assertEquals("Hello", e.eval("fn()")); + } + + + + public static void x(final String y) { + assertEquals("abc", y); + } + public static void x(final String[] y) { + assertTrue(Arrays.equals(new String[] { "foo", "bar"}, y)); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/BooleanAccessTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; +import java.util.Arrays; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.testng.TestNG; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * @test + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.BooleanAccessTest + * @run testng/othervm jdk.nashorn.api.javaaccess.BooleanAccessTest + */ +@SuppressWarnings("javadoc") +public class BooleanAccessTest { + + private static ScriptEngine e = null; + private static SharedObject o = null; + + public static void main(final String[] args) { + TestNG.main(args); + } + + @BeforeClass + public static void setUpClass() throws ScriptException { + final ScriptEngineManager m = new ScriptEngineManager(); + e = m.getEngineByName("nashorn"); + o = new SharedObject(); + e.put("o", o); + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); + } + + @AfterClass + public static void tearDownClass() { + e = null; + o = null; + } + + @Test + public void accessFieldBoolean() throws ScriptException { + e.eval("var p_boolean = o.publicBoolean;"); + assertEquals(o.publicBoolean, e.get("p_boolean")); + assertEquals("boolean", e.eval("typeof p_boolean;")); + e.eval("o.publicBoolean = false;"); + assertEquals(false, o.publicBoolean); + } + + @Test + public void accessFieldBooleanArray() throws ScriptException { + e.eval("var p_boolean_array = o.publicBooleanArray;"); + assertEquals(o.publicBooleanArray[0], e.eval("o.publicBooleanArray[0]")); + assertTrue(Arrays.equals(o.publicBooleanArray, (boolean[])e.get("p_boolean_array"))); + e.eval("var t_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + + "t_boolean_arr[0] = true;" + + "t_boolean_arr[1] = false;" + + "t_boolean_arr[2] = false;" + + "o.publicBooleanArray = t_boolean_arr;"); + assertTrue(Arrays.equals(new boolean[] { true, false, false }, o.publicBooleanArray)); + e.eval("o.publicBooleanArray[0] = false;"); + assertEquals(false, o.publicBooleanArray[0]); + } + + @Test + public void accessStaticFieldBoolean() throws ScriptException { + e.eval("var ps_boolean = SharedObject.publicStaticBoolean;"); + assertEquals(SharedObject.publicStaticBoolean, e.get("ps_boolean")); + assertEquals("boolean", e.eval("typeof ps_boolean;")); + e.eval("SharedObject.publicStaticBoolean = false;"); + assertEquals(false, SharedObject.publicStaticBoolean); + } + + @Test + public void accessStaticFieldBooleanArray() throws ScriptException { + e.eval("var ps_boolean_array = SharedObject.publicStaticBooleanArray;"); + assertEquals(SharedObject.publicStaticBooleanArray[0], e.eval("SharedObject.publicStaticBooleanArray[0]")); + assertTrue(Arrays.equals(SharedObject.publicStaticBooleanArray, (boolean[])e.get("ps_boolean_array"))); + e.eval("var ts_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + + "ts_boolean_arr[0] = true;" + + "ts_boolean_arr[1] = false;" + + "ts_boolean_arr[2] = true;" + + "SharedObject.publicStaticBooleanArray = ts_boolean_arr;"); + assertTrue(Arrays.equals(new boolean[] { true, false, true }, SharedObject.publicStaticBooleanArray)); + e.eval("SharedObject.publicStaticBooleanArray[0] = false;"); + assertEquals(false, SharedObject.publicStaticBooleanArray[0]); + } + + @Test + public void accessFinalFieldBoolean() throws ScriptException { + e.eval("var pf_boolean = o.publicFinalBoolean;"); + assertEquals(o.publicFinalBoolean, e.get("pf_boolean")); + assertEquals("boolean", e.eval("typeof pf_boolean;")); + e.eval("o.publicFinalBoolean = false;"); + assertEquals(true, o.publicFinalBoolean); + } + + @Test + public void accessFinalFieldBooleanArray() throws ScriptException { + e.eval("var pf_boolean_array = o.publicFinalBooleanArray;"); + assertEquals(o.publicFinalBooleanArray[0], e.eval("o.publicFinalBooleanArray[0]")); + assertTrue(Arrays.equals(o.publicFinalBooleanArray, (boolean[])e.get("pf_boolean_array"))); + e.eval("var tf_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + + "tf_boolean_arr[0] = false;" + + "tf_boolean_arr[1] = false;" + + "tf_boolean_arr[2] = true;" + + "o.publicOFinalbjectArray = tf_boolean_arr;"); + assertTrue(Arrays.equals(new boolean[] { false, false, true, false }, o.publicFinalBooleanArray)); + e.eval("o.publicFinalBooleanArray[0] = true;"); + assertEquals(true, o.publicFinalBooleanArray[0]); + } + + @Test + public void accessStaticFinalFieldBoolean() throws ScriptException { + e.eval("var psf_boolean = SharedObject.publicStaticFinalBoolean;"); + assertEquals(SharedObject.publicStaticFinalBoolean, e.get("psf_boolean")); + assertEquals("boolean", e.eval("typeof psf_boolean;")); + e.eval("SharedObject.publicStaticFinalBoolean = false;"); + assertEquals(true, SharedObject.publicStaticFinalBoolean); + } + + @Test + public void accessStaticFinalFieldBooleanArray() throws ScriptException { + e.eval("var psf_boolean_array = SharedObject.publicStaticFinalBooleanArray;"); + assertEquals(SharedObject.publicStaticFinalBooleanArray[0], e.eval("SharedObject.publicStaticFinalBooleanArray[0]")); + assertTrue(Arrays.equals(SharedObject.publicStaticFinalBooleanArray, (boolean[])e.get("psf_boolean_array"))); + e.eval("var tsf_boolean_arr = new (Java.type(\"boolean[]\"))(3);" + + "tsf_boolean_arr[0] = false;" + + "tsf_boolean_arr[1] = true;" + + "tsf_boolean_arr[2] = false;" + + "SharedObject.publicStaticFinalBooleanArray = tsf_boolean_arr;"); + assertTrue(Arrays.equals(new boolean[] { false, true, false, false }, SharedObject.publicStaticFinalBooleanArray)); + e.eval("SharedObject.publicStaticFinalBooleanArray[0] = true;"); + assertEquals(true, SharedObject.publicStaticFinalBooleanArray[0]); + } + + @Test + public void accessFieldBooleanBoxing() throws ScriptException { + e.eval("var p_boolean_box = o.publicBooleanBox;"); + assertEquals(o.publicBooleanBox, e.get("p_boolean_box")); + assertEquals("boolean", e.eval("typeof p_boolean_box;")); + e.eval("o.publicBooleanBox = false;"); + assertEquals(false, (boolean)o.publicBooleanBox); + } + + @Test + public void accessStaticFieldBooleanBoxing() throws ScriptException { + e.eval("var ps_boolean_box = SharedObject.publicStaticBooleanBox;"); + assertEquals(SharedObject.publicStaticBooleanBox, e.get("ps_boolean_box")); + assertEquals("boolean", e.eval("typeof ps_boolean_box;")); + e.eval("SharedObject.publicStaticBooleanBox = false;"); + assertEquals(false, (boolean)SharedObject.publicStaticBooleanBox); + } + + @Test + public void accessFinalFieldBooleanBoxing() throws ScriptException { + e.eval("var pf_boolean_box = o.publicFinalBooleanBox;"); + assertEquals(o.publicFinalBooleanBox, e.get("pf_boolean_box")); + assertEquals("boolean", e.eval("typeof pf_boolean_box;")); + e.eval("o.publicFinalBooleanBox = false;"); + assertEquals(true, (boolean)o.publicFinalBooleanBox); + } + + @Test + public void accessStaticFinalFieldBooleanBoxing() throws ScriptException { + e.eval("var psf_boolean_box = SharedObject.publicStaticFinalBooleanBox;"); + assertEquals(SharedObject.publicStaticFinalBooleanBox, e.get("psf_boolean_box")); + assertEquals("boolean", e.eval("typeof psf_boolean_box;")); + e.eval("SharedObject.publicStaticFinalBooleanBox = false;"); + assertEquals(true, (boolean)SharedObject.publicStaticFinalBooleanBox); + } + + @Test + public void accessVolatileField() throws ScriptException { + e.eval("var pv_boolean = o.volatileBoolean;"); + assertEquals(o.volatileBoolean, e.get("pv_boolean")); + assertEquals("boolean", e.eval("typeof pv_boolean;")); + e.eval("o.volatileBoolean = false;"); + assertEquals(false, o.volatileBoolean); + } + + @Test + public void accessTransientField() throws ScriptException { + e.eval("var pt_boolean = o.transientBoolean;"); + assertEquals(o.transientBoolean, e.get("pt_boolean")); + assertEquals("boolean", e.eval("typeof pt_boolean;")); + e.eval("o.transientBoolean = false;"); + assertEquals(false, o.transientBoolean); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/ConsStringTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import static org.testng.AssertJUnit.assertEquals; +import java.util.HashMap; +import java.util.Map; +import javax.script.Bindings; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import jdk.nashorn.api.scripting.JSObject; +import org.testng.TestNG; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +@SuppressWarnings("javadoc") +public class ConsStringTest { + private static ScriptEngine e = null; + + public static void main(final String[] args) { + TestNG.main(args); + } + + @BeforeClass + public static void setUpClass() { + e = new ScriptEngineManager().getEngineByName("nashorn"); + } + + @AfterClass + public static void tearDownClass() { + e = null; + } + + @Test + public void testConsStringFlattening() throws ScriptException { + final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); + final Map<Object, Object> m = new HashMap<>(); + b.put("m", m); + e.eval("var x = 'f'; x += 'oo'; var y = 'b'; y += 'ar'; m.put(x, y)"); + assertEquals("bar", m.get("foo")); + } + + @Test + public void testConsStringFromMirror() throws ScriptException { + final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); + //final Map<Object, Object> m = new HashMap<>(); + e.eval("var x = 'f'; x += 'oo'; var obj = {x: x};"); + assertEquals("foo", ((JSObject)b.get("obj")).getMember("x")); + } + + @Test + public void testArrayConsString() throws ScriptException { + final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); + final ArrayHolder h = new ArrayHolder(); + b.put("h", h); + e.eval("var x = 'f'; x += 'oo'; h.array = [x];"); + assertEquals(1, h.array.length); + assertEquals("foo", h.array[0]); + } + + + public static class ArrayHolder { + private Object[] array; + + public void setArray(final Object[] array) { + this.array = array; + } + + public Object[] getArray() { + return array; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/MethodAccessTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,466 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Locale; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.testng.TestNG; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * @test + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.MethodAccessTest + * @run testng/othervm jdk.nashorn.api.javaaccess.MethodAccessTest + */ +@SuppressWarnings("javadoc") +public class MethodAccessTest { + + private static ScriptEngine e = null; + private static SharedObject o = null; + + public static void main(final String[] args) { + TestNG.main(args); + } + + @BeforeClass + public static void setUpClass() throws ScriptException { + final ScriptEngineManager m = new ScriptEngineManager(); + e = m.getEngineByName("nashorn"); + o = new SharedObject(); + o.setEngine(e); + e.put("o", o); + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); + e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;"); + } + + @AfterClass + public static void tearDownClass() { + e = null; + o = null; + } + + @Test + public void accessMethodthrowsCheckedException() throws ScriptException { + e.eval("try {" + + " var a = java.lang.Long.parseLong('foo');" + + "} catch(e) {" + + " var isThrown = true;" + + " var isNumberException = e instanceof java.lang.NumberFormatException;" + + "} finally {" + + " var isFinalized = true;" + + "}"); + assertEquals("Exception thrown", true, e.get("isThrown")); + assertEquals("Finally called", true, e.get("isFinalized")); + assertEquals("Type is NumberFormatException", true, e.get("isNumberException")); + } + + @Test + public void accessMethodthrowsUnCheckedException() throws ScriptException { + e.eval("try {" + + " var a = java.lang.String.valueOf(null);" + + "} catch(e) {" + + " var isThrown = true;" + + " var isNumberException = e instanceof java.lang.NullPointerException;" + + "} finally {" + + " var isFinalized = true;" + + "}"); + assertEquals(true, e.get("isThrown")); + assertEquals(true, e.get("isFinalized")); + assertEquals(true, e.get("isNumberException")); + } + + @Test + public void accessMethodStartsThread() throws ScriptException { + e.eval("o.methodStartsThread();"); + assertEquals(false, o.isFinished); + } + + @Test + public void accessStaticMethod() throws ScriptException { + assertEquals(10, e.eval("java.lang.Math.abs(-10);")); + } + + @Test + public void accessSynchronousMethod() throws ScriptException { + e.eval("var v = new java.util.Vector();" + "v.add(10);" + "v.add(20);" + "v.add(30);"); + assertEquals(10, e.eval("v[0]")); + assertEquals(20, e.eval("v[1]")); + assertEquals(30, e.eval("v[2]")); + assertEquals(3, e.eval("v.size()")); + } + + @Test + public void accessStaticSynchronousMethod() throws ScriptException { + e.eval("var locales = java.util.Calendar.getAvailableLocales();"); + final Locale[] locales = (Locale[])e.get("locales"); + assertEquals(locales.length, Calendar.getAvailableLocales().length); + } + + @Test + public void accessNativeMethod() throws ScriptException { + assertEquals(4.0, e.eval("java.lang.StrictMath.log10(10000);")); + } + + @Test + public void accessConstructorOfAbstractClass() throws ScriptException { + e.eval("try {" + + " var a = new java.util.AbstractList();" + + " print('fail');" + + "} catch(e) {" + + " var isThrown = true;" + + "}"); + assertEquals(true, e.get("isThrown")); + } + + @Test + public void accessMethodVoid() throws ScriptException { + o.isAccessed = false; + e.eval("o.voidMethod();"); + assertTrue(o.isAccessed); + } + + @Test + public void accessMethodBoolean() throws ScriptException { + assertEquals(true, e.eval("o.booleanMethod(false);")); + assertEquals(false, e.eval("o.booleanMethod(true);")); + assertEquals(false, e.eval("o.booleanMethod('false');")); + assertEquals(true, e.eval("o.booleanMethod('');")); + assertEquals(true, e.eval("o.booleanMethod(0);")); + } + + @Test + public void accessMethodInt() throws ScriptException { + assertEquals(0, e.eval("o.intMethod(0);")); + assertEquals(-200, e.eval("o.intMethod(-100);")); + assertEquals(0, e.eval("o.intMethod('0');")); + assertEquals(-200, e.eval("o.intMethod('-100');")); + } + + @Test + public void accessMethodLong() throws ScriptException { + assertEquals((long)0, e.eval("o.longMethod(0);")); + assertEquals((long)400, e.eval("o.longMethod(200);")); + assertEquals((long) 0, e.eval("o.longMethod('0');")); + assertEquals((long) 400, e.eval("o.longMethod('200');")); + } + + @Test + public void accessMethodByte() throws ScriptException { + assertEquals((byte) 0, e.eval("o.byteMethod(0);")); + assertEquals((byte) 10, e.eval("o.byteMethod(5);")); + assertEquals((byte) 0, e.eval("o.byteMethod('0');")); + assertEquals((byte) 10, e.eval("o.byteMethod('5');")); + } + + @Test + public void accessMethodShort() throws ScriptException { + assertEquals((short)0, e.eval("o.shortMethod(0);")); + assertEquals((short)8000, e.eval("o.shortMethod(4000);")); + assertEquals((short) 0, e.eval("o.shortMethod('0');")); + assertEquals((short) 8000, e.eval("o.shortMethod('4000');")); + } + + @Test + public void accessMethodChar() throws ScriptException { + assertEquals('A', e.eval("o.charMethod('a');")); + assertEquals('Z', e.eval("o.charMethod('z');")); + assertEquals(o.charMethod((char)0), e.eval("o.charMethod(0);")); + assertEquals(o.charMethod((char)3150), e.eval("o.charMethod(3150);")); + } + + @Test + public void accessMethodFloat() throws ScriptException { + assertEquals(0.0f, e.eval("o.floatMethod(0.0);")); + assertEquals(4.2f, e.eval("o.floatMethod(2.1);")); + assertEquals(0.0f, e.eval("o.floatMethod('0.0');")); + assertEquals(4.2f, e.eval("o.floatMethod('2.1');")); + } + + @Test + public void accessMethodDouble() throws ScriptException { + assertEquals(0.0, e.eval("o.doubleMethod(0.0);")); + assertEquals(14.0, e.eval("o.doubleMethod(7.0);")); + assertEquals(0.0, e.eval("o.doubleMethod('0.0');")); + assertEquals(14.0, e.eval("o.doubleMethod('7.0');")); + } + + @Test + public void accessMethodBooleanBoxing() throws ScriptException { + assertEquals(Boolean.TRUE, e.eval("o.booleanBoxingMethod(java.lang.Boolean.FALSE);")); + assertEquals(Boolean.FALSE, e.eval("o.booleanBoxingMethod(java.lang.Boolean.TRUE);")); + assertEquals(Boolean.TRUE, e.eval("o.booleanBoxingMethod('');")); + assertEquals(Boolean.FALSE, e.eval("o.booleanBoxingMethod('false');")); + } + + @Test + public void accessMethodIntBoxing() throws ScriptException { + assertEquals(0, e.eval("o.intBoxingMethod(0);")); + assertEquals(-200, e.eval("o.intBoxingMethod(-100);")); + assertTrue((int)e.eval("(new java.lang.Integer(2)).compareTo(10.0)") < 0); + } + + @Test + public void accessMethodLongBoxing() throws ScriptException { + assertEquals((long) 0, e.eval("o.longBoxingMethod(0);")); + assertEquals((long) 400, e.eval("o.longBoxingMethod(200);")); + assertTrue((int)e.eval("(new java.lang.Long(2)).compareTo(10.0)") < 0); + } + + @Test + public void accessMethodByteBoxing() throws ScriptException { + assertEquals((byte) 0, e.eval("o.byteBoxingMethod(0);")); + assertEquals((byte) 10, e.eval("o.byteBoxingMethod(5);")); + assertTrue((int)e.eval("(new java.lang.Byte(2)).compareTo(10.0)") < 0); + } + + @Test + public void accessMethodShortBoxing() throws ScriptException { + assertEquals((short) 0, e.eval("o.shortBoxingMethod(0);")); + assertEquals((short) 8000, e.eval("o.shortBoxingMethod(4000);")); + assertTrue((int)e.eval("(new java.lang.Short(2)).compareTo(10.0)") < 0); + } + + @Test + public void accessMethodCharBoxing() throws ScriptException { + assertEquals('A', e.eval("o.charBoxingMethod('a');")); + assertEquals('Z', e.eval("o.charBoxingMethod('z');")); + assertTrue((int)e.eval("(new java.lang.Character(2)).compareTo(10)") < 0); + } + + @Test + public void accessMethodFloatBoxing() throws ScriptException { + assertEquals(0.0f, e.eval("o.floatBoxingMethod(0.0);")); + assertEquals(4.2f, e.eval("o.floatBoxingMethod(2.1);")); + assertTrue((int)e.eval("(new java.lang.Float(2.0)).compareTo(10.0)") < 0); + } + + @Test + public void accessMethodDoubleBoxing() throws ScriptException { + assertEquals(0.0, e.eval("o.doubleBoxingMethod(0.0);")); + assertEquals(14.0, e.eval("o.doubleBoxingMethod(7.0);")); + assertTrue((int)e.eval("(new java.lang.Double(2)).compareTo(10.0)") < 0); + } + + @Test + public void accessMethodString() throws ScriptException { + assertEquals("", e.eval("o.stringMethod('');")); + assertEquals("abcabc", e.eval("o.stringMethod('abc');")); + } + + @Test + public void accessMethodObject() throws ScriptException { + e.put("so", new Person(5)); + e.eval("var rso = o.objectMethod(so);"); + assertEquals(new Person(10), e.get("rso")); + } + + @Test + public void accessMethodBooleanArray() throws ScriptException { + assertTrue(Arrays.equals(o.booleanArrayMethod(o.publicBooleanArray), (boolean[])e.eval("o.booleanArrayMethod(o.publicBooleanArray);"))); + } + + @Test + public void accessMethodIntArray() throws ScriptException { + assertArrayEquals(o.intArrayMethod(o.publicIntArray), (int[])e.eval("o.intArrayMethod(o.publicIntArray);")); + } + + @Test + public void accessMethodLongArray() throws ScriptException { + assertArrayEquals(o.longArrayMethod(o.publicLongArray), (long[])e.eval("o.longArrayMethod(o.publicLongArray);")); + } + + @Test + public void accessMethodByteArray() throws ScriptException { + assertArrayEquals(o.byteArrayMethod(o.publicByteArray), (byte[])e.eval("o.byteArrayMethod(o.publicByteArray);")); + } + + @Test + public void accessMethodShortArray() throws ScriptException { + assertArrayEquals(o.shortArrayMethod(o.publicShortArray), (short[])e.eval("o.shortArrayMethod(o.publicShortArray);")); + } + + @Test + public void accessMethodCharArray() throws ScriptException { + assertArrayEquals(o.charArrayMethod(o.publicCharArray), (char[])e.eval("o.charArrayMethod(o.publicCharArray);")); + } + + @Test + public void accessMethodFloatArray() throws ScriptException { + assertArrayEquals(o.floatArrayMethod(o.publicFloatArray), (float[])e.eval("o.floatArrayMethod(o.publicFloatArray);"), 1e-10f); + } + + @Test + public void accessMethodDoubleArray() throws ScriptException { + assertArrayEquals(o.doubleArrayMethod(o.publicDoubleArray), (double[])e.eval("o.doubleArrayMethod(o.publicDoubleArray);"), 1e-10); + } + + @Test + public void accessMethodStringArray() throws ScriptException { + assertArrayEquals(o.stringArrayMethod(o.publicStringArray), (String[])e.eval("o.stringArrayMethod(o.publicStringArray);")); + } + + @Test + public void accessMethodObjectArray() throws ScriptException { + assertArrayEquals(o.objectArrayMethod(o.publicObjectArray), (Person[])e.eval("o.objectArrayMethod(o.publicObjectArray);")); + } + + @Test + public void accessDefaultConstructor() throws ScriptException { + e.eval("var dc = new Packages.jdk.nashorn.api.javaaccess.Person()"); + assertEquals(new Person(), e.get("dc")); + } + + @Test + public void accessCustomConstructor() throws ScriptException { + e.eval("var cc = new Packages.jdk.nashorn.api.javaaccess.Person(17)"); + assertEquals(new Person(17), e.get("cc")); + } + + @Test + public void accessMethod2PrimitiveParams() throws ScriptException { + assertEquals(o.twoParamMethod(50, 40.0), e.eval("o.twoParamMethod(50,40);")); + } + + @Test + public void accessMethod3PrimitiveParams() throws ScriptException { + assertEquals(o.threeParamMethod((short)10, 20L, 'b'), e.eval("o.threeParamMethod(10,20,'b');")); + } + + @Test + public void accessMethod2ObjectParams() throws ScriptException { + assertArrayEquals(new Person[] { new Person(200), new Person(300) }, (Person[])e.eval("o.twoObjectParamMethod(new Person(300),new Person(200));")); + } + + @Test + public void accessMethod3ObjectParams() throws ScriptException { + 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));")); + } + + @Test + public void accessMethod8ObjectParams() throws ScriptException { + 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));")); + } + + @Test + public void accessMethod9ObjectParams() throws ScriptException { + 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));")); + } + + @Test + public void accessMethodObjectEllipsis() throws ScriptException { + 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));")); + assertArrayEquals(new Person[] {}, (Person[])e.eval("o.methodObjectEllipsis()")); + assertArrayEquals(new Person[] { new Person(9) }, (Person[])e.eval("o.methodObjectEllipsis(new Person(9))")); + } + + @Test + public void accessMethodPrimitiveEllipsis() throws ScriptException { + assertArrayEquals(new Person[] { new Person(1), new Person(3), new Person(2) }, (Person[])e.eval("o.methodPrimitiveEllipsis(1,3,2);")); + assertArrayEquals(new Person[] {}, (Person[])e.eval("o.methodPrimitiveEllipsis();")); + 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);")); + } + + @Test + public void accessMethodMixedEllipsis() throws ScriptException { + assertArrayEquals(new Object[] { new Person(1), 12, "hello", true }, (Object[])e.eval("o.methodMixedEllipsis(new Person(1),12,'hello',true);")); + assertArrayEquals(new Object[] {}, (Object[])e.eval("o.methodMixedEllipsis();")); + } + + @Test + public void accessMethodObjectWithEllipsis() throws ScriptException { + assertArrayEquals(new Object[] { "hello", 12, 15, 16 }, (Object[])e.eval("o.methodObjectWithEllipsis('hello',12,15,16);")); + assertArrayEquals(new Object[] { "hello" }, (Object[])e.eval("o.methodObjectWithEllipsis('hello');")); + } + + @Test + public void accessMethodPrimitiveWithEllipsis() throws ScriptException { + assertArrayEquals(new Object[] { 14, 12L, 15L, 16L }, (Object[])e.eval("o.methodPrimitiveWithEllipsis(14,12,15,16);")); + assertArrayEquals(new Object[] { 12 }, (Object[])e.eval("o.methodPrimitiveWithEllipsis(12);")); + } + + @Test + public void accessMethodMixedWithEllipsis() throws ScriptException { + assertArrayEquals(new Object[] { "Hello", 10, true, -100500, 80d }, (Object[])e.eval("o.methodMixedWithEllipsis('Hello', 10, true, -100500,80.0);")); + assertArrayEquals(new Object[] { "Nashorn", 15 }, (Object[])e.eval("o.methodMixedWithEllipsis('Nashorn',15);")); + } + + @Test + public void accessMethodOverloaded() throws ScriptException { + assertEquals(0, e.eval("o.overloadedMethod(0);")); + assertEquals(2000, e.eval("o.overloadedMethod(1000);")); + assertEquals(2, e.eval("o.overloadedMethod('10');")); + assertEquals(7, e.eval("o.overloadedMethod('Nashorn');")); + assertEquals(4, e.eval("o.overloadedMethod('true');")); + assertEquals(1, e.eval("o.overloadedMethod(true);")); + assertEquals(0, e.eval("o.overloadedMethod(false);")); + assertEquals(44, e.eval("o.overloadedMethod(new Person(22));")); + assertEquals(0, e.eval("o.overloadedMethod(new Person());")); + } + + @Test + public void accessMethodDoubleVSintOverloaded() throws ScriptException { + assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.0);")); + assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(1000.0);")); + assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(0.01);")); + assertEquals("double", e.eval("o.overloadedMethodDoubleVSint(100.02);")); + assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(0);")); + assertEquals("int", e.eval("o.overloadedMethodDoubleVSint(1000);")); + } + + @Test + public void accessJavaMethodIntFromJSFromJavaFromJS() throws ScriptException { + e.eval("function secondLevelMethodInt(a) {" + + "return o.thirdLevelMethodInt(a);" + + "}"); + assertEquals(50, e.eval("o.firstLevelMethodInt(10);")); + } + + @Test + public void accessJavaMethodIntegerFromJSFromJavaFromJS() throws ScriptException { + e.eval("function secondLevelMethodInteger(a) {" + + "return o.thirdLevelMethodInteger(a);" + + "}"); + assertEquals(100, e.eval("o.firstLevelMethodInteger(10);")); + } + + @Test + public void accessJavaMethodObjectFromJSFromJavaFromJS() throws ScriptException { + e.eval("function secondLevelMethodObject(p) {" + + "return o.thirdLevelMethodObject(p);" + + "}"); + assertEquals(new Person(100), e.eval("o.firstLevelMethodObject(new Person(10));")); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/NumberAccessTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,789 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.testng.TestNG; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * @test + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.NumberAccessTest + * @run testng/othervm jdk.nashorn.api.javaaccess.NumberAccessTest + */ +@SuppressWarnings("javadoc") +public class NumberAccessTest { + + private static ScriptEngine e; + private static SharedObject o; + + public static void main(final String[] args) { + TestNG.main(args); + } + + @BeforeClass + public static void setUpClass() throws ScriptException { + final ScriptEngineManager m = new ScriptEngineManager(); + e = m.getEngineByName("nashorn"); + o = new SharedObject(); + e.put("o", o); + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); + } + + @AfterClass + public static void tearDownClass() { + e = null; + o = null; + } + + // --------------------------------long + // tests------------------------------------ + @Test + public void accessFieldLong() throws ScriptException { + e.eval("var p_long = o.publicLong;"); + assertEquals(o.publicLong, e.get("p_long")); + e.eval("o.publicLong = 12;"); + assertEquals(12, o.publicLong); + } + + @Test + public void accessFieldLongArray() throws ScriptException { + e.eval("var p_long_array = o.publicLongArray;"); + assertEquals(o.publicLongArray[0], e.eval("o.publicLongArray[0];")); + assertArrayEquals(o.publicLongArray, (long[])e.get("p_long_array")); + e.eval("var t_long_arr = new (Java.type(\"long[]\"))(3);" + + "t_long_arr[0] = -189009;" + + "t_long_arr[1] = 456;" + + "t_long_arr[2] = 600000001;" + + "o.publicLongArray = t_long_arr;"); + // e.eval("o.publicIntArray = [-189009,456,600000001];"); + assertArrayEquals(new long[] { -189009, 456, 600000001 }, o.publicLongArray); + e.eval("o.publicLongArray[0] = 10;"); + assertEquals(10, o.publicLongArray[0]); + } + + @Test + public void accessStaticFieldLong() throws ScriptException { + e.eval("var ps_long = SharedObject.publicStaticLong;"); + assertEquals(SharedObject.publicStaticLong, e.get("ps_long")); + e.eval("SharedObject.publicStaticLong = 120;"); + assertEquals(120, SharedObject.publicStaticLong); + } + + @Test + public void accessStaticFieldLongArray() throws ScriptException { + e.eval("var ps_long_array = SharedObject.publicStaticLongArray;"); + assertEquals(SharedObject.publicStaticLongArray[0], e.eval("SharedObject.publicStaticLongArray[0];")); + assertArrayEquals(SharedObject.publicStaticLongArray, (long[])e.get("ps_long_array")); + e.eval("var ts_long_arr = new (Java.type(\"long[]\"))(3);" + + "ts_long_arr[0] = -189009;" + + "ts_long_arr[1] = 456;" + + "ts_long_arr[2] = 600000001;" + + "SharedObject.publicStaticLongArray = ts_long_arr;"); + // e.eval("o.publicIntArray = [-189009,456,600000001];"); + assertArrayEquals(new long[] { -189009, 456, 600000001 }, SharedObject.publicStaticLongArray); + e.eval("SharedObject.publicStaticLongArray[0] = 10;"); + assertEquals(10, SharedObject.publicStaticLongArray[0]); + } + + @Test + public void accessFinalFieldLong() throws ScriptException { + e.eval("var pf_long = o.publicFinalLong;"); + assertEquals(o.publicFinalLong, e.get("pf_long")); + e.eval("o.publicFinalLong = 120;"); + assertEquals(13353333333333333L, o.publicFinalLong); + } + + @Test + public void accessFinalFieldLongArray() throws ScriptException { + e.eval("var pf_long_array = o.publicFinalLongArray;"); + assertEquals(o.publicFinalLongArray[0], e.eval("o.publicFinalLongArray[0];")); + assertArrayEquals(o.publicFinalLongArray, (long[])e.get("pf_long_array")); + e.eval("var tf_long_arr = new (Java.type(\"long[]\"))(3);" + + "tf_long_arr[0] = -189009;" + + "tf_long_arr[1] = 456;" + + "tf_long_arr[2] = 600000001;" + + "o.publicFinalLongArray = tf_long_arr;"); + // e.eval("o.publicIntArray = [-189009,456,600000001];"); + assertArrayEquals(new long[] { 1901733333333L, -2247355555L, 3977377777L }, o.publicFinalLongArray); + e.eval("o.publicFinalLongArray[0] = 10;"); + assertEquals(10, o.publicFinalLongArray[0]); + } + + @Test + public void accessStaticFinalFieldLong() throws ScriptException { + e.eval("var psf_long = SharedObject.publicStaticFinalLong;"); + assertEquals(SharedObject.publicStaticFinalLong, e.get("psf_long")); + e.eval("SharedObject.publicStaticFinalLong = 120;"); + assertEquals(8333333333333L, SharedObject.publicStaticFinalLong); + } + + @Test + public void accessStaticFinalFieldLongArray() throws ScriptException { + e.eval("var psf_long_array = SharedObject.publicStaticFinalLongArray;"); + assertEquals(SharedObject.publicStaticFinalLongArray[0], e.eval("SharedObject.publicStaticFinalLongArray[0];")); + assertArrayEquals(SharedObject.publicStaticFinalLongArray, (long[])e.get("psf_long_array")); + e.eval("var tsf_long_arr = new (Java.type(\"long[]\"))(3);" + + "tsf_long_arr[0] = -189009;" + + "tsf_long_arr[1] = 456;" + + "tsf_long_arr[2] = 600000001;" + + "SharedObject.publicStaticFinalLongArray = tsf_long_arr;"); + // e.eval("o.publicIntArray = [-189009,456,600000001];"); + assertArrayEquals(new long[] { 19017383333L, -2247358L, 39773787L }, SharedObject.publicStaticFinalLongArray); + e.eval("SharedObject.publicStaticFinalLongArray[0] = 10;"); + assertEquals(10, SharedObject.publicStaticFinalLongArray[0]); + } + + // --------------------------------int + // tests------------------------------------ + @Test + public void accessFieldInt() throws ScriptException { + e.eval("var p_int = o.publicInt;"); + assertEquals(o.publicInt, e.get("p_int")); + e.eval("o.publicInt = 14;"); + assertEquals(14, o.publicInt); + } + + @Test + public void accessFieldIntArray() throws ScriptException { + e.eval("var p_int_array = o.publicIntArray;"); + assertEquals(o.publicIntArray[0], e.eval("o.publicIntArray[0];")); + assertArrayEquals(o.publicIntArray, (int[])e.get("p_int_array")); + e.eval("var t_int_arr = new (Java.type(\"int[]\"))(3);" + + "t_int_arr[0] = 4;" + + "t_int_arr[1] = 5;" + + "t_int_arr[2] = 6;" + + "o.publicIntArray = t_int_arr;"); + assertArrayEquals(new int[] { 4, 5, 6 }, o.publicIntArray); + e.eval("o.publicIntArray[0] = 100;"); + assertEquals(100, o.publicIntArray[0]); + } + + @Test + public void accessStaticFieldInt() throws ScriptException { + e.eval("var ps_int = SharedObject.publicStaticInt;"); + assertEquals(SharedObject.publicStaticInt, e.get("ps_int")); + e.eval("SharedObject.publicStaticInt = 140;"); + assertEquals(140, SharedObject.publicStaticInt); + } + + @Test + public void accessStaticFieldIntArray() throws ScriptException { + e.eval("var ps_int_array = SharedObject.publicStaticIntArray;"); + assertEquals(SharedObject.publicStaticIntArray[0], e.eval("SharedObject.publicStaticIntArray[0];")); + assertArrayEquals(SharedObject.publicStaticIntArray, (int[])e.get("ps_int_array")); + e.eval("var ts_int_arr = new (Java.type(\"int[]\"))(3);" + + "ts_int_arr[0] = 4;" + + "ts_int_arr[1] = 5;" + + "ts_int_arr[2] = 6;" + + "SharedObject.publicStaticIntArray = ts_int_arr;"); + assertArrayEquals(new int[] { 4, 5, 6 }, SharedObject.publicStaticIntArray); + e.eval("SharedObject.publicStaticIntArray[0] = 100;"); + assertEquals(100, SharedObject.publicStaticIntArray[0]); + } + + @Test + public void accessFinalFieldInt() throws ScriptException { + e.eval("var pf_int = o.publicFinalInt;"); + assertEquals(o.publicFinalInt, e.get("pf_int")); + + e.eval("o.publicFinalInt = 10;"); + assertEquals(20712023, o.publicFinalInt); + } + + @Test + public void accessFinalFieldIntArray() throws ScriptException { + e.eval("var pf_int_array = o.publicFinalIntArray;"); + assertEquals(o.publicFinalIntArray[0], e.eval("o.publicFinalIntArray[0];")); + assertArrayEquals(o.publicFinalIntArray, (int[])e.get("pf_int_array")); + e.eval("var tf_int_arr = new (Java.type(\"int[]\"))(3);" + + "tf_int_arr[0] = 4;" + + "tf_int_arr[1] = 5;" + + "tf_int_arr[2] = 6;" + + "o.publicFinalIntArray = tf_int_arr;"); + assertArrayEquals(new int[] { 50, 80, 130, 210, 340 }, o.publicFinalIntArray); + e.eval("o.publicFinalIntArray[0] = 100;"); + assertEquals(100, o.publicFinalIntArray[0]); + } + + @Test + public void accessStaticFinalFieldInt() throws ScriptException { + e.eval("var psf_int = SharedObject.publicStaticFinalInt;"); + assertEquals(SharedObject.publicStaticFinalInt, e.get("psf_int")); + e.eval("SharedObject.publicStaticFinalInt = 140;"); + assertEquals(207182023, SharedObject.publicStaticFinalInt); + } + + @Test + public void accessStaticFinalFieldIntArray() throws ScriptException { + e.eval("var psf_int_array = SharedObject.publicStaticFinalIntArray;"); + assertEquals(SharedObject.publicStaticFinalIntArray[0], e.eval("SharedObject.publicStaticFinalIntArray[0];")); + assertArrayEquals(SharedObject.publicStaticFinalIntArray, (int[])e.get("psf_int_array")); + e.eval("var tsf_int_arr = new (Java.type(\"int[]\"))(3);" + + "tsf_int_arr[0] = 4;" + + "tsf_int_arr[1] = 5;" + + "tsf_int_arr[2] = 6;" + + "SharedObject.publicStaticFinalIntArray = tsf_int_arr;"); + assertArrayEquals(new int[] { 1308, 210, 340 }, SharedObject.publicStaticFinalIntArray); + e.eval("SharedObject.publicStaticFinalIntArray[0] = 100;"); + assertEquals(100, SharedObject.publicStaticFinalIntArray[0]); + } + + // --------------------------------byte + // tests------------------------------------ + @Test + public void accessFieldByte() throws ScriptException { + e.eval("var p_byte = o.publicByte;"); + assertEquals((double)o.publicByte, ((Number)e.get("p_byte")).doubleValue()); + e.eval("o.publicByte = 16;"); + assertEquals(16, o.publicByte); + } + + @Test + public void accessFieldByteArray() throws ScriptException { + e.eval("var p_byte_array = o.publicByteArray;"); + assertEquals(o.publicByteArray[0], e.eval("o.publicByteArray[0];")); + assertArrayEquals(o.publicByteArray, (byte[])e.get("p_byte_array")); + e.eval("var t_byte_arr = new (Java.type(\"byte[]\"))(3);" + + "t_byte_arr[0] = -18;" + + "t_byte_arr[1] = 56;" + + "t_byte_arr[2] = 60;" + + "o.publicByteArray = t_byte_arr;"); + assertArrayEquals(new byte[] { -18, 56, 60 }, o.publicByteArray); + e.eval("o.publicByteArray[0] = 100;"); + assertEquals(100, o.publicByteArray[0]); + } + + @Test + public void accessStaticFieldByte() throws ScriptException { + e.eval("var ps_byte = SharedObject.publicStaticByte;"); + assertEquals((double)SharedObject.publicStaticByte, ((Number)e.get("ps_byte")).doubleValue()); + e.eval("SharedObject.publicStaticByte = 16;"); + assertEquals(16, SharedObject.publicStaticByte); + } + + @Test + public void accessStaticFieldByteArray() throws ScriptException { + e.eval("var ps_byte_array = SharedObject.publicStaticByteArray;"); + assertEquals(SharedObject.publicStaticByteArray[0], e.eval("SharedObject.publicStaticByteArray[0];")); + assertArrayEquals(SharedObject.publicStaticByteArray, (byte[])e.get("ps_byte_array")); + e.eval("var ts_byte_arr = new (Java.type(\"byte[]\"))(3);" + + "ts_byte_arr[0] = -18;" + + "ts_byte_arr[1] = 56;" + + "ts_byte_arr[2] = 60;" + + "SharedObject.publicStaticByteArray = ts_byte_arr;"); + assertArrayEquals(new byte[] { -18, 56, 60 }, SharedObject.publicStaticByteArray); + e.eval("SharedObject.publicStaticByteArray[0] = -90;"); + assertEquals(-90, SharedObject.publicStaticByteArray[0]); + } + + @Test + public void accessFinalFieldByte() throws ScriptException { + e.eval("var pf_byte = o.publicFinalByte;"); + assertEquals((double)o.publicFinalByte, ((Number)e.get("pf_byte")).doubleValue()); + e.eval("o.publicFinalByte = 16;"); + assertEquals(-7, o.publicFinalByte); + } + + @Test + public void accessFinalFieldByteArray() throws ScriptException { + e.eval("var pf_byte_array = o.publicFinalByteArray;"); + assertEquals(o.publicFinalByteArray[0], e.eval("o.publicFinalByteArray[0];")); + assertArrayEquals(o.publicFinalByteArray, (byte[])e.get("pf_byte_array")); + e.eval("var tf_byte_arr = new (Java.type(\"byte[]\"))(3);" + + "tf_byte_arr[0] = -18;" + + "tf_byte_arr[1] = 56;" + + "tf_byte_arr[2] = 60;" + + "o.publicFinalByteArray = tf_byte_arr;"); + assertArrayEquals(new byte[] { 1, 3, 6, 17, -128 }, o.publicFinalByteArray); + e.eval("o.publicFinalByteArray[0] = -90;"); + assertEquals(-90, o.publicFinalByteArray[0]); + } + + @Test + public void accessStaticFinalFieldByte() throws ScriptException { + e.eval("var psf_byte = SharedObject.publicStaticFinalByte;"); + assertEquals((double)SharedObject.publicStaticFinalByte, ((Number)e.get("psf_byte")).doubleValue()); + e.eval("SharedObject.publicStaticFinalByte = 16;"); + assertEquals(-70, SharedObject.publicStaticFinalByte); + } + + @Test + public void accessStaticFinalFieldByteArray() throws ScriptException { + e.eval("var psf_byte_array = SharedObject.publicStaticFinalByteArray;"); + assertEquals(SharedObject.publicStaticFinalByteArray[0], e.eval("SharedObject.publicStaticFinalByteArray[0];")); + assertArrayEquals(SharedObject.publicStaticFinalByteArray, (byte[])e.get("psf_byte_array")); + e.eval("var tsf_byte_arr = new (Java.type(\"byte[]\"))(3);" + + "tsf_byte_arr[0] = -18;" + + "tsf_byte_arr[1] = 56;" + + "tsf_byte_arr[2] = 60;" + + "SharedObject.publicStaticFinalByteArray = tsf_byte_arr;"); + assertArrayEquals(new byte[] { 17, -128, 81 }, SharedObject.publicStaticFinalByteArray); + e.eval("SharedObject.publicStaticFinalByteArray[0] = -90;"); + assertEquals(-90, SharedObject.publicStaticFinalByteArray[0]); + } + + // --------------------------------short + // tests------------------------------------ + @Test + public void accessFieldShort() throws ScriptException { + e.eval("var p_short = o.publicShort;"); + assertEquals((double)o.publicShort, ((Number)e.get("p_short")).doubleValue()); + e.eval("o.publicShort = 18;"); + assertEquals(18, o.publicShort); + } + + @Test + public void accessFieldShortArray() throws ScriptException { + e.eval("var p_short_array = o.publicShortArray;"); + assertEquals(o.publicShortArray[0], e.eval("o.publicShortArray[0];")); + assertArrayEquals(o.publicShortArray, (short[])e.get("p_short_array")); + e.eval("var t_short_arr = new (Java.type(\"short[]\"))(3);" + + "t_short_arr[0] = 90;" + + "t_short_arr[1] = 5;" + + "t_short_arr[2] = -6000;" + + "o.publicShortArray = t_short_arr;"); + assertArrayEquals(new short[] { 90, 5, -6000 }, o.publicShortArray); + e.eval("o.publicShortArray[0] = -1000;"); + assertEquals(-1000, o.publicShortArray[0]); + } + + @Test + public void accessStaticFieldShort() throws ScriptException { + e.eval("var ps_short = SharedObject.publicStaticShort;"); + assertEquals((double)SharedObject.publicStaticShort, ((Number)e.get("ps_short")).doubleValue()); + e.eval("SharedObject.publicStaticShort = 180;"); + assertEquals(180, SharedObject.publicStaticShort); + } + + @Test + public void accessStaticFieldShortArray() throws ScriptException { + e.eval("var ps_short_array = SharedObject.publicStaticShortArray;"); + assertEquals(SharedObject.publicStaticShortArray[0], e.eval("SharedObject.publicStaticShortArray[0];")); + assertArrayEquals(SharedObject.publicStaticShortArray, (short[])e.get("ps_short_array")); + e.eval("var ts_short_arr = new (Java.type(\"short[]\"))(3);" + + "ts_short_arr[0] = 90;" + + "ts_short_arr[1] = 5;" + + "ts_short_arr[2] = -6000;" + + "SharedObject.publicStaticShortArray = ts_short_arr;"); + assertArrayEquals(new short[] { 90, 5, -6000 }, SharedObject.publicStaticShortArray); + e.eval("SharedObject.publicStaticShortArray[0] = -1000;"); + assertEquals(-1000, SharedObject.publicStaticShortArray[0]); + } + + @Test + public void accessFinalFieldShort() throws ScriptException { + e.eval("var pf_short = o.publicFinalShort;"); + assertEquals((double)o.publicFinalShort, ((Number)e.get("pf_short")).doubleValue()); + e.eval("o.publicFinalShort = 180;"); + assertEquals(31220, o.publicFinalShort); + } + + @Test + public void accessFinalFieldShortArray() throws ScriptException { + e.eval("var pf_short_array = o.publicFinalShortArray;"); + assertEquals(o.publicFinalShortArray[0], e.eval("o.publicFinalShortArray[0];")); + assertArrayEquals(o.publicFinalShortArray, (short[])e.get("pf_short_array")); + e.eval("var tf_short_arr = new (Java.type(\"short[]\"))(3);" + + "tf_short_arr[0] = 90;" + + "tf_short_arr[1] = 5;" + + "tf_short_arr[2] = -6000;" + + "o.publicFinalShortArray = tf_short_arr;"); + assertArrayEquals(new short[] { 12240, 9200, -17289, 1200, 12 }, o.publicFinalShortArray); + e.eval("o.publicFinalShortArray[0] = -1000;"); + assertEquals(-1000, o.publicFinalShortArray[0]); + } + + @Test + public void accessStaticFinalFieldShort() throws ScriptException { + e.eval("var psf_short = SharedObject.publicStaticFinalShort;"); + assertEquals((double)SharedObject.publicStaticFinalShort, ((Number)e.get("psf_short")).doubleValue()); + e.eval("SharedObject.publicStaticFinalShort = 180;"); + assertEquals(8888, SharedObject.publicStaticFinalShort); + } + + @Test + public void accessStaticFinalFieldShortArray() throws ScriptException { + e.eval("var psf_short_array = SharedObject.publicStaticFinalShortArray;"); + assertEquals(SharedObject.publicStaticFinalShortArray[0], e.eval("SharedObject.publicStaticFinalShortArray[0];")); + assertArrayEquals(SharedObject.publicStaticFinalShortArray, (short[])e.get("psf_short_array")); + e.eval("var tsf_short_arr = new (Java.type(\"short[]\"))(3);" + + "tsf_short_arr[0] = 90;" + + "tsf_short_arr[1] = 5;" + + "tsf_short_arr[2] = -6000;" + + "SharedObject.publicStaticFinalShortArray = tsf_short_arr;"); + assertArrayEquals(new short[] { 8240, 9280, -1289, 120, 812 }, SharedObject.publicStaticFinalShortArray); + e.eval("SharedObject.publicStaticFinalShortArray[0] = -1000;"); + assertEquals(-1000, SharedObject.publicStaticFinalShortArray[0]); + } + + // --------------------------------char + // tests------------------------------------ + @Test + public void accessFieldChar() throws ScriptException { + e.eval("var p_char = o.publicChar;"); + assertEquals(o.publicChar, e.get("p_char")); + e.eval("o.publicChar = 'S';"); + assertEquals('S', o.publicChar); + e.eval("o.publicChar = 10;"); + assertEquals(10, o.publicChar); + e.eval("try {" + + " o.publicChar = 'Big string';" + + "} catch(e) {" + + " var isThrown = true;" + + "}"); + assertEquals("Exception thrown", true, e.get("isThrown")); + assertEquals(10, o.publicChar); + } + + @Test + public void accessFieldCharArray() throws ScriptException { + e.eval("var p_char_array = o.publicCharArray;"); + assertEquals(o.publicCharArray[0], e.eval("o.publicCharArray[0];")); + assertArrayEquals(o.publicCharArray, (char[])e.get("p_char_array")); + e.eval("var t_char_arr = new (Java.type(\"char[]\"))(3);" + + "t_char_arr[0] = 'F';" + + "t_char_arr[1] = 'o';" + + "t_char_arr[2] = 'o';" + + "o.publicCharArray = t_char_arr;"); + assertArrayEquals("Foo".toCharArray(), o.publicCharArray); + e.eval("o.publicCharArray[0] = 'Z';"); + assertEquals('Z', o.publicCharArray[0]); + } + + @Test + public void accessStaticFieldChar() throws ScriptException { + e.eval("var ps_char = SharedObject.publicStaticChar;"); + assertEquals(SharedObject.publicStaticChar, e.get("ps_char")); + e.eval("SharedObject.publicStaticChar = 'Z';"); + assertEquals('Z', SharedObject.publicStaticChar); + } + + @Test + public void accessStaticFieldCharArray() throws ScriptException { + e.eval("var ps_char_array = SharedObject.publicStaticCharArray;"); + assertEquals(SharedObject.publicStaticCharArray[0], e.eval("SharedObject.publicStaticCharArray[0];")); + assertArrayEquals(SharedObject.publicStaticCharArray, (char[])e.get("ps_char_array")); + e.eval("var ts_char_arr = new (Java.type(\"char[]\"))(3);" + + "ts_char_arr[0] = 'G';" + + "ts_char_arr[1] = 'o';" + + "ts_char_arr[2] = 'o';" + + "SharedObject.publicStaticCharArray = ts_char_arr;"); + assertArrayEquals("Goo".toCharArray(), SharedObject.publicStaticCharArray); + e.eval("SharedObject.publicStaticCharArray[0] = 'Z';"); + assertEquals('Z', SharedObject.publicStaticCharArray[0]); + } + + @Test + public void accessFinalFieldChar() throws ScriptException { + e.eval("var pf_char = o.publicFinalChar;"); + assertEquals(o.publicFinalChar, e.get("pf_char")); + e.eval("o.publicFinalChar = 'S';"); + assertEquals('E', o.publicFinalChar); + } + + @Test + public void accessFinalCharArray() throws ScriptException { + e.eval("var pf_char_array = o.publicFinalCharArray;"); + assertEquals(o.publicFinalCharArray[0], e.eval("o.publicFinalCharArray[0];")); + assertArrayEquals(o.publicFinalCharArray, (char[])e.get("pf_char_array")); + e.eval("var tf_char_arr = new (Java.type(\"char[]\"))(3);" + + "tf_char_arr[0] = 'F';" + + "tf_char_arr[1] = 'o';" + + "tf_char_arr[2] = 'o';" + + "o.publicFinalCharArray = tf_char_arr;"); + assertArrayEquals("Nashorn hello".toCharArray(), o.publicFinalCharArray); + e.eval("o.publicFinalCharArray[0] = 'Z';"); + assertEquals('Z', o.publicFinalCharArray[0]); + } + + @Test + public void accessStaticFinalFieldChar() throws ScriptException { + e.eval("var psf_char = SharedObject.publicStaticFinalChar;"); + assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char")); + e.eval("SharedObject.publicStaticFinalChar = 'Z';"); + assertEquals('K', SharedObject.publicStaticFinalChar); + } + + @Test + public void accessStaticFinalFieldCharArray() throws ScriptException { + e.eval("var psf_char_array = SharedObject.publicStaticFinalCharArray;"); + assertEquals(SharedObject.publicStaticFinalCharArray[0], e.eval("SharedObject.publicStaticFinalCharArray[0];")); + assertArrayEquals(SharedObject.publicStaticFinalCharArray, (char[])e.get("psf_char_array")); + e.eval("var tsf_char_arr = new (Java.type(\"char[]\"))(3);" + + "tsf_char_arr[0] = 'Z';" + + "tsf_char_arr[1] = 'o';" + + "tsf_char_arr[2] = 'o';" + + "SharedObject.publicStaticFinalCharArray = tsf_char_arr;"); + assertArrayEquals("StaticString".toCharArray(), SharedObject.publicStaticFinalCharArray); + e.eval("SharedObject.publicStaticFinalCharArray[0] = 'Z';"); + assertEquals('Z', SharedObject.publicStaticFinalCharArray[0]); + } + + // --------------------------------float + // tests------------------------------------ + @Test + public void accessFieldFloat() throws ScriptException { + e.eval("var p_float = o.publicFloat;"); + assertEquals((double)o.publicFloat, ((Number)e.get("p_float")).doubleValue()); + o.publicFloat = 0.0f / 0.0f; + assertEquals(true, e.eval("isNaN(o.publicFloat)")); + o.publicFloat = 1.0f / 0.0f; + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicFloat")); + o.publicFloat = -1.0f / 0.0f; + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicFloat")); + e.eval("o.publicFloat = 20;"); + assertEquals(20, o.publicFloat, 1e-10); + e.eval("o.publicFloat = 0.0/0.0;"); + assertTrue(Float.isNaN(o.publicFloat)); + e.eval("o.publicFloat = 1.0/0.0;"); + assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(o.publicFloat)); + e.eval("o.publicFloat = -1.0/0.0;"); + assertEquals(Float.NEGATIVE_INFINITY, o.publicFloat, 1e-10); + } + + @Test + public void accessFieldFloatArray() throws ScriptException { + e.eval("var p_float_array = o.publicFloatArray;"); + assertEquals(o.publicFloatArray[0], e.eval("o.publicFloatArray[0];")); + assertArrayEquals(o.publicFloatArray, (float[])e.get("p_float_array"), 1e-10f); + e.eval("var t_float_arr = new (Java.type(\"float[]\"))(3);" + + "t_float_arr[0] = 9.0;" + + "t_float_arr[1] = 5.12345;" + + "t_float_arr[2] = -60.03;" + + "o.publicFloatArray = t_float_arr;"); + assertArrayEquals(new float[] { 9.0f, 5.12345f, -60.03f }, o.publicFloatArray, 1e-10f); + e.eval("o.publicFloatArray[0] = -513.2;"); + assertArrayEquals(new float[] { -513.2f, 5.12345f, -60.03f }, o.publicFloatArray, 1e-10f); + } + + @Test + public void accessStaticFieldFloat() throws ScriptException { + e.eval("var ps_float = SharedObject.publicStaticFloat;"); + assertEquals((double)SharedObject.publicStaticFloat, ((Number)e.get("ps_float")).doubleValue()); + SharedObject.publicStaticFloat = 0.0f / 0.0f; + assertEquals(true, e.eval("isNaN(SharedObject.publicStaticFloat)")); + SharedObject.publicStaticFloat = 1.0f / 0.0f; + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticFloat")); + SharedObject.publicStaticFloat = -1.0f / 0.0f; + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticFloat")); + e.eval("SharedObject.publicStaticFloat = 20.0;"); + assertEquals(20.0f, SharedObject.publicStaticFloat, 1e-10); + e.eval("SharedObject.publicStaticFloat = 0.0/0.0;"); + assertTrue(Float.isNaN(SharedObject.publicStaticFloat)); + e.eval("SharedObject.publicStaticFloat = 1.0/0.0;"); + assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); + e.eval("SharedObject.publicStaticFloat = -1.0/0.0;"); + assertEquals(Float.floatToIntBits(Float.NEGATIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); + } + + @Test + public void accessStaticFieldFloatArray() throws ScriptException { + e.eval("var ps_float_array = SharedObject.publicStaticFloatArray;"); + assertEquals(SharedObject.publicStaticFloatArray[0], e.eval("SharedObject.publicStaticFloatArray[0];")); + assertArrayEquals(SharedObject.publicStaticFloatArray, (float[])e.get("ps_float_array"), 1e-10f); + e.eval("var ts_float_arr = new (Java.type(\"float[]\"))(3);" + + "ts_float_arr[0] = 9.0;" + + "ts_float_arr[1] = 5.12345;" + + "ts_float_arr[2] = -60.03;" + + "SharedObject.publicStaticFloatArray = ts_float_arr;"); + assertArrayEquals(new float[] { 9.0f, 5.12345f, -60.03f }, SharedObject.publicStaticFloatArray, 1e-10f); + e.eval("SharedObject.publicStaticFloatArray[0] = -513.2;"); + assertArrayEquals(new float[] { -513.2f, 5.12345f, -60.03f }, SharedObject.publicStaticFloatArray, 1e-10f); + } + + @Test + public void accessFinalFloat() throws ScriptException { + e.eval("var pf_float = o.publicFinalFloat;"); + assertEquals((double)o.publicFinalFloat, ((Number)e.get("pf_float")).doubleValue()); + e.eval("o.publicFinalFloat = 20.0;"); + assertEquals(7.72e8f, o.publicFinalFloat, 1e-10); + } + + @Test + public void accessFinalFloatArray() throws ScriptException { + e.eval("var pf_float_array = o.publicFinalFloatArray;"); + assertEquals(o.publicFinalFloatArray[0], e.eval("o.publicFinalFloatArray[0];")); + assertArrayEquals(o.publicFinalFloatArray, (float[])e.get("pf_float_array"), 1e-10f); + e.eval("var tf_float_arr = new (Java.type(\"float[]\"))(3);" + + "tf_float_arr[0] = 9.0;" + + "tf_float_arr[1] = 5.12345;" + + "tf_float_arr[2] = -60.03;" + + "o.publicFinalFloatArray = tf_float_arr;"); + assertArrayEquals(new float[] { -131.012f, 189.32f, -31.32e8f, 3.72f }, o.publicFinalFloatArray, 1e-10f); + e.eval("o.publicFinalFloatArray[0] = -513.2;"); + assertEquals(-513.2f, o.publicFinalFloatArray[0], 1e-10f); + } + + @Test + public void accessStaticFinalFieldFloat() throws ScriptException { + e.eval("var psf_float = SharedObject.publicStaticFinalFloat;"); + assertEquals((double)SharedObject.publicStaticFinalFloat, ((Number)e.get("psf_float")).doubleValue()); + e.eval("SharedObject.publicStaticFinalFloat = 20.0;"); + assertEquals(0.72e8f, SharedObject.publicStaticFinalFloat, 1e-10); + } + + @Test + public void accessStaticFinalFieldFloatArray() throws ScriptException { + e.eval("var psf_float_array = SharedObject.publicStaticFinalFloatArray;"); + assertEquals(SharedObject.publicStaticFinalFloatArray[0], e.eval("SharedObject.publicStaticFinalFloatArray[0];")); + assertArrayEquals(SharedObject.publicStaticFinalFloatArray, (float[])e.get("psf_float_array"), 1e-10f); + e.eval("var tsf_float_arr = new (Java.type(\"float[]\"))(3);" + + "tsf_float_arr[0] = 9.0;" + + "tsf_float_arr[1] = 5.12345;" + + "tsf_float_arr[2] = -60.03;" + + "SharedObject.publicStaticFinalFloatArray = tsf_float_arr;"); + assertArrayEquals(new float[] { -8131.012f, 9.32f, -138.32e8f, 0.72f }, SharedObject.publicStaticFinalFloatArray, 1e-10f); + e.eval("SharedObject.publicStaticFinalFloatArray[0] = -513.2;"); + assertEquals(-513.2f, SharedObject.publicStaticFinalFloatArray[0], 1e-10f); + } + + // --------------------------------double + // tests------------------------------------ + @Test + public void accessFieldDouble() throws ScriptException { + e.eval("var p_double = o.publicDouble;"); + assertEquals(o.publicDouble, e.get("p_double")); + o.publicDouble = 0.0 / 0.0; + assertEquals(true, e.eval("isNaN(o.publicDouble)")); + o.publicDouble = 1.0 / 0.0; + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicDouble")); + o.publicDouble = -1.0 / 0.0; + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicDouble")); + e.eval("o.publicDouble = 30;"); + assertEquals(Double.doubleToLongBits(30.0), Double.doubleToLongBits(o.publicDouble)); + e.eval("o.publicDouble = 0.0/0.0;"); + assertTrue(Double.isNaN(o.publicDouble)); + e.eval("o.publicDouble = 1.0/0.0;"); + assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(o.publicDouble)); + e.eval("o.publicDouble = -1.0/0.0;"); + assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(o.publicDouble)); + } + + @Test + public void accessFieldDoubleArrayRead() throws ScriptException { + e.eval("var p_double_array = o.publicDoubleArray;"); + assertEquals(o.publicDoubleArray[0], e.eval("o.publicDoubleArray[0];")); + assertArrayEquals(o.publicDoubleArray, (double[])e.get("p_double_array"), 1e-10); + e.eval("var t_double_arr = new (Java.type(\"double[]\"))(3);" + + "t_double_arr[0] = 9e10;" + + "t_double_arr[1] = 0.677777;" + + "t_double_arr[2] = -0.0000001;" + + "o.publicDoubleArray = t_double_arr;"); + assertArrayEquals(new double[] { 9e10, 0.677777, -0.0000001 }, o.publicDoubleArray, 1e-10f); + e.eval("o.publicDoubleArray[0] = -5.2e10;"); + assertEquals(-5.2e10, o.publicDoubleArray[0], 1e-10f); + } + + @Test + public void accessStaticFieldDouble() throws ScriptException { + e.eval("var ps_double = SharedObject.publicStaticDouble;"); + assertEquals(SharedObject.publicStaticDouble, e.get("ps_double")); + SharedObject.publicStaticDouble = 0.0 / 0.0; + assertEquals(true, e.eval("isNaN(SharedObject.publicStaticDouble)")); + SharedObject.publicStaticDouble = 1.0 / 0.0; + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticDouble")); + SharedObject.publicStaticDouble = -1.0 / 0.0; + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticDouble")); + e.eval("SharedObject.publicStaticDouble = 40.0;"); + assertEquals(Double.doubleToLongBits(40.0), Double.doubleToLongBits(SharedObject.publicStaticDouble)); + e.eval("SharedObject.publicStaticDouble = 0.0/0.0;"); + assertTrue(Double.isNaN(SharedObject.publicStaticDouble)); + e.eval("SharedObject.publicStaticDouble = 1.0/0.0;"); + assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); + e.eval("SharedObject.publicStaticDouble = -1.0/0.0;"); + assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); + } + + @Test + public void accessStaticFieldDoubleArrayRead() throws ScriptException { + e.eval("var ps_double_array = SharedObject.publicStaticDoubleArray;"); + assertEquals(SharedObject.publicStaticDoubleArray[0], e.eval("SharedObject.publicStaticDoubleArray[0];")); + assertArrayEquals(SharedObject.publicStaticDoubleArray, (double[])e.get("ps_double_array"), 1e-10); + e.eval("var ts_double_arr = new (Java.type(\"double[]\"))(3);" + + "ts_double_arr[0] = 9e10;" + + "ts_double_arr[1] = 0.677777;" + + "ts_double_arr[2] = -0.0000001;" + + "SharedObject.publicStaticDoubleArray = ts_double_arr;"); + assertArrayEquals(new double[] { 9e10, 0.677777, -0.0000001 }, SharedObject.publicStaticDoubleArray, 1e-10f); + e.eval("SharedObject.publicStaticDoubleArray[0] = -5.2e10;"); + assertEquals(-5.2e10, SharedObject.publicStaticDoubleArray[0], 1e-10f); + } + + @Test + public void accessFinalFieldDouble() throws ScriptException { + e.eval("var pf_double = o.publicFinalDouble;"); + assertEquals(o.publicFinalDouble, e.get("pf_double")); + e.eval("o.publicFinalDouble = 30.0;"); + assertEquals(Double.doubleToLongBits(1.3412e20), Double.doubleToLongBits(o.publicFinalDouble)); + } + + @Test + public void accessFinalFieldDoubleArrayRead() throws ScriptException { + e.eval("var pf_double_array = o.publicFinalDoubleArray;"); + assertEquals(o.publicFinalDoubleArray[0], e.eval("o.publicFinalDoubleArray[0];")); + assertArrayEquals(o.publicFinalDoubleArray, (double[])e.get("pf_double_array"), 1e-10); + e.eval("var tf_double_arr = new (Java.type(\"double[]\"))(3);" + + "tf_double_arr[0] = 9e10;" + + "tf_double_arr[1] = 0.677777;" + + "tf_double_arr[2] = -0.0000001;" + + "o.publicFinalDoubleArray = tf_double_arr;"); + assertArrayEquals(new double[] { 0.725e80, 0.12e10, 8e-3, 1.00077 }, o.publicFinalDoubleArray, 1e-10f); + e.eval("o.publicFinalDoubleArray[0] = -5.2e10;"); + assertEquals(-5.2e10, o.publicFinalDoubleArray[0], 1e-10f); + } + + @Test + public void accessStaticFinalFieldDouble() throws ScriptException { + e.eval("var psf_double = SharedObject.publicStaticFinalDouble;"); + assertEquals(SharedObject.publicStaticFinalDouble, e.get("psf_double")); + e.eval("SharedObject.publicStaticFinalDouble = 40.0;"); + assertEquals(Double.doubleToLongBits(1.8e12), Double.doubleToLongBits(SharedObject.publicStaticFinalDouble)); + } + + @Test + public void accessStaticFinalFieldDoubleArrayRead() throws ScriptException { + e.eval("var psf_double_array = SharedObject.publicStaticFinalDoubleArray;"); + assertEquals(SharedObject.publicStaticFinalDoubleArray[0], e.eval("SharedObject.publicStaticFinalDoubleArray[0];")); + assertArrayEquals(SharedObject.publicStaticFinalDoubleArray, (double[])e.get("psf_double_array"), 1e-10); + e.eval("var tsf_double_arr = new (Java.type(\"double[]\"))(3);" + + "tsf_double_arr[0] = 9e10;" + + "tsf_double_arr[1] = 0.677777;" + + "tsf_double_arr[2] = -0.0000001;" + + "SharedObject.publicStaticFinalDoubleArray = tsf_double_arr;"); + assertArrayEquals(new double[] { 8.725e80, 0.82e10, 18e-3, 1.08077 }, SharedObject.publicStaticFinalDoubleArray, 1e-10f); + e.eval("SharedObject.publicStaticFinalDoubleArray[0] = -5.2e10;"); + assertEquals(-5.2e10, SharedObject.publicStaticFinalDoubleArray[0], 1e-10f); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/NumberBoxingTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,365 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.testng.TestNG; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * @test + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.NumberBoxingTest + * @run testng/othervm jdk.nashorn.api.javaaccess.NumberBoxingTest + */ +@SuppressWarnings("javadoc") +public class NumberBoxingTest { + + private static ScriptEngine e; + private static SharedObject o; + + public static void main(final String[] args) { + TestNG.main(args); + } + + @BeforeClass + public static void setUpClass() throws ScriptException { + final ScriptEngineManager m = new ScriptEngineManager(); + e = m.getEngineByName("nashorn"); + o = new SharedObject(); + e.put("o", o); + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); + } + + @AfterClass + public static void tearDownClass() { + e = null; + o = null; + } + + // --------------------------------long + // tests------------------------------------ + @Test + public void accessFieldLongBoxing() throws ScriptException { + e.eval("var p_long = o.publicLongBox;"); + assertEquals(o.publicLongBox, e.get("p_long")); + e.eval("o.publicLongBox = 12;"); + assertEquals(Long.valueOf(12), o.publicLongBox); + } + + @Test + public void accessStaticFieldLongBoxing() throws ScriptException { + e.eval("var ps_long = SharedObject.publicStaticLongBox;"); + assertEquals(SharedObject.publicStaticLongBox, e.get("ps_long")); + e.eval("SharedObject.publicStaticLongBox = 120;"); + assertEquals(120L, SharedObject.publicStaticLongBox.longValue()); + } + + @Test + public void accessFinalFieldLongBoxing() throws ScriptException { + e.eval("var pf_long = o.publicFinalLongBox;"); + assertEquals(o.publicFinalLongBox, e.get("pf_long")); + e.eval("o.publicFinalLongBox = 120;"); + assertEquals(Long.valueOf(9377333334L), o.publicFinalLongBox); + } + + @Test + public void accessStaticFinalFieldLongBoxing() throws ScriptException { + e.eval("var psf_long = SharedObject.publicStaticFinalLong;"); + assertEquals(SharedObject.publicStaticFinalLong, e.get("psf_long")); + e.eval("SharedObject.publicStaticFinalLong = 120;"); + assertEquals(8333333333333L, SharedObject.publicStaticFinalLong); + } + + // --------------------------------int + // tests------------------------------------ + @Test + public void accessFieldIntBoxing() throws ScriptException { + e.eval("var p_int = o.publicIntBox;"); + assertEquals(o.publicIntBox, e.get("p_int")); + e.eval("o.publicIntBox = 14;"); + assertEquals(Integer.valueOf(14), o.publicIntBox); + } + + @Test + public void accessStaticFieldIntBoxing() throws ScriptException { + e.eval("var ps_int = SharedObject.publicStaticInt;"); + assertEquals(SharedObject.publicStaticInt, e.get("ps_int")); + e.eval("SharedObject.publicStaticInt = 140;"); + assertEquals(140, SharedObject.publicStaticInt); + } + + @Test + public void accessFinalFieldIntBoxing() throws ScriptException { + e.eval("var pf_int = o.publicFinalIntBox;"); + assertEquals(o.publicFinalIntBox, e.get("pf_int")); + e.eval("o.publicFinalIntBox = 10;"); + assertEquals(Integer.valueOf(207512301), o.publicFinalIntBox); + } + + @Test + public void accessStaticFinalFieldIntBoxing() throws ScriptException { + e.eval("var psf_int = SharedObject.publicStaticFinalInt;"); + assertEquals(SharedObject.publicStaticFinalInt, e.get("psf_int")); + e.eval("SharedObject.publicStaticFinalInt = 140;"); + assertEquals(207182023, SharedObject.publicStaticFinalInt); + } + + // --------------------------------byte + // tests------------------------------------ + @Test + public void accessFieldByteBoxing() throws ScriptException { + e.eval("var p_byte = o.publicByteBox;"); + assertEqualsDouble(o.publicByteBox, "p_byte"); + e.eval("o.publicByteBox = 16;"); + assertEquals(Byte.valueOf((byte)16), o.publicByteBox); + } + + @Test + public void accessStaticFieldByteBoxing() throws ScriptException { + e.eval("var ps_byte = SharedObject.publicStaticByte;"); + assertEqualsDouble(SharedObject.publicStaticByte, "ps_byte"); + e.eval("SharedObject.publicStaticByte = 16;"); + assertEquals(16, SharedObject.publicStaticByte); + } + + @Test + public void accessFinalFieldByteBoxing() throws ScriptException { + e.eval("var pf_byte = o.publicFinalByteBox;"); + assertEqualsDouble(o.publicFinalByteBox, "pf_byte"); + e.eval("o.publicFinalByteBox = 16;"); + assertEquals(Byte.valueOf((byte)19), o.publicFinalByteBox); + } + + @Test + public void accessStaticFinalFieldByteBoxing() throws ScriptException { + e.eval("var psf_byte = SharedObject.publicStaticFinalByte;"); + assertEqualsDouble(SharedObject.publicStaticFinalByte, "psf_byte"); + e.eval("SharedObject.publicStaticFinalByte = 16;"); + assertEquals(-70, SharedObject.publicStaticFinalByte); + } + + // --------------------------------short + // tests------------------------------------ + @Test + public void accessFieldShortBoxing() throws ScriptException { + e.eval("var p_short = o.publicShortBox;"); + assertEqualsDouble(o.publicShortBox, "p_short"); + e.eval("o.publicShortBox = 18;"); + assertEquals(Short.valueOf((short)18), o.publicShortBox); + } + + private static void assertEqualsDouble(final Number n, final String name) { + assertEquals(n.doubleValue(), ((Number)e.get(name)).doubleValue()); + } + + @Test + public void accessStaticFieldShortBoxing() throws ScriptException { + e.eval("var ps_short = SharedObject.publicStaticShort;"); + assertEqualsDouble(SharedObject.publicStaticShort, "ps_short"); + e.eval("SharedObject.publicStaticShort = 180;"); + assertEquals(180, SharedObject.publicStaticShort); + } + + @Test + public void accessFinalFieldShortBoxing() throws ScriptException { + e.eval("var pf_short = o.publicFinalShortBox;"); + assertEqualsDouble(o.publicFinalShortBox, "pf_short"); + e.eval("o.publicFinalShortBox = 180;"); + assertEquals(Short.valueOf((short)-26777), o.publicFinalShortBox); + } + + @Test + public void accessStaticFinalFieldShortBoxing() throws ScriptException { + e.eval("var psf_short = SharedObject.publicStaticFinalShort;"); + assertEqualsDouble(SharedObject.publicStaticFinalShort, "psf_short"); + e.eval("SharedObject.publicStaticFinalShort = 180;"); + assertEquals(8888, SharedObject.publicStaticFinalShort); + } + + // --------------------------------char + // tests------------------------------------ + @Test + public void accessFieldCharBoxing() throws ScriptException { + e.eval("var p_char = o.publicCharBox;"); + assertEquals(o.publicCharBox, e.get("p_char")); + e.eval("o.publicCharBox = 'S';"); + assertEquals(Character.valueOf('S'), o.publicCharBox); + e.eval("try {" + + " o.publicCharBox = 'Big string';" + + "} catch(e) {" + + " var isThrown = true;" + + "}"); + assertEquals("Exception thrown", true, e.get("isThrown")); + assertEquals(Character.valueOf('S'), o.publicCharBox); + } + + @Test + public void accessStaticFieldCharBoxing() throws ScriptException { + e.eval("var ps_char = SharedObject.publicStaticChar;"); + assertEquals(SharedObject.publicStaticChar, e.get("ps_char")); + e.eval("SharedObject.publicStaticChar = 'Z';"); + assertEquals('Z', SharedObject.publicStaticChar); + } + + @Test + public void accessFinalFieldCharBoxing() throws ScriptException { + e.eval("var pf_char = o.publicFinalCharBox;"); + assertEquals(o.publicFinalCharBox, e.get("pf_char")); + e.eval("o.publicFinalCharBox = 'S';"); + assertEquals(Character.valueOf('F'), o.publicFinalCharBox); + } + + @Test + public void accessStaticFinalFieldCharBoxing() throws ScriptException { + e.eval("var psf_char = SharedObject.publicStaticFinalChar;"); + assertEquals(SharedObject.publicStaticFinalChar, e.get("psf_char")); + e.eval("SharedObject.publicStaticFinalChar = 'Z';"); + assertEquals('K', SharedObject.publicStaticFinalChar); + } + + // --------------------------------float + // tests------------------------------------ + @Test + public void accessFieldFloatBoxing() throws ScriptException { + e.eval("var p_float = o.publicFloatBox;"); + assertEqualsDouble(o.publicFloatBox, "p_float"); + o.publicFloatBox = 0.0f / 0.0f; + assertEquals(true, e.eval("isNaN(o.publicFloatBox)")); + o.publicFloatBox = 1.0f / 0.0f; + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicFloatBox")); + o.publicFloatBox = -1.0f / 0.0f; + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicFloatBox")); + e.eval("o.publicFloatBox = 20;"); + assertEquals(20, o.publicFloatBox, 1e-10); + e.eval("o.publicFloatBox = 0.0/0.0;"); + assertTrue(Float.isNaN(o.publicFloatBox)); + e.eval("o.publicFloatBox = 1.0/0.0;"); + assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(o.publicFloatBox)); + e.eval("o.publicFloatBox = -1.0/0.0;"); + assertEquals(Float.NEGATIVE_INFINITY, o.publicFloatBox, 1e-10); + } + + @Test + public void accessStaticFieldFloatBoxing() throws ScriptException { + e.eval("var ps_float = SharedObject.publicStaticFloat;"); + assertEqualsDouble(SharedObject.publicStaticFloat, "ps_float"); + SharedObject.publicStaticFloat = 0.0f / 0.0f; + assertEquals(true, e.eval("isNaN(SharedObject.publicStaticFloat)")); + SharedObject.publicStaticFloat = 1.0f / 0.0f; + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticFloat")); + SharedObject.publicStaticFloat = -1.0f / 0.0f; + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticFloat")); + e.eval("SharedObject.publicStaticFloat = 20.0;"); + assertEquals(20.0f, SharedObject.publicStaticFloat, 1e-10); + e.eval("SharedObject.publicStaticFloat = 0.0/0.0;"); + assertTrue(Float.isNaN(SharedObject.publicStaticFloat)); + e.eval("SharedObject.publicStaticFloat = 1.0/0.0;"); + assertEquals(Float.floatToIntBits(Float.POSITIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); + e.eval("SharedObject.publicStaticFloat = -1.0/0.0;"); + assertEquals(Float.floatToIntBits(Float.NEGATIVE_INFINITY), Float.floatToIntBits(SharedObject.publicStaticFloat)); + } + + @Test + public void accessFinalFloatBoxing() throws ScriptException { + e.eval("var pf_float = o.publicFinalFloatBox;"); + assertEqualsDouble(o.publicFinalFloatBox, "pf_float"); + e.eval("o.publicFinalFloatBox = 20.0;"); + assertEquals(1.372e4f, o.publicFinalFloatBox, 1e-10); + } + + @Test + public void accessStaticFinalFieldFloatBoxing() throws ScriptException { + e.eval("var psf_float = SharedObject.publicStaticFinalFloat;"); + assertEqualsDouble(SharedObject.publicStaticFinalFloat, "psf_float"); + e.eval("SharedObject.publicStaticFinalFloat = 20.0;"); + assertEquals(0.72e8f, SharedObject.publicStaticFinalFloat, 1e-10); + } + + // --------------------------------double + // tests------------------------------------ + @Test + public void accessFieldDoubleBoxing() throws ScriptException { + e.eval("var p_double = o.publicDoubleBox;"); + assertEquals(o.publicDoubleBox, e.get("p_double")); + o.publicDoubleBox = 0.0 / 0.0; + assertEquals(true, e.eval("isNaN(o.publicDoubleBox)")); + o.publicDoubleBox = 1.0 / 0.0; + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === o.publicDoubleBox")); + o.publicDoubleBox = -1.0 / 0.0; + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === o.publicDoubleBox")); + e.eval("o.publicDoubleBox = 30;"); + assertEquals(Double.doubleToLongBits(30.0), Double.doubleToLongBits(o.publicDoubleBox)); + e.eval("o.publicDoubleBox = 0.0/0.0;"); + assertTrue(Double.isNaN(o.publicDoubleBox)); + e.eval("o.publicDoubleBox = 1.0/0.0;"); + assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(o.publicDoubleBox)); + e.eval("o.publicDoubleBox = -1.0/0.0;"); + assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(o.publicDoubleBox)); + } + + @Test + public void accessStaticFieldDoubleBoxing() throws ScriptException { + e.eval("var ps_double = SharedObject.publicStaticDouble;"); + assertEquals(SharedObject.publicStaticDouble, e.get("ps_double")); + SharedObject.publicStaticDouble = 0.0 / 0.0; + assertEquals(true, e.eval("isNaN(SharedObject.publicStaticDouble)")); + SharedObject.publicStaticDouble = 1.0 / 0.0; + assertEquals(true, e.eval("Number.POSITIVE_INFINITY === SharedObject.publicStaticDouble")); + SharedObject.publicStaticDouble = -1.0 / 0.0; + assertEquals(true, e.eval("Number.NEGATIVE_INFINITY === SharedObject.publicStaticDouble")); + e.eval("SharedObject.publicStaticDouble = 40.0;"); + assertEquals(Double.doubleToLongBits(40.0), Double.doubleToLongBits(SharedObject.publicStaticDouble)); + e.eval("SharedObject.publicStaticDouble = 0.0/0.0;"); + assertTrue(Double.isNaN(SharedObject.publicStaticDouble)); + e.eval("SharedObject.publicStaticDouble = 1.0/0.0;"); + assertEquals(Double.doubleToLongBits(Double.POSITIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); + e.eval("SharedObject.publicStaticDouble = -1.0/0.0;"); + assertEquals(Double.doubleToLongBits(Double.NEGATIVE_INFINITY), Double.doubleToLongBits(SharedObject.publicStaticDouble)); + } + + @Test + public void accessFinalFieldDoubleBoxing() throws ScriptException { + e.eval("var pf_double = o.publicFinalDoubleBox;"); + assertEquals(o.publicFinalDoubleBox, e.get("pf_double")); + e.eval("o.publicFinalDoubleBox = 30.0;"); + assertEquals(Double.doubleToLongBits(1.412e-12), Double.doubleToLongBits(o.publicFinalDoubleBox)); + } + + @Test + public void accessStaticFinalFieldDoubleBoxing() throws ScriptException { + e.eval("var psf_double = SharedObject.publicStaticFinalDouble;"); + assertEquals(SharedObject.publicStaticFinalDouble, e.get("psf_double")); + e.eval("SharedObject.publicStaticFinalDouble = 40.0;"); + assertEquals(Double.doubleToLongBits(1.8e12), Double.doubleToLongBits(SharedObject.publicStaticFinalDouble)); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/ObjectAccessTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.testng.TestNG; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * @test + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.ObjectAccessTest + * @run testng/othervm jdk.nashorn.api.javaaccess.ObjectAccessTest + */ +@SuppressWarnings("javadoc") +public class ObjectAccessTest { + + private static ScriptEngine e = null; + private static SharedObject o = null; + + public static void main(final String[] args) { + TestNG.main(args); + } + + @BeforeClass + public static void setUpClass() throws ScriptException { + final ScriptEngineManager m = new ScriptEngineManager(); + e = m.getEngineByName("nashorn"); + o = new SharedObject(); + e.put("o", o); + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); + e.eval("var Person = Packages.jdk.nashorn.api.javaaccess.Person;"); + } + + @AfterClass + public static void tearDownClass() { + e = null; + o = null; + } + + @Test + public void accessFieldObject() throws ScriptException { + e.eval("var p_object = o.publicObject;"); + assertEquals(o.publicObject, e.get("p_object")); + assertEquals("object", e.eval("typeof p_object;")); + e.eval("o.publicObject = new Person(14);"); + assertEquals(new Person(14), o.publicObject); + } + + @Test + public void accessFieldObjectArray() throws ScriptException { + e.eval("var p_object_array = o.publicObjectArray;"); + assertEquals(o.publicObjectArray[0], e.eval("o.publicObjectArray[0]")); + assertArrayEquals(o.publicObjectArray, (Object[])e.get("p_object_array")); + e.eval("var t_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + + "t_object_arr[0] = new Person(100);" + + "t_object_arr[1] = new Person(120);" + + "t_object_arr[2] = new Person(140);" + + "o.publicObjectArray = t_object_arr;"); + assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, o.publicObjectArray); + e.eval("o.publicObjectArray[0] = new Person(10);"); + assertEquals(new Person(10), o.publicObjectArray[0]); + } + + @Test + public void accessStaticFieldObject() throws ScriptException { + e.eval("var ps_object = SharedObject.publicStaticObject;"); + assertEquals(SharedObject.publicStaticObject, e.get("ps_object")); + assertEquals("object", e.eval("typeof ps_object;")); + e.eval("SharedObject.publicStaticObject = new Person(16);"); + assertEquals(new Person(16), SharedObject.publicStaticObject); + } + + @Test + public void accessStaticFieldObjectArray() throws ScriptException { + e.eval("var ps_object_array = SharedObject.publicStaticObjectArray;"); + assertEquals(SharedObject.publicStaticObjectArray[0], e.eval("SharedObject.publicStaticObjectArray[0]")); + assertArrayEquals(SharedObject.publicStaticObjectArray, (Object[])e.get("ps_object_array")); + e.eval("var ts_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + + "ts_object_arr[0] = new Person(100);" + + "ts_object_arr[1] = new Person(120);" + + "ts_object_arr[2] = new Person(140);" + + "SharedObject.publicStaticObjectArray = ts_object_arr;"); + assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, SharedObject.publicStaticObjectArray); + e.eval("SharedObject.publicStaticObjectArray[0] = new Person(10);"); + assertEquals(new Person(10), SharedObject.publicStaticObjectArray[0]); + } + + @Test + public void accessFinalFieldObject() throws ScriptException { + e.eval("var pf_object = o.publicFinalObject;"); + assertEquals(o.publicFinalObject, e.get("pf_object")); + assertEquals("object", e.eval("typeof pf_object;")); + e.eval("o.publicFinalObject = new Person(-999);"); + assertEquals(new Person(1024), o.publicFinalObject); + } + + @Test + public void accessFinalFieldObjectArray() throws ScriptException { + e.eval("var pf_object_array = o.publicFinalObjectArray;"); + assertEquals(o.publicFinalObjectArray[0], e.eval("o.publicFinalObjectArray[0]")); + assertArrayEquals(o.publicFinalObjectArray, (Object[])e.get("pf_object_array")); + e.eval("var tf_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + + "tf_object_arr[0] = new Person(100);" + + "tf_object_arr[1] = new Person(120);" + + "tf_object_arr[2] = new Person(140);" + + "o.publicOFinalbjectArray = tf_object_arr;"); + assertArrayEquals(new Person[] { new Person(-900), new Person(1000), new Person(180) }, o.publicFinalObjectArray); + e.eval("o.publicFinalObjectArray[0] = new Person(10);"); + assertEquals(new Person(10), o.publicFinalObjectArray[0]); + } + + @Test + public void accessStaticFinalFieldObject() throws ScriptException { + e.eval("var psf_object = SharedObject.publicStaticFinalObject;"); + assertEquals(SharedObject.publicStaticFinalObject, e.get("psf_object")); + assertEquals("object", e.eval("typeof psf_object;")); + e.eval("SharedObject.publicStaticFinalObject = new Person(6);"); + assertEquals(new Person(2048), SharedObject.publicStaticFinalObject); + } + + @Test + public void accessStaticFinalFieldObjectArray() throws ScriptException { + e.eval("var psf_object_array = SharedObject.publicStaticFinalObjectArray;"); + assertEquals(SharedObject.publicStaticFinalObjectArray[0], e.eval("SharedObject.publicStaticFinalObjectArray[0]")); + assertArrayEquals(SharedObject.publicStaticFinalObjectArray, (Object[])e.get("psf_object_array")); + e.eval("var tsf_object_arr = new (Java.type(\"jdk.nashorn.api.javaaccess.Person[]\"))(3);" + + "tsf_object_arr[0] = new Person(100);" + + "tsf_object_arr[1] = new Person(120);" + + "tsf_object_arr[2] = new Person(140);" + + "SharedObject.publicStaticFinalObjectArray = tsf_object_arr;"); + assertArrayEquals(new Person[] { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }, SharedObject.publicStaticFinalObjectArray); + e.eval("SharedObject.publicStaticFinalObjectArray[0] = new Person(90);"); + assertEquals(new Person(90), SharedObject.publicStaticFinalObjectArray[0]); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/Person.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +@SuppressWarnings("javadoc") +public class Person { + + public int id = 0; + + public Person() { + } + + public Person(final int code) { + this.id = code; + } + + @Override + public boolean equals(final Object obj) { + if (obj != null && obj instanceof Person) { + final Person o = (Person)obj; + return this.id == o.id; + } + return false; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public String toString() { + return "Person(" + id + ")"; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/SharedObject.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,467 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptException; + +@SuppressWarnings("javadoc") +public class SharedObject { + + // Public fields + public String publicString = "PublicString"; + public String[] publicStringArray = { "ArrayString[0]", "ArrayString[1]", "ArrayString[2]", "ArrayString[3]" }; + public Person publicObject = new Person(256); + public Person[] publicObjectArray = { new Person(4), new Person(-422), new Person(14) }; + public boolean publicBoolean = true; + public boolean[] publicBooleanArray = { true, false, false, true }; + public Boolean publicBooleanBox = true; + public long publicLong = 933333333333333333L; + public long[] publicLongArray = { 99012333333333L, -124355555L, 89777777777L }; + public Long publicLongBox = 9333333333L; + public int publicInt = 2076543123; + public int[] publicIntArray = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; + public Integer publicIntBox = 20765123; + public byte publicByte = -128; + public byte[] publicByteArray = { 1, 2, 4, 8, 16, 32, 64, 127, -128 }; + public Byte publicByteBox = 127; + public short publicShort = 32000; + public short[] publicShortArray = { 3240, 8900, -16789, 1, 12 }; + public Short publicShortBox = Short.MIN_VALUE; + public float publicFloat = 0.7f; + public float[] publicFloatArray = { -32.01f, 89.3f, -1.3e8f, 3.1f }; + public Float publicFloatBox = 1.377e4f; + public double publicDouble = 1.34e20; + public double[] publicDoubleArray = { 0.75e80, 8e-43, 1.000077, 0.123e10 }; + public Double publicDoubleBox = 1.4e-19; + public char publicChar = 'A'; + public char[] publicCharArray = "Hello Nashorn".toCharArray(); + public Character publicCharBox = 'B'; + // Public static fields + public static String publicStaticString = "PublicStaticString"; + public static String[] publicStaticStringArray = { "StaticArrayString[0]", "StaticArrayString[1]", "StaticArrayString[2]", "StaticArrayString[3]" }; + public static Person publicStaticObject = new Person(512); + public static Person[] publicStaticObjectArray = { new Person(40), new Person(-22), new Person(18) }; + public static boolean publicStaticBoolean = true; + public static boolean[] publicStaticBooleanArray = { false, false, false, true }; + public static Boolean publicStaticBooleanBox = true; + public static long publicStaticLong = 13333333333333333L; + public static long[] publicStaticLongArray = { 19012333333333L, -224355555L, 39777777777L }; + public static Long publicStaticLongBox = 9333333334L; + public static int publicStaticInt = 207654323; + public static int[] publicStaticIntArray = { 5, 8, 13, 21, 34 }; + public static Integer publicStaticIntBox = 2075123; + public static byte publicStaticByte = -12; + public static byte[] publicStaticByteArray = { 16, 32, 64, 127, -128 }; + public static Byte publicStaticByteBox = 17; + public static short publicStaticShort = 320; + public static short[] publicStaticShortArray = { 1240, 900, -1789, 100, 12 }; + public static Short publicStaticShortBox = -16777; + public static float publicStaticFloat = 7.7e8f; + public static float[] publicStaticFloatArray = { -131.01f, 189.3f, -31.3e8f, 3.7f }; + public static Float publicStaticFloatBox = 1.37e4f; + public static double publicStaticDouble = 1.341e20; + public static double[] publicStaticDoubleArray = { 0.75e80, 0.123e10, 8e-43, 1.000077 }; + public static Double publicStaticDoubleBox = 1.41e-12; + public static char publicStaticChar = 'C'; + public static char[] publicStaticCharArray = "Nashorn".toCharArray(); + public static Character publicStaticCharBox = 'D'; + // Public final fields + public final String publicFinalString = "PublicFinalString"; + public final String[] publicFinalStringArray = { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" }; + public final Person publicFinalObject = new Person(1024); + public final Person[] publicFinalObjectArray = { new Person(-900), new Person(1000), new Person(180) }; + public final boolean publicFinalBoolean = true; + public final boolean[] publicFinalBooleanArray = { false, false, true, false }; + public final Boolean publicFinalBooleanBox = true; + public final long publicFinalLong = 13353333333333333L; + public final long[] publicFinalLongArray = { 1901733333333L, -2247355555L, 3977377777L }; + public final Long publicFinalLongBox = 9377333334L; + public final int publicFinalInt = 20712023; + public final int[] publicFinalIntArray = { 50, 80, 130, 210, 340 }; + public final Integer publicFinalIntBox = 207512301; + public final byte publicFinalByte = -7; + public final byte[] publicFinalByteArray = { 1, 3, 6, 17, -128 }; + public final Byte publicFinalByteBox = 19; + public final short publicFinalShort = 31220; + public final short[] publicFinalShortArray = { 12240, 9200, -17289, 1200, 12 }; + public final Short publicFinalShortBox = -26777; + public final float publicFinalFloat = 7.72e8f; + public final float[] publicFinalFloatArray = { -131.012f, 189.32f, -31.32e8f, 3.72f }; + public final Float publicFinalFloatBox = 1.372e4f; + public final double publicFinalDouble = 1.3412e20; + public final double[] publicFinalDoubleArray = { 0.725e80, 0.12e10, 8e-3, 1.00077 }; + public final Double publicFinalDoubleBox = 1.412e-12; + public final char publicFinalChar = 'E'; + public final char[] publicFinalCharArray = "Nashorn hello".toCharArray(); + public final Character publicFinalCharBox = 'F'; + // Public static final fields + public static final String publicStaticFinalString = "PublicStaticFinalString"; + public static final String[] publicStaticFinalStringArray = { "StaticFinalArrayString[0]", "StaticFinalArrayString[1]", "StaticFinalArrayString[2]", "StaticFinalArrayString[3]" }; + public static final Person publicStaticFinalObject = new Person(2048); + public static final Person[] publicStaticFinalObjectArray = { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }; + public static final boolean publicStaticFinalBoolean = true; + public static final boolean[] publicStaticFinalBooleanArray = { false, true, false, false }; + public static final Boolean publicStaticFinalBooleanBox = true; + public static final long publicStaticFinalLong = 8333333333333L; + public static final long[] publicStaticFinalLongArray = { 19017383333L, -2247358L, 39773787L }; + public static final Long publicStaticFinalLongBox = 9377388334L; + public static final int publicStaticFinalInt = 207182023; + public static final int[] publicStaticFinalIntArray = { 1308, 210, 340 }; + public static final Integer publicStaticFinalIntBox = 2078301; + public static final byte publicStaticFinalByte = -70; + public static final byte[] publicStaticFinalByteArray = { 17, -128, 81 }; + public static final Byte publicStaticFinalByteBox = 91; + public static final short publicStaticFinalShort = 8888; + public static final short[] publicStaticFinalShortArray = { 8240, 9280, -1289, 120, 812 }; + public static final Short publicStaticFinalShortBox = -26; + public static final float publicStaticFinalFloat = 0.72e8f; + public static final float[] publicStaticFinalFloatArray = { -8131.012f, 9.32f, -138.32e8f, 0.72f }; + public static final Float publicStaticFinalFloatBox = 1.2e4f; + public static final double publicStaticFinalDouble = 1.8e12; + public static final double[] publicStaticFinalDoubleArray = { 8.725e80, 0.82e10, 18e-3, 1.08077 }; + public static final Double publicStaticFinalDoubleBox = 1.5612e-13; + public static final char publicStaticFinalChar = 'K'; + public static final char[] publicStaticFinalCharArray = "StaticString".toCharArray(); + public static final Character publicStaticFinalCharBox = 'L'; + + // Special vars + public volatile boolean volatileBoolean = true; + public transient boolean transientBoolean = true; + + // For methods testing + public boolean isAccessed = false; + public volatile boolean isFinished = false; + + private ScriptEngine engine; + + public ScriptEngine getEngine() { + return engine; + } + + public void setEngine(final ScriptEngine engine) { + this.engine = engine; + } + + public void voidMethod() { + isAccessed = true; + } + + public boolean booleanMethod(final boolean arg) { + return !arg; + } + + public Boolean booleanBoxingMethod(final Boolean arg) { + return !arg.booleanValue(); + } + + public boolean[] booleanArrayMethod(final boolean arg[]) { + final boolean[] res = new boolean[arg.length]; + for (int i = 0; i < arg.length; i++) { + res[i] = !arg[i]; + } + return res; + } + + public int intMethod(final int arg) { + return arg + arg; + } + + public Integer intBoxingMethod(final Integer arg) { + return arg + arg; + } + + public int[] intArrayMethod(final int arg[]) { + final int[] res = new int[arg.length]; + for (int i = 0; i < arg.length; i++) { + res[i] = arg[i] * 2; + } + return res; + } + + public long longMethod(final long arg) { + return arg + arg; + } + + public Long longBoxingMethod(final Long arg) { + return arg + arg; + } + + public long[] longArrayMethod(final long[] arg) { + final long[] res = new long[arg.length]; + for (int i = 0; i < arg.length; i++) { + res[i] = arg[i] * 2; + } + return res; + } + + public byte byteMethod(final byte arg) { + return (byte)(arg + arg); + } + + public Byte byteBoxingMethod(final Byte arg) { + return (byte)(arg + arg); + } + + public byte[] byteArrayMethod(final byte[] arg) { + final byte[] res = new byte[arg.length]; + for (int i = 0; i < arg.length; i++) { + res[i] = (byte)(arg[i] * 2); + } + return res; + } + + public char charMethod(final char arg) { + return Character.toUpperCase(arg); + } + + public Character charBoxingMethod(final Character arg) { + return Character.toUpperCase(arg); + } + + public char[] charArrayMethod(final char[] arg) { + final char[] res = new char[arg.length]; + for (int i = 0; i < arg.length; i++) { + res[i] = Character.toUpperCase(arg[i]); + } + return res; + } + + public short shortMethod(final short arg) { + return (short)(arg + arg); + } + + public Short shortBoxingMethod(final Short arg) { + return (short)(arg + arg); + } + + public short[] shortArrayMethod(final short[] arg) { + final short[] res = new short[arg.length]; + for (int i = 0; i < arg.length; i++) { + res[i] = (short)(arg[i] * 2); + } + return res; + } + + public float floatMethod(final float arg) { + return arg + arg; + } + + public Float floatBoxingMethod(final Float arg) { + return arg + arg; + } + + public float[] floatArrayMethod(final float[] arg) { + final float[] res = new float[arg.length]; + for (int i = 0; i < arg.length; i++) { + res[i] = arg[i] * 2; + } + return res; + } + + public double doubleMethod(final double arg) { + return arg + arg; + } + + public Double doubleBoxingMethod(final Double arg) { + return arg + arg; + } + + public double[] doubleArrayMethod(final double[] arg) { + final double[] res = new double[arg.length]; + for (int i = 0; i < arg.length; i++) { + res[i] = arg[i] * 2; + } + return res; + } + + public String stringMethod(final String str) { + return str + str; + } + + public String[] stringArrayMethod(final String[] arr) { + final int l = arr.length; + final String[] res = new String[l]; + for (int i = 0; i < l; i++) { + res[i] = arr[l - i - 1]; + } + return res; + } + + public Person[] objectArrayMethod(final Person[] arr) { + final Person[] res = new Person[arr.length]; + for (int i = 0; i < arr.length; i++) { + res[i] = new Person(i + 100); + } + return res; + } + + public Person objectMethod(final Person t) { + t.id *= 2; + return t; + } + + public int twoParamMethod(final long l, final double d) { + return (int)(l + d); + } + + public int threeParamMethod(final short s, final long l, final char c) { + return (int)(s + l + c); + } + + public Person[] twoObjectParamMethod(final Person arg1, final Person arg2) { + return new Person[] { arg2, arg1 }; + } + + public Person[] threeObjectParamMethod(final Person arg1, final Person arg2, final Person arg3) { + return new Person[] { arg3, arg2, arg1 }; + } + + 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) { + return new Person[] { arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 }; + } + + 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) { + return new Person[] { arg9, arg8, arg7, arg6, arg5, arg4, arg3, arg2, arg1 }; + } + + public Person[] methodObjectEllipsis(final Person... args) { + final int l = args.length; + final Person[] res = new Person[l]; + for (int i = 0; i < l; i++) { + res[i] = args[l - i - 1]; + } + return res; + } + + public Person[] methodPrimitiveEllipsis(final int... args) { + final int l = args.length; + final Person[] res = new Person[l]; + for (int i = 0; i < l; i++) { + res[i] = new Person(args[i]); + } + return res; + } + + public Object[] methodMixedEllipsis(final Object... args) { + return args; + } + + public Object[] methodObjectWithEllipsis(final String arg, final int... args) { + final Object[] res = new Object[args.length + 1]; + res[0] = arg; + for (int i = 0; i < args.length; i++) { + res[i + 1] = args[i]; + } + return res; + } + + public Object[] methodPrimitiveWithEllipsis(final int arg, final long... args) { + final Object[] res = new Object[args.length + 1]; + res[0] = arg; + for (int i = 0; i < args.length; i++) { + res[i + 1] = args[i]; + } + return res; + } + + public Object[] methodMixedWithEllipsis(final String arg1, final int arg2, final Object... args) { + final Object[] res = new Object[args.length + 2]; + res[0] = arg1; + res[1] = arg2; + System.arraycopy(args, 0, res, 2, args.length); + return res; + } + + public void methodStartsThread() { + isFinished = false; + + final Thread t = new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1000); + isFinished = true; + } catch (final InterruptedException e) { + e.printStackTrace(); + } + } + }); + + t.start(); + } + + public String overloadedMethodDoubleVSint(final int arg) { + return "int"; + } + + public String overloadedMethodDoubleVSint(final double arg) { + return "double"; + } + + public int overloadedMethod(final int arg) { + return arg*2; + } + + public int overloadedMethod(final String arg) { + return arg.length(); + } + + public int overloadedMethod(final boolean arg) { + return (arg) ? 1 : 0; + } + + public int overloadedMethod(final Person arg) { + return arg.id*2; + } + + public int firstLevelMethodInt(final int arg) throws ScriptException, NoSuchMethodException { + return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInt", arg); + } + + public int thirdLevelMethodInt(final int arg) { + return arg*5; + } + + public int firstLevelMethodInteger(final Integer arg) throws ScriptException, NoSuchMethodException { + return (int) ((Invocable)engine).invokeFunction("secondLevelMethodInteger", arg); + } + + public int thirdLevelMethodInteger(final Integer arg) { + return arg*10; + } + + public Person firstLevelMethodObject(final Person p) throws ScriptException, NoSuchMethodException { + return (Person) ((Invocable)engine).invokeFunction("secondLevelMethodObject", p); + } + + public Person thirdLevelMethodObject(final Person p) { + p.id *= 10; + return p; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/src/jdk/nashorn/api/javaaccess/test/StringAccessTest.java Wed Apr 08 14:31:51 2015 -0700 @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.nashorn.api.javaaccess.test; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.testng.TestNG; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * @test + * @build jdk.nashorn.api.javaaccess.SharedObject jdk.nashorn.api.javaaccess.Person jdk.nashorn.api.javaaccess.StringAccessTest + * @run testng/othervm jdk.nashorn.api.javaaccess.StringAccessTest + */ +@SuppressWarnings("javadoc") +public class StringAccessTest { + + private static ScriptEngine e = null; + private static SharedObject o = null; + + public static void main(final String[] args) { + TestNG.main(args); + } + + @BeforeClass + public static void setUpClass() throws ScriptException { + final ScriptEngineManager m = new ScriptEngineManager(); + e = m.getEngineByName("nashorn"); + o = new SharedObject(); + e.put("o", o); + e.eval("var SharedObject = Packages.jdk.nashorn.api.javaaccess.SharedObject;"); + } + + @AfterClass + public static void tearDownClass() { + e = null; + o = null; + } + + @Test + public void accessFieldString() throws ScriptException { + e.eval("var p_string = o.publicString;"); + assertEquals(o.publicString, e.get("p_string")); + assertEquals("string", e.eval("typeof p_string;")); + e.eval("o.publicString = 'changedString';"); + assertEquals("changedString", o.publicString); + } + + @Test + public void accessFieldStringArray() throws ScriptException { + e.eval("var p_string_array = o.publicStringArray;"); + assertEquals(o.publicStringArray[0], e.eval("o.publicStringArray[0]")); + assertArrayEquals(o.publicStringArray, (String[])e.get("p_string_array")); + e.eval("var t_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + + "t_string_arr[0] = 'abc';" + + "t_string_arr[1] = '123';" + + "t_string_arr[2] = 'xyzzzz';" + + "o.publicStringArray = t_string_arr;"); + assertArrayEquals(new String[] { "abc", "123", "xyzzzz" }, o.publicStringArray); + e.eval("o.publicStringArray[0] = 'nashorn';"); + assertEquals("nashorn", o.publicStringArray[0]); + } + + @Test + public void accessStaticFieldString() throws ScriptException { + e.eval("var ps_string = SharedObject.publicStaticString;"); + assertEquals(SharedObject.publicStaticString, e.get("ps_string")); + assertEquals("string", e.eval("typeof ps_string;")); + e.eval("SharedObject.publicStaticString = 'changedString';"); + assertEquals("changedString", SharedObject.publicStaticString); + } + + @Test + public void accessStaticFieldStringArray() throws ScriptException { + e.eval("var ps_string_array = SharedObject.publicStaticStringArray;"); + assertEquals(SharedObject.publicStaticStringArray[0], e.eval("SharedObject.publicStaticStringArray[0]")); + assertArrayEquals(SharedObject.publicStaticStringArray, (String[])e.get("ps_string_array")); + e.eval("var ts_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + + "ts_string_arr[0] = 'abc';" + + "ts_string_arr[1] = '123';" + + "ts_string_arr[2] = 'xyzzzz';" + + "SharedObject.publicStaticStringArray = ts_string_arr;"); + assertArrayEquals(new String[] { "abc", "123", "xyzzzz" }, SharedObject.publicStaticStringArray); + e.eval("SharedObject.publicStaticStringArray[0] = 'nashorn';"); + assertEquals("nashorn", SharedObject.publicStaticStringArray[0]); + } + + @Test + public void accessFinalFieldString() throws ScriptException { + e.eval("var pf_string = o.publicFinalString;"); + assertEquals(o.publicFinalString, e.get("pf_string")); + assertEquals("string", e.eval("typeof pf_string;")); + e.eval("o.publicFinalString = 'changedString';"); + assertEquals("PublicFinalString", o.publicFinalString); + } + + @Test + public void accessFinalFieldStringArray() throws ScriptException { + e.eval("var pf_string_array = o.publicFinalStringArray;"); + assertEquals(o.publicFinalStringArray[0], e.eval("o.publicFinalStringArray[0]")); + assertArrayEquals(o.publicFinalStringArray, (String[])e.get("pf_string_array")); + e.eval("var tf_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + + "tf_string_arr[0] = 'abc';" + + "tf_string_arr[1] = '123';" + + "tf_string_arr[2] = 'xyzzzz';" + + "o.publicFinalStringArray = tf_string_arr;"); + assertArrayEquals(new String[] { "FinalArrayString[0]", "FinalArrayString[1]", "FinalArrayString[2]", "FinalArrayString[3]" }, o.publicFinalStringArray); + e.eval("o.publicFinalStringArray[0] = 'nashorn';"); + assertEquals("nashorn", o.publicFinalStringArray[0]); + } + + @Test + public void accessStaticFinalFieldString() throws ScriptException { + e.eval("var psf_string = SharedObject.publicStaticFinalString;"); + assertEquals(SharedObject.publicStaticFinalString, e.get("psf_string")); + assertEquals("string", e.eval("typeof psf_string;")); + e.eval("SharedObject.publicStaticFinalString = 'changedString';"); + assertEquals("PublicStaticFinalString", SharedObject.publicStaticFinalString); + } + + @Test + public void accessStaticFinalFieldStringArray() throws ScriptException { + e.eval("var psf_string_array = SharedObject.publicStaticFinalStringArray;"); + assertEquals(SharedObject.publicStaticFinalStringArray[0], e.eval("SharedObject.publicStaticFinalStringArray[0]")); + assertArrayEquals(SharedObject.publicStaticFinalStringArray, (String[])e.get("psf_string_array")); + e.eval("var tsf_string_arr = new (Java.type(\"java.lang.String[]\"))(3);" + + "tsf_string_arr[0] = 'abc';" + + "tsf_string_arr[1] = '123';" + + "tsf_string_arr[2] = 'xyzzzz';" + + "SharedObject.publicStaticFinalStringArray = tsf_string_arr;"); + assertArrayEquals(new String[] { "StaticFinalArrayString[0]", + "StaticFinalArrayString[1]", + "StaticFinalArrayString[2]", + "StaticFinalArrayString[3]" }, + SharedObject.publicStaticFinalStringArray); + e.eval("SharedObject.publicStaticFinalStringArray[0] = 'nashorn';"); + assertEquals("nashorn", SharedObject.publicStaticFinalStringArray[0]); + } + +}
--- a/test/src/jdk/nashorn/api/scripting/InvocableTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,539 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.scripting; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; -import java.util.Objects; -import java.util.function.Function; -import javax.script.Invocable; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import javax.script.SimpleScriptContext; -import org.testng.Assert; -import org.testng.annotations.Test; - -/** - * Tests for javax.script.Invocable implementation of nashorn. - */ -@SuppressWarnings("javadoc") -public class InvocableTest { - - private static void log(final String msg) { - org.testng.Reporter.log(msg, true); - } - - @Test - public void invokeMethodTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();"); - final Object obj = e.get("myExample"); - final Object res = ((Invocable) e).invokeMethod(obj, "hello"); - assertEquals(res, "Hello World!"); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - @Test - /** - * Check that we can call invokeMethod on an object that we got by - * evaluating script with different Context set. - */ - public void invokeMethodDifferentContextTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - // define an object with method on it - final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); - - final ScriptContext ctxt = new SimpleScriptContext(); - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); - e.setContext(ctxt); - - // invoke 'func' on obj - but with current script context changed - final Object res = ((Invocable) e).invokeMethod(obj, "hello"); - assertEquals(res, "Hello World!"); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - @Test - /** - * Check that invokeMethod throws NPE on null method name. - */ - public void invokeMethodNullNameTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - final Object obj = e.eval("({})"); - ((Invocable) e).invokeMethod(obj, null); - fail("should have thrown NPE"); - } catch (final Exception exp) { - if (!(exp instanceof NullPointerException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * Check that invokeMethod throws NoSuchMethodException on missing method. - */ - public void invokeMethodMissingTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - final Object obj = e.eval("({})"); - ((Invocable) e).invokeMethod(obj, "nonExistentMethod"); - fail("should have thrown NoSuchMethodException"); - } catch (final Exception exp) { - if (!(exp instanceof NoSuchMethodException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * Check that calling method on non-script object 'thiz' results in - * IllegalArgumentException. - */ - public void invokeMethodNonScriptObjectThizTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - ((Invocable) e).invokeMethod(new Object(), "toString"); - fail("should have thrown IllegalArgumentException"); - } catch (final Exception exp) { - if (!(exp instanceof IllegalArgumentException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * Check that calling method on null 'thiz' results in - * IllegalArgumentException. - */ - public void invokeMethodNullThizTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - ((Invocable) e).invokeMethod(null, "toString"); - fail("should have thrown IllegalArgumentException"); - } catch (final Exception exp) { - if (!(exp instanceof IllegalArgumentException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * Check that calling method on mirror created by another engine results in - * IllegalArgumentException. - */ - public void invokeMethodMixEnginesTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine engine1 = m.getEngineByName("nashorn"); - final ScriptEngine engine2 = m.getEngineByName("nashorn"); - - try { - final Object obj = engine1.eval("({ run: function() {} })"); - // pass object from engine1 to engine2 as 'thiz' for invokeMethod - ((Invocable) engine2).invokeMethod(obj, "run"); - fail("should have thrown IllegalArgumentException"); - } catch (final Exception exp) { - if (!(exp instanceof IllegalArgumentException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - public void getInterfaceTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Invocable inv = (Invocable) e; - - // try to get interface from global functions - try { - e.eval("function run() { print('run'); };"); - final Runnable runnable = inv.getInterface(Runnable.class); - runnable.run(); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - - // try interface on specific script object - try { - e.eval("var obj = { run: function() { print('run from obj'); } };"); - final Object obj = e.get("obj"); - final Runnable runnable = inv.getInterface(obj, Runnable.class); - runnable.run(); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - public interface Foo { - - public void bar(); - } - - public interface Foo2 extends Foo { - - public void bar2(); - } - - @Test - public void getInterfaceMissingTest() { - final ScriptEngineManager manager = new ScriptEngineManager(); - final ScriptEngine engine = manager.getEngineByName("nashorn"); - - // don't define any function. - try { - engine.eval(""); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - - Runnable runnable = ((Invocable) engine).getInterface(Runnable.class); - if (runnable != null) { - fail("runnable is not null!"); - } - - // now define "run" - try { - engine.eval("function run() { print('this is run function'); }"); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - runnable = ((Invocable) engine).getInterface(Runnable.class); - // should not return null now! - runnable.run(); - - // define only one method of "Foo2" - try { - engine.eval("function bar() { print('bar function'); }"); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - - Foo2 foo2 = ((Invocable) engine).getInterface(Foo2.class); - if (foo2 != null) { - throw new RuntimeException("foo2 is not null!"); - } - - // now define other method of "Foo2" - try { - engine.eval("function bar2() { print('bar2 function'); }"); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - foo2 = ((Invocable) engine).getInterface(Foo2.class); - foo2.bar(); - foo2.bar2(); - } - - @Test - /** - * Try passing non-interface Class object for interface implementation. - */ - public void getNonInterfaceGetInterfaceTest() { - final ScriptEngineManager manager = new ScriptEngineManager(); - final ScriptEngine engine = manager.getEngineByName("nashorn"); - try { - log(Objects.toString(((Invocable) engine).getInterface(Object.class))); - fail("Should have thrown IllegalArgumentException"); - } catch (final Exception exp) { - if (!(exp instanceof IllegalArgumentException)) { - fail("IllegalArgumentException expected, got " + exp); - } - } - } - - @Test - /** - * Check that we can get interface out of a script object even after - * switching to use different ScriptContext. - */ - public void getInterfaceDifferentContext() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - final Object obj = e.eval("({ run: function() { } })"); - - // change script context - final ScriptContext ctxt = new SimpleScriptContext(); - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); - e.setContext(ctxt); - - final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); - r.run(); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - @Test - /** - * Check that getInterface on non-script object 'thiz' results in - * IllegalArgumentException. - */ - public void getInterfaceNonScriptObjectThizTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - ((Invocable) e).getInterface(new Object(), Runnable.class); - fail("should have thrown IllegalArgumentException"); - } catch (final Exception exp) { - if (!(exp instanceof IllegalArgumentException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * Check that getInterface on null 'thiz' results in - * IllegalArgumentException. - */ - public void getInterfaceNullThizTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - ((Invocable) e).getInterface(null, Runnable.class); - fail("should have thrown IllegalArgumentException"); - } catch (final Exception exp) { - if (!(exp instanceof IllegalArgumentException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * Check that calling getInterface on mirror created by another engine - * results in IllegalArgumentException. - */ - public void getInterfaceMixEnginesTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine engine1 = m.getEngineByName("nashorn"); - final ScriptEngine engine2 = m.getEngineByName("nashorn"); - - try { - final Object obj = engine1.eval("({ run: function() {} })"); - // pass object from engine1 to engine2 as 'thiz' for getInterface - ((Invocable) engine2).getInterface(obj, Runnable.class); - fail("should have thrown IllegalArgumentException"); - } catch (final Exception exp) { - if (!(exp instanceof IllegalArgumentException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * check that null function name results in NPE. - */ - public void invokeFunctionNullNameTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - ((Invocable)e).invokeFunction(null); - fail("should have thrown NPE"); - } catch (final Exception exp) { - if (!(exp instanceof NullPointerException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * Check that attempt to call missing function results in - * NoSuchMethodException. - */ - public void invokeFunctionMissingTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - ((Invocable)e).invokeFunction("NonExistentFunc"); - fail("should have thrown NoSuchMethodException"); - } catch (final Exception exp) { - if (!(exp instanceof NoSuchMethodException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - /** - * Check that invokeFunction calls functions only from current context's - * Bindings. - */ - public void invokeFunctionDifferentContextTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - try { - // define an object with method on it - e.eval("function hello() { return 'Hello World!'; }"); - final ScriptContext ctxt = new SimpleScriptContext(); - ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); - // change engine's current context - e.setContext(ctxt); - - ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context! - fail("should have thrown NoSuchMethodException"); - } catch (final Exception exp) { - if (!(exp instanceof NoSuchMethodException)) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - } - - @Test - public void invokeFunctionExceptionTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.eval("function func() { throw new TypeError(); }"); - } catch (final Throwable t) { - t.printStackTrace(); - fail(t.getMessage()); - } - - try { - ((Invocable) e).invokeFunction("func"); - fail("should have thrown exception"); - } catch (final ScriptException se) { - // ECMA TypeError property wrapped as a ScriptException - log("got " + se + " as expected"); - } catch (final Throwable t) { - t.printStackTrace(); - fail(t.getMessage()); - } - } - - @Test - public void invokeMethodExceptionTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }"); - } catch (final Throwable t) { - t.printStackTrace(); - fail(t.getMessage()); - } - - try { - final Object sobj = e.get("sobj"); - ((Invocable) e).invokeMethod(sobj, "foo"); - fail("should have thrown exception"); - } catch (final ScriptException se) { - // ECMA TypeError property wrapped as a ScriptException - log("got " + se + " as expected"); - } catch (final Throwable t) { - t.printStackTrace(); - fail(t.getMessage()); - } - } - - @Test - /** - * Tests whether invocation of a JavaScript method through a variable arity - * Java method will pass the vararg array. Both non-vararg and vararg - * JavaScript methods are tested. - * - * @throws ScriptException - */ - public void variableArityInterfaceTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - e.eval( - "function test1(i, strings) {" - + " return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)" - + "}" - + "function test2() {" - + " 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])" - + "}"); - final VariableArityTestInterface itf = ((Invocable) e).getInterface(VariableArityTestInterface.class); - Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]"); - Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]"); - } - - @Test - public void defaultMethodTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Invocable inv = (Invocable) e; - - final Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})"); - @SuppressWarnings("unchecked") - final Function<String, String> func = inv.getInterface(obj, Function.class); - assertEquals(func.apply("hello"), "HELLO"); - } -}
--- a/test/src/jdk/nashorn/api/scripting/MultipleEngineTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.scripting; - -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.annotations.Test; - -/** - * Test that we can create multiple, independent script engines and use those - * independently. - * - * @test - * @run testng jdk.nashorn.api.scripting.MultipleEngineTest - */ -@SuppressWarnings("javadoc") -public class MultipleEngineTest { - @Test - public void createAndUseManyEngine() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - - final ScriptEngine e1 = m.getEngineByName("nashorn"); - e1.eval("var x = 33; print(x);"); - - final ScriptEngine e2 = m.getEngineByName("nashorn"); - e2.eval("try { print(x) } catch(e) { print(e); }"); - } -}
--- a/test/src/jdk/nashorn/api/scripting/PluggableJSObjectTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,288 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.scripting; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.fail; - -import java.nio.IntBuffer; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Set; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import org.testng.annotations.Test; - -/** - * Tests for pluggable external impls. of jdk.nashorn.api.scripting.JSObject. - * - * JDK-8024615: Refactor ScriptObjectMirror and JSObject to support external - * JSObject implementations. - */ -@SuppressWarnings("javadoc") -public class PluggableJSObjectTest { - public static class MapWrapperObject extends AbstractJSObject { - private final HashMap<String, Object> map = new LinkedHashMap<>(); - - public HashMap<String, Object> getMap() { - return map; - } - - @Override - public Object getMember(final String name) { - return map.get(name); - } - - @Override - public void setMember(final String name, final Object value) { - map.put(name, value); - } - - @Override - public boolean hasMember(final String name) { - return map.containsKey(name); - } - - @Override - public void removeMember(final String name) { - map.remove(name); - } - - @Override - public Set<String> keySet() { - return map.keySet(); - } - - @Override - public Collection<Object> values() { - return map.values(); - } - } - - @Test - // Named property access on a JSObject - public void namedAccessTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - final MapWrapperObject obj = new MapWrapperObject(); - e.put("obj", obj); - obj.getMap().put("foo", "bar"); - - // property-like access on MapWrapperObject objects - assertEquals(e.eval("obj.foo"), "bar"); - e.eval("obj.foo = 'hello'"); - assertEquals(e.eval("'foo' in obj"), Boolean.TRUE); - assertEquals(e.eval("obj.foo"), "hello"); - assertEquals(obj.getMap().get("foo"), "hello"); - e.eval("delete obj.foo"); - assertFalse(obj.getMap().containsKey("foo")); - assertEquals(e.eval("'foo' in obj"), Boolean.FALSE); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - // @bug 8062030: Nashorn bug retrieving array property after key string concatenation - @Test - // ConsString attribute access on a JSObject - public void consStringTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - final MapWrapperObject obj = new MapWrapperObject(); - e.put("obj", obj); - e.put("f", "f"); - e.eval("obj[f + 'oo'] = 'bar';"); - - assertEquals(obj.getMap().get("foo"), "bar"); - assertEquals(e.eval("obj[f + 'oo']"), "bar"); - assertEquals(e.eval("obj['foo']"), "bar"); - assertEquals(e.eval("f + 'oo' in obj"), Boolean.TRUE); - assertEquals(e.eval("'foo' in obj"), Boolean.TRUE); - e.eval("delete obj[f + 'oo']"); - assertFalse(obj.getMap().containsKey("foo")); - assertEquals(e.eval("obj[f + 'oo']"), null); - assertEquals(e.eval("obj['foo']"), null); - assertEquals(e.eval("f + 'oo' in obj"), Boolean.FALSE); - assertEquals(e.eval("'foo' in obj"), Boolean.FALSE); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - public static class BufferObject extends AbstractJSObject { - private final IntBuffer buf; - - public BufferObject(final int size) { - buf = IntBuffer.allocate(size); - } - - public IntBuffer getBuffer() { - return buf; - } - - @Override - public Object getMember(final String name) { - return name.equals("length")? buf.capacity() : null; - } - - @Override - public boolean hasSlot(final int i) { - return i > -1 && i < buf.capacity(); - } - - @Override - public Object getSlot(final int i) { - return buf.get(i); - } - - @Override - public void setSlot(final int i, final Object value) { - buf.put(i, ((Number)value).intValue()); - } - - @Override - public boolean isArray() { - return true; - } - } - - @Test - // array-like indexed access for a JSObject - public void indexedAccessTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - final BufferObject buf = new BufferObject(2); - e.put("buf", buf); - - // array-like access on BufferObject objects - assertEquals(e.eval("buf.length"), buf.getBuffer().capacity()); - e.eval("buf[0] = 23"); - assertEquals(buf.getBuffer().get(0), 23); - assertEquals(e.eval("buf[0]"), 23); - assertEquals(e.eval("buf[1]"), 0); - buf.getBuffer().put(1, 42); - assertEquals(e.eval("buf[1]"), 42); - assertEquals(e.eval("Array.isArray(buf)"), Boolean.TRUE); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - public static class Adder extends AbstractJSObject { - @Override - public Object call(final Object thiz, final Object... args) { - double res = 0.0; - for (final Object arg : args) { - res += ((Number)arg).doubleValue(); - } - return res; - } - - @Override - public boolean isFunction() { - return true; - } - } - - @Test - // a callable JSObject - public void callableJSObjectTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.put("sum", new Adder()); - // check callability of Adder objects - assertEquals(e.eval("typeof sum"), "function"); - assertEquals(((Number)e.eval("sum(1, 2, 3, 4, 5)")).intValue(), 15); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - public static class Factory extends AbstractJSObject { - @SuppressWarnings("unused") - @Override - public Object newObject(final Object... args) { - return new HashMap<Object, Object>(); - } - - @Override - public boolean isFunction() { - return true; - } - } - - @Test - // a factory JSObject - public void factoryJSObjectTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.put("Factory", new Factory()); - - // check new on Factory - assertEquals(e.eval("typeof Factory"), "function"); - assertEquals(e.eval("typeof new Factory()"), "object"); - assertEquals(e.eval("(new Factory()) instanceof java.util.Map"), Boolean.TRUE); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } - - @Test - // iteration tests - public void iteratingJSObjectTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - final MapWrapperObject obj = new MapWrapperObject(); - obj.setMember("foo", "hello"); - obj.setMember("bar", "world"); - e.put("obj", obj); - - // check for..in - Object val = e.eval("var str = ''; for (i in obj) str += i; str"); - assertEquals(val.toString(), "foobar"); - - // check for..each..in - val = e.eval("var str = ''; for each (i in obj) str += i; str"); - assertEquals(val.toString(), "helloworld"); - } catch (final Exception exp) { - exp.printStackTrace(); - fail(exp.getMessage()); - } - } -}
--- a/test/src/jdk/nashorn/api/scripting/ScopeTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,779 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package jdk.nashorn.api.scripting; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; -import javax.script.Bindings; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import javax.script.SimpleBindings; -import javax.script.SimpleScriptContext; -import org.testng.Assert; -import org.testng.annotations.Test; - -/** - * Tests for jsr223 Bindings "scope" (engine, global scopes) - */ -@SuppressWarnings("javadoc") -public class ScopeTest { - - @Test - public void createBindingsTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - b.put("foo", 42.0); - Object res = null; - try { - res = e.eval("foo == 42.0", b); - } catch (final ScriptException | NullPointerException se) { - se.printStackTrace(); - fail(se.getMessage()); - } - - assertEquals(res, Boolean.TRUE); - } - - @Test - public void engineScopeTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings engineScope = e.getBindings(ScriptContext.ENGINE_SCOPE); - - // check few ECMA standard built-in global properties - assertNotNull(engineScope.get("Object")); - assertNotNull(engineScope.get("TypeError")); - assertNotNull(engineScope.get("eval")); - - // can access via ScriptEngine.get as well - assertNotNull(e.get("Object")); - assertNotNull(e.get("TypeError")); - assertNotNull(e.get("eval")); - - // Access by either way should return same object - assertEquals(engineScope.get("Array"), e.get("Array")); - assertEquals(engineScope.get("EvalError"), e.get("EvalError")); - assertEquals(engineScope.get("undefined"), e.get("undefined")); - - // try exposing a new variable from scope - engineScope.put("myVar", "foo"); - try { - assertEquals(e.eval("myVar"), "foo"); - } catch (final ScriptException se) { - se.printStackTrace(); - fail(se.getMessage()); - } - - // update "myVar" in script an check the value from scope - try { - e.eval("myVar = 'nashorn';"); - } catch (final ScriptException se) { - se.printStackTrace(); - fail(se.getMessage()); - } - - // now check modified value from scope and engine - assertEquals(engineScope.get("myVar"), "nashorn"); - assertEquals(e.get("myVar"), "nashorn"); - } - - @Test - public void multiGlobalTest() { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - - try { - final Object obj1 = e.eval("Object"); - final Object obj2 = e.eval("Object", newCtxt); - Assert.assertNotEquals(obj1, obj2); - Assert.assertNotNull(obj1); - Assert.assertNotNull(obj2); - Assert.assertEquals(obj1.toString(), obj2.toString()); - - e.eval("x = 'hello'"); - e.eval("x = 'world'", newCtxt); - Object x1 = e.getContext().getAttribute("x"); - Object x2 = newCtxt.getAttribute("x"); - Assert.assertNotEquals(x1, x2); - Assert.assertEquals(x1, "hello"); - Assert.assertEquals(x2, "world"); - - x1 = e.eval("x"); - x2 = e.eval("x", newCtxt); - Assert.assertNotEquals(x1, x2); - Assert.assertEquals(x1, "hello"); - Assert.assertEquals(x2, "world"); - - final ScriptContext origCtxt = e.getContext(); - e.setContext(newCtxt); - e.eval("y = new Object()"); - e.eval("y = new Object()", origCtxt); - - final Object y1 = origCtxt.getAttribute("y"); - final Object y2 = newCtxt.getAttribute("y"); - Assert.assertNotEquals(y1, y2); - final Object yeval1 = e.eval("y"); - final Object yeval2 = e.eval("y", origCtxt); - Assert.assertNotEquals(yeval1, yeval2); - Assert.assertEquals("[object Object]", y1.toString()); - Assert.assertEquals("[object Object]", y2.toString()); - } catch (final ScriptException se) { - se.printStackTrace(); - fail(se.getMessage()); - } - } - - @Test - public void userEngineScopeBindingsTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - e.eval("function func() {}"); - - final ScriptContext newContext = new SimpleScriptContext(); - newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); - // we are using a new bindings - so it should have 'func' defined - final Object value = e.eval("typeof func", newContext); - assertTrue(value.equals("undefined")); - } - - @Test - public void userEngineScopeBindingsNoLeakTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final ScriptContext newContext = new SimpleScriptContext(); - newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); - e.eval("function foo() {}", newContext); - - // in the default context's ENGINE_SCOPE, 'foo' shouldn't exist - assertTrue(e.eval("typeof foo").equals("undefined")); - } - - @Test - public void userEngineScopeBindingsRetentionTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final ScriptContext newContext = new SimpleScriptContext(); - newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); - e.eval("function foo() {}", newContext); - - // definition retained with user's ENGINE_SCOPE Binding - assertTrue(e.eval("typeof foo", newContext).equals("function")); - - final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE); - // but not in another ENGINE_SCOPE binding - newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); - assertTrue(e.eval("typeof foo", newContext).equals("undefined")); - - // restore ENGINE_SCOPE and check again - newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE); - assertTrue(e.eval("typeof foo", newContext).equals("function")); - } - - @Test - // check that engine.js definitions are visible in all new global instances - public void checkBuiltinsInNewBindingsTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - // check default global instance has engine.js definitions - final Bindings g = (Bindings) e.eval("this"); - Object value = g.get("__noSuchProperty__"); - assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); - value = g.get("print"); - assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); - - // check new global instance created has engine.js definitions - final Bindings b = e.createBindings(); - value = b.get("__noSuchProperty__"); - assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); - value = b.get("print"); - assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); - - // put a mapping into GLOBAL_SCOPE - final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); - globalScope.put("x", "hello"); - - // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval - assertTrue(e.eval("x").equals("hello")); - - final ScriptContext ctx = new SimpleScriptContext(); - ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); - ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); - - // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval - assertTrue(e.eval("x", ctx).equals("hello")); - - // try some arbitray Bindings for ENGINE_SCOPE - final Bindings sb = new SimpleBindings(); - ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); - - // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval - assertTrue(e.eval("x", ctx).equals("hello")); - - // engine.js builtins are still defined even with arbitrary Bindings - assertTrue(e.eval("typeof print", ctx).equals("function")); - assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); - - // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition - sb.put("x", "newX"); - assertTrue(e.eval("x", ctx).equals("newX")); - } - - /** - * Test multi-threaded access to defined global variables for shared script classes with multiple globals. - */ - @Test - public static void multiThreadedVarTest() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - final String sharedScript = "foo"; - - assertEquals(e.eval("var foo = 'original context';", origContext), null); - assertEquals(e.eval("var foo = 'new context';", newCtxt), null); - - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); - t1.start(); - t2.start(); - t1.join(); - t2.join(); - - assertEquals(e.eval("var foo = 'newer context';", newCtxt), null); - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); - - t3.start(); - t4.start(); - t3.join(); - t4.join(); - - assertEquals(e.eval(sharedScript), "original context"); - assertEquals(e.eval(sharedScript, newCtxt), "newer context"); - } - - /** - * Test multi-threaded access to undefined global variables for shared script classes with multiple globals. - */ - @Test - public static void multiThreadedGlobalTest() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - - assertEquals(e.eval("foo = 'original context';", origContext), "original context"); - assertEquals(e.eval("foo = 'new context';", newCtxt), "new context"); - final String sharedScript = "foo"; - - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); - t1.start(); - t2.start(); - t1.join(); - t2.join(); - - final Object obj3 = e.eval("delete foo; foo = 'newer context';", newCtxt); - assertEquals(obj3, "newer context"); - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); - - t3.start(); - t4.start(); - t3.join(); - t4.join(); - - Assert.assertEquals(e.eval(sharedScript), "original context"); - Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); - } - - /** - * Test multi-threaded access using the postfix ++ operator for shared script classes with multiple globals. - */ - @Test - public static void multiThreadedIncTest() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - - assertEquals(e.eval("var x = 0;", origContext), null); - assertEquals(e.eval("var x = 2;", newCtxt), null); - final String sharedScript = "x++;"; - - final Thread t1 = new Thread(new Runnable() { - @Override - public void run() { - try { - for (int i = 0; i < 1000; i++) { - assertEquals(e.eval(sharedScript, origContext), (double)i); - } - } catch (final ScriptException se) { - fail(se.toString()); - } - } - }); - final Thread t2 = new Thread(new Runnable() { - @Override - public void run() { - try { - for (int i = 2; i < 1000; i++) { - assertEquals(e.eval(sharedScript, newCtxt), (double)i); - } - } catch (final ScriptException se) { - fail(se.toString()); - } - } - }); - t1.start(); - t2.start(); - t1.join(); - t2.join(); - } - - /** - * Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals. - */ - @Test - public static void multiThreadedPrimitiveTest() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - - final Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext); - final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt); - assertEquals(obj1, "original context"); - assertEquals(obj2, "new context"); - final String sharedScript = "''.foo"; - - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); - t1.start(); - t2.start(); - t1.join(); - t2.join(); - - final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); - assertEquals(obj3, "newer context"); - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); - - t3.start(); - t4.start(); - t3.join(); - t4.join(); - - Assert.assertEquals(e.eval(sharedScript), "original context"); - Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); - } - - - /** - * Test multi-threaded access to prototype user accessor properties for shared script classes with multiple globals. - */ - @Test - public static void multiThreadedAccessorTest() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - - e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'original context' })", origContext); - e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'new context', configurable: true })", newCtxt); - final String sharedScript = "({}).foo"; - - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); - t1.start(); - t2.start(); - t1.join(); - t2.join(); - - final Object obj3 = e.eval("delete Object.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); - assertEquals(obj3, "newer context"); - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); - - t3.start(); - t4.start(); - t3.join(); - t4.join(); - } - - /** - * Test multi-threaded access to primitive prototype user accessor properties for shared script classes with multiple globals. - */ - @Test - public static void multiThreadedPrimitiveAccessorTest() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - - e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'original context' })", origContext); - e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'new context' })", newCtxt); - final String sharedScript = "''.foo"; - - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); - t1.start(); - t2.start(); - t1.join(); - t2.join(); - - final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); - assertEquals(obj3, "newer context"); - final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); - final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); - - t3.start(); - t4.start(); - t3.join(); - t4.join(); - } - - /** - * Test multi-threaded scope function invocation for shared script classes with multiple globals. - */ - @Test - public static void multiThreadedFunctionTest() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - - e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext); - assertEquals(origContext.getAttribute("scopeVar"), 1); - assertEquals(e.eval("scopeTest()"), 1); - - e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt); - assertEquals(newCtxt.getAttribute("scopeVar"), 1); - assertEquals(e.eval("scopeTest();", newCtxt), 1); - - assertEquals(e.eval("scopeVar = 3;", newCtxt), 3); - assertEquals(newCtxt.getAttribute("scopeVar"), 3); - - - final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000)); - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000)); - - t1.start(); - t2.start(); - t1.join(); - t2.join(); - - } - - /** - * Test multi-threaded access to global getters and setters for shared script classes with multiple globals. - */ - @Test - public static void getterSetterTest() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - final String sharedScript = "accessor1"; - - e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); - assertEquals(e.eval("accessor1 = 1;"), 1); - assertEquals(e.eval(sharedScript), 1); - - e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); - assertEquals(e.eval("accessor1 = 2;", newCtxt), 2); - assertEquals(e.eval(sharedScript, newCtxt), 2); - - - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); - - t1.start(); - t2.start(); - t1.join(); - t2.join(); - - assertEquals(e.eval(sharedScript), 1); - assertEquals(e.eval(sharedScript, newCtxt), 2); - assertEquals(e.eval("v"), 1); - assertEquals(e.eval("v", newCtxt), 2); - } - - /** - * Test multi-threaded access to global getters and setters for shared script classes with multiple globals. - */ - @Test - public static void getterSetter2Test() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = e.createBindings(); - final ScriptContext origContext = e.getContext(); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - final String sharedScript = "accessor2"; - - e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); - assertEquals(e.eval("accessor2 = 1;"), 1); - assertEquals(e.eval(sharedScript), 1); - - e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); - assertEquals(e.eval("accessor2 = 2;", newCtxt), 2); - assertEquals(e.eval(sharedScript, newCtxt), 2); - - - final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); - final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); - - t1.start(); - t2.start(); - t1.join(); - t2.join(); - - assertEquals(e.eval(sharedScript), 1); - assertEquals(e.eval(sharedScript, newCtxt), 2); - assertEquals(e.eval("x"), 1); - assertEquals(e.eval("x", newCtxt), 2); - } - - // @bug 8058422: Users should be able to overwrite "context" and "engine" variables - @Test - public static void contextOverwriteTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = new SimpleBindings(); - b.put("context", "hello"); - b.put("foo", 32); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - e.setContext(newCtxt); - assertEquals(e.eval("context"), "hello"); - assertEquals(((Number)e.eval("foo")).intValue(), 32); - } - - // @bug 8058422: Users should be able to overwrite "context" and "engine" variables - @Test - public static void contextOverwriteInScriptTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - e.put("foo", 32); - - assertEquals(((Number)e.eval("foo")).intValue(), 32); - assertEquals(e.eval("context = 'bar'"), "bar"); - assertEquals(((Number)e.eval("foo")).intValue(), 32); - } - - // @bug 8058422: Users should be able to overwrite "context" and "engine" variables - @Test - public static void engineOverwriteTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Bindings b = new SimpleBindings(); - b.put("engine", "hello"); - b.put("foo", 32); - final ScriptContext newCtxt = new SimpleScriptContext(); - newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - e.setContext(newCtxt); - assertEquals(e.eval("engine"), "hello"); - assertEquals(((Number)e.eval("foo")).intValue(), 32); - } - - // @bug 8058422: Users should be able to overwrite "context" and "engine" variables - @Test - public static void engineOverwriteInScriptTest() throws ScriptException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - e.put("foo", 32); - - assertEquals(((Number)e.eval("foo")).intValue(), 32); - assertEquals(e.eval("engine = 'bar'"), "bar"); - assertEquals(((Number)e.eval("foo")).intValue(), 32); - } - - // @bug 8044750: megamorphic getter for scope objects does not call __noSuchProperty__ hook - @Test - public static void testMegamorphicGetInGlobal() throws Exception { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine engine = m.getEngineByName("nashorn"); - final String script = "foo"; - // "foo" is megamorphic because of different global scopes. - // Make sure ScriptContext variable search works even after - // it becomes megamorphic. - for (int index = 0; index < 25; index++) { - final Bindings bindings = new SimpleBindings(); - bindings.put("foo", index); - final Number value = (Number)engine.eval(script, bindings); - assertEquals(index, value.intValue()); - } - } - - /** - * Test "slow" scopes involving {@code with} and {@code eval} statements for shared script classes with multiple globals. - * @throws ScriptException - * @throws InterruptedException - */ - @Test - public static void testSlowScope() throws ScriptException, InterruptedException { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - for (int i = 0; i < 100; i++) { - final Bindings b = e.createBindings(); - final ScriptContext ctxt = new SimpleScriptContext(); - ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE); - - e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt); - assertEquals(e.eval("a", ctxt), 1); - assertEquals(b.get("a"), 1); - assertEquals(e.eval("b", ctxt), 3); - assertEquals(b.get("b"), 3); - assertEquals(e.eval("c", ctxt), 10); - assertEquals(b.get("c"), 10); - } - } - - private static class ScriptRunner implements Runnable { - - final ScriptEngine engine; - final ScriptContext context; - final String source; - final Object expected; - final int iterations; - - ScriptRunner(final ScriptEngine engine, final ScriptContext context, final String source, final Object expected, final int iterations) { - this.engine = engine; - this.context = context; - this.source = source; - this.expected = expected; - this.iterations = iterations; - } - - @Override - public void run() { - try { - for (int i = 0; i < iterations; i++) { - assertEquals(engine.eval(source, context), expected); - } - } catch (final ScriptException se) { - throw new RuntimeException(se); - } - } - } - - // @bug 8071678: NashornScriptEngine returns javax.script.ScriptContext instance - // with get/setAttribute methods insonsistent for GLOBAL_SCOPE - @Test - public void testGlobalScopeSearch() throws Exception { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final ScriptContext c = e.getContext(); - c.setAttribute("name1234", "value", ScriptContext.GLOBAL_SCOPE); - assertEquals(c.getAttribute("name1234"), "value"); - assertEquals(c.getAttributesScope("name1234"), - ScriptContext.GLOBAL_SCOPE); - } - - // @bug 8071594: NashornScriptEngine returns javax.script.ScriptContext instance - // which doesn't completely conform to the spec regarding exceptions throwing - @Test - public void testScriptContext_NPE_IAE() throws Exception { - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final ScriptContext c = e.getContext(); - try { - c.getAttribute(""); - throw new AssertionError("should have thrown IAE"); - } catch (IllegalArgumentException iae1) {} - - try { - c.getAttribute(null); - throw new AssertionError("should have thrown NPE"); - } catch (NullPointerException npe1) {} - - try { - c.getAttribute("", ScriptContext.ENGINE_SCOPE); - throw new AssertionError("should have thrown IAE"); - } catch (IllegalArgumentException iae2) {} - - try { - c.getAttribute(null, ScriptContext.ENGINE_SCOPE); - throw new AssertionError("should have thrown NPE"); - } catch (NullPointerException npe2) {} - - try { - c.removeAttribute("", ScriptContext.ENGINE_SCOPE); - throw new AssertionError("should have thrown IAE"); - } catch (IllegalArgumentException iae3) {} - - try { - c.removeAttribute(null, ScriptContext.ENGINE_SCOPE); - throw new AssertionError("should have thrown NPE"); - } catch (NullPointerException npe3) {} - - try { - c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE); - throw new AssertionError("should have thrown IAE"); - } catch (IllegalArgumentException iae4) {} - - try { - c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE); - throw new AssertionError("should have thrown NPE"); - } catch (NullPointerException npe4) {} - - try { - c.getAttributesScope(""); - throw new AssertionError("should have thrown IAE"); - } catch (IllegalArgumentException iae5) {} - - try { - c.getAttributesScope(null); - throw new AssertionError("should have thrown NPE"); - } catch (NullPointerException npe5) {} - } -}
--- a/test/src/jdk/nashorn/api/scripting/ScriptEngineSecurityTest.java Wed Apr 08 13:12:53 2015 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,311 +0,0 @@ -/* - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package jdk.nashorn.api.scripting; - -import static org.testng.Assert.fail; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import org.testng.annotations.Test; - -/** - * jsr223 tests for security access checks. - */ -@SuppressWarnings("javadoc") -public class ScriptEngineSecurityTest { - - private static void log(final String msg) { - org.testng.Reporter.log(msg, true); - } - - @Test - public void securityPackagesTest() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.eval("var v = Packages.sun.misc.Unsafe;"); - fail("should have thrown SecurityException"); - } catch (final Exception exp) { - if (exp instanceof SecurityException) { - log("got " + exp + " as expected"); - } else { - fail(exp.getMessage()); - } - } - } - - @Test - public void securityJavaTypeTest() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.eval("var v = Java.type('sun.misc.Unsafe');"); - fail("should have thrown SecurityException"); - } catch (final Exception exp) { - if (exp instanceof SecurityException) { - log("got " + exp + " as expected"); - } else { - fail(exp.getMessage()); - } - } - } - - @Test - public void securityClassForNameTest() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.eval("var v = java.lang.Class.forName('sun.misc.Unsafe');"); - fail("should have thrown SecurityException"); - } catch (final Exception exp) { - if (exp instanceof SecurityException) { - log("got " + exp + " as expected"); - } else { - fail(exp.getMessage()); - } - } - } - - @Test - public void securitySystemExit() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.eval("java.lang.System.exit(0);"); - fail("should have thrown SecurityException"); - } catch (final Exception exp) { - if (exp instanceof SecurityException) { - log("got " + exp + " as expected"); - } else { - fail(exp.getMessage()); - } - } - } - - - @Test - public void securitySystemExitFromFinalizerThread() throws ScriptException { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - e.eval("var o = Java.extend(Java.type('javax.imageio.spi.ServiceRegistry'), { deregisterAll: this.exit.bind(null, 1234)});\n" + - "new o(new java.util.ArrayList().iterator())"); - System.gc(); - System.runFinalization(); - // NOTE: this test just exits the VM if it fails. - } - - @Test - public void securitySystemLoadLibrary() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - try { - e.eval("java.lang.System.loadLibrary('foo');"); - fail("should have thrown SecurityException"); - } catch (final Exception exp) { - if (exp instanceof SecurityException) { - log("got " + exp + " as expected"); - } else { - fail(exp.getMessage()); - } - } - } - - // @bug 8032948: Nashorn linkages awry - @SuppressWarnings("serial") - public static class FakeProxy extends Proxy { - public FakeProxy(final InvocationHandler ih) { - super(ih); - } - - public static Class<?> makeProxyClass(final ClassLoader cl, final Class<?>... ifaces) { - return Proxy.getProxyClass(cl, ifaces); - } - } - - @Test - public void fakeProxySubclassAccessCheckTest() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - e.put("name", ScriptEngineSecurityTest.class.getName()); - e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); - e.put("intfs", new Class[] { Runnable.class }); - - final String getClass = "Java.type(name + '$FakeProxy').getProxyClass(cl, intfs);"; - - // Should not be able to call static methods of Proxy via fake subclass - try { - e.eval(getClass); - fail("should have thrown SecurityException"); - } catch (final Exception exp) { - if (! (exp instanceof SecurityException)) { - fail("SecurityException expected, got " + exp); - } - } - } - - @Test - public void fakeProxySubclassAccessCheckTest2() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - - e.put("name", ScriptEngineSecurityTest.class.getName()); - e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); - e.put("intfs", new Class[] { Runnable.class }); - - final String getClass = "Java.type(name + '$FakeProxy').makeProxyClass(cl, intfs);"; - - // Should not be able to call static methods of Proxy via fake subclass - try { - e.eval(getClass); - fail("should have thrown SecurityException"); - } catch (final Exception exp) { - if (! (exp instanceof SecurityException)) { - fail("SecurityException expected, got " + exp); - } - } - } - - @Test - public static void proxyStaticAccessCheckTest() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final ScriptEngineManager m = new ScriptEngineManager(); - final ScriptEngine e = m.getEngineByName("nashorn"); - final Runnable r = (Runnable)Proxy.newProxyInstance( - ScriptEngineTest.class.getClassLoader(), - new Class[] { Runnable.class }, - new InvocationHandler() { - @Override - public Object invoke(final Object p, final Method mtd, final Object[] a) { - return null; - } - }); - - e.put("rc", r.getClass()); - e.put("cl", ScriptEngineSecurityTest.class.getClassLoader()); - e.put("intfs", new Class[] { Runnable.class }); - - // make sure static methods of Proxy is not accessible via subclass - try { - e.eval("rc.static.getProxyClass(cl, intfs)"); - fail("Should have thrown SecurityException"); - } catch (final Exception exp) { - if (! (exp instanceof SecurityException)) { - fail("SecurityException expected, got " + exp); - } - } - } - - - @Test - public void nashornConfigSecurityTest() { - if (System.getSecurityManager() == null) { - // pass vacuously - return; - } - - final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); - try { - fac.getScriptEngine(new ClassFilter() { - @Override -