-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathTarFileTest.java
110 lines (95 loc) · 3.79 KB
/
TarFileTest.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
package org.codehaus.plexus.archiver.tar;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Enumeration;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.TestSupport;
import org.codehaus.plexus.archiver.bzip2.BZip2Compressor;
import org.codehaus.plexus.archiver.gzip.GZipCompressor;
import org.codehaus.plexus.archiver.util.Compressor;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.junit.jupiter.api.Test;
import static org.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
/**
* Test case for {@link TarFile}.
*/
public class TarFileTest extends TestSupport {
private interface TarFileCreator {
TarFile newTarFile(File file) throws IOException;
}
/**
* Test for the uncompressed tar file, {@link TarFile}.
*/
@Test
public void testTarFile() throws Exception {
testTarFile(null, null, new TarFileCreator() {
@Override
public TarFile newTarFile(File file) throws IOException {
return new TarFile(file);
}
});
}
/**
* Test for the gzip compressed tar file, {@link GZipTarFile}.
*/
@Test
public void testGZipTarFile() throws Exception {
final GZipCompressor compressor = new GZipCompressor();
testTarFile(compressor, ".gz", new TarFileCreator() {
@Override
public TarFile newTarFile(File file) throws IOException {
return new GZipTarFile(file);
}
});
}
/**
* Test for the bzip2 compressed tar file, {@link BZip2TarFile}.
*/
@Test
public void testBZip2TarFile() throws Exception {
final BZip2Compressor compressor = new BZip2Compressor();
testTarFile(compressor, ".bz2", new TarFileCreator() {
@Override
public TarFile newTarFile(File file) throws IOException {
return new BZip2TarFile(file);
}
});
}
private void testTarFile(Compressor compressor, String extension, TarFileCreator tarFileCreator) throws Exception {
File file = new File("target/output/TarFileTest.tar");
final TarArchiver archiver = (TarArchiver) lookup(Archiver.class, "tar");
archiver.setLongfile(TarLongFileMode.posix);
archiver.setDestFile(file);
archiver.addDirectory(new File("src"));
FileUtils.removePath(file.getPath());
archiver.createArchive();
if (compressor != null) {
final File compressedFile = new File(file.getPath() + extension);
compressor.setSource(createResource(file, file.getName()));
compressor.setDestFile(compressedFile);
compressor.compress();
compressor.close();
file = compressedFile;
}
final TarFile tarFile = tarFileCreator.newTarFile(file);
for (Enumeration<ArchiveEntry> en = tarFile.getEntries(); en.hasMoreElements(); ) {
final TarArchiveEntry te = (TarArchiveEntry) en.nextElement();
if (te.isDirectory() || te.isSymbolicLink() || te.isLink()) {
continue;
}
final File teFile = new File("src", te.getName());
final InputStream teStream = tarFile.getInputStream(te);
final InputStream fileStream = Files.newInputStream(teFile.toPath());
assertArrayEquals(IOUtil.toByteArray(teStream), IOUtil.toByteArray(fileStream));
teStream.close();
fileStream.close();
}
tarFile.close();
}
}