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@2
|
94 |
private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
|
jaroslav@2
|
95 |
StringBuilder sb = new StringBuilder();
|
jaroslav@2
|
96 |
Invocable i = compileClass("StaticMethod.class", sb);
|
jaroslav@2
|
97 |
|
jaroslav@2
|
98 |
Object ret = null;
|
jaroslav@2
|
99 |
try {
|
jaroslav@2
|
100 |
ret = i.invokeFunction(methodName, args);
|
jaroslav@5
|
101 |
} catch (ScriptException ex) {
|
jaroslav@5
|
102 |
fail("Execution failed in " + sb, ex);
|
jaroslav@2
|
103 |
} catch (NoSuchMethodException ex) {
|
jaroslav@2
|
104 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@2
|
105 |
}
|
jaroslav@2
|
106 |
if (ret == null && expRes == null) {
|
jaroslav@2
|
107 |
return;
|
jaroslav@2
|
108 |
}
|
jaroslav@2
|
109 |
if (expRes.equals(ret)) {
|
jaroslav@2
|
110 |
return;
|
jaroslav@2
|
111 |
}
|
jaroslav@3
|
112 |
assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
|
jaroslav@2
|
113 |
|
jaroslav@2
|
114 |
}
|
jaroslav@2
|
115 |
|
jaroslav@2
|
116 |
static Invocable compileClass(String name, StringBuilder sb) throws ScriptException, IOException {
|
jaroslav@0
|
117 |
InputStream is = StaticMethodTest.class.getResourceAsStream(name);
|
jaroslav@0
|
118 |
assertNotNull(is, "Class file found");
|
jaroslav@2
|
119 |
if (sb == null) {
|
jaroslav@2
|
120 |
sb = new StringBuilder();
|
jaroslav@2
|
121 |
}
|
jaroslav@0
|
122 |
ByteCodeToJavaScript.compile(name, is, sb);
|
jaroslav@0
|
123 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@0
|
124 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@0
|
125 |
try {
|
jaroslav@0
|
126 |
Object res = js.eval(sb.toString());
|
jaroslav@0
|
127 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@0
|
128 |
return (Invocable)js;
|
jaroslav@0
|
129 |
} catch (ScriptException ex) {
|
jaroslav@0
|
130 |
fail("Could not compile:\n" + sb, ex);
|
jaroslav@0
|
131 |
return null;
|
jaroslav@0
|
132 |
}
|
jaroslav@0
|
133 |
}
|
jaroslav@0
|
134 |
}
|