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   *   https://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;
20  
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.OutputStream;
27  
28  import org.apache.commons.compress.AbstractTest;
29  import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
30  import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
31  import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
32  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
33  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
34  import org.junit.jupiter.api.Test;
35  
36  public class ArchiveOutputStreamTest<O extends ArchiveOutputStream<E>, E extends ArchiveEntry> extends AbstractTest {
37  
38      private O createArchiveWithDummyEntry(final String archiveType, final OutputStream out1, final File dummy) throws Exception {
39          final O outputStream = factory.createArchiveOutputStream(archiveType, out1);
40          outputStream.putArchiveEntry(outputStream.createArchiveEntry(dummy, "dummy"));
41          outputStream.write(dummy);
42          return outputStream;
43      }
44  
45      private void doCallSequence(final String archiveType) throws Exception {
46          final OutputStream out1 = new ByteArrayOutputStream();
47          final File dummy = getFile("test1.xml"); // need a real file
48  
49          try (O outputStream = factory.createArchiveOutputStream(archiveType, out1)) {
50              outputStream.putArchiveEntry(outputStream.createArchiveEntry(dummy, "dummy"));
51              outputStream.write(dummy);
52              outputStream.closeArchiveEntry();
53              // omitted finish
54          }
55  
56          // TODO - check if archives ensure that data has been written to the stream?
57  
58          final O aos2 = factory.createArchiveOutputStream(archiveType, out1);
59          assertThrows(IOException.class, aos2::closeArchiveEntry, "Should have raised IOException - closeArchiveEntry() called before putArchiveEntry()");
60  
61          aos2.putArchiveEntry(aos2.createArchiveEntry(dummy, "dummy"));
62          aos2.write(dummy);
63  
64          // TODO check if second putArchiveEntry() can follow without closeAE?
65  
66          assertThrows(IOException.class, aos2::finish, "Should have raised IOException - finish() called before closeArchiveEntry()");
67          assertThrows(IOException.class, aos2::close, "Should have raised IOException - close() called before closeArchiveEntry()");
68  
69          final O aos3 = createArchiveWithDummyEntry(archiveType, out1, dummy);
70          aos3.closeArchiveEntry();
71          assertThrows(IOException.class, aos3::closeArchiveEntry, "Should have raised IOException - closeArchiveEntry() called with no open entry");
72  
73          final O aos4 = createArchiveWithDummyEntry(archiveType, out1, dummy);
74          aos4.closeArchiveEntry();
75          aos4.finish();
76          aos4.close();
77          assertThrows(IOException.class, aos4::finish, "Should have raised IOException - finish() called after close()");
78      }
79  
80      @Test
81      void testCallSequenceAr() throws Exception {
82          doCallSequence("Ar");
83      }
84  
85      @Test
86      void testCallSequenceCpio() throws Exception {
87          doCallSequence("Cpio");
88      }
89  
90      @Test
91      void testCallSequenceJar() throws Exception {
92          doCallSequence("Jar");
93      }
94  
95      @Test
96      void testCallSequenceTar() throws Exception {
97          doCallSequence("Tar");
98      }
99  
100     @Test
101     void testCallSequenceZip() throws Exception {
102         doCallSequence("Zip");
103     }
104 
105     @Test
106     void testFinish() throws Exception {
107         final OutputStream out1 = new ByteArrayOutputStream();
108 
109         try (ArchiveOutputStream<? super ArchiveEntry> aios = factory.createArchiveOutputStream("zip", out1)) {
110             aios.putArchiveEntry(new ZipArchiveEntry("dummy"));
111             assertThrows(IOException.class, () -> aios.finish(), "After putArchiveEntry() should follow closeArchiveEntry()");
112             aios.closeArchiveEntry();
113         }
114 
115         try (ArchiveOutputStream<JarArchiveEntry> aios = factory.createArchiveOutputStream("jar", out1)) {
116             aios.putArchiveEntry(new JarArchiveEntry("dummy"));
117             assertThrows(IOException.class, () -> aios.finish(), "After putArchiveEntry() should follow closeArchiveEntry()");
118             aios.closeArchiveEntry();
119         }
120 
121         try (ArchiveOutputStream<ArArchiveEntry> aios = factory.createArchiveOutputStream("ar", out1)) {
122             aios.putArchiveEntry(new ArArchiveEntry("dummy", 100));
123             assertThrows(IOException.class, () -> aios.finish(), "After putArchiveEntry() should follow closeArchiveEntry()");
124             aios.closeArchiveEntry();
125         }
126 
127         try (ArchiveOutputStream<CpioArchiveEntry> aios = factory.createArchiveOutputStream("cpio", out1)) {
128             aios.putArchiveEntry(new CpioArchiveEntry("dummy"));
129             assertThrows(IOException.class, () -> aios.finish(), "After putArchiveEntry() should follow closeArchiveEntry()");
130             aios.closeArchiveEntry();
131         }
132 
133         try (ArchiveOutputStream<TarArchiveEntry> aios = factory.createArchiveOutputStream("tar", out1)) {
134             aios.putArchiveEntry(new TarArchiveEntry("dummy"));
135             assertThrows(IOException.class, () -> aios.finish(), "After putArchiveEntry() should follow closeArchiveEntry()");
136             aios.closeArchiveEntry();
137         }
138     }
139 
140     @Test
141     void testOptionalFinish() throws Exception {
142         final OutputStream out1 = new ByteArrayOutputStream();
143 
144         try (ArchiveOutputStream<ZipArchiveEntry> aos1 = factory.createArchiveOutputStream("zip", out1)) {
145             aos1.putArchiveEntry(new ZipArchiveEntry("dummy"));
146             aos1.closeArchiveEntry();
147         }
148 
149         final ArchiveOutputStream<JarArchiveEntry> finishTest;
150         try (ArchiveOutputStream<JarArchiveEntry> aos1 = factory.createArchiveOutputStream("jar", out1)) {
151             finishTest = aos1;
152             aos1.putArchiveEntry(new JarArchiveEntry("dummy"));
153             aos1.closeArchiveEntry();
154         }
155         assertThrows(IOException.class, () -> finishTest.finish(), "finish() cannot follow close()");
156         finishTest.close();
157     }
158 }