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; jtulach@169: import static org.testng.Assert.*; 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 { jaroslav@708: private static TestVM code; jtulach@169: jaroslav@302: @Test public void compareGeneratedCodeForArrayClass() throws Exception { jaroslav@334: compareCode("org/apidesign/vm4brwsr/Array.class"); jaroslav@302: } jaroslav@303: jaroslav@303: @Test public void compareGeneratedCodeForClassesClass() throws Exception { jaroslav@334: compareCode("org/apidesign/vm4brwsr/Classes.class"); jaroslav@303: } jaroslav@334: jaroslav@302: @BeforeClass jaroslav@302: public void compileTheCode() throws Exception { jaroslav@708: code = TestVM.compileClass("org/apidesign/vm4brwsr/VMinVM"); jaroslav@302: } jaroslav@302: jaroslav@302: private void compareCode(final String nm) throws Exception, IOException { jaroslav@334: byte[] arr = BytesLoader.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@708: w.append(code.toString()); 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@303: StringBuilder msg = new StringBuilder("Difference found between "); jaroslav@708: msg.append(TestVM.dumpJS(ret1)); jaroslav@303: msg.append(" "); jaroslav@708: msg.append(TestVM.dumpJS((CharSequence) ret)); jaroslav@303: msg.append(" compiled by "); jaroslav@708: msg.append(code.toString()); jaroslav@302: fail(msg.toString()); jaroslav@170: } jaroslav@170: } jtulach@169: }