You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2018/05/11 09:25:35 UTC

commons-compress git commit: unit tests for Archiver

Repository: commons-compress
Updated Branches:
  refs/heads/master 6edf2fc72 -> 789c39551


unit tests for Archiver


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/789c3955
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/789c3955
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/789c3955

Branch: refs/heads/master
Commit: 789c39551ea74f8b2f8ac754d20a1c7ff3ab55b5
Parents: 6edf2fc
Author: Stefan Bodewig <bo...@apache.org>
Authored: Fri May 11 11:25:18 2018 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Fri May 11 11:25:18 2018 +0200

----------------------------------------------------------------------
 .../compress/archivers/examples/Archiver.java   |   1 +
 .../examples/ParameterizedArchiverTest.java     | 153 +++++++++++++++++++
 .../archivers/examples/SevenZArchiverTest.java  | 124 +++++++++++++++
 3 files changed, 278 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/789c3955/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java b/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java
index 4c43bb9..f85538a 100644
--- a/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java
+++ b/src/main/java/org/apache/commons/compress/archivers/examples/Archiver.java
@@ -117,6 +117,7 @@ public class Archiver {
         } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
             create(new SevenZOutputFile(target), directory);
         } else {
+            // never reached as prefersSeekableByteChannel only returns true fpr ZIP and 7z
             throw new ArchiveException("don't know how to handle format " + format);
         }
     }

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/789c3955/src/test/java/org/apache/commons/compress/archivers/examples/ParameterizedArchiverTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/archivers/examples/ParameterizedArchiverTest.java b/src/test/java/org/apache/commons/compress/archivers/examples/ParameterizedArchiverTest.java
new file mode 100644
index 0000000..2267610
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress/archivers/examples/ParameterizedArchiverTest.java
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.archivers.examples;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.channels.FileChannel;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.commons.compress.AbstractTestCase;
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.compress.utils.IOUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized.Parameters;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class ParameterizedArchiverTest extends AbstractTestCase {
+
+    // can't test 7z here as 7z cannot write to non-seekable streams
+    // and reading logic would be different as well - see
+    // SevenZArchiverTest class
+    @Parameters(name = "format={0}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(
+            new Object[] { "tar" },
+            new Object[] { "cpio" },
+            new Object[] { "zip" }
+        );
+    }
+
+    private final String format;
+    private File target;
+
+    public ParameterizedArchiverTest(String format) {
+        this.format = format;
+    }
+
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        File c = new File(dir, "a/b/c");
+        c.mkdirs();
+        try (OutputStream os = Files.newOutputStream(new File(dir, "a/b/d.txt").toPath())) {
+            os.write("Hello, world 1".getBytes(StandardCharsets.UTF_8));
+        }
+        try (OutputStream os = Files.newOutputStream(new File(dir, "a/b/c/e.txt").toPath())) {
+            os.write("Hello, world 2".getBytes(StandardCharsets.UTF_8));
+        }
+        target = new File(resultDir, "test." + format);
+    }
+
+    @Test
+    public void fileVersion() throws IOException, ArchiveException {
+        new Archiver().create(format, target, dir);
+        verifyContent();
+    }
+
+    @Test
+    public void outputStreamVersion() throws IOException, ArchiveException {
+        try (OutputStream os = Files.newOutputStream(target.toPath())) {
+            new Archiver().create(format, os, dir);
+        }
+        verifyContent();
+    }
+
+    @Test
+    public void channelVersion() throws IOException, ArchiveException {
+        try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
+            StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
+            new Archiver().create(format, c, dir);
+        }
+        verifyContent();
+    }
+
+    @Test
+    public void archiveStreamVersion() throws IOException, ArchiveException {
+        try (OutputStream os = Files.newOutputStream(target.toPath());
+             ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream(format, os)) {
+            new Archiver().create(aos, dir);
+        }
+        verifyContent();
+    }
+
+    private void verifyContent() throws IOException, ArchiveException {
+        try (InputStream is = Files.newInputStream(target.toPath());
+             BufferedInputStream bis = new BufferedInputStream(is);
+             ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(format, bis)) {
+            assertDir("a", ais.getNextEntry());
+            assertDir("a/b", ais.getNextEntry());
+            ArchiveEntry n = ais.getNextEntry();
+            Assert.assertNotNull(n);
+            // File.list may return a/b/c or a/b/d.txt first
+            if (n.getName().endsWith("/")) {
+                assertDir("a/b/c", n);
+                assertHelloWorld("a/b/c/e.txt", "2", ais.getNextEntry(), ais);
+                assertHelloWorld("a/b/d.txt", "1", ais.getNextEntry(), ais);
+            } else {
+                assertHelloWorld("a/b/d.txt", "1", n, ais);
+                assertDir("a/b/c", ais.getNextEntry());
+                assertHelloWorld("a/b/c/e.txt", "2", ais.getNextEntry(), ais);
+            }
+        }
+    }
+
+    private void assertDir(String expectedName, ArchiveEntry entry) {
+        Assert.assertNotNull(expectedName + " does not exists", entry);
+        Assert.assertEquals(expectedName + "/", entry.getName());
+        Assert.assertTrue(expectedName + " is not a directory", entry.isDirectory());
+    }
+
+    private void assertHelloWorld(String expectedName, String suffix, ArchiveEntry entry, InputStream is)
+        throws IOException {
+        Assert.assertNotNull(expectedName + " does not exists", entry);
+        Assert.assertEquals(expectedName, entry.getName());
+        Assert.assertFalse(expectedName + " is a directory", entry.isDirectory());
+        byte[] expected = ("Hello, world " + suffix).getBytes(StandardCharsets.UTF_8);
+        byte[] actual = IOUtils.toByteArray(is);
+        Assert.assertArrayEquals(expected, actual);
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/789c3955/src/test/java/org/apache/commons/compress/archivers/examples/SevenZArchiverTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/archivers/examples/SevenZArchiverTest.java b/src/test/java/org/apache/commons/compress/archivers/examples/SevenZArchiverTest.java
new file mode 100644
index 0000000..635c899
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress/archivers/examples/SevenZArchiverTest.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.archivers.examples;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.channels.FileChannel;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+
+import org.apache.commons.compress.AbstractTestCase;
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveException;
+import org.apache.commons.compress.archivers.StreamingNotSupportedException;
+import org.apache.commons.compress.archivers.sevenz.SevenZFile;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SevenZArchiverTest extends AbstractTestCase {
+    private File target;
+
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        File c = new File(dir, "a/b/c");
+        c.mkdirs();
+        try (OutputStream os = Files.newOutputStream(new File(dir, "a/b/d.txt").toPath())) {
+            os.write("Hello, world 1".getBytes(StandardCharsets.UTF_8));
+        }
+        try (OutputStream os = Files.newOutputStream(new File(dir, "a/b/c/e.txt").toPath())) {
+            os.write("Hello, world 2".getBytes(StandardCharsets.UTF_8));
+        }
+        target = new File(resultDir, "test.7z");
+    }
+
+    @Test
+    public void fileVersion() throws IOException, ArchiveException {
+        new Archiver().create("7z", target, dir);
+        verifyContent();
+    }
+
+    @Test(expected = StreamingNotSupportedException.class)
+    public void outputStreamVersion() throws IOException, ArchiveException {
+        try (OutputStream os = Files.newOutputStream(target.toPath())) {
+            new Archiver().create("7z", os, dir);
+        }
+    }
+
+    @Test
+    public void channelVersion() throws IOException, ArchiveException {
+        try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
+            StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
+            new Archiver().create("7z", c, dir);
+        }
+        verifyContent();
+    }
+
+    // not really a 7z test but I didn't feel like adding a new test just for this
+    @Test(expected = ArchiveException.class)
+    public void unknownFormat() throws IOException, ArchiveException {
+        try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE,
+            StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
+            new Archiver().create("unknown format", c, dir);
+        }
+    }
+
+    private void verifyContent() throws IOException, ArchiveException {
+        try (SevenZFile z = new SevenZFile(target)) {
+            assertDir("a", z.getNextEntry());
+            assertDir("a/b", z.getNextEntry());
+            ArchiveEntry n = z.getNextEntry();
+            Assert.assertNotNull(n);
+            // File.list may return a/b/c or a/b/d.txt first
+            if (n.getName().endsWith("/")) {
+                assertDir("a/b/c", n);
+                assertHelloWorld("a/b/c/e.txt", "2", z.getNextEntry(), z);
+                assertHelloWorld("a/b/d.txt", "1", z.getNextEntry(), z);
+            } else {
+                assertHelloWorld("a/b/d.txt", "1", n, z);
+                assertDir("a/b/c", z.getNextEntry());
+                assertHelloWorld("a/b/c/e.txt", "2", z.getNextEntry(), z);
+            }
+        }
+    }
+
+    private void assertDir(String expectedName, ArchiveEntry entry) {
+        Assert.assertNotNull(expectedName + " does not exists", entry);
+        Assert.assertEquals(expectedName + "/", entry.getName());
+        Assert.assertTrue(expectedName + " is not a directory", entry.isDirectory());
+    }
+
+    private void assertHelloWorld(String expectedName, String suffix, ArchiveEntry entry, SevenZFile z)
+        throws IOException {
+        Assert.assertNotNull(expectedName + " does not exists", entry);
+        Assert.assertEquals(expectedName, entry.getName());
+        Assert.assertFalse(expectedName + " is a directory", entry.isDirectory());
+        byte[] expected = ("Hello, world " + suffix).getBytes(StandardCharsets.UTF_8);
+        byte[] actual = new byte[expected.length];
+        Assert.assertEquals(actual.length, z.read(actual));
+        Assert.assertEquals(-1, z.read());
+        Assert.assertArrayEquals(expected, actual);
+    }
+}