vm/src/test/java/org/apidesign/vm4brwsr/ExceptionsTest.java
author tzezula
Sat, 08 Dec 2012 08:50:32 +0100
branchexceptions
changeset 286 15053b74bdd9
parent 285 c8be2d837788
child 287 6f696a0ef12f
permissions -rw-r--r--
Fixing unit test.
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@21
    18
/*
jaroslav@21
    19
Java 4 Browser Bytecode Translator
jaroslav@21
    20
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@21
    21
jaroslav@21
    22
This program is free software: you can redistribute it and/or modify
jaroslav@21
    23
it under the terms of the GNU General Public License as published by
jaroslav@21
    24
the Free Software Foundation, version 2 of the License.
jaroslav@21
    25
jaroslav@21
    26
This program is distributed in the hope that it will be useful,
jaroslav@21
    27
but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@21
    28
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@21
    29
GNU General Public License for more details.
jaroslav@21
    30
jaroslav@21
    31
You should have received a copy of the GNU General Public License
jaroslav@21
    32
along with this program. Look for COPYING file in the top folder.
jaroslav@21
    33
If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@21
    34
*/
jaroslav@22
    35
package org.apidesign.vm4brwsr;
jaroslav@21
    36
jaroslav@21
    37
import javax.script.Invocable;
jaroslav@21
    38
import javax.script.ScriptException;
jaroslav@21
    39
import org.testng.annotations.Test;
jaroslav@21
    40
import static org.testng.Assert.*;
jaroslav@103
    41
import org.testng.annotations.BeforeClass;
jaroslav@21
    42
jaroslav@21
    43
/**
jaroslav@21
    44
 *
tzezula@285
    45
 * @author Tomas Zezula <tzezula@netbeans.org>
jaroslav@21
    46
 */
tzezula@285
    47
public class ExceptionsTest {
tzezula@285
    48
    @Test
tzezula@285
    49
    public void verifyMethodWithTryCatchNoThrow() throws Exception {
tzezula@285
    50
            assertExec("MethodWithTryCatch", "org_apidesign_vm4brwsr_Exceptions_methodWithTryCatchNoThrowI",
tzezula@285
    51
            new Double(1.0));
jaroslav@21
    52
    }
tzezula@285
    53
tzezula@285
    54
    @Test
tzezula@285
    55
    public void verifyMethodWithTryCatchThrow() throws Exception {
tzezula@285
    56
            assertExec("MethodWithTryCatch", "org_apidesign_vm4brwsr_Exceptions_methodWithTryCatchThrowI",
tzezula@286
    57
            new Double(2.0));
jaroslav@104
    58
    }
jaroslav@104
    59
    
jaroslav@103
    60
    private static CharSequence codeSeq;
jaroslav@103
    61
    private static Invocable code;
jaroslav@103
    62
    
jaroslav@103
    63
    @BeforeClass 
jaroslav@103
    64
    public void compileTheCode() throws Exception {
jaroslav@21
    65
        StringBuilder sb = new StringBuilder();
jaroslav@103
    66
        code = StaticMethodTest.compileClass(sb, 
tzezula@285
    67
            "org/apidesign/vm4brwsr/Exceptions"
jaroslav@21
    68
        );
jaroslav@103
    69
        codeSeq = sb;
jaroslav@103
    70
    }
jaroslav@103
    71
    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
jaroslav@21
    72
        Object ret = null;
jaroslav@21
    73
        try {
jaroslav@103
    74
            ret = code.invokeFunction(methodName, args);
jaroslav@21
    75
        } catch (ScriptException ex) {
jaroslav@103
    76
            fail("Execution failed in\n" + codeSeq, ex);
jaroslav@21
    77
        } catch (NoSuchMethodException ex) {
jaroslav@103
    78
            fail("Cannot find method in\n" + codeSeq, ex);
jaroslav@21
    79
        }
jaroslav@21
    80
        if (ret == null && expRes == null) {
jaroslav@21
    81
            return;
jaroslav@21
    82
        }
jaroslav@21
    83
        if (expRes.equals(ret)) {
jaroslav@21
    84
            return;
jaroslav@21
    85
        }
jaroslav@103
    86
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
jaroslav@21
    87
    }
jaroslav@21
    88
}