vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 06 Dec 2012 21:31:09 +0100
branchlazyvm
changeset 274 81f6e7778135
parent 248 0bfcb6585290
child 276 aeb9fe11cd60
permissions -rw-r--r--
Hiding all generated class methods from sight of external users. Exposing only bck2brwsr entry point function
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 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 java.io.File;
    21 import java.io.FileWriter;
    22 import java.io.IOException;
    23 import javax.script.Invocable;
    24 import javax.script.ScriptEngine;
    25 import javax.script.ScriptEngineManager;
    26 import javax.script.ScriptException;
    27 import static org.testng.Assert.*;
    28 import org.testng.annotations.BeforeClass;
    29 import org.testng.annotations.Test;
    30 
    31 /** Checks the basic behavior of the translator.
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 public class StaticMethodTest {
    36     @Test public void threePlusFour() throws Exception {
    37         assertExec(
    38             "Should be seven", 
    39             StaticMethod.class, "sum__III", 
    40             Double.valueOf(7), 
    41             3, 4
    42         );
    43     }
    44 
    45     @Test public void checkReallyInitializedValues() throws Exception {
    46         assertExec(
    47             "Return true", 
    48             StaticMethod.class, "isNull__Z", 
    49             Double.valueOf(1)
    50         );
    51     }
    52 
    53     @Test public void powerOfThree() throws Exception {
    54         assertExec(
    55             "Should be nine", 
    56             StaticMethod.class, "power__FF", 
    57             Double.valueOf(9),
    58             3.0f
    59         );
    60     }
    61 
    62     @Test public void minusOne() throws Exception {
    63         assertExec(
    64             "Should be minus one", 
    65             StaticMethod.class, "minusOne__I", 
    66             Double.valueOf(-1)
    67         );
    68     }
    69 
    70     @Test public void doubleWithoutLong() throws Exception {
    71         assertExec(
    72             "Should be two",
    73             StaticMethod.class, "minus__DDJ", 
    74             Double.valueOf(2),
    75             3.0d, 1l
    76         );
    77     }
    78 
    79     @Test public void divAndRound() throws Exception {
    80         assertExec(
    81             "Should be rounded to one",
    82             StaticMethod.class, "div__IBD", 
    83             Double.valueOf(1),
    84             3, 3.75
    85         );
    86     }
    87     @Test public void mixedMethodFourParams() throws Exception {
    88         assertExec(
    89             "Should be two",
    90             StaticMethod.class, "mix__IIJBD", 
    91             Double.valueOf(20),
    92             2, 10l, 5, 2.0
    93         );
    94     }
    95     @Test public void factRec() throws Exception {
    96         assertExec(
    97             "Factorial of 5 is 120",
    98             StaticMethod.class, "factRec__JI", 
    99             Double.valueOf(120),
   100             5
   101         );
   102     }
   103     @Test public void factIter() throws Exception {
   104         assertExec(
   105             "Factorial of 5 is 120",
   106             StaticMethod.class, "factIter__JI", 
   107             Double.valueOf(120),
   108             5
   109         );
   110     }
   111     
   112     @Test public void xor() throws Exception {
   113         assertExec(
   114             "Xor is 4",
   115             StaticMethod.class, "xor__JIJ",
   116             Double.valueOf(4),
   117             7,
   118             3
   119         );
   120     }
   121     
   122     @Test public void or() throws Exception {
   123         assertExec(
   124             "Or will be 7",
   125             StaticMethod.class, "orOrAnd__JZII",
   126             Double.valueOf(7),
   127             true,
   128             4,
   129             3
   130         );
   131     }
   132     @Test public void nullCheck() throws Exception {
   133         assertExec(
   134             "Returns nothing",
   135             StaticMethod.class, "none__Ljava_lang_Object_2II",
   136             null, 1, 3
   137         );
   138     }
   139     @Test public void and() throws Exception {
   140         assertExec(
   141             "And will be 3",
   142             StaticMethod.class, "orOrAnd__JZII",
   143             Double.valueOf(3),
   144             false,
   145             7,
   146             3
   147         );
   148     }
   149     @Test public void inc4() throws Exception {
   150         assertExec(
   151             "It will be 4",
   152             StaticMethod.class, "inc4__I",
   153             Double.valueOf(4)
   154         );
   155     }
   156     
   157     @Test public void shiftLeftInJava() throws Exception {
   158         int res = StaticMethod.shiftLeft(1, 8);
   159         assertEquals(res, 256);
   160     }
   161 
   162     @Test public void shiftLeftInJS() throws Exception {
   163         assertExec(
   164             "Setting 9th bit",
   165             StaticMethod.class, "shiftLeft__III",
   166             Double.valueOf(256),
   167             1, 8
   168         );
   169     }
   170 
   171     @Test public void shiftRightInJava() throws Exception {
   172         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   173         assertEquals(res, -1);
   174     }
   175 
   176     @Test public void shiftRightInJS() throws Exception {
   177         assertExec(
   178             "Get -1",
   179             StaticMethod.class, "shiftArithmRight__IIIZ",
   180             Double.valueOf(-1),
   181             -8, 3, true
   182         );
   183     }
   184     @Test public void unsignedShiftRightInJava() throws Exception {
   185         int res = StaticMethod.shiftArithmRight(8, 3, false);
   186         assertEquals(res, 1);
   187     }
   188 
   189     @Test public void unsignedShiftRightInJS() throws Exception {
   190         assertExec(
   191             "Get -1",
   192             StaticMethod.class, "shiftArithmRight__IIIZ",
   193             Double.valueOf(1),
   194             8, 3, false
   195         );
   196     }
   197     
   198     @Test public void javaScriptBody() throws Exception {
   199         assertExec(
   200             "JavaScript string",
   201             StaticMethod.class, "i2s__Ljava_lang_String_2II",
   202             "333",
   203             330, 3
   204         );
   205     }
   206     
   207     @Test public void switchJarda() throws Exception {
   208         assertExec(
   209             "The expected value",
   210             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   211             "Jarda",
   212             0
   213         );
   214     }
   215     
   216     @Test public void switchDarda() throws Exception {
   217         assertExec(
   218             "The expected value",
   219             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   220             "Darda",
   221             1
   222         );
   223     }
   224     @Test public void switchParda() throws Exception {
   225         assertExec(
   226             "The expected value",
   227             StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   228             "Parda",
   229             22
   230         );
   231     }
   232     @Test public void switchMarda() throws Exception {
   233         assertExec(
   234             "The expected value",
   235             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   236             "Marda",
   237             -433
   238         );
   239     }
   240     
   241     private static CharSequence codeSeq;
   242     private static Invocable code;
   243     
   244     @BeforeClass 
   245     public void compileTheCode() throws Exception {
   246         StringBuilder sb = new StringBuilder();
   247         code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   248         codeSeq = sb;
   249     }
   250     
   251     
   252     private static void assertExec(
   253         String msg, Class clazz, String method, 
   254         Object expRes, Object... args
   255     ) throws Exception {
   256         assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   257     }
   258     static void assertExec(
   259         Invocable toRun, CharSequence theCode,
   260         String msg, Class clazz, String method, 
   261         Object expRes, Object... args
   262     ) throws Exception {
   263         Object ret = null;
   264         try {
   265             ret = toRun.invokeFunction("bck2brwsr");
   266             ret = toRun.invokeMethod(ret, "loadClass", clazz.getName().replace('.', '_'));
   267             ret = toRun.invokeMethod(ret, method, args);
   268         } catch (ScriptException ex) {
   269             fail("Execution failed in\n" + dumpJS(theCode), ex);
   270         } catch (NoSuchMethodException ex) {
   271             fail("Cannot find method in\n" + dumpJS(theCode), ex);
   272         }
   273         if (ret == null && expRes == null) {
   274             return;
   275         }
   276         if (expRes != null && expRes.equals(ret)) {
   277             return;
   278         }
   279         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
   280         
   281     }
   282 
   283     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   284         return compileClass(sb, null, names);
   285     }
   286     static Invocable compileClass(
   287         StringBuilder sb, ScriptEngine[] eng, String... names
   288     ) throws ScriptException, IOException {
   289         if (sb == null) {
   290             sb = new StringBuilder();
   291         }
   292         GenJS.compile(sb, names);
   293         ScriptEngineManager sem = new ScriptEngineManager();
   294         ScriptEngine js = sem.getEngineByExtension("js");
   295         if (eng != null) {
   296             eng[0] = js;
   297         }
   298         try {
   299             Object res = js.eval(sb.toString());
   300             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   301             return (Invocable)js;
   302         } catch (Exception ex) {
   303             if (sb.length() > 2000) {
   304                 sb = dumpJS(sb);
   305             }
   306             fail("Could not evaluate:\n" + sb, ex);
   307             return null;
   308         }
   309     }
   310     static StringBuilder dumpJS(CharSequence sb) throws IOException {
   311         File f = File.createTempFile("execution", ".js");
   312         FileWriter w = new FileWriter(f);
   313         w.append(sb);
   314         w.close();
   315         return new StringBuilder(f.getPath());
   316     }
   317 }