1 /*
   2  * Copyright (c) 2010, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.api.javaaccess.test;
  27 
  28 import static org.testng.AssertJUnit.assertEquals;
  29 import static org.testng.AssertJUnit.assertFalse;
  30 import static org.testng.AssertJUnit.assertNull;
  31 import static org.testng.AssertJUnit.assertTrue;
  32 import java.util.Arrays;
  33 import java.util.List;
  34 import javax.script.ScriptContext;
  35 import javax.script.ScriptEngine;
  36 import javax.script.ScriptEngineManager;
  37 import javax.script.ScriptException;
  38 import org.testng.TestNG;
  39 import org.testng.annotations.AfterClass;
  40 import org.testng.annotations.BeforeClass;
  41 import org.testng.annotations.Test;
  42 
  43 @SuppressWarnings("javadoc")
  44 public class ArrayConversionTest {
  45     private static ScriptEngine e = null;
  46 
  47     public static void main(final String[] args) {
  48         TestNG.main(args);
  49     }
  50 
  51     @BeforeClass
  52     public static void setUpClass() {
  53         e = new ScriptEngineManager().getEngineByName("nashorn");
  54     }
  55 
  56     @AfterClass
  57     public static void tearDownClass() {
  58         e = null;
  59     }
  60 
  61     @Test
  62     public void testIntArrays() throws ScriptException {
  63         runTest("assertNullIntArray", "null");
  64         runTest("assertEmptyIntArray", "[]");
  65         runTest("assertSingle42IntArray", "[42]");
  66         runTest("assertSingle42IntArray", "['42']");
  67         runTest("assertIntArrayConversions", "[false, true, NaN, Infinity, -Infinity, 0.4, 0.6, null, undefined, [], {}, [1], [1, 2]]");
  68     }
  69 
  70     @Test
  71     public void testIntIntArrays() throws ScriptException {
  72         runTest("assertNullIntIntArray", "null");
  73         runTest("assertEmptyIntIntArray", "[]");
  74         runTest("assertSingleEmptyIntIntArray", "[[]]");
  75         runTest("assertSingleNullIntIntArray", "[null]");
  76         runTest("assertLargeIntIntArray", "[[false], [1], [2, 3], [4, 5, 6], ['7', {valueOf: function() { return 8 }}]]");
  77     }
  78 
  79     @Test
  80     public void testObjectObjectArrays() throws ScriptException {
  81         runTest("assertLargeObjectObjectArray", "[[false], [1], ['foo', 42.3], [{x: 17}]]");
  82     }
  83 
  84     @Test
  85     public void testBooleanArrays() throws ScriptException {
  86         runTest("assertBooleanArrayConversions", "[false, true, '', 'false', 0, 1, 0.4, 0.6, {}, [], [false], [true], NaN, Infinity, null, undefined]");
  87     }
  88 
  89     @Test
  90     public void testArrayAmbiguity() throws ScriptException {
  91         runTest("x", "'abc'");
  92         runTest("x", "['foo', 'bar']");
  93     }
  94 
  95     @Test
  96     public void testListArrays() throws ScriptException {
  97         runTest("assertListArray", "[['foo', 'bar'], ['apple', 'orange']]");
  98     }
  99 
 100     @Test
 101     public void testVarArgs() throws ScriptException {
 102         // Sole NativeArray in vararg position becomes vararg array itself
 103         runTest("assertVarArg_42_17", "[42, 17]");
 104         // NativeArray in vararg position becomes an argument if there are more arguments
 105         runTest("assertVarArg_array_17", "[42], 18");
 106         // Only NativeArray is converted to vararg array, other objects (e.g. a function) aren't
 107         runTest("assertVarArg_function", "function() { return 'Hello' }");
 108     }
 109 
 110     private static void runTest(final String testMethodName, final String argument) throws ScriptException {
 111         e.eval("Java.type('" + ArrayConversionTest.class.getName() + "')." + testMethodName + "(" + argument + ")");
 112     }
 113 
 114     public static void assertNullIntArray(final int[] array) {
 115         assertNull(array);
 116     }
 117 
 118     public static void assertNullIntIntArray(final int[][] array) {
 119         assertNull(array);
 120     }
 121 
 122     public static void assertEmptyIntArray(final int[] array) {
 123         assertEquals(0, array.length);
 124     }
 125 
 126     public static void assertSingle42IntArray(final int[] array) {
 127         assertEquals(1, array.length);
 128         assertEquals(42, array[0]);
 129     }
 130 
 131 
 132     public static void assertIntArrayConversions(final int[] array) {
 133         assertEquals(13, array.length);
 134         assertEquals(0, array[0]); // false
 135         assertEquals(1, array[1]); // true
 136         assertEquals(0, array[2]); // NaN
 137         assertEquals(0, array[3]); // Infinity
 138         assertEquals(0, array[4]); // -Infinity
 139         assertEquals(0, array[5]); // 0.4
 140         assertEquals(0, array[6]); // 0.6 - floor, not round
 141         assertEquals(0, array[7]); // null
 142         assertEquals(0, array[8]); // undefined
 143         assertEquals(0, array[9]); // []
 144         assertEquals(0, array[10]); // {}
 145         assertEquals(1, array[11]); // [1]
 146         assertEquals(0, array[12]); // [1, 2]
 147     }
 148 
 149     public static void assertEmptyIntIntArray(final int[][] array) {
 150         assertEquals(0, array.length);
 151     }
 152 
 153     public static void assertSingleEmptyIntIntArray(final int[][] array) {
 154         assertEquals(1, array.length);
 155         assertTrue(Arrays.equals(new int[0], array[0]));
 156     }
 157 
 158     public static void assertSingleNullIntIntArray(final int[][] array) {
 159         assertEquals(1, array.length);
 160         assertNull(null, array[0]);
 161     }
 162 
 163     public static void assertLargeIntIntArray(final int[][] array) {
 164         assertEquals(5, array.length);
 165         assertTrue(Arrays.equals(new int[] { 0 }, array[0]));
 166         assertTrue(Arrays.equals(new int[] { 1 }, array[1]));
 167         assertTrue(Arrays.equals(new int[] { 2, 3 }, array[2]));
 168         assertTrue(Arrays.equals(new int[] { 4, 5, 6 }, array[3]));
 169         assertTrue(Arrays.equals(new int[] { 7, 8 }, array[4]));
 170     }
 171 
 172     public static void assertLargeObjectObjectArray(final Object[][] array) throws ScriptException {
 173         assertEquals(4, array.length);
 174         assertTrue(Arrays.equals(new Object[] { Boolean.FALSE }, array[0]));
 175         assertTrue(Arrays.equals(new Object[] { 1 }, array[1]));
 176         assertTrue(Arrays.equals(new Object[] { "foo", 42.3d }, array[2]));
 177         assertEquals(1, array[3].length);
 178         e.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", array[3][0]);
 179         assertEquals(17, e.eval("obj.x"));
 180     }
 181 
 182     public static void assertBooleanArrayConversions(final boolean[] array) {
 183         assertEquals(16, array.length);
 184         assertFalse(array[0]); // false
 185         assertTrue(array[1]); // true
 186         assertFalse(array[2]); // ''
 187         assertTrue(array[3]); // 'false' (yep, every non-empty string converts to true)
 188         assertFalse(array[4]); // 0
 189         assertTrue(array[5]); // 1
 190         assertTrue(array[6]); // 0.4
 191         assertTrue(array[7]); // 0.6
 192         assertTrue(array[8]); // {}
 193         assertTrue(array[9]); // []
 194         assertTrue(array[10]); // [false]
 195         assertTrue(array[11]); // [true]
 196         assertFalse(array[12]); // NaN
 197         assertTrue(array[13]); // Infinity
 198         assertFalse(array[14]); // null
 199         assertFalse(array[15]); // undefined
 200     }
 201 
 202     public static void assertListArray(final List<?>[] array) {
 203         assertEquals(2, array.length);
 204         assertEquals(Arrays.asList("foo", "bar"), array[0]);
 205         assertEquals(Arrays.asList("apple", "orange"), array[1]);
 206     }
 207 
 208     public static void assertVarArg_42_17(final Object... args) {
 209         assertEquals(2, args.length);
 210         assertEquals(42, ((Number)args[0]).intValue());
 211         assertEquals(17, ((Number)args[1]).intValue());
 212     }
 213 
 214     public static void assertVarArg_array_17(final Object... args) throws ScriptException {
 215         assertEquals(2, args.length);
 216         e.getBindings(ScriptContext.ENGINE_SCOPE).put("arr", args[0]);
 217         assertTrue((Boolean)e.eval("arr instanceof Array && arr.length == 1 && arr[0] == 42"));
 218         assertEquals(18, ((Number)args[1]).intValue());
 219     }
 220 
 221     public static void assertVarArg_function(final Object... args) throws ScriptException {
 222         assertEquals(1, args.length);
 223         e.getBindings(ScriptContext.ENGINE_SCOPE).put("fn", args[0]);
 224         assertEquals("Hello", e.eval("fn()"));
 225     }
 226 
 227 
 228 
 229     public static void x(final String y) {
 230         assertEquals("abc", y);
 231     }
 232     public static void x(final String[] y) {
 233         assertTrue(Arrays.equals(new String[] { "foo", "bar"}, y));
 234     }
 235 }