jaroslav@8
|
1 |
/*
|
jaroslav@8
|
2 |
* To change this template, choose Tools | Templates
|
jaroslav@8
|
3 |
* and open the template in the editor.
|
jaroslav@8
|
4 |
*/
|
jaroslav@8
|
5 |
package org.apidesign.java4browser;
|
jaroslav@8
|
6 |
|
jaroslav@8
|
7 |
import javax.script.Invocable;
|
jaroslav@8
|
8 |
import javax.script.ScriptException;
|
jaroslav@8
|
9 |
import org.testng.annotations.Test;
|
jaroslav@8
|
10 |
import static org.testng.Assert.*;
|
jaroslav@8
|
11 |
|
jaroslav@8
|
12 |
/**
|
jaroslav@8
|
13 |
*
|
jaroslav@8
|
14 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@8
|
15 |
*/
|
jaroslav@8
|
16 |
public class InstanceTest {
|
jaroslav@10
|
17 |
@Test public void verifyDefaultDoubleValue() throws Exception {
|
jaroslav@10
|
18 |
assertExec(
|
jaroslav@10
|
19 |
"Will be zero",
|
jaroslav@10
|
20 |
"org_apidesign_java4browser_Instance_defaultDblValueD",
|
jaroslav@10
|
21 |
Double.valueOf(0)
|
jaroslav@10
|
22 |
);
|
jaroslav@10
|
23 |
}
|
jaroslav@10
|
24 |
@Test public void verifyAssignedByteValue() throws Exception {
|
jaroslav@10
|
25 |
assertExec(
|
jaroslav@10
|
26 |
"Will one thirty one",
|
jaroslav@10
|
27 |
"org_apidesign_java4browser_Instance_assignedByteValueB",
|
jaroslav@10
|
28 |
Double.valueOf(31)
|
jaroslav@10
|
29 |
);
|
jaroslav@10
|
30 |
}
|
jaroslav@8
|
31 |
@Test public void verifyMagicOne() throws Exception {
|
jaroslav@8
|
32 |
assertExec(
|
jaroslav@10
|
33 |
"Should be three and something",
|
jaroslav@8
|
34 |
"org_apidesign_java4browser_Instance_magicOneD",
|
jaroslav@8
|
35 |
Double.valueOf(3.3)
|
jaroslav@8
|
36 |
);
|
jaroslav@8
|
37 |
}
|
jaroslav@12
|
38 |
@Test public void verifyInstanceMethods() throws Exception {
|
jaroslav@12
|
39 |
assertExec(
|
jaroslav@14
|
40 |
"Should be eleven as we invoke overwritten method, plus 44",
|
jaroslav@14
|
41 |
"org_apidesign_java4browser_Instance_virtualBytesI",
|
jaroslav@14
|
42 |
Double.valueOf(55)
|
jaroslav@12
|
43 |
);
|
jaroslav@12
|
44 |
}
|
jaroslav@8
|
45 |
|
jaroslav@8
|
46 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@8
|
47 |
StringBuilder sb = new StringBuilder();
|
jaroslav@13
|
48 |
Invocable i = StaticMethodTest.compileClass(sb, "Instance.class", "InstanceSub.class");
|
jaroslav@8
|
49 |
|
jaroslav@8
|
50 |
Object ret = null;
|
jaroslav@8
|
51 |
try {
|
jaroslav@8
|
52 |
ret = i.invokeFunction(methodName, args);
|
jaroslav@8
|
53 |
} catch (ScriptException ex) {
|
jaroslav@8
|
54 |
fail("Execution failed in " + sb, ex);
|
jaroslav@8
|
55 |
} catch (NoSuchMethodException ex) {
|
jaroslav@8
|
56 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@8
|
57 |
}
|
jaroslav@8
|
58 |
if (ret == null && expRes == null) {
|
jaroslav@8
|
59 |
return;
|
jaroslav@8
|
60 |
}
|
jaroslav@8
|
61 |
if (expRes.equals(ret)) {
|
jaroslav@8
|
62 |
return;
|
jaroslav@8
|
63 |
}
|
jaroslav@8
|
64 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
|
jaroslav@8
|
65 |
|
jaroslav@8
|
66 |
}
|
jaroslav@8
|
67 |
|
jaroslav@8
|
68 |
}
|