1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java Sun Apr 28 10:26:08 2013 +0200
1.3 @@ -0,0 +1,272 @@
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 boolean isAssignableToPrimitiveType() {
1.60 + return boolean.class.isAssignableFrom(Runnable.class);
1.61 + }
1.62 +
1.63 + @Compare public boolean isAssignableFromPrimitiveType() {
1.64 + return Runnable.class.isAssignableFrom(boolean.class);
1.65 + }
1.66 +
1.67 + @Compare public boolean isAssignableLongFromInt() {
1.68 + return long.class.isAssignableFrom(int.class);
1.69 + }
1.70 +
1.71 + @Compare public boolean isAssignableIntFromLong() {
1.72 + return int.class.isAssignableFrom(long.class);
1.73 + }
1.74 +
1.75 + @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
1.76 + return Runnable.class.getMethod("run").getName();
1.77 + }
1.78 +
1.79 + @Compare public String namesOfMethods() {
1.80 + StringBuilder sb = new StringBuilder();
1.81 + String[] arr = new String[20];
1.82 + int i = 0;
1.83 + for (Method m : StaticUse.class.getMethods()) {
1.84 + arr[i++] = m.getName();
1.85 + }
1.86 + for (String s : sort(arr, i)) {
1.87 + sb.append(s).append("\n");
1.88 + }
1.89 + return sb.toString();
1.90 + }
1.91 +
1.92 + @Compare public String namesOfDeclaringClassesOfMethods() {
1.93 + StringBuilder sb = new StringBuilder();
1.94 + String[] arr = new String[20];
1.95 + int i = 0;
1.96 + for (Method m : StaticUse.class.getMethods()) {
1.97 + arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
1.98 + }
1.99 + for (String s : sort(arr, i)) {
1.100 + sb.append(s).append("\n");
1.101 + }
1.102 + return sb.toString();
1.103 + }
1.104 +
1.105 + @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
1.106 + StaticUse.class.getMethod("instanceMethod").invoke(null);
1.107 + return "should not happen";
1.108 + }
1.109 +
1.110 + @Compare public String classCastException() {
1.111 + try {
1.112 + Integer i = (Integer)StaticUseSub.getNonNull();
1.113 + return "" + i.intValue();
1.114 + } catch (ClassCastException ex) {
1.115 + return ex.getClass().getName();
1.116 + }
1.117 + }
1.118 +
1.119 + @Compare public String methodThatThrowsException() throws Exception {
1.120 + StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
1.121 + return "should not happen";
1.122 + }
1.123 +
1.124 + @Compare public Object voidReturnType() throws Exception {
1.125 + return StaticUse.class.getMethod("instanceMethod").getReturnType();
1.126 + }
1.127 +
1.128 + @Retention(RetentionPolicy.RUNTIME)
1.129 + @interface Ann {
1.130 + }
1.131 +
1.132 + @Compare public String annoClass() throws Exception {
1.133 + Retention r = Ann.class.getAnnotation(Retention.class);
1.134 + assert r != null : "Annotation is present";
1.135 + assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
1.136 + return r.annotationType().getName();
1.137 + }
1.138 +
1.139 + @Compare public boolean isAnnotation() {
1.140 + return Ann.class.isAnnotation();
1.141 + }
1.142 + @Compare public boolean isNotAnnotation() {
1.143 + return String.class.isAnnotation();
1.144 + }
1.145 + @Compare public boolean isNotAnnotationEnum() {
1.146 + return E.class.isAnnotation();
1.147 + }
1.148 + enum E { A, B };
1.149 + @Compare public boolean isEnum() {
1.150 + return E.A.getClass().isEnum();
1.151 + }
1.152 +
1.153 + @Compare public boolean isNotEnum() {
1.154 + return "".getClass().isEnum();
1.155 + }
1.156 +
1.157 + @Compare public String newInstanceFails() throws InstantiationException {
1.158 + try {
1.159 + return "success: " + StaticUseSub.class.newInstance();
1.160 + } catch (IllegalAccessException ex) {
1.161 + return ex.getClass().getName();
1.162 + }
1.163 + }
1.164 +
1.165 + @Compare public String paramTypes() throws Exception {
1.166 + Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
1.167 + final Class[] pt = plus.getParameterTypes();
1.168 + return pt[0].getName();
1.169 + }
1.170 + @Compare public String paramTypesNotFound() throws Exception {
1.171 + return StaticUse.class.getMethod("plus", int.class, double.class).toString();
1.172 + }
1.173 + @Compare public int methodWithArgs() throws Exception {
1.174 + Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
1.175 + return (Integer)plus.invoke(null, 2, 3);
1.176 + }
1.177 +
1.178 + @Compare public String classGetNameForByte() {
1.179 + return byte.class.getName();
1.180 + }
1.181 + @Compare public String classGetNameForBaseObject() {
1.182 + return newObject().getClass().getName();
1.183 + }
1.184 + @Compare public String classGetNameForJavaObject() {
1.185 + return new Object().getClass().getName();
1.186 + }
1.187 + @Compare public String classGetNameForObjectArray() {
1.188 + return (new Object[3]).getClass().getName();
1.189 + }
1.190 + @Compare public String classGetNameForSimpleIntArray() {
1.191 + return (new int[3]).getClass().getName();
1.192 + }
1.193 + @Compare public boolean sameClassGetNameForSimpleCharArray() {
1.194 + return (new char[3]).getClass() == (new char[34]).getClass();
1.195 + }
1.196 + @Compare public String classGetNameForMultiIntArray() {
1.197 + return (new int[3][4][5][6][7][8][9]).getClass().getName();
1.198 + }
1.199 + @Compare public String classGetNameForMultiIntArrayInner() {
1.200 + final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
1.201 + int[][][][][][] subarr = arr[0];
1.202 + int[][][][][] subsubarr = subarr[0];
1.203 + return subsubarr.getClass().getName();
1.204 + }
1.205 + @Compare public String classGetNameForMultiStringArray() {
1.206 + return (new String[3][4][5][6][7][8][9]).getClass().getName();
1.207 + }
1.208 +
1.209 + @Compare public String classForByte() throws Exception {
1.210 + return Class.forName("[Z").getName();
1.211 + }
1.212 +
1.213 + @Compare public String classForUnknownArray() {
1.214 + try {
1.215 + return Class.forName("[W").getName();
1.216 + } catch (Exception ex) {
1.217 + return ex.getClass().getName();
1.218 + }
1.219 + }
1.220 +
1.221 + @Compare public String classForUnknownDeepArray() {
1.222 + try {
1.223 + return Class.forName("[[[[[W").getName();
1.224 + } catch (Exception ex) {
1.225 + return ex.getClass().getName();
1.226 + }
1.227 + }
1.228 +
1.229 + @Compare public String componentGetNameForObjectArray() {
1.230 + return (new Object[3]).getClass().getComponentType().getName();
1.231 + }
1.232 + @Compare public boolean sameComponentGetNameForObjectArray() {
1.233 + return (new Object[3]).getClass().getComponentType() == Object.class;
1.234 + }
1.235 + @Compare public String componentGetNameForSimpleIntArray() {
1.236 + return (new int[3]).getClass().getComponentType().getName();
1.237 + }
1.238 + @Compare public String componentGetNameForMultiIntArray() {
1.239 + return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
1.240 + }
1.241 + @Compare public String componentGetNameForMultiStringArray() {
1.242 + Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
1.243 + StringBuilder sb = new StringBuilder();
1.244 + for (;;) {
1.245 + sb.append(c.getName()).append("\n");
1.246 + c = c.getComponentType();
1.247 + if (c == null) {
1.248 + break;
1.249 + }
1.250 + }
1.251 + return sb.toString();
1.252 + }
1.253 +
1.254 + @Compare public boolean isArray() {
1.255 + return new Object[0].getClass().isArray();
1.256 + }
1.257 +
1.258 + @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
1.259 + private static String[] sort(String[] arr, int len) {
1.260 + List<String> list = Arrays.asList(arr).subList(0, len);
1.261 + Collections.sort(list);
1.262 + return list.toArray(new String[0]);
1.263 + }
1.264 +
1.265 + @JavaScriptBody(args = {}, body = "return new Object();")
1.266 + private static Object newObject() {
1.267 + return new Object();
1.268 + }
1.269 +
1.270 + @Factory
1.271 + public static Object[] create() {
1.272 + return VMTest.create(ReflectionTest.class);
1.273 + }
1.274 +
1.275 +}