making sure all classes check the same object for desiredAssertionStatus. Allowing the status to be set per class.
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.launcher.impl;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.UnsupportedEncodingException;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Modifier;
27 import java.util.Enumeration;
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 @org.apidesign.bck2brwsr.core.Exported
35 public class Console {
39 turnAssetionStatusOn();
42 @JavaScriptBody(args = {"id", "attr"}, body =
43 "return window.document.getElementById(id)[attr].toString();")
44 private static native Object getAttr(String id, String attr);
45 @JavaScriptBody(args = {"elem", "attr"}, body =
46 "return elem[attr].toString();")
47 private static native Object getAttr(Object elem, String attr);
49 @JavaScriptBody(args = {"id", "attr", "value"}, body =
50 "window.document.getElementById(id)[attr] = value;")
51 private static native void setAttr(String id, String attr, Object value);
52 @JavaScriptBody(args = {"elem", "attr", "value"}, body =
53 "elem[attr] = value;")
54 private static native void setAttr(Object id, String attr, Object value);
56 @JavaScriptBody(args = {}, body = "return; window.close();")
57 private static native void closeWindow();
59 private static Object textArea;
60 private static Object statusArea;
62 private static void log(String newText) {
63 if (textArea == null) {
66 String attr = "value";
67 setAttr(textArea, attr, getAttr(textArea, attr) + "\n" + newText);
68 setAttr(textArea, "scrollTop", getAttr(textArea, "scrollHeight"));
71 private static void beginTest(Case c) {
72 Object[] arr = new Object[2];
73 beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
78 private static void finishTest(Case c, Object res) {
79 if ("null".equals(res)) {
80 setAttr(statusArea, "innerHTML", "Success");
82 setAttr(statusArea, "innerHTML", "Result " + res);
88 @JavaScriptBody(args = { "test", "c", "arr" }, body =
89 "var ul = window.document.getElementById('bck2brwsr.result');\n"
90 + "var li = window.document.createElement('li');\n"
91 + "var span = window.document.createElement('span');"
92 + "span.innerHTML = test + ' - ';\n"
93 + "var details = window.document.createElement('a');\n"
94 + "details.innerHTML = 'Details';\n"
95 + "details.href = '#';\n"
96 + "var p = window.document.createElement('p');\n"
97 + "var status = window.document.createElement('a');\n"
98 + "status.innerHTML = 'running';"
99 + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
100 + "status.onclick = function() { c.again__V_3Ljava_lang_Object_2(arr); }\n"
101 + "var pre = window.document.createElement('textarea');\n"
104 + "li.appendChild(span);\n"
105 + "li.appendChild(status);\n"
106 + "var span = window.document.createElement('span');"
107 + "span.innerHTML = ' ';\n"
108 + "li.appendChild(span);\n"
109 + "li.appendChild(details);\n"
110 + "p.appendChild(pre);\n"
111 + "ul.appendChild(li);\n"
113 + "arr[1] = status;\n"
115 private static native void beginTest(String test, Case c, Object[] arr);
117 @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
118 + "var request = new XMLHttpRequest();\n"
119 + "request.open('GET', url, true);\n"
120 + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
121 + "request.onreadystatechange = function() {\n"
122 + " if (this.readyState!==4) return;\n"
123 + " arr[0] = this.responseText;\n"
124 + " callback.run__V();\n"
128 private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
130 public static void harness(String url) throws IOException {
131 log("Connecting to " + url);
132 Request r = new Request(url);
135 private static class Request implements Runnable {
136 private final String[] arr = { null };
137 private final String url;
141 private Request(String url) throws IOException {
143 loadText(url, this, arr);
145 private Request(String url, String u) throws IOException {
147 loadText(u, this, arr);
154 String data = arr[0];
157 log("Some error exiting");
162 if (data.isEmpty()) {
163 log("No data, exiting");
168 c = Case.parseData(data);
170 log("Got \"" + data + "\"");
172 log("Processing \"" + arr[0] + "\" for " + retries + " time");
174 Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
175 finishTest(c, result);
177 String u = url + "?request=" + c.getRequestId() + "&result=" + result;
179 } catch (Exception ex) {
180 if (ex instanceof InterruptedException) {
181 log("Re-scheduling in 100ms");
185 log(ex.getClass().getName() + ":" + ex.getMessage());
190 private static String encodeURL(String r) throws UnsupportedEncodingException {
191 final String SPECIAL = "%$&+,/:;=?@";
192 StringBuilder sb = new StringBuilder();
193 byte[] utf8 = r.getBytes("UTF-8");
194 for (int i = 0; i < utf8.length; i++) {
195 int ch = utf8[i] & 0xff;
196 if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
197 final String numbers = "0" + Integer.toHexString(ch);
198 sb.append("%").append(numbers.substring(numbers.length() - 2));
207 return sb.toString();
210 static String invoke(String clazz, String method) throws
211 ClassNotFoundException, InvocationTargetException, IllegalAccessException,
212 InstantiationException, InterruptedException {
213 final Object r = new Case(null).invokeMethod(clazz, method);
214 return r == null ? "null" : r.toString().toString();
217 /** Helper method that inspects the classpath and loads given resource
218 * (usually a class file). Used while running tests in Rhino.
220 * @param name resource name to find
221 * @return the array of bytes in the given resource
222 * @throws IOException I/O in case something goes wrong
224 public static byte[] read(String name, int skip) throws IOException {
226 if (!name.endsWith(".class")) {
227 u = getResource(name, skip);
229 Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
230 while (en.hasMoreElements()) {
231 u = en.nextElement();
235 if (name.endsWith(".class")) {
236 throw new IOException("Can't find " + name);
241 try (InputStream is = u.openStream()) {
243 arr = new byte[is.available()];
245 while (offset < arr.length) {
246 int len = is.read(arr, offset, arr.length - offset);
248 throw new IOException("Can't read " + name);
256 private static URL getResource(String resource, int skip) throws IOException {
258 Enumeration<URL> en = Console.class.getClassLoader().getResources(resource);
259 while (en.hasMoreElements()) {
260 final URL now = en.nextElement();
269 @JavaScriptBody(args = {}, body = "vm.java_lang_Class(false).desiredAssertionStatus = true;")
270 private static void turnAssetionStatusOn() {
273 @JavaScriptBody(args = {"r", "time"}, body =
274 "return window.setTimeout(function() { r.run__V(); }, time);")
275 private static native Object schedule(Runnable r, int time);
277 private static final class Case {
278 private final Object data;
281 private Case(Object data) {
285 public static Case parseData(String s) {
286 return new Case(toJSON(s));
289 public String getMethodName() {
290 return value("methodName", data);
293 public String getClassName() {
294 return value("className", data);
297 public String getRequestId() {
298 return value("request", data);
301 public String getHtmlFragment() {
302 return value("html", data);
305 void again(Object[] arr) {
309 setAttr(textArea, "value", "");
311 } catch (Exception ex) {
312 log(ex.getClass().getName() + ":" + ex.getMessage());
316 private Object runTest() throws IllegalAccessException,
317 IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException,
318 InvocationTargetException, InstantiationException, InterruptedException {
319 if (this.getHtmlFragment() != null) {
320 setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
322 log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId());
323 Object result = invokeMethod(this.getClassName(), this.getMethodName());
324 setAttr("bck2brwsr.fragment", "innerHTML", "");
325 log("Result: " + result);
326 result = encodeURL("" + result);
327 log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
331 private Object invokeMethod(String clazz, String method)
332 throws ClassNotFoundException, InvocationTargetException,
333 InterruptedException, IllegalAccessException, IllegalArgumentException,
334 InstantiationException {
336 Class<?> c = Class.forName(clazz);
337 for (Method m : c.getMethods()) {
338 if (m.getName().equals(method)) {
345 if ((found.getModifiers() & Modifier.STATIC) != 0) {
346 res = found.invoke(null);
349 inst = c.newInstance();
351 res = found.invoke(inst);
353 } catch (Throwable ex) {
354 if (ex instanceof InvocationTargetException) {
355 ex = ((InvocationTargetException) ex).getTargetException();
357 if (ex instanceof InterruptedException) {
358 throw (InterruptedException)ex;
360 res = ex.getClass().getName() + ":" + ex.getMessage();
363 res = "Can't find method " + method + " in " + clazz;
368 @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
369 private static native Object toJSON(String s);
371 @JavaScriptBody(args = {"p", "d"}, body =
373 + "if (typeof v === 'undefined') return null;\n"
374 + "return v.toString();"
376 private static native String value(String p, Object d);