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@22
|
5 |
package org.apidesign.vm4brwsr;
|
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@22
|
20 |
"org_apidesign_vm4brwsr_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@22
|
27 |
"org_apidesign_vm4brwsr_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@22
|
34 |
"org_apidesign_vm4brwsr_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@22
|
41 |
"org_apidesign_vm4brwsr_Instance_virtualBytesI",
|
jaroslav@14
|
42 |
Double.valueOf(55)
|
jaroslav@12
|
43 |
);
|
jaroslav@12
|
44 |
}
|
jaroslav@15
|
45 |
@Test public void verifyInterfaceMethods() throws Exception {
|
jaroslav@15
|
46 |
assertExec(
|
jaroslav@15
|
47 |
"Retruns default value",
|
jaroslav@22
|
48 |
"org_apidesign_vm4brwsr_Instance_interfaceBytesF",
|
jaroslav@15
|
49 |
Double.valueOf(31)
|
jaroslav@15
|
50 |
);
|
jaroslav@15
|
51 |
}
|
jaroslav@16
|
52 |
|
jaroslav@16
|
53 |
@Test public void isNull() throws Exception {
|
jaroslav@16
|
54 |
assertExec(
|
jaroslav@16
|
55 |
"Yes, we are instance",
|
jaroslav@22
|
56 |
"org_apidesign_vm4brwsr_Instance_isNullZ",
|
jaroslav@16
|
57 |
Double.valueOf(0.0)
|
jaroslav@16
|
58 |
);
|
jaroslav@16
|
59 |
}
|
jaroslav@17
|
60 |
|
jaroslav@16
|
61 |
@Test public void isInstanceOf() throws Exception {
|
jaroslav@16
|
62 |
assertExec(
|
jaroslav@16
|
63 |
"Yes, we are instance",
|
jaroslav@22
|
64 |
"org_apidesign_vm4brwsr_Instance_instanceOfZZ",
|
jaroslav@16
|
65 |
Double.valueOf(1.0), true
|
jaroslav@16
|
66 |
);
|
jaroslav@16
|
67 |
}
|
jaroslav@16
|
68 |
|
jaroslav@16
|
69 |
@Test public void notInstanceOf() throws Exception {
|
jaroslav@16
|
70 |
assertExec(
|
jaroslav@16
|
71 |
"No, we are not an instance",
|
jaroslav@22
|
72 |
"org_apidesign_vm4brwsr_Instance_instanceOfZZ",
|
jaroslav@16
|
73 |
Double.valueOf(0.0), false
|
jaroslav@16
|
74 |
);
|
jaroslav@16
|
75 |
}
|
jaroslav@17
|
76 |
|
jaroslav@8
|
77 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@8
|
78 |
StringBuilder sb = new StringBuilder();
|
jaroslav@18
|
79 |
Invocable i = StaticMethodTest.compileClass(sb,
|
jaroslav@22
|
80 |
"org/apidesign/vm4brwsr/Instance"
|
jaroslav@18
|
81 |
);
|
jaroslav@8
|
82 |
|
jaroslav@8
|
83 |
Object ret = null;
|
jaroslav@8
|
84 |
try {
|
jaroslav@8
|
85 |
ret = i.invokeFunction(methodName, args);
|
jaroslav@8
|
86 |
} catch (ScriptException ex) {
|
jaroslav@8
|
87 |
fail("Execution failed in " + sb, ex);
|
jaroslav@8
|
88 |
} catch (NoSuchMethodException ex) {
|
jaroslav@8
|
89 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@8
|
90 |
}
|
jaroslav@8
|
91 |
if (ret == null && expRes == null) {
|
jaroslav@8
|
92 |
return;
|
jaroslav@8
|
93 |
}
|
jaroslav@8
|
94 |
if (expRes.equals(ret)) {
|
jaroslav@8
|
95 |
return;
|
jaroslav@8
|
96 |
}
|
jaroslav@8
|
97 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
|
jaroslav@8
|
98 |
|
jaroslav@8
|
99 |
}
|
jaroslav@8
|
100 |
|
jaroslav@8
|
101 |
}
|