vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 31 Jan 2013 19:21:37 +0100
branchemul
changeset 626 f08eb4df84c1
parent 623 4af0d3dedb9d
child 641 d4aaa46cac0d
permissions -rw-r--r--
Introducing resource attribute for @HttpResource
     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.HttpResource;
    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 HttpResource http;
    46     Object value;
    47 
    48     Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, HttpResource 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                 if (!http.content().isEmpty()) {
    66                     InputStream is = new ByteArrayInputStream(http.content().getBytes("UTF-8"));
    67                     c.setHttpResource(http.path(), http.mimeType(), is);
    68                 } else {
    69                     InputStream is = m.getDeclaringClass().getResourceAsStream(http.resource());
    70                     c.setHttpResource(http.path(), http.mimeType(), is);
    71                 }
    72             }
    73             String res = c.invoke();
    74             value = res;
    75             if (fail) {
    76                 int idx = res.indexOf(':');
    77                 if (idx >= 0) {
    78                     Class<? extends Throwable> thrwbl = null;
    79                     try {
    80                         Class<?> exCls = Class.forName(res.substring(0, idx));
    81                         if (Throwable.class.isAssignableFrom(exCls)) {
    82                             thrwbl = exCls.asSubclass(Throwable.class);
    83                         }
    84                     } catch (Exception ex) {
    85                         // ignore
    86                     }
    87                     if (thrwbl != null) {
    88                         Throwable t = null;
    89                         try {
    90                             for (Constructor<?> cnstr : thrwbl.getConstructors()) {
    91                                 if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
    92                                     t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
    93                                     break;
    94                                 }
    95                             }
    96                         } catch (Throwable ex) {
    97                             t = thrwbl.newInstance().initCause(ex);
    98                         }
    99                         if (t == null) {
   100                             t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
   101                         }
   102                         throw t;
   103                     }
   104                     throw new AssertionError(res);
   105                 }
   106             }
   107         } else {
   108             try {
   109                 value = m.invoke(m.getDeclaringClass().newInstance());
   110             } catch (InvocationTargetException ex) {
   111                 Throwable t = ex.getTargetException();
   112                 value = t.getClass().getName() + ":" + t.getMessage();
   113             }
   114         }
   115     }
   116 
   117     @Override
   118     public String getTestName() {
   119         return m.getName() + "[" + typeName() + "]";
   120     }
   121 
   122     final String typeName() {
   123         return type;
   124     }
   125     static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
   126         File f = File.createTempFile(c.m.getName(), ".js");
   127         try (final FileWriter w = new FileWriter(f)) {
   128             w.append(c.l.toString());
   129         }
   130         sb.append("Path: ").append(f.getPath());
   131     }
   132 }