Using the Console for invoking VMTests in Rhino. All tests are passing now.
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 org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
33 import org.testng.Assert;
34 import org.testng.ITest;
35 import org.testng.annotations.Factory;
36 import org.testng.annotations.Test;
38 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
39 * in provided class and builds set of tests that compare the computations
40 * in real as well as JavaScript virtual machines. Use as:<pre>
41 * {@code @}{@link Factory} public static create() {
42 * return @{link VMTest}.{@link #create(YourClass.class);
45 * @author Jaroslav Tulach <jtulach@netbeans.org>
47 public final class VMTest implements ITest {
48 private static final Launcher JS = new Launcher("js");
49 private static final Launcher BROWSER = new Launcher();
51 private final Run first, second;
52 private final Method m;
54 private VMTest(Method m, Run first, Run second) {
60 /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
61 * instances of tests. Each instance runs the test in different virtual
62 * machine and at the end they compare the results.
64 * @param clazz the class to inspect
65 * @return the set of created tests
67 public static Object[] create(Class<?> clazz) {
68 Method[] arr = clazz.getMethods();
69 Object[] ret = new Object[5 * arr.length];
71 for (Method m : arr) {
72 Compare c = m.getAnnotation(Compare.class);
76 final Run real = new Run(m, 0);
77 final Run js = new Run(m, 1);
78 final Run brwsr = new Run(m, 2);
82 ret[cnt++] = new VMTest(m, real, js);
83 ret[cnt++] = new VMTest(m, real, brwsr);
85 Object[] r = new Object[cnt];
86 for (int i = 0; i < cnt; i++) {
92 /** Test that compares the previous results.
95 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
96 Object v1 = first.value;
97 Object v2 = second.value;
103 Assert.assertEquals(v2, v1, "Comparing results");
107 * @return name of the tested method followed by a suffix
110 public String getTestName() {
111 return m.getName() + "[Compare " + second.typeName() + "]";
114 /** Helper method that inspects the classpath and loads given resource
115 * (usually a class file). Used while running tests in Rhino.
117 * @param name resource name to find
118 * @return the array of bytes in the given resource
119 * @throws IOException I/O in case something goes wrong
121 public static byte[] read(String name) throws IOException {
123 Enumeration<URL> en = VMTest.class.getClassLoader().getResources(name);
124 while (en.hasMoreElements()) {
125 u = en.nextElement();
128 throw new IOException("Can't find " + name);
130 try (InputStream is = u.openStream()) {
132 arr = new byte[is.available()];
134 while (offset < arr.length) {
135 int len = is.read(arr, offset, arr.length - offset);
137 throw new IOException("Can't read " + name);
145 public static final class Run implements ITest {
146 private final Method m;
147 private final int type;
149 private Invocable code;
150 private CharSequence codeSeq;
151 private static final Map<Class,Object[]> compiled = new WeakHashMap<>();
154 private Run(Method m, int type) {
159 } catch (Throwable ex) {
160 Logger.getLogger(VMTest.class.getName()).log(Level.SEVERE, null, ex);
164 private void initialize() throws Throwable {
166 inst = JS.addMethod(m.getDeclaringClass(), m.getName());
169 inst = BROWSER.addMethod(m.getDeclaringClass(), m.getName());
173 @Test(groups = "run") public void executeCode() throws Throwable {
175 Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
177 value = c.toString();
178 } else if (type == 2) {
179 Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
181 value = c.toString();
183 value = m.invoke(m.getDeclaringClass().newInstance());
187 public String getTestName() {
188 return m.getName() + "[" + typeName() + "]";
191 final String typeName() {
193 case 0: return "Java";
194 case 1: return "JavaScript";
195 case 2: return "Browser";
196 default: return "Unknown type " + type;
200 private static String computeSignature(Method m) {
201 StringBuilder sb = new StringBuilder();
202 appendType(sb, m.getReturnType());
203 for (Class<?> c : m.getParameterTypes()) {
206 return sb.toString();
209 private static void appendType(StringBuilder sb, Class<?> t) {
214 if (t.isPrimitive()) {
216 if (t == int.class) {
219 if (t == short.class) {
222 if (t == byte.class) {
225 if (t == boolean.class) {
228 if (t == long.class) {
231 if (t == float.class) {
234 if (t == double.class) {
237 assert ch != -1 : "Unknown primitive type " + t;
243 appendType(sb, t.getComponentType());
247 sb.append(t.getName().replace('.', '_'));
252 static StringBuilder dumpJS(CharSequence sb) throws IOException {
253 File f = File.createTempFile("execution", ".js");
254 try (FileWriter w = new FileWriter(f)) {
257 return new StringBuilder(f.getPath());