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