1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ExceptionsTest.java Sat Dec 08 08:19:46 2012 +0100
1.3 @@ -0,0 +1,88 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +/*
1.22 +Java 4 Browser Bytecode Translator
1.23 +Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.24 +
1.25 +This program is free software: you can redistribute it and/or modify
1.26 +it under the terms of the GNU General Public License as published by
1.27 +the Free Software Foundation, version 2 of the License.
1.28 +
1.29 +This program is distributed in the hope that it will be useful,
1.30 +but WITHOUT ANY WARRANTY; without even the implied warranty of
1.31 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.32 +GNU General Public License for more details.
1.33 +
1.34 +You should have received a copy of the GNU General Public License
1.35 +along with this program. Look for COPYING file in the top folder.
1.36 +If not, see http://opensource.org/licenses/GPL-2.0.
1.37 +*/
1.38 +package org.apidesign.vm4brwsr;
1.39 +
1.40 +import javax.script.Invocable;
1.41 +import javax.script.ScriptException;
1.42 +import org.testng.annotations.Test;
1.43 +import static org.testng.Assert.*;
1.44 +import org.testng.annotations.BeforeClass;
1.45 +
1.46 +/**
1.47 + *
1.48 + * @author Tomas Zezula <tzezula@netbeans.org>
1.49 + */
1.50 +public class ExceptionsTest {
1.51 + @Test
1.52 + public void verifyMethodWithTryCatchNoThrow() throws Exception {
1.53 + assertExec("MethodWithTryCatch", "org_apidesign_vm4brwsr_Exceptions_methodWithTryCatchNoThrowI",
1.54 + new Double(1.0));
1.55 + }
1.56 +
1.57 + @Test
1.58 + public void verifyMethodWithTryCatchThrow() throws Exception {
1.59 + assertExec("MethodWithTryCatch", "org_apidesign_vm4brwsr_Exceptions_methodWithTryCatchThrowI",
1.60 + new Double(1.0));
1.61 + }
1.62 +
1.63 + private static CharSequence codeSeq;
1.64 + private static Invocable code;
1.65 +
1.66 + @BeforeClass
1.67 + public void compileTheCode() throws Exception {
1.68 + StringBuilder sb = new StringBuilder();
1.69 + code = StaticMethodTest.compileClass(sb,
1.70 + "org/apidesign/vm4brwsr/Exceptions"
1.71 + );
1.72 + codeSeq = sb;
1.73 + }
1.74 + private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
1.75 + Object ret = null;
1.76 + try {
1.77 + ret = code.invokeFunction(methodName, args);
1.78 + } catch (ScriptException ex) {
1.79 + fail("Execution failed in\n" + codeSeq, ex);
1.80 + } catch (NoSuchMethodException ex) {
1.81 + fail("Cannot find method in\n" + codeSeq, ex);
1.82 + }
1.83 + if (ret == null && expRes == null) {
1.84 + return;
1.85 + }
1.86 + if (expRes.equals(ret)) {
1.87 + return;
1.88 + }
1.89 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
1.90 + }
1.91 +}