• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.harmony.tests.java.util.zip;
19 
20 import java.io.EOFException;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.util.zip.Deflater;
27 import java.util.zip.DeflaterOutputStream;
28 import java.util.zip.InflaterInputStream;
29 import libcore.junit.junit3.TestCaseWithRules;
30 import libcore.junit.util.ResourceLeakageDetector;
31 import libcore.junit.util.ResourceLeakageDetector.DisableResourceLeakageDetection;
32 import org.junit.Rule;
33 import org.junit.rules.TestRule;
34 
35 public class DeflaterOutputStreamTest extends TestCaseWithRules {
36     @Rule
37     public TestRule guardRule = ResourceLeakageDetector.getRule();
38 
39     private class MyDeflaterOutputStream extends DeflaterOutputStream {
40         boolean deflateFlag = false;
41 
MyDeflaterOutputStream(OutputStream out)42         MyDeflaterOutputStream(OutputStream out) {
43             super(out);
44         }
45 
MyDeflaterOutputStream(OutputStream out, Deflater defl)46         MyDeflaterOutputStream(OutputStream out, Deflater defl) {
47             super(out, defl);
48         }
49 
MyDeflaterOutputStream(OutputStream out, Deflater defl, int size)50         MyDeflaterOutputStream(OutputStream out, Deflater defl, int size) {
51             super(out, defl, size);
52         }
53 
getProtectedBuf()54         byte[] getProtectedBuf() {
55             return buf;
56         }
57 
deflate()58         protected void deflate() throws IOException {
59             deflateFlag = true;
60             super.deflate();
61         }
62 
getDaflateFlag()63         boolean getDaflateFlag() {
64             return deflateFlag;
65         }
66     }
67 
68     private final byte outputBuf[] = new byte[500];
69 
70     @Override
setUp()71     protected void setUp() {
72         // setting up a deflater to be used
73         byte byteArray[] = { 1, 3, 4, 7, 8 };
74         int x = 0;
75         Deflater deflate = new Deflater(1);
76         deflate.setInput(byteArray);
77         while (!(deflate.needsInput())) {
78             x += deflate.deflate(outputBuf, x, outputBuf.length - x);
79         }
80         deflate.finish();
81         while (!(deflate.finished())) {
82             x = x + deflate.deflate(outputBuf, x, outputBuf.length - x);
83         }
84         deflate.end();
85     }
86 
87     /**
88      * java.util.zip.DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,
89      *java.util.zip.Deflater)
90      */
test_ConstructorLjava_io_OutputStreamLjava_util_zip_Deflater()91     public void test_ConstructorLjava_io_OutputStreamLjava_util_zip_Deflater() throws Exception {
92         byte byteArray[] = { 1, 3, 4, 7, 8 };
93         File f1 = File.createTempFile("hyts_ConstruOD", ".tst");
94         FileOutputStream fos = new FileOutputStream(f1);
95         Deflater defl = null;
96         MyDeflaterOutputStream dos;
97         // Test for a null Deflater.
98         try {
99             dos = new MyDeflaterOutputStream(fos, defl);
100             fail("NullPointerException Not Thrown");
101         } catch (NullPointerException e) {
102         }
103         defl = new Deflater();
104         dos = new MyDeflaterOutputStream(fos, defl);
105 
106         // Test to see if DeflaterOutputStream was created with the correct
107         // buffer.
108         assertEquals("Incorrect Buffer Size", 512, dos.getProtectedBuf().length);
109 
110         dos.write(byteArray);
111         dos.close();
112         f1.delete();
113         defl.end();
114     }
115 
116     /**
117      * java.util.zip.DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream)
118      */
test_ConstructorLjava_io_OutputStream()119     public void test_ConstructorLjava_io_OutputStream() throws Exception {
120         File f1 = File.createTempFile("hyts_ConstruO", ".tst");
121         FileOutputStream fos = new FileOutputStream(f1);
122         MyDeflaterOutputStream dos = new MyDeflaterOutputStream(fos);
123 
124         // Test to see if DeflaterOutputStream was created with the correct
125         // buffer.
126         assertEquals("Incorrect Buffer Size", 512, dos.getProtectedBuf().length);
127 
128         dos.write(outputBuf);
129         dos.close();
130         f1.delete();
131     }
132 
133     /**
134      * java.util.zip.DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,
135      *java.util.zip.Deflater, int)
136      */
test_ConstructorLjava_io_OutputStreamLjava_util_zip_DeflaterI()137     public void test_ConstructorLjava_io_OutputStreamLjava_util_zip_DeflaterI()
138             throws Exception {
139         int buf = 5;
140         int negBuf = -5;
141         int zeroBuf = 0;
142         byte byteArray[] = { 1, 3, 4, 7, 8, 3, 6 };
143         File f1 = File.createTempFile("hyts_ConstruODI", ".tst");
144         FileOutputStream fos = new FileOutputStream(f1);
145         Deflater defl = null;
146         MyDeflaterOutputStream dos;
147 
148         // Test for a null Deflater.
149         try {
150             dos = new MyDeflaterOutputStream(fos, defl, buf);
151             fail("NullPointerException Not Thrown");
152         } catch (NullPointerException e) {
153         }
154         defl = new Deflater();
155 
156         // Test for a negative buf.
157         try {
158             dos = new MyDeflaterOutputStream(fos, defl, negBuf);
159             fail("IllegalArgumentException Not Thrown");
160         } catch (IllegalArgumentException e) {
161         }
162 
163         // Test for a zero buf.
164         try {
165             dos = new MyDeflaterOutputStream(fos, defl, zeroBuf);
166             fail("IllegalArgumentException Not Thrown");
167         } catch (IllegalArgumentException e) {
168         }
169 
170         // Test to see if DeflaterOutputStream was created with the correct
171         // buffer.
172         dos = new MyDeflaterOutputStream(fos, defl, buf);
173         assertEquals("Incorrect Buffer Size", 5, dos.getProtectedBuf().length);
174 
175         dos.write(byteArray);
176         dos.close();
177         f1.delete();
178         defl.end();
179     }
180 
181     /**
182      * java.util.zip.DeflaterOutputStream#close()
183      */
184     @DisableResourceLeakageDetection(
185             why = "DeflaterOutputStream.close() does not work properly if finish() throws an"
186                     + " exception; DeflaterOutputStream.finish() throws an exception if the"
187                     + " underlying OutputStream has been closed and the Deflater still has data to"
188                     + " write.",
189             bug = "31797037")
test_close()190     public void test_close() throws Exception {
191         File f1 = File.createTempFile("close", ".tst");
192 
193         try (InflaterInputStream iis = new InflaterInputStream(new FileInputStream(f1))) {
194             try {
195                 iis.read();
196                 fail("EOFException Not Thrown");
197             } catch (EOFException e) {
198             }
199         }
200 
201         FileOutputStream fos = new FileOutputStream(f1);
202         DeflaterOutputStream dos = new DeflaterOutputStream(fos);
203         byte byteArray[] = { 1, 3, 4, 6 };
204         dos.write(byteArray);
205         dos.close();
206 
207         try (InflaterInputStream iis = new InflaterInputStream(new FileInputStream(f1))) {
208             // Test to see if the finish method wrote the bytes to the file.
209             assertEquals("Incorrect Byte Returned.", 1, iis.read());
210             assertEquals("Incorrect Byte Returned.", 3, iis.read());
211             assertEquals("Incorrect Byte Returned.", 4, iis.read());
212             assertEquals("Incorrect Byte Returned.", 6, iis.read());
213             assertEquals("Incorrect Byte Returned.", -1, iis.read());
214             assertEquals("Incorrect Byte Returned.", -1, iis.read());
215         }
216 
217         // Not sure if this test will stay.
218         FileOutputStream fos2 = new FileOutputStream(f1);
219         DeflaterOutputStream dos2 = new DeflaterOutputStream(fos2);
220         fos2.close();
221         try {
222             dos2.close();
223             fail("IOException not thrown");
224         } catch (IOException e) {
225         }
226 
227         // Test to write to a closed DeflaterOutputStream
228         try {
229             dos.write(5);
230             fail("DeflaterOutputStream Able To Write After Being Closed.");
231         } catch (IOException e) {
232         }
233 
234         // Test to write to a FileOutputStream that should have been closed
235         // by
236         // the DeflaterOutputStream.
237         try {
238             fos.write(("testing").getBytes());
239             fail("FileOutputStream Able To Write After Being Closed.");
240         } catch (IOException e) {
241         }
242 
243         f1.delete();
244     }
245 
246     /**
247      * java.util.zip.DeflaterOutputStream#finish()
248      */
test_finish()249     public void test_finish() throws Exception {
250         // Need test to see if method finish() actually finishes
251         // Only testing possible errors, not if it actually works
252 
253         File f1 = File.createTempFile("finish", ".tst");
254         FileOutputStream fos1 = new FileOutputStream(f1);
255         DeflaterOutputStream dos = new DeflaterOutputStream(fos1);
256         byte byteArray[] = { 1, 3, 4, 6 };
257         dos.write(byteArray);
258         dos.finish();
259 
260         // Test to see if the same FileOutputStream can be used with the
261         // DeflaterOutputStream after finish is called.
262         try {
263             dos.write(1);
264             fail("IOException not thrown");
265         } catch (IOException e) {
266         }
267 
268         // Test for writing with a new FileOutputStream using the same
269         // DeflaterOutputStream.
270         FileOutputStream fos2 = new FileOutputStream(f1);
271         DeflaterOutputStream dos4 = new DeflaterOutputStream(fos2);
272         dos4.write(1);
273 
274         // Test for writing to FileOutputStream fos1, which should be open.
275         fos1.write(("testing").getBytes());
276 
277         // Test for writing to FileOutputStream fos2, which should be open.
278         fos2.write(("testing").getBytes());
279 
280         // Not sure if this test will stay.
281         FileOutputStream fos3 = new FileOutputStream(f1);
282         DeflaterOutputStream dos3 = new DeflaterOutputStream(fos3);
283         fos3.close();
284         try {
285             dos3.finish();
286             fail("IOException not thrown");
287         } catch (IOException e) {
288         }
289         dos3.close();
290 
291         dos.close();
292         dos4.close();
293         f1.delete();
294     }
295 
296     /**
297      * java.util.zip.DeflaterOutputStream#write(int)
298      */
299     @DisableResourceLeakageDetection(
300             why = "DeflaterOutputStream.close() does not work properly if finish() throws an"
301                     + " exception; DeflaterOutputStream.finish() throws an exception if the"
302                     + " underlying OutputStream has been closed and the Deflater still has data to"
303                     + " write.",
304             bug = "31797037")
test_writeI()305     public void test_writeI() throws Exception {
306         File f1 = File.createTempFile("writeIL", ".tst");
307         FileOutputStream fos = new FileOutputStream(f1);
308         DeflaterOutputStream dos = new DeflaterOutputStream(fos);
309         for (int i = 0; i < 3; i++) {
310             dos.write(i);
311         }
312         dos.close();
313         FileInputStream fis = new FileInputStream(f1);
314         InflaterInputStream iis = new InflaterInputStream(fis);
315         for (int i = 0; i < 3; i++) {
316             assertEquals("Incorrect Byte Returned.", i, iis.read());
317         }
318         assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
319         assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
320         iis.close();
321 
322         // Not sure if this test is that important.
323         // Checks to see if you can write using the DeflaterOutputStream
324         // after
325         // the FileOutputStream has been closed.
326         FileOutputStream fos2 = new FileOutputStream(f1);
327         DeflaterOutputStream dos2 = new DeflaterOutputStream(fos2);
328         fos2.close();
329         try {
330             dos2.write(2);
331             fail("IOException not thrown");
332         } catch (IOException e) {
333         }
334         // Close to try and free up the resources.
335         try {
336             dos2.close();
337             fail("IOException not thrown");
338         } catch (IOException e) {
339         }
340 
341         f1.delete();
342     }
343 
344     /**
345      * java.util.zip.DeflaterOutputStream#write(byte[], int, int)
346      */
347     @DisableResourceLeakageDetection(
348             why = "DeflaterOutputStream.close() does not work properly if finish() throws an"
349                     + " exception; DeflaterOutputStream.finish() throws an exception if the"
350                     + " underlying OutputStream has been closed and the Deflater still has data to"
351                     + " write.",
352             bug = "31797037")
test_write$BII()353     public void test_write$BII() throws Exception {
354         byte byteArray[] = { 1, 3, 4, 7, 8, 3, 6 };
355 
356         // Test to see if the correct bytes are saved.
357         File f1 = File.createTempFile("writeBII", ".tst");
358         FileOutputStream fos1 = new FileOutputStream(f1);
359         DeflaterOutputStream dos1 = new DeflaterOutputStream(fos1);
360         dos1.write(byteArray, 2, 3);
361         dos1.close();
362         FileInputStream fis = new FileInputStream(f1);
363         InflaterInputStream iis = new InflaterInputStream(fis);
364         assertEquals("Incorrect Byte Returned.", 4, iis.read());
365         assertEquals("Incorrect Byte Returned.", 7, iis.read());
366         assertEquals("Incorrect Byte Returned.", 8, iis.read());
367         assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
368         assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
369         iis.close();
370         f1.delete();
371 
372         // Test for trying to write more bytes than available from the array
373         File f2 = File.createTempFile("writeBII2", ".tst");
374         FileOutputStream fos2 = new FileOutputStream(f2);
375         DeflaterOutputStream dos2 = new DeflaterOutputStream(fos2);
376         try {
377             dos2.write(byteArray, 2, 10);
378             fail("IndexOutOfBoundsException not thrown");
379         } catch (IndexOutOfBoundsException e) {
380         }
381 
382         // Test for trying to write a negative number of bytes.
383         try {
384             dos2.write(byteArray, 2, Integer.MIN_VALUE);
385             fail("IndexOutOfBoundsException not thrown");
386         } catch (IndexOutOfBoundsException e) {
387         }
388 
389         // Test for trying to start writing from a byte < 0 from the array.
390         try {
391             dos2.write(byteArray, Integer.MIN_VALUE, 2);
392             fail("IndexOutOfBoundsException not thrown");
393         } catch (IndexOutOfBoundsException e) {
394         }
395 
396         // Test for trying to start writing from a byte > than the array
397         // size.
398         try {
399             dos2.write(byteArray, 10, 2);
400             fail("IndexOutOfBoundsException not thrown");
401         } catch (IndexOutOfBoundsException e) {
402         }
403         dos2.close();
404 
405         // Not sure if this test is that important.
406         // Checks to see if you can write using the DeflaterOutputStream
407         // after
408         // the FileOutputStream has been closed.
409         FileOutputStream fos3 = new FileOutputStream(f2);
410         DeflaterOutputStream dos3 = new DeflaterOutputStream(fos3);
411         fos3.close();
412         try {
413             dos3.write(byteArray, 2, 3);
414             fail("IOException not thrown");
415         } catch (IOException e) {
416         }
417         // Close to try and free up the resources.
418         try {
419             dos3.close();
420             fail("IOException not thrown");
421         } catch (IOException e) {
422         }
423 
424         f2.delete();
425     }
426 
test_deflate()427     public void test_deflate() throws Exception {
428         File f1 = File.createTempFile("writeI1", ".tst");
429         FileOutputStream fos = new FileOutputStream(f1);
430         MyDeflaterOutputStream dos = new MyDeflaterOutputStream(fos);
431         assertFalse(dos.getDaflateFlag());
432         for (int i = 0; i < 3; i++) {
433             dos.write(i);
434         }
435         assertTrue(dos.getDaflateFlag());
436         dos.close();
437     }
438 }
439