jtulach@169: /** jtulach@169: * Back 2 Browser Bytecode Translator jtulach@169: * Copyright (C) 2012 Jaroslav Tulach jtulach@169: * jtulach@169: * This program is free software: you can redistribute it and/or modify jtulach@169: * it under the terms of the GNU General Public License as published by jtulach@169: * the Free Software Foundation, version 2 of the License. jtulach@169: * jtulach@169: * This program is distributed in the hope that it will be useful, jtulach@169: * but WITHOUT ANY WARRANTY; without even the implied warranty of jtulach@169: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jtulach@169: * GNU General Public License for more details. jtulach@169: * jtulach@169: * You should have received a copy of the GNU General Public License jtulach@169: * along with this program. Look for COPYING file in the top folder. jtulach@169: * If not, see http://opensource.org/licenses/GPL-2.0. jtulach@169: */ jtulach@169: package org.apidesign.vm4brwsr; jtulach@169: jaroslav@170: import java.io.File; jaroslav@170: import java.io.FileWriter; jaroslav@170: import java.io.IOException; jaroslav@170: import java.io.InputStream; jtulach@169: import static org.testng.Assert.*; jtulach@169: import javax.script.Invocable; jtulach@169: import org.testng.annotations.BeforeClass; jtulach@169: import org.testng.annotations.Test; jtulach@169: jtulach@169: /** jtulach@169: * jtulach@169: * @author Jaroslav Tulach jtulach@169: */ jtulach@169: public class VMinVMTest { jtulach@169: jtulach@169: private static CharSequence codeSeq; jtulach@169: private static Invocable code; jtulach@169: jtulach@169: @Test public void compareTheGeneratedCode() throws Exception { jaroslav@170: byte[] arr = readClass("/org/apidesign/vm4brwsr/Array.class"); jaroslav@170: String ret1 = VMinVM.toJavaScript(arr); jtulach@169: jaroslav@170: Object ret; jaroslav@170: try { jaroslav@279: ret = code.invokeFunction("bck2brwsr"); jaroslav@279: ret = code.invokeMethod(ret, "loadClass", VMinVM.class.getName()); jaroslav@248: ret = code.invokeMethod(ret, "toJavaScript__Ljava_lang_String_2_3B", arr); jaroslav@170: } catch (Exception ex) { jaroslav@170: File f = File.createTempFile("execution", ".js"); jaroslav@170: FileWriter w = new FileWriter(f); jaroslav@173: w.append("var byteCode = [\n "); jaroslav@173: String sep = ""; jaroslav@173: for (int i = 0; i < arr.length; i++) { jaroslav@173: w.append(sep).append(Integer.toString((arr[i] + 256) % 256)); jaroslav@173: sep = ", "; jaroslav@173: if (i % 20 == 0) { jaroslav@173: w.append("\n "); jaroslav@173: } jaroslav@173: } jaroslav@173: w.append("\n];\n"); jaroslav@170: w.append(codeSeq); jaroslav@170: w.close(); jaroslav@170: throw new Exception(ex.getMessage() + " file: " + f, ex); jaroslav@170: } jaroslav@170: jaroslav@170: jtulach@169: assertTrue(ret instanceof String, "It is string: " + ret); jtulach@169: jaroslav@170: assertEquals((String)ret, ret1.toString(), "The code is the same"); jtulach@169: } jtulach@169: jtulach@169: @BeforeClass jtulach@169: public void compileTheCode() throws Exception { jtulach@169: StringBuilder sb = new StringBuilder(); jtulach@169: code = StaticMethodTest.compileClass(sb, jaroslav@170: "org/apidesign/vm4brwsr/VMinVM" jtulach@169: ); jtulach@169: codeSeq = sb; jtulach@169: } jaroslav@170: jaroslav@170: private static byte[] readClass(String res) throws IOException { jaroslav@170: InputStream is1 = VMinVMTest.class.getResourceAsStream(res); jaroslav@170: assertNotNull(is1, "Stream found"); jaroslav@170: byte[] arr = new byte[is1.available()]; jaroslav@170: int len = is1.read(arr); jaroslav@170: is1.close(); jaroslav@170: if (len != arr.length) { jaroslav@170: throw new IOException("Wrong len " + len + " for arr: " + arr.length); jaroslav@170: } jaroslav@170: return arr; jaroslav@170: } jtulach@169: }