Debian Codegraph @

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