rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.vmtest.impl;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.File;
    22 import java.io.FileOutputStream;
    23 import java.io.IOException;
    24 import java.io.InputStream;
    25 import java.io.OutputStreamWriter;
    26 import java.io.Writer;
    27 import java.lang.reflect.Constructor;
    28 import java.lang.reflect.InvocationTargetException;
    29 import java.lang.reflect.Method;
    30 import org.apidesign.bck2brwsr.launcher.Launcher;
    31 import org.apidesign.bck2brwsr.launcher.InvocationContext;
    32 import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
    33 import org.apidesign.bck2brwsr.vmtest.Http;
    34 import org.testng.ITest;
    35 import org.testng.annotations.Test;
    36 
    37 /**
    38  *
    39  * @author Jaroslav Tulach <jtulach@netbeans.org>
    40  */
    41 public final class Bck2BrwsrCase implements ITest {
    42     private final Method m;
    43     private final Launcher l;
    44     private final String type;
    45     private final boolean fail;
    46     private final HtmlFragment html;
    47     private final Http.Resource[] http;
    48     private final InvocationContext c;
    49     Object value;
    50     int time;
    51 
    52     Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
    53         this.l = l;
    54         this.m = m;
    55         this.type = type;
    56         this.fail = fail;
    57         this.html = html;
    58         this.http = http;
    59         this.c = l != null ? l.createInvocation(m.getDeclaringClass(), m.getName()) : null;
    60     }
    61 
    62     @Test(groups = "run")
    63     public void executeCode() throws Throwable {
    64         if (l != null) {
    65             if (html != null) {
    66                 c.setHtmlFragment(html.value());
    67             }
    68             if (http != null) {
    69                 for (Http.Resource r : http) {
    70                     if (!r.content().isEmpty()) {
    71                         InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
    72                         c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
    73                     } else {
    74                         InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
    75                         c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
    76                     }
    77                 }
    78             }
    79             int[] time = { 0 };
    80             String res = c.invoke(time);
    81             this.value = res;
    82             this.time = time[0];
    83             if (fail) {
    84                 int idx = res == null ? -1 : res.indexOf(':');
    85                 if (idx >= 0) {
    86                     Class<? extends Throwable> thrwbl = null;
    87                     try {
    88                         Class<?> exCls = Class.forName(res.substring(0, idx));
    89                         if (Throwable.class.isAssignableFrom(exCls)) {
    90                             thrwbl = exCls.asSubclass(Throwable.class);
    91                         }
    92                     } catch (Exception ex) {
    93                         // ignore
    94                     }
    95                     if (thrwbl != null) {
    96                         Throwable t = null;
    97                         try {
    98                             for (Constructor<?> cnstr : thrwbl.getConstructors()) {
    99                                 if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
   100                                     t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
   101                                     break;
   102                                 }
   103                             }
   104                         } catch (Throwable ex) {
   105                             t = thrwbl.newInstance().initCause(ex);
   106                         }
   107                         if (t == null) {
   108                             t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
   109                         }
   110                         throw t;
   111                     }
   112                     throw new AssertionError(res);
   113                 }
   114             }
   115         } else {
   116             try {
   117                 long now = System.currentTimeMillis();
   118                 value = m.invoke(m.getDeclaringClass().newInstance());
   119                 time = (int) (System.currentTimeMillis() - now);
   120             } catch (InvocationTargetException ex) {
   121                 Throwable t = ex.getTargetException();
   122                 value = t.getClass().getName() + ":" + t.getMessage();
   123                 if (t instanceof AssertionError) {
   124                     throw t;
   125                 }
   126             }
   127         }
   128     }
   129 
   130     @Override
   131     public String getTestName() {
   132         return m.getName() + "[" + typeName() + "]";
   133     }
   134 
   135     final String typeName() {
   136         return type;
   137     }
   138     static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
   139         File f = File.createTempFile(c.m.getName(), ".js");
   140         try (final Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8")) {
   141             w.append(c.l.toString());
   142         }
   143         sb.append("Path: ").append(f.getPath());
   144     }
   145 }