vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
branchcanvas
changeset 808 bd8b726902a3
parent 731 e8283f10a127
parent 807 e93506e603ad
child 809 5c61f5e4898e
     1.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java	Thu Feb 14 12:06:16 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,199 +0,0 @@
     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.tck;
    1.22 -
    1.23 -import java.lang.reflect.Method;
    1.24 -import java.util.Arrays;
    1.25 -import java.util.Collections;
    1.26 -import java.util.List;
    1.27 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.28 -import org.apidesign.bck2brwsr.vmtest.Compare;
    1.29 -import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.30 -import org.testng.annotations.Factory;
    1.31 -
    1.32 -/**
    1.33 - *
    1.34 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.35 - */
    1.36 -public class ReflectionTest {
    1.37 -    @Compare public boolean nonNullThis() {
    1.38 -        return this == null;
    1.39 -    }
    1.40 -    
    1.41 -    @Compare public String intType() {
    1.42 -        return Integer.TYPE.toString();
    1.43 -    }
    1.44 -
    1.45 -    @Compare public String voidType() throws Exception {
    1.46 -        return void.class.toString();
    1.47 -    }
    1.48 -
    1.49 -    @Compare public String longClass() {
    1.50 -        return long.class.toString();
    1.51 -    }
    1.52 -    
    1.53 -    @Compare public String namesOfMethods() {
    1.54 -        StringBuilder sb = new StringBuilder();
    1.55 -        String[] arr = new String[20];
    1.56 -        int i = 0;
    1.57 -        for (Method m : StaticUse.class.getMethods()) {
    1.58 -            arr[i++] = m.getName();
    1.59 -        }
    1.60 -        for (String s : sort(arr, i)) {
    1.61 -            sb.append(s).append("\n");
    1.62 -        }
    1.63 -        return sb.toString();
    1.64 -    }
    1.65 -    
    1.66 -    @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
    1.67 -        StaticUse.class.getMethod("instanceMethod").invoke(null);
    1.68 -        return "should not happen";
    1.69 -    }
    1.70 -
    1.71 -    @Compare public Object voidReturnType() throws Exception {
    1.72 -        return StaticUse.class.getMethod("instanceMethod").getReturnType();
    1.73 -    }
    1.74 -    
    1.75 -    enum E { A, B };
    1.76 -    @Compare public boolean isEnum() {
    1.77 -        return E.A.getClass().isEnum();
    1.78 -    }
    1.79 -
    1.80 -    @Compare public boolean isNotEnum() {
    1.81 -        return "".getClass().isEnum();
    1.82 -    }
    1.83 -    
    1.84 -    @Compare public String newInstanceFails() throws InstantiationException {
    1.85 -        try {
    1.86 -            return "success: " + StaticUse.class.newInstance();
    1.87 -        } catch (IllegalAccessException ex) {
    1.88 -            return ex.getClass().getName();
    1.89 -        }
    1.90 -    }
    1.91 -    
    1.92 -    @Compare public String paramTypes() throws Exception {
    1.93 -        Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
    1.94 -        final Class[] pt = plus.getParameterTypes();
    1.95 -        return pt[0].getName();
    1.96 -    }
    1.97 -    @Compare public String paramTypesNotFound() throws Exception {
    1.98 -        return StaticUse.class.getMethod("plus", int.class, double.class).toString();
    1.99 -    }
   1.100 -    @Compare public int methodWithArgs() throws Exception {
   1.101 -        Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   1.102 -        return (Integer)plus.invoke(null, 2, 3);
   1.103 -    }
   1.104 -    
   1.105 -    @Compare public String classGetNameForByte() {
   1.106 -         return byte.class.getName();
   1.107 -    }
   1.108 -    @Compare public String classGetNameForBaseObject() {
   1.109 -        return newObject().getClass().getName();
   1.110 -    }
   1.111 -    @Compare public String classGetNameForJavaObject() {
   1.112 -        return new Object().getClass().getName();
   1.113 -    }
   1.114 -    @Compare public String classGetNameForObjectArray() {
   1.115 -        return (new Object[3]).getClass().getName();
   1.116 -    }
   1.117 -    @Compare public String classGetNameForSimpleIntArray() {
   1.118 -        return (new int[3]).getClass().getName();
   1.119 -    }
   1.120 -    @Compare public boolean sameClassGetNameForSimpleCharArray() {
   1.121 -        return (new char[3]).getClass() == (new char[34]).getClass();
   1.122 -    }
   1.123 -    @Compare public String classGetNameForMultiIntArray() {
   1.124 -        return (new int[3][4][5][6][7][8][9]).getClass().getName();
   1.125 -    }
   1.126 -    @Compare public String classGetNameForMultiIntArrayInner() {
   1.127 -        final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   1.128 -        int[][][][][][] subarr = arr[0];
   1.129 -        int[][][][][] subsubarr = subarr[0];
   1.130 -        return subsubarr.getClass().getName();
   1.131 -    }
   1.132 -    @Compare public String classGetNameForMultiStringArray() {
   1.133 -        return (new String[3][4][5][6][7][8][9]).getClass().getName();
   1.134 -    }
   1.135 -    
   1.136 -    @Compare public String classForByte() throws Exception {
   1.137 -        return Class.forName("[Z").getName();
   1.138 -    }
   1.139 -
   1.140 -    @Compare public String classForUnknownArray() {
   1.141 -        try {
   1.142 -            return Class.forName("[W").getName();
   1.143 -        } catch (Exception ex) {
   1.144 -            return ex.getClass().getName();
   1.145 -        }
   1.146 -    }
   1.147 -    
   1.148 -    @Compare public String classForUnknownDeepArray() {
   1.149 -        try {
   1.150 -            return Class.forName("[[[[[W").getName();
   1.151 -        } catch (Exception ex) {
   1.152 -            return ex.getClass().getName();
   1.153 -        }
   1.154 -    }
   1.155 -    
   1.156 -    @Compare public String componentGetNameForObjectArray() {
   1.157 -        return (new Object[3]).getClass().getComponentType().getName();
   1.158 -    }
   1.159 -    @Compare public boolean sameComponentGetNameForObjectArray() {
   1.160 -        return (new Object[3]).getClass().getComponentType() == Object.class;
   1.161 -    }
   1.162 -    @Compare public String componentGetNameForSimpleIntArray() {
   1.163 -        return (new int[3]).getClass().getComponentType().getName();
   1.164 -    }
   1.165 -    @Compare public String componentGetNameForMultiIntArray() {
   1.166 -        return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   1.167 -    }
   1.168 -    @Compare public String componentGetNameForMultiStringArray() {
   1.169 -        Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   1.170 -        StringBuilder sb = new StringBuilder();
   1.171 -        for (;;) {
   1.172 -            sb.append(c.getName()).append("\n");
   1.173 -            c = c.getComponentType();
   1.174 -            if (c == null) {
   1.175 -                break;
   1.176 -            }
   1.177 -        }
   1.178 -        return sb.toString();
   1.179 -    }
   1.180 -    
   1.181 -    @Compare public boolean isArray() {
   1.182 -        return new Object[0].getClass().isArray();
   1.183 -    }
   1.184 -    
   1.185 -    @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   1.186 -    private static String[] sort(String[] arr, int len) {
   1.187 -        List<String> list = Arrays.asList(arr).subList(0, len);
   1.188 -        Collections.sort(list);
   1.189 -        return list.toArray(new String[0]);
   1.190 -    }
   1.191 -    
   1.192 -    @JavaScriptBody(args = {}, body = "return new Object();")
   1.193 -    private static Object newObject() {
   1.194 -        return new Object();
   1.195 -    }
   1.196 -    
   1.197 -    @Factory
   1.198 -    public static Object[] create() {
   1.199 -        return VMTest.create(ReflectionTest.class);
   1.200 -    }
   1.201 -    
   1.202 -}