jaroslav@706: /** jaroslav@706: * Back 2 Browser Bytecode Translator jaroslav@706: * Copyright (C) 2012 Jaroslav Tulach jaroslav@706: * jaroslav@706: * This program is free software: you can redistribute it and/or modify jaroslav@706: * it under the terms of the GNU General Public License as published by jaroslav@706: * the Free Software Foundation, version 2 of the License. jaroslav@706: * jaroslav@706: * This program is distributed in the hope that it will be useful, jaroslav@706: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@706: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@706: * GNU General Public License for more details. jaroslav@706: * jaroslav@706: * You should have received a copy of the GNU General Public License jaroslav@706: * along with this program. Look for COPYING file in the top folder. jaroslav@706: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@706: */ jaroslav@706: package org.apidesign.bck2brwsr.vmtest.impl; jaroslav@706: jaroslav@706: import java.io.ByteArrayInputStream; jaroslav@706: import java.io.IOException; jaroslav@706: import java.io.InputStream; jaroslav@706: import org.apidesign.bck2brwsr.emul.zip.FastJar; jaroslav@706: import org.testng.annotations.Test; jaroslav@706: import static org.testng.Assert.*; jaroslav@706: jaroslav@706: /** jaroslav@706: * jaroslav@706: * @author Jaroslav Tulach jaroslav@706: */ jaroslav@706: @GenerateZip(name = "five.zip", contents = { jaroslav@706: "1.txt", "one", jaroslav@706: "2.txt", "duo", jaroslav@706: "3.txt", "three", jaroslav@706: "4.txt", "four", jaroslav@706: "5.txt", "five" jaroslav@706: }) jaroslav@706: public class ZipEntryTest { jaroslav@706: @Test jaroslav@706: public void readEntriesEffectively() throws IOException { jaroslav@706: InputStream is = ZipEntryTest.class.getResourceAsStream("five.zip"); jaroslav@706: byte[] arr = new byte[is.available()]; jaroslav@706: int len = is.read(arr); jaroslav@706: assertEquals(len, arr.length, "Read fully"); jaroslav@706: jaroslav@706: FastJar fj = new FastJar(arr); jaroslav@706: FastJar.Entry[] entrs = fj.list(); jaroslav@706: jaroslav@706: assertEquals(5, entrs.length, "Five entries"); jaroslav@706: jaroslav@706: for (int i = 1; i <= 5; i++) { jaroslav@706: FastJar.Entry en = entrs[i - 1]; jaroslav@706: assertEquals(en.name, i + ".txt"); jaroslav@706: // assertEquals(cis.cnt, 0, "Content of the file should be skipped, not read"); jaroslav@706: } jaroslav@706: jaroslav@706: assertContent("three", fj.getInputStream(entrs[3 - 1]), "read OK"); jaroslav@706: assertContent("five", fj.getInputStream(entrs[5 - 1]), "read OK"); jaroslav@706: } jaroslav@706: jaroslav@706: private static void assertContent(String exp, InputStream is, String msg) throws IOException { jaroslav@706: byte[] arr = new byte[512]; jaroslav@706: int len = is.read(arr); jaroslav@706: String s = new String(arr, 0, len); jaroslav@706: assertEquals(exp, s, msg); jaroslav@706: } jaroslav@706: }