src/test/java/org/apidesign/java4browser/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 27 Aug 2012 05:47:44 +0200
changeset 2 4679bf342a1f
parent 1 48b1dce93691
child 3 e44f0155d946
permissions -rw-r--r--
Support for subtraction. Double occupies two slots. static import of byte codes.
     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     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    62         StringBuilder sb = new StringBuilder();
    63         Invocable i = compileClass("StaticMethod.class", sb);
    64         
    65         Object ret = null;
    66         try {
    67             ret = i.invokeFunction(methodName, args);
    68         } catch (NoSuchMethodException ex) {
    69             fail("Cannot find method in " + sb, ex);
    70         }
    71         if (ret == null && expRes == null) {
    72             return;
    73         }
    74         if (expRes.equals(ret)) {
    75             return;
    76         }
    77         assertEquals(ret, expRes, msg + "\n" + sb);
    78         
    79     }
    80 
    81     static Invocable compileClass(String name, StringBuilder sb) throws ScriptException, IOException {
    82         InputStream is = StaticMethodTest.class.getResourceAsStream(name);
    83         assertNotNull(is, "Class file found");
    84         if (sb == null) {
    85             sb = new StringBuilder();
    86         }
    87         ByteCodeToJavaScript.compile(name, is, sb);
    88         ScriptEngineManager sem = new ScriptEngineManager();
    89         ScriptEngine js = sem.getEngineByExtension("js");
    90         try {
    91             Object res = js.eval(sb.toString());
    92             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
    93             return (Invocable)js;
    94         } catch (ScriptException ex) {
    95             fail("Could not compile:\n" + sb, ex);
    96             return null;
    97         }
    98     }
    99 }