src/test/java/org/apidesign/java4browser/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Sep 2012 20:22:12 +0200
changeset 14 018eee22b8cf
parent 13 99f832e5765f
child 15 65f6fdfd34b6
permissions -rw-r--r--
Try multiple instances of the same class and make sure they don't influence their variables
     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     
    46     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    47         StringBuilder sb = new StringBuilder();
    48         Invocable i = StaticMethodTest.compileClass(sb, "Instance.class", "InstanceSub.class");
    49         
    50         Object ret = null;
    51         try {
    52             ret = i.invokeFunction(methodName, args);
    53         } catch (ScriptException ex) {
    54             fail("Execution failed in " + sb, ex);
    55         } catch (NoSuchMethodException ex) {
    56             fail("Cannot find method in " + sb, ex);
    57         }
    58         if (ret == null && expRes == null) {
    59             return;
    60         }
    61         if (expRes.equals(ret)) {
    62             return;
    63         }
    64         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    65         
    66     }
    67     
    68 }