vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 07 Dec 2012 10:35:31 +0100
changeset 280 21c0cf272b05
parent 273 59b9f9f5364f
child 284 58bd38caf70d
child 296 fbf8eb98a8ef
permissions -rw-r--r--
Specify dependency between the test run and comparing the results
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@280
    39
    private final Run first, second;
jaroslav@273
    40
    private final Method m;
jaroslav@273
    41
    
jaroslav@280
    42
    private CompareVMs(Method m, Run first, Run second) {
jaroslav@273
    43
        this.first = first;
jaroslav@273
    44
        this.second = second;
jaroslav@273
    45
        this.m = m;
jaroslav@273
    46
    }
jaroslav@273
    47
jaroslav@273
    48
    public static Object[] create(Class<?> clazz) {
jaroslav@273
    49
        Method[] arr = clazz.getMethods();
jaroslav@273
    50
        Object[] ret = new Object[3 * arr.length];
jaroslav@273
    51
        int cnt = 0;
jaroslav@273
    52
        for (Method m : arr) {
jaroslav@273
    53
            Compare c = m.getAnnotation(Compare.class);
jaroslav@273
    54
            if (c == null) {
jaroslav@273
    55
                continue;
jaroslav@273
    56
            }
jaroslav@280
    57
            final Run real = new Run(m, false);
jaroslav@280
    58
            final Run js = new Run(m, true);
jaroslav@273
    59
            ret[cnt++] = real;
jaroslav@273
    60
            ret[cnt++] = js;
jaroslav@273
    61
            ret[cnt++] = new CompareVMs(m, real, js);
jaroslav@273
    62
        }
jaroslav@273
    63
        Object[] r = new Object[cnt];
jaroslav@273
    64
        for (int i = 0; i < cnt; i++) {
jaroslav@273
    65
            r[i] = ret[i];
jaroslav@273
    66
        }
jaroslav@273
    67
        return r;
jaroslav@273
    68
    }
jaroslav@273
    69
    
jaroslav@280
    70
    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
jaroslav@280
    71
        Object v1 = first.value;
jaroslav@280
    72
        Object v2 = second.value;
jaroslav@280
    73
        if (v1 instanceof Number) {
jaroslav@280
    74
            v1 = ((Number)v1).doubleValue();
jaroslav@280
    75
        }
jaroslav@280
    76
        Assert.assertEquals(v1, v2, "Comparing results");
jaroslav@280
    77
    }
jaroslav@280
    78
    
jaroslav@280
    79
    @Override
jaroslav@280
    80
    public String getTestName() {
jaroslav@280
    81
        return m.getName() + "[Compare]";
jaroslav@280
    82
    }
jaroslav@280
    83
    
jaroslav@280
    84
    public static final class Run implements ITest {
jaroslav@280
    85
        private final Method m;
jaroslav@280
    86
        private final boolean js;
jaroslav@280
    87
        Object value;
jaroslav@280
    88
        private static Invocable code;
jaroslav@280
    89
        private static CharSequence codeSeq;
jaroslav@280
    90
jaroslav@280
    91
        private Run(Method m, boolean js) {
jaroslav@280
    92
            this.m = m;
jaroslav@280
    93
            this.js = js;
jaroslav@280
    94
        }
jaroslav@280
    95
jaroslav@280
    96
        private static void compileTheCode(Class<?> clazz) throws Exception {
jaroslav@280
    97
            if (code != null) {
jaroslav@280
    98
                return;
jaroslav@273
    99
            }
jaroslav@280
   100
            StringBuilder sb = new StringBuilder();
jaroslav@280
   101
            class SkipMe extends GenJS {
jaroslav@280
   102
jaroslav@280
   103
                public SkipMe(Appendable out) {
jaroslav@280
   104
                    super(out);
jaroslav@280
   105
                }
jaroslav@280
   106
jaroslav@280
   107
                @Override
jaroslav@280
   108
                protected boolean requireReference(String cn) {
jaroslav@280
   109
                    if (cn.contains("CompareVMs")) {
jaroslav@280
   110
                        return true;
jaroslav@280
   111
                    }
jaroslav@280
   112
                    return super.requireReference(cn);
jaroslav@280
   113
                }
jaroslav@280
   114
            }
jaroslav@280
   115
            SkipMe sm = new SkipMe(sb);
jaroslav@280
   116
            sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
jaroslav@280
   117
                clazz.getName().replace('.', '/')));
jaroslav@280
   118
jaroslav@280
   119
            ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@280
   120
            ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@280
   121
jaroslav@280
   122
            Object res = js.eval(sb.toString());
jaroslav@280
   123
            Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@280
   124
            code = (Invocable) js;
jaroslav@280
   125
            codeSeq = sb;
jaroslav@280
   126
        }
jaroslav@280
   127
jaroslav@280
   128
        @Test(groups = "run") public void executeCode() throws Throwable {
jaroslav@273
   129
            if (js) {
jaroslav@273
   130
                try {
jaroslav@273
   131
                    compileTheCode(m.getDeclaringClass());
jaroslav@273
   132
                    Object inst = code.invokeFunction(m.getDeclaringClass().getName().replace('.', '_'), false);
jaroslav@273
   133
                    value = code.invokeMethod(inst, m.getName() + "__I");
jaroslav@273
   134
                } catch (Exception ex) {
jaroslav@273
   135
                    throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
jaroslav@273
   136
                }
jaroslav@273
   137
            } else {
jaroslav@273
   138
                value = m.invoke(m.getDeclaringClass().newInstance());
jaroslav@273
   139
            }
jaroslav@273
   140
        }
jaroslav@280
   141
        @Override
jaroslav@280
   142
        public String getTestName() {
jaroslav@280
   143
            return m.getName() + (js ? "[JavaScript]" : "[Java]");
jaroslav@273
   144
        }
jaroslav@273
   145
    }
jaroslav@273
   146
}