1.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Mon May 26 18:21:24 2014 +0200
1.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Thu Oct 30 01:50:21 2014 +0100
1.3 @@ -17,10 +17,15 @@
1.4 */
1.5 package org.apidesign.bck2brwsr.tck;
1.6
1.7 +import java.io.ByteArrayInputStream;
1.8 +import java.io.Closeable;
1.9 import java.io.IOException;
1.10 import java.io.InputStream;
1.11 import java.net.URL;
1.12 +import java.net.URLConnection;
1.13 import java.util.Enumeration;
1.14 +import net.java.html.js.JavaScriptBody;
1.15 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
1.16 import org.apidesign.bck2brwsr.vmtest.Compare;
1.17 import org.apidesign.bck2brwsr.vmtest.VMTest;
1.18 import org.testng.annotations.Factory;
1.19 @@ -63,8 +68,78 @@
1.20 return readString(is);
1.21 }
1.22
1.23 + @Compare public String readResourceViaXMLHttpRequest() throws Exception {
1.24 + return readResourceViaXHR("Resources.txt", null);
1.25 + }
1.26 +
1.27 + @BrwsrTest public void xhrTestedInBrowser() throws Exception {
1.28 + boolean[] run = { false };
1.29 + readResourceViaXHR("Resources.txt", run);
1.30 + assert run[0] : "XHR really used in browser";
1.31 + }
1.32 +
1.33 + @Compare public String readBinaryResourceViaXMLHttpRequest() throws Exception {
1.34 + return readResourceViaXHR("0xfe", null);
1.35 + }
1.36 +
1.37 + private String readResourceViaXHR(final String res, boolean[] exec) throws IOException {
1.38 + URL url = getClass().getResource(res);
1.39 + URLConnection conn = url.openConnection();
1.40 + String java = readBytes(url.openStream());
1.41 + String java2 = readBytes(conn.getInputStream());
1.42 + assert java.equals(java2) : "Java:\n" + java + "\nConn:\n" + java2;
1.43 +
1.44 + URL url2 = conn.getURL();
1.45 + String java3 = readBytes(url.openStream());
1.46 + assert java.equals(java3) : "Java:\n" + java + "\nConnURL:\n" + java3;
1.47 +
1.48 +
1.49 + byte[] xhr = readXHR(url2.toExternalForm());
1.50 + if (xhr != null) {
1.51 + if (exec != null) {
1.52 + exec[0] = true;
1.53 + }
1.54 + String s = readBytes(new ByteArrayInputStream(xhr));
1.55 + assert java.equals(s) : "Java:\n" + java + "\nXHR:\n" + s;
1.56 +
1.57 + assert conn instanceof Closeable : "Can be closed";
1.58 +
1.59 + Closeable c = (Closeable) conn;
1.60 + c.close();
1.61 +
1.62 + byte[] xhr2 = null;
1.63 + try {
1.64 + xhr2 = readXHR(url2.toExternalForm());
1.65 + } catch (Throwable t) {
1.66 + // OK, expecting error
1.67 + }
1.68 + assert xhr2 == null : "Cannot read the URL anymore";
1.69 + }
1.70 + return java;
1.71 + }
1.72 +
1.73 + @JavaScriptBody(args = { "url" }, body =
1.74 + "if (typeof XMLHttpRequest === 'undefined') return null;\n" +
1.75 + "var xhr = new XMLHttpRequest();\n" +
1.76 + "xhr.overrideMimeType('text\\/plain; charset=x-user-defined');\n" +
1.77 + "xhr.open('GET', url, false);\n" +
1.78 + "xhr.send();\n" +
1.79 + "var ret = []\n" +
1.80 + "for (var i = 0; i < xhr.responseText.length; i++) {\n" +
1.81 + " ret.push(xhr.responseText.charCodeAt(i) & 0xff);\n" +
1.82 + "}\n" +
1.83 + "return ret;\n"
1.84 + )
1.85 + private static byte[] readXHR(String url) {
1.86 + return null;
1.87 + }
1.88 +
1.89 @Compare public String readResourceViaConnection() throws Exception {
1.90 - InputStream is = getClass().getResource("Resources.txt").openConnection().getInputStream();
1.91 + final URL url = getClass().getResource("Resources.txt");
1.92 + String str = url.toExternalForm();
1.93 + int idx = str.indexOf("org/apidesign/bck2brwsr/tck");
1.94 + assert idx >= 0 : "Package name found in the URL name: " + str;
1.95 + InputStream is = url.openConnection().getInputStream();
1.96 return readString(is);
1.97 }
1.98
1.99 @@ -82,6 +157,20 @@
1.100 }
1.101 }
1.102
1.103 + private String readBytes(InputStream is) throws IOException {
1.104 + StringBuilder sb = new StringBuilder();
1.105 + byte[] b = new byte[512];
1.106 + for (;;) {
1.107 + int len = is.read(b);
1.108 + if (len == -1) {
1.109 + return sb.toString();
1.110 + }
1.111 + for (int i = 0; i < len; i++) {
1.112 + sb.append((int)b[i]).append(" ");
1.113 + }
1.114 + }
1.115 + }
1.116 +
1.117 @Compare public String readResourceAsStreamFromClassLoader() throws Exception {
1.118 InputStream is = getClass().getClassLoader().getResourceAsStream("org/apidesign/bck2brwsr/tck/Resources.txt");
1.119 return readString(is);