rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 01 Nov 2013 06:36:08 +0100
changeset 1404 60ca4f72a70c
parent 1392 da9e5973e699
child 1453 e046cfcb8f00
permissions -rw-r--r--
Need to ignore newly added URLConnection
jaroslav@708
     1
/**
jaroslav@708
     2
 * Back 2 Browser Bytecode Translator
jaroslav@708
     3
 * Copyright (C) 2012 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@708
    18
package org.apidesign.vm4brwsr;
jaroslav@708
    19
jaroslav@708
    20
import java.io.File;
jaroslav@708
    21
import java.io.FileWriter;
jaroslav@708
    22
import java.io.IOException;
jaroslav@708
    23
import java.io.InputStream;
jaroslav@708
    24
import java.net.URL;
jaroslav@708
    25
import java.util.Enumeration;
jaroslav@708
    26
import javax.script.Invocable;
jaroslav@1367
    27
import javax.script.ScriptContext;
jaroslav@708
    28
import javax.script.ScriptEngine;
jaroslav@708
    29
import javax.script.ScriptEngineManager;
jaroslav@708
    30
import javax.script.ScriptException;
jaroslav@708
    31
import static org.testng.Assert.*;
jaroslav@708
    32
jaroslav@1367
    33
public final class TestVM {
jaroslav@708
    34
    private final Invocable code;
jaroslav@708
    35
    private final CharSequence codeSeq;
jaroslav@708
    36
    private final Object bck2brwsr;
jaroslav@1367
    37
    private BytesLoader resources;
jaroslav@708
    38
    
jaroslav@708
    39
    
jaroslav@708
    40
    private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
jaroslav@708
    41
        this.code = code;
jaroslav@708
    42
        this.codeSeq = codeSeq;
jaroslav@1367
    43
        this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
jaroslav@1367
    44
        ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
jaroslav@708
    45
    }
jaroslav@708
    46
    
jaroslav@1367
    47
    public void register(BytesLoader res) {
jaroslav@1367
    48
        this.resources = res;
jaroslav@1367
    49
    }
jaroslav@1367
    50
    
jaroslav@1367
    51
    public byte[] get(String res) throws IOException {
jaroslav@1367
    52
        return resources != null ? resources.get(res) : null;
jaroslav@1367
    53
    }
jaroslav@708
    54
jaroslav@708
    55
    public Object execCode(
jaroslav@708
    56
        String msg, Class<?> clazz, String method, 
jaroslav@708
    57
        Object expRes, Object... args
jaroslav@708
    58
    ) throws Exception {
jaroslav@708
    59
        Object ret = null;
jaroslav@708
    60
        try {
jaroslav@708
    61
            ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
jaroslav@708
    62
            ret = code.invokeMethod(ret, method, args);
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@1392
    78
                final Object toFP = ((ScriptEngine)code).eval("Number.prototype.toFP");
jaroslav@1392
    79
                if (ret instanceof Long) {
jaroslav@1392
    80
                    ret = code.invokeMethod(toFP, "call", ret);
jaroslav@1392
    81
                }
jaroslav@708
    82
                ret = code.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@708
   109
        return compileClass(null, names);
jaroslav@708
   110
    }
jaroslav@708
   111
    
jaroslav@708
   112
    static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
jaroslav@708
   113
        return compileClass(sb, null, names);
jaroslav@708
   114
    }
jaroslav@708
   115
jaroslav@708
   116
    static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
jaroslav@708
   117
        if (sb == null) {
jaroslav@708
   118
            sb = new StringBuilder();
jaroslav@708
   119
        }
jaroslav@708
   120
        Bck2Brwsr.generate(sb, new EmulationResources(), names);
jaroslav@708
   121
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@708
   122
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@708
   123
        if (eng != null) {
jaroslav@708
   124
            eng[0] = js;
jaroslav@708
   125
        }
jaroslav@708
   126
        try {
jaroslav@708
   127
            Object res = js.eval(sb.toString());
jaroslav@708
   128
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@708
   129
            return new TestVM((Invocable) js, sb);
jaroslav@708
   130
        } catch (Exception ex) {
jaroslav@708
   131
            if (sb.length() > 2000) {
jaroslav@708
   132
                sb = dumpJS(sb);
jaroslav@708
   133
            }
jaroslav@840
   134
            fail("Could not evaluate:" + ex.getClass() + ":" + ex.getMessage() + "\n" + sb, ex);
jaroslav@708
   135
            return null;
jaroslav@708
   136
        }
jaroslav@708
   137
    }
jaroslav@708
   138
jaroslav@708
   139
    Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
jaroslav@708
   140
        return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
jaroslav@708
   141
    }
jaroslav@708
   142
    
jaroslav@708
   143
    Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
jaroslav@708
   144
        return code.invokeMethod(obj, method, params);
jaroslav@708
   145
    }
jaroslav@708
   146
jaroslav@708
   147
    Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
jaroslav@708
   148
        return code.invokeFunction(methodName, args);
jaroslav@708
   149
    }
jaroslav@708
   150
jaroslav@708
   151
    static StringBuilder dumpJS(CharSequence sb) throws IOException {
jaroslav@708
   152
        File f = File.createTempFile("execution", ".js");
jaroslav@708
   153
        FileWriter w = new FileWriter(f);
jaroslav@708
   154
        w.append(sb);
jaroslav@708
   155
        w.close();
jaroslav@708
   156
        return new StringBuilder(f.getPath());
jaroslav@708
   157
    }
jaroslav@708
   158
jaroslav@708
   159
    @Override
jaroslav@708
   160
    public String toString() {
jaroslav@708
   161
        try {
jaroslav@708
   162
            return dumpJS(codeSeq).toString();
jaroslav@708
   163
        } catch (IOException ex) {
jaroslav@708
   164
            return ex.toString();
jaroslav@708
   165
        }
jaroslav@708
   166
    }
jaroslav@708
   167
    
jaroslav@708
   168
    
jaroslav@708
   169
    private static class EmulationResources implements Bck2Brwsr.Resources {
jaroslav@708
   170
        @Override
jaroslav@708
   171
        public InputStream get(String name) throws IOException {
jaroslav@1344
   172
            if ("java/net/URI.class".equals(name)) {
jaroslav@1344
   173
                // skip
jaroslav@1344
   174
                return null;
jaroslav@1344
   175
            }
jaroslav@1404
   176
            if ("java/net/URLConnection.class".equals(name)) {
jaroslav@1404
   177
                // skip
jaroslav@1404
   178
                return null;
jaroslav@1404
   179
            }
jaroslav@1344
   180
            if ("java/lang/System.class".equals(name)) {
jaroslav@1344
   181
                // skip
jaroslav@1344
   182
                return null;
jaroslav@1344
   183
            }
jaroslav@708
   184
            Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
jaroslav@708
   185
            URL u = null;
jaroslav@708
   186
            while (en.hasMoreElements()) {
jaroslav@708
   187
                u = en.nextElement();
jaroslav@708
   188
            }
jaroslav@708
   189
            if (u == null) {
jaroslav@708
   190
                throw new IOException("Can't find " + name);
jaroslav@708
   191
            }
jaroslav@708
   192
            if (u.toExternalForm().contains("rt.jar!")) {
jaroslav@708
   193
                throw new IOException("No emulation for " + u);
jaroslav@708
   194
            }
jaroslav@708
   195
            return u.openStream();
jaroslav@708
   196
        }
jaroslav@708
   197
    }
jaroslav@708
   198
}