rt/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 708 vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java@59d5596a9c6c
child 789 bb7506513353
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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 static org.testng.Assert.*;
    24 import org.testng.annotations.BeforeClass;
    25 import org.testng.annotations.Test;
    26 
    27 /**
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 public class VMinVMTest {
    32     private static TestVM code;
    33     
    34     @Test public void compareGeneratedCodeForArrayClass() throws Exception {
    35         compareCode("org/apidesign/vm4brwsr/Array.class");
    36     }
    37 
    38     @Test public void compareGeneratedCodeForClassesClass() throws Exception {
    39         compareCode("org/apidesign/vm4brwsr/Classes.class");
    40     }
    41 
    42     @BeforeClass
    43     public void compileTheCode() throws Exception {
    44         code = TestVM.compileClass("org/apidesign/vm4brwsr/VMinVM");
    45     }
    46     
    47     private void compareCode(final String nm) throws Exception, IOException {
    48         byte[] arr = BytesLoader.readClass(nm);
    49         String ret1 = VMinVM.toJavaScript(arr);
    50         
    51         Object ret;
    52         try {
    53             ret = code.invokeFunction("bck2brwsr");
    54             ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName());
    55             ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
    56         } catch (Exception ex) {
    57             File f = File.createTempFile("execution", ".js");
    58             FileWriter w = new FileWriter(f);
    59             w.append("var byteCode = [\n  ");
    60             String sep = "";
    61             for (int i = 0; i < arr.length; i++) {
    62                 w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
    63                 sep = ", ";
    64                 if (i % 20 == 0) {
    65                     w.append("\n  ");
    66                 }
    67             }
    68             w.append("\n];\n");
    69             w.append(code.toString());
    70             w.close();
    71             throw new Exception(ex.getMessage() + " file: " + f, ex);
    72         }
    73 
    74         
    75         assertTrue(ret instanceof String, "It is string: " + ret);
    76         
    77         if (!ret1.toString().equals(ret)) {
    78             StringBuilder msg = new StringBuilder("Difference found between ");
    79             msg.append(TestVM.dumpJS(ret1));
    80             msg.append(" ");
    81             msg.append(TestVM.dumpJS((CharSequence) ret));
    82             msg.append(" compiled by ");
    83             msg.append(code.toString());
    84             fail(msg.toString());
    85         }
    86     }
    87 }