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