1.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java Wed Feb 27 17:50:47 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,242 +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.annotation.Retention;
1.24 -import java.lang.annotation.RetentionPolicy;
1.25 -import java.lang.reflect.Method;
1.26 -import java.util.Arrays;
1.27 -import java.util.Collections;
1.28 -import java.util.List;
1.29 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.30 -import org.apidesign.bck2brwsr.vmtest.Compare;
1.31 -import org.apidesign.bck2brwsr.vmtest.VMTest;
1.32 -import org.testng.annotations.Factory;
1.33 -
1.34 -/**
1.35 - *
1.36 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.37 - */
1.38 -public class ReflectionTest {
1.39 - @Compare public boolean nonNullThis() {
1.40 - return this == null;
1.41 - }
1.42 -
1.43 - @Compare public String intType() {
1.44 - return Integer.TYPE.toString();
1.45 - }
1.46 -
1.47 - @Compare public String voidType() throws Exception {
1.48 - return void.class.toString();
1.49 - }
1.50 -
1.51 - @Compare public String longClass() {
1.52 - return long.class.toString();
1.53 - }
1.54 -
1.55 - @Compare public boolean isRunnableInterface() {
1.56 - return Runnable.class.isInterface();
1.57 - }
1.58 -
1.59 - @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
1.60 - return Runnable.class.getMethod("run").getName();
1.61 - }
1.62 -
1.63 - @Compare public String namesOfMethods() {
1.64 - StringBuilder sb = new StringBuilder();
1.65 - String[] arr = new String[20];
1.66 - int i = 0;
1.67 - for (Method m : StaticUse.class.getMethods()) {
1.68 - arr[i++] = m.getName();
1.69 - }
1.70 - for (String s : sort(arr, i)) {
1.71 - sb.append(s).append("\n");
1.72 - }
1.73 - return sb.toString();
1.74 - }
1.75 -
1.76 - @Compare public String namesOfDeclaringClassesOfMethods() {
1.77 - StringBuilder sb = new StringBuilder();
1.78 - String[] arr = new String[20];
1.79 - int i = 0;
1.80 - for (Method m : StaticUse.class.getMethods()) {
1.81 - arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
1.82 - }
1.83 - for (String s : sort(arr, i)) {
1.84 - sb.append(s).append("\n");
1.85 - }
1.86 - return sb.toString();
1.87 - }
1.88 -
1.89 - @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
1.90 - StaticUse.class.getMethod("instanceMethod").invoke(null);
1.91 - return "should not happen";
1.92 - }
1.93 -
1.94 - @Compare public Object voidReturnType() throws Exception {
1.95 - return StaticUse.class.getMethod("instanceMethod").getReturnType();
1.96 - }
1.97 -
1.98 - @Retention(RetentionPolicy.RUNTIME)
1.99 - @interface Ann {
1.100 - }
1.101 -
1.102 - @Compare public String annoClass() throws Exception {
1.103 - Retention r = Ann.class.getAnnotation(Retention.class);
1.104 - assert r != null : "Annotation is present";
1.105 - assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
1.106 - return r.annotationType().getName();
1.107 - }
1.108 -
1.109 - @Compare public boolean isAnnotation() {
1.110 - return Ann.class.isAnnotation();
1.111 - }
1.112 - @Compare public boolean isNotAnnotation() {
1.113 - return String.class.isAnnotation();
1.114 - }
1.115 - @Compare public boolean isNotAnnotationEnum() {
1.116 - return E.class.isAnnotation();
1.117 - }
1.118 - enum E { A, B };
1.119 - @Compare public boolean isEnum() {
1.120 - return E.A.getClass().isEnum();
1.121 - }
1.122 -
1.123 - @Compare public boolean isNotEnum() {
1.124 - return "".getClass().isEnum();
1.125 - }
1.126 -
1.127 - @Compare public String newInstanceFails() throws InstantiationException {
1.128 - try {
1.129 - return "success: " + StaticUse.class.newInstance();
1.130 - } catch (IllegalAccessException ex) {
1.131 - return ex.getClass().getName();
1.132 - }
1.133 - }
1.134 -
1.135 - @Compare public String paramTypes() throws Exception {
1.136 - Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
1.137 - final Class[] pt = plus.getParameterTypes();
1.138 - return pt[0].getName();
1.139 - }
1.140 - @Compare public String paramTypesNotFound() throws Exception {
1.141 - return StaticUse.class.getMethod("plus", int.class, double.class).toString();
1.142 - }
1.143 - @Compare public int methodWithArgs() throws Exception {
1.144 - Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
1.145 - return (Integer)plus.invoke(null, 2, 3);
1.146 - }
1.147 -
1.148 - @Compare public String classGetNameForByte() {
1.149 - return byte.class.getName();
1.150 - }
1.151 - @Compare public String classGetNameForBaseObject() {
1.152 - return newObject().getClass().getName();
1.153 - }
1.154 - @Compare public String classGetNameForJavaObject() {
1.155 - return new Object().getClass().getName();
1.156 - }
1.157 - @Compare public String classGetNameForObjectArray() {
1.158 - return (new Object[3]).getClass().getName();
1.159 - }
1.160 - @Compare public String classGetNameForSimpleIntArray() {
1.161 - return (new int[3]).getClass().getName();
1.162 - }
1.163 - @Compare public boolean sameClassGetNameForSimpleCharArray() {
1.164 - return (new char[3]).getClass() == (new char[34]).getClass();
1.165 - }
1.166 - @Compare public String classGetNameForMultiIntArray() {
1.167 - return (new int[3][4][5][6][7][8][9]).getClass().getName();
1.168 - }
1.169 - @Compare public String classGetNameForMultiIntArrayInner() {
1.170 - final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
1.171 - int[][][][][][] subarr = arr[0];
1.172 - int[][][][][] subsubarr = subarr[0];
1.173 - return subsubarr.getClass().getName();
1.174 - }
1.175 - @Compare public String classGetNameForMultiStringArray() {
1.176 - return (new String[3][4][5][6][7][8][9]).getClass().getName();
1.177 - }
1.178 -
1.179 - @Compare public String classForByte() throws Exception {
1.180 - return Class.forName("[Z").getName();
1.181 - }
1.182 -
1.183 - @Compare public String classForUnknownArray() {
1.184 - try {
1.185 - return Class.forName("[W").getName();
1.186 - } catch (Exception ex) {
1.187 - return ex.getClass().getName();
1.188 - }
1.189 - }
1.190 -
1.191 - @Compare public String classForUnknownDeepArray() {
1.192 - try {
1.193 - return Class.forName("[[[[[W").getName();
1.194 - } catch (Exception ex) {
1.195 - return ex.getClass().getName();
1.196 - }
1.197 - }
1.198 -
1.199 - @Compare public String componentGetNameForObjectArray() {
1.200 - return (new Object[3]).getClass().getComponentType().getName();
1.201 - }
1.202 - @Compare public boolean sameComponentGetNameForObjectArray() {
1.203 - return (new Object[3]).getClass().getComponentType() == Object.class;
1.204 - }
1.205 - @Compare public String componentGetNameForSimpleIntArray() {
1.206 - return (new int[3]).getClass().getComponentType().getName();
1.207 - }
1.208 - @Compare public String componentGetNameForMultiIntArray() {
1.209 - return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
1.210 - }
1.211 - @Compare public String componentGetNameForMultiStringArray() {
1.212 - Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
1.213 - StringBuilder sb = new StringBuilder();
1.214 - for (;;) {
1.215 - sb.append(c.getName()).append("\n");
1.216 - c = c.getComponentType();
1.217 - if (c == null) {
1.218 - break;
1.219 - }
1.220 - }
1.221 - return sb.toString();
1.222 - }
1.223 -
1.224 - @Compare public boolean isArray() {
1.225 - return new Object[0].getClass().isArray();
1.226 - }
1.227 -
1.228 - @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
1.229 - private static String[] sort(String[] arr, int len) {
1.230 - List<String> list = Arrays.asList(arr).subList(0, len);
1.231 - Collections.sort(list);
1.232 - return list.toArray(new String[0]);
1.233 - }
1.234 -
1.235 - @JavaScriptBody(args = {}, body = "return new Object();")
1.236 - private static Object newObject() {
1.237 - return new Object();
1.238 - }
1.239 -
1.240 - @Factory
1.241 - public static Object[] create() {
1.242 - return VMTest.create(ReflectionTest.class);
1.243 - }
1.244 -
1.245 -}