jaroslav@273: /** jaroslav@273: * Back 2 Browser Bytecode Translator jaroslav@273: * Copyright (C) 2012 Jaroslav Tulach jaroslav@273: * jaroslav@273: * This program is free software: you can redistribute it and/or modify jaroslav@273: * it under the terms of the GNU General Public License as published by jaroslav@273: * the Free Software Foundation, version 2 of the License. jaroslav@273: * jaroslav@273: * This program is distributed in the hope that it will be useful, jaroslav@273: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@273: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@273: * GNU General Public License for more details. jaroslav@273: * jaroslav@273: * You should have received a copy of the GNU General Public License jaroslav@273: * along with this program. Look for COPYING file in the top folder. jaroslav@273: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@273: */ jaroslav@273: package org.apidesign.vm4brwsr; jaroslav@273: jaroslav@273: import java.lang.reflect.Method; jaroslav@273: import javax.script.Invocable; jaroslav@298: import javax.script.ScriptContext; jaroslav@273: import javax.script.ScriptEngine; jaroslav@273: import javax.script.ScriptEngineManager; jaroslav@273: import org.testng.Assert; jaroslav@273: import org.testng.ITest; jaroslav@273: import org.testng.annotations.Factory; jaroslav@273: import org.testng.annotations.Test; jaroslav@273: jaroslav@273: /** A TestNG {@link Factory} that seeks for {@link Compare} annotations jaroslav@273: * in provided class and builds set of tests that compare the computations jaroslav@273: * in real as well as JavaScript virtual machines. Use as:
jaroslav@273:  * {@code @}{@link Factory} public static create() {
jaroslav@273:  *   return @{link CompareVMs}.{@link #create(YourClass.class);
jaroslav@273:  * }
jaroslav@273: * jaroslav@273: * @author Jaroslav Tulach jaroslav@273: */ jaroslav@273: public final class CompareVMs implements ITest { jaroslav@280: private final Run first, second; jaroslav@273: private final Method m; jaroslav@273: jaroslav@280: private CompareVMs(Method m, Run first, Run second) { jaroslav@273: this.first = first; jaroslav@273: this.second = second; jaroslav@273: this.m = m; jaroslav@273: } jaroslav@273: jaroslav@273: public static Object[] create(Class clazz) { jaroslav@273: Method[] arr = clazz.getMethods(); jaroslav@273: Object[] ret = new Object[3 * arr.length]; jaroslav@273: int cnt = 0; jaroslav@273: for (Method m : arr) { jaroslav@273: Compare c = m.getAnnotation(Compare.class); jaroslav@273: if (c == null) { jaroslav@273: continue; jaroslav@273: } jaroslav@280: final Run real = new Run(m, false); jaroslav@280: final Run js = new Run(m, true); jaroslav@273: ret[cnt++] = real; jaroslav@273: ret[cnt++] = js; jaroslav@273: ret[cnt++] = new CompareVMs(m, real, js); jaroslav@273: } jaroslav@273: Object[] r = new Object[cnt]; jaroslav@273: for (int i = 0; i < cnt; i++) { jaroslav@273: r[i] = ret[i]; jaroslav@273: } jaroslav@273: return r; jaroslav@273: } jaroslav@273: jaroslav@280: @Test(dependsOnGroups = "run") public void compareResults() throws Throwable { jaroslav@280: Object v1 = first.value; jaroslav@280: Object v2 = second.value; jaroslav@280: if (v1 instanceof Number) { jaroslav@280: v1 = ((Number)v1).doubleValue(); jaroslav@280: } jaroslav@280: Assert.assertEquals(v1, v2, "Comparing results"); jaroslav@280: } jaroslav@280: jaroslav@280: @Override jaroslav@280: public String getTestName() { jaroslav@280: return m.getName() + "[Compare]"; jaroslav@280: } jaroslav@280: jaroslav@280: public static final class Run implements ITest { jaroslav@280: private final Method m; jaroslav@280: private final boolean js; jaroslav@280: Object value; jaroslav@280: private static Invocable code; jaroslav@280: private static CharSequence codeSeq; jaroslav@280: jaroslav@280: private Run(Method m, boolean js) { jaroslav@280: this.m = m; jaroslav@280: this.js = js; jaroslav@280: } jaroslav@280: jaroslav@280: private static void compileTheCode(Class clazz) throws Exception { jaroslav@280: if (code != null) { jaroslav@280: return; jaroslav@273: } jaroslav@280: StringBuilder sb = new StringBuilder(); jaroslav@298: Bck2Brwsr.generate(sb, CompareVMs.class.getClassLoader()); jaroslav@280: jaroslav@280: ScriptEngineManager sem = new ScriptEngineManager(); jaroslav@280: ScriptEngine js = sem.getEngineByExtension("js"); jaroslav@298: js.getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE); jaroslav@303: jaroslav@303: sb.append("\nfunction initVM() {" jaroslav@303: + "\n return bck2brwsr(" jaroslav@303: + "\n function(name) { return loader.get(name);}" jaroslav@303: + "\n );" jaroslav@303: + "\n};"); jaroslav@280: jaroslav@280: Object res = js.eval(sb.toString()); jaroslav@280: Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res); jaroslav@280: code = (Invocable) js; jaroslav@280: codeSeq = sb; jaroslav@280: } jaroslav@280: jaroslav@280: @Test(groups = "run") public void executeCode() throws Throwable { jaroslav@273: if (js) { jaroslav@273: try { jaroslav@273: compileTheCode(m.getDeclaringClass()); jaroslav@303: Object vm = code.invokeFunction("initVM"); jaroslav@276: Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName()); jaroslav@273: value = code.invokeMethod(inst, m.getName() + "__I"); jaroslav@273: } catch (Exception ex) { jaroslav@273: throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex); jaroslav@273: } jaroslav@273: } else { jaroslav@273: value = m.invoke(m.getDeclaringClass().newInstance()); jaroslav@273: } jaroslav@273: } jaroslav@280: @Override jaroslav@280: public String getTestName() { jaroslav@280: return m.getName() + (js ? "[JavaScript]" : "[Java]"); jaroslav@273: } jaroslav@273: } jaroslav@273: }