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.ScriptEngine;
23 import javax.script.ScriptEngineManager;
24 import org.testng.Assert;
25 import org.testng.ITest;
26 import org.testng.annotations.Factory;
27 import org.testng.annotations.Test;
29 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
30 * in provided class and builds set of tests that compare the computations
31 * in real as well as JavaScript virtual machines. Use as:<pre>
32 * {@code @}{@link Factory} public static create() {
33 * return @{link CompareVMs}.{@link #create(YourClass.class);
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 public final class CompareVMs implements ITest {
39 private final Method m;
40 private final boolean js;
41 private final CompareVMs first, second;
43 private static Invocable code;
44 private static CharSequence codeSeq;
46 private CompareVMs(Method m, boolean js) {
53 private CompareVMs(Method m, CompareVMs first, CompareVMs second) {
60 private static void compileTheCode(Class<?> clazz) throws Exception {
64 StringBuilder sb = new StringBuilder();
65 class SkipMe extends GenJS {
66 public SkipMe(Appendable out) {
71 protected boolean requireReference(String cn) {
72 if (cn.contains("CompareVMs")) {
75 return super.requireReference(cn);
80 SkipMe sm = new SkipMe(sb);
81 sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
82 clazz.getName().replace('.', '/')
85 ScriptEngineManager sem = new ScriptEngineManager();
86 ScriptEngine js = sem.getEngineByExtension("js");
88 Object res = js.eval(sb.toString());
89 Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
95 public static Object[] create(Class<?> clazz) {
96 Method[] arr = clazz.getMethods();
97 Object[] ret = new Object[3 * arr.length];
99 for (Method m : arr) {
100 Compare c = m.getAnnotation(Compare.class);
104 final CompareVMs real = new CompareVMs(m, false);
105 final CompareVMs js = new CompareVMs(m, true);
108 ret[cnt++] = new CompareVMs(m, real, js);
110 Object[] r = new Object[cnt];
111 for (int i = 0; i < cnt; i++) {
117 @Test public void runTest() throws Throwable {
119 Object v1 = first.value;
120 Object v2 = second.value;
121 if (v1 instanceof Number) {
122 v1 = ((Number)v1).doubleValue();
124 Assert.assertEquals(v1, v2, "Comparing results");
128 compileTheCode(m.getDeclaringClass());
129 Object vm = code.invokeFunction("bck2brwsr");
130 Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
131 value = code.invokeMethod(inst, m.getName() + "__I");
132 } catch (Exception ex) {
133 throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
136 value = m.invoke(m.getDeclaringClass().newInstance());
142 public String getTestName() {
144 return m.getName() + "[Compare]";
146 return m.getName() + (js ? "[JavaScript]" : "[Java]");