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