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  */
  30 var arr = java.util.Arrays.asList("hello world".split(' '));
  31 // We split it into a list of two elements: [hello, world]
  32 Assert.assertTrue(arr instanceof java.util.List);
  33 Assert.assertEquals(arr.length, 2);
  34 Assert.assertEquals(arr[0], "hello");
  35 Assert.assertEquals(arr[1], "world");
  36 
  37 var Jdk8072596TestSubject = Java.type("jdk.nashorn.test.models.Jdk8072596TestSubject");
  38 var testSubject = new Jdk8072596TestSubject({bar: 0});
  39 testSubject.test1(true, {foo: 1}, {bar: 2});
  40 testSubject.test2(true, {foo: 1}, {bar: 2}, {baz: 3}, {bing: 4});
  41 var h = "h";
  42 var ello = "ello";
  43 testSubject.test3(true, {foo: 5}, /* ConsString, why not */ h + ello, [6, 7], 8);
  44 Jdk8072596TestSubject.test4({foo: 9});
  45 
  46 // Test wrapping setters arguments and unwrapping getters return values on list.
  47 var list = new java.util.ArrayList();
  48 list.add(null);
  49 var obj0 = {valueOf: function() { return 0; }};
  50 var obj1 = {foo: 10};
  51 list[obj0] = obj1;
  52 testSubject.testListHasWrappedObject(list);
  53 // NOTE: can't use Assert.assertSame(obj1, list[obj0]), as the arguments would end up being wrapped...
  54 Assert.assertTrue(obj1 === list[obj0]);
  55 
  56 // Test wrapping setters arguments and unwrapping getters return values on array.
  57 var arr2 = new (Java.type("java.lang.Object[]"))(1);
  58 var obj2 = {bar: 11};
  59 arr2[obj0] = obj2;
  60 testSubject.testArrayHasWrappedObject(arr2);
  61 Assert.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.
  65 var map = new java.util.HashMap();
  66 var obj3 = {bar: 12};
  67 map[obj0] = obj3;
  68 testSubject.testMapHasWrappedObject(map, obj0);
  69 Assert.assertTrue(obj3 === map[obj0]);