Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/liveconnect/slot/Slot_001.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> number
23 * <td> java.lang.Double
24 * </tr>
25 * <tr>
26 * <td> string
27 * <td> java.lang.String
28 * </tr>
29 * <tr>
30 * <td> boolean
31 * <td> java.lang.Boolean
32 * </tr>
33 * <tr>
34 * <td> null
35 * <td> null
36 * </tr>
37 * <tr>
38 * <td> JavaObject
39 * <td> the unwrapped original java object (use ==, not .equals() )
40 * </tr>
41 * <tr>
42 * <th colspan=2> Not covered in this test
43 * </tr>
44 * <tr>
45 * <td> JavaScript object
46 * <td> netscape.javascript.JSObject
47 * </tr>
48 * </table>
49 *
50 * @see netscape.javascript.JSObject#getSlot
51 * @see netscape.javascript.JSObject#setSlot
52 *
53 * @author christine m begle
54 */
55 public class Slot_001 extends LiveConnectTest {
56 public Slot_001() {
57 super();
58 }
59 public static void main( String[] args ) {
60 Slot_001 test = new Slot_001();
61 test.start();
62 }
63
64 // The structure of the arrays is the array expression followed by
65 // a list of java objects, correctly typed for what we expect to get
66 // back from getSlot. testMatrix is an array of arrays.
67
68 Object test1[] = { new String("null"), null };
69 Object test2[] = { new String( "" ) };
70 Object test3[] = { new String("true,\"hi\",3.14159"),
71 new Boolean(true), new String("hi"), new Double("3.14159") };
72
73 Object test4[] = { new String( "new java.lang.Boolean(true), " +
74 "new java.lang.String(\"hello\"), java.lang.System.out, "+
75 "new java.lang.Integer(5)"),
76 new Boolean(true),
77 new String("hello"),
78 System.out,
79 new Integer(5) };
80
81 Object testMatrix[] = { test1, test2, test3, test4 };
82
83 public void executeTest() {
84 for ( int i = 0; i < testMatrix.length; i++ ) {
85 String args = (String) ((Object[]) testMatrix[i])[0];
86
87 JSObject jsArray = createJSArray( args );
88
89 // get the length of the array using JSObject.getMember
90 int javaLength = ((Object[]) testMatrix[i]).length -1;
91
92 getLength( jsArray, javaLength );
93
94 for ( int j = 0; j < javaLength ; j++ ) {
95 getSlot( jsArray, j, ((Object[]) testMatrix[i])[j+1] );
96 }
97 }
98 }
99
100 /**
101 * Create a JavaScript Array named jsArray.
102 *
103 * @param newArrayArguments string containing a list of arguments for the
104 * JavaScript Array constructor
105 *
106 * @return the JSObject array object
107 */
108 public JSObject createJSArray( String newArrayArguments ) {
109 JSObject jsArray = null;
110 String args = "var jsArray = new Array( " + newArrayArguments +" )";
111 String result = "passed!";
112 try {
113 System.out.println( args );
114 global.eval( args );
115 jsArray = (JSObject) global.getMember("jsArray");
116
117 } catch ( Exception e ) {
118 result = "failed!";
119 exception = "global.getMember(\"jsArray\")"+
120 "threw " + e.toString();
121 file.exception += exception;
122 e.printStackTrace();
123 } finally {
124 addTestCase(
125 "global.eval(\"var jsArray = "+
126 "new Array(\""+newArrayArguments+"\"); " +
127 "JSObject jsArray = (JSObject) global.getMember(\"jsArray\")",
128 "passed!",
129 result,
130 exception );
131 }
132 return jsArray;
133 }
134
135 /**
136 * Use JSObject.getMember to get the length property of a JSObject.
137 *
138 * @param jsArray a JSObject with a property named "length"
139 * @param javaLength the expected length of the JSObject
140 */
141 public void getLength( JSObject jsArray, int javaLength ) {
142 String exception = "";
143 int jsLength = 0;
144
145 try {
146 jsLength = ((Double) jsArray.getMember("length")).intValue();
147 } catch ( Exception e ) {
148 exception = "jsArray.getMember(\"length\") threw "+
149 e.toString();
150 file.exception += exception;
151 e.printStackTrace();
152 } finally {
153 addTestCase(
154 "[length is " + jsLength +"] "+
155 "( jsArray.getMember(\"length\")).intValue() == " +
156 javaLength +" )",
157 "true",
158 (jsLength == javaLength) + "",
159 exception );
160 }
161 }
162
163 /**
164 * Use JSObject.getSlot to get an indexed member of a JSObject.
165 *
166 * @param jsArray the JSObject with indexed members
167 * @param slot the index property to retrieve
168 * @param javaValue a java object whose type and value are what we
169 * expect jsArray.getSlot(slot) to return.
170 */
171 public void getSlot( JSObject jsArray, int slot, Object javaValue ) {
172 String exception = "";
173 Object result = null;
174 Class eClass = null;
175 Class aClass = null;
176
177 try {
178 result = jsArray.getSlot( slot );
179 if ( javaValue != null ) {
180 eClass = javaValue.getClass();
181 aClass = result.getClass();
182 }
183 } catch ( Exception e ) {
184 exception = "jsArray.getSlot( " + slot + " ) "+
185 "threw " + e.toString();
186 file.exception += exception;
187 e.printStackTrace();
188 } finally {
189 if ( javaValue == null ) {
190 addTestCase(
191 "[jsArray.getSlot(" + slot +") returned " + result +"] "+
192 "( " +javaValue +" == " + result +" )",
193 "true",
194 (javaValue == result ) +"",
195 exception );
196
197 } else {
198 addTestCase(
199 "[jsArray.getSlot(" + slot +") returned " + result +"] "+
200 javaValue +".equals( " + result +" ) ",
201 "true",
202 javaValue.equals( result ) +"",
203 exception );
204
205 addTestCase(
206 "( " + eClass +".equals( " + aClass +" ) )",
207 "true",
208 eClass.equals( aClass ) +"",
209 exception );
210 }
211 }
212 }
213
214 /**
215 * JSObject.setSlot returns undefined. This just verifies that we can
216 * call setSlot without throwing an exception.
217 *
218 * @param jsArray the JSObject on which setSlot will be called
219 * @param slot the indexed member that will be set
220 * @param javaValue the value to set the indexed member
221 */
222 public void setSlot( JSObject jsArray, int slot, Object javaValue ) {
223 String exception = null;
224 String result = "passed!";
225
226 try {
227 jsArray.setSlot( slot, javaValue );
228 } catch ( Exception e ) {
229 result = "failed!";
230 exception = "jsArray.setSlot( " + slot +","+ javaValue +") "+
231 "threw " + e.toString();
232 file.exception += exception;
233 e.printStackTrace();
234 } finally {
235 addTestCase(
236 "jsArray.setSlot( " + slot +", "+ javaValue +")",
237 "passed!",
238 result,
239 exception );
240 }
241 }
242 }
243