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 java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  
28  import org.apache.commons.compress.AbstractTestCase;
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.apache.commons.compress.utils.IOUtils;
35  
36  public class ArchiveOutputStreamTest extends AbstractTestCase {
37  
38      @Override
39      protected void setUp() throws Exception {
40          super.setUp();
41      }
42  
43      @Override
44      protected void tearDown() throws Exception {
45          super.tearDown();
46      }
47  
48      public void testFinish() throws Exception {
49          OutputStream out1 = new ByteArrayOutputStream();
50  
51          ArchiveOutputStream aos1 = factory.createArchiveOutputStream("zip", out1);
52          aos1.putArchiveEntry(new ZipArchiveEntry("dummy"));
53          try {
54              aos1.finish();
55              fail("After putArchive should follow closeArchive");
56          } catch (IOException io) {
57              // Exception expected
58          }
59  
60          aos1 = factory.createArchiveOutputStream("jar", out1);
61          aos1.putArchiveEntry(new JarArchiveEntry("dummy"));
62          try {
63              aos1.finish();
64              fail("After putArchive should follow closeArchive");
65          } catch (IOException io) {
66              // Exception expected
67          }
68  
69          aos1 = factory.createArchiveOutputStream("ar", out1);
70          aos1.putArchiveEntry(new ArArchiveEntry("dummy", 100));
71          try {
72              aos1.finish();
73              fail("After putArchive should follow closeArchive");
74          } catch (IOException io) {
75              // Exception expected
76          }
77  
78          aos1 = factory.createArchiveOutputStream("cpio", out1);
79          aos1.putArchiveEntry(new CpioArchiveEntry("dummy"));
80          try {
81              aos1.finish();
82              fail("After putArchive should follow closeArchive");
83          } catch (IOException io) {
84              // Exception expected
85          }
86  
87          aos1 = factory.createArchiveOutputStream("tar", out1);
88          aos1.putArchiveEntry(new TarArchiveEntry("dummy"));
89          try {
90              aos1.finish();
91              fail("After putArchive should follow closeArchive");
92          } catch (IOException io) {
93              // Exception expected
94          }
95      }
96  
97      public void testOptionalFinish() throws Exception {
98          OutputStream out1 = new ByteArrayOutputStream();
99  
100         ArchiveOutputStream aos1 = factory.createArchiveOutputStream("zip", out1);
101         aos1.putArchiveEntry(new ZipArchiveEntry("dummy"));
102         aos1.closeArchiveEntry();
103         aos1.close();
104 
105         aos1 = factory.createArchiveOutputStream("jar", out1);
106         aos1.putArchiveEntry(new JarArchiveEntry("dummy"));
107         aos1.closeArchiveEntry();
108         aos1.close();
109         try {
110             aos1.finish();
111             fail("finish() cannot follow close()");
112         } catch (IOException io) {
113             // Exception expected
114         }
115     }
116 
117     public void testCallSequenceAr() throws Exception{
118         doCallSequence("Ar");
119     }
120 
121     public void testCallSequenceCpio() throws Exception{
122         doCallSequence("Cpio");
123     }
124 
125     public void testCallSequenceJar() throws Exception{
126         doCallSequence("Jar");
127     }
128 
129     public void testCallSequenceTar() throws Exception{
130         doCallSequence("Tar");
131     }
132 
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 }