jaroslav@0
|
1 |
/*
|
jaroslav@0
|
2 |
Java 4 Browser Bytecode Translator
|
jaroslav@0
|
3 |
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@0
|
4 |
|
jaroslav@0
|
5 |
This program is free software: you can redistribute it and/or modify
|
jaroslav@0
|
6 |
it under the terms of the GNU General Public License as published by
|
jaroslav@0
|
7 |
the Free Software Foundation, version 2 of the License.
|
jaroslav@0
|
8 |
|
jaroslav@0
|
9 |
This program is distributed in the hope that it will be useful,
|
jaroslav@0
|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@0
|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@0
|
12 |
GNU General Public License for more details.
|
jaroslav@0
|
13 |
|
jaroslav@0
|
14 |
You should have received a copy of the GNU General Public License
|
jaroslav@0
|
15 |
along with this program. Look for COPYING file in the top folder.
|
jaroslav@0
|
16 |
If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@0
|
17 |
*/
|
jaroslav@22
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@0
|
19 |
|
jaroslav@0
|
20 |
import java.io.IOException;
|
jaroslav@0
|
21 |
import javax.script.Invocable;
|
jaroslav@0
|
22 |
import javax.script.ScriptEngine;
|
jaroslav@0
|
23 |
import javax.script.ScriptEngineManager;
|
jaroslav@0
|
24 |
import javax.script.ScriptException;
|
jaroslav@0
|
25 |
import static org.testng.Assert.*;
|
jaroslav@0
|
26 |
import org.testng.annotations.Test;
|
jaroslav@0
|
27 |
|
jaroslav@0
|
28 |
/** Checks the basic behavior of the translator.
|
jaroslav@0
|
29 |
*
|
jaroslav@0
|
30 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@0
|
31 |
*/
|
jaroslav@0
|
32 |
public class StaticMethodTest {
|
jaroslav@0
|
33 |
@Test public void threePlusFour() throws Exception {
|
jaroslav@2
|
34 |
assertExec(
|
jaroslav@2
|
35 |
"Should be seven",
|
jaroslav@22
|
36 |
"org_apidesign_vm4brwsr_StaticMethod_sumIII",
|
jaroslav@2
|
37 |
Double.valueOf(7),
|
jaroslav@2
|
38 |
3, 4
|
jaroslav@2
|
39 |
);
|
jaroslav@0
|
40 |
}
|
jaroslav@0
|
41 |
|
jaroslav@1
|
42 |
@Test public void powerOfThree() throws Exception {
|
jaroslav@2
|
43 |
assertExec(
|
jaroslav@2
|
44 |
"Should be nine",
|
jaroslav@22
|
45 |
"org_apidesign_vm4brwsr_StaticMethod_powerFF",
|
jaroslav@2
|
46 |
Double.valueOf(9),
|
jaroslav@2
|
47 |
3.0f
|
jaroslav@2
|
48 |
);
|
jaroslav@1
|
49 |
}
|
jaroslav@1
|
50 |
|
jaroslav@2
|
51 |
@Test public void doubleWithoutLong() throws Exception {
|
jaroslav@2
|
52 |
assertExec(
|
jaroslav@2
|
53 |
"Should be two",
|
jaroslav@22
|
54 |
"org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
|
jaroslav@2
|
55 |
Double.valueOf(2),
|
jaroslav@2
|
56 |
3.0d, 1l
|
jaroslav@2
|
57 |
);
|
jaroslav@2
|
58 |
}
|
jaroslav@3
|
59 |
|
jaroslav@3
|
60 |
@Test public void divAndRound() throws Exception {
|
jaroslav@3
|
61 |
assertExec(
|
jaroslav@3
|
62 |
"Should be rounded to one",
|
jaroslav@22
|
63 |
"org_apidesign_vm4brwsr_StaticMethod_divIBD",
|
jaroslav@3
|
64 |
Double.valueOf(1),
|
jaroslav@3
|
65 |
3, 3.75
|
jaroslav@3
|
66 |
);
|
jaroslav@3
|
67 |
}
|
jaroslav@3
|
68 |
@Test public void mixedMethodFourParams() throws Exception {
|
jaroslav@3
|
69 |
assertExec(
|
jaroslav@3
|
70 |
"Should be two",
|
jaroslav@22
|
71 |
"org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
|
jaroslav@3
|
72 |
Double.valueOf(20),
|
jaroslav@3
|
73 |
2, 10l, 5, 2.0
|
jaroslav@3
|
74 |
);
|
jaroslav@3
|
75 |
}
|
jaroslav@4
|
76 |
@Test public void factRec() throws Exception {
|
jaroslav@4
|
77 |
assertExec(
|
jaroslav@4
|
78 |
"Factorial of 5 is 120",
|
jaroslav@22
|
79 |
"org_apidesign_vm4brwsr_StaticMethod_factRecJI",
|
jaroslav@4
|
80 |
Double.valueOf(120),
|
jaroslav@4
|
81 |
5
|
jaroslav@4
|
82 |
);
|
jaroslav@4
|
83 |
}
|
jaroslav@5
|
84 |
@Test public void factIter() throws Exception {
|
jaroslav@5
|
85 |
assertExec(
|
jaroslav@5
|
86 |
"Factorial of 5 is 120",
|
jaroslav@22
|
87 |
"org_apidesign_vm4brwsr_StaticMethod_factIterJI",
|
jaroslav@5
|
88 |
Double.valueOf(120),
|
jaroslav@5
|
89 |
5
|
jaroslav@5
|
90 |
);
|
jaroslav@5
|
91 |
}
|
jaroslav@2
|
92 |
|
jaroslav@6
|
93 |
@Test public void xor() throws Exception {
|
jaroslav@6
|
94 |
assertExec(
|
jaroslav@6
|
95 |
"Xor is 4",
|
jaroslav@22
|
96 |
"org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
|
jaroslav@6
|
97 |
Double.valueOf(4),
|
jaroslav@6
|
98 |
7,
|
jaroslav@6
|
99 |
3
|
jaroslav@6
|
100 |
);
|
jaroslav@6
|
101 |
}
|
jaroslav@6
|
102 |
|
jaroslav@7
|
103 |
@Test public void or() throws Exception {
|
jaroslav@7
|
104 |
assertExec(
|
jaroslav@7
|
105 |
"Or will be 7",
|
jaroslav@22
|
106 |
"org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
|
jaroslav@7
|
107 |
Double.valueOf(7),
|
jaroslav@7
|
108 |
true,
|
jaroslav@7
|
109 |
4,
|
jaroslav@7
|
110 |
3
|
jaroslav@7
|
111 |
);
|
jaroslav@7
|
112 |
}
|
jaroslav@7
|
113 |
@Test public void and() throws Exception {
|
jaroslav@7
|
114 |
assertExec(
|
jaroslav@7
|
115 |
"And will be 3",
|
jaroslav@22
|
116 |
"org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
|
jaroslav@7
|
117 |
Double.valueOf(3),
|
jaroslav@7
|
118 |
false,
|
jaroslav@7
|
119 |
7,
|
jaroslav@7
|
120 |
3
|
jaroslav@7
|
121 |
);
|
jaroslav@7
|
122 |
}
|
jaroslav@9
|
123 |
@Test public void inc4() throws Exception {
|
jaroslav@9
|
124 |
assertExec(
|
jaroslav@9
|
125 |
"It will be 4",
|
jaroslav@22
|
126 |
"org_apidesign_vm4brwsr_StaticMethod_inc4I",
|
jaroslav@9
|
127 |
Double.valueOf(4)
|
jaroslav@9
|
128 |
);
|
jaroslav@9
|
129 |
}
|
jaroslav@7
|
130 |
|
jaroslav@2
|
131 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@2
|
132 |
StringBuilder sb = new StringBuilder();
|
jaroslav@22
|
133 |
Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
|
jaroslav@2
|
134 |
|
jaroslav@2
|
135 |
Object ret = null;
|
jaroslav@2
|
136 |
try {
|
jaroslav@2
|
137 |
ret = i.invokeFunction(methodName, args);
|
jaroslav@5
|
138 |
} catch (ScriptException ex) {
|
jaroslav@5
|
139 |
fail("Execution failed in " + sb, ex);
|
jaroslav@2
|
140 |
} catch (NoSuchMethodException ex) {
|
jaroslav@2
|
141 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@2
|
142 |
}
|
jaroslav@2
|
143 |
if (ret == null && expRes == null) {
|
jaroslav@2
|
144 |
return;
|
jaroslav@2
|
145 |
}
|
jaroslav@2
|
146 |
if (expRes.equals(ret)) {
|
jaroslav@2
|
147 |
return;
|
jaroslav@2
|
148 |
}
|
jaroslav@3
|
149 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
|
jaroslav@2
|
150 |
|
jaroslav@2
|
151 |
}
|
jaroslav@2
|
152 |
|
jaroslav@13
|
153 |
static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
|
jaroslav@19
|
154 |
if (sb == null) {
|
jaroslav@19
|
155 |
sb = new StringBuilder();
|
jaroslav@19
|
156 |
}
|
jaroslav@29
|
157 |
GenJS.compile(sb, names);
|
jaroslav@0
|
158 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@0
|
159 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@0
|
160 |
try {
|
jaroslav@0
|
161 |
Object res = js.eval(sb.toString());
|
jaroslav@0
|
162 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@0
|
163 |
return (Invocable)js;
|
jaroslav@0
|
164 |
} catch (ScriptException ex) {
|
jaroslav@0
|
165 |
fail("Could not compile:\n" + sb, ex);
|
jaroslav@0
|
166 |
return null;
|
jaroslav@0
|
167 |
}
|
jaroslav@0
|
168 |
}
|
jaroslav@0
|
169 |
}
|