src/test/java/org/apidesign/java4browser/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 27 Aug 2012 14:27:06 +0200
changeset 5 d3193a7086e7
parent 4 f352a33fb71b
child 6 6e4682985907
permissions -rw-r--r--
Support for factorial computed with a for cycle
     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     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    95         StringBuilder sb = new StringBuilder();
    96         Invocable i = compileClass("StaticMethod.class", sb);
    97         
    98         Object ret = null;
    99         try {
   100             ret = i.invokeFunction(methodName, args);
   101         } catch (ScriptException ex) {
   102             fail("Execution failed in " + sb, ex);
   103         } catch (NoSuchMethodException ex) {
   104             fail("Cannot find method in " + sb, ex);
   105         }
   106         if (ret == null && expRes == null) {
   107             return;
   108         }
   109         if (expRes.equals(ret)) {
   110             return;
   111         }
   112         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   113         
   114     }
   115 
   116     static Invocable compileClass(String name, StringBuilder sb) throws ScriptException, IOException {
   117         InputStream is = StaticMethodTest.class.getResourceAsStream(name);
   118         assertNotNull(is, "Class file found");
   119         if (sb == null) {
   120             sb = new StringBuilder();
   121         }
   122         ByteCodeToJavaScript.compile(name, is, sb);
   123         ScriptEngineManager sem = new ScriptEngineManager();
   124         ScriptEngine js = sem.getEngineByExtension("js");
   125         try {
   126             Object res = js.eval(sb.toString());
   127             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   128             return (Invocable)js;
   129         } catch (ScriptException ex) {
   130             fail("Could not compile:\n" + sb, ex);
   131             return null;
   132         }
   133     }
   134 }