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
jtulach@169
     1
/**
jtulach@169
     2
 * Back 2 Browser Bytecode Translator
jtulach@169
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@169
     4
 *
jtulach@169
     5
 * This program is free software: you can redistribute it and/or modify
jtulach@169
     6
 * it under the terms of the GNU General Public License as published by
jtulach@169
     7
 * the Free Software Foundation, version 2 of the License.
jtulach@169
     8
 *
jtulach@169
     9
 * This program is distributed in the hope that it will be useful,
jtulach@169
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jtulach@169
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jtulach@169
    12
 * GNU General Public License for more details.
jtulach@169
    13
 *
jtulach@169
    14
 * You should have received a copy of the GNU General Public License
jtulach@169
    15
 * along with this program. Look for COPYING file in the top folder.
jtulach@169
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jtulach@169
    17
 */
jtulach@169
    18
package org.apidesign.vm4brwsr;
jtulach@169
    19
jaroslav@170
    20
import java.io.File;
jaroslav@170
    21
import java.io.FileWriter;
jaroslav@170
    22
import java.io.IOException;
jaroslav@170
    23
import java.io.InputStream;
jtulach@169
    24
import static org.testng.Assert.*;
jtulach@169
    25
import javax.script.Invocable;
jtulach@169
    26
import org.testng.annotations.BeforeClass;
jtulach@169
    27
import org.testng.annotations.Test;
jtulach@169
    28
jtulach@169
    29
/**
jtulach@169
    30
 *
jtulach@169
    31
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@169
    32
 */
jtulach@169
    33
public class VMinVMTest {
jtulach@169
    34
jtulach@169
    35
    private static CharSequence codeSeq;
jtulach@169
    36
    private static Invocable code;
jtulach@169
    37
    
jtulach@169
    38
    @Test public void compareTheGeneratedCode() throws Exception {
jaroslav@170
    39
        byte[] arr = readClass("/org/apidesign/vm4brwsr/Array.class");
jaroslav@170
    40
        String ret1 = VMinVM.toJavaScript(arr);
jtulach@169
    41
        
jaroslav@170
    42
        Object ret;
jaroslav@170
    43
        try {
jaroslav@205
    44
            ret = code.invokeFunction(VMinVM.class.getName().replace('.', '_'), true);
jaroslav@248
    45
            ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
jaroslav@170
    46
        } catch (Exception ex) {
jaroslav@170
    47
            File f = File.createTempFile("execution", ".js");
jaroslav@170
    48
            FileWriter w = new FileWriter(f);
jaroslav@173
    49
            w.append("var byteCode = [\n  ");
jaroslav@173
    50
            String sep = "";
jaroslav@173
    51
            for (int i = 0; i < arr.length; i++) {
jaroslav@173
    52
                w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
jaroslav@173
    53
                sep = ", ";
jaroslav@173
    54
                if (i % 20 == 0) {
jaroslav@173
    55
                    w.append("\n  ");
jaroslav@173
    56
                }
jaroslav@173
    57
            }
jaroslav@173
    58
            w.append("\n];\n");
jaroslav@170
    59
            w.append(codeSeq);
jaroslav@170
    60
            w.close();
jaroslav@170
    61
            throw new Exception(ex.getMessage() + " file: " + f, ex);
jaroslav@170
    62
        }
jaroslav@170
    63
jaroslav@170
    64
        
jtulach@169
    65
        assertTrue(ret instanceof String, "It is string: " + ret);
jtulach@169
    66
        
jaroslav@170
    67
        assertEquals((String)ret, ret1.toString(), "The code is the same");
jtulach@169
    68
    }
jtulach@169
    69
    
jtulach@169
    70
    @BeforeClass
jtulach@169
    71
    public void compileTheCode() throws Exception {
jtulach@169
    72
        StringBuilder sb = new StringBuilder();
jtulach@169
    73
        code = StaticMethodTest.compileClass(sb, 
jaroslav@170
    74
            "org/apidesign/vm4brwsr/VMinVM"
jtulach@169
    75
        );
jtulach@169
    76
        codeSeq = sb;
jtulach@169
    77
    }
jaroslav@170
    78
    
jaroslav@170
    79
    private static byte[] readClass(String res) throws IOException {
jaroslav@170
    80
        InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
jaroslav@170
    81
        assertNotNull(is1, "Stream found");
jaroslav@170
    82
        byte[] arr = new byte[is1.available()];
jaroslav@170
    83
        int len = is1.read(arr);
jaroslav@170
    84
        is1.close();
jaroslav@170
    85
        if (len != arr.length) {
jaroslav@170
    86
            throw new IOException("Wrong len " + len + " for arr: " + arr.length);
jaroslav@170
    87
        }
jaroslav@170
    88
        return arr;
jaroslav@170
    89
    }
jtulach@169
    90
}