vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 03 Jan 2013 11:29:22 +0100
branchTypeNickNames
changeset 406 2670f519a46d
parent 405 e41809be6106
permissions -rw-r--r--
Using 'o' instead of full name of java.lang.Object
     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.vm4brwsr;
    19 
    20 import javax.script.Invocable;
    21 import org.testng.annotations.Test;
    22 import static org.testng.Assert.*;
    23 import org.testng.annotations.BeforeClass;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public class ClassTest {
    30 
    31     @Test public void superClassEqualsGetSuperclass() {
    32         assertTrue(Classes.equalsClassesOfExceptions(), "Classes are equal");
    33     }
    34 
    35     @Test public void jsSuperClassEqualsGetSuperclass() throws Exception {
    36         assertExec("Classes are equal", Classes.class, "equalsClassesOfExceptions__Z", Double.valueOf(1.0));
    37     }
    38 
    39     @Test public void classesAreDifferent() {
    40         assertTrue(Classes.differenceInClasses(), "Classes are not equal");
    41     }
    42 
    43     @Test public void jsClassesAreDifferent() throws Exception {
    44         assertExec("Classes are not equal", Classes.class, "differenceInClasses__Z", Double.valueOf(1.0));
    45     }
    46 
    47     @Test public void javaInstanceName() throws Exception {
    48         assertEquals(Classes.classForInstance(), "java.io.IOException");
    49     }
    50     @Test public void jsInstanceName() throws Exception {
    51         assertExec("I/O name", Classes.class, "classForInstance__s", "java.io.IOException");
    52     }
    53     @Test public void javaName() throws Exception {
    54         assertEquals(Classes.name(), "java.io.IOException");
    55     }
    56     @Test public void jsName() throws Exception {
    57         assertExec("I/O name", Classes.class, "name__s", "java.io.IOException");
    58     }
    59     @Test public void javaSimpleName() throws Exception {
    60         assertEquals(Classes.simpleName(), "IOException");
    61     }
    62     @Test public void jsGetsSimpleName() throws Exception {
    63         assertExec("I/O simple name", Classes.class, "simpleName__s", "IOException");
    64     }
    65     @Test public void javaCanonicalName() {
    66         assertEquals(Classes.canonicalName(), "java.io.IOException");
    67     }
    68     @Test public void jsCanonicalName() throws Exception {
    69         assertExec("I/O simple name", Classes.class, "canonicalName__s", "java.io.IOException");
    70     }
    71     @Test public void javaNewInstance() throws Exception {
    72         assertTrue(Classes.newInstance());
    73     }
    74     @Test public void jsNewInstance() throws Exception {
    75         assertExec("Check new instance", Classes.class, "newInstance__Z", Double.valueOf(1));
    76     }
    77     @Test public void javaNoNewInstance() throws Exception {
    78         assertEquals("java.lang.InstantiationException:java.lang.Float", 
    79             Classes.newInstanceNoPubConstructor()
    80         );
    81     }
    82     @Test public void jsNoNewInstance() throws Exception {
    83         assertExec("Check problems with new instance", Classes.class, "newInstanceNoPubConstructor__s", 
    84             "java.lang.InstantiationException:java.lang.Float"
    85         );
    86     }
    87     @Test public void jsAnnotation() throws Exception {
    88         assertExec("Check class annotation", Classes.class, "getMarker__I", Double.valueOf(10));
    89     }
    90     @Test public void jsStringAnnotation() throws Exception {
    91         assertExec("Check class annotation", Classes.class, "getNamer__sZ", "my text", true);
    92     }
    93     @Test public void jsStringAnnotationFromArray() throws Exception {
    94         assertExec("Check class annotation", Classes.class, "getNamer__sZ", "my text", false);
    95     }
    96     @Test public void javaInvokeMethod() throws Exception {
    97         assertEquals(Classes.reflectiveMethodCall(true, "name"), "java.io.IOException", "Calls the name() method via reflection");
    98     }
    99     @Test public void jsInvokeMethod() throws Exception {
   100         assertExec("Calls the name() method via reflection", Classes.class, 
   101             "reflectiveMethodCall__oZs", 
   102             "java.io.IOException", true, "name"
   103         );
   104     }
   105     @Test public void javaFindMethod() throws Exception {
   106         assertEquals(Classes.reflectiveMethodCall(false, "name"), "java.io.IOException", "Calls the name() method via reflection");
   107     }
   108     @Test public void jsFindMethod() throws Exception {
   109         assertExec("Calls the name() method via reflection", Classes.class, 
   110             "reflectiveMethodCall__oZs", 
   111             "java.io.IOException", false, "name"
   112         );
   113     }
   114     @Test public void primitiveReturnType() throws Exception {
   115         assertExec("Tries to get an integer via reflection", Classes.class, 
   116             "primitiveType__ss", 
   117             Classes.primitiveType("primitive"), "primitive"
   118         );
   119     }
   120     @Test public void primitiveBoolReturnType() throws Exception {
   121         assertExec("Tries to get an integer via reflection", Classes.class, 
   122             "primitiveType__ss", 
   123             Classes.primitiveType("primitiveB"), "primitiveB"
   124         );
   125     }
   126     @Test public void javaAnnotatedMethod() throws Exception {
   127         assertEquals(Classes.reflectiveMethodCall(false, null), "java.io.IOException", "Calls the name() method via reflection");
   128     }
   129     @Test public void jsAnnotatedMethod() throws Exception {
   130         assertExec("Calls the name() method via reflection", Classes.class, 
   131             "reflectiveMethodCall__oZs", 
   132             "java.io.IOException", false, null
   133         );
   134     }
   135     @Test public void jsClassParam() throws Exception {
   136         assertExec("Calls the nameOfIO()", Classes.class, 
   137             "nameOfIO__s", 
   138             "java.io.IOException"
   139         );
   140     }
   141     @Test public void noInterface() throws Exception {
   142         assertExec("Calls Class.isInterface", Classes.class, 
   143             "isInterface__Zs", 
   144             0.0, "java.lang.String"
   145         );
   146     }
   147     /*
   148     @Test public void isInterface() throws Exception {
   149         assertExec("Calls Class.isInterface", Classes.class, 
   150             "isInterface__Zs", 
   151             1.0, "java.lang.Runnable"
   152         );
   153     }
   154     */
   155     @Test public void integerType() throws Exception {
   156         assertExec("Computes the type", Classes.class, 
   157             "intType__s", 
   158             Classes.intType()
   159         );
   160     }
   161     
   162     private static CharSequence codeSeq;
   163     private static Invocable code;
   164     
   165     @BeforeClass
   166     public void compileTheCode() throws Exception {
   167         if (codeSeq == null) {
   168             StringBuilder sb = new StringBuilder();
   169             code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Classes");
   170             codeSeq = sb;
   171         }
   172     }
   173     
   174     private void assertExec(
   175         String msg, Class clazz, String method, Object expRes, Object... args
   176     ) throws Exception {
   177         StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   178     }
   179     
   180 }