Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/liveconnect/slot/Slot_004.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package com.netscape.javascript.qa.liveconnect.slot;
6
7 import netscape.javascript.*;
8 import com.netscape.javascript.qa.liveconnect.*;
9
10 /**
11 * Create a JavaScript array. Set an indexed member of the array. Get an
12 * indexed member of the array.
13 *
14 * This covers the following JavaScript -> Java cases through getSlot:
15 * <table border = 1>
16 * <tr>
17 * <th> Java type
18 * <th> JavaScript type
19 * </tr>
20 * <tr>
21 * <td> Java Object
22 * <td> JavaScript JavaObject
23 * </tr>
24 * </table>
25 *
26 * @see netscape.javascript.JSObject#getSlot
27 * @see netscape.javascript.JSObject#setSlot
28 *
29 * @author christine m begle
30 */
31 public class Slot_004 extends LiveConnectTest {
32 public Slot_004() {
33 super();
34 }
35 public static void main( String[] args ) {
36 Slot_004 test = new Slot_004();
37 test.start();
38 }
39
40 public void executeTest() {
41 Object testMatrix[] = getDataArray();
42
43 JSObject jsArray = createJSArray();
44 getLength( jsArray, 0 );
45
46 for ( int i = 0; i < testMatrix.length; i++ ) {
47 setSlot( jsArray, i, (Object[]) testMatrix[i] );
48 getSlot( jsArray, i, (Object[]) testMatrix[i] );
49 }
50 getLength( jsArray, testMatrix.length );
51 }
52
53 /**
54 * Get the data array, which is an array of arrays. Each of the internal
55 * arrays consists of three objects: a string whose value will be passed
56 * to the JavaScript array constructor; a String which is the expected
57 * string value of retreiving the JSObject via getSlot; and a JSObject,
58 * which is the JSObject's constructor, that allows us to verify the type
59 * of JSObject.
60 *
61 * @return the data array.
62 */
63 public Object[] getDataArray() {
64 Object item0[] = {
65 new String("Java String"),
66 new String ("Java String"),
67 "java.lang.String" };
68
69 Object item1[] = {
70 new Integer(12345),
71 new String( "12345" ),
72 "java.lang.Integer" };
73
74 Object item2[] = {
75 new Boolean(false),
76 new String( "false" ),
77 "java.lang.Boolean" };
78
79 Object item3[] = {
80 new Double(12345.0),
81 new String( "12345.0" ),
82 "java.lang.Double" };
83
84 Object dataArray[] = { item0, item1, item2, item3 };
85 return dataArray;
86 }
87
88 /**
89 * Create an empty JavaScript Array named jsArray.
90 *
91 * @return the JSObject array object
92 */
93 public JSObject createJSArray() {
94 JSObject jsArray = null;
95
96 String args = "var jsArray = new Array()";
97 String result = "passed!";
98 try {
99 System.out.println( args );
100 global.eval( args );
101 jsArray = (JSObject) global.getMember("jsArray");
102
103 } catch ( Exception e ) {
104 result = "failed!";
105 exception = "global.getMember(\"jsArray\")"+
106 "threw " + e.toString();
107 file.exception += exception;
108 e.printStackTrace();
109 } finally {
110 addTestCase(
111 "global.eval(\"var jsArray = "+
112 "new Array(); " +
113 "JSObject jsArray = (JSObject) global.getMember(\"jsArray\")",
114 "passed!",
115 result,
116 exception );
117 }
118 return jsArray;
119 }
120
121 /**
122 * Use JSObject.getMember to get the length property of a JSObject.
123 *
124 * @param jsArray a JSObject with a property named "length"
125 * @param javaLength the expected length of the JSObject
126 */
127 public void getLength( JSObject jsArray, int javaLength ) {
128 String exception = "";
129 int jsLength = 0;
130
131 try {
132 jsLength = ((Double) jsArray.getMember("length")).intValue();
133 } catch ( Exception e ) {
134 exception = "jsArray.getMember(\"length\") threw "+
135 e.toString();
136 file.exception += exception;
137 e.printStackTrace();
138 } finally {
139 addTestCase(
140 "[length is " + jsLength +"] "+
141 "( jsArray.getMember(\"length\")).intValue() == " +
142 javaLength +" )",
143 "true",
144 (jsLength == javaLength) + "",
145 exception );
146 }
147 }
148
149 /**
150 * Use JSObject.getSlot to get an indexed member of a JSObject. In this
151 * test, the expected class of all objects is JSObject.
152 *
153 * @param jsArray the JSObject with indexed members
154 * @param slot the index property to retrieve
155 * @param data object array containing the string representation of the
156 * expected result of jsArray.getSlot(slot) and the JSObjectconstructor
157 * of the expected result, which allows us to verify the value and type
158 * of the result object.
159 */
160 public void getSlot( JSObject jsArray, int slot, Object[] data ) {
161 String exception = "";
162 Object result = null;
163 Class eClass = null;
164 Class aClass = null;
165
166 try {
167 result = (Object) jsArray.getSlot( slot );
168 if ( result != null ) {
169 eClass = Class.forName((String) data[2]);
170 aClass = result.getClass();
171 }
172 } catch ( Exception e ) {
173 exception = "jsArray.getSlot( " + slot + " ) "+
174 "threw " + e.toString();
175 file.exception += exception;
176 e.printStackTrace();
177 } finally {
178 if ( result == null ) {
179 addTestCase(
180 "[jsArray.getSlot(" + slot +").toString().trim() returned " + result +"] "+
181 "( " + data[1] +".equals( " + result +" )",
182 "true",
183 data[1].toString().trim().equals( result.toString() ) +"",
184 exception );
185
186 } else {
187 // check the string value of the result
188 addTestCase(
189 "[jsArray.getSlot(" + slot +") returned " + result +"] "+
190 data[1] +".toString().equals( " + result +" ) ",
191 "true",
192 data[1].equals( result.toString() ) +"",
193 exception );
194
195 // check the class of the result. all should be JSObjects.
196 addTestCase(
197 "( " + eClass +".equals( " + aClass +" ) )",
198 "true",
199 eClass.equals( aClass ) +"",
200 exception );
201 }
202 }
203 }
204
205 /**
206 * JSObject.setSlot returns undefined. This just verifies that we can
207 * call setSlot without throwing an exception.
208 *
209 * @param jsArray the JSObject on which setSlot will be called
210 * @param slot the indexed member that will be set
211 * @param javaValue the value to set the indexed member
212 */
213 public void setSlot( JSObject jsArray, int slot, Object[] data ) {
214 String exception = null;
215 String result = "passed!";
216
217 try {
218 jsArray.setSlot( slot, (Object) data[0] );
219 } catch ( Exception e ) {
220 result = "failed!";
221 exception = "jsArray.setSlot( " + slot +","+ data[0] +") "+
222 "threw " + e.toString();
223 file.exception += exception;
224 e.printStackTrace();
225 } finally {
226 addTestCase(
227 "jsArray.setSlot( " + slot +", "+ data[0] +")",
228 "passed!",
229 result,
230 exception );
231 }
232 }
233 }