jaroslav@708: /** jaroslav@708: * Back 2 Browser Bytecode Translator jaroslav@708: * Copyright (C) 2012 Jaroslav Tulach jaroslav@708: * jaroslav@708: * This program is free software: you can redistribute it and/or modify jaroslav@708: * it under the terms of the GNU General Public License as published by jaroslav@708: * the Free Software Foundation, version 2 of the License. jaroslav@708: * jaroslav@708: * This program is distributed in the hope that it will be useful, jaroslav@708: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@708: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@708: * GNU General Public License for more details. jaroslav@708: * jaroslav@708: * You should have received a copy of the GNU General Public License jaroslav@708: * along with this program. Look for COPYING file in the top folder. jaroslav@708: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@708: */ jaroslav@708: package org.apidesign.vm4brwsr; jaroslav@708: jaroslav@708: import java.io.File; jaroslav@708: import java.io.FileWriter; jaroslav@708: import java.io.IOException; jaroslav@708: import java.io.InputStream; jaroslav@708: import java.net.URL; jaroslav@708: import java.util.Enumeration; jaroslav@708: import javax.script.Invocable; jaroslav@708: import javax.script.ScriptEngine; jaroslav@708: import javax.script.ScriptEngineManager; jaroslav@708: import javax.script.ScriptException; jaroslav@708: import static org.testng.Assert.*; jaroslav@708: jaroslav@708: final class TestVM { jaroslav@708: private final Invocable code; jaroslav@708: private final CharSequence codeSeq; jaroslav@708: private final Object bck2brwsr; jaroslav@708: jaroslav@708: jaroslav@708: private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException { jaroslav@708: this.code = code; jaroslav@708: this.codeSeq = codeSeq; jaroslav@708: this.bck2brwsr = code.invokeFunction("bck2brwsr"); jaroslav@708: } jaroslav@708: jaroslav@708: jaroslav@708: public Object execCode( jaroslav@708: String msg, Class clazz, String method, jaroslav@708: Object expRes, Object... args jaroslav@708: ) throws Exception { jaroslav@708: Object ret = null; jaroslav@708: try { jaroslav@708: ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName()); jaroslav@708: ret = code.invokeMethod(ret, method, args); jaroslav@708: } catch (ScriptException ex) { jaroslav@708: fail("Execution failed in " + dumpJS(codeSeq), ex); jaroslav@708: } catch (NoSuchMethodException ex) { jaroslav@708: fail("Cannot find method in " + dumpJS(codeSeq), ex); jaroslav@708: } jaroslav@708: if (ret == null && expRes == null) { jaroslav@708: return null; jaroslav@708: } jaroslav@708: if (expRes.equals(ret)) { jaroslav@708: return null; jaroslav@708: } jaroslav@708: if (expRes instanceof Number) { jaroslav@708: // in case of Long it is necessary convert it to number jaroslav@708: // since the Long is represented by two numbers in JavaScript jaroslav@708: try { jaroslav@708: ret = code.invokeMethod(ret, "toFP"); jaroslav@708: ret = code.invokeFunction("Number", ret); jaroslav@708: } catch (ScriptException ex) { jaroslav@708: fail("Conversion to number failed in " + dumpJS(codeSeq), ex); jaroslav@708: } catch (NoSuchMethodException ex) { jaroslav@708: fail("Cannot find global Number(x) function in " + dumpJS(codeSeq), ex); jaroslav@708: } jaroslav@708: } jaroslav@708: return ret; jaroslav@708: } jaroslav@708: jaroslav@708: void assertExec( jaroslav@708: String msg, Class clazz, String method, Object expRes, Object... args jaroslav@708: ) throws Exception { jaroslav@708: Object ret = execCode(msg, clazz, method, expRes, args); jaroslav@708: if (ret == null) { jaroslav@708: return; jaroslav@708: } jaroslav@708: if (expRes != null && expRes.equals(ret)) { jaroslav@708: return; jaroslav@708: } jaroslav@708: assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq)); jaroslav@708: } jaroslav@708: jaroslav@708: static TestVM compileClass(String... names) throws ScriptException, IOException { jaroslav@708: return compileClass(null, names); jaroslav@708: } jaroslav@708: jaroslav@708: static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException { jaroslav@708: return compileClass(sb, null, names); jaroslav@708: } jaroslav@708: jaroslav@708: static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException { jaroslav@708: if (sb == null) { jaroslav@708: sb = new StringBuilder(); jaroslav@708: } jaroslav@708: Bck2Brwsr.generate(sb, new EmulationResources(), names); jaroslav@708: ScriptEngineManager sem = new ScriptEngineManager(); jaroslav@708: ScriptEngine js = sem.getEngineByExtension("js"); jaroslav@708: if (eng != null) { jaroslav@708: eng[0] = js; jaroslav@708: } jaroslav@708: try { jaroslav@708: Object res = js.eval(sb.toString()); jaroslav@708: assertTrue(js instanceof Invocable, "It is invocable object: " + res); jaroslav@708: return new TestVM((Invocable) js, sb); jaroslav@708: } catch (Exception ex) { jaroslav@708: if (sb.length() > 2000) { jaroslav@708: sb = dumpJS(sb); jaroslav@708: } jaroslav@708: fail("Could not evaluate:\n" + sb, ex); jaroslav@708: return null; jaroslav@708: } jaroslav@708: } jaroslav@708: jaroslav@708: Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException { jaroslav@708: return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName()); jaroslav@708: } jaroslav@708: jaroslav@708: Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException { jaroslav@708: return code.invokeMethod(obj, method, params); jaroslav@708: } jaroslav@708: jaroslav@708: Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException { jaroslav@708: return code.invokeFunction(methodName, args); jaroslav@708: } jaroslav@708: jaroslav@708: static StringBuilder dumpJS(CharSequence sb) throws IOException { jaroslav@708: File f = File.createTempFile("execution", ".js"); jaroslav@708: FileWriter w = new FileWriter(f); jaroslav@708: w.append(sb); jaroslav@708: w.close(); jaroslav@708: return new StringBuilder(f.getPath()); jaroslav@708: } jaroslav@708: jaroslav@708: @Override jaroslav@708: public String toString() { jaroslav@708: try { jaroslav@708: return dumpJS(codeSeq).toString(); jaroslav@708: } catch (IOException ex) { jaroslav@708: return ex.toString(); jaroslav@708: } jaroslav@708: } jaroslav@708: jaroslav@708: jaroslav@708: private static class EmulationResources implements Bck2Brwsr.Resources { jaroslav@708: @Override jaroslav@708: public InputStream get(String name) throws IOException { jaroslav@708: Enumeration en = StaticMethodTest.class.getClassLoader().getResources(name); jaroslav@708: URL u = null; jaroslav@708: while (en.hasMoreElements()) { jaroslav@708: u = en.nextElement(); jaroslav@708: } jaroslav@708: if (u == null) { jaroslav@708: throw new IOException("Can't find " + name); jaroslav@708: } jaroslav@708: if (u.toExternalForm().contains("rt.jar!")) { jaroslav@708: throw new IOException("No emulation for " + u); jaroslav@708: } jaroslav@708: return u.openStream(); jaroslav@708: } jaroslav@708: } jaroslav@708: }