-
Notifications
You must be signed in to change notification settings - Fork 23
/
ZipUtilsTester.java
executable file
·270 lines (240 loc) · 10.7 KB
/
ZipUtilsTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* #%L
* Netarchivesuite - common - test
* %%
* Copyright (C) 2005 - 2018 The Royal Danish Library,
* the National Library of France and the Austrian National Library.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package dk.netarkivet.common.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import dk.netarkivet.common.exceptions.ArgumentNotValid;
import dk.netarkivet.common.exceptions.IOFailure;
import dk.netarkivet.common.exceptions.PermissionDenied;
import dk.netarkivet.testutils.ReflectUtils;
import dk.netarkivet.testutils.StringAsserts;
import dk.netarkivet.testutils.TestFileUtils;
/**
* Unit tests for the class ZipUtils.
*/
// @SuppressWarnings("resource")
public class ZipUtilsTester {
@Before
public void setUp() throws Exception {
TestFileUtils.copyDirectoryNonCVS(TestInfo.FILEUTILS_DATADIR, TestInfo.TEMPDIR);
}
@After
public void tearDown() throws Exception {
FileUtils.removeRecursively(TestInfo.TEMPDIR);
}
@Test
public void testZipDirectory() throws Exception {
Set<String> files = getFileListNonDirectory();
File zipFile = new File(TestInfo.TEMPDIR, "temp.zip");
assertFalse("Zip file should not exist before", zipFile.exists());
ZipUtils.zipDirectory(TestInfo.ZIPDIR, zipFile);
assertTrue("Zip file must exist afterwards", zipFile.exists());
ZipFile reader = new ZipFile(zipFile);
Enumeration<? extends ZipEntry> entries = reader.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
assertTrue("Zipped file " + entry.getName() + " should be in dir", files.contains(entry.getName()));
File file = new File(TestInfo.ZIPDIR, entry.getName());
assertEquals("Should have same original size as the file", file.length(), entry.getSize());
files.remove(entry.getName());
}
assertTrue("Should have zipped all files, but " + files + " remain", files.isEmpty());
try {
ZipUtils.zipDirectory(null, zipFile);
fail("Should fail on null dir");
} catch (ArgumentNotValid e) {
// Expected
}
try {
ZipUtils.zipDirectory(zipFile, null);
fail("Should fail on null file");
} catch (ArgumentNotValid e) {
// Expected
}
try {
ZipUtils.zipDirectory(zipFile, zipFile);
fail("Should fail on source dir existing as file");
} catch (ArgumentNotValid e) {
// Expected
}
try {
ZipUtils.zipDirectory(TestInfo.ZIPDIR, TestInfo.TEMPDIR);
fail("Should fail on target existing as dir");
} catch (IOFailure e) {
// Expected
}
// Should not fail on existing zipFile
long beforeSize = zipFile.length();
ZipUtils.zipDirectory(TestInfo.DATADIR, zipFile);
assertFalse("Zipfile should have been overwritten", beforeSize == zipFile.length());
}
@Test
public void testUnzip() throws IOException {
File unzipDir = new File(TestInfo.TEMPDIR, "unzip");
assertFalse("Unzipdir should not exist beforehand", unzipDir.exists());
ZipUtils.unzip(TestInfo.ZIPPED_DIR, unzipDir);
assertTrue("Unzipdir should have been created", unzipDir.exists());
Set<String> files = getFileListNonDirectory();
for (String s : new ArrayList<String>(files)) {
if (!s.equals(TestInfo.ZIPPED_DIR.getName())) {
// Can't get zippedDir.zip to contain the right contents
// (at least not easily), so that one is skipped.
File unpackedFile = new File(unzipDir, s);
assertTrue("File " + s + " should exist in unpacked dir", unpackedFile.exists());
File originalFile = new File(TestInfo.ZIPDIR, s);
assertEquals("File " + s + " should have same size in unpacked dir", originalFile.length(),
unpackedFile.length());
assertEquals("MD5 should be the same on old and new file",
ChecksumCalculator.calculateMd5(originalFile), ChecksumCalculator.calculateMd5(unpackedFile));
}
files.remove(s);
}
assertTrue("Should have no files left, but has " + files, files.isEmpty());
FileUtils.removeRecursively(unzipDir);
ZipUtils.unzip(TestInfo.ZIPPED_DIR_WITH_SUBDIRS, unzipDir);
assertTrue("Should have dir a", new File(unzipDir, "a").isDirectory());
assertTrue("Should have file b in a", new File(new File(unzipDir, "a"), "b").isFile());
assertEquals("File b should have length 4", 4, new File(new File(unzipDir, "a"), "b").length());
assertTrue("Should have dir c in b", new File(new File(unzipDir, "a"), "c").isDirectory());
assertTrue("Should have file d in c", new File(new File(new File(unzipDir, "a"), "c"), "d").isFile());
assertEquals("File b should have length 7", 7, new File(new File(new File(unzipDir, "a"), "c"), "d").length());
try {
ZipUtils.unzip(null, unzipDir);
fail("Should fail on null zipfile");
} catch (ArgumentNotValid e) {
// expected
}
try {
ZipUtils.unzip(TestInfo.ZIPPED_DIR, null);
fail("Should fail on null target dir");
} catch (ArgumentNotValid e) {
// expected
}
// Output dir is file
try {
ZipUtils.unzip(TestInfo.ZIPPED_DIR, TestInfo.ZIPPED_DIR);
fail("Should fail on file for target dir");
} catch (PermissionDenied e) {
// expected
}
// Input zip is not a zip file but a dir
try {
ZipUtils.unzip(unzipDir, unzipDir);
fail("Should fail on dir for zipfile");
} catch (IOFailure e) {
// expected
}
// Input file is not a zip file
try {
ZipUtils.unzip(new File(TestInfo.ZIPDIR, "f1.arc"), unzipDir);
fail("Should fail on non-zip zipfile");
} catch (IOFailure e) {
// expected
}
}
private Set<String> getFileListNonDirectory() {
File[] fileList = TestInfo.ZIPDIR.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return !pathname.isDirectory();
}
});
Set<String> files = new HashSet<String>(fileList.length);
for (File f : fileList) {
files.add(f.getName());
}
return files;
}
@Test
public void testGzipFiles() throws Exception {
Method gzipFiles = ReflectUtils.getPrivateMethod(ZipUtils.class, "gzipFiles", File.class, File.class);
File testInputDir = new File(new File(TestInfo.TEMPDIR, "cache"), "cdxindex");
File testOutputDir = new File(TestInfo.TEMPDIR, "gzipped");
gzipFiles.invoke(null, testInputDir, testOutputDir);
assertTrue("Should have output file 1", new File(testOutputDir, "1-cache.gz").exists());
assertTrue("Should have output file 2", new File(testOutputDir, "4-cache.gz").exists());
assertEquals("Should have no tmpDir left", 0, testOutputDir.getParentFile().list(new FilenameFilter() {
public boolean accept(File f, String name) {
return name.contains("tmpDir");
}
}).length);
File internalDir = new File(testInputDir, "aDir");
FileUtils.createDir(internalDir);
FileUtils.removeRecursively(testOutputDir);
gzipFiles.invoke(null, testInputDir, testOutputDir);
assertTrue("Should have output file 1", new File(testOutputDir, "1-cache.gz").exists());
assertTrue("Should have output file 2", new File(testOutputDir, "4-cache.gz").exists());
assertFalse("SHould not have output zipped dir", new File(testOutputDir, "aDir.gz").exists());
assertEquals("Should have no tmpDir left", 0, testOutputDir.getParentFile().list(new FilenameFilter() {
public boolean accept(File f, String name) {
return name.contains("tmpDir");
}
}).length);
try {
gzipFiles.invoke(null, null, testOutputDir);
fail("Should fail on null input dir");
} catch (InvocationTargetException e) {
StringAsserts.assertStringContains("Should mention fromDir", "fromDir", e.getCause().getMessage());
}
try {
gzipFiles.invoke(null, testInputDir, null);
fail("Should fail on null output dir");
} catch (InvocationTargetException e) {
StringAsserts.assertStringContains("Should mention toDir", "toDir", e.getCause().getMessage());
}
try {
gzipFiles.invoke(null, new File(TestInfo.TEMPDIR, "FOO"), testOutputDir);
fail("Should fail on missing input dir");
} catch (InvocationTargetException e) {
StringAsserts.assertStringContains("Should mention fromDir", "FOO", e.getCause().getMessage());
}
try {
gzipFiles.invoke(null, testInputDir, TestInfo.TEMPDIR);
fail("Should fail on existing output dir");
} catch (InvocationTargetException e) {
StringAsserts.assertStringContains("Should mention toDir", TestInfo.TEMPDIR.getName(), e.getCause()
.getMessage());
}
assertEquals("Should have no tmpDir left", 0, testOutputDir.getParentFile().list(new FilenameFilter() {
public boolean accept(File f, String name) {
return name.contains("tmpDir");
}
}).length);
}
}