vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java
changeset 273 59b9f9f5364f
child 275 6e8422eca1ed
child 280 21c0cf272b05
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/CompareVMs.java	Thu Dec 06 16:13:03 2012 +0100
     1.3 @@ -0,0 +1,147 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +import java.lang.reflect.Method;
    1.24 +import javax.script.Invocable;
    1.25 +import javax.script.ScriptEngine;
    1.26 +import javax.script.ScriptEngineManager;
    1.27 +import org.testng.Assert;
    1.28 +import org.testng.ITest;
    1.29 +import org.testng.annotations.Factory;
    1.30 +import org.testng.annotations.Test;
    1.31 +
    1.32 +/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
    1.33 + * in provided class and builds set of tests that compare the computations
    1.34 + * in real as well as JavaScript virtual machines. Use as:<pre>
    1.35 + * {@code @}{@link Factory} public static create() {
    1.36 + *   return @{link CompareVMs}.{@link #create(YourClass.class);
    1.37 + * }</pre>
    1.38 + *
    1.39 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.40 + */
    1.41 +public final class CompareVMs implements ITest {
    1.42 +    private final Method m;
    1.43 +    private final boolean js;
    1.44 +    private final CompareVMs first, second;
    1.45 +    private Object value;
    1.46 +    private static Invocable code;
    1.47 +    private static CharSequence codeSeq;
    1.48 +    
    1.49 +    private CompareVMs(Method m, boolean js) {
    1.50 +        this.m = m;
    1.51 +        this.js = js;
    1.52 +        this.first = null;
    1.53 +        this.second = null;
    1.54 +    }
    1.55 +
    1.56 +    private CompareVMs(Method m, CompareVMs first, CompareVMs second) {
    1.57 +        this.first = first;
    1.58 +        this.second = second;
    1.59 +        this.m = m;
    1.60 +        this.js = false;
    1.61 +    }
    1.62 +    
    1.63 +    private static void compileTheCode(Class<?> clazz) throws Exception {
    1.64 +        if (code != null) {
    1.65 +            return;
    1.66 +        }
    1.67 +        StringBuilder sb = new StringBuilder();
    1.68 +        class SkipMe extends GenJS {
    1.69 +            public SkipMe(Appendable out) {
    1.70 +                super(out);
    1.71 +            }
    1.72 +
    1.73 +            @Override
    1.74 +            protected boolean requireReference(String cn) {
    1.75 +                if (cn.contains("CompareVMs")) {
    1.76 +                    return true;
    1.77 +                }
    1.78 +                return super.requireReference(cn);
    1.79 +            }
    1.80 +            
    1.81 +            
    1.82 +        }
    1.83 +        SkipMe sm = new SkipMe(sb);
    1.84 +        sm.doCompile(CompareVMs.class.getClassLoader(), StringArray.asList(
    1.85 +            clazz.getName().replace('.', '/')
    1.86 +        ));
    1.87 +        
    1.88 +        ScriptEngineManager sem = new ScriptEngineManager();
    1.89 +        ScriptEngine js = sem.getEngineByExtension("js");
    1.90 +        
    1.91 +        Object res = js.eval(sb.toString());
    1.92 +        Assert.assertTrue(js instanceof Invocable, "It is invocable object: " + res);
    1.93 +        code = (Invocable)js;
    1.94 +        codeSeq = sb;
    1.95 +    }
    1.96 +    
    1.97 +
    1.98 +    public static Object[] create(Class<?> clazz) {
    1.99 +        Method[] arr = clazz.getMethods();
   1.100 +        Object[] ret = new Object[3 * arr.length];
   1.101 +        int cnt = 0;
   1.102 +        for (Method m : arr) {
   1.103 +            Compare c = m.getAnnotation(Compare.class);
   1.104 +            if (c == null) {
   1.105 +                continue;
   1.106 +            }
   1.107 +            final CompareVMs real = new CompareVMs(m, false);
   1.108 +            final CompareVMs js = new CompareVMs(m, true);
   1.109 +            ret[cnt++] = real;
   1.110 +            ret[cnt++] = js;
   1.111 +            ret[cnt++] = new CompareVMs(m, real, js);
   1.112 +        }
   1.113 +        Object[] r = new Object[cnt];
   1.114 +        for (int i = 0; i < cnt; i++) {
   1.115 +            r[i] = ret[i];
   1.116 +        }
   1.117 +        return r;
   1.118 +    }
   1.119 +    
   1.120 +    @Test public void runTest() throws Throwable {
   1.121 +        if (first != null) {
   1.122 +            Object v1 = first.value;
   1.123 +            Object v2 = second.value;
   1.124 +            if (v1 instanceof Number) {
   1.125 +                v1 = ((Number)v1).doubleValue();
   1.126 +            }
   1.127 +            Assert.assertEquals(v1, v2, "Comparing results");
   1.128 +        } else {
   1.129 +            if (js) {
   1.130 +                try {
   1.131 +                    compileTheCode(m.getDeclaringClass());
   1.132 +                    Object inst = code.invokeFunction(m.getDeclaringClass().getName().replace('.', '_'), false);
   1.133 +                    value = code.invokeMethod(inst, m.getName() + "__I");
   1.134 +                } catch (Exception ex) {
   1.135 +                    throw new AssertionError(StaticMethodTest.dumpJS(codeSeq)).initCause(ex);
   1.136 +                }
   1.137 +            } else {
   1.138 +                value = m.invoke(m.getDeclaringClass().newInstance());
   1.139 +            }
   1.140 +        }
   1.141 +    }
   1.142 +    
   1.143 +    @Override
   1.144 +    public String getTestName() {
   1.145 +        if (first != null) {
   1.146 +            return m.getName() + "[Compare]";
   1.147 +        }
   1.148 +        return m.getName() + (js ? "[JavaScript]" : "[Java]");
   1.149 +    }
   1.150 +}