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.HashSet;
24 import java.util.Iterator;
25 import java.util.LinkedList;
27 import java.util.TreeSet;
28 import javax.script.Invocable;
29 import javax.script.ScriptEngine;
30 import javax.script.ScriptEngineManager;
31 import javax.script.ScriptException;
32 import static org.testng.Assert.*;
33 import org.testng.annotations.Test;
35 /** Checks the basic behavior of the translator.
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 public class StaticMethodTest {
40 @Test public void threePlusFour() throws Exception {
43 "org_apidesign_java4browser_StaticMethod_sumIII",
49 @Test public void powerOfThree() throws Exception {
52 "org_apidesign_java4browser_StaticMethod_powerFF",
58 @Test public void doubleWithoutLong() throws Exception {
61 "org_apidesign_java4browser_StaticMethod_minusDDJ",
67 @Test public void divAndRound() throws Exception {
69 "Should be rounded to one",
70 "org_apidesign_java4browser_StaticMethod_divIBD",
75 @Test public void mixedMethodFourParams() throws Exception {
78 "org_apidesign_java4browser_StaticMethod_mixIIJBD",
83 @Test public void factRec() throws Exception {
85 "Factorial of 5 is 120",
86 "org_apidesign_java4browser_StaticMethod_factRecJI",
91 @Test public void factIter() throws Exception {
93 "Factorial of 5 is 120",
94 "org_apidesign_java4browser_StaticMethod_factIterJI",
100 @Test public void xor() throws Exception {
103 "org_apidesign_java4browser_StaticMethod_xorJIJ",
110 @Test public void or() throws Exception {
113 "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
120 @Test public void and() throws Exception {
123 "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
130 @Test public void inc4() throws Exception {
133 "org_apidesign_java4browser_StaticMethod_inc4I",
138 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
139 StringBuilder sb = new StringBuilder();
140 Invocable i = compileClass(sb, "org/apidesign/java4browser/StaticMethod");
144 ret = i.invokeFunction(methodName, args);
145 } catch (ScriptException ex) {
146 fail("Execution failed in " + sb, ex);
147 } catch (NoSuchMethodException ex) {
148 fail("Cannot find method in " + sb, ex);
150 if (ret == null && expRes == null) {
153 if (expRes.equals(ret)) {
156 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
160 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
162 sb = new StringBuilder();
164 Set<String> processed = new HashSet<String>();
166 LinkedList<String> toProcess = new LinkedList<String>(Arrays.asList(names));
168 toProcess.removeAll(processed);
169 if (toProcess.isEmpty()) {
172 String name = toProcess.getFirst();
174 if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
177 InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
178 assertNotNull(is, "Class file found");
180 ByteCodeToJavaScript.compile(is, sb, toProcess);
181 } catch (RuntimeException ex) {
182 int lastBlock = sb.lastIndexOf("{");
183 throw new IllegalStateException(
184 "Error while compiling " + name + "\n" +
185 sb.substring(0, sb.length()),
190 ScriptEngineManager sem = new ScriptEngineManager();
191 ScriptEngine js = sem.getEngineByExtension("js");
193 Object res = js.eval(sb.toString());
194 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
195 return (Invocable)js;
196 } catch (ScriptException ex) {
197 fail("Could not compile:\n" + sb, ex);