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@789
|
22 |
import org.testng.annotations.AfterClass;
|
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
|
jaroslav@423
|
41 |
public void catchJavaScriptStringAsThrowable() throws Exception {
|
jaroslav@423
|
42 |
assertExec(
|
jaroslav@423
|
43 |
"Throw hello!",
|
jaroslav@423
|
44 |
Exceptions.class,
|
jaroslav@423
|
45 |
"catchThrowableCatchesAll__Ljava_lang_String_2",
|
jaroslav@423
|
46 |
"Hello!"
|
jaroslav@423
|
47 |
);
|
jaroslav@423
|
48 |
}
|
jaroslav@423
|
49 |
|
jaroslav@423
|
50 |
@Test
|
tzezula@285
|
51 |
public void verifyMethodWithTryCatchThrow() throws Exception {
|
tzezula@287
|
52 |
assertExec(
|
tzezula@287
|
53 |
"Throw",
|
tzezula@287
|
54 |
Exceptions.class,
|
tzezula@287
|
55 |
"methodWithTryCatchThrow__I",
|
tzezula@287
|
56 |
new Double(2.0));
|
jaroslav@104
|
57 |
}
|
jaroslav@104
|
58 |
|
jaroslav@400
|
59 |
@Test public void createObject() throws Exception {
|
jaroslav@400
|
60 |
assertExec("Object created", Exceptions.class,
|
jaroslav@400
|
61 |
"newInstance__Ljava_lang_String_2Ljava_lang_String_2",
|
jaroslav@400
|
62 |
"java.lang.Object",
|
jaroslav@400
|
63 |
"java.lang.Object"
|
jaroslav@400
|
64 |
);
|
jaroslav@400
|
65 |
}
|
jaroslav@400
|
66 |
|
jaroslav@400
|
67 |
@Test public void createFloatFails() throws Exception {
|
jaroslav@400
|
68 |
assertExec("Float not created", Exceptions.class,
|
jaroslav@400
|
69 |
"newInstance__Ljava_lang_String_2Ljava_lang_String_2",
|
jaroslav@400
|
70 |
"java.lang.Float",
|
jaroslav@400
|
71 |
"java.lang.Float"
|
jaroslav@400
|
72 |
);
|
jaroslav@400
|
73 |
}
|
jaroslav@400
|
74 |
|
jaroslav@400
|
75 |
@Test public void createUnknownFails() throws Exception {
|
jaroslav@400
|
76 |
assertExec("Object created", Exceptions.class,
|
jaroslav@400
|
77 |
"newInstance__Ljava_lang_String_2Ljava_lang_String_2",
|
jaroslav@400
|
78 |
"CNFE:org.apidesign.Unknown",
|
jaroslav@400
|
79 |
"org.apidesign.Unknown"
|
jaroslav@400
|
80 |
);
|
jaroslav@400
|
81 |
}
|
jaroslav@400
|
82 |
|
jaroslav@401
|
83 |
@Test public void testThreeCalls() throws Exception {
|
jaroslav@708
|
84 |
Object clazz = code.loadClass("loadClass", Exceptions.class.getName());
|
jaroslav@401
|
85 |
|
jaroslav@401
|
86 |
String method = "readCounter__ILjava_lang_String_2";
|
jaroslav@401
|
87 |
|
jaroslav@401
|
88 |
try {
|
jaroslav@401
|
89 |
Object ret = code.invokeMethod(clazz, method, "org.apidesign.Unknown");
|
jaroslav@401
|
90 |
fail("We expect an CNFE!");
|
jaroslav@401
|
91 |
} catch (ScriptException scriptException) {
|
jaroslav@401
|
92 |
// script exception should be OK
|
jaroslav@401
|
93 |
}
|
jaroslav@401
|
94 |
{
|
jaroslav@401
|
95 |
// 2nd invocation
|
jaroslav@401
|
96 |
Object ret = code.invokeMethod(clazz, method, "java.lang.String");
|
jaroslav@1392
|
97 |
assertTrue(ret instanceof Number, "Is number: " + ret);
|
jaroslav@1392
|
98 |
assertEquals(((Number)ret).doubleValue(), 2.0);
|
jaroslav@401
|
99 |
}
|
jaroslav@401
|
100 |
{
|
jaroslav@401
|
101 |
// 3rd invocation
|
jaroslav@401
|
102 |
Object ret = code.invokeMethod(clazz, method, "java.lang.Integer");
|
jaroslav@1392
|
103 |
assertTrue(ret instanceof Number, "Is number: " + ret);
|
jaroslav@1392
|
104 |
assertEquals(((Number)ret).doubleValue(), 3.0);
|
jaroslav@401
|
105 |
}
|
jaroslav@401
|
106 |
}
|
jaroslav@401
|
107 |
|
jaroslav@708
|
108 |
private static TestVM code;
|
jaroslav@103
|
109 |
|
jaroslav@103
|
110 |
@BeforeClass
|
jaroslav@103
|
111 |
public void compileTheCode() throws Exception {
|
jaroslav@708
|
112 |
code = TestVM.compileClass("org/apidesign/vm4brwsr/Exceptions");
|
jaroslav@103
|
113 |
}
|
jaroslav@789
|
114 |
@AfterClass
|
jaroslav@789
|
115 |
public static void releaseTheCode() {
|
jaroslav@789
|
116 |
code = null;
|
jaroslav@789
|
117 |
}
|
jaroslav@203
|
118 |
private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
|
jaroslav@708
|
119 |
code.assertExec(msg, clazz, method, expRes, args);
|
jaroslav@21
|
120 |
}
|
jaroslav@21
|
121 |
}
|