jaroslav@706
|
1 |
/**
|
jaroslav@706
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@706
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@706
|
4 |
*
|
jaroslav@706
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@706
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@706
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@706
|
8 |
*
|
jaroslav@706
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@706
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@706
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@706
|
12 |
* GNU General Public License for more details.
|
jaroslav@706
|
13 |
*
|
jaroslav@706
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@706
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@706
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@706
|
17 |
*/
|
jaroslav@706
|
18 |
package org.apidesign.bck2brwsr.vmtest.impl;
|
jaroslav@706
|
19 |
|
jaroslav@706
|
20 |
import java.io.ByteArrayInputStream;
|
jaroslav@706
|
21 |
import java.io.IOException;
|
jaroslav@706
|
22 |
import java.io.InputStream;
|
jaroslav@706
|
23 |
import org.apidesign.bck2brwsr.emul.zip.FastJar;
|
jaroslav@706
|
24 |
import org.testng.annotations.Test;
|
jaroslav@706
|
25 |
import static org.testng.Assert.*;
|
jaroslav@706
|
26 |
|
jaroslav@706
|
27 |
/**
|
jaroslav@706
|
28 |
*
|
jaroslav@706
|
29 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@706
|
30 |
*/
|
jaroslav@706
|
31 |
@GenerateZip(name = "five.zip", contents = {
|
jaroslav@706
|
32 |
"1.txt", "one",
|
jaroslav@706
|
33 |
"2.txt", "duo",
|
jaroslav@706
|
34 |
"3.txt", "three",
|
jaroslav@706
|
35 |
"4.txt", "four",
|
jaroslav@706
|
36 |
"5.txt", "five"
|
jaroslav@706
|
37 |
})
|
jaroslav@706
|
38 |
public class ZipEntryTest {
|
jaroslav@706
|
39 |
@Test
|
jaroslav@706
|
40 |
public void readEntriesEffectively() throws IOException {
|
jaroslav@706
|
41 |
InputStream is = ZipEntryTest.class.getResourceAsStream("five.zip");
|
jaroslav@706
|
42 |
byte[] arr = new byte[is.available()];
|
jaroslav@706
|
43 |
int len = is.read(arr);
|
jaroslav@706
|
44 |
assertEquals(len, arr.length, "Read fully");
|
jaroslav@706
|
45 |
|
jaroslav@706
|
46 |
FastJar fj = new FastJar(arr);
|
jaroslav@706
|
47 |
FastJar.Entry[] entrs = fj.list();
|
jaroslav@706
|
48 |
|
jaroslav@706
|
49 |
assertEquals(5, entrs.length, "Five entries");
|
jaroslav@706
|
50 |
|
jaroslav@706
|
51 |
for (int i = 1; i <= 5; i++) {
|
jaroslav@706
|
52 |
FastJar.Entry en = entrs[i - 1];
|
jaroslav@706
|
53 |
assertEquals(en.name, i + ".txt");
|
jaroslav@706
|
54 |
// assertEquals(cis.cnt, 0, "Content of the file should be skipped, not read");
|
jaroslav@706
|
55 |
}
|
jaroslav@706
|
56 |
|
jaroslav@706
|
57 |
assertContent("three", fj.getInputStream(entrs[3 - 1]), "read OK");
|
jaroslav@706
|
58 |
assertContent("five", fj.getInputStream(entrs[5 - 1]), "read OK");
|
jaroslav@706
|
59 |
}
|
jaroslav@706
|
60 |
|
jaroslav@706
|
61 |
private static void assertContent(String exp, InputStream is, String msg) throws IOException {
|
jaroslav@706
|
62 |
byte[] arr = new byte[512];
|
jaroslav@706
|
63 |
int len = is.read(arr);
|
jaroslav@706
|
64 |
String s = new String(arr, 0, len);
|
jaroslav@706
|
65 |
assertEquals(exp, s, msg);
|
jaroslav@706
|
66 |
}
|
jaroslav@706
|
67 |
}
|