rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 07 Apr 2013 15:24:45 +0200
branchmodel
changeset 938 0eec1b51c13c
parent 886 88540bb74300
child 962 787578f33c21
permissions -rw-r--r--
Wrap invoke exceptions into InvocationTargetException
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    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;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 public class ReflectionTest {
    36     @Compare public boolean nonNullThis() {
    37         return this == null;
    38     }
    39     
    40     @Compare public String intType() {
    41         return Integer.TYPE.toString();
    42     }
    43 
    44     @Compare public String voidType() throws Exception {
    45         return void.class.toString();
    46     }
    47 
    48     @Compare public String longClass() {
    49         return long.class.toString();
    50     }
    51     
    52     @Compare public boolean isRunnableInterface() {
    53         return Runnable.class.isInterface();
    54     }
    55 
    56     @Compare public boolean isAssignableToPrimitiveType() {
    57         return boolean.class.isAssignableFrom(Runnable.class);
    58     }
    59 
    60     @Compare public boolean isAssignableFromPrimitiveType() {
    61         return Runnable.class.isAssignableFrom(boolean.class);
    62     }
    63 
    64     @Compare public boolean isAssignableLongFromInt() {
    65         return long.class.isAssignableFrom(int.class);
    66     }
    67 
    68     @Compare public boolean isAssignableIntFromLong() {
    69         return int.class.isAssignableFrom(long.class);
    70     }
    71 
    72     @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
    73         return Runnable.class.getMethod("run").getName();
    74     }
    75     
    76     @Compare public String namesOfMethods() {
    77         StringBuilder sb = new StringBuilder();
    78         String[] arr = new String[20];
    79         int i = 0;
    80         for (Method m : StaticUse.class.getMethods()) {
    81             arr[i++] = m.getName();
    82         }
    83         for (String s : sort(arr, i)) {
    84             sb.append(s).append("\n");
    85         }
    86         return sb.toString();
    87     }
    88 
    89     @Compare public String namesOfDeclaringClassesOfMethods() {
    90         StringBuilder sb = new StringBuilder();
    91         String[] arr = new String[20];
    92         int i = 0;
    93         for (Method m : StaticUse.class.getMethods()) {
    94             arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
    95         }
    96         for (String s : sort(arr, i)) {
    97             sb.append(s).append("\n");
    98         }
    99         return sb.toString();
   100     }
   101     
   102     @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
   103         StaticUse.class.getMethod("instanceMethod").invoke(null);
   104         return "should not happen";
   105     }
   106 
   107     @Compare public String methodThatThrowsException() throws Exception {
   108         StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
   109         return "should not happen";
   110     }
   111 
   112     @Compare public Object voidReturnType() throws Exception {
   113         return StaticUse.class.getMethod("instanceMethod").getReturnType();
   114     }
   115     
   116     @Retention(RetentionPolicy.RUNTIME)
   117     @interface Ann {
   118     }
   119     
   120     @Compare public String annoClass() throws Exception {
   121         Retention r = Ann.class.getAnnotation(Retention.class);
   122         assert r != null : "Annotation is present";
   123         assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
   124         return r.annotationType().getName();
   125     }
   126     
   127     @Compare public boolean isAnnotation() {
   128         return Ann.class.isAnnotation();
   129     }
   130     @Compare public boolean isNotAnnotation() {
   131         return String.class.isAnnotation();
   132     }
   133     @Compare public boolean isNotAnnotationEnum() {
   134         return E.class.isAnnotation();
   135     }
   136     enum E { A, B };
   137     @Compare public boolean isEnum() {
   138         return E.A.getClass().isEnum();
   139     }
   140 
   141     @Compare public boolean isNotEnum() {
   142         return "".getClass().isEnum();
   143     }
   144     
   145     @Compare public String newInstanceFails() throws InstantiationException {
   146         try {
   147             return "success: " + StaticUseSub.class.newInstance();
   148         } catch (IllegalAccessException ex) {
   149             return ex.getClass().getName();
   150         }
   151     }
   152     
   153     @Compare public String paramTypes() throws Exception {
   154         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   155         final Class[] pt = plus.getParameterTypes();
   156         return pt[0].getName();
   157     }
   158     @Compare public String paramTypesNotFound() throws Exception {
   159         return StaticUse.class.getMethod("plus", int.class, double.class).toString();
   160     }
   161     @Compare public int methodWithArgs() throws Exception {
   162         Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
   163         return (Integer)plus.invoke(null, 2, 3);
   164     }
   165     
   166     @Compare public String classGetNameForByte() {
   167          return byte.class.getName();
   168     }
   169     @Compare public String classGetNameForBaseObject() {
   170         return newObject().getClass().getName();
   171     }
   172     @Compare public String classGetNameForJavaObject() {
   173         return new Object().getClass().getName();
   174     }
   175     @Compare public String classGetNameForObjectArray() {
   176         return (new Object[3]).getClass().getName();
   177     }
   178     @Compare public String classGetNameForSimpleIntArray() {
   179         return (new int[3]).getClass().getName();
   180     }
   181     @Compare public boolean sameClassGetNameForSimpleCharArray() {
   182         return (new char[3]).getClass() == (new char[34]).getClass();
   183     }
   184     @Compare public String classGetNameForMultiIntArray() {
   185         return (new int[3][4][5][6][7][8][9]).getClass().getName();
   186     }
   187     @Compare public String classGetNameForMultiIntArrayInner() {
   188         final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
   189         int[][][][][][] subarr = arr[0];
   190         int[][][][][] subsubarr = subarr[0];
   191         return subsubarr.getClass().getName();
   192     }
   193     @Compare public String classGetNameForMultiStringArray() {
   194         return (new String[3][4][5][6][7][8][9]).getClass().getName();
   195     }
   196     
   197     @Compare public String classForByte() throws Exception {
   198         return Class.forName("[Z").getName();
   199     }
   200 
   201     @Compare public String classForUnknownArray() {
   202         try {
   203             return Class.forName("[W").getName();
   204         } catch (Exception ex) {
   205             return ex.getClass().getName();
   206         }
   207     }
   208     
   209     @Compare public String classForUnknownDeepArray() {
   210         try {
   211             return Class.forName("[[[[[W").getName();
   212         } catch (Exception ex) {
   213             return ex.getClass().getName();
   214         }
   215     }
   216     
   217     @Compare public String componentGetNameForObjectArray() {
   218         return (new Object[3]).getClass().getComponentType().getName();
   219     }
   220     @Compare public boolean sameComponentGetNameForObjectArray() {
   221         return (new Object[3]).getClass().getComponentType() == Object.class;
   222     }
   223     @Compare public String componentGetNameForSimpleIntArray() {
   224         return (new int[3]).getClass().getComponentType().getName();
   225     }
   226     @Compare public String componentGetNameForMultiIntArray() {
   227         return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
   228     }
   229     @Compare public String componentGetNameForMultiStringArray() {
   230         Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
   231         StringBuilder sb = new StringBuilder();
   232         for (;;) {
   233             sb.append(c.getName()).append("\n");
   234             c = c.getComponentType();
   235             if (c == null) {
   236                 break;
   237             }
   238         }
   239         return sb.toString();
   240     }
   241     
   242     @Compare public boolean isArray() {
   243         return new Object[0].getClass().isArray();
   244     }
   245     
   246     @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
   247     private static String[] sort(String[] arr, int len) {
   248         List<String> list = Arrays.asList(arr).subList(0, len);
   249         Collections.sort(list);
   250         return list.toArray(new String[0]);
   251     }
   252     
   253     @JavaScriptBody(args = {}, body = "return new Object();")
   254     private static Object newObject() {
   255         return new Object();
   256     }
   257     
   258     @Factory
   259     public static Object[] create() {
   260         return VMTest.create(ReflectionTest.class);
   261     }
   262     
   263 }