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.
     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 compareTheGeneratedCode() throws Exception {
    39         byte[] arr = readClass("/org/apidesign/vm4brwsr/Array.class");
    40         String ret1 = VMinVM.toJavaScript(arr);
    41         
    42         Object ret;
    43         try {
    44             ret = code.invokeFunction(
    45                 "org_apidesign_vm4brwsr_VMinVM_toJavaScriptLjava_lang_StringAB",
    46                 arr
    47             );
    48         } catch (Exception ex) {
    49             File f = File.createTempFile("execution", ".js");
    50             FileWriter w = new FileWriter(f);
    51             w.append(codeSeq);
    52             w.close();
    53             throw new Exception(ex.getMessage() + " file: " + f, ex);
    54         }
    55 
    56         
    57         assertTrue(ret instanceof String, "It is string: " + ret);
    58         
    59         assertEquals((String)ret, ret1.toString(), "The code is the same");
    60     }
    61     
    62     @BeforeClass
    63     public void compileTheCode() throws Exception {
    64         StringBuilder sb = new StringBuilder();
    65         code = StaticMethodTest.compileClass(sb, 
    66             "org/apidesign/vm4brwsr/VMinVM"
    67         );
    68         codeSeq = sb;
    69     }
    70     
    71     private static byte[] readClass(String res) throws IOException {
    72         InputStream is1 = VMinVMTest.class.getResourceAsStream(res);
    73         assertNotNull(is1, "Stream found");
    74         byte[] arr = new byte[is1.available()];
    75         int len = is1.read(arr);
    76         is1.close();
    77         if (len != arr.length) {
    78             throw new IOException("Wrong len " + len + " for arr: " + arr.length);
    79         }
    80         return arr;
    81     }
    82 }