jaroslav@611: /** jaroslav@611: * Back 2 Browser Bytecode Translator jaroslav@611: * Copyright (C) 2012 Jaroslav Tulach jaroslav@611: * jaroslav@611: * This program is free software: you can redistribute it and/or modify jaroslav@611: * it under the terms of the GNU General Public License as published by jaroslav@611: * the Free Software Foundation, version 2 of the License. jaroslav@611: * jaroslav@611: * This program is distributed in the hope that it will be useful, jaroslav@611: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@611: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@611: * GNU General Public License for more details. jaroslav@611: * jaroslav@611: * You should have received a copy of the GNU General Public License jaroslav@611: * along with this program. Look for COPYING file in the top folder. jaroslav@611: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@611: */ jaroslav@611: package org.apidesign.bck2brwsr.vmtest.impl; jaroslav@611: jaroslav@611: import java.io.IOException; jaroslav@611: import java.io.InputStream; jaroslav@640: import java.util.Objects; jaroslav@611: import java.util.zip.ZipEntry; jaroslav@611: import java.util.zip.ZipInputStream; jaroslav@640: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@640: import org.apidesign.bck2brwsr.vmtest.BrwsrTest; jaroslav@611: import org.apidesign.bck2brwsr.vmtest.Compare; jaroslav@667: import org.apidesign.bck2brwsr.vmtest.Http; jaroslav@611: import org.apidesign.bck2brwsr.vmtest.VMTest; jaroslav@611: import org.testng.annotations.Factory; jaroslav@611: jaroslav@611: /** jaroslav@611: * jaroslav@611: * @author Jaroslav Tulach jaroslav@611: */ jaroslav@640: @GenerateZip(name = "readAnEntry.zip", contents = { jaroslav@640: "my/main/file.txt", "Hello World!" jaroslav@640: }) jaroslav@611: public class ZipFileTest { jaroslav@611: jaroslav@611: @Compare public String readAnEntry() throws IOException { jaroslav@611: InputStream is = ZipFileTest.class.getResourceAsStream("readAnEntry.zip"); jaroslav@611: ZipInputStream zip = new ZipInputStream(is); jaroslav@611: ZipEntry entry = zip.getNextEntry(); jaroslav@611: assertEquals(entry.getName(), "my/main/file.txt", "Correct entry"); jaroslav@640: jaroslav@611: byte[] arr = new byte[4096]; jaroslav@611: int len = zip.read(arr); jaroslav@611: jaroslav@640: assertEquals(zip.getNextEntry(), null, "No next entry"); jaroslav@640: jaroslav@640: final String ret = new String(arr, 0, len, "UTF-8"); jaroslav@640: return ret; jaroslav@611: } jaroslav@640: jaroslav@644: @JavaScriptBody(args = { "res", "path" }, body = jaroslav@644: "var myvm = new bck2brwsr(path);\n" jaroslav@644: + "var cls = myvm.loadClass('java.lang.String');\n" jaroslav@644: + "return cls.getClass__Ljava_lang_Class_2().getResourceAsStream__Ljava_io_InputStream_2Ljava_lang_String_2(res);\n" jaroslav@644: ) jaroslav@644: private static native Object loadVMResource(String res, String...path); jaroslav@644: jaroslav@667: @Http({ jaroslav@667: @Http.Resource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip") jaroslav@667: }) jaroslav@644: @BrwsrTest public void canVmLoadResourceFromZip() throws IOException { jaroslav@645: Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar"); jaroslav@644: assert res instanceof InputStream : "Got array of bytes: " + res; jaroslav@644: InputStream is = (InputStream)res; jaroslav@644: jaroslav@644: byte[] arr = new byte[4096]; jaroslav@644: int len = is.read(arr); jaroslav@644: jaroslav@644: final String ret = new String(arr, 0, len, "UTF-8"); jaroslav@644: jaroslav@644: assertEquals(ret, "Hello World!", "Can read the bytes"); jaroslav@644: } jaroslav@640: jaroslav@640: private static void assertEquals(Object real, Object exp, String msg) { jaroslav@640: assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real; jaroslav@611: } jaroslav@611: jaroslav@611: @Factory public static Object[] create() { jaroslav@611: return VMTest.create(ZipFileTest.class); jaroslav@611: } jaroslav@611: }