src/test/java/org/apidesign/java4browser/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 16 Sep 2012 18:35:16 +0200
changeset 10 e84d9314f1bc
parent 8 82772c96ec57
child 12 282828609b86
permissions -rw-r--r--
Basic support for non-virtual instance methods and constructors
     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     
    39     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    40         StringBuilder sb = new StringBuilder();
    41         Invocable i = StaticMethodTest.compileClass("Instance.class", sb);
    42         
    43         Object ret = null;
    44         try {
    45             ret = i.invokeFunction(methodName, args);
    46         } catch (ScriptException ex) {
    47             fail("Execution failed in " + sb, ex);
    48         } catch (NoSuchMethodException ex) {
    49             fail("Cannot find method in " + sb, ex);
    50         }
    51         if (ret == null && expRes == null) {
    52             return;
    53         }
    54         if (expRes.equals(ret)) {
    55             return;
    56         }
    57         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    58         
    59     }
    60     
    61 }