Repository: commons-compress Updated Branches: refs/heads/master 789c39551 -> 040685c80
add tests for Expander Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/c67040b8 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/c67040b8 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/c67040b8 Branch: refs/heads/master Commit: c67040b8d18e6cb3ad8035045039ac066c3eb923 Parents: 789c395 Author: Stefan Bodewig <bode...@apache.org> Authored: Fri May 11 21:32:37 2018 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Fri May 11 21:32:37 2018 +0200 ---------------------------------------------------------------------- .../compress/archivers/examples/Expander.java | 1 + .../archivers/examples/ExpanderTest.java | 170 +++++++++++++++++++ .../examples/ParameterizedExpanderTest.java | 157 +++++++++++++++++ 3 files changed, 328 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/c67040b8/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java b/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java index f6bee61..4f33848 100644 --- a/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java +++ b/src/main/java/org/apache/commons/compress/archivers/examples/Expander.java @@ -141,6 +141,7 @@ public class Expander { } else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) { expand(new SevenZFile(archive), targetDirectory); } else { + // never reached as prefersSeekableByteChannel only returns true for 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/c67040b8/src/test/java/org/apache/commons/compress/archivers/examples/ExpanderTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/examples/ExpanderTest.java b/src/test/java/org/apache/commons/compress/archivers/examples/ExpanderTest.java new file mode 100644 index 0000000..a579261 --- /dev/null +++ b/src/test/java/org/apache/commons/compress/archivers/examples/ExpanderTest.java @@ -0,0 +1,170 @@ +/* + * 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.archivers.StreamingNotSupportedException; +import org.apache.commons.compress.archivers.sevenz.SevenZFile; +import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile; +import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.commons.compress.utils.IOUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ExpanderTest extends AbstractTestCase { + + private File archive; + + @Test + public void sevenZTwoFileVersion() throws IOException, ArchiveException { + setup7z(); + new Expander().expand("7z", archive, resultDir); + verifyTargetDir(); + } + + @Test + public void sevenZTwoFileVersionWithAutoDetection() throws IOException, ArchiveException { + setup7z(); + new Expander().expand(archive, resultDir); + verifyTargetDir(); + } + + @Test(expected = StreamingNotSupportedException.class) + public void sevenZInputStreamVersion() throws IOException, ArchiveException { + setup7z(); + try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) { + new Expander().expand("7z", i, resultDir); + } + } + + @Test(expected = StreamingNotSupportedException.class) + public void sevenZInputStreamVersionWithAutoDetection() throws IOException, ArchiveException { + setup7z(); + try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) { + new Expander().expand(i, resultDir); + } + } + + @Test + public void sevenZChannelVersion() throws IOException, ArchiveException { + setup7z(); + try (SeekableByteChannel c = FileChannel.open(archive.toPath(), StandardOpenOption.READ)) { + new Expander().expand("7z", c, resultDir); + } + verifyTargetDir(); + } + + @Test + public void sevenZFileVersion() throws IOException, ArchiveException { + setup7z(); + new Expander().expand(new SevenZFile(archive), resultDir); + verifyTargetDir(); + } + + @Test + public void zipFileVersion() throws IOException, ArchiveException { + setupZip(); + new Expander().expand(new ZipFile(archive), resultDir); + verifyTargetDir(); + } + + private void setup7z() throws IOException, ArchiveException { + archive = new File(dir, "test.7z"); + File dummy = new File(dir, "x"); + try (OutputStream o = Files.newOutputStream(dummy.toPath())) { + o.write(new byte[14]); + } + try (SevenZOutputFile aos = new SevenZOutputFile(archive)) { + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a/b")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a/b/c")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dummy, "a/b/d.txt")); + aos.write("Hello, world 1".getBytes(StandardCharsets.UTF_8)); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dummy, "a/b/c/e.txt")); + aos.write("Hello, world 2".getBytes(StandardCharsets.UTF_8)); + aos.closeArchiveEntry(); + aos.finish(); + } + } + + private void setupZip() throws IOException, ArchiveException { + archive = new File(dir, "test.zip"); + File dummy = new File(dir, "x"); + try (OutputStream o = Files.newOutputStream(dummy.toPath())) { + o.write(new byte[14]); + } + try (ArchiveOutputStream aos = new ArchiveStreamFactory() + .createArchiveOutputStream("zip", Files.newOutputStream(archive.toPath()))) { + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a/b")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a/b/c")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dummy, "a/b/d.txt")); + aos.write("Hello, world 1".getBytes(StandardCharsets.UTF_8)); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dummy, "a/b/c/e.txt")); + aos.write("Hello, world 2".getBytes(StandardCharsets.UTF_8)); + aos.closeArchiveEntry(); + aos.finish(); + } + } + + private void verifyTargetDir() throws IOException { + Assert.assertTrue("a has not been created", new File(resultDir, "a").isDirectory()); + Assert.assertTrue("a/b has not been created", new File(resultDir, "a/b").isDirectory()); + Assert.assertTrue("a/b/c has not been created", new File(resultDir, "a/b/c").isDirectory()); + assertHelloWorld("a/b/d.txt", "1"); + assertHelloWorld("a/b/c/e.txt", "2"); + } + + private void assertHelloWorld(String fileName, String suffix) throws IOException { + Assert.assertTrue(fileName + " does not exist", new File(resultDir, fileName).isFile()); + byte[] expected = ("Hello, world " + suffix).getBytes(StandardCharsets.UTF_8); + try (InputStream is = Files.newInputStream(new File(resultDir, fileName).toPath())) { + byte[] actual = IOUtils.toByteArray(is); + Assert.assertArrayEquals(expected, actual); + } + } + +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/c67040b8/src/test/java/org/apache/commons/compress/archivers/examples/ParameterizedExpanderTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/examples/ParameterizedExpanderTest.java b/src/test/java/org/apache/commons/compress/archivers/examples/ParameterizedExpanderTest.java new file mode 100644 index 0000000..5d6da4b --- /dev/null +++ b/src/test/java/org/apache/commons/compress/archivers/examples/ParameterizedExpanderTest.java @@ -0,0 +1,157 @@ +/* + * 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 ParameterizedExpanderTest extends AbstractTestCase { + + // 7z and ZIP using ZipFile is in ExpanderTest + @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 archive; + + public ParameterizedExpanderTest(String format) { + this.format = format; + } + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + archive = new File(dir, "test." + format); + File dummy = new File(dir, "x"); + try (OutputStream o = Files.newOutputStream(dummy.toPath())) { + o.write(new byte[14]); + } + try (ArchiveOutputStream aos = new ArchiveStreamFactory() + .createArchiveOutputStream(format, Files.newOutputStream(archive.toPath()))) { + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a/b")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dir, "a/b/c")); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dummy, "a/b/d.txt")); + aos.write("Hello, world 1".getBytes(StandardCharsets.UTF_8)); + aos.closeArchiveEntry(); + aos.putArchiveEntry(aos.createArchiveEntry(dummy, "a/b/c/e.txt")); + aos.write("Hello, world 2".getBytes(StandardCharsets.UTF_8)); + aos.closeArchiveEntry(); + aos.finish(); + } + } + + @Test + public void fileVersion() throws IOException, ArchiveException { + new Expander().expand(format, archive, resultDir); + verifyTargetDir(); + } + + @Test + public void fileVersionWithAutoDetection() throws IOException, ArchiveException { + new Expander().expand(archive, resultDir); + verifyTargetDir(); + } + + @Test + public void inputStreamVersion() throws IOException, ArchiveException { + try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) { + new Expander().expand(format, i, resultDir); + } + verifyTargetDir(); + } + + @Test + public void inputStreamVersionWithAutoDetection() throws IOException, ArchiveException { + try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath()))) { + new Expander().expand(i, resultDir); + } + verifyTargetDir(); + } + + @Test + public void channelVersion() throws IOException, ArchiveException { + try (SeekableByteChannel c = FileChannel.open(archive.toPath(), StandardOpenOption.READ)) { + new Expander().expand(format, c, resultDir); + } + verifyTargetDir(); + } + + @Test + public void archiveInputStreamVersion() throws IOException, ArchiveException { + try (InputStream i = new BufferedInputStream(Files.newInputStream(archive.toPath())); + ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(format, i)) { + new Expander().expand(ais, resultDir); + } + verifyTargetDir(); + } + + private void verifyTargetDir() throws IOException { + Assert.assertTrue("a has not been created", new File(resultDir, "a").isDirectory()); + Assert.assertTrue("a/b has not been created", new File(resultDir, "a/b").isDirectory()); + Assert.assertTrue("a/b/c has not been created", new File(resultDir, "a/b/c").isDirectory()); + assertHelloWorld("a/b/d.txt", "1"); + assertHelloWorld("a/b/c/e.txt", "2"); + } + + private void assertHelloWorld(String fileName, String suffix) throws IOException { + Assert.assertTrue(fileName + " does not exist", new File(resultDir, fileName).isFile()); + byte[] expected = ("Hello, world " + suffix).getBytes(StandardCharsets.UTF_8); + try (InputStream is = Files.newInputStream(new File(resultDir, fileName).toPath())) { + byte[] actual = IOUtils.toByteArray(is); + Assert.assertArrayEquals(expected, actual); + } + } + +}