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;
20  
21  import static org.junit.Assert.*;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  
30  import org.apache.commons.compress.AbstractTestCase;
31  import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
32  import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
33  import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
34  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
35  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
36  import org.apache.commons.compress.utils.IOUtils;
37  import org.junit.Test;
38  
39  public class ArchiveOutputStreamTest extends AbstractTestCase {
40  
41      @Test
42      public void testFinish() throws Exception {
43          final OutputStream out1 = new ByteArrayOutputStream();
44  
45          ArchiveOutputStream aos1 = factory.createArchiveOutputStream("zip", out1);
46          aos1.putArchiveEntry(new ZipArchiveEntry("dummy"));
47          try {
48              aos1.finish();
49              fail("After putArchive should follow closeArchive");
50          } catch (final IOException io) {
51              // Exception expected
52          }
53  
54          aos1 = factory.createArchiveOutputStream("jar", out1);
55          aos1.putArchiveEntry(new JarArchiveEntry("dummy"));
56          try {
57              aos1.finish();
58              fail("After putArchive should follow closeArchive");
59          } catch (final IOException io) {
60              // Exception expected
61          }
62  
63          aos1 = factory.createArchiveOutputStream("ar", out1);
64          aos1.putArchiveEntry(new ArArchiveEntry("dummy", 100));
65          try {
66              aos1.finish();
67              fail("After putArchive should follow closeArchive");
68          } catch (final IOException io) {
69              // Exception expected
70          }
71  
72          aos1 = factory.createArchiveOutputStream("cpio", out1);
73          aos1.putArchiveEntry(new CpioArchiveEntry("dummy"));
74          try {
75              aos1.finish();
76              fail("After putArchive should follow closeArchive");
77          } catch (final IOException io) {
78              // Exception expected
79          }
80  
81          aos1 = factory.createArchiveOutputStream("tar", out1);
82          aos1.putArchiveEntry(new TarArchiveEntry("dummy"));
83          try {
84              aos1.finish();
85              fail("After putArchive should follow closeArchive");
86          } catch (final IOException io) {
87              // Exception expected
88          }
89      }
90  
91      @Test
92      public void testOptionalFinish() throws Exception {
93          final OutputStream out1 = new ByteArrayOutputStream();
94  
95          try (ArchiveOutputStream aos1 = factory.createArchiveOutputStream("zip", out1)) {
96              aos1.putArchiveEntry(new ZipArchiveEntry("dummy"));
97              aos1.closeArchiveEntry();
98          }
99  
100         final ArchiveOutputStream finishTest;
101         try (ArchiveOutputStream aos1 = factory.createArchiveOutputStream("jar", out1)) {
102             finishTest = aos1;
103             aos1.putArchiveEntry(new JarArchiveEntry("dummy"));
104             aos1.closeArchiveEntry();
105         }
106         try {
107             finishTest.finish();
108             fail("finish() cannot follow close()");
109         } catch (final IOException io) {
110             // Exception expected
111         }
112         finishTest.close();
113     }
114 
115     @Test
116     public void testCallSequenceAr() throws Exception{
117         doCallSequence("Ar");
118     }
119 
120     @Test
121     public void testCallSequenceCpio() throws Exception{
122         doCallSequence("Cpio");
123     }
124 
125     @Test
126     public void testCallSequenceJar() throws Exception{
127         doCallSequence("Jar");
128     }
129 
130     @Test
131     public void testCallSequenceTar() throws Exception{
132         doCallSequence("Tar");
133     }
134 
135     @Test
136     public void testCallSequenceZip() throws Exception{
137         doCallSequence("Zip");
138     }
139 
140     private void doCallSequence(final String archiveType) throws Exception {
141         final OutputStream out1 = new ByteArrayOutputStream();
142         final File dummy = getFile("test1.xml"); // need a real file
143 
144         ArchiveOutputStream aos1;
145         aos1 = factory.createArchiveOutputStream(archiveType, out1);
146         aos1.putArchiveEntry(aos1.createArchiveEntry(dummy, "dummy"));
147         try (InputStream is = new FileInputStream(dummy)) {
148             IOUtils.copy(is, aos1);
149         }
150         aos1.closeArchiveEntry();
151         aos1.close(); // omitted finish
152 
153         // TODO - check if archives ensure that data has been written to the stream?
154 
155         aos1 = factory.createArchiveOutputStream(archiveType, out1);
156         try {
157             aos1.closeArchiveEntry();
158             fail("Should have raised IOException - closeArchiveEntry() called before putArchiveEntry()");
159         } catch (final IOException expected) {
160         }
161 
162         aos1.putArchiveEntry(aos1.createArchiveEntry(dummy, "dummy"));
163         try (InputStream is = new FileInputStream(dummy)) {
164             IOUtils.copy(is, aos1);
165         }
166 
167         // TODO check if second putArchiveEntry() can follow without closeAE?
168 
169         try {
170             aos1.finish();
171             fail("Should have raised IOException - finish() called before closeArchiveEntry()");
172         } catch (final IOException expected) {
173         }
174         try {
175             aos1.close();
176             fail("Should have raised IOException - close() called before closeArchiveEntry()");
177         } catch (final IOException expected) {
178         }
179         aos1.closeArchiveEntry();
180         try {
181             aos1.closeArchiveEntry();
182             fail("Should have raised IOException - closeArchiveEntry() called with no open entry");
183         } catch (final IOException expected) {
184         }
185         aos1.finish();
186         aos1.close();
187         try {
188             aos1.finish();
189             fail("Should have raised IOException - finish() called after close()");
190         } catch (final IOException expected) {
191         }
192     }
193 }