1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.tools; |
17 | |
18 | import java.io.BufferedInputStream; |
19 | import java.io.File; |
20 | import java.io.IOException; |
21 | import java.io.InputStream; |
22 | import java.util.Enumeration; |
23 | import java.util.zip.ZipException; |
24 | |
25 | import junit.framework.TestCase; |
26 | |
27 | import org.apache.commons.logging.Log; |
28 | import org.apache.commons.logging.LogFactory; |
29 | |
30 | import org.apache.tools.zip.ZipEntry; |
31 | import org.apache.tools.zip.ZipFile; |
32 | |
33 | /** |
34 | * TestCase for alternative Zip implementation taken from Ant that allows to specify encoding of |
35 | * file names. |
36 | * |
37 | * @author Thomas Aglassinger |
38 | */ |
39 | public class AntZipTest extends TestCase |
40 | { |
41 | private static final int BUFFER_SIZE = 4096; |
42 | private static Log logger = LogFactory.getLog(AntZipTest.class); |
43 | private TestTools testTools; |
44 | |
45 | protected void setUp() |
46 | throws Exception { |
47 | super.setUp(); |
48 | testTools = TestTools.instance(); |
49 | } |
50 | |
51 | public void testUnzipBrokenDirectoryIndex() |
52 | throws IOException { |
53 | try { |
54 | testUnzip("broken_archive_internal_zip_error.cbz"); |
55 | fail("broken archive must result in ZipException"); |
56 | } catch (ZipException error) { |
57 | if (logger.isInfoEnabled()) { |
58 | logger.info("ignoring expected error", error); |
59 | } |
60 | } |
61 | } |
62 | |
63 | public void testUnzipNonAsciiNames() |
64 | throws IOException { |
65 | testUnzip("test_no_stream_closed.cbz"); |
66 | } |
67 | |
68 | protected void tearDown() |
69 | throws Exception { |
70 | testTools = null; |
71 | super.tearDown(); |
72 | } |
73 | |
74 | private void testUnzip(String baseFileName) |
75 | throws IOException { |
76 | File testFileToUnzip = testTools.getTestFile(baseFileName); |
77 | ZipFile zipFile = new ZipFile(testFileToUnzip); |
78 | |
79 | try { |
80 | Enumeration zipEntries = zipFile.getEntries(); |
81 | |
82 | while (zipEntries.hasMoreElements()) { |
83 | ZipEntry entry = (ZipEntry) zipEntries.nextElement(); |
84 | String name = entry.getName(); |
85 | |
86 | if (logger.isInfoEnabled()) { |
87 | logger.info("extract \"" + name + "\""); |
88 | } |
89 | if (!entry.isDirectory()) { |
90 | InputStream in = zipFile.getInputStream(entry); |
91 | |
92 | assertNotNull("in", in); |
93 | try { |
94 | BufferedInputStream inBuffered = new BufferedInputStream(in); |
95 | |
96 | try { |
97 | byte[] data = new byte[BUFFER_SIZE]; |
98 | |
99 | while (inBuffered.read(data, 0, BUFFER_SIZE) != -1) { |
100 | logger.debug("discard data"); |
101 | } |
102 | } finally { |
103 | inBuffered.close(); |
104 | } |
105 | } finally { |
106 | in.close(); |
107 | } |
108 | } |
109 | } |
110 | } finally { |
111 | zipFile.close(); |
112 | } |
113 | } |
114 | |
115 | } |