vm/src/test/java/org/apidesign/vm4brwsr/ExceptionsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 31 Dec 2012 17:50:27 +0100
changeset 401 a9be982d9b9c
parent 400 5452b9fbd253
child 405 e41809be6106
child 423 9b5868bf56ec
permissions -rw-r--r--
Finally block is supported
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.Invocable;
jaroslav@21
    21
import javax.script.ScriptException;
jaroslav@21
    22
import static org.testng.Assert.*;
jaroslav@103
    23
import org.testng.annotations.BeforeClass;
jtulach@128
    24
import org.testng.annotations.Test;
jaroslav@21
    25
jaroslav@21
    26
/**
jaroslav@21
    27
 *
tzezula@285
    28
 * @author Tomas Zezula <tzezula@netbeans.org>
jaroslav@21
    29
 */
tzezula@285
    30
public class ExceptionsTest {
tzezula@285
    31
    @Test
tzezula@285
    32
    public void verifyMethodWithTryCatchNoThrow() throws Exception {
tzezula@287
    33
            assertExec(
tzezula@287
    34
                    "No throw",
tzezula@287
    35
                    Exceptions.class,
tzezula@287
    36
                    "methodWithTryCatchNoThrow__I",
tzezula@287
    37
                    new Double(1.0));
jaroslav@21
    38
    }
tzezula@285
    39
tzezula@285
    40
    @Test
tzezula@285
    41
    public void verifyMethodWithTryCatchThrow() throws Exception {
tzezula@287
    42
            assertExec(
tzezula@287
    43
                    "Throw",
tzezula@287
    44
                    Exceptions.class,
tzezula@287
    45
                    "methodWithTryCatchThrow__I",
tzezula@287
    46
                    new Double(2.0));
jaroslav@104
    47
    }
jaroslav@104
    48
    
jaroslav@400
    49
    @Test public void createObject() throws Exception {
jaroslav@400
    50
        assertExec("Object created", Exceptions.class, 
jaroslav@400
    51
            "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
jaroslav@400
    52
            "java.lang.Object",
jaroslav@400
    53
            "java.lang.Object"
jaroslav@400
    54
        );
jaroslav@400
    55
    }
jaroslav@400
    56
jaroslav@400
    57
    @Test public void createFloatFails() throws Exception {
jaroslav@400
    58
        assertExec("Float not created", Exceptions.class, 
jaroslav@400
    59
            "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
jaroslav@400
    60
            "java.lang.Float",
jaroslav@400
    61
            "java.lang.Float"
jaroslav@400
    62
        );
jaroslav@400
    63
    }
jaroslav@400
    64
jaroslav@400
    65
    @Test public void createUnknownFails() throws Exception {
jaroslav@400
    66
        assertExec("Object created", Exceptions.class, 
jaroslav@400
    67
            "newInstance__Ljava_lang_String_2Ljava_lang_String_2",
jaroslav@400
    68
            "CNFE:org.apidesign.Unknown",
jaroslav@400
    69
            "org.apidesign.Unknown"
jaroslav@400
    70
        );
jaroslav@400
    71
    }
jaroslav@400
    72
    
jaroslav@401
    73
    @Test public void testThreeCalls() throws Exception {
jaroslav@401
    74
        Object vm = code.invokeFunction("bck2brwsr");
jaroslav@401
    75
        Object clazz = code.invokeMethod(vm, "loadClass", Exceptions.class.getName());
jaroslav@401
    76
        
jaroslav@401
    77
        String method = "readCounter__ILjava_lang_String_2";
jaroslav@401
    78
        
jaroslav@401
    79
        try {
jaroslav@401
    80
            Object ret = code.invokeMethod(clazz, method, "org.apidesign.Unknown");
jaroslav@401
    81
            fail("We expect an CNFE!");
jaroslav@401
    82
        } catch (ScriptException scriptException) {
jaroslav@401
    83
            // script exception should be OK
jaroslav@401
    84
        }
jaroslav@401
    85
        {
jaroslav@401
    86
            // 2nd invocation
jaroslav@401
    87
            Object ret = code.invokeMethod(clazz, method, "java.lang.String");
jaroslav@401
    88
            assertEquals(ret, Double.valueOf(2));
jaroslav@401
    89
        }
jaroslav@401
    90
        {
jaroslav@401
    91
            // 3rd invocation
jaroslav@401
    92
            Object ret = code.invokeMethod(clazz, method, "java.lang.Integer");
jaroslav@401
    93
            assertEquals(ret, Double.valueOf(3));
jaroslav@401
    94
        }
jaroslav@401
    95
    }
jaroslav@401
    96
    
jaroslav@103
    97
    private static CharSequence codeSeq;
jaroslav@103
    98
    private static Invocable code;
jaroslav@103
    99
    
jaroslav@103
   100
    @BeforeClass 
jaroslav@103
   101
    public void compileTheCode() throws Exception {
jaroslav@21
   102
        StringBuilder sb = new StringBuilder();
jaroslav@103
   103
        code = StaticMethodTest.compileClass(sb, 
tzezula@285
   104
            "org/apidesign/vm4brwsr/Exceptions"
jaroslav@21
   105
        );
jaroslav@103
   106
        codeSeq = sb;
jaroslav@103
   107
    }
jaroslav@203
   108
    private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
jaroslav@203
   109
        StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
jaroslav@21
   110
    }
jaroslav@21
   111
}