javaarrayconversion.js revision 278:1fd18f40ab52
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. 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 * Tests for conversion of JavaScript arrays to Java arrays and the other 26 * way round. Also generally useful as a JavaScript-to-Java type conversion 27 * test. 28 * 29 * @test 30 * @run 31 */ 32 33var x; // used for undefined 34var testCount = 0; 35 36function testF(inputValue, type, testFn) { 37 var x = Java.to([inputValue], type + "[]")[0]; 38 if(!testFn(x)) { 39 throw ("unexpected value: " + x) 40 } 41 ++testCount; 42} 43 44function test(inputValue, type, expectedValue) { 45 testF(inputValue, type, function(x) { return x === expectedValue }) 46} 47 48function testNaN(inputValue, type) { 49 testF(inputValue, type, isNaN) 50} 51 52// Those labeled "Correct?" are not clearly correct conversions. Those 53// labeled "TypeError maybe?" could actually throw a TypeError, or only 54// throw a TypeError when in strict mode. 55// The case of ("false", "boolean") => true is particularly amusing. 56 57test(x, "int", 0) // Correct? TypeError maybe? 58test(null, "int", 0) // Correct? TypeError maybe? 59test(1234, "int", 1234) 60test("1234", "int", 1234) 61test("1234.49", "int", 1234) 62test("1234.51", "int", 1234) // truncates, not rounds 63test(true, "int", 1) 64test(false, "int", 0) 65test("foo", "int", 0) // Correct? TypeError maybe? 66 67test(x, "boolean", false) // Correct? TypeError maybe? 68test(null, "boolean", false) // Correct? TypeError maybe? 69test(0, "boolean", false) 70test(1234, "boolean", true) 71test("foo", "boolean", true) 72test("", "boolean", false) 73test("false", "boolean", true) // Correct? false maybe? 74 75test(x, "java.lang.String", "undefined") // Correct? TypeError maybe? 76test(null, "java.lang.String", null) 77test(1234, "java.lang.String", "1234") 78test(1234.5, "java.lang.String", "1234.5") 79test(true, "java.lang.String", "true") 80test(false, "java.lang.String", "false") 81 82test(x, "java.lang.Integer", null) // Correct? TypeError maybe? 83test(null, "java.lang.Integer", null) 84test(1234, "java.lang.Integer", 1234) 85test("1234", "java.lang.Integer", 1234) 86test("1234.49", "java.lang.Integer", 1234) 87test("1234.51", "java.lang.Integer", 1234) // truncates, not rounds 88test(true, "java.lang.Integer", 1) 89test(false, "java.lang.Integer", 0) 90test("foo", "java.lang.Integer", 0) // Correct? TypeError maybe? 91 92test(x, "java.lang.Boolean", null) // Correct? TypeError maybe? 93test(null, "java.lang.Boolean", null) 94test(0, "java.lang.Boolean", false) 95test(1234, "java.lang.Boolean", true) 96test("foo", "java.lang.Boolean", true) 97test("", "java.lang.Boolean", false) 98test("false", "java.lang.Boolean", true) // Correct? false maybe? 99 100testNaN(x, "double") 101test(null, "double", 0) 102test(1234, "double", 1234) 103test("1234", "double", 1234) 104test("1234.5", "double", 1234.5) 105test(true, "double", 1) 106test(false, "double", 0) 107testNaN("foo", "double") 108 109testNaN(x, "java.lang.Double") 110test(null, "java.lang.Double", null) 111test(1234, "java.lang.Double", 1234) 112test("1234", "java.lang.Double", 1234) 113test("1234.5", "java.lang.Double", 1234.5) 114test(true, "java.lang.Double", 1) 115test(false, "java.lang.Double", 0) 116testNaN("foo", "java.lang.Double") 117 118test({ valueOf: function() { return 42; } }, "int", 42) 119test({ valueOf: function() { return "42"; } }, "int", 42) 120// If there's no valueOf, toString is used 121test({ toString: function() { return "42"; } }, "int", 42) 122// For numbers, valueOf takes precedence over toString 123test({ valueOf: function() { return "42"; }, toString: function() { return "43"; } }, "int", 42) 124 125test({ toString: function() { return "foo"; } }, "java.lang.String", "foo") 126// Yep, even if we have valueOf, toString from prototype takes precedence 127test({ valueOf: function() { return 42; } }, "java.lang.String", "[object Object]") 128// Converting to string, toString takes precedence over valueOf 129test({ valueOf: function() { return "42"; }, toString: function() { return "43"; } }, "java.lang.String", "43") 130 131function assertCantConvert(sourceType, targetType) { 132 try { 133 Java.to([new Java.type(sourceType)()], targetType + "[]") 134 throw "no TypeError encountered" 135 } catch(e) { 136 if(!(e instanceof TypeError)) { 137 throw e; 138 } 139 ++testCount; 140 } 141} 142 143// Arbitrary POJOs can't be converted to Java values 144assertCantConvert("java.util.BitSet", "int") 145assertCantConvert("java.util.BitSet", "double") 146assertCantConvert("java.util.BitSet", "long") 147assertCantConvert("java.util.BitSet", "boolean") 148assertCantConvert("java.util.BitSet", "java.lang.String") 149assertCantConvert("java.util.BitSet", "java.lang.Double") 150assertCantConvert("java.util.BitSet", "java.lang.Long") 151 152/*************************************************************************** 153 * Now testing the other way round - Java arrays & collections to JavaScript 154 **************************************************************************/ 155 156function assert(x) { 157 if(!x) { 158 throw "Assertion failed" 159 } 160 ++testCount; 161} 162 163var intArray = new (Java.type("int[]"))(3) 164intArray[0] = 1234; 165intArray[1] = 42; 166intArray[2] = 5; 167var jsIntArray = Java.from(intArray) 168assert(jsIntArray instanceof Array); 169assert(jsIntArray[0] === 1234); 170assert(jsIntArray[1] === 42); 171assert(jsIntArray[2] === 5); 172 173// The arrays are copies, they don't reflect each other 174intArray[2] = 6; 175assert(jsIntArray[2] === 5); 176jsIntArray[2] = 7; 177assert(intArray[2] === 6); 178 179var byteArray = new (Java.type("byte[]"))(2) 180byteArray[0] = -128; 181byteArray[1] = 127; 182var jsByteArray = Java.from(byteArray) 183assert(jsByteArray instanceof Array); 184assert(jsByteArray[0] === -128); 185assert(jsByteArray[1] === 127); 186 187var shortArray = new (Java.type("short[]"))(2) 188shortArray[0] = -32768; 189shortArray[1] = 32767; 190var jsShortArray = Java.from(shortArray) 191assert(jsShortArray instanceof Array); 192assert(jsShortArray[0] === -32768); 193assert(jsShortArray[1] === 32767); 194 195var floatArray = new (Java.type("float[]"))(2) 196floatArray[0] = java.lang.Float.MIN_VALUE; 197floatArray[1] = java.lang.Float.MAX_VALUE; 198var jsFloatArray = Java.from(floatArray) 199assert(jsFloatArray instanceof Array); 200assert(jsFloatArray[0] == java.lang.Float.MIN_VALUE); 201assert(jsFloatArray[1] == java.lang.Float.MAX_VALUE); 202 203var charArray = new (Java.type("char[]"))(3) 204charArray[0] = "a"; 205charArray[1] = "b"; 206charArray[2] = "1"; 207var jsCharArray = Java.from(charArray) 208assert(jsCharArray instanceof Array); 209assert(jsCharArray[0] === 97); 210assert(jsCharArray[1] === 98); 211assert(jsCharArray[2] === 49); 212 213var booleanArray = new (Java.type("boolean[]"))(2) 214booleanArray[0] = true; 215booleanArray[1] = false; 216var jsBooleanArray = Java.from(booleanArray) 217assert(jsBooleanArray instanceof Array); 218assert(jsBooleanArray[0] === true); 219assert(jsBooleanArray[1] === false); 220 221print(testCount + " tests completed ok") 222