src/test/java/org/apidesign/java4browser/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 16 Sep 2012 07:28:57 +0200
changeset 9 0cab1e07e677
parent 7 5b135a2f2de3
child 13 99f832e5765f
permissions -rw-r--r--
Support for static integer fields
     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     @Test public void or() throws Exception {
   105         assertExec(
   106             "Or will be 7",
   107             "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
   108             Double.valueOf(7),
   109             true,
   110             4,
   111             3
   112         );
   113     }
   114     @Test public void and() throws Exception {
   115         assertExec(
   116             "And will be 3",
   117             "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
   118             Double.valueOf(3),
   119             false,
   120             7,
   121             3
   122         );
   123     }
   124     @Test public void inc4() throws Exception {
   125         assertExec(
   126             "It will be 4",
   127             "org_apidesign_java4browser_StaticMethod_inc4I",
   128             Double.valueOf(4)
   129         );
   130     }
   131     
   132     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   133         StringBuilder sb = new StringBuilder();
   134         Invocable i = compileClass("StaticMethod.class", sb);
   135         
   136         Object ret = null;
   137         try {
   138             ret = i.invokeFunction(methodName, args);
   139         } catch (ScriptException ex) {
   140             fail("Execution failed in " + sb, ex);
   141         } catch (NoSuchMethodException ex) {
   142             fail("Cannot find method in " + sb, ex);
   143         }
   144         if (ret == null && expRes == null) {
   145             return;
   146         }
   147         if (expRes.equals(ret)) {
   148             return;
   149         }
   150         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   151         
   152     }
   153 
   154     static Invocable compileClass(String name, StringBuilder sb) throws ScriptException, IOException {
   155         InputStream is = StaticMethodTest.class.getResourceAsStream(name);
   156         assertNotNull(is, "Class file found");
   157         if (sb == null) {
   158             sb = new StringBuilder();
   159         }
   160         ByteCodeToJavaScript.compile(name, is, sb);
   161         ScriptEngineManager sem = new ScriptEngineManager();
   162         ScriptEngine js = sem.getEngineByExtension("js");
   163         try {
   164             Object res = js.eval(sb.toString());
   165             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   166             return (Invocable)js;
   167         } catch (ScriptException ex) {
   168             fail("Could not compile:\n" + sb, ex);
   169             return null;
   170         }
   171     }
   172 }