@BrwsrTest that throws InterruptedException will be re-executed again later model
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 07 Apr 2013 16:11:56 +0200
branchmodel
changeset 939ce08b0b23949
parent 938 0eec1b51c13c
child 940 350a8fde33da
@BrwsrTest that throws InterruptedException will be re-executed again later
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java
rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CallMeTwiceTest.java
     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);
     2.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java	Sun Apr 07 15:24:45 2013 +0200
     2.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java	Sun Apr 07 16:11:56 2013 +0200
     2.3 @@ -75,7 +75,7 @@
     2.4              String res = c.invoke();
     2.5              value = res;
     2.6              if (fail) {
     2.7 -                int idx = res.indexOf(':');
     2.8 +                int idx = res == null ? -1 : res.indexOf(':');
     2.9                  if (idx >= 0) {
    2.10                      Class<? extends Throwable> thrwbl = null;
    2.11                      try {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CallMeTwiceTest.java	Sun Apr 07 16:11:56 2013 +0200
     3.3 @@ -0,0 +1,43 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.bck2brwsr.vmtest.impl;
    3.22 +
    3.23 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    3.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    3.25 +import org.testng.annotations.Factory;
    3.26 +
    3.27 +/**
    3.28 + *
    3.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.30 + */
    3.31 +public class CallMeTwiceTest {
    3.32 +    int cnt;
    3.33 +    
    3.34 +    @BrwsrTest public void callMeTwice() throws InterruptedException {
    3.35 +        if (cnt++ == 0) {
    3.36 +            throw new InterruptedException();
    3.37 +        }
    3.38 +        int prevCnt = cnt;
    3.39 +        cnt = 0;
    3.40 +        assert prevCnt == 2 : "We need to receive two calls " + prevCnt;
    3.41 +    }
    3.42 +    
    3.43 +    @Factory public static Object[] create() {
    3.44 +        return VMTest.create(CallMeTwiceTest.class);
    3.45 +    }
    3.46 +}