You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/04/07 12:24:42 UTC

(commons-compress) 02/02: Add tests

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit 3c2c3e70a135b4b5fbabe8ce79543007c17f7d0d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Apr 7 08:24:34 2026 -0400

    Add tests
---
 .../apache/commons/compress/archivers/ZipTest.java | 35 ++++++++++++++++++++++
 .../compress/archivers/sevenz/SevenZFileTest.java  | 23 ++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTest.java b/src/test/java/org/apache/commons/compress/archivers/ZipTest.java
index 9e39f9305..a130c091c 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ZipTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ZipTest.java
@@ -59,6 +59,7 @@
 import org.apache.commons.compress.utils.InputStreamStatistics;
 import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.channels.ByteArraySeekableByteChannel;
 import org.junit.jupiter.api.Test;
 
 public final class ZipTest extends AbstractTest {
@@ -770,6 +771,40 @@ void testZipArchiveCreation() throws Exception {
      */
     @Test
     void testZipArchiveCreationInMemory() throws Exception {
+        final byte[] file1Contents = readAllBytes("test1.xml");
+        final byte[] file2Contents = readAllBytes("test2.xml");
+        final List<byte[]> results = new ArrayList<>();
+        try (ByteArraySeekableByteChannel channel = new ByteArraySeekableByteChannel()) {
+            try (ZipArchiveOutputStream os = new ZipArchiveOutputStream(channel)) {
+                os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
+                os.write(file1Contents);
+                os.closeArchiveEntry();
+
+                os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
+                os.write(file2Contents);
+                os.closeArchiveEntry();
+            }
+            // Unarchive the same
+            try (ZipArchiveInputStream inputStream = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", new ByteArrayInputStream(channel.array()))) {
+                ZipArchiveEntry entry;
+                while ((entry = inputStream.getNextEntry()) != null) {
+                    final byte[] result = new byte[(int) entry.getSize()];
+                    IOUtils.readFully(inputStream, result);
+                    results.add(result);
+                }
+            }
+        }
+        assertArrayEquals(results.get(0), file1Contents);
+        assertArrayEquals(results.get(1), file2Contents);
+    }
+
+    /**
+     * 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
+     *
+     * @throws Exception
+     */
+    @Test
+    void testZipArchiveCreationInMemoryDepreacted() throws Exception {
         final byte[] file1Contents = readAllBytes("test1.xml");
         final byte[] file2Contents = readAllBytes("test2.xml");
         final List<byte[]> results = new ArrayList<>();
diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
index d6a9ac379..7bcedee00 100644
--- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
@@ -62,6 +62,7 @@
 import org.apache.commons.compress.utils.MultiReadOnlySeekableByteChannel;
 import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.channels.ByteArraySeekableByteChannel;
 import org.apache.commons.io.input.ChecksumInputStream;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -337,6 +338,14 @@ void test7zDeflateUnarchive() throws Exception {
 
     @Test
     void test7zMultiVolumeUnarchive() throws Exception {
+        try (SevenZFile sevenZFile = SevenZFile.builder()
+                .setChannel(MultiReadOnlySeekableByteChannel.forFiles(getFile("bla-multi.7z.001"), getFile("bla-multi.7z.002"))).get()) {
+            test7zUnarchive(sevenZFile, SevenZMethod.LZMA2);
+        }
+    }
+
+    @Test
+    void test7zMultiVolumeUnarchiveDeprecated() throws Exception {
         try (SevenZFile sevenZFile = SevenZFile.builder()
                 .setSeekableByteChannel(MultiReadOnlySeekableByteChannel.forFiles(getFile("bla-multi.7z.001"), getFile("bla-multi.7z.002"))).get()) {
             test7zUnarchive(sevenZFile, SevenZMethod.LZMA2);
@@ -539,6 +548,20 @@ void testGetDefaultName() throws Exception {
 
     @Test
     void testGetEntriesOfUnarchiveInMemory() throws IOException {
+        final byte[] data = readAllBytes("bla.7z");
+        try (SevenZFile sevenZFile = SevenZFile.builder().setChannel(ByteArraySeekableByteChannel.wrap(data)).get()) {
+            final Iterable<SevenZArchiveEntry> entries = sevenZFile.getEntries();
+            final Iterator<SevenZArchiveEntry> iter = entries.iterator();
+            SevenZArchiveEntry entry = iter.next();
+            assertEquals("test1.xml", entry.getName());
+            entry = iter.next();
+            assertEquals("test2.xml", entry.getName());
+            assertFalse(iter.hasNext());
+        }
+    }
+
+    @Test
+    void testGetEntriesOfUnarchiveInMemoryDeprecacted() throws IOException {
         final byte[] data = readAllBytes("bla.7z");
         try (SevenZFile sevenZFile = SevenZFile.builder().setSeekableByteChannel(new SeekableInMemoryByteChannel(data)).get()) {
             final Iterable<SevenZArchiveEntry> entries = sevenZFile.getEntries();