rt/flow/src/test/java/org/apidesign/bck2brwsr/flow/TestVM.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 13 Mar 2015 11:58:30 +0100
branchflow
changeset 1817 c1fd23f4e0ae
parent 1815 fbe883b5a793
permissions -rw-r--r--
Cleaning up the test harness
jaroslav@708
     1
/**
jaroslav@708
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@708
     4
 *
jaroslav@708
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@708
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@708
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@708
     8
 *
jaroslav@708
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@708
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@708
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@708
    12
 * GNU General Public License for more details.
jaroslav@708
    13
 *
jaroslav@708
    14
 * You should have received a copy of the GNU General Public License
jaroslav@708
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@708
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@708
    17
 */
jaroslav@1812
    18
package org.apidesign.bck2brwsr.flow;
jaroslav@708
    19
jaroslav@708
    20
import java.io.File;
jaroslav@1762
    21
import java.io.FileOutputStream;
jaroslav@708
    22
import java.io.IOException;
jaroslav@708
    23
import java.io.InputStream;
jaroslav@1762
    24
import java.io.OutputStreamWriter;
jaroslav@1762
    25
import java.io.Writer;
jaroslav@708
    26
import java.net.URL;
jaroslav@1609
    27
import java.util.ArrayList;
jaroslav@1609
    28
import java.util.Arrays;
jaroslav@708
    29
import java.util.Enumeration;
jaroslav@1609
    30
import java.util.List;
jaroslav@708
    31
import javax.script.Invocable;
jaroslav@1367
    32
import javax.script.ScriptContext;
jaroslav@708
    33
import javax.script.ScriptEngine;
jaroslav@708
    34
import javax.script.ScriptEngineManager;
jaroslav@708
    35
import javax.script.ScriptException;
jaroslav@1812
    36
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@1812
    37
import org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
jaroslav@708
    38
import static org.testng.Assert.*;
jaroslav@708
    39
jaroslav@1367
    40
public final class TestVM {
jaroslav@1815
    41
    private final ScriptEngine js;
jaroslav@1815
    42
    private Invocable code;
jaroslav@708
    43
    private final CharSequence codeSeq;
jaroslav@1815
    44
    private Object bck2brwsr;
jaroslav@708
    45
    
jaroslav@708
    46
    
jaroslav@1815
    47
    private TestVM(ScriptEngine js, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
jaroslav@1815
    48
        this.js = js;
jaroslav@708
    49
        this.codeSeq = codeSeq;
jaroslav@708
    50
    }
jaroslav@708
    51
    
jaroslav@708
    52
    public Object execCode(
jaroslav@708
    53
        String msg, Class<?> clazz, String method, 
jaroslav@708
    54
        Object expRes, Object... args
jaroslav@708
    55
    ) throws Exception {
jaroslav@708
    56
        Object ret = null;
jaroslav@708
    57
        try {
jaroslav@1815
    58
            ret = getCode().invokeMethod(bck2brwsr, "loadClass", clazz.getName());
jaroslav@1812
    59
            List<Object> ma = new ArrayList<Object>();
jaroslav@1609
    60
            ma.add(method);
jaroslav@1609
    61
            ma.addAll(Arrays.asList(args));
jaroslav@1815
    62
            ret = getCode().invokeMethod(ret, "invoke", ma.toArray());
jaroslav@708
    63
        } catch (ScriptException ex) {
jaroslav@782
    64
            fail("Execution failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
jaroslav@708
    65
        } catch (NoSuchMethodException ex) {
jaroslav@708
    66
            fail("Cannot find method in " + dumpJS(codeSeq), ex);
jaroslav@708
    67
        }
jaroslav@708
    68
        if (ret == null && expRes == null) {
jaroslav@708
    69
            return null;
jaroslav@708
    70
        }
jaroslav@747
    71
        if (expRes != null && expRes.equals(ret)) {
jaroslav@708
    72
            return null;
jaroslav@708
    73
        }
jaroslav@708
    74
        if (expRes instanceof Number) {
jaroslav@708
    75
            // in case of Long it is necessary convert it to number
jaroslav@708
    76
            // since the Long is represented by two numbers in JavaScript
jaroslav@708
    77
            try {
jaroslav@1815
    78
                final Object toFP = ((ScriptEngine)getCode()).eval("Number.prototype.toFP");
jaroslav@1392
    79
                if (ret instanceof Long) {
jaroslav@1815
    80
                    ret = getCode().invokeMethod(toFP, "call", ret);
jaroslav@1392
    81
                }
jaroslav@1815
    82
                ret = getCode().invokeFunction("Number", ret);
jaroslav@708
    83
            } catch (ScriptException ex) {
jaroslav@784
    84
                fail("Conversion to number failed in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
jaroslav@708
    85
            } catch (NoSuchMethodException ex) {
jaroslav@784
    86
                fail("Cannot find global Number(x) function in " + dumpJS(codeSeq) + ": " + ex.getMessage(), ex);
jaroslav@708
    87
            }
jaroslav@708
    88
        }
jaroslav@708
    89
        return ret;
jaroslav@708
    90
    }
jaroslav@708
    91
    
jaroslav@708
    92
    void assertExec(
jaroslav@708
    93
        String msg, Class clazz, String method, Object expRes, Object... args
jaroslav@708
    94
    ) throws Exception {
jaroslav@708
    95
        Object ret = execCode(msg, clazz, method, expRes, args);
jaroslav@708
    96
        if (ret == null) {
jaroslav@708
    97
            return;
jaroslav@708
    98
        }
jaroslav@1392
    99
        if (expRes instanceof Integer && ret instanceof Double) {
jaroslav@1392
   100
            expRes = ((Integer)expRes).doubleValue();
jaroslav@1392
   101
        }
jaroslav@708
   102
        if (expRes != null && expRes.equals(ret)) {
jaroslav@708
   103
            return;
jaroslav@708
   104
        }
jaroslav@708
   105
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
jaroslav@708
   106
    }    
jaroslav@708
   107
jaroslav@708
   108
    static TestVM compileClass(String... names) throws ScriptException, IOException {
jaroslav@1812
   109
        return compileClass(null, GraalFlowAnalyzer.getDefault(), names);
jaroslav@708
   110
    }
jaroslav@708
   111
    
jaroslav@1812
   112
    static TestVM compileClass(StringBuilder sb, Flow.Analyzer flow, String... names) throws ScriptException, IOException {
jaroslav@1812
   113
        return compileClass(sb, null, flow, names);
jaroslav@708
   114
    }
jaroslav@708
   115
jaroslav@1812
   116
    static TestVM compileClass(StringBuilder sb, 
jaroslav@1812
   117
        ScriptEngine[] eng, Flow.Analyzer flow, String... names
jaroslav@1812
   118
    ) throws ScriptException, IOException {
jaroslav@1812
   119
        return compileClass(sb, eng, flow, new EmulationResources(), names);
jaroslav@1645
   120
    }
jaroslav@1645
   121
    static TestVM compileClass(
jaroslav@1645
   122
        StringBuilder sb, 
jaroslav@1645
   123
        ScriptEngine[] eng, 
jaroslav@1812
   124
        Flow.Analyzer flow,
jaroslav@1645
   125
        Bck2Brwsr.Resources resources, 
jaroslav@1645
   126
        String... names
jaroslav@1645
   127
    ) throws ScriptException, IOException {
jaroslav@708
   128
        if (sb == null) {
jaroslav@708
   129
            sb = new StringBuilder();
jaroslav@708
   130
        }
jaroslav@1812
   131
        Bck2Brwsr.newCompiler()
jaroslav@1812
   132
            .resources(resources)
jaroslav@1812
   133
            .addClasses(names)
jaroslav@1812
   134
            .flowAnalyzer(flow)
jaroslav@1812
   135
            .generate(sb);
jaroslav@708
   136
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@708
   137
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@708
   138
        if (eng != null) {
jaroslav@708
   139
            eng[0] = js;
jaroslav@708
   140
        }
jaroslav@708
   141
        try {
jaroslav@1815
   142
            return new TestVM(js, sb);
jaroslav@1493
   143
        } catch (Exception ex) {
jaroslav@1493
   144
            if (sb.length() > 2000) {
jaroslav@1493
   145
                sb = dumpJS(sb);
jaroslav@1493
   146
            }
jaroslav@1493
   147
            fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
jaroslav@1493
   148
            return null;
jaroslav@1493
   149
        }
jaroslav@1493
   150
    }
jaroslav@840
   151
jaroslav@708
   152
    Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
jaroslav@1815
   153
        return getCode().invokeMethod(bck2brwsr, "loadClass", LoopControl.class.getName());
jaroslav@708
   154
    }
jaroslav@708
   155
    
jaroslav@708
   156
    Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
jaroslav@1815
   157
        return getCode().invokeMethod(obj, method, params);
jaroslav@708
   158
    }
jaroslav@708
   159
jaroslav@708
   160
    Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
jaroslav@1815
   161
        return getCode().invokeFunction(methodName, args);
jaroslav@708
   162
    }
jaroslav@708
   163
jaroslav@708
   164
    static StringBuilder dumpJS(CharSequence sb) throws IOException {
jaroslav@708
   165
        File f = File.createTempFile("execution", ".js");
jaroslav@1762
   166
        Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
jaroslav@708
   167
        w.append(sb);
jaroslav@708
   168
        w.close();
jaroslav@708
   169
        return new StringBuilder(f.getPath());
jaroslav@708
   170
    }
jaroslav@708
   171
jaroslav@708
   172
    @Override
jaroslav@708
   173
    public String toString() {
jaroslav@708
   174
        try {
jaroslav@708
   175
            return dumpJS(codeSeq).toString();
jaroslav@708
   176
        } catch (IOException ex) {
jaroslav@708
   177
            return ex.toString();
jaroslav@708
   178
        }
jaroslav@708
   179
    }
jaroslav@1453
   180
jaroslav@1453
   181
    final CharSequence codeSeq() {
jaroslav@1453
   182
        return codeSeq;
jaroslav@1453
   183
    }
jaroslav@1815
   184
jaroslav@1815
   185
    /**
jaroslav@1815
   186
     * @return the code
jaroslav@1815
   187
     */
jaroslav@1815
   188
    private Invocable getCode() throws ScriptException {
jaroslav@1815
   189
        if (code == null) {
jaroslav@1817
   190
            js.eval(codeSeq.toString());
jaroslav@1817
   191
            js.getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
jaroslav@1817
   192
            this.bck2brwsr = js.eval("bck2brwsr(function(n) { return loader.get(n); })");
jaroslav@1815
   193
            code = (Invocable) js;
jaroslav@1815
   194
        }
jaroslav@1815
   195
        return (Invocable)code;
jaroslav@1815
   196
    }
jaroslav@708
   197
    
jaroslav@708
   198
    private static class EmulationResources implements Bck2Brwsr.Resources {
jaroslav@708
   199
        @Override
jaroslav@708
   200
        public InputStream get(String name) throws IOException {
jaroslav@1344
   201
            if ("java/net/URI.class".equals(name)) {
jaroslav@1344
   202
                // skip
jaroslav@1344
   203
                return null;
jaroslav@1344
   204
            }
jaroslav@1404
   205
            if ("java/net/URLConnection.class".equals(name)) {
jaroslav@1404
   206
                // skip
jaroslav@1404
   207
                return null;
jaroslav@1404
   208
            }
jaroslav@1344
   209
            if ("java/lang/System.class".equals(name)) {
jaroslav@1344
   210
                // skip
jaroslav@1344
   211
                return null;
jaroslav@1344
   212
            }
jaroslav@1812
   213
            if ("java/io/PrintStream.class".equals(name)) {
jaroslav@1812
   214
                // skip
jaroslav@1812
   215
                return null;
jaroslav@1812
   216
            }
jaroslav@1812
   217
            if ("java/io/PrintWriter.class".equals(name)) {
jaroslav@1812
   218
                // skip
jaroslav@1812
   219
                return null;
jaroslav@1812
   220
            }
jaroslav@1812
   221
            Enumeration<URL> en = LoopControlTest.class.getClassLoader().getResources(name);
jaroslav@708
   222
            URL u = null;
jaroslav@708
   223
            while (en.hasMoreElements()) {
jaroslav@708
   224
                u = en.nextElement();
jaroslav@708
   225
            }
jaroslav@708
   226
            if (u == null) {
jaroslav@708
   227
                throw new IOException("Can't find " + name);
jaroslav@708
   228
            }
jaroslav@708
   229
            if (u.toExternalForm().contains("rt.jar!")) {
jaroslav@708
   230
                throw new IOException("No emulation for " + u);
jaroslav@708
   231
            }
jaroslav@708
   232
            return u.openStream();
jaroslav@708
   233
        }
jaroslav@708
   234
    }
jaroslav@708
   235
}