Separating compact profile tests into own project, so launcher.http can depend on compact profile JAR
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.tck;
20 import java.io.ByteArrayInputStream;
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.io.InputStream;
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;
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
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;
44 while (en.hasMoreElements()) {
45 URL url = en.nextElement();
46 String now = readString(url.openStream());
47 if (!first.equals(now)) {
52 throw new IllegalStateException(
53 "Giving up. First manifest:\n" + first +
54 "\nLast manifest:\n" + now
58 assert different : "Not all manifests should look like first one:\n" + first;
59 if (cnt > 40 && cnt < 50) {
66 @Compare public String readResourceAsStream() throws Exception {
67 InputStream is = getClass().getResourceAsStream("Resources.txt");
68 return readString(is);
71 @Compare public String readResourceViaXMLHttpRequest() throws Exception {
72 return readResourceViaXHR("Resources.txt", null);
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";
81 @Compare public String readBinaryResourceViaXMLHttpRequest() throws Exception {
82 return readResourceViaXHR("0xfe", null);
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;
92 URL url2 = conn.getURL();
93 String java3 = readBytes(url.openStream());
94 assert java.equals(java3) : "Java:\n" + java + "\nConnURL:\n" + java3;
97 byte[] xhr = readXHR(url2.toExternalForm());
102 String s = readBytes(new ByteArrayInputStream(xhr));
103 assert java.equals(s) : "Java:\n" + java + "\nXHR:\n" + s;
105 assert conn instanceof Closeable : "Can be closed";
107 Closeable c = (Closeable) conn;
112 xhr2 = readXHR(url2.toExternalForm());
113 } catch (Throwable t) {
114 // OK, expecting error
116 assert xhr2 == null : "Cannot read the URL anymore";
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" +
127 "if (xhr.status >= 300) throw 'Status: ' + xhr.status + ' ' + xhr.statusText;\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" +
136 private static byte[] readXHR(String url) {
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);
149 private String readString(InputStream is) throws IOException {
150 StringBuilder sb = new StringBuilder();
151 byte[] b = new byte[512];
153 int len = is.read(b);
155 return sb.toString();
157 for (int i = 0; i < len; i++) {
158 sb.append((char)b[i]);
163 private String readBytes(InputStream is) throws IOException {
164 StringBuilder sb = new StringBuilder();
165 byte[] b = new byte[512];
167 int len = is.read(b);
169 return sb.toString();
171 for (int i = 0; i < len; i++) {
172 sb.append((int)b[i]).append(" ");
177 @Compare public String readResourceAsStreamFromClassLoader() throws Exception {
178 InputStream is = getClass().getClassLoader().getResourceAsStream("org/apidesign/bck2brwsr/tck/Resources.txt");
179 return readString(is);
182 @Compare public String toURIFromURL() throws Exception {
183 URL u = new URL("http://apidesign.org");
184 return u.toURI().toString();
187 @Factory public static Object[] create() {
188 return VMTest.create(ResourcesTest.class);