vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 11 Dec 2012 11:05:06 +0100
branchlazyvm
changeset 303 c12342170235
parent 298 885acca2fa0b
child 306 f36b3c273de6
permissions -rw-r--r--
VM in VM properly processes class constants
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.lang.reflect.Method;
    21 import javax.script.Invocable;
    22 import javax.script.ScriptContext;
    23 import javax.script.ScriptEngine;
    24 import javax.script.ScriptEngineManager;
    25 import org.testng.Assert;
    26 import org.testng.ITest;
    27 import org.testng.annotations.Factory;
    28 import org.testng.annotations.Test;
    29 
    30 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
    31  * in provided class and builds set of tests that compare the computations
    32  * in real as well as JavaScript virtual machines. Use as:<pre>
    33  * {@code @}{@link Factory} public static create() {
    34  *   return @{link CompareVMs}.{@link #create(YourClass.class);
    35  * }</pre>
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public final class CompareVMs implements ITest {
    40     private final Run first, second;
    41     private final Method m;
    42     
    43     private CompareVMs(Method m, Run first, Run second) {
    44         this.first = first;
    45         this.second = second;
    46         this.m = m;
    47     }
    48 
    49     public static Object[] create(Class<?> clazz) {
    50         Method[] arr = clazz.getMethods();
    51         Object[] ret = new Object[3 * arr.length];
    52         int cnt = 0;
    53         for (Method m : arr) {
    54             Compare c = m.getAnnotation(Compare.class);
    55             if (c == null) {
    56                 continue;
    57             }
    58             final Run real = new Run(m, false);
    59             final Run js = new Run(m, true);
    60             ret[cnt++] = real;
    61             ret[cnt++] = js;
    62             ret[cnt++] = new CompareVMs(m, real, js);
    63         }
    64         Object[] r = new Object[cnt];
    65         for (int i = 0; i < cnt; i++) {
    66             r[i] = ret[i];
    67         }
    68         return r;
    69     }
    70     
    71     @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
    72         Object v1 = first.value;
    73         Object v2 = second.value;
    74         if (v1 instanceof Number) {
    75             v1 = ((Number)v1).doubleValue();
    76         }
    77         Assert.assertEquals(v1, v2, "Comparing results");
    78     }
    79     
    80     @Override
    81     public String getTestName() {
    82         return m.getName() + "[Compare]";
    83     }
    84     
    85     public static final class Run implements ITest {
    86         private final Method m;
    87         private final boolean js;
    88         Object value;
    89         private static Invocable code;
    90         private static CharSequence codeSeq;
    91 
    92         private Run(Method m, boolean js) {
    93             this.m = m;
    94             this.js = js;
    95         }
    96 
    97         private static void compileTheCode(Class<?> clazz) throws Exception {
    98             if (code != null) {
    99                 return;
   100             }
   101             StringBuilder sb = new StringBuilder();
   102             Bck2Brwsr.generate(sb, CompareVMs.class.getClassLoader());
   103 
   104             ScriptEngineManager sem = new ScriptEngineManager();
   105             ScriptEngine js = sem.getEngineByExtension("js");
   106             js.getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
   107             
   108             sb.append("\nfunction initVM() {"
   109                 + "\n  return bck2brwsr("
   110                 + "\n    function(name) { return loader.get(name);}"
   111                 + "\n  );"
   112                 + "\n};");
   113 
   114             Object res = js.eval(sb.toString());
   115             Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   116             code = (Invocable) js;
   117             codeSeq = sb;
   118         }
   119 
   120         @Test(groups = "run") public void executeCode() throws Throwable {
   121             if (js) {
   122                 try {
   123                     compileTheCode(m.getDeclaringClass());
   124                     Object vm = code.invokeFunction("initVM");
   125                     Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
   126                     value = code.invokeMethod(inst, m.getName() + "__I");
   127                 } catch (Exception ex) {
   128                     throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
   129                 }
   130             } else {
   131                 value = m.invoke(m.getDeclaringClass().newInstance());
   132             }
   133         }
   134         @Override
   135         public String getTestName() {
   136             return m.getName() + (js ? "[JavaScript]" : "[Java]");
   137         }
   138     }
   139 }