1.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Thu Feb 14 12:06:16 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,135 +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.bck2brwsr.vmtest.impl;
1.22 -
1.23 -import java.io.ByteArrayInputStream;
1.24 -import java.io.File;
1.25 -import java.io.FileWriter;
1.26 -import java.io.IOException;
1.27 -import java.io.InputStream;
1.28 -import java.lang.reflect.Constructor;
1.29 -import java.lang.reflect.InvocationTargetException;
1.30 -import java.lang.reflect.Method;
1.31 -import org.apidesign.bck2brwsr.launcher.Launcher;
1.32 -import org.apidesign.bck2brwsr.launcher.InvocationContext;
1.33 -import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
1.34 -import org.apidesign.bck2brwsr.vmtest.HttpResource;
1.35 -import org.testng.ITest;
1.36 -import org.testng.annotations.Test;
1.37 -
1.38 -/**
1.39 - *
1.40 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.41 - */
1.42 -public final class Bck2BrwsrCase implements ITest {
1.43 - private final Method m;
1.44 - private final Launcher l;
1.45 - private final String type;
1.46 - private final boolean fail;
1.47 - private final HtmlFragment html;
1.48 - private final HttpResource http;
1.49 - Object value;
1.50 -
1.51 - Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, HttpResource http) {
1.52 - this.l = l;
1.53 - this.m = m;
1.54 - this.type = type;
1.55 - this.fail = fail;
1.56 - this.html = html;
1.57 - this.http = http;
1.58 - }
1.59 -
1.60 - @Test(groups = "run")
1.61 - public void executeCode() throws Throwable {
1.62 - if (l != null) {
1.63 - InvocationContext c = l.createInvocation(m.getDeclaringClass(), m.getName());
1.64 - if (html != null) {
1.65 - c.setHtmlFragment(html.value());
1.66 - }
1.67 - if (http != null) {
1.68 - if (!http.content().isEmpty()) {
1.69 - InputStream is = new ByteArrayInputStream(http.content().getBytes("UTF-8"));
1.70 - c.setHttpResource(http.path(), http.mimeType(), is);
1.71 - } else {
1.72 - InputStream is = m.getDeclaringClass().getResourceAsStream(http.resource());
1.73 - c.setHttpResource(http.path(), http.mimeType(), is);
1.74 - }
1.75 - }
1.76 - String res = c.invoke();
1.77 - value = res;
1.78 - if (fail) {
1.79 - int idx = res.indexOf(':');
1.80 - if (idx >= 0) {
1.81 - Class<? extends Throwable> thrwbl = null;
1.82 - try {
1.83 - Class<?> exCls = Class.forName(res.substring(0, idx));
1.84 - if (Throwable.class.isAssignableFrom(exCls)) {
1.85 - thrwbl = exCls.asSubclass(Throwable.class);
1.86 - }
1.87 - } catch (Exception ex) {
1.88 - // ignore
1.89 - }
1.90 - if (thrwbl != null) {
1.91 - Throwable t = null;
1.92 - try {
1.93 - for (Constructor<?> cnstr : thrwbl.getConstructors()) {
1.94 - if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
1.95 - t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
1.96 - break;
1.97 - }
1.98 - }
1.99 - } catch (Throwable ex) {
1.100 - t = thrwbl.newInstance().initCause(ex);
1.101 - }
1.102 - if (t == null) {
1.103 - t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
1.104 - }
1.105 - throw t;
1.106 - }
1.107 - throw new AssertionError(res);
1.108 - }
1.109 - }
1.110 - } else {
1.111 - try {
1.112 - value = m.invoke(m.getDeclaringClass().newInstance());
1.113 - } catch (InvocationTargetException ex) {
1.114 - Throwable t = ex.getTargetException();
1.115 - value = t.getClass().getName() + ":" + t.getMessage();
1.116 - if (t instanceof AssertionError) {
1.117 - throw t;
1.118 - }
1.119 - }
1.120 - }
1.121 - }
1.122 -
1.123 - @Override
1.124 - public String getTestName() {
1.125 - return m.getName() + "[" + typeName() + "]";
1.126 - }
1.127 -
1.128 - final String typeName() {
1.129 - return type;
1.130 - }
1.131 - static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
1.132 - File f = File.createTempFile(c.m.getName(), ".js");
1.133 - try (final FileWriter w = new FileWriter(f)) {
1.134 - w.append(c.l.toString());
1.135 - }
1.136 - sb.append("Path: ").append(f.getPath());
1.137 - }
1.138 -}