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.bck2brwsr.vmtest;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.lang.reflect.Method;
26 import java.util.Enumeration;
28 import java.util.WeakHashMap;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import javax.script.Invocable;
32 import javax.script.ScriptEngine;
33 import javax.script.ScriptEngineManager;
34 import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
35 import org.apidesign.vm4brwsr.Bck2Brwsr;
36 import org.testng.Assert;
37 import org.testng.ITest;
38 import org.testng.annotations.BeforeTest;
39 import org.testng.annotations.Factory;
40 import org.testng.annotations.Test;
42 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
43 * in provided class and builds set of tests that compare the computations
44 * in real as well as JavaScript virtual machines. Use as:<pre>
45 * {@code @}{@link Factory} public static create() {
46 * return @{link VMTest}.{@link #create(YourClass.class);
49 * @author Jaroslav Tulach <jtulach@netbeans.org>
51 public final class VMTest implements ITest {
52 private final Run first, second;
53 private final Method m;
55 private VMTest(Method m, Run first, Run second) {
61 /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
62 * instances of tests. Each instance runs the test in different virtual
63 * machine and at the end they compare the results.
65 * @param clazz the class to inspect
66 * @return the set of created tests
68 public static Object[] create(Class<?> clazz) {
69 Method[] arr = clazz.getMethods();
70 Object[] ret = new Object[5 * arr.length];
72 for (Method m : arr) {
73 Compare c = m.getAnnotation(Compare.class);
77 final Run real = new Run(m, 0);
78 final Run js = new Run(m, 1);
79 final Run brwsr = new Run(m, 2);
83 ret[cnt++] = new VMTest(m, real, js);
84 ret[cnt++] = new VMTest(m, real, brwsr);
86 Object[] r = new Object[cnt];
87 for (int i = 0; i < cnt; i++) {
93 /** Test that compares the previous results.
96 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
97 Object v1 = first.value;
98 Object v2 = second.value;
99 if (v1 instanceof Number) {
100 v1 = ((Number)v1).doubleValue();
102 Assert.assertEquals(v2, v1, "Comparing results");
106 * @return name of the tested method followed by a suffix
109 public String getTestName() {
110 return m.getName() + "[Compare " + second.typeName() + "]";
113 /** Helper method that inspects the classpath and loads given resource
114 * (usually a class file). Used while running tests in Rhino.
116 * @param name resource name to find
117 * @return the array of bytes in the given resource
118 * @throws IOException I/O in case something goes wrong
120 public static byte[] read(String name) throws IOException {
122 Enumeration<URL> en = VMTest.class.getClassLoader().getResources(name);
123 while (en.hasMoreElements()) {
124 u = en.nextElement();
127 throw new IOException("Can't find " + name);
129 try (InputStream is = u.openStream()) {
131 arr = new byte[is.available()];
133 while (offset < arr.length) {
134 int len = is.read(arr, offset, arr.length - offset);
136 throw new IOException("Can't read " + name);
144 public static final class Run implements ITest {
145 private final Method m;
146 private final int type;
148 private Invocable code;
149 private CharSequence codeSeq;
150 private static final Map<Class,Object[]> compiled = new WeakHashMap<>();
153 private Run(Method m, int type) {
158 } catch (Throwable ex) {
159 Logger.getLogger(VMTest.class.getName()).log(Level.SEVERE, null, ex);
163 private void compileTheCode(Class<?> clazz) throws Exception {
164 final Object[] data = compiled.get(clazz);
166 code = (Invocable) data[0];
167 codeSeq = (CharSequence) data[1];
170 StringBuilder sb = new StringBuilder();
171 Bck2Brwsr.generate(sb, VMTest.class.getClassLoader());
173 ScriptEngineManager sem = new ScriptEngineManager();
174 ScriptEngine mach = sem.getEngineByExtension("js");
177 "\nvar vm = bck2brwsr(org.apidesign.bck2brwsr.vmtest.VMTest.read);"
178 + "\nfunction initVM() { return vm; };"
181 Object res = mach.eval(sb.toString());
182 Assert.assertTrue(mach instanceof Invocable, "It is invocable object: " + res);
183 code = (Invocable) mach;
185 compiled.put(clazz, new Object[] { code, codeSeq });
188 private void initialize() throws Throwable {
190 compileTheCode(m.getDeclaringClass());
191 Object vm = code.invokeFunction("initVM");
192 inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
193 } else if (type == 2) {
194 inst = addBrowserMethod(m.getDeclaringClass(), m.getName());
198 @Test(groups = "run") public void executeCode() throws Throwable {
201 value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
202 } catch (Exception ex) {
203 throw new AssertionError(dumpJS(codeSeq)).initCause(ex);
205 } else if (type == 2) {
206 Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
208 value = c.toString();
210 value = m.invoke(m.getDeclaringClass().newInstance());
214 public String getTestName() {
215 return m.getName() + "[" + typeName() + "]";
218 final String typeName() {
220 case 0: return "Java";
221 case 1: return "JavaScript";
222 case 2: return "Browser";
223 default: return "Unknown type " + type;
227 private static String computeSignature(Method m) {
228 StringBuilder sb = new StringBuilder();
229 appendType(sb, m.getReturnType());
230 for (Class<?> c : m.getParameterTypes()) {
233 return sb.toString();
236 private static void appendType(StringBuilder sb, Class<?> t) {
241 if (t.isPrimitive()) {
243 if (t == int.class) {
246 if (t == short.class) {
249 if (t == byte.class) {
252 if (t == boolean.class) {
255 if (t == long.class) {
258 if (t == float.class) {
261 if (t == double.class) {
264 assert ch != -1 : "Unknown primitive type " + t;
270 appendType(sb, t.getComponentType());
274 sb.append(t.getName().replace('.', '_'));
279 static StringBuilder dumpJS(CharSequence sb) throws IOException {
280 File f = File.createTempFile("execution", ".js");
281 try (FileWriter w = new FileWriter(f)) {
284 return new StringBuilder(f.getPath());
287 private static Bck2BrwsrLauncher launcher;
289 private static synchronized Bck2BrwsrLauncher.MethodInvocation addBrowserMethod(
290 Class<?> clazz, String name
292 if (launcher == null) {
293 launcher = new Bck2BrwsrLauncher();
294 launcher.setTimeout(5000);
296 return launcher.addMethod(clazz, name);
299 private static void execBrowser() throws Exception {
300 Bck2BrwsrLauncher l = clearBrowser();
305 private static synchronized Bck2BrwsrLauncher clearBrowser() {
306 Bck2BrwsrLauncher l = launcher;