vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 06 Dec 2012 16:13:03 +0100
changeset 273 59b9f9f5364f
child 275 6e8422eca1ed
child 280 21c0cf272b05
permissions -rw-r--r--
Infrastructure for comparing runs in Hotspot as well as bck2brwsr VMs. Just use @Compare and CompareVMs.create
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@273
    22
import javax.script.ScriptEngine;
jaroslav@273
    23
import javax.script.ScriptEngineManager;
jaroslav@273
    24
import org.testng.Assert;
jaroslav@273
    25
import org.testng.ITest;
jaroslav@273
    26
import org.testng.annotations.Factory;
jaroslav@273
    27
import org.testng.annotations.Test;
jaroslav@273
    28
jaroslav@273
    29
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
jaroslav@273
    30
 * in provided class and builds set of tests that compare the computations
jaroslav@273
    31
 * in real as well as JavaScript virtual machines. Use as:<pre>
jaroslav@273
    32
 * {@code @}{@link Factory} public static create() {
jaroslav@273
    33
 *   return @{link CompareVMs}.{@link #create(YourClass.class);
jaroslav@273
    34
 * }</pre>
jaroslav@273
    35
 *
jaroslav@273
    36
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@273
    37
 */
jaroslav@273
    38
public final class CompareVMs implements ITest {
jaroslav@273
    39
    private final Method m;
jaroslav@273
    40
    private final boolean js;
jaroslav@273
    41
    private final CompareVMs first, second;
jaroslav@273
    42
    private Object value;
jaroslav@273
    43
    private static Invocable code;
jaroslav@273
    44
    private static CharSequence codeSeq;
jaroslav@273
    45
    
jaroslav@273
    46
    private CompareVMs(Method m, boolean js) {
jaroslav@273
    47
        this.m = m;
jaroslav@273
    48
        this.js = js;
jaroslav@273
    49
        this.first = null;
jaroslav@273
    50
        this.second = null;
jaroslav@273
    51
    }
jaroslav@273
    52
jaroslav@273
    53
    private CompareVMs(Method m, CompareVMs first, CompareVMs second) {
jaroslav@273
    54
        this.first = first;
jaroslav@273
    55
        this.second = second;
jaroslav@273
    56
        this.m = m;
jaroslav@273
    57
        this.js = false;
jaroslav@273
    58
    }
jaroslav@273
    59
    
jaroslav@273
    60
    private static void compileTheCode(Class<?> clazz) throws Exception {
jaroslav@273
    61
        if (code != null) {
jaroslav@273
    62
            return;
jaroslav@273
    63
        }
jaroslav@273
    64
        StringBuilder sb = new StringBuilder();
jaroslav@273
    65
        class SkipMe extends GenJS {
jaroslav@273
    66
            public SkipMe(Appendable out) {
jaroslav@273
    67
                super(out);
jaroslav@273
    68
            }
jaroslav@273
    69
jaroslav@273
    70
            @Override
jaroslav@273
    71
            protected boolean requireReference(String cn) {
jaroslav@273
    72
                if (cn.contains("CompareVMs")) {
jaroslav@273
    73
                    return true;
jaroslav@273
    74
                }
jaroslav@273
    75
                return super.requireReference(cn);
jaroslav@273
    76
            }
jaroslav@273
    77
            
jaroslav@273
    78
            
jaroslav@273
    79
        }
jaroslav@273
    80
        SkipMe sm = new SkipMe(sb);
jaroslav@273
    81
        sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
jaroslav@273
    82
            clazz.getName().replace('.', '/')
jaroslav@273
    83
        ));
jaroslav@273
    84
        
jaroslav@273
    85
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@273
    86
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@273
    87
        
jaroslav@273
    88
        Object res = js.eval(sb.toString());
jaroslav@273
    89
        Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@273
    90
        code = (Invocable)js;
jaroslav@273
    91
        codeSeq = sb;
jaroslav@273
    92
    }
jaroslav@273
    93
    
jaroslav@273
    94
jaroslav@273
    95
    public static Object[] create(Class<?> clazz) {
jaroslav@273
    96
        Method[] arr = clazz.getMethods();
jaroslav@273
    97
        Object[] ret = new Object[3 * arr.length];
jaroslav@273
    98
        int cnt = 0;
jaroslav@273
    99
        for (Method m : arr) {
jaroslav@273
   100
            Compare c = m.getAnnotation(Compare.class);
jaroslav@273
   101
            if (c == null) {
jaroslav@273
   102
                continue;
jaroslav@273
   103
            }
jaroslav@273
   104
            final CompareVMs real = new CompareVMs(m, false);
jaroslav@273
   105
            final CompareVMs js = new CompareVMs(m, true);
jaroslav@273
   106
            ret[cnt++] = real;
jaroslav@273
   107
            ret[cnt++] = js;
jaroslav@273
   108
            ret[cnt++] = new CompareVMs(m, real, js);
jaroslav@273
   109
        }
jaroslav@273
   110
        Object[] r = new Object[cnt];
jaroslav@273
   111
        for (int i = 0; i < cnt; i++) {
jaroslav@273
   112
            r[i] = ret[i];
jaroslav@273
   113
        }
jaroslav@273
   114
        return r;
jaroslav@273
   115
    }
jaroslav@273
   116
    
jaroslav@273
   117
    @Test public void runTest() throws Throwable {
jaroslav@273
   118
        if (first != null) {
jaroslav@273
   119
            Object v1 = first.value;
jaroslav@273
   120
            Object v2 = second.value;
jaroslav@273
   121
            if (v1 instanceof Number) {
jaroslav@273
   122
                v1 = ((Number)v1).doubleValue();
jaroslav@273
   123
            }
jaroslav@273
   124
            Assert.assertEquals(v1, v2, "Comparing results");
jaroslav@273
   125
        } else {
jaroslav@273
   126
            if (js) {
jaroslav@273
   127
                try {
jaroslav@273
   128
                    compileTheCode(m.getDeclaringClass());
jaroslav@273
   129
                    Object inst = code.invokeFunction(m.getDeclaringClass().getName().replace('.', '_'), false);
jaroslav@273
   130
                    value = code.invokeMethod(inst, m.getName() + "__I");
jaroslav@273
   131
                } catch (Exception ex) {
jaroslav@273
   132
                    throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
jaroslav@273
   133
                }
jaroslav@273
   134
            } else {
jaroslav@273
   135
                value = m.invoke(m.getDeclaringClass().newInstance());
jaroslav@273
   136
            }
jaroslav@273
   137
        }
jaroslav@273
   138
    }
jaroslav@273
   139
    
jaroslav@273
   140
    @Override
jaroslav@273
   141
    public String getTestName() {
jaroslav@273
   142
        if (first != null) {
jaroslav@273
   143
            return m.getName() + "[Compare]";
jaroslav@273
   144
        }
jaroslav@273
   145
        return m.getName() + (js ? "[JavaScript]" : "[Java]");
jaroslav@273
   146
    }
jaroslav@273
   147
}