2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.lang.reflect.Method;
21 import javax.script.Invocable;
22 import javax.script.ScriptContext;
23 import javax.script.ScriptEngine;
24 import javax.script.ScriptEngineManager;
25 import org.testng.Assert;
26 import org.testng.ITest;
27 import org.testng.annotations.Factory;
28 import org.testng.annotations.Test;
30 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
31 * in provided class and builds set of tests that compare the computations
32 * in real as well as JavaScript virtual machines. Use as:<pre>
33 * {@code @}{@link Factory} public static create() {
34 * return @{link CompareVMs}.{@link #create(YourClass.class);
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 public final class CompareVMs implements ITest {
40 private final Run first, second;
41 private final Method m;
43 private CompareVMs(Method m, Run first, Run second) {
49 public static Object[] create(Class<?> clazz) {
50 Method[] arr = clazz.getMethods();
51 Object[] ret = new Object[3 * arr.length];
53 for (Method m : arr) {
54 Compare c = m.getAnnotation(Compare.class);
58 final Run real = new Run(m, false);
59 final Run js = new Run(m, true);
62 ret[cnt++] = new CompareVMs(m, real, js);
64 Object[] r = new Object[cnt];
65 for (int i = 0; i < cnt; i++) {
71 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
72 Object v1 = first.value;
73 Object v2 = second.value;
74 if (v1 instanceof Number) {
75 v1 = ((Number)v1).doubleValue();
77 Assert.assertEquals(v1, v2, "Comparing results");
81 public String getTestName() {
82 return m.getName() + "[Compare]";
85 public static final class Run implements ITest {
86 private final Method m;
87 private final boolean js;
89 private static Invocable code;
90 private static CharSequence codeSeq;
92 private Run(Method m, boolean js) {
97 private static void compileTheCode(Class<?> clazz) throws Exception {
101 StringBuilder sb = new StringBuilder();
102 Bck2Brwsr.generate(sb, CompareVMs.class.getClassLoader());
104 ScriptEngineManager sem = new ScriptEngineManager();
105 ScriptEngine js = sem.getEngineByExtension("js");
106 js.getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
108 sb.append("\nfunction initVM() {"
109 + "\n return bck2brwsr("
110 + "\n function(name) { return loader.get(name);}"
114 Object res = js.eval(sb.toString());
115 Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
116 code = (Invocable) js;
120 @Test(groups = "run") public void executeCode() throws Throwable {
123 compileTheCode(m.getDeclaringClass());
124 Object vm = code.invokeFunction("initVM");
125 Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
126 value = code.invokeMethod(inst, m.getName() + "__I");
127 } catch (Exception ex) {
128 throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
131 value = m.invoke(m.getDeclaringClass().newInstance());
135 public String getTestName() {
136 return m.getName() + (js ? "[JavaScript]" : "[Java]");