rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Jan 2016 04:36:23 +0100
changeset 1860 4ce38f21f4cd
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
JavaScript in a browser can be at most three times slower than HotSpot when computing five or ten thousands of prime numbers
jaroslav@372
     1
/**
jaroslav@372
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 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@1762
    22
import java.io.FileOutputStream;
jaroslav@393
    23
import java.io.IOException;
jaroslav@626
    24
import java.io.InputStream;
jaroslav@1762
    25
import java.io.OutputStreamWriter;
jaroslav@1762
    26
import java.io.Writer;
jaroslav@518
    27
import java.lang.reflect.Constructor;
jaroslav@413
    28
import java.lang.reflect.InvocationTargetException;
jaroslav@372
    29
import java.lang.reflect.Method;
jaroslav@384
    30
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@622
    31
import org.apidesign.bck2brwsr.launcher.InvocationContext;
jaroslav@623
    32
import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
jaroslav@667
    33
import org.apidesign.bck2brwsr.vmtest.Http;
jaroslav@372
    34
import org.testng.ITest;
jaroslav@372
    35
import org.testng.annotations.Test;
jaroslav@372
    36
jaroslav@372
    37
/**
jaroslav@372
    38
 *
jaroslav@372
    39
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@372
    40
 */
jaroslav@372
    41
public final class Bck2BrwsrCase implements ITest {
jaroslav@372
    42
    private final Method m;
jaroslav@384
    43
    private final Launcher l;
jaroslav@383
    44
    private final String type;
jaroslav@518
    45
    private final boolean fail;
jaroslav@623
    46
    private final HtmlFragment html;
jaroslav@667
    47
    private final Http.Resource[] http;
jaroslav@1525
    48
    private final InvocationContext c;
jaroslav@372
    49
    Object value;
jaroslav@1860
    50
    int time;
jaroslav@372
    51
jaroslav@667
    52
    Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
jaroslav@372
    53
        this.l = l;
jaroslav@372
    54
        this.m = m;
jaroslav@372
    55
        this.type = type;
jaroslav@518
    56
        this.fail = fail;
jaroslav@526
    57
        this.html = html;
jaroslav@623
    58
        this.http = http;
jaroslav@1525
    59
        this.c = l != null ? l.createInvocation(m.getDeclaringClass(), m.getName()) : null;
jaroslav@372
    60
    }
jaroslav@372
    61
jaroslav@372
    62
    @Test(groups = "run")
jaroslav@372
    63
    public void executeCode() throws Throwable {
jaroslav@383
    64
        if (l != null) {
jaroslav@622
    65
            if (html != null) {
jaroslav@623
    66
                c.setHtmlFragment(html.value());
jaroslav@623
    67
            }
jaroslav@623
    68
            if (http != null) {
jaroslav@667
    69
                for (Http.Resource r : http) {
jaroslav@667
    70
                    if (!r.content().isEmpty()) {
jaroslav@667
    71
                        InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
jaroslav@954
    72
                        c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
jaroslav@667
    73
                    } else {
jaroslav@667
    74
                        InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
jaroslav@954
    75
                        c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
jaroslav@667
    76
                    }
jaroslav@626
    77
                }
jaroslav@622
    78
            }
jaroslav@1860
    79
            int[] time = { 0 };
jaroslav@1860
    80
            String res = c.invoke(time);
jaroslav@1860
    81
            this.value = res;
jaroslav@1860
    82
            this.time = time[0];
jaroslav@518
    83
            if (fail) {
jaroslav@939
    84
                int idx = res == null ? -1 : res.indexOf(':');
jaroslav@518
    85
                if (idx >= 0) {
jaroslav@518
    86
                    Class<? extends Throwable> thrwbl = null;
jaroslav@518
    87
                    try {
jaroslav@518
    88
                        Class<?> exCls = Class.forName(res.substring(0, idx));
jaroslav@518
    89
                        if (Throwable.class.isAssignableFrom(exCls)) {
jaroslav@518
    90
                            thrwbl = exCls.asSubclass(Throwable.class);
jaroslav@518
    91
                        }
jaroslav@518
    92
                    } catch (Exception ex) {
jaroslav@518
    93
                        // ignore
jaroslav@518
    94
                    }
jaroslav@518
    95
                    if (thrwbl != null) {
jaroslav@518
    96
                        Throwable t = null;
jaroslav@518
    97
                        try {
jaroslav@518
    98
                            for (Constructor<?> cnstr : thrwbl.getConstructors()) {
jaroslav@518
    99
                                if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
jaroslav@518
   100
                                    t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
jaroslav@518
   101
                                    break;
jaroslav@518
   102
                                }
jaroslav@518
   103
                            }
jaroslav@518
   104
                        } catch (Throwable ex) {
jaroslav@518
   105
                            t = thrwbl.newInstance().initCause(ex);
jaroslav@518
   106
                        }
jaroslav@518
   107
                        if (t == null) {
jaroslav@518
   108
                            t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
jaroslav@518
   109
                        }
jaroslav@518
   110
                        throw t;
jaroslav@518
   111
                    }
jaroslav@518
   112
                    throw new AssertionError(res);
jaroslav@518
   113
                }
jaroslav@518
   114
            }
jaroslav@372
   115
        } else {
jaroslav@413
   116
            try {
jaroslav@1860
   117
                long now = System.currentTimeMillis();
jaroslav@413
   118
                value = m.invoke(m.getDeclaringClass().newInstance());
jaroslav@1860
   119
                time = (int) (System.currentTimeMillis() - now);
jaroslav@413
   120
            } catch (InvocationTargetException ex) {
jaroslav@413
   121
                Throwable t = ex.getTargetException();
jaroslav@413
   122
                value = t.getClass().getName() + ":" + t.getMessage();
jaroslav@641
   123
                if (t instanceof AssertionError) {
jaroslav@641
   124
                    throw t;
jaroslav@641
   125
                }
jaroslav@413
   126
            }
jaroslav@372
   127
        }
jaroslav@372
   128
    }
jaroslav@372
   129
jaroslav@372
   130
    @Override
jaroslav@372
   131
    public String getTestName() {
jaroslav@372
   132
        return m.getName() + "[" + typeName() + "]";
jaroslav@372
   133
    }
jaroslav@372
   134
jaroslav@372
   135
    final String typeName() {
jaroslav@383
   136
        return type;
jaroslav@372
   137
    }
jaroslav@393
   138
    static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
jaroslav@393
   139
        File f = File.createTempFile(c.m.getName(), ".js");
jaroslav@1762
   140
        try (final Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8")) {
jaroslav@393
   141
            w.append(c.l.toString());
jaroslav@393
   142
        }
jaroslav@393
   143
        sb.append("Path: ").append(f.getPath());
jaroslav@393
   144
    }
jaroslav@372
   145
}