rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 05 May 2014 10:16:30 +0200
branchclosure
changeset 1525 777bd3ed81ba
parent 954 6448c284fe21
child 1762 293838e72201
permissions -rw-r--r--
Export only test classes, classes from exported packages and those referenced in META-INF/services
     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     private final InvocationContext c;
    47     Object value;
    48 
    49     Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
    50         this.l = l;
    51         this.m = m;
    52         this.type = type;
    53         this.fail = fail;
    54         this.html = html;
    55         this.http = http;
    56         this.c = l != null ? l.createInvocation(m.getDeclaringClass(), m.getName()) : null;
    57     }
    58 
    59     @Test(groups = "run")
    60     public void executeCode() throws Throwable {
    61         if (l != null) {
    62             if (html != null) {
    63                 c.setHtmlFragment(html.value());
    64             }
    65             if (http != null) {
    66                 for (Http.Resource r : http) {
    67                     if (!r.content().isEmpty()) {
    68                         InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
    69                         c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
    70                     } else {
    71                         InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
    72                         c.addHttpResource(r.path(), r.mimeType(), r.parameters(), is);
    73                     }
    74                 }
    75             }
    76             String res = c.invoke();
    77             value = res;
    78             if (fail) {
    79                 int idx = res == null ? -1 : res.indexOf(':');
    80                 if (idx >= 0) {
    81                     Class<? extends Throwable> thrwbl = null;
    82                     try {
    83                         Class<?> exCls = Class.forName(res.substring(0, idx));
    84                         if (Throwable.class.isAssignableFrom(exCls)) {
    85                             thrwbl = exCls.asSubclass(Throwable.class);
    86                         }
    87                     } catch (Exception ex) {
    88                         // ignore
    89                     }
    90                     if (thrwbl != null) {
    91                         Throwable t = null;
    92                         try {
    93                             for (Constructor<?> cnstr : thrwbl.getConstructors()) {
    94                                 if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
    95                                     t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
    96                                     break;
    97                                 }
    98                             }
    99                         } catch (Throwable ex) {
   100                             t = thrwbl.newInstance().initCause(ex);
   101                         }
   102                         if (t == null) {
   103                             t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
   104                         }
   105                         throw t;
   106                     }
   107                     throw new AssertionError(res);
   108                 }
   109             }
   110         } else {
   111             try {
   112                 value = m.invoke(m.getDeclaringClass().newInstance());
   113             } catch (InvocationTargetException ex) {
   114                 Throwable t = ex.getTargetException();
   115                 value = t.getClass().getName() + ":" + t.getMessage();
   116                 if (t instanceof AssertionError) {
   117                     throw t;
   118                 }
   119             }
   120         }
   121     }
   122 
   123     @Override
   124     public String getTestName() {
   125         return m.getName() + "[" + typeName() + "]";
   126     }
   127 
   128     final String typeName() {
   129         return type;
   130     }
   131     static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
   132         File f = File.createTempFile(c.m.getName(), ".js");
   133         try (final FileWriter w = new FileWriter(f)) {
   134             w.append(c.l.toString());
   135         }
   136         sb.append("Path: ").append(f.getPath());
   137     }
   138 }