rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
changeset 772 d382dacfd73f
parent 667 5866e89ef568
child 939 ce08b0b23949
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.vmtest.impl;
    1.22 +
    1.23 +import java.io.ByteArrayInputStream;
    1.24 +import java.io.File;
    1.25 +import java.io.FileWriter;
    1.26 +import java.io.IOException;
    1.27 +import java.io.InputStream;
    1.28 +import java.lang.reflect.Constructor;
    1.29 +import java.lang.reflect.InvocationTargetException;
    1.30 +import java.lang.reflect.Method;
    1.31 +import org.apidesign.bck2brwsr.launcher.Launcher;
    1.32 +import org.apidesign.bck2brwsr.launcher.InvocationContext;
    1.33 +import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
    1.34 +import org.apidesign.bck2brwsr.vmtest.Http;
    1.35 +import org.testng.ITest;
    1.36 +import org.testng.annotations.Test;
    1.37 +
    1.38 +/**
    1.39 + *
    1.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.41 + */
    1.42 +public final class Bck2BrwsrCase implements ITest {
    1.43 +    private final Method m;
    1.44 +    private final Launcher l;
    1.45 +    private final String type;
    1.46 +    private final boolean fail;
    1.47 +    private final HtmlFragment html;
    1.48 +    private final Http.Resource[] http;
    1.49 +    Object value;
    1.50 +
    1.51 +    Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
    1.52 +        this.l = l;
    1.53 +        this.m = m;
    1.54 +        this.type = type;
    1.55 +        this.fail = fail;
    1.56 +        this.html = html;
    1.57 +        this.http = http;
    1.58 +    }
    1.59 +
    1.60 +    @Test(groups = "run")
    1.61 +    public void executeCode() throws Throwable {
    1.62 +        if (l != null) {
    1.63 +            InvocationContext c = l.createInvocation(m.getDeclaringClass(), m.getName());
    1.64 +            if (html != null) {
    1.65 +                c.setHtmlFragment(html.value());
    1.66 +            }
    1.67 +            if (http != null) {
    1.68 +                for (Http.Resource r : http) {
    1.69 +                    if (!r.content().isEmpty()) {
    1.70 +                        InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
    1.71 +                        c.addHttpResource(r.path(), r.mimeType(), is);
    1.72 +                    } else {
    1.73 +                        InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
    1.74 +                        c.addHttpResource(r.path(), r.mimeType(), is);
    1.75 +                    }
    1.76 +                }
    1.77 +            }
    1.78 +            String res = c.invoke();
    1.79 +            value = res;
    1.80 +            if (fail) {
    1.81 +                int idx = res.indexOf(':');
    1.82 +                if (idx >= 0) {
    1.83 +                    Class<? extends Throwable> thrwbl = null;
    1.84 +                    try {
    1.85 +                        Class<?> exCls = Class.forName(res.substring(0, idx));
    1.86 +                        if (Throwable.class.isAssignableFrom(exCls)) {
    1.87 +                            thrwbl = exCls.asSubclass(Throwable.class);
    1.88 +                        }
    1.89 +                    } catch (Exception ex) {
    1.90 +                        // ignore
    1.91 +                    }
    1.92 +                    if (thrwbl != null) {
    1.93 +                        Throwable t = null;
    1.94 +                        try {
    1.95 +                            for (Constructor<?> cnstr : thrwbl.getConstructors()) {
    1.96 +                                if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) {
    1.97 +                                    t = (Throwable) cnstr.newInstance(res.substring(idx + 1));
    1.98 +                                    break;
    1.99 +                                }
   1.100 +                            }
   1.101 +                        } catch (Throwable ex) {
   1.102 +                            t = thrwbl.newInstance().initCause(ex);
   1.103 +                        }
   1.104 +                        if (t == null) {
   1.105 +                            t = thrwbl.newInstance().initCause(new Exception(res.substring(idx)));
   1.106 +                        }
   1.107 +                        throw t;
   1.108 +                    }
   1.109 +                    throw new AssertionError(res);
   1.110 +                }
   1.111 +            }
   1.112 +        } else {
   1.113 +            try {
   1.114 +                value = m.invoke(m.getDeclaringClass().newInstance());
   1.115 +            } catch (InvocationTargetException ex) {
   1.116 +                Throwable t = ex.getTargetException();
   1.117 +                value = t.getClass().getName() + ":" + t.getMessage();
   1.118 +                if (t instanceof AssertionError) {
   1.119 +                    throw t;
   1.120 +                }
   1.121 +            }
   1.122 +        }
   1.123 +    }
   1.124 +
   1.125 +    @Override
   1.126 +    public String getTestName() {
   1.127 +        return m.getName() + "[" + typeName() + "]";
   1.128 +    }
   1.129 +
   1.130 +    final String typeName() {
   1.131 +        return type;
   1.132 +    }
   1.133 +    static void dumpJS(StringBuilder sb, Bck2BrwsrCase c) throws IOException {
   1.134 +        File f = File.createTempFile(c.m.getName(), ".js");
   1.135 +        try (final FileWriter w = new FileWriter(f)) {
   1.136 +            w.append(c.l.toString());
   1.137 +        }
   1.138 +        sb.append("Path: ").append(f.getPath());
   1.139 +    }
   1.140 +}