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 Run first, second;
40 private final Method m;
42 private CompareVMs(Method m, Run first, Run second) {
48 public static Object[] create(Class<?> clazz) {
49 Method[] arr = clazz.getMethods();
50 Object[] ret = new Object[3 * arr.length];
52 for (Method m : arr) {
53 Compare c = m.getAnnotation(Compare.class);
57 final Run real = new Run(m, false);
58 final Run js = new Run(m, true);
61 ret[cnt++] = new CompareVMs(m, real, js);
63 Object[] r = new Object[cnt];
64 for (int i = 0; i < cnt; i++) {
70 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
71 Object v1 = first.value;
72 Object v2 = second.value;
73 if (v1 instanceof Number) {
74 v1 = ((Number)v1).doubleValue();
76 Assert.assertEquals(v2, v1, "Comparing results");
80 public String getTestName() {
81 return m.getName() + "[Compare]";
84 public static final class Run implements ITest {
85 private final Method m;
86 private final boolean js;
88 private static Invocable code;
89 private static CharSequence codeSeq;
91 private Run(Method m, boolean js) {
96 private static void compileTheCode(Class<?> clazz) throws Exception {
100 StringBuilder sb = new StringBuilder();
101 class SkipMe extends GenJS {
103 public SkipMe(Appendable out) {
108 protected boolean requireReference(String cn) {
109 if (cn.contains("CompareVMs")) {
112 return super.requireReference(cn);
115 SkipMe sm = new SkipMe(sb);
116 sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
117 clazz.getName().replace('.', '/')));
119 ScriptEngineManager sem = new ScriptEngineManager();
120 ScriptEngine js = sem.getEngineByExtension("js");
122 Object res = js.eval(sb.toString());
123 Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
124 code = (Invocable) js;
128 @Test(groups = "run") public void executeCode() throws Throwable {
131 compileTheCode(m.getDeclaringClass());
132 Object inst = code.invokeFunction(m.getDeclaringClass().getName().replace('.', '_'), false);
133 value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
134 } catch (Exception ex) {
135 throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
138 value = m.invoke(m.getDeclaringClass().newInstance());
142 public String getTestName() {
143 return m.getName() + (js ? "[JavaScript]" : "[Java]");
146 private static String computeSignature(Method m) {
147 StringBuilder sb = new StringBuilder();
148 appendType(sb, m.getReturnType());
149 for (Class<?> c : m.getParameterTypes()) {
152 return sb.toString();
155 private static void appendType(StringBuilder sb, Class<?> t) {
160 if (t.isPrimitive()) {
162 if (t == int.class) {
165 if (t == short.class) {
168 if (t == byte.class) {
171 if (t == boolean.class) {
174 if (t == long.class) {
177 if (t == float.class) {
180 if (t == double.class) {
183 assert ch != -1 : "Unknown primitive type " + t;
189 appendType(sb, t.getComponentType());
193 sb.append(t.getName().replace('.', '_'));