rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 667 vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java@5866e89ef568
child 939 ce08b0b23949
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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.FileWriter;
    23 import java.io.IOException;
    24 import java.io.InputStream;
    25 import java.lang.reflect.Constructor;
    26 import java.lang.reflect.InvocationTargetException;
    27 import java.lang.reflect.Method;
    28 import org.apidesign.bck2brwsr.launcher.Launcher;
    29 import org.apidesign.bck2brwsr.launcher.InvocationContext;
    30 import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
    31 import org.apidesign.bck2brwsr.vmtest.Http;
    32 import org.testng.ITest;
    33 import org.testng.annotations.Test;
    34 
    35 /**
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public final class Bck2BrwsrCase implements ITest {
    40     private final Method m;
    41     private final Launcher l;
    42     private final String type;
    43     private final boolean fail;
    44     private final HtmlFragment html;
    45     private final Http.Resource[] http;
    46     Object value;
    47 
    48     Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
    49         this.l = l;
    50         this.m = m;
    51         this.type = type;
    52         this.fail = fail;
    53         this.html = html;
    54         this.http = http;
    55     }
    56 
    57     @Test(groups = "run")
    58     public void executeCode() throws Throwable {
    59         if (l != null) {
    60             InvocationContext c = l.createInvocation(m.getDeclaringClass(), m.getName());
    61             if (html != null) {
    62                 c.setHtmlFragment(html.value());
    63             }
    64             if (http != null) {
    65                 for (Http.Resource r : http) {
    66                     if (!r.content().isEmpty()) {
    67                         InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
    68                         c.addHttpResource(r.path(), r.mimeType(), is);
    69                     } else {
    70                         InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
    71                         c.addHttpResource(r.path(), r.mimeType(), is);
    72                     }
    73                 }
    74             }
    75             String res = c.invoke();
    76             value = res;
    77             if (fail) {
    78                 int idx = res.indexOf(':');
    79                 if (idx >= 0) {
    80                     Class<? extends Throwable> thrwbl = null;
    81                     try {
    82                         Class<?> exCls = Class.forName(res.substring(0, idx));
    83                         if (Throwable.class.isAssignableFrom(exCls)) {
    84                             thrwbl = exCls.asSubclass(Throwable.class);
    85                         }
    86                     } catch (Exception ex) {
    87                         // ignore
    88                     }
    89                     if (thrwbl != null) {
    90                         Throwable t = null;
    91                         try {
    92                             for (Constructor<?> cnstr : thrwbl.getConstructors()) {
    93                                 if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
    94                                     t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
    95                                     break;
    96                                 }
    97                             }
    98                         } catch (Throwable ex) {
    99                             t = thrwbl.newInstance().initCause(ex);
   100                         }
   101                         if (t == null) {
   102                             t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
   103                         }
   104                         throw t;
   105                     }
   106                     throw new AssertionError(res);
   107                 }
   108             }
   109         } else {
   110             try {
   111                 value = m.invoke(m.getDeclaringClass().newInstance());
   112             } catch (InvocationTargetException ex) {
   113                 Throwable t = ex.getTargetException();
   114                 value = t.getClass().getName() + ":" + t.getMessage();
   115                 if (t instanceof AssertionError) {
   116                     throw t;
   117                 }
   118             }
   119         }
   120     }
   121 
   122     @Override
   123     public String getTestName() {
   124         return m.getName() + "[" + typeName() + "]";
   125     }
   126 
   127     final String typeName() {
   128         return type;
   129     }
   130     static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
   131         File f = File.createTempFile(c.m.getName(), ".js");
   132         try (final FileWriter w = new FileWriter(f)) {
   133             w.append(c.l.toString());
   134         }
   135         sb.append("Path: ").append(f.getPath());
   136     }
   137 }