src/test/java/org/apidesign/java4browser/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 27 Aug 2012 14:36:48 +0200
changeset 6 6e4682985907
parent 5 d3193a7086e7
child 7 5b135a2f2de3
permissions -rw-r--r--
Support for xor
     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 javax.script.Invocable;
    23 import javax.script.ScriptEngine;
    24 import javax.script.ScriptEngineManager;
    25 import javax.script.ScriptException;
    26 import static org.testng.Assert.*;
    27 import org.testng.annotations.Test;
    28 
    29 /** Checks the basic behavior of the translator.
    30  *
    31  * @author Jaroslav Tulach <jtulach@netbeans.org>
    32  */
    33 public class StaticMethodTest {
    34     @Test public void threePlusFour() throws Exception {
    35         assertExec(
    36             "Should be seven", 
    37             "org_apidesign_java4browser_StaticMethod_sumIII", 
    38             Double.valueOf(7), 
    39             3, 4
    40         );
    41     }
    42 
    43     @Test public void powerOfThree() throws Exception {
    44         assertExec(
    45             "Should be nine", 
    46             "org_apidesign_java4browser_StaticMethod_powerFF", 
    47             Double.valueOf(9),
    48             3.0f
    49         );
    50     }
    51 
    52     @Test public void doubleWithoutLong() throws Exception {
    53         assertExec(
    54             "Should be two",
    55             "org_apidesign_java4browser_StaticMethod_minusDDJ", 
    56             Double.valueOf(2),
    57             3.0d, 1l
    58         );
    59     }
    60 
    61     @Test public void divAndRound() throws Exception {
    62         assertExec(
    63             "Should be rounded to one",
    64             "org_apidesign_java4browser_StaticMethod_divIBD", 
    65             Double.valueOf(1),
    66             3, 3.75
    67         );
    68     }
    69     @Test public void mixedMethodFourParams() throws Exception {
    70         assertExec(
    71             "Should be two",
    72             "org_apidesign_java4browser_StaticMethod_mixIIJBD", 
    73             Double.valueOf(20),
    74             2, 10l, 5, 2.0
    75         );
    76     }
    77     @Test public void factRec() throws Exception {
    78         assertExec(
    79             "Factorial of 5 is 120",
    80             "org_apidesign_java4browser_StaticMethod_factRecJI", 
    81             Double.valueOf(120),
    82             5
    83         );
    84     }
    85     @Test public void factIter() throws Exception {
    86         assertExec(
    87             "Factorial of 5 is 120",
    88             "org_apidesign_java4browser_StaticMethod_factIterJI", 
    89             Double.valueOf(120),
    90             5
    91         );
    92     }
    93     
    94     @Test public void xor() throws Exception {
    95         assertExec(
    96             "Xor is 4",
    97             "org_apidesign_java4browser_StaticMethod_xorJIJ",
    98             Double.valueOf(4),
    99             7,
   100             3
   101         );
   102     }
   103     
   104     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   105         StringBuilder sb = new StringBuilder();
   106         Invocable i = compileClass("StaticMethod.class", sb);
   107         
   108         Object ret = null;
   109         try {
   110             ret = i.invokeFunction(methodName, args);
   111         } catch (ScriptException ex) {
   112             fail("Execution failed in " + sb, ex);
   113         } catch (NoSuchMethodException ex) {
   114             fail("Cannot find method in " + sb, ex);
   115         }
   116         if (ret == null && expRes == null) {
   117             return;
   118         }
   119         if (expRes.equals(ret)) {
   120             return;
   121         }
   122         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   123         
   124     }
   125 
   126     static Invocable compileClass(String name, StringBuilder sb) throws ScriptException, IOException {
   127         InputStream is = StaticMethodTest.class.getResourceAsStream(name);
   128         assertNotNull(is, "Class file found");
   129         if (sb == null) {
   130             sb = new StringBuilder();
   131         }
   132         ByteCodeToJavaScript.compile(name, is, sb);
   133         ScriptEngineManager sem = new ScriptEngineManager();
   134         ScriptEngine js = sem.getEngineByExtension("js");
   135         try {
   136             Object res = js.eval(sb.toString());
   137             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   138             return (Invocable)js;
   139         } catch (ScriptException ex) {
   140             fail("Could not compile:\n" + sb, ex);
   141             return null;
   142         }
   143     }
   144 }