rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
branchclosure
changeset 1513 ba912ef24b27
parent 1497 daa5f903b529
parent 1453 e046cfcb8f00
child 1538 dfddf572c7ec
     1.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Mon Apr 28 20:23:44 2014 +0200
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Wed Apr 30 15:04:10 2014 +0200
     1.3 @@ -25,23 +25,33 @@
     1.4  import java.net.URL;
     1.5  import java.util.Enumeration;
     1.6  import javax.script.Invocable;
     1.7 +import javax.script.ScriptContext;
     1.8  import javax.script.ScriptEngine;
     1.9  import javax.script.ScriptEngineManager;
    1.10  import javax.script.ScriptException;
    1.11  import static org.testng.Assert.*;
    1.12  
    1.13 -final class TestVM {
    1.14 +public final class TestVM {
    1.15      private final Invocable code;
    1.16      private final CharSequence codeSeq;
    1.17      private final Object bck2brwsr;
    1.18 +    private BytesLoader resources;
    1.19      
    1.20      
    1.21      private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
    1.22          this.code = code;
    1.23          this.codeSeq = codeSeq;
    1.24 -        this.bck2brwsr = code.invokeFunction("bck2brwsr");
    1.25 +        this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
    1.26 +        ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
    1.27      }
    1.28      
    1.29 +    public void register(BytesLoader res) {
    1.30 +        this.resources = res;
    1.31 +    }
    1.32 +    
    1.33 +    public byte[] get(String res) throws IOException {
    1.34 +        return resources != null ? resources.get(res) : null;
    1.35 +    }
    1.36  
    1.37      public Object execCode(
    1.38          String msg, Class<?> clazz, String method, 
    1.39 @@ -234,11 +244,26 @@
    1.40              return ex.toString();
    1.41          }
    1.42      }
    1.43 -    
    1.44 +
    1.45 +    final CharSequence codeSeq() {
    1.46 +        return codeSeq;
    1.47 +    }
    1.48      
    1.49      private static class EmulationResources implements Bck2Brwsr.Resources {
    1.50          @Override
    1.51          public InputStream get(String name) throws IOException {
    1.52 +            if ("java/net/URI.class".equals(name)) {
    1.53 +                // skip
    1.54 +                return null;
    1.55 +            }
    1.56 +            if ("java/net/URLConnection.class".equals(name)) {
    1.57 +                // skip
    1.58 +                return null;
    1.59 +            }
    1.60 +            if ("java/lang/System.class".equals(name)) {
    1.61 +                // skip
    1.62 +                return null;
    1.63 +            }
    1.64              Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
    1.65              URL u = null;
    1.66              while (en.hasMoreElements()) {