diff -r 1ff540c1650f -r 59d5596a9c6c vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Tue Feb 05 17:04:22 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Mon Feb 11 12:46:43 2013 +0100 @@ -17,16 +17,6 @@ */ package org.apidesign.vm4brwsr; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Enumeration; -import javax.script.Invocable; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; import static org.testng.Assert.*; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -331,88 +321,18 @@ ); } - private static CharSequence codeSeq; - private static Invocable code; + private static TestVM code; @BeforeClass public void compileTheCode() throws Exception { StringBuilder sb = new StringBuilder(); - code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod"); - codeSeq = sb; - } - - - private static void assertExec( - String msg, Class clazz, String method, - Object expRes, Object... args - ) throws Exception { - assertExec(code, codeSeq, msg, clazz, method, expRes, args); - } - static void assertExec( - Invocable toRun, CharSequence theCode, - String msg, Class clazz, String method, - Object expRes, Object... args - ) throws Exception { - Object ret = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args); - if (ret == null) { - return; - } - if (expRes != null && expRes.equals(ret)) { - return; - } - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode)); - + code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod"); } - static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException { - return compileClass(sb, null, names); - } - static Invocable compileClass( - StringBuilder sb, ScriptEngine[] eng, String... names - ) throws ScriptException, IOException { - if (sb == null) { - sb = new StringBuilder(); - } - Bck2Brwsr.generate(sb, new EmulationResources(), names); - ScriptEngineManager sem = new ScriptEngineManager(); - ScriptEngine js = sem.getEngineByExtension("js"); - if (eng != null) { - eng[0] = js; - } - try { - Object res = js.eval(sb.toString()); - assertTrue(js instanceof Invocable, "It is invocable object: " + res); - return (Invocable)js; - } catch (Exception ex) { - if (sb.length() > 2000) { - sb = dumpJS(sb); - } - fail("Could not evaluate:\n" + sb, ex); - return null; - } - } - static StringBuilder dumpJS(CharSequence sb) throws IOException { - File f = File.createTempFile("execution", ".js"); - FileWriter w = new FileWriter(f); - w.append(sb); - w.close(); - return new StringBuilder(f.getPath()); - } - private static class EmulationResources implements Bck2Brwsr.Resources { - @Override - public InputStream get(String name) throws IOException { - Enumeration en = StaticMethodTest.class.getClassLoader().getResources(name); - URL u = null; - while (en.hasMoreElements()) { - u = en.nextElement(); - } - if (u == null) { - throw new IOException("Can't find " + name); - } - if (u.toExternalForm().contains("rt.jar!")) { - throw new IOException("No emulation for " + u); - } - return u.openStream(); - } + private void assertExec( + String msg, Class clazz, String method, + Object ret, Object... args + ) throws Exception { + code.assertExec(msg, clazz, method, ret, args); } }