Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.tck;
20 import java.lang.annotation.Retention;
21 import java.lang.annotation.RetentionPolicy;
22 import java.lang.reflect.Method;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import org.apidesign.bck2brwsr.core.JavaScriptBody;
27 import org.apidesign.bck2brwsr.vmtest.Compare;
28 import org.apidesign.bck2brwsr.vmtest.VMTest;
29 import org.testng.annotations.Factory;
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 public class ReflectionTest {
36 @Compare public boolean nonNullThis() {
40 @Compare public String intType() {
41 return Integer.TYPE.toString();
44 @Compare public String voidType() throws Exception {
45 return void.class.toString();
48 @Compare public String longClass() {
49 return long.class.toString();
52 @Compare public boolean isRunnableInterface() {
53 return Runnable.class.isInterface();
56 @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
57 return Runnable.class.getMethod("run").getName();
60 @Compare public String namesOfMethods() {
61 StringBuilder sb = new StringBuilder();
62 String[] arr = new String[20];
64 for (Method m : StaticUse.class.getMethods()) {
65 arr[i++] = m.getName();
67 for (String s : sort(arr, i)) {
68 sb.append(s).append("\n");
73 @Compare public String namesOfDeclaringClassesOfMethods() {
74 StringBuilder sb = new StringBuilder();
75 String[] arr = new String[20];
77 for (Method m : StaticUse.class.getMethods()) {
78 arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
80 for (String s : sort(arr, i)) {
81 sb.append(s).append("\n");
86 @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
87 StaticUse.class.getMethod("instanceMethod").invoke(null);
88 return "should not happen";
91 @Compare public Object voidReturnType() throws Exception {
92 return StaticUse.class.getMethod("instanceMethod").getReturnType();
95 @Retention(RetentionPolicy.RUNTIME)
99 @Compare public String annoClass() throws Exception {
100 Retention r = Ann.class.getAnnotation(Retention.class);
101 assert r != null : "Annotation is present";
102 assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
103 return r.annotationType().getName();
106 @Compare public boolean isAnnotation() {
107 return Ann.class.isAnnotation();
109 @Compare public boolean isNotAnnotation() {
110 return String.class.isAnnotation();
112 @Compare public boolean isNotAnnotationEnum() {
113 return E.class.isAnnotation();
116 @Compare public boolean isEnum() {
117 return E.A.getClass().isEnum();
120 @Compare public boolean isNotEnum() {
121 return "".getClass().isEnum();
124 @Compare public String newInstanceFails() throws InstantiationException {
126 return "success: " + StaticUse.class.newInstance();
127 } catch (IllegalAccessException ex) {
128 return ex.getClass().getName();
132 @Compare public String paramTypes() throws Exception {
133 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
134 final Class[] pt = plus.getParameterTypes();
135 return pt[0].getName();
137 @Compare public String paramTypesNotFound() throws Exception {
138 return StaticUse.class.getMethod("plus", int.class, double.class).toString();
140 @Compare public int methodWithArgs() throws Exception {
141 Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
142 return (Integer)plus.invoke(null, 2, 3);
145 @Compare public String classGetNameForByte() {
146 return byte.class.getName();
148 @Compare public String classGetNameForBaseObject() {
149 return newObject().getClass().getName();
151 @Compare public String classGetNameForJavaObject() {
152 return new Object().getClass().getName();
154 @Compare public String classGetNameForObjectArray() {
155 return (new Object[3]).getClass().getName();
157 @Compare public String classGetNameForSimpleIntArray() {
158 return (new int[3]).getClass().getName();
160 @Compare public boolean sameClassGetNameForSimpleCharArray() {
161 return (new char[3]).getClass() == (new char[34]).getClass();
163 @Compare public String classGetNameForMultiIntArray() {
164 return (new int[3][4][5][6][7][8][9]).getClass().getName();
166 @Compare public String classGetNameForMultiIntArrayInner() {
167 final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
168 int[][][][][][] subarr = arr[0];
169 int[][][][][] subsubarr = subarr[0];
170 return subsubarr.getClass().getName();
172 @Compare public String classGetNameForMultiStringArray() {
173 return (new String[3][4][5][6][7][8][9]).getClass().getName();
176 @Compare public String classForByte() throws Exception {
177 return Class.forName("[Z").getName();
180 @Compare public String classForUnknownArray() {
182 return Class.forName("[W").getName();
183 } catch (Exception ex) {
184 return ex.getClass().getName();
188 @Compare public String classForUnknownDeepArray() {
190 return Class.forName("[[[[[W").getName();
191 } catch (Exception ex) {
192 return ex.getClass().getName();
196 @Compare public String componentGetNameForObjectArray() {
197 return (new Object[3]).getClass().getComponentType().getName();
199 @Compare public boolean sameComponentGetNameForObjectArray() {
200 return (new Object[3]).getClass().getComponentType() == Object.class;
202 @Compare public String componentGetNameForSimpleIntArray() {
203 return (new int[3]).getClass().getComponentType().getName();
205 @Compare public String componentGetNameForMultiIntArray() {
206 return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
208 @Compare public String componentGetNameForMultiStringArray() {
209 Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
210 StringBuilder sb = new StringBuilder();
212 sb.append(c.getName()).append("\n");
213 c = c.getComponentType();
218 return sb.toString();
221 @Compare public boolean isArray() {
222 return new Object[0].getClass().isArray();
225 @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
226 private static String[] sort(String[] arr, int len) {
227 List<String> list = Arrays.asList(arr).subList(0, len);
228 Collections.sort(list);
229 return list.toArray(new String[0]);
232 @JavaScriptBody(args = {}, body = "return new Object();")
233 private static Object newObject() {
238 public static Object[] create() {
239 return VMTest.create(ReflectionTest.class);