1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Wed Nov 19 19:32:00 2014 +0100
1.3 @@ -0,0 +1,190 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.tck;
1.22 +
1.23 +import java.io.ByteArrayInputStream;
1.24 +import java.io.Closeable;
1.25 +import java.io.IOException;
1.26 +import java.io.InputStream;
1.27 +import java.net.URL;
1.28 +import java.net.URLConnection;
1.29 +import java.util.Enumeration;
1.30 +import net.java.html.js.JavaScriptBody;
1.31 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
1.32 +import org.apidesign.bck2brwsr.vmtest.Compare;
1.33 +import org.apidesign.bck2brwsr.vmtest.VMTest;
1.34 +import org.testng.annotations.Factory;
1.35 +
1.36 +/**
1.37 + *
1.38 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.39 + */
1.40 +public class ResourcesTest {
1.41 + @Compare public String allManifests() throws Exception {
1.42 + Enumeration<URL> en = ClassLoader.getSystemResources("META-INF/MANIFEST.MF");
1.43 + assert en.hasMoreElements() : "Should have at least one manifest";
1.44 + String first = readString(en.nextElement().openStream());
1.45 + boolean different = false;
1.46 + int cnt = 1;
1.47 + while (en.hasMoreElements()) {
1.48 + URL url = en.nextElement();
1.49 + String now = readString(url.openStream());
1.50 + if (!first.equals(now)) {
1.51 + different = true;
1.52 + }
1.53 + cnt++;
1.54 + if (cnt > 500) {
1.55 + throw new IllegalStateException(
1.56 + "Giving up. First manifest:\n" + first +
1.57 + "\nLast manifest:\n" + now
1.58 + );
1.59 + }
1.60 + }
1.61 + assert different : "Not all manifests should look like first one:\n" + first;
1.62 + if (cnt > 40 && cnt < 50) {
1.63 + return "OK";
1.64 + } else {
1.65 + return "" + cnt;
1.66 + }
1.67 + }
1.68 +
1.69 + @Compare public String readResourceAsStream() throws Exception {
1.70 + InputStream is = getClass().getResourceAsStream("Resources.txt");
1.71 + return readString(is);
1.72 + }
1.73 +
1.74 + @Compare public String readResourceViaXMLHttpRequest() throws Exception {
1.75 + return readResourceViaXHR("Resources.txt", null);
1.76 + }
1.77 +
1.78 + @BrwsrTest public void xhrTestedInBrowser() throws Exception {
1.79 + boolean[] run = { false };
1.80 + readResourceViaXHR("Resources.txt", run);
1.81 + assert run[0] : "XHR really used in browser";
1.82 + }
1.83 +
1.84 + @Compare public String readBinaryResourceViaXMLHttpRequest() throws Exception {
1.85 + return readResourceViaXHR("0xfe", null);
1.86 + }
1.87 +
1.88 + private String readResourceViaXHR(final String res, boolean[] exec) throws IOException {
1.89 + URL url = getClass().getResource(res);
1.90 + URLConnection conn = url.openConnection();
1.91 + String java = readBytes(url.openStream());
1.92 + String java2 = readBytes(conn.getInputStream());
1.93 + assert java.equals(java2) : "Java:\n" + java + "\nConn:\n" + java2;
1.94 +
1.95 + URL url2 = conn.getURL();
1.96 + String java3 = readBytes(url.openStream());
1.97 + assert java.equals(java3) : "Java:\n" + java + "\nConnURL:\n" + java3;
1.98 +
1.99 +
1.100 + byte[] xhr = readXHR(url2.toExternalForm());
1.101 + if (xhr != null) {
1.102 + if (exec != null) {
1.103 + exec[0] = true;
1.104 + }
1.105 + String s = readBytes(new ByteArrayInputStream(xhr));
1.106 + assert java.equals(s) : "Java:\n" + java + "\nXHR:\n" + s;
1.107 +
1.108 + assert conn instanceof Closeable : "Can be closed";
1.109 +
1.110 + Closeable c = (Closeable) conn;
1.111 + c.close();
1.112 +
1.113 + byte[] xhr2 = null;
1.114 + try {
1.115 + xhr2 = readXHR(url2.toExternalForm());
1.116 + } catch (Throwable t) {
1.117 + // OK, expecting error
1.118 + }
1.119 + assert xhr2 == null : "Cannot read the URL anymore";
1.120 + }
1.121 + return java;
1.122 + }
1.123 +
1.124 + @JavaScriptBody(args = { "url" }, body =
1.125 + "if (typeof XMLHttpRequest === 'undefined') return null;\n" +
1.126 + "var xhr = new XMLHttpRequest();\n" +
1.127 + "xhr.overrideMimeType('text\\/plain; charset=x-user-defined');\n" +
1.128 + "xhr.open('GET', url, false);\n" +
1.129 + "xhr.send();\n" +
1.130 + "if (xhr.status >= 300) throw 'Status: ' + xhr.status + ' ' + xhr.statusText;\n" +
1.131 + "var ret = []\n" +
1.132 + "for (var i = 0; i < xhr.responseText.length; i++) {\n" +
1.133 + " var b = xhr.responseText.charCodeAt(i) & 0xff;\n" +
1.134 + " if (b > 127) b -= 256;\n" +
1.135 + " ret.push(b);\n" +
1.136 + "}\n" +
1.137 + "return ret;\n"
1.138 + )
1.139 + private static byte[] readXHR(String url) {
1.140 + return null;
1.141 + }
1.142 +
1.143 + @Compare public String readResourceViaConnection() throws Exception {
1.144 + final URL url = getClass().getResource("Resources.txt");
1.145 + String str = url.toExternalForm();
1.146 + int idx = str.indexOf("org/apidesign/bck2brwsr/tck");
1.147 + assert idx >= 0 : "Package name found in the URL name: " + str;
1.148 + InputStream is = url.openConnection().getInputStream();
1.149 + return readString(is);
1.150 + }
1.151 +
1.152 + private String readString(InputStream is) throws IOException {
1.153 + StringBuilder sb = new StringBuilder();
1.154 + byte[] b = new byte[512];
1.155 + for (;;) {
1.156 + int len = is.read(b);
1.157 + if (len == -1) {
1.158 + return sb.toString();
1.159 + }
1.160 + for (int i = 0; i < len; i++) {
1.161 + sb.append((char)b[i]);
1.162 + }
1.163 + }
1.164 + }
1.165 +
1.166 + private String readBytes(InputStream is) throws IOException {
1.167 + StringBuilder sb = new StringBuilder();
1.168 + byte[] b = new byte[512];
1.169 + for (;;) {
1.170 + int len = is.read(b);
1.171 + if (len == -1) {
1.172 + return sb.toString();
1.173 + }
1.174 + for (int i = 0; i < len; i++) {
1.175 + sb.append((int)b[i]).append(" ");
1.176 + }
1.177 + }
1.178 + }
1.179 +
1.180 + @Compare public String readResourceAsStreamFromClassLoader() throws Exception {
1.181 + InputStream is = getClass().getClassLoader().getResourceAsStream("org/apidesign/bck2brwsr/tck/Resources.txt");
1.182 + return readString(is);
1.183 + }
1.184 +
1.185 + @Compare public String toURIFromURL() throws Exception {
1.186 + URL u = new URL("http://apidesign.org");
1.187 + return u.toURI().toString();
1.188 + }
1.189 +
1.190 + @Factory public static Object[] create() {
1.191 + return VMTest.create(ResourcesTest.class);
1.192 + }
1.193 +}