vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Feb 2013 12:58:12 +0100
branchemul
changeset 694 0d277415ed02
parent 641 d4aaa46cac0d
permissions -rw-r--r--
Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
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@626
    20
import java.io.ByteArrayInputStream;
jaroslav@393
    21
import java.io.File;
jaroslav@393
    22
import java.io.FileWriter;
jaroslav@393
    23
import java.io.IOException;
jaroslav@626
    24
import java.io.InputStream;
jaroslav@518
    25
import java.lang.reflect.Constructor;
jaroslav@413
    26
import java.lang.reflect.InvocationTargetException;
jaroslav@372
    27
import java.lang.reflect.Method;
jaroslav@384
    28
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@622
    29
import org.apidesign.bck2brwsr.launcher.InvocationContext;
jaroslav@623
    30
import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
jaroslav@667
    31
import org.apidesign.bck2brwsr.vmtest.Http;
jaroslav@372
    32
import org.testng.ITest;
jaroslav@372
    33
import org.testng.annotations.Test;
jaroslav@372
    34
jaroslav@372
    35
/**
jaroslav@372
    36
 *
jaroslav@372
    37
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@372
    38
 */
jaroslav@372
    39
public final class Bck2BrwsrCase implements ITest {
jaroslav@372
    40
    private final Method m;
jaroslav@384
    41
    private final Launcher l;
jaroslav@383
    42
    private final String type;
jaroslav@518
    43
    private final boolean fail;
jaroslav@623
    44
    private final HtmlFragment html;
jaroslav@667
    45
    private final Http.Resource[] http;
jaroslav@372
    46
    Object value;
jaroslav@372
    47
jaroslav@667
    48
    Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
jaroslav@372
    49
        this.l = l;
jaroslav@372
    50
        this.m = m;
jaroslav@372
    51
        this.type = type;
jaroslav@518
    52
        this.fail = fail;
jaroslav@526
    53
        this.html = html;
jaroslav@623
    54
        this.http = http;
jaroslav@372
    55
    }
jaroslav@372
    56
jaroslav@372
    57
    @Test(groups = "run")
jaroslav@372
    58
    public void executeCode() throws Throwable {
jaroslav@383
    59
        if (l != null) {
jaroslav@622
    60
            InvocationContext c = l.createInvocation(m.getDeclaringClass(), m.getName());
jaroslav@622
    61
            if (html != null) {
jaroslav@623
    62
                c.setHtmlFragment(html.value());
jaroslav@623
    63
            }
jaroslav@623
    64
            if (http != null) {
jaroslav@667
    65
                for (Http.Resource r : http) {
jaroslav@667
    66
                    if (!r.content().isEmpty()) {
jaroslav@667
    67
                        InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
jaroslav@667
    68
                        c.addHttpResource(r.path(), r.mimeType(), is);
jaroslav@667
    69
                    } else {
jaroslav@667
    70
                        InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
jaroslav@667
    71
                        c.addHttpResource(r.path(), r.mimeType(), is);
jaroslav@667
    72
                    }
jaroslav@626
    73
                }
jaroslav@622
    74
            }
jaroslav@622
    75
            String res = c.invoke();
jaroslav@518
    76
            value = res;
jaroslav@518
    77
            if (fail) {
jaroslav@518
    78
                int idx = res.indexOf(':');
jaroslav@518
    79
                if (idx >= 0) {
jaroslav@518
    80
                    Class<? extends Throwable> thrwbl = null;
jaroslav@518
    81
                    try {
jaroslav@518
    82
                        Class<?> exCls = Class.forName(res.substring(0, idx));
jaroslav@518
    83
                        if (Throwable.class.isAssignableFrom(exCls)) {
jaroslav@518
    84
                            thrwbl = exCls.asSubclass(Throwable.class);
jaroslav@518
    85
                        }
jaroslav@518
    86
                    } catch (Exception ex) {
jaroslav@518
    87
                        // ignore
jaroslav@518
    88
                    }
jaroslav@518
    89
                    if (thrwbl != null) {
jaroslav@518
    90
                        Throwable t = null;
jaroslav@518
    91
                        try {
jaroslav@518
    92
                            for (Constructor<?> cnstr : thrwbl.getConstructors()) {
jaroslav@518
    93
                                if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
jaroslav@518
    94
                                    t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
jaroslav@518
    95
                                    break;
jaroslav@518
    96
                                }
jaroslav@518
    97
                            }
jaroslav@518
    98
                        } catch (Throwable ex) {
jaroslav@518
    99
                            t = thrwbl.newInstance().initCause(ex);
jaroslav@518
   100
                        }
jaroslav@518
   101
                        if (t == null) {
jaroslav@518
   102
                            t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
jaroslav@518
   103
                        }
jaroslav@518
   104
                        throw t;
jaroslav@518
   105
                    }
jaroslav@518
   106
                    throw new AssertionError(res);
jaroslav@518
   107
                }
jaroslav@518
   108
            }
jaroslav@372
   109
        } else {
jaroslav@413
   110
            try {
jaroslav@413
   111
                value = m.invoke(m.getDeclaringClass().newInstance());
jaroslav@413
   112
            } catch (InvocationTargetException ex) {
jaroslav@413
   113
                Throwable t = ex.getTargetException();
jaroslav@413
   114
                value = t.getClass().getName() + ":" + t.getMessage();
jaroslav@641
   115
                if (t instanceof AssertionError) {
jaroslav@641
   116
                    throw t;
jaroslav@641
   117
                }
jaroslav@413
   118
            }
jaroslav@372
   119
        }
jaroslav@372
   120
    }
jaroslav@372
   121
jaroslav@372
   122
    @Override
jaroslav@372
   123
    public String getTestName() {
jaroslav@372
   124
        return m.getName() + "[" + typeName() + "]";
jaroslav@372
   125
    }
jaroslav@372
   126
jaroslav@372
   127
    final String typeName() {
jaroslav@383
   128
        return type;
jaroslav@372
   129
    }
jaroslav@393
   130
    static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
jaroslav@393
   131
        File f = File.createTempFile(c.m.getName(), ".js");
jaroslav@393
   132
        try (final FileWriter w = new FileWriter(f)) {
jaroslav@393
   133
            w.append(c.l.toString());
jaroslav@393
   134
        }
jaroslav@393
   135
        sb.append("Path: ").append(f.getPath());
jaroslav@393
   136
    }
jaroslav@372
   137
}