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
18package org.apache.harmony.tests.java.util.zip;
19
20import java.io.ByteArrayOutputStream;
21import java.io.IOException;
22import java.util.zip.Deflater;
23import java.util.zip.Inflater;
24import java.util.zip.InflaterOutputStream;
25import java.util.zip.ZipException;
26
27import junit.framework.TestCase;
28
29public class InflaterOutputStreamTest extends TestCase {
30
31    private ByteArrayOutputStream os = new ByteArrayOutputStream();
32
33    private byte[] compressedBytes = new byte[100];
34
35    private String testString = "Hello world";
36
37    /**
38     * java.util.zip.InflaterOutputStream#InflaterOutputStream(java.io.OutputStream)
39     */
40    public void test_ConstructorLjava_io_OutputStream() throws IOException {
41        new InflaterOutputStream(os);
42
43        try {
44            new InflaterOutputStream(null);
45            fail("Should throw NullPointerException");
46        } catch (NullPointerException e) {
47            // expected
48        }
49    }
50
51    /**
52     * java.util.zip.InflaterOutputStream#InflaterOutputStream(java.io.OutputStream, Inflater)
53     */
54    public void test_ConstructorLjava_io_OutputStreamLjava_util_zip_Inflater() {
55        new InflaterOutputStream(os, new Inflater());
56
57        try {
58            new InflaterOutputStream(null, new Inflater());
59            fail("Should throw NullPointerException");
60        } catch (NullPointerException e) {
61            // expected
62        }
63
64        try {
65            new InflaterOutputStream(os, null);
66            fail("Should throw NullPointerException");
67        } catch (NullPointerException e) {
68            // expected
69        }
70    }
71
72    /**
73     * java.util.zip.InflaterOutputStream#InflaterOutputStream(java.io.OutputStream, Inflater, int)
74     */
75    public void test_ConstructorLjava_io_OutputStreamLjava_util_zip_InflaterI() {
76        new InflaterOutputStream(os, new Inflater(), 20);
77
78        try {
79            new InflaterOutputStream(null, null, 10);
80            fail("Should throw NullPointerException");
81        } catch (NullPointerException e) {
82            // expected
83        }
84
85        try {
86            new InflaterOutputStream(null, new Inflater(), -1);
87            fail("Should throw NullPointerException");
88        } catch (NullPointerException e) {
89            // expected
90        }
91
92        try {
93            new InflaterOutputStream(os, null, -1);
94            fail("Should throw NullPointerException");
95        } catch (NullPointerException e) {
96            // expected
97        }
98
99        try {
100            new InflaterOutputStream(null, null, -1);
101            fail("Should throw NullPointerException");
102        } catch (NullPointerException e) {
103            // expected
104        }
105
106        try {
107            new InflaterOutputStream(os, new Inflater(), 0);
108            fail("Should throw IllegalArgumentException");
109        } catch (IllegalArgumentException e) {
110            // expected
111        }
112
113        try {
114            new InflaterOutputStream(os, new Inflater(), -10000);
115            fail("Should throw IllegalArgumentException");
116        } catch (IllegalArgumentException e) {
117            // expected
118        }
119    }
120
121    /**
122     * java.util.zip.InflaterOutputStream#close()
123     */
124    public void test_close() throws IOException {
125        InflaterOutputStream ios = new InflaterOutputStream(os);
126        ios.close();
127        // multiple close
128        ios.close();
129    }
130
131    /**
132     * java.util.zip.InflaterOutputStream#flush()
133     */
134    public void test_flush() throws IOException {
135        InflaterOutputStream ios = new InflaterOutputStream(os);
136        ios.close();
137        try {
138            ios.flush();
139            fail("Should throw IOException");
140        } catch (IOException e) {
141            // expected
142        }
143
144        ios = new InflaterOutputStream(os);
145        ios.flush();
146        ios.flush();
147    }
148
149    /**
150     * java.util.zip.InflaterOutputStream#finish()
151     */
152    public void test_finish() throws IOException {
153        InflaterOutputStream ios = new InflaterOutputStream(os);
154        ios.close();
155        try {
156            ios.finish();
157            fail("Should throw IOException");
158        } catch (IOException e) {
159            // expected
160        }
161
162        ios = new InflaterOutputStream(os);
163        ios.finish();
164        ios.finish();
165        ios.flush();
166        ios.flush();
167        ios.finish();
168
169        byte[] bytes1 = { 10, 20, 30, 40, 50 };
170        Deflater defaultDeflater = new Deflater(Deflater.BEST_SPEED);
171        defaultDeflater.setInput(bytes1);
172        defaultDeflater.finish();
173        int length1 = defaultDeflater.deflate(compressedBytes);
174
175        byte[] bytes2 = { 100, 90, 80, 70, 60 };
176        Deflater bestDeflater = new Deflater(Deflater.BEST_COMPRESSION);
177        bestDeflater.setInput(bytes2);
178        bestDeflater.finish();
179        int length2 = bestDeflater.deflate(compressedBytes, length1, compressedBytes.length - length1);
180
181        ios = new InflaterOutputStream(os);
182        for (int i = 0; i < length1; i++) {
183            ios.write(compressedBytes[i]);
184        }
185        ios.finish();
186        ios.close();
187
188        byte[] result = os.toByteArray();
189        for (int i = 0; i < bytes1.length; i++) {
190            assertEquals(bytes1[i], result[i]);
191        }
192
193        ios = new InflaterOutputStream(os);
194        for (int i = length1; i < length2 * 2; i++) {
195            ios.write(compressedBytes[i]);
196        }
197        ios.finish();
198        ios.close();
199
200        result = os.toByteArray();
201        for (int i = 0; i < bytes2.length; i++) {
202            assertEquals(bytes2[i], result[bytes1.length + i]);
203        }
204
205    }
206
207    /**
208     * java.util.zip.InflaterOutputStream#write(int)
209     */
210    public void test_write_I() throws IOException {
211        int length = compressToBytes(testString);
212
213        // uncompress the data stored in the compressedBytes
214        InflaterOutputStream ios = new InflaterOutputStream(os);
215        for (int i = 0; i < length; i++) {
216            ios.write(compressedBytes[i]);
217        }
218
219        String result = new String(os.toByteArray());
220        assertEquals(testString, result);
221    }
222
223    /**
224     * java.util.zip.InflaterOutputStream#write(int)
225     */
226    public void test_write_I_Illegal() throws IOException {
227
228        // write after close
229        InflaterOutputStream ios = new InflaterOutputStream(os);
230        ios.close();
231        try {
232            ios.write(-1);
233            fail("Should throw IOException");
234        } catch (IOException e) {
235            // expected
236        }
237    }
238
239    /**
240     * java.util.zip.InflaterOutputStream#write(byte[], int, int)
241     */
242    public void test_write_$BII() throws IOException {
243        int length = compressToBytes(testString);
244
245        // uncompress the data stored in the compressedBytes
246        InflaterOutputStream ios = new InflaterOutputStream(os);
247        ios.write(compressedBytes, 0, length);
248
249        String result = new String(os.toByteArray());
250        assertEquals(testString, result);
251    }
252
253    /**
254     * java.util.zip.InflaterOutputStream#write(byte[], int, int)
255     */
256    public void test_write_$BII_Illegal() throws IOException {
257        // write error compression (ZIP) format
258        InflaterOutputStream ios = new InflaterOutputStream(os);
259        byte[] bytes = { 0, 1, 2, 3 };
260        try {
261            ios.write(bytes, 0, 4);
262            fail("Should throw ZipException");
263        } catch (ZipException e) {
264            // expected
265        }
266        try {
267            ios.flush();
268            fail("Should throw ZipException");
269        } catch (ZipException e) {
270            // expected
271        }
272
273        // write after close
274        ios = new InflaterOutputStream(os);
275        ios.close();
276        try {
277            ios.write(bytes, 0, 4);
278            fail("Should throw IOException");
279        } catch (IOException e) {
280            // expected
281        }
282        try {
283            ios.write(bytes, -1, 4);
284            fail("Should throw IOException");
285        } catch (IOException e) {
286            // expected
287        }
288        try {
289            ios.write(bytes, -1, -4);
290            fail("Should throw IOException");
291        } catch (IOException e) {
292            // expected
293        }
294        try {
295            ios.write(bytes, 0, 400);
296            fail("Should throw IOException");
297        } catch (IOException e) {
298            // expected
299        }
300        try {
301            ios.write(null, -1, 4);
302            fail("Should throw IOException");
303        } catch (IOException e) {
304            // expected
305        }
306
307        ios = new InflaterOutputStream(os);
308        try {
309            ios.write(null, 0, 4);
310            fail("Should throw NullPointerException");
311        } catch (NullPointerException e) {
312            // expected
313        }
314        try {
315            ios.write(null, -1, 4);
316            fail("Should throw NullPointerException");
317        } catch (NullPointerException e) {
318            // expected
319        }
320        try {
321            ios.write(null, 0, -4);
322            fail("Should throw NullPointerException");
323        } catch (NullPointerException e) {
324            // expected
325        }
326        try {
327            ios.write(null, 0, 1000);
328            fail("Should throw NullPointerException");
329        } catch (NullPointerException e) {
330            // expected
331        }
332        try {
333            ios.write(bytes, -1, 4);
334            fail("Should throw IndexOutOfBoundsException");
335        } catch (IndexOutOfBoundsException e) {
336            // expected
337        }
338        try {
339            ios.write(bytes, 0, -4);
340            fail("Should throw IndexOutOfBoundsException");
341        } catch (IndexOutOfBoundsException e) {
342            // expected
343        }
344        try {
345            ios.write(bytes, 0, 100);
346            fail("Should throw IndexOutOfBoundsException");
347        } catch (IndexOutOfBoundsException e) {
348            // expected
349        }
350        try {
351            ios.write(bytes, -100, 100);
352            fail("Should throw IndexOutOfBoundsException");
353        } catch (IndexOutOfBoundsException e) {
354            // expected
355        }
356
357        ios = new InflaterOutputStream(os);
358        ios.finish();
359
360        try {
361            ios.write(bytes, -1, -100);
362            fail("Should throw IndexOutOfBoundsException");
363        } catch (IndexOutOfBoundsException e) {
364            // expected
365        }
366        try {
367            ios.write(null, -1, -100);
368            fail("Should throw NullPointerException");
369        } catch (NullPointerException e) {
370            // expected
371        }
372
373        ios = new InflaterOutputStream(os);
374        ios.flush();
375        try {
376            ios.write(bytes, 0, 4);
377            fail("Should throw ZipException");
378        } catch (ZipException e) {
379            // expected
380        }
381    }
382
383    // Compress the test string into compressedBytes
384    private int compressToBytes(String string) {
385        byte[] input = string.getBytes();
386        Deflater deflater = new Deflater();
387        deflater.setInput(input);
388        deflater.finish();
389        return deflater.deflate(compressedBytes);
390    }
391
392}
393