1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Fri Mar 22 16:59:47 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,170 +0,0 @@
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.io.File;
1.24 -import java.io.FileWriter;
1.25 -import java.io.IOException;
1.26 -import java.io.InputStream;
1.27 -import java.net.URL;
1.28 -import java.util.Enumeration;
1.29 -import javax.script.Invocable;
1.30 -import javax.script.ScriptEngine;
1.31 -import javax.script.ScriptEngineManager;
1.32 -import javax.script.ScriptException;
1.33 -import static org.testng.Assert.*;
1.34 -
1.35 -final class TestVM {
1.36 - private final Invocable code;
1.37 - private final CharSequence codeSeq;
1.38 - private final Object bck2brwsr;
1.39 -
1.40 -
1.41 - private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
1.42 - this.code = code;
1.43 - this.codeSeq = codeSeq;
1.44 - this.bck2brwsr = code.invokeFunction("bck2brwsr");
1.45 - }
1.46 -
1.47 -
1.48 - public Object execCode(
1.49 - String msg, Class<?> clazz, String method,
1.50 - Object expRes, Object... args
1.51 - ) throws Exception {
1.52 - Object ret = null;
1.53 - try {
1.54 - ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
1.55 - ret = code.invokeMethod(ret, method, args);
1.56 - } catch (ScriptException ex) {
1.57 - fail("Execution failed in " + dumpJS(codeSeq), ex);
1.58 - } catch (NoSuchMethodException ex) {
1.59 - fail("Cannot find method in " + dumpJS(codeSeq), ex);
1.60 - }
1.61 - if (ret == null && expRes == null) {
1.62 - return null;
1.63 - }
1.64 - if (expRes != null && expRes.equals(ret)) {
1.65 - return null;
1.66 - }
1.67 - if (expRes instanceof Number) {
1.68 - // in case of Long it is necessary convert it to number
1.69 - // since the Long is represented by two numbers in JavaScript
1.70 - try {
1.71 - ret = code.invokeMethod(ret, "toFP");
1.72 - ret = code.invokeFunction("Number", ret);
1.73 - } catch (ScriptException ex) {
1.74 - fail("Conversion to number failed in " + dumpJS(codeSeq), ex);
1.75 - } catch (NoSuchMethodException ex) {
1.76 - fail("Cannot find global Number(x) function in " + dumpJS(codeSeq), ex);
1.77 - }
1.78 - }
1.79 - return ret;
1.80 - }
1.81 -
1.82 - void assertExec(
1.83 - String msg, Class clazz, String method, Object expRes, Object... args
1.84 - ) throws Exception {
1.85 - Object ret = execCode(msg, clazz, method, expRes, args);
1.86 - if (ret == null) {
1.87 - return;
1.88 - }
1.89 - if (expRes != null && expRes.equals(ret)) {
1.90 - return;
1.91 - }
1.92 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
1.93 - }
1.94 -
1.95 - static TestVM compileClass(String... names) throws ScriptException, IOException {
1.96 - return compileClass(null, names);
1.97 - }
1.98 -
1.99 - static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
1.100 - return compileClass(sb, null, names);
1.101 - }
1.102 -
1.103 - static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
1.104 - if (sb == null) {
1.105 - sb = new StringBuilder();
1.106 - }
1.107 - Bck2Brwsr.generate(sb, new EmulationResources(), names);
1.108 - ScriptEngineManager sem = new ScriptEngineManager();
1.109 - ScriptEngine js = sem.getEngineByExtension("js");
1.110 - if (eng != null) {
1.111 - eng[0] = js;
1.112 - }
1.113 - try {
1.114 - Object res = js.eval(sb.toString());
1.115 - assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.116 - return new TestVM((Invocable) js, sb);
1.117 - } catch (Exception ex) {
1.118 - if (sb.length() > 2000) {
1.119 - sb = dumpJS(sb);
1.120 - }
1.121 - fail("Could not evaluate:\n" + sb, ex);
1.122 - return null;
1.123 - }
1.124 - }
1.125 -
1.126 - Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
1.127 - return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
1.128 - }
1.129 -
1.130 - Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
1.131 - return code.invokeMethod(obj, method, params);
1.132 - }
1.133 -
1.134 - Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
1.135 - return code.invokeFunction(methodName, args);
1.136 - }
1.137 -
1.138 - static StringBuilder dumpJS(CharSequence sb) throws IOException {
1.139 - File f = File.createTempFile("execution", ".js");
1.140 - FileWriter w = new FileWriter(f);
1.141 - w.append(sb);
1.142 - w.close();
1.143 - return new StringBuilder(f.getPath());
1.144 - }
1.145 -
1.146 - @Override
1.147 - public String toString() {
1.148 - try {
1.149 - return dumpJS(codeSeq).toString();
1.150 - } catch (IOException ex) {
1.151 - return ex.toString();
1.152 - }
1.153 - }
1.154 -
1.155 -
1.156 - private static class EmulationResources implements Bck2Brwsr.Resources {
1.157 - @Override
1.158 - public InputStream get(String name) throws IOException {
1.159 - Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
1.160 - URL u = null;
1.161 - while (en.hasMoreElements()) {
1.162 - u = en.nextElement();
1.163 - }
1.164 - if (u == null) {
1.165 - throw new IOException("Can't find " + name);
1.166 - }
1.167 - if (u.toExternalForm().contains("rt.jar!")) {
1.168 - throw new IOException("No emulation for " + u);
1.169 - }
1.170 - return u.openStream();
1.171 - }
1.172 - }
1.173 -}