Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/liveconnect/call/Call_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.call;
6
7 import com.netscape.javascript.qa.liveconnect.*;
8 import netscape.javascript.*;
9
10 /**
11 * Instantiate a JavaScript object. From java, use JSObject.call to invoke a
12 * method of that object. Verify the return value.
13 *
14 * call global method. call built in method. cal user defined method. call
15 * method that takes no arguments.
16 *
17 * @see netscape.javascript.JSObject
18 *
19 * @author christine m begle
20 */
21 public class Call_001 extends LiveConnectTest {
22 public Call_001() {
23 super();
24 }
25
26 public static void main( String[] args ) {
27 Call_001 test = new Call_001();
28 test.start();
29 }
30
31 public void setupTestEnvironment() {
32 super.setupTestEnvironment();
33 }
34
35 public void executeTest() {
36 Object data[] = getDataArray();
37 for ( int i = 0; i < data.length; i++ ) {
38 JSObject jsObject = getJSObject( (Object[]) data[i] );
39 call( jsObject, (Object[]) data[i] );
40 }
41 }
42
43 /**
44 * Create and return a JSObject using data in the data array.
45 *
46 * @param data Object array containing name of JSObject, and assignment
47 * expression
48 * @return the JSObject
49 */
50 public JSObject getJSObject( Object[] data ) {
51 String constructor = data[0] +" = " + data[1];
52 JSObject theThis = (JSObject) global.eval( constructor );
53 return theThis;
54 }
55
56 /**
57 * Use JSObject call to invoke a JavaScript method. Verify the value and
58 * class of the object returned.
59 *
60 * @param theThis JSObject whose method will be called will be checked
61 * @param data Object array containing the name of the method, arguments
62 * to be passed to that method, and the expected return value of the
63 * method.
64 */
65 public void call( JSObject theThis, Object[] data ) {
66 String exception = null;
67 String method = (String) data[2];
68 Object args[] = (Object[]) data[3];
69
70 Object eValue = data[4];
71 Object aValue = null;
72 Class eClass = null;
73 Class aClass = null;
74
75 try {
76 aValue = theThis.call( method, (Object[]) args );
77 if ( aValue != null ) {
78 eClass = eValue.getClass();
79 aClass = aValue.getClass();
80 }
81 } catch ( Exception e ) {
82 exception = theThis +".call( " + method + ", " + args+" ) threw "+
83 e.toString();
84 file.exception += exception;
85 e.printStackTrace();
86 } finally {
87 if ( aValue == null ) {
88 } else {
89 // check the value of the property
90 addTestCase(
91 "[ getMember returned " + aValue +" ] "+
92 theThis+".call( "+method+", "+args+" ).equals( "+eValue+" )",
93 "true",
94 aValue.equals(eValue) +"",
95 exception );
96
97 // check the class of the property
98 addTestCase (
99 "[ "+ aValue+".getClass() returned "+aClass.getName()+"] "+
100 aClass.getName() +".equals( " +eClass.getName() +" )",
101 "true",
102 aClass.getName().equals(eClass.getName()) +"",
103 exception );
104 }
105 }
106 }
107
108 /**
109 * Get the data array, which is an object array data arrays, which are
110 * also object arrays.
111 *
112 * <ul>
113 * <li> Identifier for JavaScript object
114 * <li> Assignment expression to initialize JavaScript object
115 * <li> Method of the JavaScript object
116 * <li> Arguments to pass to the method
117 * <li> Value returned by the method
118 * </ul>
119 *
120 * To add test cases to this test, modify this method.
121 *
122 * @return the data array.
123 */
124 public Object[] getDataArray() {
125 Object d0[] = {
126 new String( "boo" ), // 0 identifier
127 new String( "new Boolean()"), // 1 assignment expression
128 new String( "valueOf" ), // 2 method
129 null, // 3 argument array
130 new Boolean( "false" ), // 4 return value
131 };
132
133 Object d1[] = {
134 new String( "date" ),
135 new String( "new Date(0)"),
136 new String( "getUTCFullYear" ),
137 null,
138 new Double( "1970" ),
139 };
140
141 Object dataArray[] = { d0, d1 };
142 return dataArray;
143 }
144 }