vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Jan 2013 12:18:24 +0100
changeset 595 784aaf9ee179
parent 526 a0d8b5ab79a2
child 622 a07253cf2ca4
permissions -rw-r--r--
String.getBytes and InputStreamReader support 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.File;
    21 import java.io.FileWriter;
    22 import java.io.IOException;
    23 import java.lang.reflect.Constructor;
    24 import java.lang.reflect.InvocationTargetException;
    25 import java.lang.reflect.Method;
    26 import org.apidesign.bck2brwsr.launcher.Launcher;
    27 import org.apidesign.bck2brwsr.launcher.MethodInvocation;
    28 import org.testng.ITest;
    29 import org.testng.annotations.Test;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 public final class Bck2BrwsrCase implements ITest {
    36     private final Method m;
    37     private final Launcher l;
    38     private final String type;
    39     private final boolean fail;
    40     Object value;
    41     private final String html;
    42 
    43     Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, String html) {
    44         this.l = l;
    45         this.m = m;
    46         this.type = type;
    47         this.fail = fail;
    48         this.html = html;
    49     }
    50 
    51     @Test(groups = "run")
    52     public void executeCode() throws Throwable {
    53         if (l != null) {
    54             MethodInvocation c = l.invokeMethod(m.getDeclaringClass(), m.getName(), html);
    55             String res = c.toString();
    56             value = res;
    57             if (fail) {
    58                 int idx = res.indexOf(':');
    59                 if (idx >= 0) {
    60                     Class<? extends Throwable> thrwbl = null;
    61                     try {
    62                         Class<?> exCls = Class.forName(res.substring(0, idx));
    63                         if (Throwable.class.isAssignableFrom(exCls)) {
    64                             thrwbl = exCls.asSubclass(Throwable.class);
    65                         }
    66                     } catch (Exception ex) {
    67                         // ignore
    68                     }
    69                     if (thrwbl != null) {
    70                         Throwable t = null;
    71                         try {
    72                             for (Constructor<?> cnstr : thrwbl.getConstructors()) {
    73                                 if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
    74                                     t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
    75                                     break;
    76                                 }
    77                             }
    78                         } catch (Throwable ex) {
    79                             t = thrwbl.newInstance().initCause(ex);
    80                         }
    81                         if (t == null) {
    82                             t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
    83                         }
    84                         throw t;
    85                     }
    86                     throw new AssertionError(res);
    87                 }
    88             }
    89         } else {
    90             try {
    91                 value = m.invoke(m.getDeclaringClass().newInstance());
    92             } catch (InvocationTargetException ex) {
    93                 Throwable t = ex.getTargetException();
    94                 value = t.getClass().getName() + ":" + t.getMessage();
    95             }
    96         }
    97     }
    98 
    99     @Override
   100     public String getTestName() {
   101         return m.getName() + "[" + typeName() + "]";
   102     }
   103 
   104     final String typeName() {
   105         return type;
   106     }
   107     static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
   108         File f = File.createTempFile(c.m.getName(), ".js");
   109         try (final FileWriter w = new FileWriter(f)) {
   110             w.append(c.l.toString());
   111         }
   112         sb.append("Path: ").append(f.getPath());
   113     }
   114 }