vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 17 Nov 2012 11:21:17 +0100
branchjavap
changeset 170 2336c52d3ee5
parent 169 6f2aef4cf160
child 173 2f0205599623
permissions -rw-r--r--
The JavaScript VM is able to generate some (not yet valid) code.
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@170
    44
            ret = code.invokeFunction(
jaroslav@170
    45
                "org_apidesign_vm4brwsr_VMinVM_toJavaScriptLjava_lang_StringAB",
jaroslav@170
    46
                arr
jaroslav@170
    47
            );
jaroslav@170
    48
        } catch (Exception ex) {
jaroslav@170
    49
            File f = File.createTempFile("execution", ".js");
jaroslav@170
    50
            FileWriter w = new FileWriter(f);
jaroslav@170
    51
            w.append(codeSeq);
jaroslav@170
    52
            w.close();
jaroslav@170
    53
            throw new Exception(ex.getMessage() + " file: " + f, ex);
jaroslav@170
    54
        }
jaroslav@170
    55
jaroslav@170
    56
        
jtulach@169
    57
        assertTrue(ret instanceof String, "It is string: " + ret);
jtulach@169
    58
        
jaroslav@170
    59
        assertEquals((String)ret, ret1.toString(), "The code is the same");
jtulach@169
    60
    }
jtulach@169
    61
    
jtulach@169
    62
    @BeforeClass
jtulach@169
    63
    public void compileTheCode() throws Exception {
jtulach@169
    64
        StringBuilder sb = new StringBuilder();
jtulach@169
    65
        code = StaticMethodTest.compileClass(sb, 
jaroslav@170
    66
            "org/apidesign/vm4brwsr/VMinVM"
jtulach@169
    67
        );
jtulach@169
    68
        codeSeq = sb;
jtulach@169
    69
    }
jaroslav@170
    70
    
jaroslav@170
    71
    private static byte[] readClass(String res) throws IOException {
jaroslav@170
    72
        InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
jaroslav@170
    73
        assertNotNull(is1, "Stream found");
jaroslav@170
    74
        byte[] arr = new byte[is1.available()];
jaroslav@170
    75
        int len = is1.read(arr);
jaroslav@170
    76
        is1.close();
jaroslav@170
    77
        if (len != arr.length) {
jaroslav@170
    78
            throw new IOException("Wrong len " + len + " for arr: " + arr.length);
jaroslav@170
    79
        }
jaroslav@170
    80
        return arr;
jaroslav@170
    81
    }
jtulach@169
    82
}