Debian Codegraph @

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