jaroslav@24
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1787
|
3 |
* Copyright (C) 2012-2015 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@1633
|
20 |
import javax.script.ScriptEngine;
|
jaroslav@1633
|
21 |
import javax.script.ScriptEngineManager;
|
jaroslav@1633
|
22 |
import static org.testng.Assert.fail;
|
jaroslav@789
|
23 |
import org.testng.annotations.AfterClass;
|
jaroslav@1633
|
24 |
import org.testng.annotations.BeforeClass;
|
jaroslav@8
|
25 |
import org.testng.annotations.Test;
|
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@248
|
35 |
Instance.class, "defaultDblValue__D",
|
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@248
|
42 |
InstanceSub.class, "recallDbl__D",
|
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@248
|
49 |
Instance.class, "assignedByteValue__B",
|
jaroslav@10
|
50 |
Double.valueOf(31)
|
jaroslav@10
|
51 |
);
|
jaroslav@10
|
52 |
}
|
jaroslav@1632
|
53 |
@Test public void noInstOfExposed() throws Exception {
|
jaroslav@1632
|
54 |
assertExec(
|
jaroslav@1632
|
55 |
"No instOf properties found",
|
jaroslav@1632
|
56 |
Instance.class, "noInstOfExposed__I",
|
jaroslav@1632
|
57 |
Double.valueOf(0)
|
jaroslav@1632
|
58 |
);
|
jaroslav@1632
|
59 |
}
|
jaroslav@1633
|
60 |
@Test public void noIterablePropsOnObject() throws Exception {
|
jaroslav@1633
|
61 |
assertExec(
|
jaroslav@1633
|
62 |
"No instOf properties found",
|
jaroslav@1633
|
63 |
Instance.class, "props__Ljava_lang_String_2",
|
jaroslav@1633
|
64 |
""
|
jaroslav@1633
|
65 |
);
|
jaroslav@1633
|
66 |
}
|
jaroslav@1633
|
67 |
|
jaroslav@8
|
68 |
@Test public void verifyMagicOne() throws Exception {
|
jaroslav@8
|
69 |
assertExec(
|
jaroslav@10
|
70 |
"Should be three and something",
|
jaroslav@248
|
71 |
Instance.class, "magicOne__D",
|
jaroslav@8
|
72 |
Double.valueOf(3.3)
|
jaroslav@8
|
73 |
);
|
jaroslav@8
|
74 |
}
|
jaroslav@12
|
75 |
@Test public void verifyInstanceMethods() throws Exception {
|
jaroslav@12
|
76 |
assertExec(
|
jaroslav@14
|
77 |
"Should be eleven as we invoke overwritten method, plus 44",
|
jaroslav@248
|
78 |
Instance.class, "virtualBytes__I",
|
jaroslav@14
|
79 |
Double.valueOf(55)
|
jaroslav@12
|
80 |
);
|
jaroslav@12
|
81 |
}
|
jaroslav@15
|
82 |
@Test public void verifyInterfaceMethods() throws Exception {
|
jaroslav@15
|
83 |
assertExec(
|
jaroslav@15
|
84 |
"Retruns default value",
|
jaroslav@248
|
85 |
Instance.class, "interfaceBytes__F",
|
jaroslav@15
|
86 |
Double.valueOf(31)
|
jaroslav@15
|
87 |
);
|
jaroslav@15
|
88 |
}
|
jaroslav@16
|
89 |
|
jaroslav@16
|
90 |
@Test public void isNull() throws Exception {
|
jaroslav@16
|
91 |
assertExec(
|
jaroslav@16
|
92 |
"Yes, we are instance",
|
jaroslav@248
|
93 |
Instance.class, "isNull__Z",
|
jaroslav@16
|
94 |
Double.valueOf(0.0)
|
jaroslav@16
|
95 |
);
|
jaroslav@16
|
96 |
}
|
jaroslav@17
|
97 |
|
jaroslav@16
|
98 |
@Test public void isInstanceOf() throws Exception {
|
jaroslav@16
|
99 |
assertExec(
|
jaroslav@16
|
100 |
"Yes, we are instance",
|
jaroslav@639
|
101 |
Instance.class, "instanceOf__ZI",
|
jaroslav@639
|
102 |
Double.valueOf(1.0), 2
|
jaroslav@16
|
103 |
);
|
jaroslav@16
|
104 |
}
|
jaroslav@16
|
105 |
|
jaroslav@16
|
106 |
@Test public void notInstanceOf() throws Exception {
|
jaroslav@16
|
107 |
assertExec(
|
jaroslav@16
|
108 |
"No, we are not an instance",
|
jaroslav@639
|
109 |
Instance.class, "instanceOf__ZI",
|
jaroslav@639
|
110 |
Double.valueOf(0.0), 1
|
jaroslav@639
|
111 |
);
|
jaroslav@639
|
112 |
}
|
jaroslav@639
|
113 |
@Test public void nullInstanceOf() throws Exception {
|
jaroslav@639
|
114 |
assertExec(
|
jaroslav@639
|
115 |
"No, null is not an instance",
|
jaroslav@639
|
116 |
Instance.class, "instanceOf__ZI",
|
jaroslav@639
|
117 |
Double.valueOf(0.0), 0
|
jaroslav@16
|
118 |
);
|
jaroslav@16
|
119 |
}
|
jaroslav@17
|
120 |
|
jaroslav@30
|
121 |
@Test public void verifyCastToClass() throws Exception {
|
jaroslav@30
|
122 |
assertExec(
|
jaroslav@30
|
123 |
"Five signals all is good",
|
jaroslav@248
|
124 |
Instance.class, "castsWork__IZ",
|
jaroslav@30
|
125 |
Double.valueOf(5.0), false
|
jaroslav@30
|
126 |
);
|
jaroslav@30
|
127 |
}
|
jaroslav@30
|
128 |
@Test public void verifyCastToInterface() throws Exception {
|
jaroslav@30
|
129 |
assertExec(
|
jaroslav@30
|
130 |
"Five signals all is good",
|
jaroslav@248
|
131 |
Instance.class, "castsWork__IZ",
|
jaroslav@30
|
132 |
Double.valueOf(5.0), true
|
jaroslav@30
|
133 |
);
|
jaroslav@30
|
134 |
}
|
jaroslav@30
|
135 |
|
jaroslav@226
|
136 |
@Test public void sharedConstructor() throws Exception {
|
jaroslav@226
|
137 |
assertExec(
|
jaroslav@226
|
138 |
"Constructor of first and 2nd instance should be the same",
|
jaroslav@248
|
139 |
Instance.class, "sharedConstructor__Z",
|
jaroslav@226
|
140 |
Double.valueOf(1.0)
|
jaroslav@226
|
141 |
);
|
jaroslav@226
|
142 |
}
|
jaroslav@226
|
143 |
|
jaroslav@226
|
144 |
@Test public void differentConstructor() throws Exception {
|
jaroslav@226
|
145 |
assertExec(
|
jaroslav@226
|
146 |
"Constructor of X and Y should be the different",
|
jaroslav@248
|
147 |
Instance.class, "differentConstructor__Z",
|
jaroslav@226
|
148 |
Double.valueOf(0)
|
jaroslav@226
|
149 |
);
|
jaroslav@226
|
150 |
}
|
jaroslav@239
|
151 |
|
jaroslav@239
|
152 |
@Test public void jsObjectIsLikeJavaObject() throws Exception {
|
jaroslav@239
|
153 |
assertExec(
|
jaroslav@239
|
154 |
"JavaScript object is instance of Java Object",
|
jaroslav@248
|
155 |
Instance.class, "iofObject__Z",
|
jaroslav@239
|
156 |
Double.valueOf(1)
|
jaroslav@239
|
157 |
);
|
jaroslav@239
|
158 |
}
|
jaroslav@442
|
159 |
|
jaroslav@442
|
160 |
@Test public void jsCallingConvention() throws Exception {
|
jaroslav@442
|
161 |
assertExec(
|
jaroslav@442
|
162 |
"Pointer to 'this' is passed automatically (and not as a first argument)",
|
jaroslav@442
|
163 |
Instance.class, "jscall__I",
|
jaroslav@442
|
164 |
Double.valueOf(31)
|
jaroslav@442
|
165 |
);
|
jaroslav@442
|
166 |
}
|
jaroslav@226
|
167 |
|
jaroslav@98
|
168 |
protected String startCompilationWith() {
|
jaroslav@98
|
169 |
return "org/apidesign/vm4brwsr/Instance";
|
jaroslav@98
|
170 |
}
|
jaroslav@98
|
171 |
|
jaroslav@708
|
172 |
private static TestVM code;
|
jaroslav@103
|
173 |
|
jaroslav@113
|
174 |
@BeforeClass
|
jaroslav@103
|
175 |
public void compileTheCode() throws Exception {
|
jaroslav@708
|
176 |
code = TestVM.compileClass(startCompilationWith());
|
jaroslav@103
|
177 |
}
|
jaroslav@789
|
178 |
@AfterClass
|
jaroslav@789
|
179 |
public static void releaseTheCode() {
|
jaroslav@789
|
180 |
code = null;
|
jaroslav@789
|
181 |
}
|
jaroslav@103
|
182 |
|
jaroslav@98
|
183 |
private void assertExec(
|
jaroslav@203
|
184 |
String msg, Class clazz, String method, Object expRes, Object... args
|
jaroslav@98
|
185 |
) throws Exception {
|
jaroslav@708
|
186 |
code.assertExec(msg, clazz, method, expRes, args);
|
jaroslav@8
|
187 |
}
|
jaroslav@8
|
188 |
|
jaroslav@8
|
189 |
}
|