rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 01:50:21 +0100
changeset 1717 f5200d90b730
parent 1600 824cb8b9c4f9
child 1718 ff14ad07bc16
permissions -rw-r--r--
getClass().getResource(...).openConnection().getURL() returns a URL which is recognized by the browser and can be used to load resources with XHR or <img src='...'>. When no longer needed, convert connection to Closeable and use connection.close().
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.Closeable;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.net.URL;
    25 import java.net.URLConnection;
    26 import java.util.Enumeration;
    27 import net.java.html.js.JavaScriptBody;
    28 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    29 import org.apidesign.bck2brwsr.vmtest.Compare;
    30 import org.apidesign.bck2brwsr.vmtest.VMTest;
    31 import org.testng.annotations.Factory;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public class ResourcesTest {
    38     @Compare public String allManifests() throws Exception {
    39         Enumeration<URL> en = ClassLoader.getSystemResources("META-INF/MANIFEST.MF");
    40         assert en.hasMoreElements() : "Should have at least one manifest";
    41         String first = readString(en.nextElement().openStream());
    42         boolean different = false;
    43         int cnt = 1;
    44         while (en.hasMoreElements()) {
    45             URL url = en.nextElement();
    46             String now = readString(url.openStream());
    47             if (!first.equals(now)) {
    48                 different = true;
    49             }
    50             cnt++;
    51             if (cnt > 500) {
    52                 throw new IllegalStateException(
    53                     "Giving up. First manifest:\n" + first + 
    54                     "\nLast manifest:\n" + now
    55                 );
    56             }
    57         }
    58         assert different : "Not all manifests should look like first one:\n" + first;
    59         if (cnt > 40 && cnt < 50) {
    60             return "OK";
    61         } else {
    62             return "" + cnt;
    63         }
    64     }
    65     
    66     @Compare public String readResourceAsStream() throws Exception {
    67         InputStream is = getClass().getResourceAsStream("Resources.txt");
    68         return readString(is);
    69     }
    70     
    71     @Compare public String readResourceViaXMLHttpRequest() throws Exception {
    72         return readResourceViaXHR("Resources.txt", null);
    73     }
    74     
    75     @BrwsrTest public void xhrTestedInBrowser() throws Exception {
    76         boolean[] run = { false };
    77         readResourceViaXHR("Resources.txt", run);
    78         assert run[0] : "XHR really used in browser";
    79     }
    80 
    81     @Compare public String readBinaryResourceViaXMLHttpRequest() throws Exception {
    82         return readResourceViaXHR("0xfe", null);
    83     }
    84 
    85     private String readResourceViaXHR(final String res, boolean[] exec) throws IOException {
    86         URL url = getClass().getResource(res);
    87         URLConnection conn = url.openConnection();
    88         String java = readBytes(url.openStream());
    89         String java2 = readBytes(conn.getInputStream());
    90         assert java.equals(java2) : "Java:\n" + java + "\nConn:\n" + java2;
    91         
    92         URL url2 = conn.getURL();
    93         String java3 = readBytes(url.openStream());
    94         assert java.equals(java3) : "Java:\n" + java + "\nConnURL:\n" + java3;
    95         
    96         
    97         byte[] xhr = readXHR(url2.toExternalForm());
    98         if (xhr != null) {
    99             if (exec != null) {
   100                 exec[0] = true;
   101             }
   102             String s = readBytes(new ByteArrayInputStream(xhr));
   103             assert java.equals(s) : "Java:\n" + java + "\nXHR:\n" + s;
   104             
   105             assert conn instanceof Closeable : "Can be closed";
   106             
   107             Closeable c = (Closeable) conn;
   108             c.close();
   109             
   110             byte[] xhr2 = null;
   111             try {
   112                 xhr2 = readXHR(url2.toExternalForm());
   113             } catch (Throwable t) {
   114                 // OK, expecting error
   115             }
   116             assert xhr2 == null : "Cannot read the URL anymore";
   117         }
   118         return java;
   119     }
   120 
   121     @JavaScriptBody(args = { "url" }, body =
   122         "if (typeof XMLHttpRequest === 'undefined') return null;\n" +
   123         "var xhr = new XMLHttpRequest();\n" +
   124         "xhr.overrideMimeType('text\\/plain; charset=x-user-defined');\n" +
   125         "xhr.open('GET', url, false);\n" +
   126         "xhr.send();\n" +
   127         "var ret = []\n" +
   128         "for (var i = 0; i < xhr.responseText.length; i++) {\n" +
   129         "  ret.push(xhr.responseText.charCodeAt(i) & 0xff);\n" +
   130         "}\n" +
   131         "return ret;\n"
   132     )
   133     private static byte[] readXHR(String url) {
   134         return null;
   135     }
   136     
   137     @Compare public String readResourceViaConnection() throws Exception {
   138         final URL url = getClass().getResource("Resources.txt");
   139         String str = url.toExternalForm();
   140         int idx = str.indexOf("org/apidesign/bck2brwsr/tck");
   141         assert idx >= 0 : "Package name found in the URL name: " + str;
   142         InputStream is = url.openConnection().getInputStream();
   143         return readString(is);
   144     }
   145 
   146     private String readString(InputStream is) throws IOException {
   147         StringBuilder sb = new StringBuilder();
   148         byte[] b = new byte[512];
   149         for (;;) { 
   150             int len = is.read(b);
   151             if (len == -1) {
   152                 return sb.toString();
   153             }
   154             for (int i = 0; i < len; i++) {
   155                 sb.append((char)b[i]);
   156             }
   157         }
   158     }
   159 
   160     private String readBytes(InputStream is) throws IOException {
   161         StringBuilder sb = new StringBuilder();
   162         byte[] b = new byte[512];
   163         for (;;) { 
   164             int len = is.read(b);
   165             if (len == -1) {
   166                 return sb.toString();
   167             }
   168             for (int i = 0; i < len; i++) {
   169                 sb.append((int)b[i]).append(" ");
   170             }
   171         }
   172     }
   173 
   174     @Compare public String readResourceAsStreamFromClassLoader() throws Exception {
   175         InputStream is = getClass().getClassLoader().getResourceAsStream("org/apidesign/bck2brwsr/tck/Resources.txt");
   176         return readString(is);
   177     }
   178     
   179     @Compare public String toURIFromURL() throws Exception {
   180         URL u = new URL("http://apidesign.org");
   181         return u.toURI().toString();
   182     }
   183     
   184     @Factory public static Object[] create() {
   185         return VMTest.create(ResourcesTest.class);
   186     }
   187 }