rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 Jan 2015 20:46:35 +0100
changeset 1762 293838e72201
parent 1525 777bd3ed81ba
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Always write down the JavaScript file in UTF-8 encoding
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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 
    51     Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
    52         this.l = l;
    53         this.m = m;
    54         this.type = type;
    55         this.fail = fail;
    56         this.html = html;
    57         this.http = http;
    58         this.c = l != null ? l.createInvocation(m.getDeclaringClass(), m.getName()) : null;
    59     }
    60 
    61     @Test(groups = "run")
    62     public void executeCode() throws Throwable {
    63         if (l != null) {
    64             if (html != null) {
    65                 c.setHtmlFragment(html.value());
    66             }
    67             if (http != null) {
    68                 for (Http.Resource r : http) {
    69                     if (!r.content().isEmpty()) {
    70                         InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
    71                         c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
    72                     } else {
    73                         InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
    74                         c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
    75                     }
    76                 }
    77             }
    78             String res = c.invoke();
    79             value = res;
    80             if (fail) {
    81                 int idx = res == null ? -1 : res.indexOf(':');
    82                 if (idx >= 0) {
    83                     Class<? extends Throwable> thrwbl = null;
    84                     try {
    85                         Class<?> exCls = Class.forName(res.substring(0, idx));
    86                         if (Throwable.class.isAssignableFrom(exCls)) {
    87                             thrwbl = exCls.asSubclass(Throwable.class);
    88                         }
    89                     } catch (Exception ex) {
    90                         // ignore
    91                     }
    92                     if (thrwbl != null) {
    93                         Throwable t = null;
    94                         try {
    95                             for (Constructor<?> cnstr : thrwbl.getConstructors()) {
    96                                 if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
    97                                     t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
    98                                     break;
    99                                 }
   100                             }
   101                         } catch (Throwable ex) {
   102                             t = thrwbl.newInstance().initCause(ex);
   103                         }
   104                         if (t == null) {
   105                             t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
   106                         }
   107                         throw t;
   108                     }
   109                     throw new AssertionError(res);
   110                 }
   111             }
   112         } else {
   113             try {
   114                 value = m.invoke(m.getDeclaringClass().newInstance());
   115             } catch (InvocationTargetException ex) {
   116                 Throwable t = ex.getTargetException();
   117                 value = t.getClass().getName() + ":" + t.getMessage();
   118                 if (t instanceof AssertionError) {
   119                     throw t;
   120                 }
   121             }
   122         }
   123     }
   124 
   125     @Override
   126     public String getTestName() {
   127         return m.getName() + "[" + typeName() + "]";
   128     }
   129 
   130     final String typeName() {
   131         return type;
   132     }
   133     static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
   134         File f = File.createTempFile(c.m.getName(), ".js");
   135         try (final Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8")) {
   136             w.append(c.l.toString());
   137         }
   138         sb.append("Path: ").append(f.getPath());
   139     }
   140 }