Console width expanded to 100%. More structured output messages.
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;
22 import java.util.WeakHashMap;
23 import javax.script.Invocable;
24 import javax.script.ScriptContext;
25 import javax.script.ScriptEngine;
26 import javax.script.ScriptEngineManager;
27 import org.testng.Assert;
28 import org.testng.ITest;
29 import org.testng.annotations.Factory;
30 import org.testng.annotations.Test;
32 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
33 * in provided class and builds set of tests that compare the computations
34 * in real as well as JavaScript virtual machines. Use as:<pre>
35 * {@code @}{@link Factory} public static create() {
36 * return @{link CompareVMs}.{@link #create(YourClass.class);
39 * @author Jaroslav Tulach <jtulach@netbeans.org>
41 public final class CompareVMs implements ITest {
42 private final Run first, second;
43 private final Method m;
45 private CompareVMs(Method m, Run first, Run second) {
51 public static Object[] create(Class<?> clazz) {
52 Method[] arr = clazz.getMethods();
53 Object[] ret = new Object[3 * arr.length];
55 for (Method m : arr) {
56 Compare c = m.getAnnotation(Compare.class);
60 final Run real = new Run(m, false);
61 final Run js = new Run(m, true);
64 ret[cnt++] = new CompareVMs(m, real, js);
66 Object[] r = new Object[cnt];
67 for (int i = 0; i < cnt; i++) {
73 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
74 Object v1 = first.value;
75 Object v2 = second.value;
76 if (v1 instanceof Number) {
77 v1 = ((Number)v1).doubleValue();
79 Assert.assertEquals(v2, v1, "Comparing results");
83 public String getTestName() {
84 return m.getName() + "[Compare]";
87 public static final class Run implements ITest {
88 private final Method m;
89 private final boolean js;
91 private Invocable code;
92 private CharSequence codeSeq;
93 private static final Map<Class,Object[]> compiled = new WeakHashMap<Class,Object[]>();
95 private Run(Method m, boolean js) {
100 private void compileTheCode(Class<?> clazz) throws Exception {
101 final Object[] data = compiled.get(clazz);
103 code = (Invocable) data[0];
104 codeSeq = (CharSequence) data[1];
107 StringBuilder sb = new StringBuilder();
108 Bck2Brwsr.generate(sb, CompareVMs.class.getClassLoader());
110 ScriptEngineManager sem = new ScriptEngineManager();
111 ScriptEngine js = sem.getEngineByExtension("js");
112 js.getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
114 sb.append("\nfunction initVM() {"
115 + "\n return bck2brwsr("
116 + "\n function(name) { return loader.get(name);}"
120 Object res = js.eval(sb.toString());
121 Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
122 code = (Invocable) js;
124 compiled.put(clazz, new Object[] { code, codeSeq });
127 @Test(groups = "run") public void executeCode() throws Throwable {
130 compileTheCode(m.getDeclaringClass());
131 Object vm = code.invokeFunction("initVM");
132 Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
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('.', '_'));