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@0
|
18 |
package org.apidesign.java4browser;
|
jaroslav@0
|
19 |
|
jaroslav@0
|
20 |
import java.io.IOException;
|
jaroslav@0
|
21 |
import java.io.InputStream;
|
jaroslav@0
|
22 |
import javax.script.Invocable;
|
jaroslav@0
|
23 |
import javax.script.ScriptEngine;
|
jaroslav@0
|
24 |
import javax.script.ScriptEngineManager;
|
jaroslav@0
|
25 |
import javax.script.ScriptException;
|
jaroslav@0
|
26 |
import static org.testng.Assert.*;
|
jaroslav@0
|
27 |
import org.testng.annotations.Test;
|
jaroslav@0
|
28 |
|
jaroslav@0
|
29 |
/** Checks the basic behavior of the translator.
|
jaroslav@0
|
30 |
*
|
jaroslav@0
|
31 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@0
|
32 |
*/
|
jaroslav@0
|
33 |
public class StaticMethodTest {
|
jaroslav@0
|
34 |
@Test public void threePlusFour() throws Exception {
|
jaroslav@2
|
35 |
assertExec(
|
jaroslav@2
|
36 |
"Should be seven",
|
jaroslav@2
|
37 |
"org_apidesign_java4browser_StaticMethod_sumIII",
|
jaroslav@2
|
38 |
Double.valueOf(7),
|
jaroslav@2
|
39 |
3, 4
|
jaroslav@2
|
40 |
);
|
jaroslav@0
|
41 |
}
|
jaroslav@0
|
42 |
|
jaroslav@1
|
43 |
@Test public void powerOfThree() throws Exception {
|
jaroslav@2
|
44 |
assertExec(
|
jaroslav@2
|
45 |
"Should be nine",
|
jaroslav@2
|
46 |
"org_apidesign_java4browser_StaticMethod_powerFF",
|
jaroslav@2
|
47 |
Double.valueOf(9),
|
jaroslav@2
|
48 |
3.0f
|
jaroslav@2
|
49 |
);
|
jaroslav@1
|
50 |
}
|
jaroslav@1
|
51 |
|
jaroslav@2
|
52 |
@Test public void doubleWithoutLong() throws Exception {
|
jaroslav@2
|
53 |
assertExec(
|
jaroslav@2
|
54 |
"Should be two",
|
jaroslav@2
|
55 |
"org_apidesign_java4browser_StaticMethod_minusDDJ",
|
jaroslav@2
|
56 |
Double.valueOf(2),
|
jaroslav@2
|
57 |
3.0d, 1l
|
jaroslav@2
|
58 |
);
|
jaroslav@2
|
59 |
}
|
jaroslav@3
|
60 |
|
jaroslav@3
|
61 |
@Test public void divAndRound() throws Exception {
|
jaroslav@3
|
62 |
assertExec(
|
jaroslav@3
|
63 |
"Should be rounded to one",
|
jaroslav@3
|
64 |
"org_apidesign_java4browser_StaticMethod_divIBD",
|
jaroslav@3
|
65 |
Double.valueOf(1),
|
jaroslav@3
|
66 |
3, 3.75
|
jaroslav@3
|
67 |
);
|
jaroslav@3
|
68 |
}
|
jaroslav@3
|
69 |
@Test public void mixedMethodFourParams() throws Exception {
|
jaroslav@3
|
70 |
assertExec(
|
jaroslav@3
|
71 |
"Should be two",
|
jaroslav@3
|
72 |
"org_apidesign_java4browser_StaticMethod_mixIIJBD",
|
jaroslav@3
|
73 |
Double.valueOf(20),
|
jaroslav@3
|
74 |
2, 10l, 5, 2.0
|
jaroslav@3
|
75 |
);
|
jaroslav@3
|
76 |
}
|
jaroslav@4
|
77 |
@Test public void factRec() throws Exception {
|
jaroslav@4
|
78 |
assertExec(
|
jaroslav@4
|
79 |
"Factorial of 5 is 120",
|
jaroslav@4
|
80 |
"org_apidesign_java4browser_StaticMethod_factRecJI",
|
jaroslav@4
|
81 |
Double.valueOf(120),
|
jaroslav@4
|
82 |
5
|
jaroslav@4
|
83 |
);
|
jaroslav@4
|
84 |
}
|
jaroslav@5
|
85 |
@Test public void factIter() throws Exception {
|
jaroslav@5
|
86 |
assertExec(
|
jaroslav@5
|
87 |
"Factorial of 5 is 120",
|
jaroslav@5
|
88 |
"org_apidesign_java4browser_StaticMethod_factIterJI",
|
jaroslav@5
|
89 |
Double.valueOf(120),
|
jaroslav@5
|
90 |
5
|
jaroslav@5
|
91 |
);
|
jaroslav@5
|
92 |
}
|
jaroslav@2
|
93 |
|
jaroslav@6
|
94 |
@Test public void xor() throws Exception {
|
jaroslav@6
|
95 |
assertExec(
|
jaroslav@6
|
96 |
"Xor is 4",
|
jaroslav@6
|
97 |
"org_apidesign_java4browser_StaticMethod_xorJIJ",
|
jaroslav@6
|
98 |
Double.valueOf(4),
|
jaroslav@6
|
99 |
7,
|
jaroslav@6
|
100 |
3
|
jaroslav@6
|
101 |
);
|
jaroslav@6
|
102 |
}
|
jaroslav@6
|
103 |
|
jaroslav@7
|
104 |
@Test public void or() throws Exception {
|
jaroslav@7
|
105 |
assertExec(
|
jaroslav@7
|
106 |
"Or will be 7",
|
jaroslav@7
|
107 |
"org_apidesign_java4browser_StaticMethod_orOrAndJZII",
|
jaroslav@7
|
108 |
Double.valueOf(7),
|
jaroslav@7
|
109 |
true,
|
jaroslav@7
|
110 |
4,
|
jaroslav@7
|
111 |
3
|
jaroslav@7
|
112 |
);
|
jaroslav@7
|
113 |
}
|
jaroslav@7
|
114 |
@Test public void and() throws Exception {
|
jaroslav@7
|
115 |
assertExec(
|
jaroslav@7
|
116 |
"And will be 3",
|
jaroslav@7
|
117 |
"org_apidesign_java4browser_StaticMethod_orOrAndJZII",
|
jaroslav@7
|
118 |
Double.valueOf(3),
|
jaroslav@7
|
119 |
false,
|
jaroslav@7
|
120 |
7,
|
jaroslav@7
|
121 |
3
|
jaroslav@7
|
122 |
);
|
jaroslav@7
|
123 |
}
|
jaroslav@9
|
124 |
@Test public void inc4() throws Exception {
|
jaroslav@9
|
125 |
assertExec(
|
jaroslav@9
|
126 |
"It will be 4",
|
jaroslav@9
|
127 |
"org_apidesign_java4browser_StaticMethod_inc4I",
|
jaroslav@9
|
128 |
Double.valueOf(4)
|
jaroslav@9
|
129 |
);
|
jaroslav@9
|
130 |
}
|
jaroslav@7
|
131 |
|
jaroslav@2
|
132 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@2
|
133 |
StringBuilder sb = new StringBuilder();
|
jaroslav@2
|
134 |
Invocable i = compileClass("StaticMethod.class", sb);
|
jaroslav@2
|
135 |
|
jaroslav@2
|
136 |
Object ret = null;
|
jaroslav@2
|
137 |
try {
|
jaroslav@2
|
138 |
ret = i.invokeFunction(methodName, args);
|
jaroslav@5
|
139 |
} catch (ScriptException ex) {
|
jaroslav@5
|
140 |
fail("Execution failed in " + sb, ex);
|
jaroslav@2
|
141 |
} catch (NoSuchMethodException ex) {
|
jaroslav@2
|
142 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@2
|
143 |
}
|
jaroslav@2
|
144 |
if (ret == null && expRes == null) {
|
jaroslav@2
|
145 |
return;
|
jaroslav@2
|
146 |
}
|
jaroslav@2
|
147 |
if (expRes.equals(ret)) {
|
jaroslav@2
|
148 |
return;
|
jaroslav@2
|
149 |
}
|
jaroslav@3
|
150 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
|
jaroslav@2
|
151 |
|
jaroslav@2
|
152 |
}
|
jaroslav@2
|
153 |
|
jaroslav@2
|
154 |
static Invocable compileClass(String name, StringBuilder sb) throws ScriptException, IOException {
|
jaroslav@0
|
155 |
InputStream is = StaticMethodTest.class.getResourceAsStream(name);
|
jaroslav@0
|
156 |
assertNotNull(is, "Class file found");
|
jaroslav@2
|
157 |
if (sb == null) {
|
jaroslav@2
|
158 |
sb = new StringBuilder();
|
jaroslav@2
|
159 |
}
|
jaroslav@0
|
160 |
ByteCodeToJavaScript.compile(name, is, sb);
|
jaroslav@0
|
161 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@0
|
162 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@0
|
163 |
try {
|
jaroslav@0
|
164 |
Object res = js.eval(sb.toString());
|
jaroslav@0
|
165 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@0
|
166 |
return (Invocable)js;
|
jaroslav@0
|
167 |
} catch (ScriptException ex) {
|
jaroslav@0
|
168 |
fail("Could not compile:\n" + sb, ex);
|
jaroslav@0
|
169 |
return null;
|
jaroslav@0
|
170 |
}
|
jaroslav@0
|
171 |
}
|
jaroslav@0
|
172 |
}
|