Using profiles. Twitter demo can execute and test properly in FX profile now
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.fximpl;
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 javafx.scene.web.WebEngine;
29 import netscape.javascript.JSObject;
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 public final class Console {
39 private static Object getAttr(Object elem, String attr) {
40 return InvokeJS.CObject.call("getAttr", elem, attr);
43 private static void setAttr(String id, String attr, Object value) {
44 InvokeJS.CObject.call("setAttrId", id, attr, value);
46 private static void setAttr(Object id, String attr, Object value) {
47 InvokeJS.CObject.call("setAttr", id, attr, value);
50 private static void closeWindow() {}
52 private static Object textArea;
53 private static Object statusArea;
55 private static void log(String newText) {
56 if (textArea == null) {
59 String attr = "value";
60 setAttr(textArea, attr, getAttr(textArea, attr) + "\n" + newText);
61 setAttr(textArea, "scrollTop", getAttr(textArea, "scrollHeight"));
64 private static void beginTest(Case c) {
65 Object[] arr = new Object[2];
66 beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
71 private static void finishTest(Case c, Object res) {
72 if ("null".equals(res)) {
73 setAttr(statusArea, "innerHTML", "Success");
75 setAttr(statusArea, "innerHTML", "Result " + res);
81 private static final String BEGIN_TEST =
82 "var ul = window.document.getElementById('bck2brwsr.result');\n"
83 + "var li = window.document.createElement('li');\n"
84 + "var span = window.document.createElement('span');"
85 + "span.innerHTML = test + ' - ';\n"
86 + "var details = window.document.createElement('a');\n"
87 + "details.innerHTML = 'Details';\n"
88 + "details.href = '#';\n"
89 + "var p = window.document.createElement('p');\n"
90 + "var status = window.document.createElement('a');\n"
91 + "status.innerHTML = 'running';"
92 + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
93 + "status.onclick = function() { c.again(arr); }\n"
94 + "var pre = window.document.createElement('textarea');\n"
97 + "li.appendChild(span);\n"
98 + "li.appendChild(status);\n"
99 + "var span = window.document.createElement('span');"
100 + "span.innerHTML = ' ';\n"
101 + "li.appendChild(span);\n"
102 + "li.appendChild(details);\n"
103 + "p.appendChild(pre);\n"
104 + "ul.appendChild(li);\n"
106 + "arr[1] = status;\n";
108 private static void beginTest(String test, Case c, Object[] arr) {
109 InvokeJS.CObject.call("beginTest", test, c, arr);
112 private static final String LOAD_TEXT =
113 "var request = new XMLHttpRequest();\n"
114 + "request.open('GET', url, true);\n"
115 + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
116 + "request.onreadystatechange = function() {\n"
117 + " if (this.readyState!==4) return;\n"
119 + " arr[0] = this.responseText;\n"
120 + " callback.run__V();\n"
121 + " } catch (e) { alert(e); }"
124 private static void loadText(String url, Runnable callback, String[] arr) throws IOException {
125 InvokeJS.CObject.call("loadText", url, new Run(callback), arr);
128 public static void runHarness(String url) throws IOException {
129 new Console().harness(url);
132 public void harness(String url) throws IOException {
133 log("Connecting to " + url);
134 Request r = new Request(url);
137 private static class Request implements Runnable {
138 private final String[] arr = { null };
139 private final String url;
143 private Request(String url) throws IOException {
145 loadText(url, this, arr);
147 private Request(String url, String u) throws IOException {
149 loadText(u, this, arr);
156 String data = arr[0];
159 log("Some error exiting");
164 if (data.isEmpty()) {
165 log("No data, exiting");
170 c = Case.parseData(data);
172 log("Got \"" + data + "\"");
174 log("Processing \"" + arr[0] + "\" for " + retries + " time");
176 Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
177 finishTest(c, result);
179 String u = url + "?request=" + c.getRequestId() + "&result=" + result;
181 } catch (Exception ex) {
182 if (ex instanceof InterruptedException) {
183 log("Re-scheduling in 100ms");
187 log(ex.getClass().getName() + ":" + ex.getMessage());
192 private static String encodeURL(String r) throws UnsupportedEncodingException {
193 final String SPECIAL = "%$&+,/:;=?@";
194 StringBuilder sb = new StringBuilder();
195 byte[] utf8 = r.getBytes("UTF-8");
196 for (int i = 0; i < utf8.length; i++) {
197 int ch = utf8[i] & 0xff;
198 if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
199 final String numbers = "0" + Integer.toHexString(ch);
200 sb.append("%").append(numbers.substring(numbers.length() - 2));
209 return sb.toString();
212 static String invoke(String clazz, String method) throws
213 ClassNotFoundException, InvocationTargetException, IllegalAccessException,
214 InstantiationException, InterruptedException {
215 final Object r = new Case(null).invokeMethod(clazz, method);
216 return r == null ? "null" : r.toString().toString();
219 /** Helper method that inspects the classpath and loads given resource
220 * (usually a class file). Used while running tests in Rhino.
222 * @param name resource name to find
223 * @return the array of bytes in the given resource
224 * @throws IOException I/O in case something goes wrong
226 public static byte[] read(String name) throws IOException {
228 Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
229 while (en.hasMoreElements()) {
230 u = en.nextElement();
233 throw new IOException("Can't find " + name);
235 try (InputStream is = u.openStream()) {
237 arr = new byte[is.available()];
239 while (offset < arr.length) {
240 int len = is.read(arr, offset, arr.length - offset);
242 throw new IOException("Can't read " + name);
250 private static void turnAssetionStatusOn() {
253 private static Object schedule(Runnable r, int time) {
254 return InvokeJS.CObject.call("schedule", new Run(r), time);
257 private static final class Case {
258 private final Object data;
261 private Case(Object data) {
265 public static Case parseData(String s) {
266 return new Case(toJSON(s));
269 public String getMethodName() {
270 return (String) value("methodName", data);
273 public String getClassName() {
274 return (String) value("className", data);
277 public int getRequestId() {
278 Object v = value("request", data);
279 if (v instanceof Number) {
280 return ((Number)v).intValue();
282 return Integer.parseInt(v.toString());
285 public String getHtmlFragment() {
286 return (String) value("html", data);
289 void again(Object[] arr) {
293 setAttr(textArea, "value", "");
295 } catch (Exception ex) {
296 log(ex.getClass().getName() + ":" + ex.getMessage());
300 private Object runTest() throws IllegalAccessException,
301 IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException,
302 InvocationTargetException, InstantiationException, InterruptedException {
303 if (this.getHtmlFragment() != null) {
304 setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
306 log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId());
307 Object result = invokeMethod(this.getClassName(), this.getMethodName());
308 setAttr("bck2brwsr.fragment", "innerHTML", "");
309 log("Result: " + result);
310 result = encodeURL("" + result);
311 log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
315 private Object invokeMethod(String clazz, String method)
316 throws ClassNotFoundException, InvocationTargetException,
317 InterruptedException, IllegalAccessException, IllegalArgumentException,
318 InstantiationException {
320 Class<?> c = Class.forName(clazz);
321 for (Method m : c.getMethods()) {
322 if (m.getName().equals(method)) {
329 if ((found.getModifiers() & Modifier.STATIC) != 0) {
330 res = found.invoke(null);
333 inst = c.newInstance();
335 res = found.invoke(inst);
337 } catch (Throwable ex) {
338 if (ex instanceof InvocationTargetException) {
339 ex = ((InvocationTargetException) ex).getTargetException();
341 if (ex instanceof InterruptedException) {
342 throw (InterruptedException)ex;
344 res = ex.getClass().getName() + ":" + ex.getMessage();
347 res = "Can't find method " + method + " in " + clazz;
352 private static Object toJSON(String s) {
353 return InvokeJS.CObject.call("toJSON", s);
356 private static Object value(String p, Object d) {
357 return ((JSObject)d).getMember(p);
361 private static String safe(String txt) {
362 return "try {" + txt + "} catch (err) { alert(err); }";
366 turnAssetionStatusOn();
369 private static final class InvokeJS {
370 static final JSObject CObject = initJS();
372 private static JSObject initJS() {
373 WebEngine web = (WebEngine) System.getProperties().get("webEngine");
374 return (JSObject) web.executeScript("(function() {"
375 + "var CObject = {};"
377 + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
379 + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
380 + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
382 + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
384 + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT.replace("run__V", "run")) + "};"
386 + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
388 + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"