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