vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 10 Dec 2012 12:03:22 +0100
changeset 297 a20721a10717
parent 296 fbf8eb98a8ef
child 306 f36b3c273de6
permissions -rw-r--r--
Cache the compiled code per class, not globally
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@297
    21
import java.util.Map;
jaroslav@297
    22
import java.util.WeakHashMap;
jaroslav@273
    23
import javax.script.Invocable;
jaroslav@273
    24
import javax.script.ScriptEngine;
jaroslav@273
    25
import javax.script.ScriptEngineManager;
jaroslav@273
    26
import org.testng.Assert;
jaroslav@273
    27
import org.testng.ITest;
jaroslav@273
    28
import org.testng.annotations.Factory;
jaroslav@273
    29
import org.testng.annotations.Test;
jaroslav@273
    30
jaroslav@273
    31
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
jaroslav@273
    32
 * in provided class and builds set of tests that compare the computations
jaroslav@273
    33
 * in real as well as JavaScript virtual machines. Use as:<pre>
jaroslav@273
    34
 * {@code @}{@link Factory} public static create() {
jaroslav@273
    35
 *   return @{link CompareVMs}.{@link #create(YourClass.class);
jaroslav@273
    36
 * }</pre>
jaroslav@273
    37
 *
jaroslav@273
    38
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@273
    39
 */
jaroslav@273
    40
public final class CompareVMs implements ITest {
jaroslav@280
    41
    private final Run first, second;
jaroslav@273
    42
    private final Method m;
jaroslav@273
    43
    
jaroslav@280
    44
    private CompareVMs(Method m, Run first, Run second) {
jaroslav@273
    45
        this.first = first;
jaroslav@273
    46
        this.second = second;
jaroslav@273
    47
        this.m = m;
jaroslav@273
    48
    }
jaroslav@273
    49
jaroslav@273
    50
    public static Object[] create(Class<?> clazz) {
jaroslav@273
    51
        Method[] arr = clazz.getMethods();
jaroslav@273
    52
        Object[] ret = new Object[3 * arr.length];
jaroslav@273
    53
        int cnt = 0;
jaroslav@273
    54
        for (Method m : arr) {
jaroslav@273
    55
            Compare c = m.getAnnotation(Compare.class);
jaroslav@273
    56
            if (c == null) {
jaroslav@273
    57
                continue;
jaroslav@273
    58
            }
jaroslav@280
    59
            final Run real = new Run(m, false);
jaroslav@280
    60
            final Run js = new Run(m, true);
jaroslav@273
    61
            ret[cnt++] = real;
jaroslav@273
    62
            ret[cnt++] = js;
jaroslav@273
    63
            ret[cnt++] = new CompareVMs(m, real, js);
jaroslav@273
    64
        }
jaroslav@273
    65
        Object[] r = new Object[cnt];
jaroslav@273
    66
        for (int i = 0; i < cnt; i++) {
jaroslav@273
    67
            r[i] = ret[i];
jaroslav@273
    68
        }
jaroslav@273
    69
        return r;
jaroslav@273
    70
    }
jaroslav@273
    71
    
jaroslav@280
    72
    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
jaroslav@280
    73
        Object v1 = first.value;
jaroslav@280
    74
        Object v2 = second.value;
jaroslav@280
    75
        if (v1 instanceof Number) {
jaroslav@280
    76
            v1 = ((Number)v1).doubleValue();
jaroslav@280
    77
        }
jaroslav@296
    78
        Assert.assertEquals(v2, v1, "Comparing results");
jaroslav@280
    79
    }
jaroslav@280
    80
    
jaroslav@280
    81
    @Override
jaroslav@280
    82
    public String getTestName() {
jaroslav@280
    83
        return m.getName() + "[Compare]";
jaroslav@280
    84
    }
jaroslav@280
    85
    
jaroslav@280
    86
    public static final class Run implements ITest {
jaroslav@280
    87
        private final Method m;
jaroslav@280
    88
        private final boolean js;
jaroslav@280
    89
        Object value;
jaroslav@297
    90
        private Invocable code;
jaroslav@297
    91
        private CharSequence codeSeq;
jaroslav@297
    92
        private static final Map<Class,Object[]> compiled = new WeakHashMap<Class,Object[]>();
jaroslav@280
    93
jaroslav@280
    94
        private Run(Method m, boolean js) {
jaroslav@280
    95
            this.m = m;
jaroslav@280
    96
            this.js = js;
jaroslav@280
    97
        }
jaroslav@280
    98
jaroslav@297
    99
        private void compileTheCode(Class<?> clazz) throws Exception {
jaroslav@297
   100
            final Object[] data = compiled.get(clazz);
jaroslav@297
   101
            if (data != null) {
jaroslav@297
   102
                code = (Invocable) data[0];
jaroslav@297
   103
                codeSeq = (CharSequence) data[1];
jaroslav@280
   104
                return;
jaroslav@273
   105
            }
jaroslav@280
   106
            StringBuilder sb = new StringBuilder();
jaroslav@280
   107
            class SkipMe extends GenJS {
jaroslav@280
   108
jaroslav@280
   109
                public SkipMe(Appendable out) {
jaroslav@280
   110
                    super(out);
jaroslav@280
   111
                }
jaroslav@280
   112
jaroslav@280
   113
                @Override
jaroslav@280
   114
                protected boolean requireReference(String cn) {
jaroslav@280
   115
                    if (cn.contains("CompareVMs")) {
jaroslav@280
   116
                        return true;
jaroslav@280
   117
                    }
jaroslav@280
   118
                    return super.requireReference(cn);
jaroslav@280
   119
                }
jaroslav@280
   120
            }
jaroslav@280
   121
            SkipMe sm = new SkipMe(sb);
jaroslav@280
   122
            sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
jaroslav@280
   123
                clazz.getName().replace('.', '/')));
jaroslav@280
   124
jaroslav@280
   125
            ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@280
   126
            ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@280
   127
jaroslav@280
   128
            Object res = js.eval(sb.toString());
jaroslav@280
   129
            Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@280
   130
            code = (Invocable) js;
jaroslav@280
   131
            codeSeq = sb;
jaroslav@297
   132
            compiled.put(clazz, new Object[] { code, codeSeq });
jaroslav@280
   133
        }
jaroslav@280
   134
jaroslav@280
   135
        @Test(groups = "run") public void executeCode() throws Throwable {
jaroslav@273
   136
            if (js) {
jaroslav@273
   137
                try {
jaroslav@273
   138
                    compileTheCode(m.getDeclaringClass());
jaroslav@273
   139
                    Object inst = code.invokeFunction(m.getDeclaringClass().getName().replace('.', '_'), false);
jaroslav@296
   140
                    value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
jaroslav@273
   141
                } catch (Exception ex) {
jaroslav@273
   142
                    throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
jaroslav@273
   143
                }
jaroslav@273
   144
            } else {
jaroslav@273
   145
                value = m.invoke(m.getDeclaringClass().newInstance());
jaroslav@273
   146
            }
jaroslav@273
   147
        }
jaroslav@280
   148
        @Override
jaroslav@280
   149
        public String getTestName() {
jaroslav@280
   150
            return m.getName() + (js ? "[JavaScript]" : "[Java]");
jaroslav@273
   151
        }
jaroslav@296
   152
        
jaroslav@296
   153
        private static String computeSignature(Method m) {
jaroslav@296
   154
            StringBuilder sb = new StringBuilder();
jaroslav@296
   155
            appendType(sb, m.getReturnType());
jaroslav@296
   156
            for (Class<?> c : m.getParameterTypes()) {
jaroslav@296
   157
                appendType(sb, c);
jaroslav@296
   158
            }
jaroslav@296
   159
            return sb.toString();
jaroslav@296
   160
        }
jaroslav@296
   161
        
jaroslav@296
   162
        private static void appendType(StringBuilder sb, Class<?> t) {
jaroslav@296
   163
            if (t == null) {
jaroslav@296
   164
                sb.append('V');
jaroslav@296
   165
                return;
jaroslav@296
   166
            }
jaroslav@296
   167
            if (t.isPrimitive()) {
jaroslav@296
   168
                int ch = -1;
jaroslav@296
   169
                if (t == int.class) {
jaroslav@296
   170
                    ch = 'I';
jaroslav@296
   171
                }
jaroslav@296
   172
                if (t == short.class) {
jaroslav@296
   173
                    ch = 'S';
jaroslav@296
   174
                }
jaroslav@296
   175
                if (t == byte.class) {
jaroslav@296
   176
                    ch = 'B';
jaroslav@296
   177
                }
jaroslav@296
   178
                if (t == boolean.class) {
jaroslav@296
   179
                    ch = 'Z';
jaroslav@296
   180
                }
jaroslav@296
   181
                if (t == long.class) {
jaroslav@296
   182
                    ch = 'J';
jaroslav@296
   183
                }
jaroslav@296
   184
                if (t == float.class) {
jaroslav@296
   185
                    ch = 'F';
jaroslav@296
   186
                }
jaroslav@296
   187
                if (t == double.class) {
jaroslav@296
   188
                    ch = 'D';
jaroslav@296
   189
                }
jaroslav@296
   190
                assert ch != -1 : "Unknown primitive type " + t;
jaroslav@296
   191
                sb.append((char)ch);
jaroslav@296
   192
                return;
jaroslav@296
   193
            }
jaroslav@296
   194
            if (t.isArray()) {
jaroslav@296
   195
                sb.append("_3");
jaroslav@296
   196
                appendType(sb, t.getComponentType());
jaroslav@296
   197
                return;
jaroslav@296
   198
            }
jaroslav@296
   199
            sb.append('L');
jaroslav@296
   200
            sb.append(t.getName().replace('.', '_'));
jaroslav@296
   201
            sb.append("_2");
jaroslav@296
   202
        }
jaroslav@273
   203
    }
jaroslav@273
   204
}