vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 02 Dec 2012 21:00:12 +0100
changeset 239 8ceee38f5840
parent 226 907a52ed10e3
child 248 0bfcb6585290
permissions -rw-r--r--
Ability to control prototypes. Making sure any JavaScript Object is instance of Java 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 org.testng.annotations.BeforeClass;
    23 
    24 /**
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public class InstanceTest {
    29     @Test public void verifyDefaultDoubleValue() throws Exception {
    30         assertExec(
    31             "Will be zero",
    32             Instance.class, "defaultDblValueD",
    33             Double.valueOf(0)
    34         );
    35     }
    36     @Test public void verifyStaticMethodCall() throws Exception {
    37         assertExec(
    38             "Will be zero",
    39             InstanceSub.class, "recallDblD",
    40             Double.valueOf(0)
    41         );
    42     }
    43     @Test public void verifyAssignedByteValue() throws Exception {
    44         assertExec(
    45             "Will one thirty one",
    46             Instance.class, "assignedByteValueB",
    47             Double.valueOf(31)
    48         );
    49     }
    50     @Test public void verifyMagicOne() throws Exception {
    51         assertExec(
    52             "Should be three and something",
    53             Instance.class, "magicOneD",
    54             Double.valueOf(3.3)
    55         );
    56     }
    57     @Test public void verifyInstanceMethods() throws Exception {
    58         assertExec(
    59             "Should be eleven as we invoke overwritten method, plus 44",
    60             Instance.class, "virtualBytesI",
    61             Double.valueOf(55)
    62         );
    63     }
    64     @Test public void verifyInterfaceMethods() throws Exception {
    65         assertExec(
    66             "Retruns default value",
    67             Instance.class, "interfaceBytesF",
    68             Double.valueOf(31)
    69         );
    70     }
    71 
    72     @Test public void isNull() throws Exception {
    73         assertExec(
    74             "Yes, we are instance",
    75             Instance.class, "isNullZ",
    76             Double.valueOf(0.0)
    77         );
    78     }
    79 
    80     @Test public void isInstanceOf() throws Exception {
    81         assertExec(
    82             "Yes, we are instance",
    83             Instance.class, "instanceOfZZ",
    84             Double.valueOf(1.0), true
    85         );
    86     }
    87 
    88     @Test public void notInstanceOf() throws Exception {
    89         assertExec(
    90             "No, we are not an instance",
    91             Instance.class, "instanceOfZZ",
    92             Double.valueOf(0.0), false
    93         );
    94     }
    95     
    96     @Test public void verifyCastToClass() throws Exception {
    97         assertExec(
    98             "Five signals all is good",
    99             Instance.class, "castsWorkIZ",
   100             Double.valueOf(5.0), false
   101         );
   102     }
   103     @Test public void verifyCastToInterface() throws Exception {
   104         assertExec(
   105             "Five signals all is good",
   106             Instance.class, "castsWorkIZ",
   107             Double.valueOf(5.0), true
   108         );
   109     }
   110     
   111     @Test public void sharedConstructor() throws Exception {
   112         assertExec(
   113             "Constructor of first and 2nd instance should be the same",
   114             Instance.class, "sharedConstructorZ",
   115             Double.valueOf(1.0)
   116         );
   117     }
   118 
   119     @Test public void differentConstructor() throws Exception {
   120         assertExec(
   121             "Constructor of X and Y should be the different",
   122             Instance.class, "differentConstructorZ",
   123             Double.valueOf(0)
   124         );
   125     }
   126 
   127     @Test public void jsObjectIsLikeJavaObject() throws Exception {
   128         assertExec(
   129             "JavaScript object is instance of Java Object",
   130             Instance.class, "iofObjectZ",
   131             Double.valueOf(1)
   132         );
   133     }
   134     
   135     protected String startCompilationWith() {
   136         return "org/apidesign/vm4brwsr/Instance";
   137     }
   138     
   139     private static CharSequence codeSeq;
   140     private static Invocable code;
   141     
   142     @BeforeClass
   143     public void compileTheCode() throws Exception {
   144         if (codeSeq == null) {
   145             StringBuilder sb = new StringBuilder();
   146             code = StaticMethodTest.compileClass(sb, startCompilationWith());
   147             codeSeq = sb;
   148         }
   149     }
   150     
   151     private void assertExec(
   152         String msg, Class clazz, String method, Object expRes, Object... args
   153     ) throws Exception {
   154         StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   155     }
   156     
   157 }