The compilation to JavaScript now identifies list of external references and provides them back to the caller
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.java4browser;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Arrays;
23 import java.util.Iterator;
25 import java.util.TreeSet;
26 import javax.script.Invocable;
27 import javax.script.ScriptEngine;
28 import javax.script.ScriptEngineManager;
29 import javax.script.ScriptException;
30 import static org.testng.Assert.*;
31 import org.testng.annotations.Test;
33 /** Checks the basic behavior of the translator.
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
37 public class StaticMethodTest {
38 @Test public void threePlusFour() throws Exception {
41 "org_apidesign_java4browser_StaticMethod_sumIII",
47 @Test public void powerOfThree() throws Exception {
50 "org_apidesign_java4browser_StaticMethod_powerFF",
56 @Test public void doubleWithoutLong() throws Exception {
59 "org_apidesign_java4browser_StaticMethod_minusDDJ",
65 @Test public void divAndRound() throws Exception {
67 "Should be rounded to one",
68 "org_apidesign_java4browser_StaticMethod_divIBD",
73 @Test public void mixedMethodFourParams() throws Exception {
76 "org_apidesign_java4browser_StaticMethod_mixIIJBD",
81 @Test public void factRec() throws Exception {
83 "Factorial of 5 is 120",
84 "org_apidesign_java4browser_StaticMethod_factRecJI",
89 @Test public void factIter() throws Exception {
91 "Factorial of 5 is 120",
92 "org_apidesign_java4browser_StaticMethod_factIterJI",
98 @Test public void xor() throws Exception {
101 "org_apidesign_java4browser_StaticMethod_xorJIJ",
108 @Test public void or() throws Exception {
111 "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
118 @Test public void and() throws Exception {
121 "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
128 @Test public void inc4() throws Exception {
131 "org_apidesign_java4browser_StaticMethod_inc4I",
136 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
137 StringBuilder sb = new StringBuilder();
138 Invocable i = compileClass(sb, "org/apidesign/java4browser/StaticMethod");
142 ret = i.invokeFunction(methodName, args);
143 } catch (ScriptException ex) {
144 fail("Execution failed in " + sb, ex);
145 } catch (NoSuchMethodException ex) {
146 fail("Cannot find method in " + sb, ex);
148 if (ret == null && expRes == null) {
151 if (expRes.equals(ret)) {
154 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
158 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
159 for (String name : names) {
160 InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
161 assertNotNull(is, "Class file found");
163 sb = new StringBuilder();
165 ByteCodeToJavaScript.compile(is, sb, null);
167 ScriptEngineManager sem = new ScriptEngineManager();
168 ScriptEngine js = sem.getEngineByExtension("js");
170 Object res = js.eval(sb.toString());
171 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
172 return (Invocable)js;
173 } catch (ScriptException ex) {
174 fail("Could not compile:\n" + sb, ex);