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.
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@106
     4
 *
jaroslav@106
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@106
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@106
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@106
     8
 *
jaroslav@106
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@106
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@106
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@106
    12
 * GNU General Public License for more details.
jaroslav@106
    13
 *
jaroslav@106
    14
 * You should have received a copy of the GNU General Public License
jaroslav@106
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@106
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@106
    17
 */
jaroslav@22
    18
package org.apidesign.vm4brwsr;
jaroslav@21
    19
jaroslav@21
    20
import javax.script.ScriptException;
jaroslav@21
    21
import static org.testng.Assert.*;
jaroslav@103
    22
import org.testng.annotations.BeforeClass;
jtulach@128
    23
import org.testng.annotations.Test;
jaroslav@21
    24
jaroslav@21
    25
/**
jaroslav@21
    26
 *
tzezula@285
    27
 * @author Tomas Zezula <tzezula@netbeans.org>
jaroslav@21
    28
 */
tzezula@285
    29
public class ExceptionsTest {
tzezula@285
    30
    @Test
tzezula@285
    31
    public void verifyMethodWithTryCatchNoThrow() throws Exception {
tzezula@287
    32
            assertExec(
tzezula@287
    33
                    "No throw",
tzezula@287
    34
                    Exceptions.class,
tzezula@287
    35
                    "methodWithTryCatchNoThrow__I",
tzezula@287
    36
                    new Double(1.0));
jaroslav@21
    37
    }
tzezula@285
    38
tzezula@285
    39
    @Test
jaroslav@423
    40
    public void catchJavaScriptStringAsThrowable() throws Exception {
jaroslav@423
    41
        assertExec(
jaroslav@423
    42
            "Throw hello!",
jaroslav@423
    43
            Exceptions.class,
jaroslav@423
    44
            "catchThrowableCatchesAll__Ljava_lang_String_2",
jaroslav@423
    45
            "Hello!"
jaroslav@423
    46
        );
jaroslav@423
    47
    }
jaroslav@423
    48
jaroslav@423
    49
    @Test
tzezula@285
    50
    public void verifyMethodWithTryCatchThrow() throws Exception {
tzezula@287
    51
            assertExec(
tzezula@287
    52
                    "Throw",
tzezula@287
    53
                    Exceptions.class,
tzezula@287
    54
                    "methodWithTryCatchThrow__I",
tzezula@287
    55
                    new Double(2.0));
jaroslav@104
    56
    }
jaroslav@104
    57
    
jaroslav@400
    58
    @Test public void createObject() throws Exception {
jaroslav@400
    59
        assertExec("Object created", Exceptions.class, 
jaroslav@400
    60
            "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
jaroslav@400
    61
            "java.lang.Object",
jaroslav@400
    62
            "java.lang.Object"
jaroslav@400
    63
        );
jaroslav@400
    64
    }
jaroslav@400
    65
jaroslav@400
    66
    @Test public void createFloatFails() throws Exception {
jaroslav@400
    67
        assertExec("Float not created", Exceptions.class, 
jaroslav@400
    68
            "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
jaroslav@400
    69
            "java.lang.Float",
jaroslav@400
    70
            "java.lang.Float"
jaroslav@400
    71
        );
jaroslav@400
    72
    }
jaroslav@400
    73
jaroslav@400
    74
    @Test public void createUnknownFails() throws Exception {
jaroslav@400
    75
        assertExec("Object created", Exceptions.class, 
jaroslav@400
    76
            "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
jaroslav@400
    77
            "CNFE:org.apidesign.Unknown",
jaroslav@400
    78
            "org.apidesign.Unknown"
jaroslav@400
    79
        );
jaroslav@400
    80
    }
jaroslav@400
    81
    
jaroslav@401
    82
    @Test public void testThreeCalls() throws Exception {
jaroslav@708
    83
        Object clazz = code.loadClass("loadClass", Exceptions.class.getName());
jaroslav@401
    84
        
jaroslav@401
    85
        String method = "readCounter__ILjava_lang_String_2";
jaroslav@401
    86
        
jaroslav@401
    87
        try {
jaroslav@401
    88
            Object ret = code.invokeMethod(clazz, method, "org.apidesign.Unknown");
jaroslav@401
    89
            fail("We expect an CNFE!");
jaroslav@401
    90
        } catch (ScriptException scriptException) {
jaroslav@401
    91
            // script exception should be OK
jaroslav@401
    92
        }
jaroslav@401
    93
        {
jaroslav@401
    94
            // 2nd invocation
jaroslav@401
    95
            Object ret = code.invokeMethod(clazz, method, "java.lang.String");
jaroslav@401
    96
            assertEquals(ret, Double.valueOf(2));
jaroslav@401
    97
        }
jaroslav@401
    98
        {
jaroslav@401
    99
            // 3rd invocation
jaroslav@401
   100
            Object ret = code.invokeMethod(clazz, method, "java.lang.Integer");
jaroslav@401
   101
            assertEquals(ret, Double.valueOf(3));
jaroslav@401
   102
        }
jaroslav@401
   103
    }
jaroslav@401
   104
    
jaroslav@708
   105
    private static TestVM code;
jaroslav@103
   106
    
jaroslav@103
   107
    @BeforeClass 
jaroslav@103
   108
    public void compileTheCode() throws Exception {
jaroslav@708
   109
        code = TestVM.compileClass("org/apidesign/vm4brwsr/Exceptions");
jaroslav@103
   110
    }
jaroslav@203
   111
    private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
jaroslav@708
   112
        code.assertExec(msg, clazz, method, expRes, args);
jaroslav@21
   113
    }
jaroslav@21
   114
}