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