vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
branchlauncher
changeset 372 3485327d3080
child 382 57fc3a0563c9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java	Sun Dec 23 23:30:06 2012 +0100
     1.3 @@ -0,0 +1,130 @@
     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.lang.reflect.Method;
    1.24 +import java.util.Map;
    1.25 +import java.util.WeakHashMap;
    1.26 +import javax.script.Invocable;
    1.27 +import org.apidesign.bck2brwsr.launcher.MethodInvocation;
    1.28 +import org.testng.ITest;
    1.29 +import org.testng.annotations.Test;
    1.30 +
    1.31 +/**
    1.32 + *
    1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.34 + */
    1.35 +public final class Bck2BrwsrCase implements ITest {
    1.36 +    private final Method m;
    1.37 +    private final Launchers l;
    1.38 +    private final int type;
    1.39 +    Object value;
    1.40 +    private Invocable code;
    1.41 +    private CharSequence codeSeq;
    1.42 +    private static final Map<Class, Object[]> compiled = new WeakHashMap<>();
    1.43 +    private Object inst;
    1.44 +
    1.45 +    Bck2BrwsrCase(Method m, int type, Launchers l) {
    1.46 +        this.l = l;
    1.47 +        this.m = m;
    1.48 +        this.type = type;
    1.49 +    }
    1.50 +
    1.51 +    @Test(groups = "run")
    1.52 +    public void executeCode() throws Throwable {
    1.53 +        if (type == 1) {
    1.54 +            MethodInvocation c = l.addMethod(m.getDeclaringClass(), m.getName(), false);
    1.55 +            value = c.toString();
    1.56 +        } else if (type == 2) {
    1.57 +            MethodInvocation c = l.addMethod(m.getDeclaringClass(), m.getName(), true);
    1.58 +            value = c.toString();
    1.59 +        } else {
    1.60 +            value = m.invoke(m.getDeclaringClass().newInstance());
    1.61 +        }
    1.62 +    }
    1.63 +
    1.64 +    @Override
    1.65 +    public String getTestName() {
    1.66 +        return m.getName() + "[" + typeName() + "]";
    1.67 +    }
    1.68 +
    1.69 +    final String typeName() {
    1.70 +        switch (type) {
    1.71 +            case 0:
    1.72 +                return "Java";
    1.73 +            case 1:
    1.74 +                return "JavaScript";
    1.75 +            case 2:
    1.76 +                return "Browser";
    1.77 +            default:
    1.78 +                return "Unknown type " + type;
    1.79 +        }
    1.80 +    }
    1.81 +
    1.82 +    private static String computeSignature(Method m) {
    1.83 +        StringBuilder sb = new StringBuilder();
    1.84 +        appendType(sb, m.getReturnType());
    1.85 +        for (Class<?> c : m.getParameterTypes()) {
    1.86 +            appendType(sb, c);
    1.87 +        }
    1.88 +        return sb.toString();
    1.89 +    }
    1.90 +
    1.91 +    private static void appendType(StringBuilder sb, Class<?> t) {
    1.92 +        if (t == null) {
    1.93 +            sb.append('V');
    1.94 +            return;
    1.95 +        }
    1.96 +        if (t.isPrimitive()) {
    1.97 +            int ch = -1;
    1.98 +            if (t == int.class) {
    1.99 +                ch = 'I';
   1.100 +            }
   1.101 +            if (t == short.class) {
   1.102 +                ch = 'S';
   1.103 +            }
   1.104 +            if (t == byte.class) {
   1.105 +                ch = 'B';
   1.106 +            }
   1.107 +            if (t == boolean.class) {
   1.108 +                ch = 'Z';
   1.109 +            }
   1.110 +            if (t == long.class) {
   1.111 +                ch = 'J';
   1.112 +            }
   1.113 +            if (t == float.class) {
   1.114 +                ch = 'F';
   1.115 +            }
   1.116 +            if (t == double.class) {
   1.117 +                ch = 'D';
   1.118 +            }
   1.119 +            assert ch != -1 : "Unknown primitive type " + t;
   1.120 +            sb.append((char) ch);
   1.121 +            return;
   1.122 +        }
   1.123 +        if (t.isArray()) {
   1.124 +            sb.append("_3");
   1.125 +            appendType(sb, t.getComponentType());
   1.126 +            return;
   1.127 +        }
   1.128 +        sb.append('L');
   1.129 +        sb.append(t.getName().replace('.', '_'));
   1.130 +        sb.append("_2");
   1.131 +    }
   1.132 +    
   1.133 +}