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@273
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@273
|
19 |
|
jaroslav@273
|
20 |
import java.lang.reflect.Method;
|
jaroslav@273
|
21 |
import javax.script.Invocable;
|
jaroslav@298
|
22 |
import javax.script.ScriptContext;
|
jaroslav@273
|
23 |
import javax.script.ScriptEngine;
|
jaroslav@273
|
24 |
import javax.script.ScriptEngineManager;
|
jaroslav@273
|
25 |
import org.testng.Assert;
|
jaroslav@273
|
26 |
import org.testng.ITest;
|
jaroslav@273
|
27 |
import org.testng.annotations.Factory;
|
jaroslav@273
|
28 |
import org.testng.annotations.Test;
|
jaroslav@273
|
29 |
|
jaroslav@273
|
30 |
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
|
jaroslav@273
|
31 |
* in provided class and builds set of tests that compare the computations
|
jaroslav@273
|
32 |
* in real as well as JavaScript virtual machines. Use as:<pre>
|
jaroslav@273
|
33 |
* {@code @}{@link Factory} public static create() {
|
jaroslav@273
|
34 |
* return @{link CompareVMs}.{@link #create(YourClass.class);
|
jaroslav@273
|
35 |
* }</pre>
|
jaroslav@273
|
36 |
*
|
jaroslav@273
|
37 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@273
|
38 |
*/
|
jaroslav@273
|
39 |
public final class CompareVMs implements ITest {
|
jaroslav@280
|
40 |
private final Run first, second;
|
jaroslav@273
|
41 |
private final Method m;
|
jaroslav@273
|
42 |
|
jaroslav@280
|
43 |
private CompareVMs(Method m, Run first, Run second) {
|
jaroslav@273
|
44 |
this.first = first;
|
jaroslav@273
|
45 |
this.second = second;
|
jaroslav@273
|
46 |
this.m = m;
|
jaroslav@273
|
47 |
}
|
jaroslav@273
|
48 |
|
jaroslav@273
|
49 |
public static Object[] create(Class<?> clazz) {
|
jaroslav@273
|
50 |
Method[] arr = clazz.getMethods();
|
jaroslav@273
|
51 |
Object[] ret = new Object[3 * arr.length];
|
jaroslav@273
|
52 |
int cnt = 0;
|
jaroslav@273
|
53 |
for (Method m : arr) {
|
jaroslav@273
|
54 |
Compare c = m.getAnnotation(Compare.class);
|
jaroslav@273
|
55 |
if (c == null) {
|
jaroslav@273
|
56 |
continue;
|
jaroslav@273
|
57 |
}
|
jaroslav@280
|
58 |
final Run real = new Run(m, false);
|
jaroslav@280
|
59 |
final Run js = new Run(m, true);
|
jaroslav@273
|
60 |
ret[cnt++] = real;
|
jaroslav@273
|
61 |
ret[cnt++] = js;
|
jaroslav@273
|
62 |
ret[cnt++] = new CompareVMs(m, real, js);
|
jaroslav@273
|
63 |
}
|
jaroslav@273
|
64 |
Object[] r = new Object[cnt];
|
jaroslav@273
|
65 |
for (int i = 0; i < cnt; i++) {
|
jaroslav@273
|
66 |
r[i] = ret[i];
|
jaroslav@273
|
67 |
}
|
jaroslav@273
|
68 |
return r;
|
jaroslav@273
|
69 |
}
|
jaroslav@273
|
70 |
|
jaroslav@280
|
71 |
@Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
|
jaroslav@280
|
72 |
Object v1 = first.value;
|
jaroslav@280
|
73 |
Object v2 = second.value;
|
jaroslav@280
|
74 |
if (v1 instanceof Number) {
|
jaroslav@280
|
75 |
v1 = ((Number)v1).doubleValue();
|
jaroslav@280
|
76 |
}
|
jaroslav@280
|
77 |
Assert.assertEquals(v1, v2, "Comparing results");
|
jaroslav@280
|
78 |
}
|
jaroslav@280
|
79 |
|
jaroslav@280
|
80 |
@Override
|
jaroslav@280
|
81 |
public String getTestName() {
|
jaroslav@280
|
82 |
return m.getName() + "[Compare]";
|
jaroslav@280
|
83 |
}
|
jaroslav@280
|
84 |
|
jaroslav@280
|
85 |
public static final class Run implements ITest {
|
jaroslav@280
|
86 |
private final Method m;
|
jaroslav@280
|
87 |
private final boolean js;
|
jaroslav@280
|
88 |
Object value;
|
jaroslav@280
|
89 |
private static Invocable code;
|
jaroslav@280
|
90 |
private static CharSequence codeSeq;
|
jaroslav@280
|
91 |
|
jaroslav@280
|
92 |
private Run(Method m, boolean js) {
|
jaroslav@280
|
93 |
this.m = m;
|
jaroslav@280
|
94 |
this.js = js;
|
jaroslav@280
|
95 |
}
|
jaroslav@280
|
96 |
|
jaroslav@280
|
97 |
private static void compileTheCode(Class<?> clazz) throws Exception {
|
jaroslav@280
|
98 |
if (code != null) {
|
jaroslav@280
|
99 |
return;
|
jaroslav@273
|
100 |
}
|
jaroslav@280
|
101 |
StringBuilder sb = new StringBuilder();
|
jaroslav@298
|
102 |
Bck2Brwsr.generate(sb, CompareVMs.class.getClassLoader());
|
jaroslav@280
|
103 |
|
jaroslav@280
|
104 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@280
|
105 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@298
|
106 |
js.getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
|
jaroslav@280
|
107 |
|
jaroslav@280
|
108 |
Object res = js.eval(sb.toString());
|
jaroslav@280
|
109 |
Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@280
|
110 |
code = (Invocable) js;
|
jaroslav@280
|
111 |
codeSeq = sb;
|
jaroslav@280
|
112 |
}
|
jaroslav@280
|
113 |
|
jaroslav@280
|
114 |
@Test(groups = "run") public void executeCode() throws Throwable {
|
jaroslav@273
|
115 |
if (js) {
|
jaroslav@273
|
116 |
try {
|
jaroslav@273
|
117 |
compileTheCode(m.getDeclaringClass());
|
jaroslav@275
|
118 |
Object vm = code.invokeFunction("bck2brwsr");
|
jaroslav@276
|
119 |
Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
|
jaroslav@273
|
120 |
value = code.invokeMethod(inst, m.getName() + "__I");
|
jaroslav@273
|
121 |
} catch (Exception ex) {
|
jaroslav@273
|
122 |
throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
|
jaroslav@273
|
123 |
}
|
jaroslav@273
|
124 |
} else {
|
jaroslav@273
|
125 |
value = m.invoke(m.getDeclaringClass().newInstance());
|
jaroslav@273
|
126 |
}
|
jaroslav@273
|
127 |
}
|
jaroslav@280
|
128 |
@Override
|
jaroslav@280
|
129 |
public String getTestName() {
|
jaroslav@280
|
130 |
return m.getName() + (js ? "[JavaScript]" : "[Java]");
|
jaroslav@273
|
131 |
}
|
jaroslav@273
|
132 |
}
|
jaroslav@273
|
133 |
}
|