jaroslav@372: /** jaroslav@372: * Back 2 Browser Bytecode Translator jaroslav@372: * Copyright (C) 2012 Jaroslav Tulach jaroslav@372: * jaroslav@372: * This program is free software: you can redistribute it and/or modify jaroslav@372: * it under the terms of the GNU General Public License as published by jaroslav@372: * the Free Software Foundation, version 2 of the License. jaroslav@372: * jaroslav@372: * This program is distributed in the hope that it will be useful, jaroslav@372: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@372: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@372: * GNU General Public License for more details. jaroslav@372: * jaroslav@372: * You should have received a copy of the GNU General Public License jaroslav@372: * along with this program. Look for COPYING file in the top folder. jaroslav@372: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@372: */ jaroslav@372: package org.apidesign.bck2brwsr.vmtest.impl; jaroslav@372: jaroslav@626: import java.io.ByteArrayInputStream; jaroslav@393: import java.io.File; jaroslav@393: import java.io.FileWriter; jaroslav@393: import java.io.IOException; jaroslav@626: import java.io.InputStream; jaroslav@518: import java.lang.reflect.Constructor; jaroslav@413: import java.lang.reflect.InvocationTargetException; jaroslav@372: import java.lang.reflect.Method; jaroslav@384: import org.apidesign.bck2brwsr.launcher.Launcher; jaroslav@622: import org.apidesign.bck2brwsr.launcher.InvocationContext; jaroslav@623: import org.apidesign.bck2brwsr.vmtest.HtmlFragment; jaroslav@667: import org.apidesign.bck2brwsr.vmtest.Http; jaroslav@372: import org.testng.ITest; jaroslav@372: import org.testng.annotations.Test; jaroslav@372: jaroslav@372: /** jaroslav@372: * jaroslav@372: * @author Jaroslav Tulach jaroslav@372: */ jaroslav@372: public final class Bck2BrwsrCase implements ITest { jaroslav@372: private final Method m; jaroslav@384: private final Launcher l; jaroslav@383: private final String type; jaroslav@518: private final boolean fail; jaroslav@623: private final HtmlFragment html; jaroslav@667: private final Http.Resource[] http; jaroslav@372: Object value; jaroslav@372: jaroslav@667: Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) { jaroslav@372: this.l = l; jaroslav@372: this.m = m; jaroslav@372: this.type = type; jaroslav@518: this.fail = fail; jaroslav@526: this.html = html; jaroslav@623: this.http = http; jaroslav@372: } jaroslav@372: jaroslav@372: @Test(groups = "run") jaroslav@372: public void executeCode() throws Throwable { jaroslav@383: if (l != null) { jaroslav@622: InvocationContext c = l.createInvocation(m.getDeclaringClass(), m.getName()); jaroslav@622: if (html != null) { jaroslav@623: c.setHtmlFragment(html.value()); jaroslav@623: } jaroslav@623: if (http != null) { jaroslav@667: for (Http.Resource r : http) { jaroslav@667: if (!r.content().isEmpty()) { jaroslav@667: InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8")); jaroslav@667: c.addHttpResource(r.path(), r.mimeType(), is); jaroslav@667: } else { jaroslav@667: InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource()); jaroslav@667: c.addHttpResource(r.path(), r.mimeType(), is); jaroslav@667: } jaroslav@626: } jaroslav@622: } jaroslav@622: String res = c.invoke(); jaroslav@518: value = res; jaroslav@518: if (fail) { jaroslav@518: int idx = res.indexOf(':'); jaroslav@518: if (idx >= 0) { jaroslav@518: Class thrwbl = null; jaroslav@518: try { jaroslav@518: Class exCls = Class.forName(res.substring(0, idx)); jaroslav@518: if (Throwable.class.isAssignableFrom(exCls)) { jaroslav@518: thrwbl = exCls.asSubclass(Throwable.class); jaroslav@518: } jaroslav@518: } catch (Exception ex) { jaroslav@518: // ignore jaroslav@518: } jaroslav@518: if (thrwbl != null) { jaroslav@518: Throwable t = null; jaroslav@518: try { jaroslav@518: for (Constructor cnstr : thrwbl.getConstructors()) { jaroslav@518: if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) { jaroslav@518: t = (Throwable) cnstr.newInstance(res.substring(idx + 1)); jaroslav@518: break; jaroslav@518: } jaroslav@518: } jaroslav@518: } catch (Throwable ex) { jaroslav@518: t = thrwbl.newInstance().initCause(ex); jaroslav@518: } jaroslav@518: if (t == null) { jaroslav@518: t = thrwbl.newInstance().initCause(new Exception(res.substring(idx))); jaroslav@518: } jaroslav@518: throw t; jaroslav@518: } jaroslav@518: throw new AssertionError(res); jaroslav@518: } jaroslav@518: } jaroslav@372: } else { jaroslav@413: try { jaroslav@413: value = m.invoke(m.getDeclaringClass().newInstance()); jaroslav@413: } catch (InvocationTargetException ex) { jaroslav@413: Throwable t = ex.getTargetException(); jaroslav@413: value = t.getClass().getName() + ":" + t.getMessage(); jaroslav@641: if (t instanceof AssertionError) { jaroslav@641: throw t; jaroslav@641: } jaroslav@413: } jaroslav@372: } jaroslav@372: } jaroslav@372: jaroslav@372: @Override jaroslav@372: public String getTestName() { jaroslav@372: return m.getName() + "[" + typeName() + "]"; jaroslav@372: } jaroslav@372: jaroslav@372: final String typeName() { jaroslav@383: return type; jaroslav@372: } jaroslav@393: static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException { jaroslav@393: File f = File.createTempFile(c.m.getName(), ".js"); jaroslav@393: try (final FileWriter w = new FileWriter(f)) { jaroslav@393: w.append(c.l.toString()); jaroslav@393: } jaroslav@393: sb.append("Path: ").append(f.getPath()); jaroslav@393: } jaroslav@372: }