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 org.apidesign.vm4brwsr.ByteCodeToJavaScript;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.LinkedList;
28 import java.util.TreeSet;
29 import javax.script.Invocable;
30 import javax.script.ScriptEngine;
31 import javax.script.ScriptEngineManager;
32 import javax.script.ScriptException;
33 import static org.testng.Assert.*;
34 import org.testng.annotations.Test;
36 /** Checks the basic behavior of the translator.
38 * @author Jaroslav Tulach <jtulach@netbeans.org>
40 public class StaticMethodTest {
41 @Test public void threePlusFour() throws Exception {
44 "org_apidesign_vm4brwsr_StaticMethod_sumIII",
50 @Test public void powerOfThree() throws Exception {
53 "org_apidesign_vm4brwsr_StaticMethod_powerFF",
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 and() throws Exception {
124 "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
131 @Test public void inc4() throws Exception {
134 "org_apidesign_vm4brwsr_StaticMethod_inc4I",
139 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
140 StringBuilder sb = new StringBuilder();
141 Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
145 ret = i.invokeFunction(methodName, args);
146 } catch (ScriptException ex) {
147 fail("Execution failed in " + sb, ex);
148 } catch (NoSuchMethodException ex) {
149 fail("Cannot find method in " + sb, ex);
151 if (ret == null && expRes == null) {
154 if (expRes.equals(ret)) {
157 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
161 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
163 sb = new StringBuilder();
165 Set<String> processed = new HashSet<String>();
167 LinkedList<String> toProcess = new LinkedList<String>(Arrays.asList(names));
169 toProcess.removeAll(processed);
170 if (toProcess.isEmpty()) {
173 String name = toProcess.getFirst();
175 if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
178 InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
179 assertNotNull(is, "Class file found");
181 ByteCodeToJavaScript.compile(is, sb, toProcess);
182 } catch (RuntimeException ex) {
183 int lastBlock = sb.lastIndexOf("{");
184 throw new IllegalStateException(
185 "Error while compiling " + name + "\n" +
186 sb.substring(0, sb.length()),
191 ScriptEngineManager sem = new ScriptEngineManager();
192 ScriptEngine js = sem.getEngineByExtension("js");
194 Object res = js.eval(sb.toString());
195 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
196 return (Invocable)js;
197 } catch (ScriptException ex) {
198 fail("Could not compile:\n" + sb, ex);