rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java
changeset 958 c75bd6823179
parent 922 2fb3e929962f
parent 942 0e2ced48871d
child 1006 691c5cd3fb93
     1.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java	Wed Apr 03 13:43:22 2013 +0200
     1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java	Mon Apr 08 19:33:08 2013 +0200
     1.3 @@ -113,13 +113,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 @@ -141,39 +134,53 @@
    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 +        private int retries;
    1.23  
    1.24          private Request(String url) throws IOException {
    1.25              this.url = url;
    1.26              loadText(url, this, arr);
    1.27          }
    1.28 +        private Request(String url, String u) throws IOException {
    1.29 +            this.url = url;
    1.30 +            loadText(u, this, arr);
    1.31 +        }
    1.32          
    1.33          @Override
    1.34          public void run() {
    1.35              try {
    1.36 -                String data = arr[0];
    1.37 -                log("\nGot \"" + data + "\"");
    1.38 +                if (c == null) {
    1.39 +                    String data = arr[0];
    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 +                    log("Got \"" + data + "\"");
    1.56 +                } else {
    1.57 +                    log("Processing \"" + arr[0] + "\" for " + retries + " time");
    1.58 +                }
    1.59 +                Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
    1.60 +                finishTest(c, result);
    1.61                  
    1.62 -                if (data == null) {
    1.63 -                    log("Some error exiting");
    1.64 -                    closeWindow();
    1.65 +                String u = url + "?request=" + c.getRequestId() + "&result=" + result;
    1.66 +                new Request(url, u);
    1.67 +            } catch (Exception ex) {
    1.68 +                if (ex instanceof InterruptedException) {
    1.69 +                    log("Re-scheduling in 100ms");
    1.70 +                    schedule(this, 100);
    1.71                      return;
    1.72                  }
    1.73 -                
    1.74 -                if (data.isEmpty()) {
    1.75 -                    log("No data, exiting");
    1.76 -                    closeWindow();
    1.77 -                    return;
    1.78 -                }
    1.79 -                
    1.80 -                Case c = Case.parseData(data);
    1.81 -                beginTest(c);
    1.82 -                Object result = c.runTest();
    1.83 -                finishTest(c, result);
    1.84 -                String u = url + "?request=" + c.getRequestId() + "&result=" + result;
    1.85 -                
    1.86 -                loadText(u, this, arr);
    1.87 -                
    1.88 -            } catch (Exception ex) {
    1.89                  log(ex.getClass().getName() + ":" + ex.getMessage());
    1.90              }
    1.91          }
    1.92 @@ -199,8 +206,10 @@
    1.93          return sb.toString();
    1.94      }
    1.95      
    1.96 -    static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
    1.97 -        final Object r = invokeMethod(clazz, method);
    1.98 +    static String invoke(String clazz, String method) throws 
    1.99 +    ClassNotFoundException, InvocationTargetException, IllegalAccessException, 
   1.100 +    InstantiationException, InterruptedException {
   1.101 +        final Object r = new Case(null).invokeMethod(clazz, method);
   1.102          return r == null ? "null" : r.toString().toString();
   1.103      }
   1.104  
   1.105 @@ -235,40 +244,17 @@
   1.106          }
   1.107      }
   1.108     
   1.109 -    private static Object invokeMethod(String clazz, String method) 
   1.110 -    throws ClassNotFoundException, InvocationTargetException, 
   1.111 -    SecurityException, IllegalAccessException, IllegalArgumentException,
   1.112 -    InstantiationException {
   1.113 -        Method found = null;
   1.114 -        Class<?> c = Class.forName(clazz);
   1.115 -        for (Method m : c.getMethods()) {
   1.116 -            if (m.getName().equals(method)) {
   1.117 -                found = m;
   1.118 -            }
   1.119 -        }
   1.120 -        Object res;
   1.121 -        if (found != null) {
   1.122 -            try {
   1.123 -                if ((found.getModifiers() & Modifier.STATIC) != 0) {
   1.124 -                    res = found.invoke(null);
   1.125 -                } else {
   1.126 -                    res = found.invoke(c.newInstance());
   1.127 -                }
   1.128 -            } catch (Throwable ex) {
   1.129 -                res = ex.getClass().getName() + ":" + ex.getMessage();
   1.130 -            }
   1.131 -        } else {
   1.132 -            res = "Can't find method " + method + " in " + clazz;
   1.133 -        }
   1.134 -        return res;
   1.135 -    }
   1.136 -
   1.137      @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
   1.138      private static void turnAssetionStatusOn() {
   1.139      }
   1.140 +
   1.141 +    @JavaScriptBody(args = {"r", "time"}, body =
   1.142 +        "return window.setTimeout(function() { r.run__V(); }, time);")
   1.143 +    private static native Object schedule(Runnable r, int time);
   1.144      
   1.145      private static final class Case {
   1.146          private final Object data;
   1.147 +        private Object inst;
   1.148  
   1.149          private Case(Object data) {
   1.150              this.data = data;
   1.151 @@ -305,7 +291,9 @@
   1.152              }
   1.153          }
   1.154  
   1.155 -        private Object runTest() throws IllegalAccessException, IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException, InvocationTargetException, InstantiationException, SecurityException {
   1.156 +        private Object runTest() throws IllegalAccessException, 
   1.157 +        IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException, 
   1.158 +        InvocationTargetException, InstantiationException, InterruptedException {
   1.159              if (this.getHtmlFragment() != null) {
   1.160                  setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
   1.161              }
   1.162 @@ -317,6 +305,43 @@
   1.163              log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
   1.164              return result;
   1.165          }
   1.166 +
   1.167 +        private Object invokeMethod(String clazz, String method)
   1.168 +        throws ClassNotFoundException, InvocationTargetException,
   1.169 +        InterruptedException, IllegalAccessException, IllegalArgumentException,
   1.170 +        InstantiationException {
   1.171 +            Method found = null;
   1.172 +            Class<?> c = Class.forName(clazz);
   1.173 +            for (Method m : c.getMethods()) {
   1.174 +                if (m.getName().equals(method)) {
   1.175 +                    found = m;
   1.176 +                }
   1.177 +            }
   1.178 +            Object res;
   1.179 +            if (found != null) {
   1.180 +                try {
   1.181 +                    if ((found.getModifiers() & Modifier.STATIC) != 0) {
   1.182 +                        res = found.invoke(null);
   1.183 +                    } else {
   1.184 +                        if (inst == null) {
   1.185 +                            inst = c.newInstance();
   1.186 +                        }
   1.187 +                        res = found.invoke(inst);
   1.188 +                    }
   1.189 +                } catch (Throwable ex) {
   1.190 +                    if (ex instanceof InvocationTargetException) {
   1.191 +                        ex = ((InvocationTargetException) ex).getTargetException();
   1.192 +                    }
   1.193 +                    if (ex instanceof InterruptedException) {
   1.194 +                        throw (InterruptedException)ex;
   1.195 +                    }
   1.196 +                    res = ex.getClass().getName() + ":" + ex.getMessage();
   1.197 +                }
   1.198 +            } else {
   1.199 +                res = "Can't find method " + method + " in " + clazz;
   1.200 +            }
   1.201 +            return res;
   1.202 +        }
   1.203          
   1.204          @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
   1.205          private static native Object toJSON(String s);