rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java
branchmodel
changeset 939 ce08b0b23949
parent 938 0eec1b51c13c
child 940 350a8fde33da
     1.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java	Sun Apr 07 15:24:45 2013 +0200
     1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java	Sun Apr 07 16:11:56 2013 +0200
     1.3 @@ -108,13 +108,6 @@
     1.4      )
     1.5      private static native void beginTest(String test, Case c, Object[] arr);
     1.6      
     1.7 -    public static void execute() throws Exception {
     1.8 -        String clazz = (String) getAttr("clazz", "value");
     1.9 -        String method = (String) getAttr("method", "value");
    1.10 -        Object res = invokeMethod(clazz, method);
    1.11 -        setAttr("bck2brwsr.result", "value", res);
    1.12 -    }
    1.13 -
    1.14      @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
    1.15          + "var request = new XMLHttpRequest();\n"
    1.16          + "request.open('GET', url, true);\n"
    1.17 @@ -136,39 +129,49 @@
    1.18      private static class Request implements Runnable {
    1.19          private final String[] arr = { null };
    1.20          private final String url;
    1.21 +        private Case c;
    1.22  
    1.23          private Request(String url) throws IOException {
    1.24              this.url = url;
    1.25              loadText(url, this, arr);
    1.26          }
    1.27 +        private Request(String url, String u) throws IOException {
    1.28 +            this.url = url;
    1.29 +            loadText(u, this, arr);
    1.30 +        }
    1.31          
    1.32          @Override
    1.33          public void run() {
    1.34              try {
    1.35 -                String data = arr[0];
    1.36 -                log("\nGot \"" + data + "\"");
    1.37 +                if (c == null) {
    1.38 +                    String data = arr[0];
    1.39 +                    log("\nGot \"" + data + "\"");
    1.40 +
    1.41 +                    if (data == null) {
    1.42 +                        log("Some error exiting");
    1.43 +                        closeWindow();
    1.44 +                        return;
    1.45 +                    }
    1.46 +
    1.47 +                    if (data.isEmpty()) {
    1.48 +                        log("No data, exiting");
    1.49 +                        closeWindow();
    1.50 +                        return;
    1.51 +                    }
    1.52 +
    1.53 +                    c = Case.parseData(data);
    1.54 +                    beginTest(c);
    1.55 +                }
    1.56 +                Object result = c.runTest();
    1.57 +                finishTest(c, result);
    1.58                  
    1.59 -                if (data == null) {
    1.60 -                    log("Some error exiting");
    1.61 -                    closeWindow();
    1.62 +                String u = url + "?request=" + c.getRequestId() + "&result=" + result;
    1.63 +                new Request(url, u);
    1.64 +            } catch (Exception ex) {
    1.65 +                if (ex instanceof InterruptedException) {
    1.66 +                    interval(this, 100);
    1.67                      return;
    1.68                  }
    1.69 -                
    1.70 -                if (data.isEmpty()) {
    1.71 -                    log("No data, exiting");
    1.72 -                    closeWindow();
    1.73 -                    return;
    1.74 -                }
    1.75 -                
    1.76 -                Case c = Case.parseData(data);
    1.77 -                beginTest(c);
    1.78 -                Object result = c.runTest();
    1.79 -                finishTest(c, result);
    1.80 -                String u = url + "?request=" + c.getRequestId() + "&result=" + result;
    1.81 -                
    1.82 -                loadText(u, this, arr);
    1.83 -                
    1.84 -            } catch (Exception ex) {
    1.85                  log(ex.getClass().getName() + ":" + ex.getMessage());
    1.86              }
    1.87          }
    1.88 @@ -194,8 +197,10 @@
    1.89          return sb.toString();
    1.90      }
    1.91      
    1.92 -    static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
    1.93 -        final Object r = invokeMethod(clazz, method);
    1.94 +    static String invoke(String clazz, String method) throws 
    1.95 +    ClassNotFoundException, InvocationTargetException, IllegalAccessException, 
    1.96 +    InstantiationException, InterruptedException {
    1.97 +        final Object r = new Case(null).invokeMethod(clazz, method);
    1.98          return r == null ? "null" : r.toString().toString();
    1.99      }
   1.100  
   1.101 @@ -230,43 +235,17 @@
   1.102          }
   1.103      }
   1.104     
   1.105 -    private static Object invokeMethod(String clazz, String method) 
   1.106 -    throws ClassNotFoundException, InvocationTargetException, 
   1.107 -    SecurityException, IllegalAccessException, IllegalArgumentException,
   1.108 -    InstantiationException {
   1.109 -        Method found = null;
   1.110 -        Class<?> c = Class.forName(clazz);
   1.111 -        for (Method m : c.getMethods()) {
   1.112 -            if (m.getName().equals(method)) {
   1.113 -                found = m;
   1.114 -            }
   1.115 -        }
   1.116 -        Object res;
   1.117 -        if (found != null) {
   1.118 -            try {
   1.119 -                if ((found.getModifiers() & Modifier.STATIC) != 0) {
   1.120 -                    res = found.invoke(null);
   1.121 -                } else {
   1.122 -                    res = found.invoke(c.newInstance());
   1.123 -                }
   1.124 -            } catch (Throwable ex) {
   1.125 -                if (ex instanceof InvocationTargetException) {
   1.126 -                    ex = ((InvocationTargetException)ex).getTargetException();
   1.127 -                }
   1.128 -                res = ex.getClass().getName() + ":" + ex.getMessage();
   1.129 -            }
   1.130 -        } else {
   1.131 -            res = "Can't find method " + method + " in " + clazz;
   1.132 -        }
   1.133 -        return res;
   1.134 -    }
   1.135 -
   1.136      @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
   1.137      private static void turnAssetionStatusOn() {
   1.138      }
   1.139 +
   1.140 +    @JavaScriptBody(args = {"r", "time"}, body =
   1.141 +        "return window.setInterval(function() { r.run__V(); }, time);")
   1.142 +    private static native Object interval(Runnable r, int time);
   1.143      
   1.144      private static final class Case {
   1.145          private final Object data;
   1.146 +        private Object inst;
   1.147  
   1.148          private Case(Object data) {
   1.149              this.data = data;
   1.150 @@ -303,7 +282,9 @@
   1.151              }
   1.152          }
   1.153  
   1.154 -        private Object runTest() throws IllegalAccessException, IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException, InvocationTargetException, InstantiationException, SecurityException {
   1.155 +        private Object runTest() throws IllegalAccessException, 
   1.156 +        IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException, 
   1.157 +        InvocationTargetException, InstantiationException, InterruptedException {
   1.158              if (this.getHtmlFragment() != null) {
   1.159                  setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
   1.160              }
   1.161 @@ -315,6 +296,43 @@
   1.162              log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
   1.163              return result;
   1.164          }
   1.165 +
   1.166 +        private Object invokeMethod(String clazz, String method)
   1.167 +        throws ClassNotFoundException, InvocationTargetException,
   1.168 +        InterruptedException, IllegalAccessException, IllegalArgumentException,
   1.169 +        InstantiationException {
   1.170 +            Method found = null;
   1.171 +            Class<?> c = Class.forName(clazz);
   1.172 +            for (Method m : c.getMethods()) {
   1.173 +                if (m.getName().equals(method)) {
   1.174 +                    found = m;
   1.175 +                }
   1.176 +            }
   1.177 +            Object res;
   1.178 +            if (found != null) {
   1.179 +                try {
   1.180 +                    if ((found.getModifiers() & Modifier.STATIC) != 0) {
   1.181 +                        res = found.invoke(null);
   1.182 +                    } else {
   1.183 +                        if (inst == null) {
   1.184 +                            inst = c.newInstance();
   1.185 +                        }
   1.186 +                        res = found.invoke(inst);
   1.187 +                    }
   1.188 +                } catch (Throwable ex) {
   1.189 +                    if (ex instanceof InvocationTargetException) {
   1.190 +                        ex = ((InvocationTargetException) ex).getTargetException();
   1.191 +                    }
   1.192 +                    if (ex instanceof InterruptedException) {
   1.193 +                        throw (InterruptedException)ex;
   1.194 +                    }
   1.195 +                    res = ex.getClass().getName() + ":" + ex.getMessage();
   1.196 +                }
   1.197 +            } else {
   1.198 +                res = "Can't find method " + method + " in " + clazz;
   1.199 +            }
   1.200 +            return res;
   1.201 +        }
   1.202          
   1.203          @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
   1.204          private static native Object toJSON(String s);