2 Java 4 Browser Bytecode Translator
3 Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. Look for COPYING file in the top folder.
16 If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import javax.script.Invocable;
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptEngineManager;
24 import javax.script.ScriptException;
25 import static org.testng.Assert.*;
26 import org.testng.annotations.BeforeClass;
27 import org.testng.annotations.Test;
29 /** Checks the basic behavior of the translator.
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 public class StaticMethodTest {
34 @Test public void threePlusFour() throws Exception {
37 "org_apidesign_vm4brwsr_StaticMethod_sumIII",
43 @Test public void powerOfThree() throws Exception {
46 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
52 @Test public void minusOne() throws Exception {
54 "Should be minus one",
55 "org_apidesign_vm4brwsr_StaticMethod_minusOneI",
60 @Test public void doubleWithoutLong() throws Exception {
63 "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
69 @Test public void divAndRound() throws Exception {
71 "Should be rounded to one",
72 "org_apidesign_vm4brwsr_StaticMethod_divIBD",
77 @Test public void mixedMethodFourParams() throws Exception {
80 "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
85 @Test public void factRec() throws Exception {
87 "Factorial of 5 is 120",
88 "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
93 @Test public void factIter() throws Exception {
95 "Factorial of 5 is 120",
96 "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
102 @Test public void xor() throws Exception {
105 "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
112 @Test public void or() throws Exception {
115 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
122 @Test public void nullCheck() throws Exception {
125 "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
129 @Test public void and() throws Exception {
132 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
139 @Test public void inc4() throws Exception {
142 "org_apidesign_vm4brwsr_StaticMethod_inc4I",
147 @Test public void shiftLeftInJava() throws Exception {
148 int res = StaticMethod.shiftLeft(1, 8);
149 assertEquals(res, 256);
152 @Test public void shiftLeftInJS() throws Exception {
155 "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
161 @Test public void shiftRightInJava() throws Exception {
162 int res = StaticMethod.shiftArithmRight(-8, 3, true);
163 assertEquals(res, -1);
166 @Test public void shiftRightInJS() throws Exception {
169 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
174 @Test public void unsignedShiftRightInJava() throws Exception {
175 int res = StaticMethod.shiftArithmRight(8, 3, false);
176 assertEquals(res, 1);
179 @Test public void unsignedShiftRightInJS() throws Exception {
182 "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
188 @Test public void javaScriptBody() throws Exception {
191 "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
197 private static CharSequence codeSeq;
198 private static Invocable code;
201 public void compileTheCode() throws Exception {
202 StringBuilder sb = new StringBuilder();
203 code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
208 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
209 StringBuilder sb = new StringBuilder();
213 ret = code.invokeFunction(methodName, args);
214 } catch (ScriptException ex) {
215 fail("Execution failed in\n" + codeSeq, ex);
216 } catch (NoSuchMethodException ex) {
217 fail("Cannot find method in\n" + codeSeq, ex);
219 if (ret == null && expRes == null) {
222 if (expRes != null && expRes.equals(ret)) {
225 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
229 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
231 sb = new StringBuilder();
233 GenJS.compile(sb, names);
234 ScriptEngineManager sem = new ScriptEngineManager();
235 ScriptEngine js = sem.getEngineByExtension("js");
237 Object res = js.eval(sb.toString());
238 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
239 return (Invocable)js;
240 } catch (ScriptException ex) {
241 fail("Could not compile:\n" + sb, ex);