javaarrayconversion.js revision 1427:31f1156a592b
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 assertCanConvert(sourceType, targetType) {
132  Java.to([new (Java.type(sourceType))()], targetType + "[]")
133  ++testCount;
134}
135
136function assertCantConvert(sourceType, targetType) {
137  try {
138    Java.to([new (Java.type(sourceType))()], targetType + "[]")
139    throw "no TypeError encountered"
140  } catch(e) {
141      if(!(e instanceof TypeError) ||
142          !e.message.startsWith("Java.to conversion to array type")) {
143        throw e;
144      }
145      ++testCount;
146  }
147}
148
149// Arbitrary POJOs to JS Primitive type should work
150assertCanConvert("java.util.BitSet", "int")
151assertCanConvert("java.util.BitSet", "double")
152assertCanConvert("java.util.BitSet", "long")
153assertCanConvert("java.util.BitSet", "boolean")
154assertCanConvert("java.util.BitSet", "java.lang.String")
155
156// Arbitrary POJOs can't be converted to Java values
157assertCantConvert("java.util.BitSet", "java.lang.Double")
158assertCantConvert("java.util.BitSet", "java.lang.Long")
159
160/***************************************************************************
161 * Now testing the other way round - Java arrays & collections to JavaScript
162 **************************************************************************/
163
164function assert(x) {
165  if(!x) {
166    throw "Assertion failed"
167  }
168  ++testCount;
169}
170
171var intArray = new (Java.type("int[]"))(3)
172intArray[0] = 1234;
173intArray[1] = 42;
174intArray[2] = 5;
175var jsIntArray = Java.from(intArray)
176assert(jsIntArray instanceof Array);
177assert(jsIntArray[0] === 1234);
178assert(jsIntArray[1] === 42);
179assert(jsIntArray[2] === 5);
180
181// The arrays are copies, they don't reflect each other
182intArray[2] = 6;
183assert(jsIntArray[2] === 5);
184jsIntArray[2] = 7;
185assert(intArray[2] === 6);
186
187var byteArray = new (Java.type("byte[]"))(2)
188byteArray[0] = -128;
189byteArray[1] = 127;
190var jsByteArray = Java.from(byteArray)
191assert(jsByteArray instanceof Array);
192assert(jsByteArray[0] === -128);
193assert(jsByteArray[1] === 127);
194
195var shortArray = new (Java.type("short[]"))(2)
196shortArray[0] = -32768;
197shortArray[1] = 32767;
198var jsShortArray = Java.from(shortArray)
199assert(jsShortArray instanceof Array);
200assert(jsShortArray[0] === -32768);
201assert(jsShortArray[1] === 32767);
202
203var floatArray = new (Java.type("float[]"))(2)
204floatArray[0] = java.lang.Float.MIN_VALUE;
205floatArray[1] = java.lang.Float.MAX_VALUE;
206var jsFloatArray = Java.from(floatArray)
207assert(jsFloatArray instanceof Array);
208assert(jsFloatArray[0] == java.lang.Float.MIN_VALUE);
209assert(jsFloatArray[1] == java.lang.Float.MAX_VALUE);
210
211var charArray = new (Java.type("char[]"))(3)
212charArray[0] = "a";
213charArray[1] = "b";
214charArray[2] = "1";
215var jsCharArray = Java.from(charArray)
216assert(jsCharArray instanceof Array);
217assert(jsCharArray[0] === 97);
218assert(jsCharArray[1] === 98);
219assert(jsCharArray[2] === 49);
220
221var booleanArray = new (Java.type("boolean[]"))(2)
222booleanArray[0] = true;
223booleanArray[1] = false;
224var jsBooleanArray = Java.from(booleanArray)
225assert(jsBooleanArray instanceof Array);
226assert(jsBooleanArray[0] === true);
227assert(jsBooleanArray[1] === false);
228
229print(testCount + " tests completed ok")
230