Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/liveconnect/slot/Slot_005.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 JS object/ Set indexed members of the object using
12 * JSObject.setSlot. Get indexed members of the object again using getSlot.
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> JSObject
22 * <td> the JavaScript object it refers to
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_005 extends LiveConnectTest {
32 public Slot_005() {
33 super();
34 }
35 public static void main( String[] args ) {
36 Slot_005 test = new Slot_005();
37 test.start();
38 }
39
40 public void executeTest() {
41 getBaseObjects();
42 Object testMatrix[] = getDataArray();
43
44 for ( int i = 0; i < testMatrix.length; i++ ) {
45 JSObject jsObject = createJSObject((Object[]) testMatrix[i]);
46 setSlot( jsObject, i, (Object[]) testMatrix[i] );
47 getSlot( jsObject, i, (Object[]) testMatrix[i] );
48 }
49 }
50
51 public JSObject createJSObject( Object[] data ) {
52 return (JSObject) data[0];
53 }
54
55 public JSObject jsNumber;
56 public JSObject jsFunction;
57 public JSObject jsBoolean;
58 public JSObject jsObject;
59 public JSObject jsString;
60 public JSObject jsMath;
61 public JSObject jsDate;
62 public JSObject jsArray;
63 public JSObject jsRegExp;
64
65 /**
66 * Get the constructor of all the base JavaScript objects. The test
67 * will compare the instance constructor to the base object constructor
68 * to verify that the JavaScript type of the object is corret.
69 *
70 */
71 public boolean getBaseObjects() {
72 try {
73 jsNumber = (JSObject) global.eval( "Number.prototype.constructor" );
74 jsString = (JSObject) global.eval( "String.prototype.constructor" );
75 jsFunction = (JSObject) global.eval( "Function.prototype.constructor" );
76 jsBoolean = (JSObject) global.eval( "Boolean.prototype.constructor" );
77 jsObject = (JSObject) global.eval( "Object.prototype.constructor" );
78 jsMath = (JSObject) global.eval("Math");
79 jsDate = (JSObject) global.eval( "Date.prototype.constructor" );
80 jsArray = (JSObject) global.eval( "Array.prototype.constructor" );
81 jsRegExp = (JSObject) global.eval("RegExp.prototype.constructor");
82
83 } catch ( Exception e ) {
84 System.err.println( "Failed in getBaseObjects: " + e.toString() );
85 e.printStackTrace();
86 return false;
87 }
88
89 return true;
90 }
91
92 /**
93 * Get the data array, which is an array of arrays. Each of the internal
94 * arrays consists of three objects: a string whose value will be passed
95 * to the JavaScript array constructor; a String which is the expected
96 * string value of retreiving the JSObject via getSlot; and a JSObject,
97 * which is the JSObject's constructor, that allows us to verify the type
98 * of JSObject.
99 *
100 * @return the data array.
101 */
102 public Object[] getDataArray() {
103 Object item0[] = {
104 global.eval("new String(\"passed!\")"),
105 global.eval("new String(\"passed!\")"),
106 new String("passed!"),
107 jsString
108 };
109
110 Object item1[] = {
111 global.eval("new Number(12345)"),
112 global.eval( "new Number(98765)"),
113 new String("98765"),
114 jsNumber};
115
116 Object item2[] = {
117 global.eval("new Boolean(false)"),
118 global.eval("new Boolean(true)"),
119 new String("true"),
120 jsBoolean };
121
122 Object item3[] = {
123 global.eval("new Array(0,1,2,3,4)"),
124 global.eval("new Array(\"h\",\"i\")"),
125 new String( "h,i" ),
126 jsArray };
127
128 Object item4[] = {
129 global.eval("new Object()"),
130 global.eval("new Object()"),
131 new String( "[object Object]" ),
132 jsObject };
133
134 Object dataArray[] = { item0, item1, item2, item3, item4 };
135 return dataArray;
136 }
137
138 /**
139 * Create an empty JavaScript Array named jsArray.
140 *
141 * @return the JSObject array object
142 */
143 public JSObject createJSArray() {
144 JSObject jsArray = null;
145
146 String args = "var jsArray = new Array()";
147 String result = "passed!";
148 try {
149 System.out.println( args );
150 global.eval( args );
151 jsArray = (JSObject) global.getMember("jsArray");
152
153 } catch ( Exception e ) {
154 result = "failed!";
155 exception = "global.getMember(\"jsArray\")"+
156 "threw " + e.toString();
157 file.exception += exception;
158 e.printStackTrace();
159 } finally {
160 addTestCase(
161 "global.eval(\"var jsArray = "+
162 "new Array(); " +
163 "JSObject jsArray = (JSObject) global.getMember(\"jsArray\")",
164 "passed!",
165 result,
166 exception );
167 }
168 return jsArray;
169 }
170
171 /**
172 * Use JSObject.getMember to get the length property of a JSObject.
173 *
174 * @param jsArray a JSObject with a property named "length"
175 * @param javaLength the expected length of the JSObject
176 */
177 public void getLength( JSObject jsArray, int javaLength ) {
178 String exception = "";
179 int jsLength = 0;
180
181 try {
182 jsLength = ((Double) jsArray.getMember("length")).intValue();
183 } catch ( Exception e ) {
184 exception = "jsArray.getMember(\"length\") threw "+
185 e.toString();
186 file.exception += exception;
187 e.printStackTrace();
188 } finally {
189 addTestCase(
190 "[length is " + jsLength +"] "+
191 "( jsArray.getMember(\"length\")).intValue() == " +
192 javaLength +" )",
193 "true",
194 (jsLength == javaLength) + "",
195 exception );
196 }
197 }
198
199 /**
200 * Use JSObject.getSlot to get an indexed member of a JSObject. In this
201 * test, the expected class of all objects is JSObject.
202 *
203 * @param jsArray the JSObject with indexed members
204 * @param slot the index property to retrieve
205 * @param data object array containing the string representation of the
206 * expected result of jsArray.getSlot(slot) and the JSObjectconstructor
207 * of the expected result, which allows us to verify the value and type
208 * of the result object.
209 */
210 public void getSlot( JSObject jsArray, int slot, Object[] data ) {
211 String exception = "";
212 JSObject constructor = null;
213 JSObject result = null;
214 Class eClass = null;
215 Class aClass = null;
216
217 try {
218 result = (JSObject) jsArray.getSlot( slot );
219 if ( result != null ) {
220 eClass = Class.forName( "netscape.javascript.JSObject" );
221 aClass = result.getClass();
222 constructor = (JSObject) result.getMember( "constructor" );
223 }
224 } catch ( Exception e ) {
225 exception = "jsArray.getSlot( " + slot + " ) "+
226 "threw " + e.toString();
227 file.exception += exception;
228 e.printStackTrace();
229 } finally {
230 if ( result == null ) {
231 addTestCase(
232 "[jsArray.getSlot(" + slot +").toString().trim() returned " + result +"] "+
233 "( " + data[1] +".equals( " + result +" )",
234 "true",
235 data[1].toString().trim().equals( result.toString() ) +"",
236 exception );
237
238 } else {
239 /*
240 // check the string value of the result
241 addTestCase(
242 "[jsArray.getSlot(" + slot +") returned " + result +"] "+
243 data[2] +".toString().equals( " + result +" ) ",
244 "true",
245 data[2].equals( result.toString() ) +"",
246 exception );
247 */
248 // check the class of the result. all should be JSObjects.
249 addTestCase(
250 "( " + eClass +".equals( " + aClass +" ) )",
251 "true",
252 eClass.equals( aClass ) +"",
253 exception );
254
255 // check the constructor of the result
256 addTestCase(
257 "( " + constructor +".equals( " + data[3] +" ) )",
258 "true",
259 constructor.equals( data[3] ) +"",
260 exception );
261 }
262 }
263 }
264
265 /**
266 * JSObject.setSlot returns undefined. This just verifies that we can
267 * call setSlot without throwing an exception.
268 *
269 * @param jsArray the JSObject on which setSlot will be called
270 * @param slot the indexed member that will be set
271 * @param javaValue the value to set the indexed member
272 */
273 public void setSlot( JSObject jsArray, int slot, Object[] data ) {
274 String exception = null;
275 String result = "passed!";
276
277 try {
278 jsArray.setSlot( slot, (Object) data[1] );
279 } catch ( Exception e ) {
280 result = "failed!";
281 exception = "jsArray.setSlot( " + slot +","+ data[1] +") "+
282 "threw " + e.toString();
283 file.exception += exception;
284 e.printStackTrace();
285 } finally {
286 addTestCase(
287 "jsArray.setSlot( " + slot +", "+ data[1] +")",
288 "passed!",
289 result,
290 exception );
291 }
292 }
293 }