vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 11 Feb 2013 12:46:43 +0100
branchemul
changeset 708 59d5596a9c6c
child 747 ae352b763959
permissions -rw-r--r--
Encapsulation. Moving shared code into TestVM instance.
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@708
    27
import javax.script.ScriptEngine;
jaroslav@708
    28
import javax.script.ScriptEngineManager;
jaroslav@708
    29
import javax.script.ScriptException;
jaroslav@708
    30
import static org.testng.Assert.*;
jaroslav@708
    31
jaroslav@708
    32
final class TestVM {
jaroslav@708
    33
    private final Invocable code;
jaroslav@708
    34
    private final CharSequence codeSeq;
jaroslav@708
    35
    private final Object bck2brwsr;
jaroslav@708
    36
    
jaroslav@708
    37
    
jaroslav@708
    38
    private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
jaroslav@708
    39
        this.code = code;
jaroslav@708
    40
        this.codeSeq = codeSeq;
jaroslav@708
    41
        this.bck2brwsr = code.invokeFunction("bck2brwsr");
jaroslav@708
    42
    }
jaroslav@708
    43
    
jaroslav@708
    44
jaroslav@708
    45
    public Object execCode(
jaroslav@708
    46
        String msg, Class<?> clazz, String method, 
jaroslav@708
    47
        Object expRes, Object... args
jaroslav@708
    48
    ) throws Exception {
jaroslav@708
    49
        Object ret = null;
jaroslav@708
    50
        try {
jaroslav@708
    51
            ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
jaroslav@708
    52
            ret = code.invokeMethod(ret, method, args);
jaroslav@708
    53
        } catch (ScriptException ex) {
jaroslav@708
    54
            fail("Execution failed in " + dumpJS(codeSeq), ex);
jaroslav@708
    55
        } catch (NoSuchMethodException ex) {
jaroslav@708
    56
            fail("Cannot find method in " + dumpJS(codeSeq), ex);
jaroslav@708
    57
        }
jaroslav@708
    58
        if (ret == null && expRes == null) {
jaroslav@708
    59
            return null;
jaroslav@708
    60
        }
jaroslav@708
    61
        if (expRes.equals(ret)) {
jaroslav@708
    62
            return null;
jaroslav@708
    63
        }
jaroslav@708
    64
        if (expRes instanceof Number) {
jaroslav@708
    65
            // in case of Long it is necessary convert it to number
jaroslav@708
    66
            // since the Long is represented by two numbers in JavaScript
jaroslav@708
    67
            try {
jaroslav@708
    68
                ret = code.invokeMethod(ret, "toFP");
jaroslav@708
    69
                ret = code.invokeFunction("Number", ret);
jaroslav@708
    70
            } catch (ScriptException ex) {
jaroslav@708
    71
                fail("Conversion to number failed in " + dumpJS(codeSeq), ex);
jaroslav@708
    72
            } catch (NoSuchMethodException ex) {
jaroslav@708
    73
                fail("Cannot find global Number(x) function in " + dumpJS(codeSeq), ex);
jaroslav@708
    74
            }
jaroslav@708
    75
        }
jaroslav@708
    76
        return ret;
jaroslav@708
    77
    }
jaroslav@708
    78
    
jaroslav@708
    79
    void assertExec(
jaroslav@708
    80
        String msg, Class clazz, String method, Object expRes, Object... args
jaroslav@708
    81
    ) throws Exception {
jaroslav@708
    82
        Object ret = execCode(msg, clazz, method, expRes, args);
jaroslav@708
    83
        if (ret == null) {
jaroslav@708
    84
            return;
jaroslav@708
    85
        }
jaroslav@708
    86
        if (expRes != null && expRes.equals(ret)) {
jaroslav@708
    87
            return;
jaroslav@708
    88
        }
jaroslav@708
    89
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
jaroslav@708
    90
    }    
jaroslav@708
    91
jaroslav@708
    92
    static TestVM compileClass(String... names) throws ScriptException, IOException {
jaroslav@708
    93
        return compileClass(null, names);
jaroslav@708
    94
    }
jaroslav@708
    95
    
jaroslav@708
    96
    static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
jaroslav@708
    97
        return compileClass(sb, null, names);
jaroslav@708
    98
    }
jaroslav@708
    99
jaroslav@708
   100
    static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
jaroslav@708
   101
        if (sb == null) {
jaroslav@708
   102
            sb = new StringBuilder();
jaroslav@708
   103
        }
jaroslav@708
   104
        Bck2Brwsr.generate(sb, new EmulationResources(), names);
jaroslav@708
   105
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@708
   106
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@708
   107
        if (eng != null) {
jaroslav@708
   108
            eng[0] = js;
jaroslav@708
   109
        }
jaroslav@708
   110
        try {
jaroslav@708
   111
            Object res = js.eval(sb.toString());
jaroslav@708
   112
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@708
   113
            return new TestVM((Invocable) js, sb);
jaroslav@708
   114
        } catch (Exception ex) {
jaroslav@708
   115
            if (sb.length() > 2000) {
jaroslav@708
   116
                sb = dumpJS(sb);
jaroslav@708
   117
            }
jaroslav@708
   118
            fail("Could not evaluate:\n" + sb, ex);
jaroslav@708
   119
            return null;
jaroslav@708
   120
        }
jaroslav@708
   121
    }
jaroslav@708
   122
jaroslav@708
   123
    Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
jaroslav@708
   124
        return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
jaroslav@708
   125
    }
jaroslav@708
   126
    
jaroslav@708
   127
    Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
jaroslav@708
   128
        return code.invokeMethod(obj, method, params);
jaroslav@708
   129
    }
jaroslav@708
   130
jaroslav@708
   131
    Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
jaroslav@708
   132
        return code.invokeFunction(methodName, args);
jaroslav@708
   133
    }
jaroslav@708
   134
jaroslav@708
   135
    static StringBuilder dumpJS(CharSequence sb) throws IOException {
jaroslav@708
   136
        File f = File.createTempFile("execution", ".js");
jaroslav@708
   137
        FileWriter w = new FileWriter(f);
jaroslav@708
   138
        w.append(sb);
jaroslav@708
   139
        w.close();
jaroslav@708
   140
        return new StringBuilder(f.getPath());
jaroslav@708
   141
    }
jaroslav@708
   142
jaroslav@708
   143
    @Override
jaroslav@708
   144
    public String toString() {
jaroslav@708
   145
        try {
jaroslav@708
   146
            return dumpJS(codeSeq).toString();
jaroslav@708
   147
        } catch (IOException ex) {
jaroslav@708
   148
            return ex.toString();
jaroslav@708
   149
        }
jaroslav@708
   150
    }
jaroslav@708
   151
    
jaroslav@708
   152
    
jaroslav@708
   153
    private static class EmulationResources implements Bck2Brwsr.Resources {
jaroslav@708
   154
        @Override
jaroslav@708
   155
        public InputStream get(String name) throws IOException {
jaroslav@708
   156
            Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
jaroslav@708
   157
            URL u = null;
jaroslav@708
   158
            while (en.hasMoreElements()) {
jaroslav@708
   159
                u = en.nextElement();
jaroslav@708
   160
            }
jaroslav@708
   161
            if (u == null) {
jaroslav@708
   162
                throw new IOException("Can't find " + name);
jaroslav@708
   163
            }
jaroslav@708
   164
            if (u.toExternalForm().contains("rt.jar!")) {
jaroslav@708
   165
                throw new IOException("No emulation for " + u);
jaroslav@708
   166
            }
jaroslav@708
   167
            return u.openStream();
jaroslav@708
   168
        }
jaroslav@708
   169
    }
jaroslav@708
   170
}