jaroslav@0: /* jaroslav@0: Java 4 Browser Bytecode Translator jaroslav@0: Copyright (C) 2012-2012 Jaroslav Tulach jaroslav@0: jaroslav@0: This program is free software: you can redistribute it and/or modify jaroslav@0: it under the terms of the GNU General Public License as published by jaroslav@0: the Free Software Foundation, version 2 of the License. jaroslav@0: jaroslav@0: This program is distributed in the hope that it will be useful, jaroslav@0: but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@0: GNU General Public License for more details. jaroslav@0: jaroslav@0: You should have received a copy of the GNU General Public License jaroslav@0: along with this program. Look for COPYING file in the top folder. jaroslav@0: If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@0: */ jaroslav@0: package org.apidesign.java4browser; jaroslav@0: jaroslav@0: import java.io.IOException; jaroslav@0: import java.io.InputStream; jaroslav@0: import javax.script.Invocable; jaroslav@0: import javax.script.ScriptEngine; jaroslav@0: import javax.script.ScriptEngineManager; jaroslav@0: import javax.script.ScriptException; jaroslav@0: import static org.testng.Assert.*; jaroslav@0: import org.testng.annotations.Test; jaroslav@0: jaroslav@0: /** Checks the basic behavior of the translator. jaroslav@0: * jaroslav@0: * @author Jaroslav Tulach jaroslav@0: */ jaroslav@0: public class StaticMethodTest { jaroslav@0: @Test public void threePlusFour() throws Exception { jaroslav@0: Invocable i = compileClass("StaticMethod.class"); jaroslav@0: jaroslav@0: Object ret = i.invokeFunction("org_apidesign_java4browser_StaticMethod_sumIII", 3, 4); jaroslav@0: assertEquals(ret, Double.valueOf(7), "Should be seven"); jaroslav@0: } jaroslav@0: jaroslav@0: static Invocable compileClass(String name) throws ScriptException, IOException { jaroslav@0: InputStream is = StaticMethodTest.class.getResourceAsStream(name); jaroslav@0: assertNotNull(is, "Class file found"); jaroslav@0: StringBuilder sb = new StringBuilder(); jaroslav@0: ByteCodeToJavaScript.compile(name, is, sb); jaroslav@0: ScriptEngineManager sem = new ScriptEngineManager(); jaroslav@0: ScriptEngine js = sem.getEngineByExtension("js"); jaroslav@0: try { jaroslav@0: Object res = js.eval(sb.toString()); jaroslav@0: assertTrue(js instanceof Invocable, "It is invocable object: " + res); jaroslav@0: return (Invocable)js; jaroslav@0: } catch (ScriptException ex) { jaroslav@0: fail("Could not compile:\n" + sb, ex); jaroslav@0: return null; jaroslav@0: } jaroslav@0: } jaroslav@0: }