jaroslav@273
|
1 |
/**
|
jaroslav@273
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@273
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@273
|
4 |
*
|
jaroslav@273
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@273
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@273
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@273
|
8 |
*
|
jaroslav@273
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@273
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@273
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@273
|
12 |
* GNU General Public License for more details.
|
jaroslav@273
|
13 |
*
|
jaroslav@273
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@273
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@273
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@273
|
17 |
*/
|
jaroslav@346
|
18 |
package org.apidesign.bck2brwsr.vmtest;
|
jaroslav@273
|
19 |
|
jaroslav@346
|
20 |
import java.io.File;
|
jaroslav@346
|
21 |
import java.io.FileWriter;
|
jaroslav@346
|
22 |
import java.io.IOException;
|
jaroslav@346
|
23 |
import java.io.InputStream;
|
jaroslav@273
|
24 |
import java.lang.reflect.Method;
|
jaroslav@346
|
25 |
import java.net.URL;
|
jaroslav@346
|
26 |
import java.util.Enumeration;
|
jaroslav@297
|
27 |
import java.util.Map;
|
jaroslav@297
|
28 |
import java.util.WeakHashMap;
|
jaroslav@273
|
29 |
import javax.script.Invocable;
|
jaroslav@273
|
30 |
import javax.script.ScriptEngine;
|
jaroslav@273
|
31 |
import javax.script.ScriptEngineManager;
|
jaroslav@349
|
32 |
import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
|
jaroslav@346
|
33 |
import org.apidesign.vm4brwsr.Bck2Brwsr;
|
jaroslav@273
|
34 |
import org.testng.Assert;
|
jaroslav@273
|
35 |
import org.testng.ITest;
|
jaroslav@273
|
36 |
import org.testng.annotations.Factory;
|
jaroslav@273
|
37 |
import org.testng.annotations.Test;
|
jaroslav@273
|
38 |
|
jaroslav@273
|
39 |
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
|
jaroslav@273
|
40 |
* in provided class and builds set of tests that compare the computations
|
jaroslav@273
|
41 |
* in real as well as JavaScript virtual machines. Use as:<pre>
|
jaroslav@273
|
42 |
* {@code @}{@link Factory} public static create() {
|
jaroslav@346
|
43 |
* return @{link VMTest}.{@link #create(YourClass.class);
|
jaroslav@273
|
44 |
* }</pre>
|
jaroslav@273
|
45 |
*
|
jaroslav@273
|
46 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@273
|
47 |
*/
|
jaroslav@346
|
48 |
public final class VMTest implements ITest {
|
jaroslav@280
|
49 |
private final Run first, second;
|
jaroslav@273
|
50 |
private final Method m;
|
jaroslav@273
|
51 |
|
jaroslav@346
|
52 |
private VMTest(Method m, Run first, Run second) {
|
jaroslav@273
|
53 |
this.first = first;
|
jaroslav@273
|
54 |
this.second = second;
|
jaroslav@273
|
55 |
this.m = m;
|
jaroslav@273
|
56 |
}
|
jaroslav@273
|
57 |
|
jaroslav@346
|
58 |
/** Inspects <code>clazz</code> and for each {@lik Compare} method creates
|
jaroslav@347
|
59 |
* instances of tests. Each instance runs the test in different virtual
|
jaroslav@346
|
60 |
* machine and at the end they compare the results.
|
jaroslav@346
|
61 |
*
|
jaroslav@346
|
62 |
* @param clazz the class to inspect
|
jaroslav@346
|
63 |
* @return the set of created tests
|
jaroslav@346
|
64 |
*/
|
jaroslav@273
|
65 |
public static Object[] create(Class<?> clazz) {
|
jaroslav@273
|
66 |
Method[] arr = clazz.getMethods();
|
jaroslav@349
|
67 |
Object[] ret = new Object[5 * arr.length];
|
jaroslav@273
|
68 |
int cnt = 0;
|
jaroslav@273
|
69 |
for (Method m : arr) {
|
jaroslav@273
|
70 |
Compare c = m.getAnnotation(Compare.class);
|
jaroslav@273
|
71 |
if (c == null) {
|
jaroslav@273
|
72 |
continue;
|
jaroslav@273
|
73 |
}
|
jaroslav@349
|
74 |
final Run real = new Run(m, 0);
|
jaroslav@349
|
75 |
final Run js = new Run(m, 1);
|
jaroslav@349
|
76 |
final Run brwsr = new Run(m, 2);
|
jaroslav@273
|
77 |
ret[cnt++] = real;
|
jaroslav@273
|
78 |
ret[cnt++] = js;
|
jaroslav@349
|
79 |
ret[cnt++] = brwsr;
|
jaroslav@346
|
80 |
ret[cnt++] = new VMTest(m, real, js);
|
jaroslav@349
|
81 |
ret[cnt++] = new VMTest(m, real, brwsr);
|
jaroslav@273
|
82 |
}
|
jaroslav@273
|
83 |
Object[] r = new Object[cnt];
|
jaroslav@273
|
84 |
for (int i = 0; i < cnt; i++) {
|
jaroslav@273
|
85 |
r[i] = ret[i];
|
jaroslav@273
|
86 |
}
|
jaroslav@273
|
87 |
return r;
|
jaroslav@273
|
88 |
}
|
jaroslav@346
|
89 |
|
jaroslav@346
|
90 |
/** Test that compares the previous results.
|
jaroslav@346
|
91 |
* @throws Throwable
|
jaroslav@346
|
92 |
*/
|
jaroslav@280
|
93 |
@Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
|
jaroslav@280
|
94 |
Object v1 = first.value;
|
jaroslav@280
|
95 |
Object v2 = second.value;
|
jaroslav@280
|
96 |
if (v1 instanceof Number) {
|
jaroslav@280
|
97 |
v1 = ((Number)v1).doubleValue();
|
jaroslav@280
|
98 |
}
|
jaroslav@296
|
99 |
Assert.assertEquals(v2, v1, "Comparing results");
|
jaroslav@280
|
100 |
}
|
jaroslav@280
|
101 |
|
jaroslav@346
|
102 |
/** Test name.
|
jaroslav@346
|
103 |
* @return name of the tested method followed by a suffix
|
jaroslav@346
|
104 |
*/
|
jaroslav@280
|
105 |
@Override
|
jaroslav@280
|
106 |
public String getTestName() {
|
jaroslav@349
|
107 |
return m.getName() + "[Compare " + second.typeName() + "]";
|
jaroslav@280
|
108 |
}
|
jaroslav@346
|
109 |
|
jaroslav@346
|
110 |
/** Helper method that inspects the classpath and loads given resource
|
jaroslav@346
|
111 |
* (usually a class file). Used while running tests in Rhino.
|
jaroslav@346
|
112 |
*
|
jaroslav@346
|
113 |
* @param name resource name to find
|
jaroslav@346
|
114 |
* @return the array of bytes in the given resource
|
jaroslav@346
|
115 |
* @throws IOException I/O in case something goes wrong
|
jaroslav@346
|
116 |
*/
|
jaroslav@346
|
117 |
public static byte[] read(String name) throws IOException {
|
jaroslav@346
|
118 |
URL u = null;
|
jaroslav@346
|
119 |
Enumeration<URL> en = VMTest.class.getClassLoader().getResources(name);
|
jaroslav@346
|
120 |
while (en.hasMoreElements()) {
|
jaroslav@346
|
121 |
u = en.nextElement();
|
jaroslav@346
|
122 |
}
|
jaroslav@346
|
123 |
if (u == null) {
|
jaroslav@346
|
124 |
throw new IOException("Can't find " + name);
|
jaroslav@346
|
125 |
}
|
jaroslav@346
|
126 |
try (InputStream is = u.openStream()) {
|
jaroslav@346
|
127 |
byte[] arr;
|
jaroslav@346
|
128 |
arr = new byte[is.available()];
|
jaroslav@346
|
129 |
int offset = 0;
|
jaroslav@346
|
130 |
while (offset < arr.length) {
|
jaroslav@346
|
131 |
int len = is.read(arr, offset, arr.length - offset);
|
jaroslav@346
|
132 |
if (len == -1) {
|
jaroslav@346
|
133 |
throw new IOException("Can't read " + name);
|
jaroslav@346
|
134 |
}
|
jaroslav@346
|
135 |
offset += len;
|
jaroslav@346
|
136 |
}
|
jaroslav@346
|
137 |
return arr;
|
jaroslav@346
|
138 |
}
|
jaroslav@346
|
139 |
}
|
jaroslav@346
|
140 |
|
jaroslav@280
|
141 |
public static final class Run implements ITest {
|
jaroslav@280
|
142 |
private final Method m;
|
jaroslav@349
|
143 |
private final int type;
|
jaroslav@280
|
144 |
Object value;
|
jaroslav@297
|
145 |
private Invocable code;
|
jaroslav@297
|
146 |
private CharSequence codeSeq;
|
jaroslav@347
|
147 |
private static final Map<Class,Object[]> compiled = new WeakHashMap<>();
|
jaroslav@280
|
148 |
|
jaroslav@349
|
149 |
private Run(Method m, int type) {
|
jaroslav@280
|
150 |
this.m = m;
|
jaroslav@349
|
151 |
this.type = type;
|
jaroslav@280
|
152 |
}
|
jaroslav@280
|
153 |
|
jaroslav@297
|
154 |
private void compileTheCode(Class<?> clazz) throws Exception {
|
jaroslav@297
|
155 |
final Object[] data = compiled.get(clazz);
|
jaroslav@297
|
156 |
if (data != null) {
|
jaroslav@297
|
157 |
code = (Invocable) data[0];
|
jaroslav@297
|
158 |
codeSeq = (CharSequence) data[1];
|
jaroslav@280
|
159 |
return;
|
jaroslav@273
|
160 |
}
|
jaroslav@280
|
161 |
StringBuilder sb = new StringBuilder();
|
jaroslav@346
|
162 |
Bck2Brwsr.generate(sb, VMTest.class.getClassLoader());
|
jaroslav@280
|
163 |
|
jaroslav@280
|
164 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@347
|
165 |
ScriptEngine mach = sem.getEngineByExtension("js");
|
jaroslav@303
|
166 |
|
jaroslav@347
|
167 |
sb.append(
|
jaroslav@347
|
168 |
"\nvar vm = bck2brwsr(org.apidesign.bck2brwsr.vmtest.VMTest.read);"
|
jaroslav@347
|
169 |
+ "\nfunction initVM() { return vm; };"
|
jaroslav@347
|
170 |
+ "\n");
|
jaroslav@280
|
171 |
|
jaroslav@347
|
172 |
Object res = mach.eval(sb.toString());
|
jaroslav@347
|
173 |
Assert.assertTrue(mach instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@347
|
174 |
code = (Invocable) mach;
|
jaroslav@280
|
175 |
codeSeq = sb;
|
jaroslav@297
|
176 |
compiled.put(clazz, new Object[] { code, codeSeq });
|
jaroslav@280
|
177 |
}
|
jaroslav@280
|
178 |
|
jaroslav@280
|
179 |
@Test(groups = "run") public void executeCode() throws Throwable {
|
jaroslav@349
|
180 |
if (type == 1) {
|
jaroslav@273
|
181 |
try {
|
jaroslav@273
|
182 |
compileTheCode(m.getDeclaringClass());
|
jaroslav@303
|
183 |
Object vm = code.invokeFunction("initVM");
|
jaroslav@276
|
184 |
Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
|
jaroslav@296
|
185 |
value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
|
jaroslav@273
|
186 |
} catch (Exception ex) {
|
jaroslav@346
|
187 |
throw new AssertionError(dumpJS(codeSeq)).initCause(ex);
|
jaroslav@273
|
188 |
}
|
jaroslav@349
|
189 |
} else if (type == 2) {
|
jaroslav@349
|
190 |
Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
|
jaroslav@349
|
191 |
l.setTimeout(5000);
|
jaroslav@349
|
192 |
Bck2BrwsrLauncher.MethodInvocation c = l.addMethod(m.getDeclaringClass(), m.getName());
|
jaroslav@349
|
193 |
l.execute();
|
jaroslav@349
|
194 |
value = c.toString();
|
jaroslav@273
|
195 |
} else {
|
jaroslav@273
|
196 |
value = m.invoke(m.getDeclaringClass().newInstance());
|
jaroslav@273
|
197 |
}
|
jaroslav@273
|
198 |
}
|
jaroslav@280
|
199 |
@Override
|
jaroslav@280
|
200 |
public String getTestName() {
|
jaroslav@349
|
201 |
return m.getName() + "[" + typeName() + "]";
|
jaroslav@349
|
202 |
}
|
jaroslav@349
|
203 |
|
jaroslav@349
|
204 |
final String typeName() {
|
jaroslav@349
|
205 |
switch (type) {
|
jaroslav@349
|
206 |
case 0: return "Java";
|
jaroslav@349
|
207 |
case 1: return "JavaScript";
|
jaroslav@349
|
208 |
case 2: return "Browser";
|
jaroslav@349
|
209 |
default: return "Unknown type " + type;
|
jaroslav@349
|
210 |
}
|
jaroslav@273
|
211 |
}
|
jaroslav@296
|
212 |
|
jaroslav@296
|
213 |
private static String computeSignature(Method m) {
|
jaroslav@296
|
214 |
StringBuilder sb = new StringBuilder();
|
jaroslav@296
|
215 |
appendType(sb, m.getReturnType());
|
jaroslav@296
|
216 |
for (Class<?> c : m.getParameterTypes()) {
|
jaroslav@296
|
217 |
appendType(sb, c);
|
jaroslav@296
|
218 |
}
|
jaroslav@296
|
219 |
return sb.toString();
|
jaroslav@296
|
220 |
}
|
jaroslav@296
|
221 |
|
jaroslav@296
|
222 |
private static void appendType(StringBuilder sb, Class<?> t) {
|
jaroslav@296
|
223 |
if (t == null) {
|
jaroslav@296
|
224 |
sb.append('V');
|
jaroslav@296
|
225 |
return;
|
jaroslav@296
|
226 |
}
|
jaroslav@296
|
227 |
if (t.isPrimitive()) {
|
jaroslav@296
|
228 |
int ch = -1;
|
jaroslav@296
|
229 |
if (t == int.class) {
|
jaroslav@296
|
230 |
ch = 'I';
|
jaroslav@296
|
231 |
}
|
jaroslav@296
|
232 |
if (t == short.class) {
|
jaroslav@296
|
233 |
ch = 'S';
|
jaroslav@296
|
234 |
}
|
jaroslav@296
|
235 |
if (t == byte.class) {
|
jaroslav@296
|
236 |
ch = 'B';
|
jaroslav@296
|
237 |
}
|
jaroslav@296
|
238 |
if (t == boolean.class) {
|
jaroslav@296
|
239 |
ch = 'Z';
|
jaroslav@296
|
240 |
}
|
jaroslav@296
|
241 |
if (t == long.class) {
|
jaroslav@296
|
242 |
ch = 'J';
|
jaroslav@296
|
243 |
}
|
jaroslav@296
|
244 |
if (t == float.class) {
|
jaroslav@296
|
245 |
ch = 'F';
|
jaroslav@296
|
246 |
}
|
jaroslav@296
|
247 |
if (t == double.class) {
|
jaroslav@296
|
248 |
ch = 'D';
|
jaroslav@296
|
249 |
}
|
jaroslav@296
|
250 |
assert ch != -1 : "Unknown primitive type " + t;
|
jaroslav@296
|
251 |
sb.append((char)ch);
|
jaroslav@296
|
252 |
return;
|
jaroslav@296
|
253 |
}
|
jaroslav@296
|
254 |
if (t.isArray()) {
|
jaroslav@296
|
255 |
sb.append("_3");
|
jaroslav@296
|
256 |
appendType(sb, t.getComponentType());
|
jaroslav@296
|
257 |
return;
|
jaroslav@296
|
258 |
}
|
jaroslav@296
|
259 |
sb.append('L');
|
jaroslav@296
|
260 |
sb.append(t.getName().replace('.', '_'));
|
jaroslav@296
|
261 |
sb.append("_2");
|
jaroslav@296
|
262 |
}
|
jaroslav@273
|
263 |
}
|
jaroslav@346
|
264 |
|
jaroslav@346
|
265 |
static StringBuilder dumpJS(CharSequence sb) throws IOException {
|
jaroslav@346
|
266 |
File f = File.createTempFile("execution", ".js");
|
jaroslav@347
|
267 |
try (FileWriter w = new FileWriter(f)) {
|
jaroslav@347
|
268 |
w.append(sb);
|
jaroslav@347
|
269 |
}
|
jaroslav@346
|
270 |
return new StringBuilder(f.getPath());
|
jaroslav@346
|
271 |
}
|
jaroslav@273
|
272 |
}
|