src/test/java/org/apidesign/java4browser/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 20 Sep 2012 14:20:01 +0200
changeset 18 361b76189f8d
parent 17 cb0cfba1b863
child 19 2291e553464a
permissions -rw-r--r--
The compilation to JavaScript now identifies list of external references and provides them back to the caller
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 package org.apidesign.java4browser;
     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_java4browser_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_java4browser_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_java4browser_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_java4browser_Instance_virtualBytesI",
    42             Double.valueOf(55)
    43         );
    44     }
    45     @Test public void verifyInterfaceMethods() throws Exception {
    46         assertExec(
    47             "Retruns default value",
    48             "org_apidesign_java4browser_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_java4browser_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_java4browser_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_java4browser_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/java4browser/Instance",
    81             "org/apidesign/java4browser/InstanceSub"
    82         );
    83         
    84         Object ret = null;
    85         try {
    86             ret = i.invokeFunction(methodName, args);
    87         } catch (ScriptException ex) {
    88             fail("Execution failed in " + sb, ex);
    89         } catch (NoSuchMethodException ex) {
    90             fail("Cannot find method in " + sb, ex);
    91         }
    92         if (ret == null && expRes == null) {
    93             return;
    94         }
    95         if (expRes.equals(ret)) {
    96             return;
    97         }
    98         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    99         
   100     }
   101     
   102 }