vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 11 Oct 2012 10:43:17 -0700
changeset 103 e8438996d406
parent 102 2354255a1844
child 106 346633cd13d6
permissions -rw-r--r--
Less compilation during test execution
     1 /**
     2  * Java 4 Browser Bytecode Translator
     3  * Copyright (C) 2012-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 javax.script.ScriptException;
    22 import org.testng.annotations.Test;
    23 import static org.testng.Assert.*;
    24 import org.testng.annotations.BeforeClass;
    25 import org.testng.annotations.BeforeTest;
    26 
    27 /**
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 public class InstanceTest {
    32     @Test public void verifyDefaultDoubleValue() throws Exception {
    33         assertExec(
    34             "Will be zero",
    35             "org_apidesign_vm4brwsr_Instance_defaultDblValueD",
    36             Double.valueOf(0)
    37         );
    38     }
    39     @Test public void verifyStaticMethodCall() throws Exception {
    40         assertExec(
    41             "Will be zero",
    42             "org_apidesign_vm4brwsr_InstanceSub_recallDblD",
    43             Double.valueOf(0)
    44         );
    45     }
    46     @Test public void verifyAssignedByteValue() throws Exception {
    47         assertExec(
    48             "Will one thirty one",
    49             "org_apidesign_vm4brwsr_Instance_assignedByteValueB",
    50             Double.valueOf(31)
    51         );
    52     }
    53     @Test public void verifyMagicOne() throws Exception {
    54         assertExec(
    55             "Should be three and something",
    56             "org_apidesign_vm4brwsr_Instance_magicOneD",
    57             Double.valueOf(3.3)
    58         );
    59     }
    60     @Test public void verifyInstanceMethods() throws Exception {
    61         assertExec(
    62             "Should be eleven as we invoke overwritten method, plus 44",
    63             "org_apidesign_vm4brwsr_Instance_virtualBytesI",
    64             Double.valueOf(55)
    65         );
    66     }
    67     @Test public void verifyInterfaceMethods() throws Exception {
    68         assertExec(
    69             "Retruns default value",
    70             "org_apidesign_vm4brwsr_Instance_interfaceBytesF",
    71             Double.valueOf(31)
    72         );
    73     }
    74 
    75     @Test public void isNull() throws Exception {
    76         assertExec(
    77             "Yes, we are instance",
    78             "org_apidesign_vm4brwsr_Instance_isNullZ",
    79             Double.valueOf(0.0)
    80         );
    81     }
    82 
    83     @Test public void isInstanceOf() throws Exception {
    84         assertExec(
    85             "Yes, we are instance",
    86             "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
    87             Double.valueOf(1.0), true
    88         );
    89     }
    90 
    91     @Test public void notInstanceOf() throws Exception {
    92         assertExec(
    93             "No, we are not an instance",
    94             "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
    95             Double.valueOf(0.0), false
    96         );
    97     }
    98     
    99     @Test public void verifyCastToClass() throws Exception {
   100         assertExec(
   101             "Five signals all is good",
   102             "org_apidesign_vm4brwsr_Instance_castsWorkIZ",
   103             Double.valueOf(5.0), false
   104         );
   105     }
   106     @Test public void verifyCastToInterface() throws Exception {
   107         assertExec(
   108             "Five signals all is good",
   109             "org_apidesign_vm4brwsr_Instance_castsWorkIZ",
   110             Double.valueOf(5.0), true
   111         );
   112     }
   113     
   114     protected String startCompilationWith() {
   115         return "org/apidesign/vm4brwsr/Instance";
   116     }
   117     
   118     private static CharSequence codeSeq;
   119     private static Invocable code;
   120     
   121     @BeforeTest 
   122     public void compileTheCode() throws Exception {
   123         if (codeSeq == null) {
   124             StringBuilder sb = new StringBuilder();
   125             code = StaticMethodTest.compileClass(sb, startCompilationWith());
   126             codeSeq = sb;
   127         }
   128     }
   129     
   130     private void assertExec(
   131         String msg, String methodName, Object expRes, Object... args
   132     ) throws Exception {
   133 
   134         Object ret = null;
   135         try {
   136             ret = code.invokeFunction(methodName, args);
   137         } catch (ScriptException ex) {
   138             fail("Execution failed in\n" + codeSeq, ex);
   139         } catch (NoSuchMethodException ex) {
   140             fail("Cannot find method in\n" + codeSeq, ex);
   141         }
   142         if (ret == null && expRes == null) {
   143             return;
   144         }
   145         if (expRes.equals(ret)) {
   146             return;
   147         }
   148         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   149         
   150     }
   151     
   152 }