Skip to content

Commit 3c2c3e7

Browse files
committed
Add tests
1 parent c263458 commit 3c2c3e7

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/test/java/org/apache/commons/compress/archivers/ZipTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.apache.commons.compress.utils.InputStreamStatistics;
6060
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
6161
import org.apache.commons.io.IOUtils;
62+
import org.apache.commons.io.channels.ByteArraySeekableByteChannel;
6263
import org.junit.jupiter.api.Test;
6364

6465
public final class ZipTest extends AbstractTest {
@@ -770,6 +771,40 @@ void testZipArchiveCreation() throws Exception {
770771
*/
771772
@Test
772773
void testZipArchiveCreationInMemory() throws Exception {
774+
final byte[] file1Contents = readAllBytes("test1.xml");
775+
final byte[] file2Contents = readAllBytes("test2.xml");
776+
final List<byte[]> results = new ArrayList<>();
777+
try (ByteArraySeekableByteChannel channel = new ByteArraySeekableByteChannel()) {
778+
try (ZipArchiveOutputStream os = new ZipArchiveOutputStream(channel)) {
779+
os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
780+
os.write(file1Contents);
781+
os.closeArchiveEntry();
782+
783+
os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
784+
os.write(file2Contents);
785+
os.closeArchiveEntry();
786+
}
787+
// Unarchive the same
788+
try (ZipArchiveInputStream inputStream = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", new ByteArrayInputStream(channel.array()))) {
789+
ZipArchiveEntry entry;
790+
while ((entry = inputStream.getNextEntry()) != null) {
791+
final byte[] result = new byte[(int) entry.getSize()];
792+
IOUtils.readFully(inputStream, result);
793+
results.add(result);
794+
}
795+
}
796+
}
797+
assertArrayEquals(results.get(0), file1Contents);
798+
assertArrayEquals(results.get(1), file2Contents);
799+
}
800+
801+
/**
802+
* Archives 2 files and unarchives it again. If the file contents of result and source is the same, it looks like the operations have worked
803+
*
804+
* @throws Exception
805+
*/
806+
@Test
807+
void testZipArchiveCreationInMemoryDepreacted() throws Exception {
773808
final byte[] file1Contents = readAllBytes("test1.xml");
774809
final byte[] file2Contents = readAllBytes("test2.xml");
775810
final List<byte[]> results = new ArrayList<>();

src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.commons.compress.utils.MultiReadOnlySeekableByteChannel;
6363
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
6464
import org.apache.commons.io.IOUtils;
65+
import org.apache.commons.io.channels.ByteArraySeekableByteChannel;
6566
import org.apache.commons.io.input.ChecksumInputStream;
6667
import org.junit.jupiter.api.BeforeEach;
6768
import org.junit.jupiter.api.Test;
@@ -337,6 +338,14 @@ void test7zDeflateUnarchive() throws Exception {
337338

338339
@Test
339340
void test7zMultiVolumeUnarchive() throws Exception {
341+
try (SevenZFile sevenZFile = SevenZFile.builder()
342+
.setChannel(MultiReadOnlySeekableByteChannel.forFiles(getFile("bla-multi.7z.001"), getFile("bla-multi.7z.002"))).get()) {
343+
test7zUnarchive(sevenZFile, SevenZMethod.LZMA2);
344+
}
345+
}
346+
347+
@Test
348+
void test7zMultiVolumeUnarchiveDeprecated() throws Exception {
340349
try (SevenZFile sevenZFile = SevenZFile.builder()
341350
.setSeekableByteChannel(MultiReadOnlySeekableByteChannel.forFiles(getFile("bla-multi.7z.001"), getFile("bla-multi.7z.002"))).get()) {
342351
test7zUnarchive(sevenZFile, SevenZMethod.LZMA2);
@@ -539,6 +548,20 @@ void testGetDefaultName() throws Exception {
539548

540549
@Test
541550
void testGetEntriesOfUnarchiveInMemory() throws IOException {
551+
final byte[] data = readAllBytes("bla.7z");
552+
try (SevenZFile sevenZFile = SevenZFile.builder().setChannel(ByteArraySeekableByteChannel.wrap(data)).get()) {
553+
final Iterable<SevenZArchiveEntry> entries = sevenZFile.getEntries();
554+
final Iterator<SevenZArchiveEntry> iter = entries.iterator();
555+
SevenZArchiveEntry entry = iter.next();
556+
assertEquals("test1.xml", entry.getName());
557+
entry = iter.next();
558+
assertEquals("test2.xml", entry.getName());
559+
assertFalse(iter.hasNext());
560+
}
561+
}
562+
563+
@Test
564+
void testGetEntriesOfUnarchiveInMemoryDeprecacted() throws IOException {
542565
final byte[] data = readAllBytes("bla.7z");
543566
try (SevenZFile sevenZFile = SevenZFile.builder().setSeekableByteChannel(new SeekableInMemoryByteChannel(data)).get()) {
544567
final Iterable<SevenZArchiveEntry> entries = sevenZFile.getEntries();

0 commit comments

Comments
 (0)