1.1 --- a/src/test/java/org/apidesign/java4browser/StaticMethodTest.java Mon Sep 24 09:35:00 2012 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,201 +0,0 @@
1.4 -/*
1.5 -Java 4 Browser Bytecode Translator
1.6 -Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 -
1.8 -This program is free software: you can redistribute it and/or modify
1.9 -it under the terms of the GNU General Public License as published by
1.10 -the Free Software Foundation, version 2 of the License.
1.11 -
1.12 -This program is distributed in the hope that it will be useful,
1.13 -but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 -GNU General Public License for more details.
1.16 -
1.17 -You should have received a copy of the GNU General Public License
1.18 -along with this program. Look for COPYING file in the top folder.
1.19 -If not, see http://opensource.org/licenses/GPL-2.0.
1.20 -*/
1.21 -package org.apidesign.java4browser;
1.22 -
1.23 -import java.io.IOException;
1.24 -import java.io.InputStream;
1.25 -import java.util.Arrays;
1.26 -import java.util.HashSet;
1.27 -import java.util.Iterator;
1.28 -import java.util.LinkedList;
1.29 -import java.util.Set;
1.30 -import java.util.TreeSet;
1.31 -import javax.script.Invocable;
1.32 -import javax.script.ScriptEngine;
1.33 -import javax.script.ScriptEngineManager;
1.34 -import javax.script.ScriptException;
1.35 -import static org.testng.Assert.*;
1.36 -import org.testng.annotations.Test;
1.37 -
1.38 -/** Checks the basic behavior of the translator.
1.39 - *
1.40 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.41 - */
1.42 -public class StaticMethodTest {
1.43 - @Test public void threePlusFour() throws Exception {
1.44 - assertExec(
1.45 - "Should be seven",
1.46 - "org_apidesign_java4browser_StaticMethod_sumIII",
1.47 - Double.valueOf(7),
1.48 - 3, 4
1.49 - );
1.50 - }
1.51 -
1.52 - @Test public void powerOfThree() throws Exception {
1.53 - assertExec(
1.54 - "Should be nine",
1.55 - "org_apidesign_java4browser_StaticMethod_powerFF",
1.56 - Double.valueOf(9),
1.57 - 3.0f
1.58 - );
1.59 - }
1.60 -
1.61 - @Test public void doubleWithoutLong() throws Exception {
1.62 - assertExec(
1.63 - "Should be two",
1.64 - "org_apidesign_java4browser_StaticMethod_minusDDJ",
1.65 - Double.valueOf(2),
1.66 - 3.0d, 1l
1.67 - );
1.68 - }
1.69 -
1.70 - @Test public void divAndRound() throws Exception {
1.71 - assertExec(
1.72 - "Should be rounded to one",
1.73 - "org_apidesign_java4browser_StaticMethod_divIBD",
1.74 - Double.valueOf(1),
1.75 - 3, 3.75
1.76 - );
1.77 - }
1.78 - @Test public void mixedMethodFourParams() throws Exception {
1.79 - assertExec(
1.80 - "Should be two",
1.81 - "org_apidesign_java4browser_StaticMethod_mixIIJBD",
1.82 - Double.valueOf(20),
1.83 - 2, 10l, 5, 2.0
1.84 - );
1.85 - }
1.86 - @Test public void factRec() throws Exception {
1.87 - assertExec(
1.88 - "Factorial of 5 is 120",
1.89 - "org_apidesign_java4browser_StaticMethod_factRecJI",
1.90 - Double.valueOf(120),
1.91 - 5
1.92 - );
1.93 - }
1.94 - @Test public void factIter() throws Exception {
1.95 - assertExec(
1.96 - "Factorial of 5 is 120",
1.97 - "org_apidesign_java4browser_StaticMethod_factIterJI",
1.98 - Double.valueOf(120),
1.99 - 5
1.100 - );
1.101 - }
1.102 -
1.103 - @Test public void xor() throws Exception {
1.104 - assertExec(
1.105 - "Xor is 4",
1.106 - "org_apidesign_java4browser_StaticMethod_xorJIJ",
1.107 - Double.valueOf(4),
1.108 - 7,
1.109 - 3
1.110 - );
1.111 - }
1.112 -
1.113 - @Test public void or() throws Exception {
1.114 - assertExec(
1.115 - "Or will be 7",
1.116 - "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
1.117 - Double.valueOf(7),
1.118 - true,
1.119 - 4,
1.120 - 3
1.121 - );
1.122 - }
1.123 - @Test public void and() throws Exception {
1.124 - assertExec(
1.125 - "And will be 3",
1.126 - "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
1.127 - Double.valueOf(3),
1.128 - false,
1.129 - 7,
1.130 - 3
1.131 - );
1.132 - }
1.133 - @Test public void inc4() throws Exception {
1.134 - assertExec(
1.135 - "It will be 4",
1.136 - "org_apidesign_java4browser_StaticMethod_inc4I",
1.137 - Double.valueOf(4)
1.138 - );
1.139 - }
1.140 -
1.141 - private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
1.142 - StringBuilder sb = new StringBuilder();
1.143 - Invocable i = compileClass(sb, "org/apidesign/java4browser/StaticMethod");
1.144 -
1.145 - Object ret = null;
1.146 - try {
1.147 - ret = i.invokeFunction(methodName, args);
1.148 - } catch (ScriptException ex) {
1.149 - fail("Execution failed in " + sb, ex);
1.150 - } catch (NoSuchMethodException ex) {
1.151 - fail("Cannot find method in " + sb, ex);
1.152 - }
1.153 - if (ret == null && expRes == null) {
1.154 - return;
1.155 - }
1.156 - if (expRes.equals(ret)) {
1.157 - return;
1.158 - }
1.159 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
1.160 -
1.161 - }
1.162 -
1.163 - static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
1.164 - if (sb == null) {
1.165 - sb = new StringBuilder();
1.166 - }
1.167 - Set<String> processed = new HashSet<String>();
1.168 -
1.169 - LinkedList<String> toProcess = new LinkedList<String>(Arrays.asList(names));
1.170 - for (;;) {
1.171 - toProcess.removeAll(processed);
1.172 - if (toProcess.isEmpty()) {
1.173 - break;
1.174 - }
1.175 - String name = toProcess.getFirst();
1.176 - processed.add(name);
1.177 - if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
1.178 - continue;
1.179 - }
1.180 - InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
1.181 - assertNotNull(is, "Class file found");
1.182 - try {
1.183 - ByteCodeToJavaScript.compile(is, sb, toProcess);
1.184 - } catch (RuntimeException ex) {
1.185 - int lastBlock = sb.lastIndexOf("{");
1.186 - throw new IllegalStateException(
1.187 - "Error while compiling " + name + "\n" +
1.188 - sb.substring(0, sb.length()),
1.189 - ex
1.190 - );
1.191 - }
1.192 - }
1.193 - ScriptEngineManager sem = new ScriptEngineManager();
1.194 - ScriptEngine js = sem.getEngineByExtension("js");
1.195 - try {
1.196 - Object res = js.eval(sb.toString());
1.197 - assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.198 - return (Invocable)js;
1.199 - } catch (ScriptException ex) {
1.200 - fail("Could not compile:\n" + sb, ex);
1.201 - return null;
1.202 - }
1.203 - }
1.204 -}