1.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Sun Dec 23 17:02:34 2012 +0100
1.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Sun Dec 23 23:30:06 2012 +0100
1.3 @@ -17,20 +17,8 @@
1.4 */
1.5 package org.apidesign.bck2brwsr.vmtest;
1.6
1.7 -import java.io.File;
1.8 -import java.io.FileWriter;
1.9 -import java.io.IOException;
1.10 -import java.lang.reflect.Method;
1.11 -import java.util.Map;
1.12 -import java.util.WeakHashMap;
1.13 -import java.util.logging.Level;
1.14 -import java.util.logging.Logger;
1.15 -import javax.script.Invocable;
1.16 -import org.apidesign.bck2brwsr.launcher.MethodInvocation;
1.17 -import org.testng.Assert;
1.18 -import org.testng.ITest;
1.19 +import org.apidesign.bck2brwsr.vmtest.impl.CompareCase;
1.20 import org.testng.annotations.Factory;
1.21 -import org.testng.annotations.Test;
1.22
1.23 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
1.24 * in provided class and builds set of tests that compare the computations
1.25 @@ -41,19 +29,7 @@
1.26 *
1.27 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.28 */
1.29 -public final class VMTest implements ITest {
1.30 - private static final Launcher JS = new Launcher("js");
1.31 - private static final Launcher BROWSER = new Launcher();
1.32 -
1.33 - private final Run first, second;
1.34 - private final Method m;
1.35 -
1.36 - private VMTest(Method m, Run first, Run second) {
1.37 - this.first = first;
1.38 - this.second = second;
1.39 - this.m = m;
1.40 - }
1.41 -
1.42 +public final class VMTest {
1.43 /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
1.44 * instances of tests. Each instance runs the test in different virtual
1.45 * machine and at the end they compare the results.
1.46 @@ -62,164 +38,6 @@
1.47 * @return the set of created tests
1.48 */
1.49 public static Object[] create(Class<?> clazz) {
1.50 - Method[] arr = clazz.getMethods();
1.51 - Object[] ret = new Object[5 * arr.length];
1.52 - int cnt = 0;
1.53 - for (Method m : arr) {
1.54 - Compare c = m.getAnnotation(Compare.class);
1.55 - if (c == null) {
1.56 - continue;
1.57 - }
1.58 - final Run real = new Run(m, 0);
1.59 - final Run js = new Run(m, 1);
1.60 - final Run brwsr = new Run(m, 2);
1.61 - ret[cnt++] = real;
1.62 - ret[cnt++] = js;
1.63 - ret[cnt++] = brwsr;
1.64 - ret[cnt++] = new VMTest(m, real, js);
1.65 - ret[cnt++] = new VMTest(m, real, brwsr);
1.66 - }
1.67 - Object[] r = new Object[cnt];
1.68 - for (int i = 0; i < cnt; i++) {
1.69 - r[i] = ret[i];
1.70 - }
1.71 - return r;
1.72 - }
1.73 -
1.74 - /** Test that compares the previous results.
1.75 - * @throws Throwable
1.76 - */
1.77 - @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
1.78 - Object v1 = first.value;
1.79 - Object v2 = second.value;
1.80 - if (v1 != null) {
1.81 - v1 = v1.toString();
1.82 - } else {
1.83 - v1 = "null";
1.84 - }
1.85 - Assert.assertEquals(v2, v1, "Comparing results");
1.86 - }
1.87 -
1.88 - /** Test name.
1.89 - * @return name of the tested method followed by a suffix
1.90 - */
1.91 - @Override
1.92 - public String getTestName() {
1.93 - return m.getName() + "[Compare " + second.typeName() + "]";
1.94 - }
1.95 -
1.96 - public static final class Run implements ITest {
1.97 - private final Method m;
1.98 - private final int type;
1.99 - Object value;
1.100 - private Invocable code;
1.101 - private CharSequence codeSeq;
1.102 - private static final Map<Class,Object[]> compiled = new WeakHashMap<>();
1.103 - private Object inst;
1.104 -
1.105 - private Run(Method m, int type) {
1.106 - this.m = m;
1.107 - this.type = type;
1.108 - try {
1.109 - initialize();
1.110 - } catch (Throwable ex) {
1.111 - Logger.getLogger(VMTest.class.getName()).log(Level.SEVERE, null, ex);
1.112 - }
1.113 - }
1.114 -
1.115 - private void initialize() throws Throwable {
1.116 - if (type == 1) {
1.117 - inst = JS.addMethod(m.getDeclaringClass(), m.getName());
1.118 - }
1.119 - if (type == 2) {
1.120 - inst = BROWSER.addMethod(m.getDeclaringClass(), m.getName());
1.121 - }
1.122 - }
1.123 -
1.124 - @Test(groups = "run") public void executeCode() throws Throwable {
1.125 - if (type == 1) {
1.126 - MethodInvocation c = (MethodInvocation) inst;
1.127 - JS.exec();
1.128 - value = c.toString();
1.129 - } else if (type == 2) {
1.130 - MethodInvocation c = (MethodInvocation) inst;
1.131 - BROWSER.exec();
1.132 - value = c.toString();
1.133 - } else {
1.134 - value = m.invoke(m.getDeclaringClass().newInstance());
1.135 - }
1.136 - }
1.137 - @Override
1.138 - public String getTestName() {
1.139 - return m.getName() + "[" + typeName() + "]";
1.140 - }
1.141 -
1.142 - final String typeName() {
1.143 - switch (type) {
1.144 - case 0: return "Java";
1.145 - case 1: return "JavaScript";
1.146 - case 2: return "Browser";
1.147 - default: return "Unknown type " + type;
1.148 - }
1.149 - }
1.150 -
1.151 - private static String computeSignature(Method m) {
1.152 - StringBuilder sb = new StringBuilder();
1.153 - appendType(sb, m.getReturnType());
1.154 - for (Class<?> c : m.getParameterTypes()) {
1.155 - appendType(sb, c);
1.156 - }
1.157 - return sb.toString();
1.158 - }
1.159 -
1.160 - private static void appendType(StringBuilder sb, Class<?> t) {
1.161 - if (t == null) {
1.162 - sb.append('V');
1.163 - return;
1.164 - }
1.165 - if (t.isPrimitive()) {
1.166 - int ch = -1;
1.167 - if (t == int.class) {
1.168 - ch = 'I';
1.169 - }
1.170 - if (t == short.class) {
1.171 - ch = 'S';
1.172 - }
1.173 - if (t == byte.class) {
1.174 - ch = 'B';
1.175 - }
1.176 - if (t == boolean.class) {
1.177 - ch = 'Z';
1.178 - }
1.179 - if (t == long.class) {
1.180 - ch = 'J';
1.181 - }
1.182 - if (t == float.class) {
1.183 - ch = 'F';
1.184 - }
1.185 - if (t == double.class) {
1.186 - ch = 'D';
1.187 - }
1.188 - assert ch != -1 : "Unknown primitive type " + t;
1.189 - sb.append((char)ch);
1.190 - return;
1.191 - }
1.192 - if (t.isArray()) {
1.193 - sb.append("_3");
1.194 - appendType(sb, t.getComponentType());
1.195 - return;
1.196 - }
1.197 - sb.append('L');
1.198 - sb.append(t.getName().replace('.', '_'));
1.199 - sb.append("_2");
1.200 - }
1.201 - }
1.202 -
1.203 - static StringBuilder dumpJS(CharSequence sb) throws IOException {
1.204 - File f = File.createTempFile("execution", ".js");
1.205 - try (FileWriter w = new FileWriter(f)) {
1.206 - w.append(sb);
1.207 - }
1.208 - return new StringBuilder(f.getPath());
1.209 + return CompareCase.create(clazz);
1.210 }
1.211 }