vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Sep 2012 11:07:38 +0200
changeset 22 b9318fe303cd
parent 19 src/test/java/org/apidesign/java4browser/InstanceTest.java@2291e553464a
child 24 a82e89aae050
permissions -rw-r--r--
Getting ready for multiple projects inside one Hg repository
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 package org.apidesign.vm4brwsr;
     6 
     7 import javax.script.Invocable;
     8 import javax.script.ScriptException;
     9 import org.testng.annotations.Test;
    10 import static org.testng.Assert.*;
    11 
    12 /**
    13  *
    14  * @author Jaroslav Tulach <jtulach@netbeans.org>
    15  */
    16 public class InstanceTest {
    17     @Test public void verifyDefaultDoubleValue() throws Exception {
    18         assertExec(
    19             "Will be zero",
    20             "org_apidesign_vm4brwsr_Instance_defaultDblValueD",
    21             Double.valueOf(0)
    22         );
    23     }
    24     @Test public void verifyAssignedByteValue() throws Exception {
    25         assertExec(
    26             "Will one thirty one",
    27             "org_apidesign_vm4brwsr_Instance_assignedByteValueB",
    28             Double.valueOf(31)
    29         );
    30     }
    31     @Test public void verifyMagicOne() throws Exception {
    32         assertExec(
    33             "Should be three and something",
    34             "org_apidesign_vm4brwsr_Instance_magicOneD",
    35             Double.valueOf(3.3)
    36         );
    37     }
    38     @Test public void verifyInstanceMethods() throws Exception {
    39         assertExec(
    40             "Should be eleven as we invoke overwritten method, plus 44",
    41             "org_apidesign_vm4brwsr_Instance_virtualBytesI",
    42             Double.valueOf(55)
    43         );
    44     }
    45     @Test public void verifyInterfaceMethods() throws Exception {
    46         assertExec(
    47             "Retruns default value",
    48             "org_apidesign_vm4brwsr_Instance_interfaceBytesF",
    49             Double.valueOf(31)
    50         );
    51     }
    52 
    53     @Test public void isNull() throws Exception {
    54         assertExec(
    55             "Yes, we are instance",
    56             "org_apidesign_vm4brwsr_Instance_isNullZ",
    57             Double.valueOf(0.0)
    58         );
    59     }
    60 
    61     @Test public void isInstanceOf() throws Exception {
    62         assertExec(
    63             "Yes, we are instance",
    64             "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
    65             Double.valueOf(1.0), true
    66         );
    67     }
    68 
    69     @Test public void notInstanceOf() throws Exception {
    70         assertExec(
    71             "No, we are not an instance",
    72             "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
    73             Double.valueOf(0.0), false
    74         );
    75     }
    76     
    77     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    78         StringBuilder sb = new StringBuilder();
    79         Invocable i = StaticMethodTest.compileClass(sb, 
    80             "org/apidesign/vm4brwsr/Instance"
    81         );
    82         
    83         Object ret = null;
    84         try {
    85             ret = i.invokeFunction(methodName, args);
    86         } catch (ScriptException ex) {
    87             fail("Execution failed in " + sb, ex);
    88         } catch (NoSuchMethodException ex) {
    89             fail("Cannot find method in " + sb, ex);
    90         }
    91         if (ret == null && expRes == null) {
    92             return;
    93         }
    94         if (expRes.equals(ret)) {
    95             return;
    96         }
    97         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    98         
    99     }
   100     
   101 }