Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/liveconnect/member/Member_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.member;
6
7 import com.netscape.javascript.qa.liveconnect.*;
8 import netscape.javascript.*;
9
10 /**
11 * Create JavaScript objects, and set their properties to a JavaScript object
12 * using eval or call. Verify the values of the properties using getMember.
13 *
14 * @see netscape.javascript.JSObject
15 * @see com.netscape.javascript.qa.liveconnect.datatypes.DataTypes_003
16 * @see com.netscape.javascript.qa.liveconnect.datatypes.DataTypes_004
17 *
18 * @author christine m begle
19 */
20 public class Member_002 extends LiveConnectTest {
21 public Member_002() {
22 super();
23 }
24
25 public static void main( String[] args ) {
26 Member_002 test = new Member_002();
27 test.start();
28 }
29
30 public void setupTestEnvironment() {
31 super.setupTestEnvironment();
32 }
33
34 public void executeTest() {
35 Object data[] = getDataArray();
36 for ( int i = 0; i < data.length; i++ ) {
37 JSObject jsObject = getJSObject( (Object[]) data[i] );
38 setMember( (Object[]) data[i] );
39 getMember( jsObject, (Object[]) data[i] );
40 removeMember( jsObject, (Object[]) data[i] );
41 }
42 }
43
44 /**
45 * Create and return a JSObject using data in the data array.
46 *
47 * @param data Object array containing name of JSObject, and assignment
48 * expression
49 * @return the JSObject
50 */
51 public JSObject getJSObject( Object[] data ) {
52 String constructor = data[0] +" = " + data[1];
53 JSObject theThis = (JSObject) global.eval( constructor );
54 return theThis;
55 }
56
57 /**
58 * Use JSObject.eval to assign a JavaScript value to a property of a
59 * JavaScript object. Verify that the expression returns the value of
60 * the assignment expression.
61 *
62 * @param data Object array that has items corresponding to the name of
63 * a JSObject, a property of that object, and a JavaScript assignment
64 * expression for that property.
65 */
66 public void setMember( Object[] data ) {
67 Object result = null;
68 String evalString = (String) data[0] + "." + (String) data[2] +" = "+
69 (String) data[3];
70 try {
71 result = global.eval( evalString );
72 } catch ( Exception e ) {
73 exception = evalString + " threw " + e.toString();
74 file.exception += exception;
75 e.printStackTrace();
76 } finally {
77 addTestCase(
78 "[ " + evalString+ " returned "+result+" ] " +
79 result +".equals( " + data[5] +")",
80 "true",
81 result.equals(data[5]) +"",
82 exception );
83 }
84 }
85
86 /**
87 * Get the value of a JavaScript property. Check its type and value.
88 *
89 * @param theThis JSObject whose property will be checked
90 * @param data Object array containing the name of the JSObject property
91 * and the expected value of that property.
92 */
93 public void getMember( JSObject theThis, Object[] data ) {
94 String exception = null;
95 String property = (String) data[2];
96 Object eValue = data[6];
97 Object aValue = null;
98 Class eClass = null;
99 Class aClass = null;
100 String eType = (String) data[7];
101 String aType = null;
102
103 try {
104 aValue = theThis.getMember( property );
105 if ( aValue != null ) {
106 eClass = eValue.getClass();
107 aClass = aValue.getClass();
108 aType = (String) global.eval( "typeof " + ((String) data[0]) +
109 "." + ((String) data[2]) );
110 }
111 } catch ( Exception e ) {
112 exception = theThis +".getMember( " + property + " ) threw " +
113 e.toString();
114 file.exception += exception;
115 e.printStackTrace();
116 } finally {
117 if ( aValue == null ) {
118 } else {
119 // check the value of the property
120 addTestCase(
121 "[ getMember returned " + aValue +" ] "+
122 theThis+".getMember( " +property+" ).equals( "+eValue+" )",
123 "true",
124 aValue.equals(eValue) +"",
125 exception );
126
127 // check the class of the property
128 addTestCase (
129 "[ "+ aValue+".getClass() returned "+aClass.getName()+"] "+
130 aClass.getName() +".equals( " +eClass.getName() +" )",
131 "true",
132 aClass.getName().equals(eClass.getName()) +"",
133 exception );
134
135 // check the JS type of the property value
136 addTestCase(
137 "[ typeof " +aValue +" returned " + aType +" ] "+
138 aType +" .equals( " +eType + " )",
139 "true",
140 aType.equals( eType ) +"",
141 exception );
142 }
143 }
144 }
145 /**
146 * Delete a JSObject using JSObject.removeMember. After removing the
147 * member, check the member's JavaScript value and type.
148 *
149 *
150
151 *
152 *
153 */
154 public void removeMember( JSObject theThis, Object[] data ) {
155 String exception = null;
156 String property = (String) data[2];
157 Object eValue = data[8];
158 Object aValue = null;
159 Class eClass = null;
160 Class aClass = null;
161 String eType = (String) data[9];
162 String aType = null;
163
164 try {
165 theThis.removeMember( property );
166 aValue = theThis.getMember( property );
167 if ( aValue != null ) {
168 eClass = eValue.getClass();
169 aClass = aValue.getClass();
170 aType = (String) global.eval( "typeof " + ((String) data[0]) +
171 "." + ((String) data[2]) );
172 }
173 } catch ( Exception e ) {
174 exception = theThis +".getMember( " + property + " ) threw " +
175 e.toString();
176 file.exception += exception;
177 e.printStackTrace();
178 } finally {
179 if ( aValue == null ) {
180 } else {
181 // check the value of the property
182 addTestCase(
183 "[ after removing, getMember returned " + aValue +" ] "+
184 theThis+".getMember( " +property+" ).equals( "+eValue+" )",
185 "true",
186 aValue.equals(eValue) +"",
187 exception );
188
189 // check the class of the property
190 addTestCase (
191 "[ after removing, "+ aValue+".getClass() returned "+aClass.getName()+"] "+
192 aClass.getName() +".equals( " +eClass.getName() +" )",
193 "true",
194 aClass.getName().equals(eClass.getName()) +"",
195 exception );
196
197 // check the JS type of the property value
198 addTestCase(
199 "[ after removing, typeof " +aValue +" returned " + aType +" ] "+
200 aType +" .equals( " +eType + " )",
201 "true",
202 aType.equals( eType ) +"",
203 exception );
204 }
205 }
206
207 }
208
209 /**
210 * Get the data array, which is an object array data arrays, which are
211 * also object arrays. The data arrays consist of 8 items
212 *
213 * <ul>
214 * <li> Identifier for JavaScript object
215 * <li> Assignment expression to initialize JavaScript object
216 * <li> Property of JavaScript object to set / get
217 * <li> Value to assign to property
218 * <li> Initial value of property
219 * <li> Value of property before calling setMember
220 * <li> Value returned by setMember
221 * <li> Value of property after calling setMember
222 * <li> String typeof as determined by JavaScript
223 * </ul>
224 *
225 * To add test cases to this test, modify this method.
226 *
227 * @return the data array.
228 */
229 public Object[] getDataArray() {
230 Object d0[] = {
231 new String( "boo" ), // 0 identifier
232 new String( "new Boolean()"), // 1 assignment expression
233 new String( "foo" ), // 2 property
234 new String( "\"bar\"" ), // 3 JavaScript value to assign
235 new String( "undefined" ), // 4 value before assignment
236 new String( "bar" ), // 5 value returned by setter
237 new String( "bar" ), // 6 value after assignment
238 new String( "string"), // 7 JS typeof value
239 new String( "undefined"), // 8 value after removing
240 new String( "undefined") // 9 typeof after removing
241 };
242
243 Object d1[] = {
244 new String( "num" ),
245 new String( "new Number(12345)" ),
246 new String( "someProperty" ),
247 new String( ".02134" ),
248 new String( "undefined" ),
249 new Double ( "0.02134"),
250 new Double ( "0.02134"),
251 new String ("number"),
252 new String( "undefined"), // 8 value after removing
253 new String( "undefined") // 9 typeof after removing
254
255 };
256
257 Object d2[] = {
258 new String( "number" ),
259 new String( "Number" ),
260 new String( "POSITIVE_INFINITY" ),
261 new String( "0" ),
262 new Double( Double.POSITIVE_INFINITY ),
263 new Double("0" ),
264 new Double( Double.POSITIVE_INFINITY ),
265 new String( "number" ) ,
266 new Double( Double.POSITIVE_INFINITY ),
267 new String( "number" )
268 };
269
270 Object dataArray[] = { d0, d1, d2 };
271 return dataArray;
272 }
273 }