Debian Codegraph @

rhino/testsrc/tests/lc2/Objects/object-001.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'object-001.js';
7
8 /**
9 File Name: object-001.js
10 Description:
11
12 Given a JavaObject, calling the getClass() method of java.lang.Object
13 should return the java.lang.Class of that object.
14
15 To test this:
16
17 1. Create a JavaObject by instantiating a new object OR call
18 a java method that returns a JavaObject.
19
20 2. Call getClass() on that object. Compare it to the result of
21 java.lang.Class.forName( "<classname>" ).
22
23 3. Also compare the result of getClass() to the literal classname
24
25 Note: this test does not use the LiveConnect getClass function, which
26 currently is not available.
27
28 @author [email protected]
29 @version 1.00
30 */
31 var SECTION = "LiveConnect Objects";
32 var VERSION = "1_3";
33 var TITLE = "Getting the Class of JavaObjects";
34
35 startTest();
36 writeHeaderToLog( SECTION + " "+ TITLE);
37
38 // All JavaObjects are of the type "object"
39
40 var E_TYPE = "object";
41
42 // All JavaObjects [[Class]] property is JavaObject
43 var E_JSCLASS = "[object JavaObject]";
44
45 // Create arrays of actual results (java_array) and
46 // expected results (test_array).
47
48 var java_array = new Array();
49 var test_array = new Array();
50
51 var i = 0;
52
53 java_array[i] = new JavaValue( new java.awt.Rectangle(1,2,3,4) );
54 test_array[i] = new TestValue( "new java.awt.Rectangle(1,2,3,4)", "java.awt.Rectangle" );
55 i++;
56
57 java_array[i] = new JavaValue( new java.io.PrintStream( java.lang.System.out ) );
58 test_array[i] = new TestValue( "new java.io.PrintStream(java.lang.System.out)", "java.io.PrintStream" );
59 i++;
60
61 java_array[i] = new JavaValue( new java.lang.String("hello") );
62 test_array[i] = new TestValue( "new java.lang.String('hello')", "java.lang.String" );
63 i++;
64
65 java_array[i] = new JavaValue( new java.net.URL("http://home.netscape.com/") );
66 test_array[i] = new TestValue( "new java.net.URL('http://home.netscape.com')", "java.net.URL" );
67 i++;
68
69 /*
70 java_array[i] = new JavaValue( java.rmi.RMISecurityManager );
71 test_array[i] = new TestValue( "java.rmi.RMISecurityManager" );
72 i++;
73 java_array[i] = new JavaValue( java.text.DateFormat );
74 test_array[i] = new TestValue( "java.text.DateFormat" );
75 i++;
76 */
77 java_array[i] = new JavaValue( new java.util.Vector() );
78 test_array[i] = new TestValue( "new java.util.Vector()", "java.util.Vector" );
79 i++;
80
81 /*
82 java_array[i] = new JavaValue( new Packages.com.netscape.javascript.Context() );
83 test_array[i] = new TestValue( "new Packages.com.netscape.javascript.Context()", "com.netscape.javascript.Context" );
84 i++;
85
86 java_array[i] = new JavaValue( Packages.com.netscape.javascript.examples.Shell );
87 test_array[i] = new TestValue( "Packages.com.netscape.javascript.examples.Shell" );
88 i++;
89 */
90
91 for ( i = 0; i < java_array.length; i++ ) {
92 CompareValues( java_array[i], test_array[i] );
93 }
94
95 test();
96
97 function CompareValues( javaval, testval ) {
98 // Check type, which should be E_TYPE
99 new TestCase( SECTION,
100 "typeof (" + testval.description +" )",
101 testval.type,
102 javaval.type );
103 /*
104 // Check JavaScript class, which should be E_JSCLASS
105 new TestCase( SECTION,
106 "(" + testval.description +" ).getJSClass()",
107 testval.jsclass,
108 javaval.jsclass );
109 */
110 // Check the JavaClass, which should be the same as the result as Class.forName(description).
111 new TestCase( SECTION,
112 testval.description +".getClass().equals( " +
113 "java.lang.Class.forName( '" + testval.classname +
114 "' ) )",
115 true,
116 javaval.javaclass.equals( testval.javaclass ) );
117 }
118 function JavaValue( value ) {
119 this.type = typeof value;
120 // LC2 does not support the __proto__ property in Java objects
121 // Object.prototype.toString will show its JavaScript wrapper object.
122 // value.__proto__.getJSClass = Object.prototype.toString;
123 // this.jsclass = value.getJSClass();
124 this.javaclass = value.getClass();
125 return this;
126 }
127 function TestValue( description, classname ) {
128 this.description = description;
129 this.classname = classname;
130 this.type = E_TYPE;
131 this.jsclass = E_JSCLASS;
132 this.javaclass = java.lang.Class.forName( classname );
133 return this;
134 }