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