Debian Codegraph @

rhino/testsrc/tests/lc2/Arrays/array-001.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'array-001.js';
7
8 /**
9 File Name: array-001.js
10 Description:
11
12 When accessing a Java field whose value is a java.lang.Array,
13 or if a java method returns a java.lang.Array, JavaScript should
14 read the value as the JavaScript JavaArray, an object whose class
15 is JavaArray.
16
17 To test this:
18
19 1. Call a java method that returns a java.lang.Array
20 2. Check the value of the returned object, which should be the value
21 of the array. Iterate through the array indices.
22 3. Check the type of the returned object, which should be "object"
23 4. Check the class of the object, using Object.prototype.toString,
24 which should be "[object JavaArray]"
25 5. Check the class of the JavaArray, using getClass, and compare
26 it to java.lang.Class.forName("java.lang.Array");
27
28 @author [email protected]
29 @version 1.00
30 */
31 var SECTION = "LiveConnect";
32 var VERSION = "1_3";
33 var TITLE = "Java Array to JavaScript JavaArray object";
34
35 startTest();
36 writeHeaderToLog( SECTION + " "+ TITLE);
37
38 // In all test cases, the expected type is "object, and the expected
39 // class is "Number"
40
41 var E_TYPE = "object";
42 var E_CLASS = "[object JavaArray]";
43 var test_array = new Array();
44
45 // Create arrays of actual results (java_array) and expected results
46 // (test_array).
47
48 var java_array = new Array();
49
50 var i = 0;
51
52 // byte[]
53
54 var byte_array = ( new java.lang.String("hello") ).getBytes();
55
56 java_array[i] = new JavaValue( byte_array );
57 test_array[i] = new TestValue( "( new java.lang.String('hello') ).getBytes()",
58 ["h".charCodeAt(0),
59 "e".charCodeAt(0),
60 "l".charCodeAt(0),
61 "l".charCodeAt(0),
62 "o".charCodeAt(0) ],
63 "[B"
64 );
65 i++;
66
67 // char[]
68 var char_array = ( new java.lang.String("rhino") ).toCharArray();
69
70 java_array[i] = new JavaValue( char_array );
71 test_array[i] = new TestValue( "( new java.lang.String('rhino') ).toCharArray()",
72 [ "r".charCodeAt(0),
73 "h".charCodeAt(0),
74 "i".charCodeAt(0),
75 "n".charCodeAt(0),
76 "o".charCodeAt(0)],
77 "[C");
78 i++;
79
80
81 for ( i = 0; i < java_array.length; i++ ) {
82 CompareValues( java_array[i], test_array[i] );
83 }
84
85 test();
86
87 function CompareValues( javaval, testval ) {
88 // Check value
89
90 for ( var i = 0; i < testval.value.length; i++ ) {
91 new TestCase( SECTION,
92 "("+ testval.description +")["+i+"]",
93 testval.value[i],
94 javaval.value[i] );
95 }
96
97 // Check type
98
99 new TestCase( SECTION,
100 "typeof (" + testval.description +")",
101 testval.type,
102 javaval.type );
103
104
105 // Check class
106 new TestCase(SECTION,
107 "The Java Class of ( "+ testval.description +" )",
108 testval.lcclass +"",
109 javaval.lcclass +"");
110
111 }
112 function JavaValue( value ) {
113 this.value = value;
114 this.type = typeof value;
115 this.classname = this.value.getClass();
116
117 jlo_class = java.lang.Class.forName("java.lang.Object")
118 jlo_getClass_method = jlo_class.getMethod("getClass", null)
119 this.lcclass = jlo_getClass_method.invoke(value, null );
120
121 return this;
122 }
123 function TestValue( description, value, lcclass ) {
124 this.lcclass = java.lang.Class.forName( lcclass );
125 this.description = description;
126 this.value = value;
127 this.type = E_TYPE;
128 this.classname = E_CLASS;
129 return this;
130 }