jaroslav@24
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@106
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@24
|
4 |
*
|
jaroslav@24
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@24
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@24
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@24
|
8 |
*
|
jaroslav@24
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@24
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@24
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@24
|
12 |
* GNU General Public License for more details.
|
jaroslav@24
|
13 |
*
|
jaroslav@24
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@24
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@24
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@8
|
17 |
*/
|
jaroslav@22
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@8
|
19 |
|
jaroslav@8
|
20 |
import javax.script.Invocable;
|
jaroslav@8
|
21 |
import javax.script.ScriptException;
|
jaroslav@8
|
22 |
import org.testng.annotations.Test;
|
jaroslav@8
|
23 |
import static org.testng.Assert.*;
|
jaroslav@103
|
24 |
import org.testng.annotations.BeforeClass;
|
jaroslav@103
|
25 |
import org.testng.annotations.BeforeTest;
|
jaroslav@8
|
26 |
|
jaroslav@8
|
27 |
/**
|
jaroslav@8
|
28 |
*
|
jaroslav@8
|
29 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@8
|
30 |
*/
|
jaroslav@8
|
31 |
public class InstanceTest {
|
jaroslav@10
|
32 |
@Test public void verifyDefaultDoubleValue() throws Exception {
|
jaroslav@10
|
33 |
assertExec(
|
jaroslav@10
|
34 |
"Will be zero",
|
jaroslav@22
|
35 |
"org_apidesign_vm4brwsr_Instance_defaultDblValueD",
|
jaroslav@10
|
36 |
Double.valueOf(0)
|
jaroslav@10
|
37 |
);
|
jaroslav@10
|
38 |
}
|
jaroslav@102
|
39 |
@Test public void verifyStaticMethodCall() throws Exception {
|
jaroslav@102
|
40 |
assertExec(
|
jaroslav@102
|
41 |
"Will be zero",
|
jaroslav@102
|
42 |
"org_apidesign_vm4brwsr_InstanceSub_recallDblD",
|
jaroslav@102
|
43 |
Double.valueOf(0)
|
jaroslav@102
|
44 |
);
|
jaroslav@102
|
45 |
}
|
jaroslav@10
|
46 |
@Test public void verifyAssignedByteValue() throws Exception {
|
jaroslav@10
|
47 |
assertExec(
|
jaroslav@10
|
48 |
"Will one thirty one",
|
jaroslav@22
|
49 |
"org_apidesign_vm4brwsr_Instance_assignedByteValueB",
|
jaroslav@10
|
50 |
Double.valueOf(31)
|
jaroslav@10
|
51 |
);
|
jaroslav@10
|
52 |
}
|
jaroslav@8
|
53 |
@Test public void verifyMagicOne() throws Exception {
|
jaroslav@8
|
54 |
assertExec(
|
jaroslav@10
|
55 |
"Should be three and something",
|
jaroslav@22
|
56 |
"org_apidesign_vm4brwsr_Instance_magicOneD",
|
jaroslav@8
|
57 |
Double.valueOf(3.3)
|
jaroslav@8
|
58 |
);
|
jaroslav@8
|
59 |
}
|
jaroslav@12
|
60 |
@Test public void verifyInstanceMethods() throws Exception {
|
jaroslav@12
|
61 |
assertExec(
|
jaroslav@14
|
62 |
"Should be eleven as we invoke overwritten method, plus 44",
|
jaroslav@22
|
63 |
"org_apidesign_vm4brwsr_Instance_virtualBytesI",
|
jaroslav@14
|
64 |
Double.valueOf(55)
|
jaroslav@12
|
65 |
);
|
jaroslav@12
|
66 |
}
|
jaroslav@15
|
67 |
@Test public void verifyInterfaceMethods() throws Exception {
|
jaroslav@15
|
68 |
assertExec(
|
jaroslav@15
|
69 |
"Retruns default value",
|
jaroslav@22
|
70 |
"org_apidesign_vm4brwsr_Instance_interfaceBytesF",
|
jaroslav@15
|
71 |
Double.valueOf(31)
|
jaroslav@15
|
72 |
);
|
jaroslav@15
|
73 |
}
|
jaroslav@16
|
74 |
|
jaroslav@16
|
75 |
@Test public void isNull() throws Exception {
|
jaroslav@16
|
76 |
assertExec(
|
jaroslav@16
|
77 |
"Yes, we are instance",
|
jaroslav@22
|
78 |
"org_apidesign_vm4brwsr_Instance_isNullZ",
|
jaroslav@16
|
79 |
Double.valueOf(0.0)
|
jaroslav@16
|
80 |
);
|
jaroslav@16
|
81 |
}
|
jaroslav@17
|
82 |
|
jaroslav@16
|
83 |
@Test public void isInstanceOf() throws Exception {
|
jaroslav@16
|
84 |
assertExec(
|
jaroslav@16
|
85 |
"Yes, we are instance",
|
jaroslav@22
|
86 |
"org_apidesign_vm4brwsr_Instance_instanceOfZZ",
|
jaroslav@16
|
87 |
Double.valueOf(1.0), true
|
jaroslav@16
|
88 |
);
|
jaroslav@16
|
89 |
}
|
jaroslav@16
|
90 |
|
jaroslav@16
|
91 |
@Test public void notInstanceOf() throws Exception {
|
jaroslav@16
|
92 |
assertExec(
|
jaroslav@16
|
93 |
"No, we are not an instance",
|
jaroslav@22
|
94 |
"org_apidesign_vm4brwsr_Instance_instanceOfZZ",
|
jaroslav@16
|
95 |
Double.valueOf(0.0), false
|
jaroslav@16
|
96 |
);
|
jaroslav@16
|
97 |
}
|
jaroslav@17
|
98 |
|
jaroslav@30
|
99 |
@Test public void verifyCastToClass() throws Exception {
|
jaroslav@30
|
100 |
assertExec(
|
jaroslav@30
|
101 |
"Five signals all is good",
|
jaroslav@30
|
102 |
"org_apidesign_vm4brwsr_Instance_castsWorkIZ",
|
jaroslav@30
|
103 |
Double.valueOf(5.0), false
|
jaroslav@30
|
104 |
);
|
jaroslav@30
|
105 |
}
|
jaroslav@30
|
106 |
@Test public void verifyCastToInterface() throws Exception {
|
jaroslav@30
|
107 |
assertExec(
|
jaroslav@30
|
108 |
"Five signals all is good",
|
jaroslav@30
|
109 |
"org_apidesign_vm4brwsr_Instance_castsWorkIZ",
|
jaroslav@30
|
110 |
Double.valueOf(5.0), true
|
jaroslav@30
|
111 |
);
|
jaroslav@30
|
112 |
}
|
jaroslav@30
|
113 |
|
jaroslav@98
|
114 |
protected String startCompilationWith() {
|
jaroslav@98
|
115 |
return "org/apidesign/vm4brwsr/Instance";
|
jaroslav@98
|
116 |
}
|
jaroslav@98
|
117 |
|
jaroslav@103
|
118 |
private static CharSequence codeSeq;
|
jaroslav@103
|
119 |
private static Invocable code;
|
jaroslav@103
|
120 |
|
jaroslav@103
|
121 |
@BeforeTest
|
jaroslav@103
|
122 |
public void compileTheCode() throws Exception {
|
jaroslav@103
|
123 |
if (codeSeq == null) {
|
jaroslav@103
|
124 |
StringBuilder sb = new StringBuilder();
|
jaroslav@103
|
125 |
code = StaticMethodTest.compileClass(sb, startCompilationWith());
|
jaroslav@103
|
126 |
codeSeq = sb;
|
jaroslav@103
|
127 |
}
|
jaroslav@103
|
128 |
}
|
jaroslav@103
|
129 |
|
jaroslav@98
|
130 |
private void assertExec(
|
jaroslav@98
|
131 |
String msg, String methodName, Object expRes, Object... args
|
jaroslav@98
|
132 |
) throws Exception {
|
jaroslav@103
|
133 |
|
jaroslav@8
|
134 |
Object ret = null;
|
jaroslav@8
|
135 |
try {
|
jaroslav@103
|
136 |
ret = code.invokeFunction(methodName, args);
|
jaroslav@8
|
137 |
} catch (ScriptException ex) {
|
jaroslav@103
|
138 |
fail("Execution failed in\n" + codeSeq, ex);
|
jaroslav@8
|
139 |
} catch (NoSuchMethodException ex) {
|
jaroslav@103
|
140 |
fail("Cannot find method in\n" + codeSeq, ex);
|
jaroslav@8
|
141 |
}
|
jaroslav@8
|
142 |
if (ret == null && expRes == null) {
|
jaroslav@8
|
143 |
return;
|
jaroslav@8
|
144 |
}
|
jaroslav@8
|
145 |
if (expRes.equals(ret)) {
|
jaroslav@8
|
146 |
return;
|
jaroslav@8
|
147 |
}
|
jaroslav@103
|
148 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
|
jaroslav@8
|
149 |
|
jaroslav@8
|
150 |
}
|
jaroslav@8
|
151 |
|
jaroslav@8
|
152 |
}
|