vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 03 Jan 2013 11:20:40 +0100
branchTypeNickNames
changeset 405 e41809be6106
parent 334 b5dd05670bef
permissions -rw-r--r--
Using 's' instead of Ljava_lang_String_2
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;
jtulach@169
    23
import static org.testng.Assert.*;
jtulach@169
    24
import javax.script.Invocable;
jtulach@169
    25
import org.testng.annotations.BeforeClass;
jtulach@169
    26
import org.testng.annotations.Test;
jtulach@169
    27
jtulach@169
    28
/**
jtulach@169
    29
 *
jtulach@169
    30
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@169
    31
 */
jtulach@169
    32
public class VMinVMTest {
jtulach@169
    33
jtulach@169
    34
    private static CharSequence codeSeq;
jtulach@169
    35
    private static Invocable code;
jtulach@169
    36
    
jaroslav@302
    37
    @Test public void compareGeneratedCodeForArrayClass() throws Exception {
jaroslav@334
    38
        compareCode("org/apidesign/vm4brwsr/Array.class");
jaroslav@302
    39
    }
jaroslav@303
    40
jaroslav@303
    41
    @Test public void compareGeneratedCodeForClassesClass() throws Exception {
jaroslav@334
    42
        compareCode("org/apidesign/vm4brwsr/Classes.class");
jaroslav@303
    43
    }
jaroslav@334
    44
jaroslav@302
    45
    @BeforeClass
jaroslav@302
    46
    public void compileTheCode() throws Exception {
jaroslav@302
    47
        StringBuilder sb = new StringBuilder();
jaroslav@302
    48
        code = StaticMethodTest.compileClass(sb, 
jaroslav@302
    49
            "org/apidesign/vm4brwsr/VMinVM"
jaroslav@302
    50
        );
jaroslav@302
    51
        codeSeq = sb;
jaroslav@302
    52
    }
jaroslav@302
    53
    
jaroslav@302
    54
    private void compareCode(final String nm) throws Exception, IOException {
jaroslav@334
    55
        byte[] arr = BytesLoader.readClass(nm);
jaroslav@170
    56
        String ret1 = VMinVM.toJavaScript(arr);
jtulach@169
    57
        
jaroslav@170
    58
        Object ret;
jaroslav@170
    59
        try {
jaroslav@279
    60
            ret = code.invokeFunction("bck2brwsr");
jaroslav@279
    61
            ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName());
jaroslav@405
    62
            ret = code.invokeMethod(ret, "toJavaScript__s_3B", arr);
jaroslav@170
    63
        } catch (Exception ex) {
jaroslav@170
    64
            File f = File.createTempFile("execution", ".js");
jaroslav@170
    65
            FileWriter w = new FileWriter(f);
jaroslav@173
    66
            w.append("var byteCode = [\n  ");
jaroslav@173
    67
            String sep = "";
jaroslav@173
    68
            for (int i = 0; i < arr.length; i++) {
jaroslav@173
    69
                w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
jaroslav@173
    70
                sep = ", ";
jaroslav@173
    71
                if (i % 20 == 0) {
jaroslav@173
    72
                    w.append("\n  ");
jaroslav@173
    73
                }
jaroslav@173
    74
            }
jaroslav@173
    75
            w.append("\n];\n");
jaroslav@170
    76
            w.append(codeSeq);
jaroslav@170
    77
            w.close();
jaroslav@170
    78
            throw new Exception(ex.getMessage() + " file: " + f, ex);
jaroslav@170
    79
        }
jaroslav@170
    80
jaroslav@170
    81
        
jtulach@169
    82
        assertTrue(ret instanceof String, "It is string: " + ret);
jtulach@169
    83
        
jaroslav@302
    84
        if (!ret1.toString().equals(ret)) {
jaroslav@303
    85
            StringBuilder msg = new StringBuilder("Difference found between ");
jaroslav@303
    86
            msg.append(StaticMethodTest.dumpJS(ret1));
jaroslav@303
    87
            msg.append(" ");
jaroslav@302
    88
            msg.append(StaticMethodTest.dumpJS((CharSequence) ret));
jaroslav@303
    89
            msg.append(" compiled by ");
jaroslav@303
    90
            msg.append(StaticMethodTest.dumpJS(codeSeq));
jaroslav@302
    91
            fail(msg.toString());
jaroslav@170
    92
        }
jaroslav@170
    93
    }
jtulach@169
    94
}