vm/src/test/java/org/apidesign/vm4brwsr/ExceptionsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 11 Feb 2013 12:46:43 +0100
branchemul
changeset 708 59d5596a9c6c
parent 423 9b5868bf56ec
permissions -rw-r--r--
Encapsulation. Moving shared code into TestVM instance.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 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.ScriptException;
    21 import static org.testng.Assert.*;
    22 import org.testng.annotations.BeforeClass;
    23 import org.testng.annotations.Test;
    24 
    25 /**
    26  *
    27  * @author Tomas Zezula <tzezula@netbeans.org>
    28  */
    29 public class ExceptionsTest {
    30     @Test
    31     public void verifyMethodWithTryCatchNoThrow() throws Exception {
    32             assertExec(
    33                     "No throw",
    34                     Exceptions.class,
    35                     "methodWithTryCatchNoThrow__I",
    36                     new Double(1.0));
    37     }
    38 
    39     @Test
    40     public void catchJavaScriptStringAsThrowable() throws Exception {
    41         assertExec(
    42             "Throw hello!",
    43             Exceptions.class,
    44             "catchThrowableCatchesAll__Ljava_lang_String_2",
    45             "Hello!"
    46         );
    47     }
    48 
    49     @Test
    50     public void verifyMethodWithTryCatchThrow() throws Exception {
    51             assertExec(
    52                     "Throw",
    53                     Exceptions.class,
    54                     "methodWithTryCatchThrow__I",
    55                     new Double(2.0));
    56     }
    57     
    58     @Test public void createObject() throws Exception {
    59         assertExec("Object created", Exceptions.class, 
    60             "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
    61             "java.lang.Object",
    62             "java.lang.Object"
    63         );
    64     }
    65 
    66     @Test public void createFloatFails() throws Exception {
    67         assertExec("Float not created", Exceptions.class, 
    68             "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
    69             "java.lang.Float",
    70             "java.lang.Float"
    71         );
    72     }
    73 
    74     @Test public void createUnknownFails() throws Exception {
    75         assertExec("Object created", Exceptions.class, 
    76             "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
    77             "CNFE:org.apidesign.Unknown",
    78             "org.apidesign.Unknown"
    79         );
    80     }
    81     
    82     @Test public void testThreeCalls() throws Exception {
    83         Object clazz = code.loadClass("loadClass", Exceptions.class.getName());
    84         
    85         String method = "readCounter__ILjava_lang_String_2";
    86         
    87         try {
    88             Object ret = code.invokeMethod(clazz, method, "org.apidesign.Unknown");
    89             fail("We expect an CNFE!");
    90         } catch (ScriptException scriptException) {
    91             // script exception should be OK
    92         }
    93         {
    94             // 2nd invocation
    95             Object ret = code.invokeMethod(clazz, method, "java.lang.String");
    96             assertEquals(ret, Double.valueOf(2));
    97         }
    98         {
    99             // 3rd invocation
   100             Object ret = code.invokeMethod(clazz, method, "java.lang.Integer");
   101             assertEquals(ret, Double.valueOf(3));
   102         }
   103     }
   104     
   105     private static TestVM code;
   106     
   107     @BeforeClass 
   108     public void compileTheCode() throws Exception {
   109         code = TestVM.compileClass("org/apidesign/vm4brwsr/Exceptions");
   110     }
   111     private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
   112         code.assertExec(msg, clazz, method, expRes, args);
   113     }
   114 }