src/test/java/org/apidesign/java4browser/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Sep 2012 09:35:00 +0200
changeset 21 d8807b6a636a
parent 20 0e7dd9e2e31e
permissions -rw-r--r--
Basic support for manipulating array
     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.HashSet;
    24 import java.util.Iterator;
    25 import java.util.LinkedList;
    26 import java.util.Set;
    27 import java.util.TreeSet;
    28 import javax.script.Invocable;
    29 import javax.script.ScriptEngine;
    30 import javax.script.ScriptEngineManager;
    31 import javax.script.ScriptException;
    32 import static org.testng.Assert.*;
    33 import org.testng.annotations.Test;
    34 
    35 /** Checks the basic behavior of the translator.
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public class StaticMethodTest {
    40     @Test public void threePlusFour() throws Exception {
    41         assertExec(
    42             "Should be seven", 
    43             "org_apidesign_java4browser_StaticMethod_sumIII", 
    44             Double.valueOf(7), 
    45             3, 4
    46         );
    47     }
    48 
    49     @Test public void powerOfThree() throws Exception {
    50         assertExec(
    51             "Should be nine", 
    52             "org_apidesign_java4browser_StaticMethod_powerFF", 
    53             Double.valueOf(9),
    54             3.0f
    55         );
    56     }
    57 
    58     @Test public void doubleWithoutLong() throws Exception {
    59         assertExec(
    60             "Should be two",
    61             "org_apidesign_java4browser_StaticMethod_minusDDJ", 
    62             Double.valueOf(2),
    63             3.0d, 1l
    64         );
    65     }
    66 
    67     @Test public void divAndRound() throws Exception {
    68         assertExec(
    69             "Should be rounded to one",
    70             "org_apidesign_java4browser_StaticMethod_divIBD", 
    71             Double.valueOf(1),
    72             3, 3.75
    73         );
    74     }
    75     @Test public void mixedMethodFourParams() throws Exception {
    76         assertExec(
    77             "Should be two",
    78             "org_apidesign_java4browser_StaticMethod_mixIIJBD", 
    79             Double.valueOf(20),
    80             2, 10l, 5, 2.0
    81         );
    82     }
    83     @Test public void factRec() throws Exception {
    84         assertExec(
    85             "Factorial of 5 is 120",
    86             "org_apidesign_java4browser_StaticMethod_factRecJI", 
    87             Double.valueOf(120),
    88             5
    89         );
    90     }
    91     @Test public void factIter() throws Exception {
    92         assertExec(
    93             "Factorial of 5 is 120",
    94             "org_apidesign_java4browser_StaticMethod_factIterJI", 
    95             Double.valueOf(120),
    96             5
    97         );
    98     }
    99     
   100     @Test public void xor() throws Exception {
   101         assertExec(
   102             "Xor is 4",
   103             "org_apidesign_java4browser_StaticMethod_xorJIJ",
   104             Double.valueOf(4),
   105             7,
   106             3
   107         );
   108     }
   109     
   110     @Test public void or() throws Exception {
   111         assertExec(
   112             "Or will be 7",
   113             "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
   114             Double.valueOf(7),
   115             true,
   116             4,
   117             3
   118         );
   119     }
   120     @Test public void and() throws Exception {
   121         assertExec(
   122             "And will be 3",
   123             "org_apidesign_java4browser_StaticMethod_orOrAndJZII",
   124             Double.valueOf(3),
   125             false,
   126             7,
   127             3
   128         );
   129     }
   130     @Test public void inc4() throws Exception {
   131         assertExec(
   132             "It will be 4",
   133             "org_apidesign_java4browser_StaticMethod_inc4I",
   134             Double.valueOf(4)
   135         );
   136     }
   137     
   138     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   139         StringBuilder sb = new StringBuilder();
   140         Invocable i = compileClass(sb, "org/apidesign/java4browser/StaticMethod");
   141         
   142         Object ret = null;
   143         try {
   144             ret = i.invokeFunction(methodName, args);
   145         } catch (ScriptException ex) {
   146             fail("Execution failed in " + sb, ex);
   147         } catch (NoSuchMethodException ex) {
   148             fail("Cannot find method in " + sb, ex);
   149         }
   150         if (ret == null && expRes == null) {
   151             return;
   152         }
   153         if (expRes.equals(ret)) {
   154             return;
   155         }
   156         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   157         
   158     }
   159 
   160     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   161         if (sb == null) {
   162             sb = new StringBuilder();
   163         }
   164         Set<String> processed = new HashSet<String>();
   165 
   166         LinkedList<String> toProcess = new LinkedList<String>(Arrays.asList(names));
   167         for (;;) {
   168             toProcess.removeAll(processed);
   169             if (toProcess.isEmpty()) {
   170                 break;
   171             }
   172             String name = toProcess.getFirst();
   173             processed.add(name);
   174             if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
   175                 continue;
   176             }
   177             InputStream is = StaticMethodTest.class.getClassLoader().getResourceAsStream(name + ".class");
   178             assertNotNull(is, "Class file found");
   179             try {
   180                 ByteCodeToJavaScript.compile(is, sb, toProcess);
   181             } catch (RuntimeException ex) {
   182                 int lastBlock = sb.lastIndexOf("{");
   183                 throw new IllegalStateException(
   184                     "Error while compiling " + name + "\n" + 
   185                     sb.substring(0, sb.length()), 
   186                     ex
   187                 );
   188             }
   189         }
   190         ScriptEngineManager sem = new ScriptEngineManager();
   191         ScriptEngine js = sem.getEngineByExtension("js");
   192         try {
   193             Object res = js.eval(sb.toString());
   194             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   195             return (Invocable)js;
   196         } catch (ScriptException ex) {
   197             fail("Could not compile:\n" + sb, ex);
   198             return null;
   199         }
   200     }
   201 }