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;
46 private final InvocationContext c;
49 Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
56 this.c = l != null ? l.createInvocation(m.getDeclaringClass(), m.getName()) : null;
60 public void executeCode() throws Throwable {
63 c.setHtmlFragment(html.value());
66 for (Http.Resource r : http) {
67 if (!r.content().isEmpty()) {
68 InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
69 c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
71 InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
72 c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
76 String res = c.invoke();
79 int idx = res == null ? -1 : res.indexOf(':');
81 Class<? extends Throwable> thrwbl = null;
83 Class<?> exCls = Class.forName(res.substring(0, idx));
84 if (Throwable.class.isAssignableFrom(exCls)) {
85 thrwbl = exCls.asSubclass(Throwable.class);
87 } catch (Exception ex) {
93 for (Constructor<?> cnstr : thrwbl.getConstructors()) {
94 if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
95 t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
99 } catch (Throwable ex) {
100 t = thrwbl.newInstance().initCause(ex);
103 t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
107 throw new AssertionError(res);
112 value = m.invoke(m.getDeclaringClass().newInstance());
113 } catch (InvocationTargetException ex) {
114 Throwable t = ex.getTargetException();
115 value = t.getClass().getName() + ":" + t.getMessage();
116 if (t instanceof AssertionError) {
124 public String getTestName() {
125 return m.getName() + "[" + typeName() + "]";
128 final String typeName() {
131 static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
132 File f = File.createTempFile(c.m.getName(), ".js");
133 try (final FileWriter w = new FileWriter(f)) {
134 w.append(c.l.toString());
136 sb.append("Path: ").append(f.getPath());