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: jaroslav@302: @Test public void compareGeneratedCodeForArrayClass() throws Exception { jaroslav@302: compareCode("/org/apidesign/vm4brwsr/Array.class"); jaroslav@302: } jaroslav@302: jaroslav@302: @BeforeClass jaroslav@302: public void compileTheCode() throws Exception { jaroslav@302: StringBuilder sb = new StringBuilder(); jaroslav@302: code = StaticMethodTest.compileClass(sb, jaroslav@302: "org/apidesign/vm4brwsr/VMinVM" jaroslav@302: ); jaroslav@302: codeSeq = sb; jaroslav@302: } jaroslav@302: jaroslav@302: private static byte[] readClass(String res) throws IOException { jaroslav@302: InputStream is1 = VMinVMTest.class.getResourceAsStream(res); jaroslav@302: assertNotNull(is1, "Stream found"); jaroslav@302: byte[] arr = new byte[is1.available()]; jaroslav@302: int len = is1.read(arr); jaroslav@302: is1.close(); jaroslav@302: if (len != arr.length) { jaroslav@302: throw new IOException("Wrong len " + len + " for arr: " + arr.length); jaroslav@302: } jaroslav@302: return arr; jaroslav@302: } jaroslav@302: jaroslav@302: private void compareCode(final String nm) throws Exception, IOException { jaroslav@302: byte[] arr = readClass(nm); 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@302: if (!ret1.toString().equals(ret)) { jaroslav@302: StringBuilder msg = StaticMethodTest.dumpJS(ret1); jaroslav@302: msg.append(" is not the same as "); jaroslav@302: msg.append(StaticMethodTest.dumpJS((CharSequence) ret)); jaroslav@302: fail(msg.toString()); jaroslav@170: } jaroslav@170: } jtulach@169: }