1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.compress;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29
30 import org.apache.commons.compress.archivers.ArchiveEntry;
31 import org.apache.commons.compress.archivers.ArchiveInputStream;
32 import org.apache.commons.compress.archivers.ArchiveOutputStream;
33 import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
34 import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
35 import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
36 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
37 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
38 import org.junit.jupiter.api.Test;
39
40
41
42
43 public class IOMethodsTest extends AbstractTest {
44
45 private static final int bytesToTest = 50;
46 private static final byte[] byteTest = new byte[bytesToTest];
47 static {
48 for (int i = 0; i < byteTest.length;) {
49 byteTest[i] = (byte) i;
50 byteTest[i + 1] = (byte) -i;
51 i += 2;
52 }
53 }
54
55 private void compareReads(final String archiverName) throws Exception {
56 final OutputStream out1 = new ByteArrayOutputStream();
57 final OutputStream out2 = new ByteArrayOutputStream();
58 final OutputStream out3 = new ByteArrayOutputStream();
59 final Path file = createSingleEntryArchive(archiverName);
60
61 final InputStream is1 = Files.newInputStream(file);
62 final ArchiveInputStream<?> ais1 = factory.createArchiveInputStream(archiverName, is1);
63 final ArchiveEntry nextEntry = ais1.getNextEntry();
64 assertNotNull(nextEntry);
65
66 final byte[] buff = new byte[10];
67 final long size = nextEntry.getSize();
68 if (size != ArchiveEntry.SIZE_UNKNOWN) {
69 assertTrue(size > 0, "Size should be > 0, found: " + size);
70 }
71
72 final InputStream is2 = Files.newInputStream(file);
73 final ArchiveInputStream<?> ais2 = factory.createArchiveInputStream(archiverName, is2);
74 final ArchiveEntry nextEntry2 = ais2.getNextEntry();
75 assertNotNull(nextEntry2);
76 assertEquals(size, nextEntry2.getSize(), "Expected same entry size");
77
78 final InputStream is3 = Files.newInputStream(file);
79 final ArchiveInputStream<?> ais3 = factory.createArchiveInputStream(archiverName, is3);
80 final ArchiveEntry nextEntry3 = ais3.getNextEntry();
81 assertNotNull(nextEntry3);
82 assertEquals(size, nextEntry3.getSize(), "Expected same entry size");
83
84 int b;
85 while ((b = ais1.read()) != -1) {
86 out1.write(b);
87 }
88 ais1.close();
89
90 int bytes;
91 while ((bytes = ais2.read(buff)) > 0) {
92 out2.write(buff, 0, bytes);
93 }
94 ais2.close();
95
96 while ((bytes = ais3.read(buff, 0, buff.length)) > 0) {
97 out3.write(buff, 0, bytes);
98 }
99 ais3.close();
100
101 assertEquals(out1.toString().length(), out2.toString().length(), "out1Len!=out2Len");
102 assertEquals(out1.toString().length(), out3.toString().length(), "out1Len!=out3Len");
103 assertEquals(out1.toString(), out2.toString(), "out1!=out2");
104 assertEquals(out1.toString(), out3.toString(), "out1!=out3");
105 }
106
107 private void compareWrites(final String archiverName, final ArchiveEntry entry) throws Exception {
108 final OutputStream out1 = new ByteArrayOutputStream();
109 final OutputStream out2 = new ByteArrayOutputStream();
110 final OutputStream out3 = new ByteArrayOutputStream();
111 final ArchiveOutputStream<ArchiveEntry> aos1 = factory.createArchiveOutputStream(archiverName, out1);
112 aos1.putArchiveEntry(entry);
113 final ArchiveOutputStream<ArchiveEntry> aos2 = factory.createArchiveOutputStream(archiverName, out2);
114 aos2.putArchiveEntry(entry);
115 final ArchiveOutputStream<ArchiveEntry> aos3 = factory.createArchiveOutputStream(archiverName, out3);
116 aos3.putArchiveEntry(entry);
117 for (final byte element : byteTest) {
118 aos1.write(element);
119 }
120 aos1.closeArchiveEntry();
121 aos1.close();
122
123 aos2.write(byteTest);
124 aos2.closeArchiveEntry();
125 aos2.close();
126
127 aos3.write(byteTest, 0, byteTest.length);
128 aos3.closeArchiveEntry();
129 aos3.close();
130 assertEquals(aos1.getBytesWritten(), aos2.getBytesWritten(), "aos1Bytes!=aos2Bytes");
131 assertEquals(aos1.getBytesWritten(), aos3.getBytesWritten(), "aos1Bytes!=aos3Bytes");
132 assertEquals(out1.toString().length(), out2.toString().length(), "out1Len!=out2Len");
133 assertEquals(out1.toString().length(), out3.toString().length(), "out1Len!=out2Len");
134 assertEquals(out1.toString(), out2.toString(), "out1!=out2");
135 assertEquals(out1.toString(), out3.toString(), "out1!=out3");
136 }
137
138 @Test
139 public void testReadAr() throws Exception {
140 compareReads("ar");
141 }
142
143 @Test
144 public void testReadCpio() throws Exception {
145 compareReads("cpio");
146 }
147
148 @Test
149 public void testReadJar() throws Exception {
150 compareReads("jar");
151 }
152
153 @Test
154 public void testReadTar() throws Exception {
155 compareReads("tar");
156 }
157
158 @Test
159 public void testReadZip() throws Exception {
160 compareReads("zip");
161 }
162
163 @Test
164 public void testWriteAr() throws Exception {
165 compareWrites("ar", new ArArchiveEntry("dummy", bytesToTest));
166 }
167
168 @Test
169 public void testWriteCpio() throws Exception {
170 final ArchiveEntry entry = new CpioArchiveEntry("dummy", bytesToTest);
171 compareWrites("cpio", entry);
172 }
173
174 @Test
175 public void testWriteJar() throws Exception {
176 final ArchiveEntry entry = new JarArchiveEntry("dummy");
177 compareWrites("jar", entry);
178 }
179
180 @Test
181 public void testWriteTar() throws Exception {
182 final TarArchiveEntry entry = new TarArchiveEntry("dummy");
183 entry.setSize(bytesToTest);
184 compareWrites("tar", entry);
185 }
186
187 @Test
188 public void testWriteZip() throws Exception {
189 final ArchiveEntry entry = new ZipArchiveEntry("dummy");
190 compareWrites("zip", entry);
191 }
192 }