1.1 --- a/emul/src/main/java/java/lang/Character.java Sun Dec 16 20:11:18 2012 +0100
1.2 +++ b/emul/src/main/java/java/lang/Character.java Mon Dec 17 09:57:22 2012 +0100
1.3 @@ -25,6 +25,8 @@
1.4
1.5 package java.lang;
1.6
1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.8 +
1.9 /**
1.10 * The {@code Character} class wraps a value of the primitive
1.11 * type {@code char} in an object. An object of type
1.12 @@ -1525,7 +1527,7 @@
1.13 * @see Character#getType(char)
1.14 */
1.15 public static boolean isLowerCase(char ch) {
1.16 - throw new UnsupportedOperationException();
1.17 + return ch == toLowerCase(ch);
1.18 }
1.19
1.20 /**
1.21 @@ -1560,7 +1562,7 @@
1.22 * @since 1.0
1.23 */
1.24 public static boolean isUpperCase(char ch) {
1.25 - throw new UnsupportedOperationException();
1.26 + return ch == toUpperCase(ch);
1.27 }
1.28
1.29 /**
1.30 @@ -1676,7 +1678,7 @@
1.31 * @see Character#getType(char)
1.32 */
1.33 public static boolean isDigit(char ch) {
1.34 - return isDigit((int)ch);
1.35 + return String.valueOf(ch).matches("\\d");
1.36 }
1.37
1.38 /**
1.39 @@ -1710,8 +1712,11 @@
1.40 * @since 1.5
1.41 */
1.42 public static boolean isDigit(int codePoint) {
1.43 - return getType(codePoint) == Character.DECIMAL_DIGIT_NUMBER;
1.44 + return fromCodeChars(codePoint).matches("\\d");
1.45 }
1.46 +
1.47 + @JavaScriptBody(args = "c", body = "return String.fromCharCode(c);")
1.48 + private native static String fromCodeChars(int codePoint);
1.49
1.50 /**
1.51 * Determines if a character is defined in Unicode.
1.52 @@ -1802,7 +1807,7 @@
1.53 * @see Character#isUpperCase(char)
1.54 */
1.55 public static boolean isLetter(char ch) {
1.56 - return isLetter((int)ch);
1.57 + return String.valueOf(ch).matches("\\w") && !isDigit(ch);
1.58 }
1.59
1.60 /**
1.61 @@ -1835,12 +1840,7 @@
1.62 * @since 1.5
1.63 */
1.64 public static boolean isLetter(int codePoint) {
1.65 - return ((((1 << Character.UPPERCASE_LETTER) |
1.66 - (1 << Character.LOWERCASE_LETTER) |
1.67 - (1 << Character.TITLECASE_LETTER) |
1.68 - (1 << Character.MODIFIER_LETTER) |
1.69 - (1 << Character.OTHER_LETTER)) >> getType(codePoint)) & 1)
1.70 - != 0;
1.71 + return fromCodeChars(codePoint).matches("\\w") && !isDigit(codePoint);
1.72 }
1.73
1.74 /**
1.75 @@ -1868,7 +1868,7 @@
1.76 * @since 1.0.2
1.77 */
1.78 public static boolean isLetterOrDigit(char ch) {
1.79 - return isLetterOrDigit((int)ch);
1.80 + return String.valueOf(ch).matches("\\w");
1.81 }
1.82
1.83 /**
1.84 @@ -1889,13 +1889,7 @@
1.85 * @since 1.5
1.86 */
1.87 public static boolean isLetterOrDigit(int codePoint) {
1.88 - return ((((1 << Character.UPPERCASE_LETTER) |
1.89 - (1 << Character.LOWERCASE_LETTER) |
1.90 - (1 << Character.TITLECASE_LETTER) |
1.91 - (1 << Character.MODIFIER_LETTER) |
1.92 - (1 << Character.OTHER_LETTER) |
1.93 - (1 << Character.DECIMAL_DIGIT_NUMBER)) >> getType(codePoint)) & 1)
1.94 - != 0;
1.95 + return fromCodeChars(codePoint).matches("\\w");
1.96 }
1.97
1.98 static int getType(int x) {
1.99 @@ -1930,7 +1924,7 @@
1.100 * @see String#toLowerCase()
1.101 */
1.102 public static char toLowerCase(char ch) {
1.103 - throw new UnsupportedOperationException();
1.104 + return String.valueOf(ch).toLowerCase().charAt(0);
1.105 }
1.106
1.107 /**
1.108 @@ -1961,7 +1955,7 @@
1.109 * @see String#toUpperCase()
1.110 */
1.111 public static char toUpperCase(char ch) {
1.112 - throw new UnsupportedOperationException();
1.113 + return String.valueOf(ch).toUpperCase().charAt(0);
1.114 }
1.115
1.116 /**
2.1 --- a/emul/src/main/java/java/lang/Object.java Sun Dec 16 20:11:18 2012 +0100
2.2 +++ b/emul/src/main/java/java/lang/Object.java Mon Dec 17 09:57:22 2012 +0100
2.3 @@ -104,6 +104,11 @@
2.4 * @see java.lang.Object#equals(java.lang.Object)
2.5 * @see java.lang.System#identityHashCode
2.6 */
2.7 + @JavaScriptBody(args = "self", body =
2.8 + "if (self.$hashCode) return self.$hashCode;\n"
2.9 + + "var h = Math.random() * Math.pow(2, 32);\n"
2.10 + + "return self.$hashCode = h & h;"
2.11 + )
2.12 public native int hashCode();
2.13
2.14 /**
3.1 --- a/emul/src/main/java/java/lang/String.java Sun Dec 16 20:11:18 2012 +0100
3.2 +++ b/emul/src/main/java/java/lang/String.java Mon Dec 17 09:57:22 2012 +0100
3.3 @@ -2143,6 +2143,12 @@
3.4 * @since 1.4
3.5 * @spec JSR-51
3.6 */
3.7 + @JavaScriptBody(args = { "self", "regex" }, body =
3.8 + "self = self.toString();\n"
3.9 + + "var re = new RegExp(regex.toString());\n"
3.10 + + "var r = re.exec(self);\n"
3.11 + + "return r != null && r.length > 0 && self.length == r[0].length;"
3.12 + )
3.13 public boolean matches(String regex) {
3.14 throw new UnsupportedOperationException();
3.15 }
3.16 @@ -2555,6 +2561,7 @@
3.17 * @return the <code>String</code>, converted to lowercase.
3.18 * @see java.lang.String#toLowerCase(Locale)
3.19 */
3.20 + @JavaScriptBody(args = "self", body = "return self.toLowerCase();")
3.21 public String toLowerCase() {
3.22 throw new UnsupportedOperationException("Should be supported but without connection to locale");
3.23 }
3.24 @@ -2720,6 +2727,7 @@
3.25 * @return the <code>String</code>, converted to uppercase.
3.26 * @see java.lang.String#toUpperCase(Locale)
3.27 */
3.28 + @JavaScriptBody(args = "self", body = "return self.toUpperCase();")
3.29 public String toUpperCase() {
3.30 throw new UnsupportedOperationException();
3.31 }
4.1 --- a/emul/src/main/java/java/net/URL.java Sun Dec 16 20:11:18 2012 +0100
4.2 +++ b/emul/src/main/java/java/net/URL.java Mon Dec 17 09:57:22 2012 +0100
4.3 @@ -196,6 +196,17 @@
4.4 */
4.5 private String ref;
4.6
4.7 + /**
4.8 + * The host's IP address, used in equals and hashCode.
4.9 + * Computed on demand. An uninitialized or unknown hostAddress is null.
4.10 + */
4.11 + transient Object hostAddress;
4.12 +
4.13 + /**
4.14 + * The URLStreamHandler for this URL.
4.15 + */
4.16 + transient URLStreamHandler handler;
4.17 +
4.18 /* Our hash code.
4.19 * @serial
4.20 */
4.21 @@ -308,8 +319,47 @@
4.22 this(protocol, host, -1, file);
4.23 }
4.24
4.25 - private URL(String protocol, String host, int port, String file,
4.26 - Object handler) throws MalformedURLException {
4.27 + /**
4.28 + * Creates a <code>URL</code> object from the specified
4.29 + * <code>protocol</code>, <code>host</code>, <code>port</code>
4.30 + * number, <code>file</code>, and <code>handler</code>. Specifying
4.31 + * a <code>port</code> number of <code>-1</code> indicates that
4.32 + * the URL should use the default port for the protocol. Specifying
4.33 + * a <code>handler</code> of <code>null</code> indicates that the URL
4.34 + * should use a default stream handler for the protocol, as outlined
4.35 + * for:
4.36 + * java.net.URL#URL(java.lang.String, java.lang.String, int,
4.37 + * java.lang.String)
4.38 + *
4.39 + * <p>If the handler is not null and there is a security manager,
4.40 + * the security manager's <code>checkPermission</code>
4.41 + * method is called with a
4.42 + * <code>NetPermission("specifyStreamHandler")</code> permission.
4.43 + * This may result in a SecurityException.
4.44 + *
4.45 + * No validation of the inputs is performed by this constructor.
4.46 + *
4.47 + * @param protocol the name of the protocol to use.
4.48 + * @param host the name of the host.
4.49 + * @param port the port number on the host.
4.50 + * @param file the file on the host
4.51 + * @param handler the stream handler for the URL.
4.52 + * @exception MalformedURLException if an unknown protocol is specified.
4.53 + * @exception SecurityException
4.54 + * if a security manager exists and its
4.55 + * <code>checkPermission</code> method doesn't allow
4.56 + * specifying a stream handler explicitly.
4.57 + * @see java.lang.System#getProperty(java.lang.String)
4.58 + * @see java.net.URL#setURLStreamHandlerFactory(
4.59 + * java.net.URLStreamHandlerFactory)
4.60 + * @see java.net.URLStreamHandler
4.61 + * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
4.62 + * java.lang.String)
4.63 + * @see SecurityManager#checkPermission
4.64 + * @see java.net.NetPermission
4.65 + */
4.66 + public URL(String protocol, String host, int port, String file,
4.67 + URLStreamHandler handler) throws MalformedURLException {
4.68 if (handler != null) {
4.69 throw new SecurityException();
4.70 }
4.71 @@ -348,10 +398,11 @@
4.72
4.73 // Note: we don't do validation of the URL here. Too risky to change
4.74 // right now, but worth considering for future reference. -br
4.75 -// if (handler == null &&
4.76 -// (handler = getURLStreamHandler(protocol)) == null) {
4.77 -// throw new MalformedURLException("unknown protocol: " + protocol);
4.78 -// }
4.79 + if (handler == null &&
4.80 + (handler = getURLStreamHandler(protocol)) == null) {
4.81 + throw new MalformedURLException("unknown protocol: " + protocol);
4.82 + }
4.83 + this.handler = handler;
4.84 }
4.85
4.86 /**
4.87 @@ -441,7 +492,7 @@
4.88 * @see java.net.URLStreamHandler#parseURL(java.net.URL,
4.89 * java.lang.String, int, int)
4.90 */
4.91 - private URL(URL context, String spec, Object handler)
4.92 + public URL(URL context, String spec, URLStreamHandler handler)
4.93 throws MalformedURLException
4.94 {
4.95 String original = spec;
4.96 @@ -494,9 +545,9 @@
4.97 newProtocol.equalsIgnoreCase(context.protocol))) {
4.98 // inherit the protocol handler from the context
4.99 // if not specified to the constructor
4.100 -// if (handler == null) {
4.101 -// handler = context.handler;
4.102 -// }
4.103 + if (handler == null) {
4.104 + handler = context.handler;
4.105 + }
4.106
4.107 // If the context is a hierarchical URL scheme and the spec
4.108 // contains a matching scheme then maintain backwards
4.109 @@ -523,15 +574,15 @@
4.110
4.111 // Get the protocol handler if not specified or the protocol
4.112 // of the context could not be used
4.113 -// if (handler == null &&
4.114 -// (handler = getURLStreamHandler(protocol)) == null) {
4.115 -// throw new MalformedURLException("unknown protocol: "+protocol);
4.116 -// }
4.117 -
4.118 -// this.handler = handler;
4.119 + if (handler == null &&
4.120 + (handler = getURLStreamHandler(protocol)) == null) {
4.121 + throw new MalformedURLException("unknown protocol: "+protocol);
4.122 + }
4.123 + this.handler = handler;
4.124
4.125 i = spec.indexOf('#', start);
4.126 if (i >= 0) {
4.127 +//thrw(protocol + " hnd: " + handler.getClass().getName() + " i: " + i);
4.128 ref = spec.substring(i + 1, limit);
4.129 limit = i;
4.130 }
4.131 @@ -547,7 +598,7 @@
4.132 }
4.133 }
4.134
4.135 -// handler.parseURL(this, spec, start, limit);
4.136 + handler.parseURL(this, spec, start, limit);
4.137
4.138 } catch(MalformedURLException e) {
4.139 throw e;
4.140 @@ -557,7 +608,7 @@
4.141 throw exception;
4.142 }
4.143 }
4.144 -
4.145 +
4.146 /*
4.147 * Returns true if specified string is a valid protocol name.
4.148 */
4.149 @@ -601,6 +652,7 @@
4.150 /* This is very important. We must recompute this after the
4.151 * URL has been changed. */
4.152 hashCode = -1;
4.153 + hostAddress = null;
4.154 int q = file.lastIndexOf('?');
4.155 if (q != -1) {
4.156 query = file.substring(q+1);
4.157 @@ -639,6 +691,7 @@
4.158 /* This is very important. We must recompute this after the
4.159 * URL has been changed. */
4.160 hashCode = -1;
4.161 + hostAddress = null;
4.162 this.query = query;
4.163 this.authority = authority;
4.164 }
4.165 @@ -697,6 +750,19 @@
4.166 }
4.167
4.168 /**
4.169 + * Gets the default port number of the protocol associated
4.170 + * with this <code>URL</code>. If the URL scheme or the URLStreamHandler
4.171 + * for the URL do not define a default port number,
4.172 + * then -1 is returned.
4.173 + *
4.174 + * @return the port number
4.175 + * @since 1.4
4.176 + */
4.177 + public int getDefaultPort() {
4.178 + return handler.getDefaultPort();
4.179 + }
4.180 +
4.181 + /**
4.182 * Gets the protocol name of this <code>URL</code>.
4.183 *
4.184 * @return the protocol of this <code>URL</code>.
4.185 @@ -773,8 +839,7 @@
4.186 return false;
4.187 URL u2 = (URL)obj;
4.188
4.189 - // return handler.equals(this, u2);
4.190 - return u2 == this;
4.191 + return handler.equals(this, u2);
4.192 }
4.193
4.194 /**
4.195 @@ -789,7 +854,7 @@
4.196 if (hashCode != -1)
4.197 return hashCode;
4.198
4.199 - // hashCode = handler.hashCode(this);
4.200 + hashCode = handler.hashCode(this);
4.201 return hashCode;
4.202 }
4.203
4.204 @@ -805,8 +870,7 @@
4.205 * <code>false</code> otherwise.
4.206 */
4.207 public boolean sameFile(URL other) {
4.208 -// return handler.sameFile(this, other);
4.209 - throw new UnsupportedOperationException();
4.210 + return handler.sameFile(this, other);
4.211 }
4.212
4.213 /**
4.214 @@ -834,8 +898,7 @@
4.215 * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
4.216 */
4.217 public String toExternalForm() {
4.218 - throw new UnsupportedOperationException();
4.219 -// return handler.toExternalForm(this);
4.220 + return handler.toExternalForm(this);
4.221 }
4.222
4.223 /**
4.224 @@ -925,6 +988,11 @@
4.225 // return openConnection().getContent(classes);
4.226 }
4.227
4.228 + static URLStreamHandler getURLStreamHandler(String protocol) {
4.229 + Class<URLStreamHandler> c = URLStreamHandler.class; // XXX only here to pre-initialize URLStreamHandler
4.230 + URLStreamHandler universal = new URLStreamHandler() {};
4.231 + return universal;
4.232 + }
4.233
4.234 }
4.235
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/emul/src/main/java/java/net/URLStreamHandler.java Mon Dec 17 09:57:22 2012 +0100
5.3 @@ -0,0 +1,568 @@
5.4 +/*
5.5 + * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5.7 + *
5.8 + * This code is free software; you can redistribute it and/or modify it
5.9 + * under the terms of the GNU General Public License version 2 only, as
5.10 + * published by the Free Software Foundation. Oracle designates this
5.11 + * particular file as subject to the "Classpath" exception as provided
5.12 + * by Oracle in the LICENSE file that accompanied this code.
5.13 + *
5.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
5.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5.17 + * version 2 for more details (a copy is included in the LICENSE file that
5.18 + * accompanied this code).
5.19 + *
5.20 + * You should have received a copy of the GNU General Public License version
5.21 + * 2 along with this work; if not, write to the Free Software Foundation,
5.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5.23 + *
5.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5.25 + * or visit www.oracle.com if you need additional information or have any
5.26 + * questions.
5.27 + */
5.28 +
5.29 +package java.net;
5.30 +
5.31 +
5.32 +/**
5.33 + * The abstract class <code>URLStreamHandler</code> is the common
5.34 + * superclass for all stream protocol handlers. A stream protocol
5.35 + * handler knows how to make a connection for a particular protocol
5.36 + * type, such as <code>http</code>, <code>ftp</code>, or
5.37 + * <code>gopher</code>.
5.38 + * <p>
5.39 + * In most cases, an instance of a <code>URLStreamHandler</code>
5.40 + * subclass is not created directly by an application. Rather, the
5.41 + * first time a protocol name is encountered when constructing a
5.42 + * <code>URL</code>, the appropriate stream protocol handler is
5.43 + * automatically loaded.
5.44 + *
5.45 + * @author James Gosling
5.46 + * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
5.47 + * @since JDK1.0
5.48 + */
5.49 +public abstract class URLStreamHandler {
5.50 + /**
5.51 + * Opens a connection to the object referenced by the
5.52 + * <code>URL</code> argument.
5.53 + * This method should be overridden by a subclass.
5.54 + *
5.55 + * <p>If for the handler's protocol (such as HTTP or JAR), there
5.56 + * exists a public, specialized URLConnection subclass belonging
5.57 + * to one of the following packages or one of their subpackages:
5.58 + * java.lang, java.io, java.util, java.net, the connection
5.59 + * returned will be of that subclass. For example, for HTTP an
5.60 + * HttpURLConnection will be returned, and for JAR a
5.61 + * JarURLConnection will be returned.
5.62 + *
5.63 + * @param u the URL that this connects to.
5.64 + * @return a <code>URLConnection</code> object for the <code>URL</code>.
5.65 + * @exception IOException if an I/O error occurs while opening the
5.66 + * connection.
5.67 + */
5.68 +// abstract protected URLConnection openConnection(URL u) throws IOException;
5.69 +
5.70 + /**
5.71 + * Same as openConnection(URL), except that the connection will be
5.72 + * made through the specified proxy; Protocol handlers that do not
5.73 + * support proxying will ignore the proxy parameter and make a
5.74 + * normal connection.
5.75 + *
5.76 + * Calling this method preempts the system's default ProxySelector
5.77 + * settings.
5.78 + *
5.79 + * @param u the URL that this connects to.
5.80 + * @param p the proxy through which the connection will be made.
5.81 + * If direct connection is desired, Proxy.NO_PROXY
5.82 + * should be specified.
5.83 + * @return a <code>URLConnection</code> object for the <code>URL</code>.
5.84 + * @exception IOException if an I/O error occurs while opening the
5.85 + * connection.
5.86 + * @exception IllegalArgumentException if either u or p is null,
5.87 + * or p has the wrong type.
5.88 + * @exception UnsupportedOperationException if the subclass that
5.89 + * implements the protocol doesn't support this method.
5.90 + * @since 1.5
5.91 + */
5.92 +// protected URLConnection openConnection(URL u, Proxy p) throws IOException {
5.93 +// throw new UnsupportedOperationException("Method not implemented.");
5.94 +// }
5.95 +
5.96 + /**
5.97 + * Parses the string representation of a <code>URL</code> into a
5.98 + * <code>URL</code> object.
5.99 + * <p>
5.100 + * If there is any inherited context, then it has already been
5.101 + * copied into the <code>URL</code> argument.
5.102 + * <p>
5.103 + * The <code>parseURL</code> method of <code>URLStreamHandler</code>
5.104 + * parses the string representation as if it were an
5.105 + * <code>http</code> specification. Most URL protocol families have a
5.106 + * similar parsing. A stream protocol handler for a protocol that has
5.107 + * a different syntax must override this routine.
5.108 + *
5.109 + * @param u the <code>URL</code> to receive the result of parsing
5.110 + * the spec.
5.111 + * @param spec the <code>String</code> representing the URL that
5.112 + * must be parsed.
5.113 + * @param start the character index at which to begin parsing. This is
5.114 + * just past the '<code>:</code>' (if there is one) that
5.115 + * specifies the determination of the protocol name.
5.116 + * @param limit the character position to stop parsing at. This is the
5.117 + * end of the string or the position of the
5.118 + * "<code>#</code>" character, if present. All information
5.119 + * after the sharp sign indicates an anchor.
5.120 + */
5.121 + protected void parseURL(URL u, String spec, int start, int limit) {
5.122 + // These fields may receive context content if this was relative URL
5.123 + String protocol = u.getProtocol();
5.124 + String authority = u.getAuthority();
5.125 + String userInfo = u.getUserInfo();
5.126 + String host = u.getHost();
5.127 + int port = u.getPort();
5.128 + String path = u.getPath();
5.129 + String query = u.getQuery();
5.130 +
5.131 + // This field has already been parsed
5.132 + String ref = u.getRef();
5.133 +
5.134 + boolean isRelPath = false;
5.135 + boolean queryOnly = false;
5.136 +
5.137 +// FIX: should not assume query if opaque
5.138 + // Strip off the query part
5.139 + if (start < limit) {
5.140 + int queryStart = spec.indexOf('?');
5.141 + queryOnly = queryStart == start;
5.142 + if ((queryStart != -1) && (queryStart < limit)) {
5.143 + query = spec.substring(queryStart+1, limit);
5.144 + if (limit > queryStart)
5.145 + limit = queryStart;
5.146 + spec = spec.substring(0, queryStart);
5.147 + }
5.148 + }
5.149 +
5.150 + int i = 0;
5.151 + // Parse the authority part if any
5.152 + boolean isUNCName = (start <= limit - 4) &&
5.153 + (spec.charAt(start) == '/') &&
5.154 + (spec.charAt(start + 1) == '/') &&
5.155 + (spec.charAt(start + 2) == '/') &&
5.156 + (spec.charAt(start + 3) == '/');
5.157 + if (!isUNCName && (start <= limit - 2) && (spec.charAt(start) == '/') &&
5.158 + (spec.charAt(start + 1) == '/')) {
5.159 + start += 2;
5.160 + i = spec.indexOf('/', start);
5.161 + if (i < 0) {
5.162 + i = spec.indexOf('?', start);
5.163 + if (i < 0)
5.164 + i = limit;
5.165 + }
5.166 +
5.167 + host = authority = spec.substring(start, i);
5.168 +
5.169 + int ind = authority.indexOf('@');
5.170 + if (ind != -1) {
5.171 + userInfo = authority.substring(0, ind);
5.172 + host = authority.substring(ind+1);
5.173 + } else {
5.174 + userInfo = null;
5.175 + }
5.176 + if (host != null) {
5.177 + // If the host is surrounded by [ and ] then its an IPv6
5.178 + // literal address as specified in RFC2732
5.179 + if (host.length()>0 && (host.charAt(0) == '[')) {
5.180 + if ((ind = host.indexOf(']')) > 2) {
5.181 +
5.182 + String nhost = host ;
5.183 + host = nhost.substring(0,ind+1);
5.184 +// if (!IPAddressUtil.
5.185 +// isIPv6LiteralAddress(host.substring(1, ind))) {
5.186 +// throw new IllegalArgumentException(
5.187 +// "Invalid host: "+ host);
5.188 +// }
5.189 +
5.190 + port = -1 ;
5.191 + if (nhost.length() > ind+1) {
5.192 + if (nhost.charAt(ind+1) == ':') {
5.193 + ++ind ;
5.194 + // port can be null according to RFC2396
5.195 + if (nhost.length() > (ind + 1)) {
5.196 + port = Integer.parseInt(nhost.substring(ind+1));
5.197 + }
5.198 + } else {
5.199 + throw new IllegalArgumentException(
5.200 + "Invalid authority field: " + authority);
5.201 + }
5.202 + }
5.203 + } else {
5.204 + throw new IllegalArgumentException(
5.205 + "Invalid authority field: " + authority);
5.206 + }
5.207 + } else {
5.208 + ind = host.indexOf(':');
5.209 + port = -1;
5.210 + if (ind >= 0) {
5.211 + // port can be null according to RFC2396
5.212 + if (host.length() > (ind + 1)) {
5.213 + port = Integer.parseInt(host.substring(ind + 1));
5.214 + }
5.215 + host = host.substring(0, ind);
5.216 + }
5.217 + }
5.218 + } else {
5.219 + host = "";
5.220 + }
5.221 + if (port < -1)
5.222 + throw new IllegalArgumentException("Invalid port number :" +
5.223 + port);
5.224 + start = i;
5.225 + // If the authority is defined then the path is defined by the
5.226 + // spec only; See RFC 2396 Section 5.2.4.
5.227 + if (authority != null && authority.length() > 0)
5.228 + path = "";
5.229 + }
5.230 +
5.231 + if (host == null) {
5.232 + host = "";
5.233 + }
5.234 +
5.235 + // Parse the file path if any
5.236 + if (start < limit) {
5.237 + if (spec.charAt(start) == '/') {
5.238 + path = spec.substring(start, limit);
5.239 + } else if (path != null && path.length() > 0) {
5.240 + isRelPath = true;
5.241 + int ind = path.lastIndexOf('/');
5.242 + String seperator = "";
5.243 + if (ind == -1 && authority != null)
5.244 + seperator = "/";
5.245 + path = path.substring(0, ind + 1) + seperator +
5.246 + spec.substring(start, limit);
5.247 +
5.248 + } else {
5.249 + String seperator = (authority != null) ? "/" : "";
5.250 + path = seperator + spec.substring(start, limit);
5.251 + }
5.252 + } else if (queryOnly && path != null) {
5.253 + int ind = path.lastIndexOf('/');
5.254 + if (ind < 0)
5.255 + ind = 0;
5.256 + path = path.substring(0, ind) + "/";
5.257 + }
5.258 + if (path == null)
5.259 + path = "";
5.260 +
5.261 + if (isRelPath) {
5.262 + // Remove embedded /./
5.263 + while ((i = path.indexOf("/./")) >= 0) {
5.264 + path = path.substring(0, i) + path.substring(i + 2);
5.265 + }
5.266 + // Remove embedded /../ if possible
5.267 + i = 0;
5.268 + while ((i = path.indexOf("/../", i)) >= 0) {
5.269 + /*
5.270 + * A "/../" will cancel the previous segment and itself,
5.271 + * unless that segment is a "/../" itself
5.272 + * i.e. "/a/b/../c" becomes "/a/c"
5.273 + * but "/../../a" should stay unchanged
5.274 + */
5.275 + if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
5.276 + (path.indexOf("/../", limit) != 0)) {
5.277 + path = path.substring(0, limit) + path.substring(i + 3);
5.278 + i = 0;
5.279 + } else {
5.280 + i = i + 3;
5.281 + }
5.282 + }
5.283 + // Remove trailing .. if possible
5.284 + while (path.endsWith("/..")) {
5.285 + i = path.indexOf("/..");
5.286 + if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {
5.287 + path = path.substring(0, limit+1);
5.288 + } else {
5.289 + break;
5.290 + }
5.291 + }
5.292 + // Remove starting .
5.293 + if (path.startsWith("./") && path.length() > 2)
5.294 + path = path.substring(2);
5.295 +
5.296 + // Remove trailing .
5.297 + if (path.endsWith("/."))
5.298 + path = path.substring(0, path.length() -1);
5.299 + }
5.300 +
5.301 + setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
5.302 + }
5.303 +
5.304 + /**
5.305 + * Returns the default port for a URL parsed by this handler. This method
5.306 + * is meant to be overidden by handlers with default port numbers.
5.307 + * @return the default port for a <code>URL</code> parsed by this handler.
5.308 + * @since 1.3
5.309 + */
5.310 + protected int getDefaultPort() {
5.311 + return -1;
5.312 + }
5.313 +
5.314 + /**
5.315 + * Provides the default equals calculation. May be overidden by handlers
5.316 + * for other protocols that have different requirements for equals().
5.317 + * This method requires that none of its arguments is null. This is
5.318 + * guaranteed by the fact that it is only called by java.net.URL class.
5.319 + * @param u1 a URL object
5.320 + * @param u2 a URL object
5.321 + * @return <tt>true</tt> if the two urls are
5.322 + * considered equal, ie. they refer to the same
5.323 + * fragment in the same file.
5.324 + * @since 1.3
5.325 + */
5.326 + protected boolean equals(URL u1, URL u2) {
5.327 + String ref1 = u1.getRef();
5.328 + String ref2 = u2.getRef();
5.329 + return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
5.330 + sameFile(u1, u2);
5.331 + }
5.332 +
5.333 + /**
5.334 + * Provides the default hash calculation. May be overidden by handlers for
5.335 + * other protocols that have different requirements for hashCode
5.336 + * calculation.
5.337 + * @param u a URL object
5.338 + * @return an <tt>int</tt> suitable for hash table indexing
5.339 + * @since 1.3
5.340 + */
5.341 + protected int hashCode(URL u) {
5.342 + int h = 0;
5.343 +
5.344 + // Generate the protocol part.
5.345 + String protocol = u.getProtocol();
5.346 + if (protocol != null)
5.347 + h += protocol.hashCode();
5.348 +
5.349 + // Generate the host part.
5.350 + Object addr = getHostAddress(u);
5.351 + if (addr != null) {
5.352 + h += addr.hashCode();
5.353 + } else {
5.354 + String host = u.getHost();
5.355 + if (host != null)
5.356 + h += host.toLowerCase().hashCode();
5.357 + }
5.358 +
5.359 + // Generate the file part.
5.360 + String file = u.getFile();
5.361 + if (file != null)
5.362 + h += file.hashCode();
5.363 +
5.364 + // Generate the port part.
5.365 + if (u.getPort() == -1)
5.366 + h += getDefaultPort();
5.367 + else
5.368 + h += u.getPort();
5.369 +
5.370 + // Generate the ref part.
5.371 + String ref = u.getRef();
5.372 + if (ref != null)
5.373 + h += ref.hashCode();
5.374 +
5.375 + return h;
5.376 + }
5.377 +
5.378 + /**
5.379 + * Compare two urls to see whether they refer to the same file,
5.380 + * i.e., having the same protocol, host, port, and path.
5.381 + * This method requires that none of its arguments is null. This is
5.382 + * guaranteed by the fact that it is only called indirectly
5.383 + * by java.net.URL class.
5.384 + * @param u1 a URL object
5.385 + * @param u2 a URL object
5.386 + * @return true if u1 and u2 refer to the same file
5.387 + * @since 1.3
5.388 + */
5.389 + protected boolean sameFile(URL u1, URL u2) {
5.390 + // Compare the protocols.
5.391 + if (!((u1.getProtocol() == u2.getProtocol()) ||
5.392 + (u1.getProtocol() != null &&
5.393 + u1.getProtocol().equalsIgnoreCase(u2.getProtocol()))))
5.394 + return false;
5.395 +
5.396 + // Compare the files.
5.397 + if (!(u1.getFile() == u2.getFile() ||
5.398 + (u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
5.399 + return false;
5.400 +
5.401 + // Compare the ports.
5.402 + int port1, port2;
5.403 + port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort();
5.404 + port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort();
5.405 + if (port1 != port2)
5.406 + return false;
5.407 +
5.408 + // Compare the hosts.
5.409 + if (!hostsEqual(u1, u2))
5.410 + return false;
5.411 +
5.412 + return true;
5.413 + }
5.414 +
5.415 + /**
5.416 + * Get the IP address of our host. An empty host field or a DNS failure
5.417 + * will result in a null return.
5.418 + *
5.419 + * @param u a URL object
5.420 + * @return an <code>InetAddress</code> representing the host
5.421 + * IP address.
5.422 + * @since 1.3
5.423 + */
5.424 + private synchronized Object getHostAddress(URL u) {
5.425 + return u.hostAddress;
5.426 + }
5.427 +
5.428 + /**
5.429 + * Compares the host components of two URLs.
5.430 + * @param u1 the URL of the first host to compare
5.431 + * @param u2 the URL of the second host to compare
5.432 + * @return <tt>true</tt> if and only if they
5.433 + * are equal, <tt>false</tt> otherwise.
5.434 + * @since 1.3
5.435 + */
5.436 + protected boolean hostsEqual(URL u1, URL u2) {
5.437 + Object a1 = getHostAddress(u1);
5.438 + Object a2 = getHostAddress(u2);
5.439 + // if we have internet address for both, compare them
5.440 + if (a1 != null && a2 != null) {
5.441 + return a1.equals(a2);
5.442 + // else, if both have host names, compare them
5.443 + } else if (u1.getHost() != null && u2.getHost() != null)
5.444 + return u1.getHost().equalsIgnoreCase(u2.getHost());
5.445 + else
5.446 + return u1.getHost() == null && u2.getHost() == null;
5.447 + }
5.448 +
5.449 + /**
5.450 + * Converts a <code>URL</code> of a specific protocol to a
5.451 + * <code>String</code>.
5.452 + *
5.453 + * @param u the URL.
5.454 + * @return a string representation of the <code>URL</code> argument.
5.455 + */
5.456 + protected String toExternalForm(URL u) {
5.457 +
5.458 + // pre-compute length of StringBuffer
5.459 + int len = u.getProtocol().length() + 1;
5.460 + if (u.getAuthority() != null && u.getAuthority().length() > 0)
5.461 + len += 2 + u.getAuthority().length();
5.462 + if (u.getPath() != null) {
5.463 + len += u.getPath().length();
5.464 + }
5.465 + if (u.getQuery() != null) {
5.466 + len += 1 + u.getQuery().length();
5.467 + }
5.468 + if (u.getRef() != null)
5.469 + len += 1 + u.getRef().length();
5.470 +
5.471 + StringBuffer result = new StringBuffer(len);
5.472 + result.append(u.getProtocol());
5.473 + result.append(":");
5.474 + if (u.getAuthority() != null && u.getAuthority().length() > 0) {
5.475 + result.append("//");
5.476 + result.append(u.getAuthority());
5.477 + }
5.478 + if (u.getPath() != null) {
5.479 + result.append(u.getPath());
5.480 + }
5.481 + if (u.getQuery() != null) {
5.482 + result.append('?');
5.483 + result.append(u.getQuery());
5.484 + }
5.485 + if (u.getRef() != null) {
5.486 + result.append("#");
5.487 + result.append(u.getRef());
5.488 + }
5.489 + return result.toString();
5.490 + }
5.491 +
5.492 + /**
5.493 + * Sets the fields of the <code>URL</code> argument to the indicated values.
5.494 + * Only classes derived from URLStreamHandler are supposed to be able
5.495 + * to call the set method on a URL.
5.496 + *
5.497 + * @param u the URL to modify.
5.498 + * @param protocol the protocol name.
5.499 + * @param host the remote host value for the URL.
5.500 + * @param port the port on the remote machine.
5.501 + * @param authority the authority part for the URL.
5.502 + * @param userInfo the userInfo part of the URL.
5.503 + * @param path the path component of the URL.
5.504 + * @param query the query part for the URL.
5.505 + * @param ref the reference.
5.506 + * @exception SecurityException if the protocol handler of the URL is
5.507 + * different from this one
5.508 + * @see java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
5.509 + * @since 1.3
5.510 + */
5.511 + protected void setURL(URL u, String protocol, String host, int port,
5.512 + String authority, String userInfo, String path,
5.513 + String query, String ref) {
5.514 + if (this != u.handler) {
5.515 + throw new SecurityException("handler for url different from " +
5.516 + "this handler");
5.517 + }
5.518 + // ensure that no one can reset the protocol on a given URL.
5.519 + u.set(u.getProtocol(), host, port, authority, userInfo, path, query, ref);
5.520 + }
5.521 +
5.522 + /**
5.523 + * Sets the fields of the <code>URL</code> argument to the indicated values.
5.524 + * Only classes derived from URLStreamHandler are supposed to be able
5.525 + * to call the set method on a URL.
5.526 + *
5.527 + * @param u the URL to modify.
5.528 + * @param protocol the protocol name. This value is ignored since 1.2.
5.529 + * @param host the remote host value for the URL.
5.530 + * @param port the port on the remote machine.
5.531 + * @param file the file.
5.532 + * @param ref the reference.
5.533 + * @exception SecurityException if the protocol handler of the URL is
5.534 + * different from this one
5.535 + * @deprecated Use setURL(URL, String, String, int, String, String, String,
5.536 + * String);
5.537 + */
5.538 + @Deprecated
5.539 + protected void setURL(URL u, String protocol, String host, int port,
5.540 + String file, String ref) {
5.541 + /*
5.542 + * Only old URL handlers call this, so assume that the host
5.543 + * field might contain "user:passwd@host". Fix as necessary.
5.544 + */
5.545 + String authority = null;
5.546 + String userInfo = null;
5.547 + if (host != null && host.length() != 0) {
5.548 + authority = (port == -1) ? host : host + ":" + port;
5.549 + int at = host.lastIndexOf('@');
5.550 + if (at != -1) {
5.551 + userInfo = host.substring(0, at);
5.552 + host = host.substring(at+1);
5.553 + }
5.554 + }
5.555 +
5.556 + /*
5.557 + * Assume file might contain query part. Fix as necessary.
5.558 + */
5.559 + String path = null;
5.560 + String query = null;
5.561 + if (file != null) {
5.562 + int q = file.lastIndexOf('?');
5.563 + if (q != -1) {
5.564 + query = file.substring(q+1);
5.565 + path = file.substring(0, q);
5.566 + } else
5.567 + path = file;
5.568 + }
5.569 + setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
5.570 + }
5.571 +}
6.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Dec 16 20:11:18 2012 +0100
6.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Dec 17 09:57:22 2012 +0100
6.3 @@ -19,7 +19,6 @@
6.4
6.5 import java.io.IOException;
6.6 import java.io.InputStream;
6.7 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
6.8 import org.apidesign.javap.AnnotationParser;
6.9 import org.apidesign.javap.ClassData;
6.10 import org.apidesign.javap.FieldData;
6.11 @@ -76,6 +75,11 @@
6.12
6.13 public String compile(InputStream classFile) throws IOException {
6.14 this.jc = new ClassData(classFile);
6.15 + if (jc.getMajor_version() < 50) {
6.16 + throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
6.17 + + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
6.18 + );
6.19 + }
6.20 byte[] arrData = jc.findAnnotationData(true);
6.21 String[] arr = findAnnotation(arrData, jc,
6.22 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
6.23 @@ -632,7 +636,7 @@
6.24 case opc_i2b:
6.25 case opc_i2c:
6.26 case opc_i2s:
6.27 - out.append("/* number conversion */");
6.28 + out.append("{ /* number conversion */ }");
6.29 break;
6.30 case opc_aconst_null:
6.31 emit(out, "@1 = null;", smapper.pushA());
7.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Sun Dec 16 20:11:18 2012 +0100
7.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Mon Dec 17 09:57:22 2012 +0100
7.3 @@ -47,10 +47,10 @@
7.4
7.5 static Object load(Object loader, String name, Object[] arguments)
7.6 throws IOException, ClassNotFoundException {
7.7 - return new VMLazy(loader, arguments).load(name);
7.8 + return new VMLazy(loader, arguments).load(name, false);
7.9 }
7.10
7.11 - private Object load(String name)
7.12 + private Object load(String name, boolean instance)
7.13 throws IOException, ClassNotFoundException {
7.14 String res = name.replace('.', '/') + ".class";
7.15 byte[] arr = read(loader, res, args);
7.16 @@ -64,7 +64,7 @@
7.17 new Gen(this, out).compile(new ByteArrayInputStream(arr));
7.18 String code = out.toString().toString();
7.19 String under = name.replace('.', '_');
7.20 - return applyCode(loader, under, code);
7.21 + return applyCode(loader, under, code, instance);
7.22 }
7.23
7.24 /* possibly not needed:
7.25 @@ -76,15 +76,15 @@
7.26 */
7.27
7.28
7.29 - @JavaScriptBody(args = {"loader", "name", "script" }, body =
7.30 + @JavaScriptBody(args = {"loader", "name", "script", "instance" }, body =
7.31 "try {\n" +
7.32 " new Function(script)(loader, name);\n" +
7.33 "} catch (ex) {\n" +
7.34 " throw 'Cannot compile ' + ex + ' line: ' + ex.lineNumber + ' script:\\n' + script;\n" +
7.35 "}\n" +
7.36 - "return vm[name](false);\n"
7.37 + "return vm[name](instance);\n"
7.38 )
7.39 - private static native Object applyCode(Object loader, String name, String script);
7.40 + private static native Object applyCode(Object loader, String name, String script, boolean instance);
7.41
7.42
7.43 private static final class Gen extends ByteCodeToJavaScript {
7.44 @@ -104,7 +104,8 @@
7.45 + "\nvar vm = loader.vm;"
7.46 + "\nif (vm[cls]) return false;"
7.47 + "\nvm[cls] = function() {"
7.48 - + "\n return lazy.load__Ljava_lang_Object_2Ljava_lang_String_2(lazy, dot);"
7.49 + + "\n var instance = arguments.length == 0 || arguments[0] === true;"
7.50 + + "\n return lazy.load__Ljava_lang_Object_2Ljava_lang_String_2Z(lazy, dot, instance);"
7.51 + "\n};"
7.52 + "\nreturn true;")
7.53 @Override
8.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java Sun Dec 16 20:11:18 2012 +0100
8.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/BytesLoader.java Mon Dec 17 09:57:22 2012 +0100
8.3 @@ -19,6 +19,8 @@
8.4
8.5 import java.io.IOException;
8.6 import java.io.InputStream;
8.7 +import java.net.URL;
8.8 +import java.util.Enumeration;
8.9 import java.util.Set;
8.10 import java.util.TreeSet;
8.11
8.12 @@ -33,15 +35,7 @@
8.13 if (!requested.add(name)) {
8.14 throw new IllegalStateException("Requested for second time: " + name);
8.15 }
8.16 - InputStream is = BytesLoader.class.getClassLoader().getResourceAsStream(name);
8.17 - if (is == null) {
8.18 - throw new IOException("Can't find " + name);
8.19 - }
8.20 - byte[] arr = new byte[is.available()];
8.21 - int len = is.read(arr);
8.22 - if (len != arr.length) {
8.23 - throw new IOException("Read only " + len + " wanting " + arr.length);
8.24 - }
8.25 + byte[] arr = readClass(name);
8.26 /*
8.27 System.err.print("loader['" + name + "'] = [");
8.28 for (int i = 0; i < arr.length; i++) {
8.29 @@ -54,5 +48,29 @@
8.30 */
8.31 return arr;
8.32 }
8.33 +
8.34 + static byte[] readClass(String name) throws IOException {
8.35 + URL u = null;
8.36 + Enumeration<URL> en = BytesLoader.class.getClassLoader().getResources(name);
8.37 + while (en.hasMoreElements()) {
8.38 + u = en.nextElement();
8.39 + }
8.40 + if (u == null) {
8.41 + throw new IOException("Can't find " + name);
8.42 + }
8.43 + try (InputStream is = u.openStream()) {
8.44 + byte[] arr;
8.45 + arr = new byte[is.available()];
8.46 + int offset = 0;
8.47 + while (offset < arr.length) {
8.48 + int len = is.read(arr, offset, arr.length - offset);
8.49 + if (len == -1) {
8.50 + throw new IOException("Can't read " + name);
8.51 + }
8.52 + offset += len;
8.53 + }
8.54 + return arr;
8.55 + }
8.56 + }
8.57
8.58 }
9.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Sun Dec 16 20:11:18 2012 +0100
9.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Mon Dec 17 09:57:22 2012 +0100
9.3 @@ -20,7 +20,6 @@
9.4 import java.io.File;
9.5 import java.io.FileWriter;
9.6 import java.io.IOException;
9.7 -import java.io.InputStream;
9.8 import static org.testng.Assert.*;
9.9 import javax.script.Invocable;
9.10 import org.testng.annotations.BeforeClass;
9.11 @@ -36,13 +35,13 @@
9.12 private static Invocable code;
9.13
9.14 @Test public void compareGeneratedCodeForArrayClass() throws Exception {
9.15 - compareCode("/org/apidesign/vm4brwsr/Array.class");
9.16 + compareCode("org/apidesign/vm4brwsr/Array.class");
9.17 }
9.18
9.19 @Test public void compareGeneratedCodeForClassesClass() throws Exception {
9.20 - compareCode("/org/apidesign/vm4brwsr/Classes.class");
9.21 + compareCode("org/apidesign/vm4brwsr/Classes.class");
9.22 }
9.23 -
9.24 +
9.25 @BeforeClass
9.26 public void compileTheCode() throws Exception {
9.27 StringBuilder sb = new StringBuilder();
9.28 @@ -52,20 +51,8 @@
9.29 codeSeq = sb;
9.30 }
9.31
9.32 - private static byte[] readClass(String res) throws IOException {
9.33 - InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
9.34 - assertNotNull(is1, "Stream found");
9.35 - byte[] arr = new byte[is1.available()];
9.36 - int len = is1.read(arr);
9.37 - is1.close();
9.38 - if (len != arr.length) {
9.39 - throw new IOException("Wrong len " + len + " for arr: " + arr.length);
9.40 - }
9.41 - return arr;
9.42 - }
9.43 -
9.44 private void compareCode(final String nm) throws Exception, IOException {
9.45 - byte[] arr = readClass(nm);
9.46 + byte[] arr = BytesLoader.readClass(nm);
9.47 String ret1 = VMinVM.toJavaScript(arr);
9.48
9.49 Object ret;
10.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareHashTest.java Sun Dec 16 20:11:18 2012 +0100
10.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareHashTest.java Mon Dec 17 09:57:22 2012 +0100
10.3 @@ -30,6 +30,11 @@
10.4 return "Ahoj".hashCode();
10.5 }
10.6
10.7 + @Compare public int hashRemainsYieldsZero() {
10.8 + Object o = new Object();
10.9 + return o.hashCode() - o.hashCode();
10.10 + }
10.11 +
10.12 @Factory
10.13 public static Object[] create() {
10.14 return CompareVMs.create(CompareHashTest.class);
11.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java Sun Dec 16 20:11:18 2012 +0100
11.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java Mon Dec 17 09:57:22 2012 +0100
11.3 @@ -17,7 +17,8 @@
11.4 */
11.5 package org.apidesign.vm4brwsr.tck;
11.6
11.7 -import java.lang.reflect.Method;
11.8 +import java.net.MalformedURLException;
11.9 +import java.net.URL;
11.10 import org.apidesign.vm4brwsr.Compare;
11.11 import org.apidesign.vm4brwsr.CompareVMs;
11.12 import org.testng.annotations.Factory;
11.13 @@ -27,6 +28,10 @@
11.14 * @author Jaroslav Tulach <jtulach@netbeans.org>
11.15 */
11.16 public class CompareStringsTest {
11.17 + @Compare public static Object compareURLs() throws MalformedURLException {
11.18 + return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
11.19 + }
11.20 +
11.21 @Compare public String deleteLastTwoCharacters() {
11.22 StringBuilder sb = new StringBuilder();
11.23 sb.append("453.0");
11.24 @@ -43,9 +48,63 @@
11.25 @Compare public String nameOfArrayClass() throws Exception {
11.26 return Class.forName("org.apidesign.vm4brwsr.Array").getName();
11.27 }
11.28 +
11.29 + @Compare public String lowerHello() {
11.30 + return "HeLlO".toLowerCase();
11.31 + }
11.32 +
11.33 + @Compare public String lowerA() {
11.34 + return String.valueOf(Character.toLowerCase('A')).toString();
11.35 + }
11.36 + @Compare public String upperHello() {
11.37 + return "hello".toUpperCase();
11.38 + }
11.39 +
11.40 + @Compare public String upperA() {
11.41 + return String.valueOf(Character.toUpperCase('a')).toString();
11.42 + }
11.43 +
11.44 + @Compare public boolean matchRegExp() throws Exception {
11.45 + return "58038503".matches("\\d*");
11.46 + }
11.47 +
11.48 + @Compare public boolean doesNotMatchRegExp() throws Exception {
11.49 + return "58038503GH".matches("\\d*");
11.50 + }
11.51 +
11.52 + @Compare public boolean doesNotMatchRegExpFully() throws Exception {
11.53 + return "Hello".matches("Hell");
11.54 + }
11.55 +
11.56 + @Compare public String variousCharacterTests() throws Exception {
11.57 + StringBuilder sb = new StringBuilder();
11.58
11.59 + sb.append(Character.isUpperCase('a'));
11.60 + sb.append(Character.isUpperCase('A'));
11.61 + sb.append(Character.isLowerCase('a'));
11.62 + sb.append(Character.isLowerCase('A'));
11.63 +
11.64 + sb.append(Character.isLetter('A'));
11.65 + sb.append(Character.isLetterOrDigit('9'));
11.66 + sb.append(Character.isLetterOrDigit('A'));
11.67 + sb.append(Character.isLetter('0'));
11.68 +
11.69 + return sb.toString().toString();
11.70 + }
11.71 +
11.72 + @Compare
11.73 + public String nullFieldInitialized() {
11.74 + NullField nf = new NullField();
11.75 + return ("" + nf.name).toString();
11.76 + }
11.77 +
11.78 @Factory
11.79 public static Object[] create() {
11.80 return CompareVMs.create(CompareStringsTest.class);
11.81 }
11.82 +
11.83 + private static final class NullField {
11.84 +
11.85 + String name;
11.86 + }
11.87 }