1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 gTestfile = 'string-001.js';
42 * java.lang.String objects "inherit" JS string methods
46 var SECTION = "java.lang.Strings using JavaScript String methods";
48 var TITLE = "LiveConnect 3.0 " + SECTION;
52 var jm = getMethods( "java.lang.String" );
53 var methods = new Array();
55 for ( var i = 0; i < jm.length; i++ ) {
56 cm = jm[i].toString();
57 methods[methods.length] = [ getMethodName(cm), getArguments(cm) ];
62 // These are methods of String.prototype that differ from existing
63 // methods of java.lang.String in argument number or type, and but
64 // according to scott should still be overriden by java.lang.String
67 a[a.length] = new TestObject(
68 "var s"+a.length+" = new java.lang.String(\"hello\"); s"+a.length+".valueOf("+a.length+") +''",
75 // These are methods of String.prototype that should be overriden
76 // by methods of java.lang.String:
77 // toString charAt indexOf lastIndexOf substring substring(int, int)
78 // toLowerCase toUpperCase
80 a[a.length] = new TestObject(
81 "var s" +a.length+" = new java.lang.String(\"boo\"); s"+a.length+".toString() +''",
88 a[a.length] = new TestObject(
89 "var s" +a.length+" = new java.lang.String(\"JavaScript LiveConnect\"); s"+a.length+".charAt(0)",
96 a[a.length] = new TestObject(
97 "var s" +a.length+" = new java.lang.String(\"JavaScript LiveConnect\"); s"+a.length+".indexOf(\"L\")",
105 a[a.length] = new TestObject(
106 "var s" +a.length+" = new java.lang.String(\"JavaScript LiveConnect\"); s"+a.length+".lastIndexOf(\"t\")",
113 a[a.length] = new TestObject(
114 "var s" +a.length+" = new java.lang.String(\"JavaScript LiveConnect\"); s"+a.length+".substring(\"11\") +''",
121 a[a.length] = new TestObject(
122 "var s" +a.length+" = new java.lang.String(\"JavaScript LiveConnect\"); s"+a.length+".substring(\"15\") +''",
129 a[a.length] = new TestObject(
130 "var s" +a.length+" = new java.lang.String(\"JavaScript LiveConnect\"); s"+a.length+".substring(4,10) +''",
137 a[a.length] = new TestObject(
138 "var s" +a.length+" = new java.lang.String(\"JavaScript LiveConnect\"); s"+a.length+".toLowerCase() +''",
143 "javascript liveconnect" );
145 a[a.length] = new TestObject(
146 "var s" +a.length+" = new java.lang.String(\"JavaScript LiveConnect\"); s"+a.length+".toUpperCase() +''",
151 "JAVASCRIPT LIVECONNECT" );
153 // These are methods of String.prototype but are not methods of
154 // java.lang.String, so they should not be overriden. The method
155 // of the instance should be the same as the method of String.prototype
156 // fromCharCode charCodeAt constructor split
158 /* No longer valid in JDK 1.4: java.lang.String now has a split method.
160 a[a.length] = new TestObject(
161 "var s" +a.length+" = new java.lang.String(\"0 1 2 3 4 5 6 7 8 9\"); s"+a.length+".split(\" \") +''",
166 "0,1,2,3,4,5,6,7,8,9" );
169 a[a.length] = new TestObject(
170 "var s" +a.length+" = new java.lang.String(\"0 1 2 3 4 5 6 7 8 9\"); s"+a.length+".constructor",
175 String.prototype.constructor);
180 // figure out what methods exist
181 // if there is no java method with the same name as a js method, should
182 // be able to invoke the js method without casting to a js string. also
183 // the method should equal the same method of String.prototype.
184 // if there is a java method with the same name as a js method, invoking
185 // the method should call the java method
187 function TestObject( description, ob, method, argLength, override, expect ) {
188 this.description = description;
190 this.method = method;
191 this.override = override
192 this.argLength = argLength;
195 this.result = eval(description);
197 this.isJSMethod = eval( ob +"."+ method +" == String.prototype." + method );
204 if ( hasMethod( method, argLength ) ) {
206 ob +"." + method +" == String.prototype." + method,
211 // If the java class has no method with that name and number of
212 // arguments, the value of the method should be the value of
213 // String.prototype.methodName
216 ob +"." + method +" == String.prototype." + method,
222 function getMethods( javaString ) {
223 return java.lang.Class.forName( javaString ).getMethods();
225 function isStatic( m ) {
226 if ( m.lastIndexOf("static") > 0 ) {
227 // static method, return true
232 function getArguments( m ) {
233 var argIndex = m.lastIndexOf("(", m.length());
234 var argString = m.substr(argIndex+1, m.length() - argIndex -2);
235 return argString.split( "," );
237 function getMethodName( m ) {
238 var argIndex = m.lastIndexOf( "(", m.length());
239 var nameIndex = m.lastIndexOf( ".", argIndex);
240 return m.substr( nameIndex +1, argIndex - nameIndex -1 );
242 function hasMethod( m, noArgs ) {
243 for ( var i = 0; i < methods.length; i++ ) {
244 if ( (m == methods[i][0]) && (noArgs == methods[i][1].length)) {