View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.archivers.examples;
20  
21  import static java.nio.charset.StandardCharsets.UTF_8;
22  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertFalse;
25  import static org.junit.jupiter.api.Assertions.assertNotNull;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  
28  import java.io.BufferedInputStream;
29  import java.io.File;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.nio.channels.FileChannel;
34  import java.nio.channels.SeekableByteChannel;
35  import java.nio.file.Files;
36  import java.nio.file.StandardOpenOption;
37  import java.util.stream.Stream;
38  
39  import org.apache.commons.compress.AbstractTest;
40  import org.apache.commons.compress.archivers.ArchiveEntry;
41  import org.apache.commons.compress.archivers.ArchiveException;
42  import org.apache.commons.compress.archivers.ArchiveInputStream;
43  import org.apache.commons.compress.archivers.ArchiveOutputStream;
44  import org.apache.commons.compress.archivers.ArchiveStreamFactory;
45  import org.apache.commons.io.IOUtils;
46  import org.junit.jupiter.params.ParameterizedTest;
47  import org.junit.jupiter.params.provider.Arguments;
48  import org.junit.jupiter.params.provider.MethodSource;
49  
50  public class ParameterizedArchiverTest extends AbstractTest {
51  
52      // can't test 7z here as 7z cannot write to non-seekable streams
53      // and reading logic would be different as well - see
54      // SevenZArchiverTest class
55      public static Stream<Arguments> data() {
56          return Stream.of(Arguments.of("tar"), Arguments.of("cpio"), Arguments.of("zip"));
57      }
58  
59      private File target;
60  
61      @ParameterizedTest
62      @MethodSource("data")
63      public void archiveStreamVersion(final String format) throws Exception {
64          // TODO How to parameterize a BeforeEach method?
65          setUp(format);
66          try (OutputStream os = Files.newOutputStream(target.toPath());
67                  ArchiveOutputStream<?> aos = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream(format, os)) {
68              new Archiver().create(aos, getTempDirFile());
69          }
70          verifyContent(format);
71      }
72  
73      private void assertDir(final String expectedName, final ArchiveEntry entry) {
74          assertNotNull(entry, () -> expectedName + " does not exists");
75          assertEquals(expectedName + "/", entry.getName());
76          assertTrue(entry.isDirectory(), expectedName + " is not a directory");
77      }
78  
79      private void assertHelloWorld(final String expectedName, final String suffix, final ArchiveEntry entry, final InputStream is) throws IOException {
80          assertNotNull(entry, () -> expectedName + " does not exists");
81          assertEquals(expectedName, entry.getName());
82          assertFalse(entry.isDirectory(), expectedName + " is a directory");
83          final byte[] expected = ("Hello, world " + suffix).getBytes(UTF_8);
84          final byte[] actual = IOUtils.toByteArray(is);
85          assertArrayEquals(expected, actual);
86      }
87  
88      @ParameterizedTest
89      @MethodSource("data")
90      public void channelVersion(final String format) throws Exception {
91          // TODO How to parameterize a BeforeEach method?
92          setUp(format);
93          try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE,
94                  StandardOpenOption.TRUNCATE_EXISTING)) {
95              new Archiver().create(format, c, getTempDirFile());
96          }
97          verifyContent(format);
98      }
99  
100     @ParameterizedTest
101     @MethodSource("data")
102     public void fileVersion(final String format) throws Exception {
103         // TODO How to parameterize a BeforeEach method?
104         setUp(format);
105         new Archiver().create(format, target, getTempDirFile());
106         verifyContent(format);
107     }
108 
109     @ParameterizedTest
110     @MethodSource("data")
111     public void outputStreamVersion(final String format) throws Exception {
112         // TODO How to parameterize a BeforeEach method?
113         setUp(format);
114         try (OutputStream os = Files.newOutputStream(target.toPath())) {
115             new Archiver().create(format, os, getTempDirFile());
116         }
117         verifyContent(format);
118     }
119 
120     public void setUp(final String format) throws Exception {
121         final File c = newTempFile("a/b/c");
122         c.mkdirs();
123         try (OutputStream os = Files.newOutputStream(newTempFile("a/b/d.txt").toPath())) {
124             os.write("Hello, world 1".getBytes(UTF_8));
125         }
126         try (OutputStream os = Files.newOutputStream(newTempFile("a/b/c/e.txt").toPath())) {
127             os.write("Hello, world 2".getBytes(UTF_8));
128         }
129         target = new File(tempResultDir, "test." + format);
130     }
131 
132     private void verifyContent(final String format) throws IOException, ArchiveException {
133         try (InputStream is = Files.newInputStream(target.toPath());
134                 BufferedInputStream bis = new BufferedInputStream(is);
135                 ArchiveInputStream<?> ais = ArchiveStreamFactory.DEFAULT.createArchiveInputStream(format, bis)) {
136             assertDir("a", ais.getNextEntry());
137             assertDir("a/b", ais.getNextEntry());
138             final ArchiveEntry n = ais.getNextEntry();
139             assertNotNull(n);
140             // File.list may return a/b/c or a/b/d.txt first
141             if (n.getName().endsWith("/")) {
142                 assertDir("a/b/c", n);
143                 assertHelloWorld("a/b/c/e.txt", "2", ais.getNextEntry(), ais);
144                 assertHelloWorld("a/b/d.txt", "1", ais.getNextEntry(), ais);
145             } else {
146                 assertHelloWorld("a/b/d.txt", "1", n, ais);
147                 assertDir("a/b/c", ais.getNextEntry());
148                 assertHelloWorld("a/b/c/e.txt", "2", ais.getNextEntry(), ais);
149             }
150         }
151     }
152 }