summaryrefslogtreecommitdiffstats
path: root/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/zip/DeflaterOutputStreamTest.java
blob: e4be19824f69cc5b8af5b9f25eb598ed4257a297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.harmony.tests.java.util.zip;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

import junit.framework.TestCase;

public class DeflaterOutputStreamTest extends TestCase {

    private class MyDeflaterOutputStream extends DeflaterOutputStream {
        boolean deflateFlag = false;

        MyDeflaterOutputStream(OutputStream out) {
            super(out);
        }

        MyDeflaterOutputStream(OutputStream out, Deflater defl) {
            super(out, defl);
        }

        MyDeflaterOutputStream(OutputStream out, Deflater defl, int size) {
            super(out, defl, size);
        }

        byte[] getProtectedBuf() {
            return buf;
        }

        protected void deflate() throws IOException {
            deflateFlag = true;
            super.deflate();
        }

        boolean getDaflateFlag() {
            return deflateFlag;
        }
    }

    private final byte outputBuf[] = new byte[500];

    @Override
    protected void setUp() {
        // setting up a deflater to be used
        byte byteArray[] = { 1, 3, 4, 7, 8 };
        int x = 0;
        Deflater deflate = new Deflater(1);
        deflate.setInput(byteArray);
        while (!(deflate.needsInput())) {
            x += deflate.deflate(outputBuf, x, outputBuf.length - x);
        }
        deflate.finish();
        while (!(deflate.finished())) {
            x = x + deflate.deflate(outputBuf, x, outputBuf.length - x);
        }
        deflate.end();
    }

    /**
     * java.util.zip.DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,
     *java.util.zip.Deflater)
     */
    public void test_ConstructorLjava_io_OutputStreamLjava_util_zip_Deflater() throws Exception {
        byte byteArray[] = { 1, 3, 4, 7, 8 };
        File f1 = File.createTempFile("hyts_ConstruOD", ".tst");
        FileOutputStream fos = new FileOutputStream(f1);
        Deflater defl = null;
        MyDeflaterOutputStream dos;
        // Test for a null Deflater.
        try {
            dos = new MyDeflaterOutputStream(fos, defl);
            fail("NullPointerException Not Thrown");
        } catch (NullPointerException e) {
        }
        defl = new Deflater();
        dos = new MyDeflaterOutputStream(fos, defl);

        // Test to see if DeflaterOutputStream was created with the correct
        // buffer.
        assertEquals("Incorrect Buffer Size", 512, dos.getProtectedBuf().length);

        dos.write(byteArray);
        dos.close();
        f1.delete();
    }

    /**
     * java.util.zip.DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream)
     */
    public void test_ConstructorLjava_io_OutputStream() throws Exception {
        File f1 = File.createTempFile("hyts_ConstruO", ".tst");
        FileOutputStream fos = new FileOutputStream(f1);
        MyDeflaterOutputStream dos = new MyDeflaterOutputStream(fos);

        // Test to see if DeflaterOutputStream was created with the correct
        // buffer.
        assertEquals("Incorrect Buffer Size", 512, dos.getProtectedBuf().length);

        dos.write(outputBuf);
        dos.close();
        f1.delete();
    }

    /**
     * java.util.zip.DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,
     *java.util.zip.Deflater, int)
     */
    public void test_ConstructorLjava_io_OutputStreamLjava_util_zip_DeflaterI()
            throws Exception {
        int buf = 5;
        int negBuf = -5;
        int zeroBuf = 0;
        byte byteArray[] = { 1, 3, 4, 7, 8, 3, 6 };
        File f1 = File.createTempFile("hyts_ConstruODI", ".tst");
        FileOutputStream fos = new FileOutputStream(f1);
        Deflater defl = null;
        MyDeflaterOutputStream dos;

        // Test for a null Deflater.
        try {
            dos = new MyDeflaterOutputStream(fos, defl, buf);
            fail("NullPointerException Not Thrown");
        } catch (NullPointerException e) {
        }
        defl = new Deflater();

        // Test for a negative buf.
        try {
            dos = new MyDeflaterOutputStream(fos, defl, negBuf);
            fail("IllegalArgumentException Not Thrown");
        } catch (IllegalArgumentException e) {
        }

        // Test for a zero buf.
        try {
            dos = new MyDeflaterOutputStream(fos, defl, zeroBuf);
            fail("IllegalArgumentException Not Thrown");
        } catch (IllegalArgumentException e) {
        }

        // Test to see if DeflaterOutputStream was created with the correct
        // buffer.
        dos = new MyDeflaterOutputStream(fos, defl, buf);
        assertEquals("Incorrect Buffer Size", 5, dos.getProtectedBuf().length);

        dos.write(byteArray);
        dos.close();
        f1.delete();
    }

    /**
     * java.util.zip.DeflaterOutputStream#close()
     */
    public void test_close() throws Exception {
        File f1 = File.createTempFile("close", ".tst");

        InflaterInputStream iis = new InflaterInputStream(new FileInputStream(f1));
        try {
            iis.read();
            fail("EOFException Not Thrown");
        } catch (EOFException e) {
        }

        FileOutputStream fos = new FileOutputStream(f1);
        DeflaterOutputStream dos = new DeflaterOutputStream(fos);
        byte byteArray[] = { 1, 3, 4, 6 };
        dos.write(byteArray);
        dos.close();

        iis = new InflaterInputStream(new FileInputStream(f1));

        // Test to see if the finish method wrote the bytes to the file.
        assertEquals("Incorrect Byte Returned.", 1, iis.read());
        assertEquals("Incorrect Byte Returned.", 3, iis.read());
        assertEquals("Incorrect Byte Returned.", 4, iis.read());
        assertEquals("Incorrect Byte Returned.", 6, iis.read());
        assertEquals("Incorrect Byte Returned.", -1, iis.read());
        assertEquals("Incorrect Byte Returned.", -1, iis.read());
        iis.close();

        // Not sure if this test will stay.
        FileOutputStream fos2 = new FileOutputStream(f1);
        DeflaterOutputStream dos2 = new DeflaterOutputStream(fos2);
        fos2.close();
        try {
            dos2.close();
            fail("IOException not thrown");
        } catch (IOException e) {
        }

        // Test to write to a closed DeflaterOutputStream
        try {
            dos.write(5);
            fail("DeflaterOutputStream Able To Write After Being Closed.");
        } catch (IOException e) {
        }

        // Test to write to a FileOutputStream that should have been closed
        // by
        // the DeflaterOutputStream.
        try {
            fos.write(("testing").getBytes());
            fail("FileOutputStream Able To Write After Being Closed.");
        } catch (IOException e) {
        }

        f1.delete();
    }

    /**
     * java.util.zip.DeflaterOutputStream#finish()
     */
    public void test_finish() throws Exception {
        // Need test to see if method finish() actually finishes
        // Only testing possible errors, not if it actually works

        File f1 = File.createTempFile("finish", ".tst");
        FileOutputStream fos1 = new FileOutputStream(f1);
        DeflaterOutputStream dos = new DeflaterOutputStream(fos1);
        byte byteArray[] = { 1, 3, 4, 6 };
        dos.write(byteArray);
        dos.finish();

        // Test to see if the same FileOutputStream can be used with the
        // DeflaterOutputStream after finish is called.
        try {
            dos.write(1);
            fail("IOException not thrown");
        } catch (IOException e) {
        }

        // Test for writing with a new FileOutputStream using the same
        // DeflaterOutputStream.
        FileOutputStream fos2 = new FileOutputStream(f1);
        dos = new DeflaterOutputStream(fos2);
        dos.write(1);

        // Test for writing to FileOutputStream fos1, which should be open.
        fos1.write(("testing").getBytes());

        // Test for writing to FileOutputStream fos2, which should be open.
        fos2.write(("testing").getBytes());

        // Not sure if this test will stay.
        FileOutputStream fos3 = new FileOutputStream(f1);
        DeflaterOutputStream dos3 = new DeflaterOutputStream(fos3);
        fos3.close();
        try {
            dos3.finish();
            fail("IOException not thrown");
        } catch (IOException e) {
        }

        // dos.close() won't close fos1 because it has been re-assigned to
        // fos2
        fos1.close();
        dos.close();
        f1.delete();
    }

    /**
     * java.util.zip.DeflaterOutputStream#write(int)
     */
    public void test_writeI() throws Exception {
        File f1 = File.createTempFile("writeIL", ".tst");
        FileOutputStream fos = new FileOutputStream(f1);
        DeflaterOutputStream dos = new DeflaterOutputStream(fos);
        for (int i = 0; i < 3; i++) {
            dos.write(i);
        }
        dos.close();
        FileInputStream fis = new FileInputStream(f1);
        InflaterInputStream iis = new InflaterInputStream(fis);
        for (int i = 0; i < 3; i++) {
            assertEquals("Incorrect Byte Returned.", i, iis.read());
        }
        assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
        assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
        iis.close();

        // Not sure if this test is that important.
        // Checks to see if you can write using the DeflaterOutputStream
        // after
        // the FileOutputStream has been closed.
        FileOutputStream fos2 = new FileOutputStream(f1);
        DeflaterOutputStream dos2 = new DeflaterOutputStream(fos2);
        fos2.close();
        try {
            dos2.write(2);
            fail("IOException not thrown");
        } catch (IOException e) {
        }

        f1.delete();
    }

    /**
     * java.util.zip.DeflaterOutputStream#write(byte[], int, int)
     */
    public void test_write$BII() throws Exception {
        byte byteArray[] = { 1, 3, 4, 7, 8, 3, 6 };

        // Test to see if the correct bytes are saved.
        File f1 = File.createTempFile("writeBII", ".tst");
        FileOutputStream fos1 = new FileOutputStream(f1);
        DeflaterOutputStream dos1 = new DeflaterOutputStream(fos1);
        dos1.write(byteArray, 2, 3);
        dos1.close();
        FileInputStream fis = new FileInputStream(f1);
        InflaterInputStream iis = new InflaterInputStream(fis);
        assertEquals("Incorrect Byte Returned.", 4, iis.read());
        assertEquals("Incorrect Byte Returned.", 7, iis.read());
        assertEquals("Incorrect Byte Returned.", 8, iis.read());
        assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
        assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
        iis.close();
        f1.delete();

        // Test for trying to write more bytes than available from the array
        File f2 = File.createTempFile("writeBII2", ".tst");
        FileOutputStream fos2 = new FileOutputStream(f2);
        DeflaterOutputStream dos2 = new DeflaterOutputStream(fos2);
        try {
            dos2.write(byteArray, 2, 10);
            fail("IndexOutOfBoundsException not thrown");
        } catch (IndexOutOfBoundsException e) {
        }

        // Test for trying to write a negative number of bytes.
        try {
            dos2.write(byteArray, 2, Integer.MIN_VALUE);
            fail("IndexOutOfBoundsException not thrown");
        } catch (IndexOutOfBoundsException e) {
        }

        // Test for trying to start writing from a byte < 0 from the array.
        try {
            dos2.write(byteArray, Integer.MIN_VALUE, 2);
            fail("IndexOutOfBoundsException not thrown");
        } catch (IndexOutOfBoundsException e) {
        }

        // Test for trying to start writing from a byte > than the array
        // size.
        try {
            dos2.write(byteArray, 10, 2);
            fail("IndexOutOfBoundsException not thrown");
        } catch (IndexOutOfBoundsException e) {
        }
        dos2.close();

        // Not sure if this test is that important.
        // Checks to see if you can write using the DeflaterOutputStream
        // after
        // the FileOutputStream has been closed.
        FileOutputStream fos3 = new FileOutputStream(f2);
        DeflaterOutputStream dos3 = new DeflaterOutputStream(fos3);
        fos3.close();
        try {
            dos3.write(byteArray, 2, 3);
            fail("IOException not thrown");
        } catch (IOException e) {
        }

        f2.delete();
    }

    public void test_deflate() throws Exception {
        File f1 = File.createTempFile("writeI1", ".tst");
        FileOutputStream fos = new FileOutputStream(f1);
        MyDeflaterOutputStream dos = new MyDeflaterOutputStream(fos);
        assertFalse(dos.getDaflateFlag());
        for (int i = 0; i < 3; i++) {
            dos.write(i);
        }
        assertTrue(dos.getDaflateFlag());
        dos.close();
    }
}