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@346
|
32 |
import org.apidesign.vm4brwsr.Bck2Brwsr;
|
jaroslav@273
|
33 |
import org.testng.Assert;
|
jaroslav@273
|
34 |
import org.testng.ITest;
|
jaroslav@273
|
35 |
import org.testng.annotations.Factory;
|
jaroslav@273
|
36 |
import org.testng.annotations.Test;
|
jaroslav@273
|
37 |
|
jaroslav@273
|
38 |
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
|
jaroslav@273
|
39 |
* in provided class and builds set of tests that compare the computations
|
jaroslav@273
|
40 |
* in real as well as JavaScript virtual machines. Use as:<pre>
|
jaroslav@273
|
41 |
* {@code @}{@link Factory} public static create() {
|
jaroslav@346
|
42 |
* return @{link VMTest}.{@link #create(YourClass.class);
|
jaroslav@273
|
43 |
* }</pre>
|
jaroslav@273
|
44 |
*
|
jaroslav@273
|
45 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@273
|
46 |
*/
|
jaroslav@346
|
47 |
public final class VMTest implements ITest {
|
jaroslav@280
|
48 |
private final Run first, second;
|
jaroslav@273
|
49 |
private final Method m;
|
jaroslav@273
|
50 |
|
jaroslav@346
|
51 |
private VMTest(Method m, Run first, Run second) {
|
jaroslav@273
|
52 |
this.first = first;
|
jaroslav@273
|
53 |
this.second = second;
|
jaroslav@273
|
54 |
this.m = m;
|
jaroslav@273
|
55 |
}
|
jaroslav@273
|
56 |
|
jaroslav@346
|
57 |
/** Inspects <code>clazz</code> and for each {@lik Compare} method creates
|
jaroslav@346
|
58 |
* instances of tests. Each insteance runs the test in different virtual
|
jaroslav@346
|
59 |
* machine and at the end they compare the results.
|
jaroslav@346
|
60 |
*
|
jaroslav@346
|
61 |
* @param clazz the class to inspect
|
jaroslav@346
|
62 |
* @return the set of created tests
|
jaroslav@346
|
63 |
*/
|
jaroslav@273
|
64 |
public static Object[] create(Class<?> clazz) {
|
jaroslav@273
|
65 |
Method[] arr = clazz.getMethods();
|
jaroslav@273
|
66 |
Object[] ret = new Object[3 * arr.length];
|
jaroslav@273
|
67 |
int cnt = 0;
|
jaroslav@273
|
68 |
for (Method m : arr) {
|
jaroslav@273
|
69 |
Compare c = m.getAnnotation(Compare.class);
|
jaroslav@273
|
70 |
if (c == null) {
|
jaroslav@273
|
71 |
continue;
|
jaroslav@273
|
72 |
}
|
jaroslav@280
|
73 |
final Run real = new Run(m, false);
|
jaroslav@280
|
74 |
final Run js = new Run(m, true);
|
jaroslav@273
|
75 |
ret[cnt++] = real;
|
jaroslav@273
|
76 |
ret[cnt++] = js;
|
jaroslav@346
|
77 |
ret[cnt++] = new VMTest(m, real, js);
|
jaroslav@273
|
78 |
}
|
jaroslav@273
|
79 |
Object[] r = new Object[cnt];
|
jaroslav@273
|
80 |
for (int i = 0; i < cnt; i++) {
|
jaroslav@273
|
81 |
r[i] = ret[i];
|
jaroslav@273
|
82 |
}
|
jaroslav@273
|
83 |
return r;
|
jaroslav@273
|
84 |
}
|
jaroslav@346
|
85 |
|
jaroslav@346
|
86 |
/** Test that compares the previous results.
|
jaroslav@346
|
87 |
* @throws Throwable
|
jaroslav@346
|
88 |
*/
|
jaroslav@280
|
89 |
@Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
|
jaroslav@280
|
90 |
Object v1 = first.value;
|
jaroslav@280
|
91 |
Object v2 = second.value;
|
jaroslav@280
|
92 |
if (v1 instanceof Number) {
|
jaroslav@280
|
93 |
v1 = ((Number)v1).doubleValue();
|
jaroslav@280
|
94 |
}
|
jaroslav@296
|
95 |
Assert.assertEquals(v2, v1, "Comparing results");
|
jaroslav@280
|
96 |
}
|
jaroslav@280
|
97 |
|
jaroslav@346
|
98 |
/** Test name.
|
jaroslav@346
|
99 |
* @return name of the tested method followed by a suffix
|
jaroslav@346
|
100 |
*/
|
jaroslav@280
|
101 |
@Override
|
jaroslav@280
|
102 |
public String getTestName() {
|
jaroslav@280
|
103 |
return m.getName() + "[Compare]";
|
jaroslav@280
|
104 |
}
|
jaroslav@346
|
105 |
|
jaroslav@346
|
106 |
/** Helper method that inspects the classpath and loads given resource
|
jaroslav@346
|
107 |
* (usually a class file). Used while running tests in Rhino.
|
jaroslav@346
|
108 |
*
|
jaroslav@346
|
109 |
* @param name resource name to find
|
jaroslav@346
|
110 |
* @return the array of bytes in the given resource
|
jaroslav@346
|
111 |
* @throws IOException I/O in case something goes wrong
|
jaroslav@346
|
112 |
*/
|
jaroslav@346
|
113 |
public static byte[] read(String name) throws IOException {
|
jaroslav@346
|
114 |
URL u = null;
|
jaroslav@346
|
115 |
Enumeration<URL> en = VMTest.class.getClassLoader().getResources(name);
|
jaroslav@346
|
116 |
while (en.hasMoreElements()) {
|
jaroslav@346
|
117 |
u = en.nextElement();
|
jaroslav@346
|
118 |
}
|
jaroslav@346
|
119 |
if (u == null) {
|
jaroslav@346
|
120 |
throw new IOException("Can't find " + name);
|
jaroslav@346
|
121 |
}
|
jaroslav@346
|
122 |
try (InputStream is = u.openStream()) {
|
jaroslav@346
|
123 |
byte[] arr;
|
jaroslav@346
|
124 |
arr = new byte[is.available()];
|
jaroslav@346
|
125 |
int offset = 0;
|
jaroslav@346
|
126 |
while (offset < arr.length) {
|
jaroslav@346
|
127 |
int len = is.read(arr, offset, arr.length - offset);
|
jaroslav@346
|
128 |
if (len == -1) {
|
jaroslav@346
|
129 |
throw new IOException("Can't read " + name);
|
jaroslav@346
|
130 |
}
|
jaroslav@346
|
131 |
offset += len;
|
jaroslav@346
|
132 |
}
|
jaroslav@346
|
133 |
return arr;
|
jaroslav@346
|
134 |
}
|
jaroslav@346
|
135 |
}
|
jaroslav@346
|
136 |
|
jaroslav@280
|
137 |
public static final class Run implements ITest {
|
jaroslav@280
|
138 |
private final Method m;
|
jaroslav@280
|
139 |
private final boolean js;
|
jaroslav@280
|
140 |
Object value;
|
jaroslav@297
|
141 |
private Invocable code;
|
jaroslav@297
|
142 |
private CharSequence codeSeq;
|
jaroslav@297
|
143 |
private static final Map<Class,Object[]> compiled = new WeakHashMap<Class,Object[]>();
|
jaroslav@280
|
144 |
|
jaroslav@280
|
145 |
private Run(Method m, boolean js) {
|
jaroslav@280
|
146 |
this.m = m;
|
jaroslav@280
|
147 |
this.js = js;
|
jaroslav@280
|
148 |
}
|
jaroslav@280
|
149 |
|
jaroslav@297
|
150 |
private void compileTheCode(Class<?> clazz) throws Exception {
|
jaroslav@297
|
151 |
final Object[] data = compiled.get(clazz);
|
jaroslav@297
|
152 |
if (data != null) {
|
jaroslav@297
|
153 |
code = (Invocable) data[0];
|
jaroslav@297
|
154 |
codeSeq = (CharSequence) data[1];
|
jaroslav@280
|
155 |
return;
|
jaroslav@273
|
156 |
}
|
jaroslav@280
|
157 |
StringBuilder sb = new StringBuilder();
|
jaroslav@346
|
158 |
Bck2Brwsr.generate(sb, VMTest.class.getClassLoader());
|
jaroslav@280
|
159 |
|
jaroslav@280
|
160 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@280
|
161 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@303
|
162 |
|
jaroslav@303
|
163 |
sb.append("\nfunction initVM() {"
|
jaroslav@303
|
164 |
+ "\n return bck2brwsr("
|
jaroslav@346
|
165 |
+ "\n function(name) { return org.apidesign.bck2brwsr.vmtest.VMTest.read(name);}"
|
jaroslav@303
|
166 |
+ "\n );"
|
jaroslav@303
|
167 |
+ "\n};");
|
jaroslav@280
|
168 |
|
jaroslav@280
|
169 |
Object res = js.eval(sb.toString());
|
jaroslav@280
|
170 |
Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@280
|
171 |
code = (Invocable) js;
|
jaroslav@280
|
172 |
codeSeq = sb;
|
jaroslav@297
|
173 |
compiled.put(clazz, new Object[] { code, codeSeq });
|
jaroslav@280
|
174 |
}
|
jaroslav@280
|
175 |
|
jaroslav@280
|
176 |
@Test(groups = "run") public void executeCode() throws Throwable {
|
jaroslav@273
|
177 |
if (js) {
|
jaroslav@273
|
178 |
try {
|
jaroslav@273
|
179 |
compileTheCode(m.getDeclaringClass());
|
jaroslav@303
|
180 |
Object vm = code.invokeFunction("initVM");
|
jaroslav@276
|
181 |
Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
|
jaroslav@296
|
182 |
value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
|
jaroslav@273
|
183 |
} catch (Exception ex) {
|
jaroslav@346
|
184 |
throw new AssertionError(dumpJS(codeSeq)).initCause(ex);
|
jaroslav@273
|
185 |
}
|
jaroslav@273
|
186 |
} else {
|
jaroslav@273
|
187 |
value = m.invoke(m.getDeclaringClass().newInstance());
|
jaroslav@273
|
188 |
}
|
jaroslav@273
|
189 |
}
|
jaroslav@280
|
190 |
@Override
|
jaroslav@280
|
191 |
public String getTestName() {
|
jaroslav@280
|
192 |
return m.getName() + (js ? "[JavaScript]" : "[Java]");
|
jaroslav@273
|
193 |
}
|
jaroslav@296
|
194 |
|
jaroslav@296
|
195 |
private static String computeSignature(Method m) {
|
jaroslav@296
|
196 |
StringBuilder sb = new StringBuilder();
|
jaroslav@296
|
197 |
appendType(sb, m.getReturnType());
|
jaroslav@296
|
198 |
for (Class<?> c : m.getParameterTypes()) {
|
jaroslav@296
|
199 |
appendType(sb, c);
|
jaroslav@296
|
200 |
}
|
jaroslav@296
|
201 |
return sb.toString();
|
jaroslav@296
|
202 |
}
|
jaroslav@296
|
203 |
|
jaroslav@296
|
204 |
private static void appendType(StringBuilder sb, Class<?> t) {
|
jaroslav@296
|
205 |
if (t == null) {
|
jaroslav@296
|
206 |
sb.append('V');
|
jaroslav@296
|
207 |
return;
|
jaroslav@296
|
208 |
}
|
jaroslav@296
|
209 |
if (t.isPrimitive()) {
|
jaroslav@296
|
210 |
int ch = -1;
|
jaroslav@296
|
211 |
if (t == int.class) {
|
jaroslav@296
|
212 |
ch = 'I';
|
jaroslav@296
|
213 |
}
|
jaroslav@296
|
214 |
if (t == short.class) {
|
jaroslav@296
|
215 |
ch = 'S';
|
jaroslav@296
|
216 |
}
|
jaroslav@296
|
217 |
if (t == byte.class) {
|
jaroslav@296
|
218 |
ch = 'B';
|
jaroslav@296
|
219 |
}
|
jaroslav@296
|
220 |
if (t == boolean.class) {
|
jaroslav@296
|
221 |
ch = 'Z';
|
jaroslav@296
|
222 |
}
|
jaroslav@296
|
223 |
if (t == long.class) {
|
jaroslav@296
|
224 |
ch = 'J';
|
jaroslav@296
|
225 |
}
|
jaroslav@296
|
226 |
if (t == float.class) {
|
jaroslav@296
|
227 |
ch = 'F';
|
jaroslav@296
|
228 |
}
|
jaroslav@296
|
229 |
if (t == double.class) {
|
jaroslav@296
|
230 |
ch = 'D';
|
jaroslav@296
|
231 |
}
|
jaroslav@296
|
232 |
assert ch != -1 : "Unknown primitive type " + t;
|
jaroslav@296
|
233 |
sb.append((char)ch);
|
jaroslav@296
|
234 |
return;
|
jaroslav@296
|
235 |
}
|
jaroslav@296
|
236 |
if (t.isArray()) {
|
jaroslav@296
|
237 |
sb.append("_3");
|
jaroslav@296
|
238 |
appendType(sb, t.getComponentType());
|
jaroslav@296
|
239 |
return;
|
jaroslav@296
|
240 |
}
|
jaroslav@296
|
241 |
sb.append('L');
|
jaroslav@296
|
242 |
sb.append(t.getName().replace('.', '_'));
|
jaroslav@296
|
243 |
sb.append("_2");
|
jaroslav@296
|
244 |
}
|
jaroslav@273
|
245 |
}
|
jaroslav@346
|
246 |
|
jaroslav@346
|
247 |
static StringBuilder dumpJS(CharSequence sb) throws IOException {
|
jaroslav@346
|
248 |
File f = File.createTempFile("execution", ".js");
|
jaroslav@346
|
249 |
FileWriter w = new FileWriter(f);
|
jaroslav@346
|
250 |
w.append(sb);
|
jaroslav@346
|
251 |
w.close();
|
jaroslav@346
|
252 |
return new StringBuilder(f.getPath());
|
jaroslav@346
|
253 |
}
|
jaroslav@273
|
254 |
}
|