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@393: import java.io.File; jaroslav@393: import java.io.FileWriter; jaroslav@393: import java.io.IOException; jaroslav@413: import java.lang.reflect.InvocationTargetException; jaroslav@372: import java.lang.reflect.Method; jaroslav@372: import java.util.Map; jaroslav@372: import java.util.WeakHashMap; jaroslav@384: import org.apidesign.bck2brwsr.launcher.Launcher; jaroslav@372: import org.apidesign.bck2brwsr.launcher.MethodInvocation; 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@372: Object value; jaroslav@372: private static final Map compiled = new WeakHashMap<>(); jaroslav@372: private Object inst; jaroslav@372: jaroslav@384: Bck2BrwsrCase(Method m, String type, Launcher l) { jaroslav@372: this.l = l; jaroslav@372: this.m = m; jaroslav@372: this.type = type; jaroslav@372: } jaroslav@372: jaroslav@372: @Test(groups = "run") jaroslav@372: public void executeCode() throws Throwable { jaroslav@383: if (l != null) { jaroslav@383: MethodInvocation c = l.invokeMethod(m.getDeclaringClass(), m.getName()); jaroslav@372: value = c.toString(); 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@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: }