Hiding all generated class methods from sight of external users. Exposing only bck2brwsr entry point function
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 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;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import javax.script.Invocable;
24 import javax.script.ScriptEngine;
25 import javax.script.ScriptEngineManager;
26 import javax.script.ScriptException;
27 import static org.testng.Assert.*;
28 import org.testng.annotations.BeforeClass;
29 import org.testng.annotations.Test;
31 /** Checks the basic behavior of the translator.
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 public class StaticMethodTest {
36 @Test public void threePlusFour() throws Exception {
39 StaticMethod.class, "sum__III",
45 @Test public void checkReallyInitializedValues() throws Exception {
48 StaticMethod.class, "isNull__Z",
53 @Test public void powerOfThree() throws Exception {
56 StaticMethod.class, "power__FF",
62 @Test public void minusOne() throws Exception {
64 "Should be minus one",
65 StaticMethod.class, "minusOne__I",
70 @Test public void doubleWithoutLong() throws Exception {
73 StaticMethod.class, "minus__DDJ",
79 @Test public void divAndRound() throws Exception {
81 "Should be rounded to one",
82 StaticMethod.class, "div__IBD",
87 @Test public void mixedMethodFourParams() throws Exception {
90 StaticMethod.class, "mix__IIJBD",
95 @Test public void factRec() throws Exception {
97 "Factorial of 5 is 120",
98 StaticMethod.class, "factRec__JI",
103 @Test public void factIter() throws Exception {
105 "Factorial of 5 is 120",
106 StaticMethod.class, "factIter__JI",
112 @Test public void xor() throws Exception {
115 StaticMethod.class, "xor__JIJ",
122 @Test public void or() throws Exception {
125 StaticMethod.class, "orOrAnd__JZII",
132 @Test public void nullCheck() throws Exception {
135 StaticMethod.class, "none__Ljava_lang_Object_2II",
139 @Test public void and() throws Exception {
142 StaticMethod.class, "orOrAnd__JZII",
149 @Test public void inc4() throws Exception {
152 StaticMethod.class, "inc4__I",
157 @Test public void shiftLeftInJava() throws Exception {
158 int res = StaticMethod.shiftLeft(1, 8);
159 assertEquals(res, 256);
162 @Test public void shiftLeftInJS() throws Exception {
165 StaticMethod.class, "shiftLeft__III",
171 @Test public void shiftRightInJava() throws Exception {
172 int res = StaticMethod.shiftArithmRight(-8, 3, true);
173 assertEquals(res, -1);
176 @Test public void shiftRightInJS() throws Exception {
179 StaticMethod.class, "shiftArithmRight__IIIZ",
184 @Test public void unsignedShiftRightInJava() throws Exception {
185 int res = StaticMethod.shiftArithmRight(8, 3, false);
186 assertEquals(res, 1);
189 @Test public void unsignedShiftRightInJS() throws Exception {
192 StaticMethod.class, "shiftArithmRight__IIIZ",
198 @Test public void javaScriptBody() throws Exception {
201 StaticMethod.class, "i2s__Ljava_lang_String_2II",
207 @Test public void switchJarda() throws Exception {
209 "The expected value",
210 StaticMethod.class, "swtch__Ljava_lang_String_2I",
216 @Test public void switchDarda() throws Exception {
218 "The expected value",
219 StaticMethod.class, "swtch__Ljava_lang_String_2I",
224 @Test public void switchParda() throws Exception {
226 "The expected value",
227 StaticMethod.class, "swtch2__Ljava_lang_String_2I",
232 @Test public void switchMarda() throws Exception {
234 "The expected value",
235 StaticMethod.class, "swtch__Ljava_lang_String_2I",
241 private static CharSequence codeSeq;
242 private static Invocable code;
245 public void compileTheCode() throws Exception {
246 StringBuilder sb = new StringBuilder();
247 code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
252 private static void assertExec(
253 String msg, Class clazz, String method,
254 Object expRes, Object... args
256 assertExec(code, codeSeq, msg, clazz, method, expRes, args);
258 static void assertExec(
259 Invocable toRun, CharSequence theCode,
260 String msg, Class clazz, String method,
261 Object expRes, Object... args
265 ret = toRun.invokeFunction("bck2brwsr");
266 ret = toRun.invokeMethod(ret, "loadClass", clazz.getName().replace('.', '_'));
267 ret = toRun.invokeMethod(ret, method, args);
268 } catch (ScriptException ex) {
269 fail("Execution failed in\n" + dumpJS(theCode), ex);
270 } catch (NoSuchMethodException ex) {
271 fail("Cannot find method in\n" + dumpJS(theCode), ex);
273 if (ret == null && expRes == null) {
276 if (expRes != null && expRes.equals(ret)) {
279 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
283 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
284 return compileClass(sb, null, names);
286 static Invocable compileClass(
287 StringBuilder sb, ScriptEngine[] eng, String... names
288 ) throws ScriptException, IOException {
290 sb = new StringBuilder();
292 GenJS.compile(sb, names);
293 ScriptEngineManager sem = new ScriptEngineManager();
294 ScriptEngine js = sem.getEngineByExtension("js");
299 Object res = js.eval(sb.toString());
300 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
301 return (Invocable)js;
302 } catch (Exception ex) {
303 if (sb.length() > 2000) {
306 fail("Could not evaluate:\n" + sb, ex);
310 static StringBuilder dumpJS(CharSequence sb) throws IOException {
311 File f = File.createTempFile("execution", ".js");
312 FileWriter w = new FileWriter(f);
315 return new StringBuilder(f.getPath());