vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 22 Jan 2013 11:59:25 +0100
changeset 518 5df0a239ebeb
parent 413 c91483c86597
child 526 a0d8b5ab79a2
permissions -rw-r--r--
@BrwsrTest to execute tests directly in browser
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@518
    23
import java.lang.reflect.Constructor;
jaroslav@413
    24
import java.lang.reflect.InvocationTargetException;
jaroslav@372
    25
import java.lang.reflect.Method;
jaroslav@372
    26
import java.util.Map;
jaroslav@372
    27
import java.util.WeakHashMap;
jaroslav@384
    28
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@372
    29
import org.apidesign.bck2brwsr.launcher.MethodInvocation;
jaroslav@372
    30
import org.testng.ITest;
jaroslav@372
    31
import org.testng.annotations.Test;
jaroslav@372
    32
jaroslav@372
    33
/**
jaroslav@372
    34
 *
jaroslav@372
    35
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@372
    36
 */
jaroslav@372
    37
public final class Bck2BrwsrCase implements ITest {
jaroslav@372
    38
    private final Method m;
jaroslav@384
    39
    private final Launcher l;
jaroslav@383
    40
    private final String type;
jaroslav@518
    41
    private final boolean fail;
jaroslav@372
    42
    Object value;
jaroslav@372
    43
    private static final Map<Class, Object[]> compiled = new WeakHashMap<>();
jaroslav@372
    44
    private Object inst;
jaroslav@372
    45
jaroslav@518
    46
    Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail) {
jaroslav@372
    47
        this.l = l;
jaroslav@372
    48
        this.m = m;
jaroslav@372
    49
        this.type = type;
jaroslav@518
    50
        this.fail = fail;
jaroslav@372
    51
    }
jaroslav@372
    52
jaroslav@372
    53
    @Test(groups = "run")
jaroslav@372
    54
    public void executeCode() throws Throwable {
jaroslav@383
    55
        if (l != null) {
jaroslav@383
    56
            MethodInvocation c = l.invokeMethod(m.getDeclaringClass(), m.getName());
jaroslav@518
    57
            String res = c.toString();
jaroslav@518
    58
            value = res;
jaroslav@518
    59
            if (fail) {
jaroslav@518
    60
                int idx = res.indexOf(':');
jaroslav@518
    61
                if (idx >= 0) {
jaroslav@518
    62
                    Class<? extends Throwable> thrwbl = null;
jaroslav@518
    63
                    try {
jaroslav@518
    64
                        Class<?> exCls = Class.forName(res.substring(0, idx));
jaroslav@518
    65
                        if (Throwable.class.isAssignableFrom(exCls)) {
jaroslav@518
    66
                            thrwbl = exCls.asSubclass(Throwable.class);
jaroslav@518
    67
                        }
jaroslav@518
    68
                    } catch (Exception ex) {
jaroslav@518
    69
                        // ignore
jaroslav@518
    70
                    }
jaroslav@518
    71
                    if (thrwbl != null) {
jaroslav@518
    72
                        Throwable t = null;
jaroslav@518
    73
                        try {
jaroslav@518
    74
                            for (Constructor<?> cnstr : thrwbl.getConstructors()) {
jaroslav@518
    75
                                if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
jaroslav@518
    76
                                    t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
jaroslav@518
    77
                                    break;
jaroslav@518
    78
                                }
jaroslav@518
    79
                            }
jaroslav@518
    80
                        } catch (Throwable ex) {
jaroslav@518
    81
                            t = thrwbl.newInstance().initCause(ex);
jaroslav@518
    82
                        }
jaroslav@518
    83
                        if (t == null) {
jaroslav@518
    84
                            t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
jaroslav@518
    85
                        }
jaroslav@518
    86
                        throw t;
jaroslav@518
    87
                    }
jaroslav@518
    88
                    throw new AssertionError(res);
jaroslav@518
    89
                }
jaroslav@518
    90
            }
jaroslav@372
    91
        } else {
jaroslav@413
    92
            try {
jaroslav@413
    93
                value = m.invoke(m.getDeclaringClass().newInstance());
jaroslav@413
    94
            } catch (InvocationTargetException ex) {
jaroslav@413
    95
                Throwable t = ex.getTargetException();
jaroslav@413
    96
                value = t.getClass().getName() + ":" + t.getMessage();
jaroslav@413
    97
            }
jaroslav@372
    98
        }
jaroslav@372
    99
    }
jaroslav@372
   100
jaroslav@372
   101
    @Override
jaroslav@372
   102
    public String getTestName() {
jaroslav@372
   103
        return m.getName() + "[" + typeName() + "]";
jaroslav@372
   104
    }
jaroslav@372
   105
jaroslav@372
   106
    final String typeName() {
jaroslav@383
   107
        return type;
jaroslav@372
   108
    }
jaroslav@393
   109
    static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
jaroslav@393
   110
        File f = File.createTempFile(c.m.getName(), ".js");
jaroslav@393
   111
        try (final FileWriter w = new FileWriter(f)) {
jaroslav@393
   112
            w.append(c.l.toString());
jaroslav@393
   113
        }
jaroslav@393
   114
        sb.append("Path: ").append(f.getPath());
jaroslav@393
   115
    }
jaroslav@372
   116
}