vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
branchemul
changeset 708 59d5596a9c6c
parent 677 1ff540c1650f
     1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Tue Feb 05 17:04:22 2013 +0100
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Mon Feb 11 12:46:43 2013 +0100
     1.3 @@ -17,16 +17,6 @@
     1.4   */
     1.5  package org.apidesign.vm4brwsr;
     1.6  
     1.7 -import java.io.File;
     1.8 -import java.io.FileWriter;
     1.9 -import java.io.IOException;
    1.10 -import java.io.InputStream;
    1.11 -import java.net.URL;
    1.12 -import java.util.Enumeration;
    1.13 -import javax.script.Invocable;
    1.14 -import javax.script.ScriptEngine;
    1.15 -import javax.script.ScriptEngineManager;
    1.16 -import javax.script.ScriptException;
    1.17  import static org.testng.Assert.*;
    1.18  import org.testng.annotations.BeforeClass;
    1.19  import org.testng.annotations.Test;
    1.20 @@ -331,88 +321,18 @@
    1.21          );
    1.22      }
    1.23      
    1.24 -    private static CharSequence codeSeq;
    1.25 -    private static Invocable code;
    1.26 +    private static TestVM code;
    1.27      
    1.28      @BeforeClass 
    1.29      public void compileTheCode() throws Exception {
    1.30          StringBuilder sb = new StringBuilder();
    1.31 -        code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
    1.32 -        codeSeq = sb;
    1.33 -    }
    1.34 -    
    1.35 -    
    1.36 -    private static void assertExec(
    1.37 -        String msg, Class clazz, String method, 
    1.38 -        Object expRes, Object... args
    1.39 -    ) throws Exception {
    1.40 -        assertExec(code, codeSeq, msg, clazz, method, expRes, args);
    1.41 -    }
    1.42 -    static void assertExec(
    1.43 -        Invocable toRun, CharSequence theCode,
    1.44 -        String msg, Class clazz, String method, 
    1.45 -        Object expRes, Object... args
    1.46 -    ) throws Exception {
    1.47 -        Object ret = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args);
    1.48 -        if (ret == null) {
    1.49 -            return;
    1.50 -        }
    1.51 -        if (expRes != null && expRes.equals(ret)) {
    1.52 -            return;
    1.53 -        }
    1.54 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
    1.55 -        
    1.56 +        code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
    1.57      }
    1.58  
    1.59 -    static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
    1.60 -        return compileClass(sb, null, names);
    1.61 -    }
    1.62 -    static Invocable compileClass(
    1.63 -        StringBuilder sb, ScriptEngine[] eng, String... names
    1.64 -    ) throws ScriptException, IOException {
    1.65 -        if (sb == null) {
    1.66 -            sb = new StringBuilder();
    1.67 -        }
    1.68 -        Bck2Brwsr.generate(sb, new EmulationResources(), names);
    1.69 -        ScriptEngineManager sem = new ScriptEngineManager();
    1.70 -        ScriptEngine js = sem.getEngineByExtension("js");
    1.71 -        if (eng != null) {
    1.72 -            eng[0] = js;
    1.73 -        }
    1.74 -        try {
    1.75 -            Object res = js.eval(sb.toString());
    1.76 -            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
    1.77 -            return (Invocable)js;
    1.78 -        } catch (Exception ex) {
    1.79 -            if (sb.length() > 2000) {
    1.80 -                sb = dumpJS(sb);
    1.81 -            }
    1.82 -            fail("Could not evaluate:\n" + sb, ex);
    1.83 -            return null;
    1.84 -        }
    1.85 -    }
    1.86 -    static StringBuilder dumpJS(CharSequence sb) throws IOException {
    1.87 -        File f = File.createTempFile("execution", ".js");
    1.88 -        FileWriter w = new FileWriter(f);
    1.89 -        w.append(sb);
    1.90 -        w.close();
    1.91 -        return new StringBuilder(f.getPath());
    1.92 -    }
    1.93 -    private static class EmulationResources implements Bck2Brwsr.Resources {
    1.94 -        @Override
    1.95 -        public InputStream get(String name) throws IOException {
    1.96 -            Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
    1.97 -            URL u = null;
    1.98 -            while (en.hasMoreElements()) {
    1.99 -                u = en.nextElement();
   1.100 -            }
   1.101 -            if (u == null) {
   1.102 -                throw new IOException("Can't find " + name);
   1.103 -            }
   1.104 -            if (u.toExternalForm().contains("rt.jar!")) {
   1.105 -                throw new IOException("No emulation for " + u);
   1.106 -            }
   1.107 -            return u.openStream();
   1.108 -        }
   1.109 +    private void assertExec(
   1.110 +        String msg, Class<?> clazz, String method, 
   1.111 +        Object ret, Object... args
   1.112 +    ) throws Exception {
   1.113 +        code.assertExec(msg, clazz, method, ret, args);
   1.114      }
   1.115  }