vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 11 Dec 2012 10:24:00 +0100
branchlazyvm
changeset 302 af9c8df8a660
parent 279 ee34358037b3
child 303 c12342170235
permissions -rw-r--r--
More flexible infrastructure for comparing generated codes
     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 java.io.InputStream;
    24 import static org.testng.Assert.*;
    25 import javax.script.Invocable;
    26 import org.testng.annotations.BeforeClass;
    27 import org.testng.annotations.Test;
    28 
    29 /**
    30  *
    31  * @author Jaroslav Tulach <jtulach@netbeans.org>
    32  */
    33 public class VMinVMTest {
    34 
    35     private static CharSequence codeSeq;
    36     private static Invocable code;
    37     
    38     @Test public void compareGeneratedCodeForArrayClass() throws Exception {
    39         compareCode("/org/apidesign/vm4brwsr/Array.class");
    40     }
    41     
    42     @BeforeClass
    43     public void compileTheCode() throws Exception {
    44         StringBuilder sb = new StringBuilder();
    45         code = StaticMethodTest.compileClass(sb, 
    46             "org/apidesign/vm4brwsr/VMinVM"
    47         );
    48         codeSeq = sb;
    49     }
    50     
    51     private static byte[] readClass(String res) throws IOException {
    52         InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
    53         assertNotNull(is1, "Stream found");
    54         byte[] arr = new byte[is1.available()];
    55         int len = is1.read(arr);
    56         is1.close();
    57         if (len != arr.length) {
    58             throw new IOException("Wrong len " + len + " for arr: " + arr.length);
    59         }
    60         return arr;
    61     }
    62 
    63     private void compareCode(final String nm) throws Exception, IOException {
    64         byte[] arr = readClass(nm);
    65         String ret1 = VMinVM.toJavaScript(arr);
    66         
    67         Object ret;
    68         try {
    69             ret = code.invokeFunction("bck2brwsr");
    70             ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName());
    71             ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr);
    72         } catch (Exception ex) {
    73             File f = File.createTempFile("execution", ".js");
    74             FileWriter w = new FileWriter(f);
    75             w.append("var byteCode = [\n  ");
    76             String sep = "";
    77             for (int i = 0; i < arr.length; i++) {
    78                 w.append(sep).append(Integer.toString((arr[i] + 256) % 256));
    79                 sep = ", ";
    80                 if (i % 20 == 0) {
    81                     w.append("\n  ");
    82                 }
    83             }
    84             w.append("\n];\n");
    85             w.append(codeSeq);
    86             w.close();
    87             throw new Exception(ex.getMessage() + " file: " + f, ex);
    88         }
    89 
    90         
    91         assertTrue(ret instanceof String, "It is string: " + ret);
    92         
    93         if (!ret1.toString().equals(ret)) {
    94             StringBuilder msg = StaticMethodTest.dumpJS(ret1);
    95             msg.append(" is not the same as ");
    96             msg.append(StaticMethodTest.dumpJS((CharSequence) ret));
    97             fail(msg.toString());
    98         }
    99     }
   100 }