src/test/java/org/apidesign/java4browser/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 20 Sep 2012 14:20:01 +0200
changeset 18 361b76189f8d
parent 13 99f832e5765f
child 19 2291e553464a
permissions -rw-r--r--
The compilation to JavaScript now identifies list of external references and provides them back to the caller
     1 /*
     2 Java 4 Browser Bytecode Translator
     3 Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4 
     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.
     8 
     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.
    13 
    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.
    17 */
    18 package org.apidesign.java4browser;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.util.Arrays;
    23 import java.util.Iterator;
    24 import java.util.Set;
    25 import java.util.TreeSet;
    26 import javax.script.Invocable;
    27 import javax.script.ScriptEngine;
    28 import javax.script.ScriptEngineManager;
    29 import javax.script.ScriptException;
    30 import static org.testng.Assert.*;
    31 import org.testng.annotations.Test;
    32 
    33 /** Checks the basic behavior of the translator.
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public class StaticMethodTest {
    38     @Test public void threePlusFour() throws Exception {
    39         assertExec(
    40             "Should be seven", 
    41             "org_apidesign_java4browser_StaticMethod_sumIII", 
    42             Double.valueOf(7), 
    43             3, 4
    44         );
    45     }
    46 
    47     @Test public void powerOfThree() throws Exception {
    48         assertExec(
    49             "Should be nine", 
    50             "org_apidesign_java4browser_StaticMethod_powerFF", 
    51             Double.valueOf(9),
    52             3.0f
    53         );
    54     }
    55 
    56     @Test public void doubleWithoutLong() throws Exception {
    57         assertExec(
    58             "Should be two",
    59             "org_apidesign_java4browser_StaticMethod_minusDDJ", 
    60             Double.valueOf(2),
    61             3.0d, 1l
    62         );
    63     }
    64 
    65     @Test public void divAndRound() throws Exception {
    66         assertExec(
    67             "Should be rounded to one",
    68             "org_apidesign_java4browser_StaticMethod_divIBD", 
    69             Double.valueOf(1),
    70             3, 3.75
    71         );
    72     }
    73     @Test public void mixedMethodFourParams() throws Exception {
    74         assertExec(
    75             "Should be two",
    76             "org_apidesign_java4browser_StaticMethod_mixIIJBD", 
    77             Double.valueOf(20),
    78             2, 10l, 5, 2.0
    79         );
    80     }
    81     @Test public void factRec() throws Exception {
    82         assertExec(
    83             "Factorial of 5 is 120",
    84             "org_apidesign_java4browser_StaticMethod_factRecJI", 
    85             Double.valueOf(120),
    86             5
    87         );
    88     }
    89     @Test public void factIter() throws Exception {
    90         assertExec(
    91             "Factorial of 5 is 120",
    92             "org_apidesign_java4browser_StaticMethod_factIterJI", 
    93             Double.valueOf(120),
    94             5
    95         );
    96     }
    97     
    98     @Test public void xor() throws Exception {
    99         assertExec(
   100             "Xor is 4",
   101             "org_apidesign_java4browser_StaticMethod_xorJIJ",
   102             Double.valueOf(4),
   103             7,
   104             3
   105         );
   106     }
   107     
   108     @Test public void or() throws Exception {
   109         assertExec(
   110             "Or will be 7",
   111             "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
   112             Double.valueOf(7),
   113             true,
   114             4,
   115             3
   116         );
   117     }
   118     @Test public void and() throws Exception {
   119         assertExec(
   120             "And will be 3",
   121             "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
   122             Double.valueOf(3),
   123             false,
   124             7,
   125             3
   126         );
   127     }
   128     @Test public void inc4() throws Exception {
   129         assertExec(
   130             "It will be 4",
   131             "org_apidesign_java4browser_StaticMethod_inc4I",
   132             Double.valueOf(4)
   133         );
   134     }
   135     
   136     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   137         StringBuilder sb = new StringBuilder();
   138         Invocable i = compileClass(sb, "org/apidesign/java4browser/StaticMethod");
   139         
   140         Object ret = null;
   141         try {
   142             ret = i.invokeFunction(methodName, args);
   143         } catch (ScriptException ex) {
   144             fail("Execution failed in " + sb, ex);
   145         } catch (NoSuchMethodException ex) {
   146             fail("Cannot find method in " + sb, ex);
   147         }
   148         if (ret == null && expRes == null) {
   149             return;
   150         }
   151         if (expRes.equals(ret)) {
   152             return;
   153         }
   154         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   155         
   156     }
   157 
   158     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   159         for (String name : names) {
   160             InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
   161             assertNotNull(is, "Class file found");
   162             if (sb == null) {
   163                 sb = new StringBuilder();
   164             }
   165             ByteCodeToJavaScript.compile(is, sb, null);
   166         }
   167         ScriptEngineManager sem = new ScriptEngineManager();
   168         ScriptEngine js = sem.getEngineByExtension("js");
   169         try {
   170             Object res = js.eval(sb.toString());
   171             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   172             return (Invocable)js;
   173         } catch (ScriptException ex) {
   174             fail("Could not compile:\n" + sb, ex);
   175             return null;
   176         }
   177     }
   178 }