rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 17:21:08 +0100
changeset 1719 d83b02a4b7a8
parent 1718 ff14ad07bc16
child 1721 15f7116b57b6
permissions -rw-r--r--
chromium just returns 404 in case the URL has been revoked. Throw exceptions by ownself.
     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         "if (xhr.status !== 200) throw 'Status: ' + xhr.status + ' ' + xhr.statusText;\n" +
   128         "var ret = []\n" +
   129         "for (var i = 0; i < xhr.responseText.length; i++) {\n" +
   130         "  var b = xhr.responseText.charCodeAt(i) & 0xff;\n" +
   131         "  if (b > 127) b -= 256;\n" +
   132         "  ret.push(b);\n" +
   133         "}\n" +
   134         "return ret;\n"
   135     )
   136     private static byte[] readXHR(String url) {
   137         return null;
   138     }
   139     
   140     @Compare public String readResourceViaConnection() throws Exception {
   141         final URL url = getClass().getResource("Resources.txt");
   142         String str = url.toExternalForm();
   143         int idx = str.indexOf("org/apidesign/bck2brwsr/tck");
   144         assert idx >= 0 : "Package name found in the URL name: " + str;
   145         InputStream is = url.openConnection().getInputStream();
   146         return readString(is);
   147     }
   148 
   149     private String readString(InputStream is) throws IOException {
   150         StringBuilder sb = new StringBuilder();
   151         byte[] b = new byte[512];
   152         for (;;) { 
   153             int len = is.read(b);
   154             if (len == -1) {
   155                 return sb.toString();
   156             }
   157             for (int i = 0; i < len; i++) {
   158                 sb.append((char)b[i]);
   159             }
   160         }
   161     }
   162 
   163     private String readBytes(InputStream is) throws IOException {
   164         StringBuilder sb = new StringBuilder();
   165         byte[] b = new byte[512];
   166         for (;;) { 
   167             int len = is.read(b);
   168             if (len == -1) {
   169                 return sb.toString();
   170             }
   171             for (int i = 0; i < len; i++) {
   172                 sb.append((int)b[i]).append(" ");
   173             }
   174         }
   175     }
   176 
   177     @Compare public String readResourceAsStreamFromClassLoader() throws Exception {
   178         InputStream is = getClass().getClassLoader().getResourceAsStream("org/apidesign/bck2brwsr/tck/Resources.txt");
   179         return readString(is);
   180     }
   181     
   182     @Compare public String toURIFromURL() throws Exception {
   183         URL u = new URL("http://apidesign.org");
   184         return u.toURI().toString();
   185     }
   186     
   187     @Factory public static Object[] create() {
   188         return VMTest.create(ResourcesTest.class);
   189     }
   190 }