author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Mon, 07 Jan 2013 18:27:01 +0100 | |
changeset 413 | c91483c86597 |
parent 393 | bedc3b93a040 |
child 518 | 5df0a239ebeb |
permissions | -rw-r--r-- |
jaroslav@372 | 1 |
/** |
jaroslav@372 | 2 |
* Back 2 Browser Bytecode Translator |
jaroslav@372 | 3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
jaroslav@372 | 4 |
* |
jaroslav@372 | 5 |
* This program is free software: you can redistribute it and/or modify |
jaroslav@372 | 6 |
* it under the terms of the GNU General Public License as published by |
jaroslav@372 | 7 |
* the Free Software Foundation, version 2 of the License. |
jaroslav@372 | 8 |
* |
jaroslav@372 | 9 |
* This program is distributed in the hope that it will be useful, |
jaroslav@372 | 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
jaroslav@372 | 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
jaroslav@372 | 12 |
* GNU General Public License for more details. |
jaroslav@372 | 13 |
* |
jaroslav@372 | 14 |
* You should have received a copy of the GNU General Public License |
jaroslav@372 | 15 |
* along with this program. Look for COPYING file in the top folder. |
jaroslav@372 | 16 |
* If not, see http://opensource.org/licenses/GPL-2.0. |
jaroslav@372 | 17 |
*/ |
jaroslav@372 | 18 |
package org.apidesign.bck2brwsr.vmtest.impl; |
jaroslav@372 | 19 |
|
jaroslav@393 | 20 |
import java.io.File; |
jaroslav@393 | 21 |
import java.io.FileWriter; |
jaroslav@393 | 22 |
import java.io.IOException; |
jaroslav@413 | 23 |
import java.lang.reflect.InvocationTargetException; |
jaroslav@372 | 24 |
import java.lang.reflect.Method; |
jaroslav@372 | 25 |
import java.util.Map; |
jaroslav@372 | 26 |
import java.util.WeakHashMap; |
jaroslav@384 | 27 |
import org.apidesign.bck2brwsr.launcher.Launcher; |
jaroslav@372 | 28 |
import org.apidesign.bck2brwsr.launcher.MethodInvocation; |
jaroslav@372 | 29 |
import org.testng.ITest; |
jaroslav@372 | 30 |
import org.testng.annotations.Test; |
jaroslav@372 | 31 |
|
jaroslav@372 | 32 |
/** |
jaroslav@372 | 33 |
* |
jaroslav@372 | 34 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jaroslav@372 | 35 |
*/ |
jaroslav@372 | 36 |
public final class Bck2BrwsrCase implements ITest { |
jaroslav@372 | 37 |
private final Method m; |
jaroslav@384 | 38 |
private final Launcher l; |
jaroslav@383 | 39 |
private final String type; |
jaroslav@372 | 40 |
Object value; |
jaroslav@372 | 41 |
private static final Map<Class, Object[]> compiled = new WeakHashMap<>(); |
jaroslav@372 | 42 |
private Object inst; |
jaroslav@372 | 43 |
|
jaroslav@384 | 44 |
Bck2BrwsrCase(Method m, String type, Launcher l) { |
jaroslav@372 | 45 |
this.l = l; |
jaroslav@372 | 46 |
this.m = m; |
jaroslav@372 | 47 |
this.type = type; |
jaroslav@372 | 48 |
} |
jaroslav@372 | 49 |
|
jaroslav@372 | 50 |
@Test(groups = "run") |
jaroslav@372 | 51 |
public void executeCode() throws Throwable { |
jaroslav@383 | 52 |
if (l != null) { |
jaroslav@383 | 53 |
MethodInvocation c = l.invokeMethod(m.getDeclaringClass(), m.getName()); |
jaroslav@372 | 54 |
value = c.toString(); |
jaroslav@372 | 55 |
} else { |
jaroslav@413 | 56 |
try { |
jaroslav@413 | 57 |
value = m.invoke(m.getDeclaringClass().newInstance()); |
jaroslav@413 | 58 |
} catch (InvocationTargetException ex) { |
jaroslav@413 | 59 |
Throwable t = ex.getTargetException(); |
jaroslav@413 | 60 |
value = t.getClass().getName() + ":" + t.getMessage(); |
jaroslav@413 | 61 |
} |
jaroslav@372 | 62 |
} |
jaroslav@372 | 63 |
} |
jaroslav@372 | 64 |
|
jaroslav@372 | 65 |
@Override |
jaroslav@372 | 66 |
public String getTestName() { |
jaroslav@372 | 67 |
return m.getName() + "[" + typeName() + "]"; |
jaroslav@372 | 68 |
} |
jaroslav@372 | 69 |
|
jaroslav@372 | 70 |
final String typeName() { |
jaroslav@383 | 71 |
return type; |
jaroslav@372 | 72 |
} |
jaroslav@393 | 73 |
static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException { |
jaroslav@393 | 74 |
File f = File.createTempFile(c.m.getName(), ".js"); |
jaroslav@393 | 75 |
try (final FileWriter w = new FileWriter(f)) { |
jaroslav@393 | 76 |
w.append(c.l.toString()); |
jaroslav@393 | 77 |
} |
jaroslav@393 | 78 |
sb.append("Path: ").append(f.getPath()); |
jaroslav@393 | 79 |
} |
jaroslav@372 | 80 |
} |