vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 18 Dec 2012 13:25:32 +0100
branchlauncher
changeset 350 12debda84e3c
parent 349 3fe5a86bd123
child 356 e078953818d2
permissions -rw-r--r--
Starting the HTTP server only once
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@346
    18
package org.apidesign.bck2brwsr.vmtest;
jaroslav@273
    19
jaroslav@346
    20
import java.io.File;
jaroslav@346
    21
import java.io.FileWriter;
jaroslav@346
    22
import java.io.IOException;
jaroslav@346
    23
import java.io.InputStream;
jaroslav@273
    24
import java.lang.reflect.Method;
jaroslav@346
    25
import java.net.URL;
jaroslav@346
    26
import java.util.Enumeration;
jaroslav@297
    27
import java.util.Map;
jaroslav@297
    28
import java.util.WeakHashMap;
jaroslav@350
    29
import java.util.logging.Level;
jaroslav@350
    30
import java.util.logging.Logger;
jaroslav@273
    31
import javax.script.Invocable;
jaroslav@273
    32
import javax.script.ScriptEngine;
jaroslav@273
    33
import javax.script.ScriptEngineManager;
jaroslav@349
    34
import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
jaroslav@346
    35
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@273
    36
import org.testng.Assert;
jaroslav@273
    37
import org.testng.ITest;
jaroslav@350
    38
import org.testng.annotations.BeforeTest;
jaroslav@273
    39
import org.testng.annotations.Factory;
jaroslav@273
    40
import org.testng.annotations.Test;
jaroslav@273
    41
jaroslav@273
    42
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
jaroslav@273
    43
 * in provided class and builds set of tests that compare the computations
jaroslav@273
    44
 * in real as well as JavaScript virtual machines. Use as:<pre>
jaroslav@273
    45
 * {@code @}{@link Factory} public static create() {
jaroslav@346
    46
 *   return @{link VMTest}.{@link #create(YourClass.class);
jaroslav@273
    47
 * }</pre>
jaroslav@273
    48
 *
jaroslav@273
    49
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@273
    50
 */
jaroslav@346
    51
public final class VMTest implements ITest {
jaroslav@280
    52
    private final Run first, second;
jaroslav@273
    53
    private final Method m;
jaroslav@273
    54
    
jaroslav@346
    55
    private VMTest(Method m, Run first, Run second) {
jaroslav@273
    56
        this.first = first;
jaroslav@273
    57
        this.second = second;
jaroslav@273
    58
        this.m = m;
jaroslav@273
    59
    }
jaroslav@273
    60
jaroslav@346
    61
    /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
jaroslav@347
    62
     * instances of tests. Each instance runs the test in different virtual
jaroslav@346
    63
     * machine and at the end they compare the results.
jaroslav@346
    64
     * 
jaroslav@346
    65
     * @param clazz the class to inspect
jaroslav@346
    66
     * @return the set of created tests
jaroslav@346
    67
     */
jaroslav@273
    68
    public static Object[] create(Class<?> clazz) {
jaroslav@273
    69
        Method[] arr = clazz.getMethods();
jaroslav@349
    70
        Object[] ret = new Object[5 * arr.length];
jaroslav@273
    71
        int cnt = 0;
jaroslav@273
    72
        for (Method m : arr) {
jaroslav@273
    73
            Compare c = m.getAnnotation(Compare.class);
jaroslav@273
    74
            if (c == null) {
jaroslav@273
    75
                continue;
jaroslav@273
    76
            }
jaroslav@349
    77
            final Run real = new Run(m, 0);
jaroslav@349
    78
            final Run js = new Run(m, 1);
jaroslav@349
    79
            final Run brwsr = new Run(m, 2);
jaroslav@273
    80
            ret[cnt++] = real;
jaroslav@273
    81
            ret[cnt++] = js;
jaroslav@349
    82
            ret[cnt++] = brwsr;
jaroslav@346
    83
            ret[cnt++] = new VMTest(m, real, js);
jaroslav@349
    84
            ret[cnt++] = new VMTest(m, real, brwsr);
jaroslav@273
    85
        }
jaroslav@273
    86
        Object[] r = new Object[cnt];
jaroslav@273
    87
        for (int i = 0; i < cnt; i++) {
jaroslav@273
    88
            r[i] = ret[i];
jaroslav@273
    89
        }
jaroslav@273
    90
        return r;
jaroslav@273
    91
    }
jaroslav@346
    92
jaroslav@346
    93
    /** Test that compares the previous results.
jaroslav@346
    94
     * @throws Throwable 
jaroslav@346
    95
     */
jaroslav@280
    96
    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
jaroslav@280
    97
        Object v1 = first.value;
jaroslav@280
    98
        Object v2 = second.value;
jaroslav@280
    99
        if (v1 instanceof Number) {
jaroslav@280
   100
            v1 = ((Number)v1).doubleValue();
jaroslav@280
   101
        }
jaroslav@296
   102
        Assert.assertEquals(v2, v1, "Comparing results");
jaroslav@280
   103
    }
jaroslav@280
   104
    
jaroslav@346
   105
    /** Test name.
jaroslav@346
   106
     * @return name of the tested method followed by a suffix
jaroslav@346
   107
     */
jaroslav@280
   108
    @Override
jaroslav@280
   109
    public String getTestName() {
jaroslav@349
   110
        return m.getName() + "[Compare " + second.typeName() + "]";
jaroslav@280
   111
    }
jaroslav@346
   112
jaroslav@346
   113
    /** Helper method that inspects the classpath and loads given resource
jaroslav@346
   114
     * (usually a class file). Used while running tests in Rhino.
jaroslav@346
   115
     * 
jaroslav@346
   116
     * @param name resource name to find
jaroslav@346
   117
     * @return the array of bytes in the given resource
jaroslav@346
   118
     * @throws IOException I/O in case something goes wrong
jaroslav@346
   119
     */
jaroslav@346
   120
    public static byte[] read(String name) throws IOException {
jaroslav@346
   121
        URL u = null;
jaroslav@346
   122
        Enumeration<URL> en = VMTest.class.getClassLoader().getResources(name);
jaroslav@346
   123
        while (en.hasMoreElements()) {
jaroslav@346
   124
            u = en.nextElement();
jaroslav@346
   125
        }
jaroslav@346
   126
        if (u == null) {
jaroslav@346
   127
            throw new IOException("Can't find " + name);
jaroslav@346
   128
        }
jaroslav@346
   129
        try (InputStream is = u.openStream()) {
jaroslav@346
   130
            byte[] arr;
jaroslav@346
   131
            arr = new byte[is.available()];
jaroslav@346
   132
            int offset = 0;
jaroslav@346
   133
            while (offset < arr.length) {
jaroslav@346
   134
                int len = is.read(arr, offset, arr.length - offset);
jaroslav@346
   135
                if (len == -1) {
jaroslav@346
   136
                    throw new IOException("Can't read " + name);
jaroslav@346
   137
                }
jaroslav@346
   138
                offset += len;
jaroslav@346
   139
            }
jaroslav@346
   140
            return arr;
jaroslav@346
   141
        }
jaroslav@346
   142
    }
jaroslav@346
   143
   
jaroslav@280
   144
    public static final class Run implements ITest {
jaroslav@280
   145
        private final Method m;
jaroslav@349
   146
        private final int type;
jaroslav@280
   147
        Object value;
jaroslav@297
   148
        private Invocable code;
jaroslav@297
   149
        private CharSequence codeSeq;
jaroslav@347
   150
        private static final Map<Class,Object[]> compiled = new WeakHashMap<>();
jaroslav@350
   151
        private Object inst;
jaroslav@280
   152
jaroslav@349
   153
        private Run(Method m, int type) {
jaroslav@280
   154
            this.m = m;
jaroslav@349
   155
            this.type = type;
jaroslav@350
   156
            try {
jaroslav@350
   157
                initialize();
jaroslav@350
   158
            } catch (Throwable ex) {
jaroslav@350
   159
                Logger.getLogger(VMTest.class.getName()).log(Level.SEVERE, null, ex);
jaroslav@350
   160
            }
jaroslav@280
   161
        }
jaroslav@280
   162
jaroslav@297
   163
        private void compileTheCode(Class<?> clazz) throws Exception {
jaroslav@297
   164
            final Object[] data = compiled.get(clazz);
jaroslav@297
   165
            if (data != null) {
jaroslav@297
   166
                code = (Invocable) data[0];
jaroslav@297
   167
                codeSeq = (CharSequence) data[1];
jaroslav@280
   168
                return;
jaroslav@273
   169
            }
jaroslav@280
   170
            StringBuilder sb = new StringBuilder();
jaroslav@346
   171
            Bck2Brwsr.generate(sb, VMTest.class.getClassLoader());
jaroslav@280
   172
jaroslav@280
   173
            ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@347
   174
            ScriptEngine mach = sem.getEngineByExtension("js");
jaroslav@303
   175
            
jaroslav@347
   176
            sb.append(
jaroslav@347
   177
                  "\nvar vm = bck2brwsr(org.apidesign.bck2brwsr.vmtest.VMTest.read);"
jaroslav@347
   178
                + "\nfunction initVM() { return vm; };"
jaroslav@347
   179
                + "\n");
jaroslav@280
   180
jaroslav@347
   181
            Object res = mach.eval(sb.toString());
jaroslav@347
   182
            Assert.assertTrue(mach instanceof Invocable, "It is invocable object: " + res);
jaroslav@347
   183
            code = (Invocable) mach;
jaroslav@280
   184
            codeSeq = sb;
jaroslav@297
   185
            compiled.put(clazz, new Object[] { code, codeSeq });
jaroslav@280
   186
        }
jaroslav@350
   187
        
jaroslav@350
   188
        private void initialize() throws Throwable {
jaroslav@350
   189
            if (type == 1) {
jaroslav@350
   190
                compileTheCode(m.getDeclaringClass());
jaroslav@350
   191
                Object vm = code.invokeFunction("initVM");
jaroslav@350
   192
                inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
jaroslav@350
   193
            } else if (type == 2) {
jaroslav@350
   194
                inst = addBrowserMethod(m.getDeclaringClass(), m.getName());
jaroslav@350
   195
            }
jaroslav@350
   196
        }
jaroslav@280
   197
jaroslav@280
   198
        @Test(groups = "run") public void executeCode() throws Throwable {
jaroslav@349
   199
            if (type == 1) {
jaroslav@273
   200
                try {
jaroslav@296
   201
                    value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
jaroslav@273
   202
                } catch (Exception ex) {
jaroslav@346
   203
                    throw new AssertionError(dumpJS(codeSeq)).initCause(ex);
jaroslav@273
   204
                }
jaroslav@349
   205
            } else if (type == 2) {
jaroslav@350
   206
                Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
jaroslav@350
   207
                execBrowser();
jaroslav@349
   208
                value = c.toString();
jaroslav@273
   209
            } else {
jaroslav@273
   210
                value = m.invoke(m.getDeclaringClass().newInstance());
jaroslav@273
   211
            }
jaroslav@273
   212
        }
jaroslav@280
   213
        @Override
jaroslav@280
   214
        public String getTestName() {
jaroslav@349
   215
            return m.getName() + "[" + typeName() + "]";
jaroslav@349
   216
        }
jaroslav@349
   217
        
jaroslav@349
   218
        final String typeName() {
jaroslav@349
   219
            switch (type) {
jaroslav@349
   220
                case 0: return "Java";
jaroslav@349
   221
                case 1: return "JavaScript";
jaroslav@349
   222
                case 2: return "Browser";
jaroslav@349
   223
                default: return "Unknown type " + type;
jaroslav@349
   224
            }
jaroslav@273
   225
        }
jaroslav@296
   226
        
jaroslav@296
   227
        private static String computeSignature(Method m) {
jaroslav@296
   228
            StringBuilder sb = new StringBuilder();
jaroslav@296
   229
            appendType(sb, m.getReturnType());
jaroslav@296
   230
            for (Class<?> c : m.getParameterTypes()) {
jaroslav@296
   231
                appendType(sb, c);
jaroslav@296
   232
            }
jaroslav@296
   233
            return sb.toString();
jaroslav@296
   234
        }
jaroslav@296
   235
        
jaroslav@296
   236
        private static void appendType(StringBuilder sb, Class<?> t) {
jaroslav@296
   237
            if (t == null) {
jaroslav@296
   238
                sb.append('V');
jaroslav@296
   239
                return;
jaroslav@296
   240
            }
jaroslav@296
   241
            if (t.isPrimitive()) {
jaroslav@296
   242
                int ch = -1;
jaroslav@296
   243
                if (t == int.class) {
jaroslav@296
   244
                    ch = 'I';
jaroslav@296
   245
                }
jaroslav@296
   246
                if (t == short.class) {
jaroslav@296
   247
                    ch = 'S';
jaroslav@296
   248
                }
jaroslav@296
   249
                if (t == byte.class) {
jaroslav@296
   250
                    ch = 'B';
jaroslav@296
   251
                }
jaroslav@296
   252
                if (t == boolean.class) {
jaroslav@296
   253
                    ch = 'Z';
jaroslav@296
   254
                }
jaroslav@296
   255
                if (t == long.class) {
jaroslav@296
   256
                    ch = 'J';
jaroslav@296
   257
                }
jaroslav@296
   258
                if (t == float.class) {
jaroslav@296
   259
                    ch = 'F';
jaroslav@296
   260
                }
jaroslav@296
   261
                if (t == double.class) {
jaroslav@296
   262
                    ch = 'D';
jaroslav@296
   263
                }
jaroslav@296
   264
                assert ch != -1 : "Unknown primitive type " + t;
jaroslav@296
   265
                sb.append((char)ch);
jaroslav@296
   266
                return;
jaroslav@296
   267
            }
jaroslav@296
   268
            if (t.isArray()) {
jaroslav@296
   269
                sb.append("_3");
jaroslav@296
   270
                appendType(sb, t.getComponentType());
jaroslav@296
   271
                return;
jaroslav@296
   272
            }
jaroslav@296
   273
            sb.append('L');
jaroslav@296
   274
            sb.append(t.getName().replace('.', '_'));
jaroslav@296
   275
            sb.append("_2");
jaroslav@296
   276
        }
jaroslav@273
   277
    }
jaroslav@346
   278
    
jaroslav@346
   279
    static StringBuilder dumpJS(CharSequence sb) throws IOException {
jaroslav@346
   280
        File f = File.createTempFile("execution", ".js");
jaroslav@347
   281
        try (FileWriter w = new FileWriter(f)) {
jaroslav@347
   282
            w.append(sb);
jaroslav@347
   283
        }
jaroslav@346
   284
        return new StringBuilder(f.getPath());
jaroslav@346
   285
    }
jaroslav@350
   286
    
jaroslav@350
   287
    private static Bck2BrwsrLauncher launcher;
jaroslav@350
   288
jaroslav@350
   289
    private static synchronized Bck2BrwsrLauncher.MethodInvocation addBrowserMethod(
jaroslav@350
   290
        Class<?> clazz, String name
jaroslav@350
   291
    ) {
jaroslav@350
   292
        if (launcher == null) {
jaroslav@350
   293
            launcher = new Bck2BrwsrLauncher();
jaroslav@350
   294
            launcher.setTimeout(5000);
jaroslav@350
   295
        }
jaroslav@350
   296
        return launcher.addMethod(clazz, name);
jaroslav@350
   297
    }
jaroslav@350
   298
    
jaroslav@350
   299
    private static void execBrowser() throws Exception {
jaroslav@350
   300
        Bck2BrwsrLauncher l = clearBrowser();
jaroslav@350
   301
        if (l != null) {
jaroslav@350
   302
            l.execute();
jaroslav@350
   303
        }
jaroslav@350
   304
    }
jaroslav@350
   305
    private static synchronized Bck2BrwsrLauncher clearBrowser() {
jaroslav@350
   306
        Bck2BrwsrLauncher l = launcher;
jaroslav@350
   307
        launcher = null;
jaroslav@350
   308
        return l;
jaroslav@350
   309
    }
jaroslav@273
   310
}