vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 11 Dec 2012 11:05:06 +0100
branchlazyvm
changeset 303 c12342170235
parent 266 2e2e6f946208
child 353 fd38bdad7fb5
permissions -rw-r--r--
VM in VM properly processes class constants
     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__Ljava_lang_String_2", "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__Ljava_lang_String_2", "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__Ljava_lang_String_2", "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__Ljava_lang_String_2", "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 jsAnnotation() throws Exception {
    78         assertExec("Check class annotation", Classes.class, "getMarker__I", Double.valueOf(10));
    79     }
    80     @Test public void jsStringAnnotation() throws Exception {
    81         assertExec("Check class annotation", Classes.class, "getNamer__Ljava_lang_String_2Z", "my text", true);
    82     }
    83     @Test public void jsStringAnnotationFromArray() throws Exception {
    84         assertExec("Check class annotation", Classes.class, "getNamer__Ljava_lang_String_2Z", "my text", false);
    85     }
    86     @Test public void javaInvokeMethod() throws Exception {
    87         assertEquals(Classes.reflectiveMethodCall(true, "name"), "java.io.IOException", "Calls the name() method via reflection");
    88     }
    89     @Test public void jsInvokeMethod() throws Exception {
    90         assertExec("Calls the name() method via reflection", Classes.class, 
    91             "reflectiveMethodCall__Ljava_lang_Object_2ZLjava_lang_String_2", 
    92             "java.io.IOException", true, "name"
    93         );
    94     }
    95     @Test public void javaFindMethod() throws Exception {
    96         assertEquals(Classes.reflectiveMethodCall(false, "name"), "java.io.IOException", "Calls the name() method via reflection");
    97     }
    98     @Test public void jsFindMethod() throws Exception {
    99         assertExec("Calls the name() method via reflection", Classes.class, 
   100             "reflectiveMethodCall__Ljava_lang_Object_2ZLjava_lang_String_2", 
   101             "java.io.IOException", false, "name"
   102         );
   103     }
   104     @Test public void javaAnnotatedMethod() throws Exception {
   105         assertEquals(Classes.reflectiveMethodCall(false, null), "java.io.IOException", "Calls the name() method via reflection");
   106     }
   107     @Test public void jsAnnotatedMethod() throws Exception {
   108         assertExec("Calls the name() method via reflection", Classes.class, 
   109             "reflectiveMethodCall__Ljava_lang_Object_2ZLjava_lang_String_2", 
   110             "java.io.IOException", false, null
   111         );
   112     }
   113     @Test public void jsClassParam() throws Exception {
   114         assertExec("Calls the nameOfIO()", Classes.class, 
   115             "nameOfIO__Ljava_lang_String_2", 
   116             "java.io.IOException"
   117         );
   118     }
   119     
   120     private static CharSequence codeSeq;
   121     private static Invocable code;
   122     
   123     @BeforeClass
   124     public void compileTheCode() throws Exception {
   125         if (codeSeq == null) {
   126             StringBuilder sb = new StringBuilder();
   127             code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Classes");
   128             codeSeq = sb;
   129         }
   130     }
   131     
   132     private void assertExec(
   133         String msg, Class clazz, String method, Object expRes, Object... args
   134     ) throws Exception {
   135         StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   136     }
   137     
   138 }