2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.vmtest.impl;
20 import java.io.ByteArrayInputStream;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import org.apidesign.bck2brwsr.launcher.Launcher;
29 import org.apidesign.bck2brwsr.launcher.InvocationContext;
30 import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
31 import org.apidesign.bck2brwsr.vmtest.Http;
32 import org.testng.ITest;
33 import org.testng.annotations.Test;
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 public final class Bck2BrwsrCase implements ITest {
40 private final Method m;
41 private final Launcher l;
42 private final String type;
43 private final boolean fail;
44 private final HtmlFragment html;
45 private final Http.Resource[] http;
48 Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
58 public void executeCode() throws Throwable {
60 InvocationContext c = l.createInvocation(m.getDeclaringClass(), m.getName());
62 c.setHtmlFragment(html.value());
65 for (Http.Resource r : http) {
66 if (!r.content().isEmpty()) {
67 InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
68 c.addHttpResource(r.path(), r.mimeType(), is);
70 InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
71 c.addHttpResource(r.path(), r.mimeType(), is);
75 String res = c.invoke();
78 int idx = res.indexOf(':');
80 Class<? extends Throwable> thrwbl = null;
82 Class<?> exCls = Class.forName(res.substring(0, idx));
83 if (Throwable.class.isAssignableFrom(exCls)) {
84 thrwbl = exCls.asSubclass(Throwable.class);
86 } catch (Exception ex) {
92 for (Constructor<?> cnstr : thrwbl.getConstructors()) {
93 if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
94 t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
98 } catch (Throwable ex) {
99 t = thrwbl.newInstance().initCause(ex);
102 t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
106 throw new AssertionError(res);
111 value = m.invoke(m.getDeclaringClass().newInstance());
112 } catch (InvocationTargetException ex) {
113 Throwable t = ex.getTargetException();
114 value = t.getClass().getName() + ":" + t.getMessage();
115 if (t instanceof AssertionError) {
123 public String getTestName() {
124 return m.getName() + "[" + typeName() + "]";
127 final String typeName() {
130 static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
131 File f = File.createTempFile(c.m.getName(), ".js");
132 try (final FileWriter w = new FileWriter(f)) {
133 w.append(c.l.toString());
135 sb.append("Path: ").append(f.getPath());