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     }
113 
114     @Test
115     public void testCallSequenceAr() throws Exception{
116         doCallSequence("Ar");
117     }
118 
119     @Test
120     public void testCallSequenceCpio() throws Exception{
121         doCallSequence("Cpio");
122     }
123 
124     @Test
125     public void testCallSequenceJar() throws Exception{
126         doCallSequence("Jar");
127     }
128 
129     @Test
130     public void testCallSequenceTar() throws Exception{
131         doCallSequence("Tar");
132     }
133 
134     @Test
135     public void testCallSequenceZip() throws Exception{
136         doCallSequence("Zip");
137     }
138 
139     private void doCallSequence(final String archiveType) throws Exception {
140         final OutputStream out1 = new ByteArrayOutputStream();
141         final File dummy = getFile("test1.xml"); // need a real file
142 
143         ArchiveOutputStream aos1;
144         aos1 = factory.createArchiveOutputStream(archiveType, out1);
145         aos1.putArchiveEntry(aos1.createArchiveEntry(dummy, "dummy"));
146         try (InputStream is = new FileInputStream(dummy)) {
147             IOUtils.copy(is, aos1);
148         }
149         aos1.closeArchiveEntry();
150         aos1.close(); // omitted finish
151 
152         // TODO - check if archives ensure that data has been written to the stream?
153 
154         aos1 = factory.createArchiveOutputStream(archiveType, out1);
155         try {
156             aos1.closeArchiveEntry();
157             fail("Should have raised IOException - closeArchiveEntry() called before putArchiveEntry()");
158         } catch (final IOException expected) {
159         }
160 
161         aos1.putArchiveEntry(aos1.createArchiveEntry(dummy, "dummy"));
162         try (InputStream is = new FileInputStream(dummy)) {
163             IOUtils.copy(is, aos1);
164         }
165 
166         // TODO check if second putArchiveEntry() can follow without closeAE?
167 
168         try {
169             aos1.finish();
170             fail("Should have raised IOException - finish() called before closeArchiveEntry()");
171         } catch (final IOException expected) {
172         }
173         try {
174             aos1.close();
175             fail("Should have raised IOException - close() called before closeArchiveEntry()");
176         } catch (final IOException expected) {
177         }
178         aos1.closeArchiveEntry();
179         try {
180             aos1.closeArchiveEntry();
181             fail("Should have raised IOException - closeArchiveEntry() called with no open entry");
182         } catch (final IOException expected) {
183         }
184         aos1.finish();
185         aos1.close();
186         try {
187             aos1.finish();
188             fail("Should have raised IOException - finish() called after close()");
189         } catch (final IOException expected) {
190         }
191     }
192 }