JDK-8072596.js revision 1365:833a4df84bc7
1/* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * JDK-8072596: Arrays.asList results in ClassCastException with a JS array 26 * 27 * @test 28 * @run 29 */ 30var arr = java.util.Arrays.asList("hello world".split(' ')); 31// We split it into a list of two elements: [hello, world] 32Assert.assertTrue(arr instanceof java.util.List); 33Assert.assertEquals(arr.length, 2); 34Assert.assertEquals(arr[0], "hello"); 35Assert.assertEquals(arr[1], "world"); 36 37var Jdk8072596TestSubject = Java.type("jdk.nashorn.test.models.Jdk8072596TestSubject"); 38var testSubject = new Jdk8072596TestSubject({bar: 0}); 39testSubject.test1(true, {foo: 1}, {bar: 2}); 40testSubject.test2(true, {foo: 1}, {bar: 2}, {baz: 3}, {bing: 4}); 41var h = "h"; 42var ello = "ello"; 43testSubject.test3(true, {foo: 5}, /* ConsString, why not */ h + ello, [6, 7], 8); 44Jdk8072596TestSubject.test4({foo: 9}); 45 46// Test wrapping setters arguments and unwrapping getters return values on list. 47var list = new java.util.ArrayList(); 48list.add(null); 49var obj0 = {valueOf: function() { return 0; }}; 50var obj1 = {foo: 10}; 51list[obj0] = obj1; 52testSubject.testListHasWrappedObject(list); 53// NOTE: can't use Assert.assertSame(obj1, list[obj0]), as the arguments would end up being wrapped... 54Assert.assertTrue(obj1 === list[obj0]); 55 56// Test wrapping setters arguments and unwrapping getters return values on array. 57var arr2 = new (Java.type("java.lang.Object[]"))(1); 58var obj2 = {bar: 11}; 59arr2[obj0] = obj2; 60testSubject.testArrayHasWrappedObject(arr2); 61Assert.assertTrue(obj2 === arr2[obj0]); 62 63// Test wrapping setters index and arguments and getters index, and unwrapping getters return values on map. 64// Since ScriptObjectMirror.equals() uses underlying ScriptObject identity, using them as map keys works. 65var map = new java.util.HashMap(); 66var obj3 = {bar: 12}; 67map[obj0] = obj3; 68testSubject.testMapHasWrappedObject(map, obj0); 69Assert.assertTrue(obj3 === map[obj0]); 70