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.Test;
28 /** Checks the basic behavior of the translator.
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 public class StaticMethodTest {
33 @Test public void threePlusFour() throws Exception {
36 "org_apidesign_vm4brwsr_StaticMethod_sumIII",
42 @Test public void powerOfThree() throws Exception {
45 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
51 @Test public void minusOne() throws Exception {
53 "Should be minus one",
54 "org_apidesign_vm4brwsr_StaticMethod_minusOneI",
59 @Test public void doubleWithoutLong() throws Exception {
62 "org_apidesign_vm4brwsr_StaticMethod_minusDDJ",
68 @Test public void divAndRound() throws Exception {
70 "Should be rounded to one",
71 "org_apidesign_vm4brwsr_StaticMethod_divIBD",
76 @Test public void mixedMethodFourParams() throws Exception {
79 "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD",
84 @Test public void factRec() throws Exception {
86 "Factorial of 5 is 120",
87 "org_apidesign_vm4brwsr_StaticMethod_factRecJI",
92 @Test public void factIter() throws Exception {
94 "Factorial of 5 is 120",
95 "org_apidesign_vm4brwsr_StaticMethod_factIterJI",
101 @Test public void xor() throws Exception {
104 "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
111 @Test public void or() throws Exception {
114 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
121 @Test public void nullCheck() throws Exception {
124 "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
128 @Test public void and() throws Exception {
131 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
138 @Test public void inc4() throws Exception {
141 "org_apidesign_vm4brwsr_StaticMethod_inc4I",
146 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
147 StringBuilder sb = new StringBuilder();
148 Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
152 ret = i.invokeFunction(methodName, args);
153 } catch (ScriptException ex) {
154 fail("Execution failed in " + sb, ex);
155 } catch (NoSuchMethodException ex) {
156 fail("Cannot find method in " + sb, ex);
158 if (ret == null && expRes == null) {
161 if (expRes != null && expRes.equals(ret)) {
164 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
168 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
170 sb = new StringBuilder();
172 GenJS.compile(sb, names);
173 ScriptEngineManager sem = new ScriptEngineManager();
174 ScriptEngine js = sem.getEngineByExtension("js");
176 Object res = js.eval(sb.toString());
177 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
178 return (Invocable)js;
179 } catch (ScriptException ex) {
180 fail("Could not compile:\n" + sb, ex);