vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 04 Dec 2012 09:16:53 +0100
changeset 248 0bfcb6585290
parent 205 0256fac49ea5
child 279 ee34358037b3
permissions -rw-r--r--
Using the same mangling scheme as JNI
     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 java.io.InputStream;
    24 import static org.testng.Assert.*;
    25 import javax.script.Invocable;
    26 import org.testng.annotations.BeforeClass;
    27 import org.testng.annotations.Test;
    28 
    29 /**
    30  *
    31  * @author Jaroslav Tulach <jtulach@netbeans.org>
    32  */
    33 public class VMinVMTest {
    34 
    35     private static CharSequence codeSeq;
    36     private static Invocable code;
    37     
    38     @Test public void compareTheGeneratedCode() throws Exception {
    39         byte[] arr = readClass("/org/apidesign/vm4brwsr/Array.class");
    40         String ret1 = VMinVM.toJavaScript(arr);
    41         
    42         Object ret;
    43         try {
    44             ret = code.invokeFunction(VMinVM.class.getName().replace('.', '_'), true);
    45             ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
    46         } catch (Exception ex) {
    47             File f = File.createTempFile("execution", ".js");
    48             FileWriter w = new FileWriter(f);
    49             w.append("var byteCode = [\n  ");
    50             String sep = "";
    51             for (int i = 0; i < arr.length; i++) {
    52                 w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
    53                 sep = ", ";
    54                 if (i % 20 == 0) {
    55                     w.append("\n  ");
    56                 }
    57             }
    58             w.append("\n];\n");
    59             w.append(codeSeq);
    60             w.close();
    61             throw new Exception(ex.getMessage() + " file: " + f, ex);
    62         }
    63 
    64         
    65         assertTrue(ret instanceof String, "It is string: " + ret);
    66         
    67         assertEquals((String)ret, ret1.toString(), "The code is the same");
    68     }
    69     
    70     @BeforeClass
    71     public void compileTheCode() throws Exception {
    72         StringBuilder sb = new StringBuilder();
    73         code = StaticMethodTest.compileClass(sb, 
    74             "org/apidesign/vm4brwsr/VMinVM"
    75         );
    76         codeSeq = sb;
    77     }
    78     
    79     private static byte[] readClass(String res) throws IOException {
    80         InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
    81         assertNotNull(is1, "Stream found");
    82         byte[] arr = new byte[is1.available()];
    83         int len = is1.read(arr);
    84         is1.close();
    85         if (len != arr.length) {
    86             throw new IOException("Wrong len " + len + " for arr: " + arr.length);
    87         }
    88         return arr;
    89     }
    90 }