summaryrefslogtreecommitdiffstats
path: root/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/zip/ZipEntryTest.java
blob: f034639bd0833b85e3f638a5b12568cc78368a62 (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* 
 * 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.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.TimeZone;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import libcore.io.Streams;
import tests.support.resource.Support_Resources;

public class ZipEntryTest extends junit.framework.TestCase {
    // zip file hyts_ZipFile.zip must be included as a resource
    private ZipEntry zentry;
    private ZipFile zfile;

    private long orgSize;
    private long orgCompressedSize;
    private long orgCrc;
    private long orgTime;

    /**
     * java.util.zip.ZipEntry#ZipEntry(java.lang.String)
     */
    public void test_ConstructorLjava_lang_String() {
        // Test for method java.util.zip.ZipEntry(java.lang.String)
        zentry = zfile.getEntry("File3.txt");
        assertNotNull("Failed to create ZipEntry", zentry);
        try {
            zentry = zfile.getEntry(null);
            fail("NullPointerException not thrown");
        } catch (NullPointerException e) {
        }
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < 65535; i++) {
            s.append('a');
        }
        try {
            zentry = new ZipEntry(s.toString());
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException During Test.");
        }
        try {
            s.append('a');
            zentry = new ZipEntry(s.toString());
            fail("IllegalArgumentException not thrown");
        } catch (IllegalArgumentException e) {
        }
        try {
            String n = null;
            zentry = new ZipEntry(n);
            fail("NullPointerException not thrown");
        } catch (NullPointerException e) {
        }
    }

    /**
     * java.util.zip.ZipEntry#getComment()
     */
    public void test_getComment() {
        // Test for method java.lang.String java.util.zip.ZipEntry.getComment()
        ZipEntry zipEntry = new ZipEntry("zippy.zip");
        assertNull("Incorrect Comment Returned.", zipEntry.getComment());
        zipEntry.setComment("This Is A Comment");
        assertEquals("Incorrect Comment Returned.",
                "This Is A Comment", zipEntry.getComment());
    }

    /**
     * java.util.zip.ZipEntry#getCompressedSize()
     */
    public void test_getCompressedSize() {
        // Test for method long java.util.zip.ZipEntry.getCompressedSize()
        assertTrue("Incorrect compressed size returned", zentry
                .getCompressedSize() == orgCompressedSize);
    }

    /**
     * java.util.zip.ZipEntry#getCrc()
     */
    public void test_getCrc() {
        // Test for method long java.util.zip.ZipEntry.getCrc()
        assertEquals("Failed to get Crc", orgCrc, zentry.getCrc());
    }

    /**
     * java.util.zip.ZipEntry#getExtra()
     */
    public void test_getExtra() {
        // Test for method byte [] java.util.zip.ZipEntry.getExtra()
        assertNull("Incorrect extra information returned",
                zentry.getExtra());
        byte[] ba = { 'T', 'E', 'S', 'T' };
        zentry = new ZipEntry("test.tst");
        zentry.setExtra(ba);
        assertEquals("Incorrect Extra Information Returned.",
                ba, zentry.getExtra());
    }

    /**
     * java.util.zip.ZipEntry#getMethod()
     */
    public void test_getMethod() {
        // Test for method int java.util.zip.ZipEntry.getMethod()
        zentry = zfile.getEntry("File1.txt");
        assertEquals("Incorrect compression method returned",
                java.util.zip.ZipEntry.STORED, zentry.getMethod());
        zentry = zfile.getEntry("File3.txt");
        assertEquals("Incorrect compression method returned",
                java.util.zip.ZipEntry.DEFLATED, zentry.getMethod());
        zentry = new ZipEntry("test.tst");
        assertEquals("Incorrect Method Returned.", -1, zentry.getMethod());
    }

    /**
     * java.util.zip.ZipEntry#getName()
     */
    public void test_getName() {
        // Test for method java.lang.String java.util.zip.ZipEntry.getName()
        assertEquals("Incorrect name returned - Note return result somewhat ambiguous in spec",
                "File1.txt", zentry.getName());
    }

    /**
     * java.util.zip.ZipEntry#getSize()
     */
    public void test_getSize() {
        // Test for method long java.util.zip.ZipEntry.getSize()
        assertEquals("Incorrect size returned", orgSize, zentry.getSize());
    }

    /**
     * java.util.zip.ZipEntry#getTime()
     */
    public void test_getTime() {
        // Test for method long java.util.zip.ZipEntry.getTime()
        assertEquals("Failed to get time", orgTime, zentry.getTime());
    }

    /**
     * java.util.zip.ZipEntry#isDirectory()
     */
    public void test_isDirectory() {
        // Test for method boolean java.util.zip.ZipEntry.isDirectory()
        assertTrue("Entry should not answer true to isDirectory", !zentry
                .isDirectory());
        zentry = new ZipEntry("Directory/");
        assertTrue("Entry should answer true to isDirectory", zentry
                .isDirectory());
    }

    /**
     * java.util.zip.ZipEntry#setComment(java.lang.String)
     */
    public void test_setCommentLjava_lang_String() {
        // Test for method void
        // java.util.zip.ZipEntry.setComment(java.lang.String)
        zentry = zfile.getEntry("File1.txt");
        zentry.setComment("Set comment using api");
        assertEquals("Comment not correctly set",
                "Set comment using api", zentry.getComment());
        String n = null;
        zentry.setComment(n);
        assertNull("Comment not correctly set", zentry.getComment());
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < 0xFFFF; i++) {
            s.append('a');
        }
        try {
            zentry.setComment(s.toString());
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException During Test.");
        }
        try {
            s.append('a');
            zentry.setComment(s.toString());
            fail("IllegalArgumentException not thrown");
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * java.util.zip.ZipEntry#setCompressedSize(long)
     */
    public void test_setCompressedSizeJ() {
        // Test for method void java.util.zip.ZipEntry.setCompressedSize(long)
        zentry.setCompressedSize(orgCompressedSize + 10);
        assertEquals("Set compressed size failed",
                (orgCompressedSize + 10), zentry.getCompressedSize());
        zentry.setCompressedSize(0);
        assertEquals("Set compressed size failed",
                0, zentry.getCompressedSize());
        zentry.setCompressedSize(-25);
        assertEquals("Set compressed size failed",
                -25, zentry.getCompressedSize());
        zentry.setCompressedSize(4294967296l);
        assertEquals("Set compressed size failed",
                4294967296l, zentry.getCompressedSize());
    }

    /**
     * java.util.zip.ZipEntry#setCrc(long)
     */
    public void test_setCrcJ() {
        // Test for method void java.util.zip.ZipEntry.setCrc(long)
        zentry.setCrc(orgCrc + 100);
        assertEquals("Failed to set Crc", (orgCrc + 100), zentry.getCrc());
        zentry.setCrc(0);
        assertEquals("Failed to set Crc", 0, zentry.getCrc());
        try {
            zentry.setCrc(-25);
            fail("IllegalArgumentException not thrown");
        } catch (IllegalArgumentException e) {
        }
        try {
            zentry.setCrc(4294967295l);
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException during test");
        }
        try {
            zentry.setCrc(4294967296l);
            fail("IllegalArgumentException not thrown");
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * java.util.zip.ZipEntry#setExtra(byte[])
     */
    public void test_setExtra$B() {
        // Test for method void java.util.zip.ZipEntry.setExtra(byte [])
        zentry = zfile.getEntry("File1.txt");
        zentry.setExtra("Test setting extra information".getBytes());
        assertEquals("Extra information not written properly", "Test setting extra information", new String(zentry
                .getExtra(), 0, zentry.getExtra().length)
        );
        zentry = new ZipEntry("test.tst");
        byte[] ba = new byte[0xFFFF];
        try {
            zentry.setExtra(ba);
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException during test");
        }
        try {
            ba = new byte[0xFFFF + 1];
            zentry.setExtra(ba);
            fail("IllegalArgumentException not thrown");
        } catch (IllegalArgumentException e) {
        }

        // One constructor
        ZipEntry zeInput = new ZipEntry("InputZIP");
        byte[] extraB = { 'a', 'b', 'd', 'e' };
        zeInput.setExtra(extraB);
        assertEquals(extraB, zeInput.getExtra());
        assertEquals(extraB[3], zeInput.getExtra()[3]);
        assertEquals(extraB.length, zeInput.getExtra().length);

        // test another constructor
        ZipEntry zeOutput = new ZipEntry(zeInput);
        assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]);
        assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length);
        assertEquals(extraB[3], zeOutput.getExtra()[3]);
        assertEquals(extraB.length, zeOutput.getExtra().length);
    }

    /**
     * java.util.zip.ZipEntry#setMethod(int)
     */
    public void test_setMethodI() {
        // Test for method void java.util.zip.ZipEntry.setMethod(int)
        zentry = zfile.getEntry("File3.txt");
        zentry.setMethod(ZipEntry.STORED);
        assertEquals("Failed to set compression method",
                ZipEntry.STORED, zentry.getMethod());
        zentry.setMethod(ZipEntry.DEFLATED);
        assertEquals("Failed to set compression method",
                ZipEntry.DEFLATED, zentry.getMethod());
        try {
            int error = 1;
            zentry = new ZipEntry("test.tst");
            zentry.setMethod(error);
            fail("IllegalArgumentException not thrown");
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * java.util.zip.ZipEntry#setSize(long)
     */
    public void test_setSizeJ() {
        // Test for method void java.util.zip.ZipEntry.setSize(long)
        zentry.setSize(orgSize + 10);
        assertEquals("Set size failed", (orgSize + 10), zentry.getSize());
        zentry.setSize(0);
        assertEquals("Set size failed", 0, zentry.getSize());
        try {
            zentry.setSize(-25);
            fail("IllegalArgumentException not thrown");
        } catch (IllegalArgumentException e) {
        }
        try {
            zentry.setCrc(4294967295l);
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException during test");
        }
        try {
            zentry.setCrc(4294967296l);
            fail("IllegalArgumentException not thrown");
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * java.util.zip.ZipEntry#setTime(long)
     */
    public void test_setTimeJ() {
        // Test for method void java.util.zip.ZipEntry.setTime(long)
        zentry.setTime(orgTime + 10000);
        assertEquals("Test 1: Failed to set time: " + zentry.getTime(), (orgTime + 10000),
                zentry.getTime());
        zentry.setTime(orgTime - 10000);
        assertEquals("Test 2: Failed to set time: " + zentry.getTime(), (orgTime - 10000),
                zentry.getTime());
        TimeZone zone = TimeZone.getDefault();
        try {
            TimeZone.setDefault(TimeZone.getTimeZone("EST"));
            zentry.setTime(0);
            assertEquals("Test 3: Failed to set time: " + zentry.getTime(),
                    315550800000L, zentry.getTime());
            TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
            assertEquals("Test 3a: Failed to set time: " + zentry.getTime(),
                    315532800000L, zentry.getTime());
            zentry.setTime(0);
            TimeZone.setDefault(TimeZone.getTimeZone("EST"));
            assertEquals("Test 3b: Failed to set time: " + zentry.getTime(),
                    315550800000L, zentry.getTime());

            zentry.setTime(-25);
            assertEquals("Test 4: Failed to set time: " + zentry.getTime(),
                    315550800000L, zentry.getTime());
            zentry.setTime(4354837200000L);
            assertEquals("Test 5: Failed to set time: " + zentry.getTime(),
                    315550800000L, zentry.getTime());
        } finally {
            TimeZone.setDefault(zone);
        }
    }

    /**
     * java.util.zip.ZipEntry#toString()
     */
    public void test_toString() {
        // Test for method java.lang.String java.util.zip.ZipEntry.toString()
        assertTrue("Returned incorrect entry name", zentry.toString().indexOf(
                "File1.txt") >= 0);
    }

    /**
     * java.util.zip.ZipEntry#ZipEntry(java.util.zip.ZipEntry)
     */
    public void test_ConstructorLjava_util_zip_ZipEntry() {
        // Test for method java.util.zip.ZipEntry(util.zip.ZipEntry)
        zentry.setSize(2);
        zentry.setCompressedSize(4);
        zentry.setComment("Testing");
        ZipEntry zentry2 = new ZipEntry(zentry);
        assertEquals("ZipEntry Created With Incorrect Size.",
                2, zentry2.getSize());
        assertEquals("ZipEntry Created With Incorrect Compressed Size.", 4, zentry2
                .getCompressedSize());
        assertEquals("ZipEntry Created With Incorrect Comment.", "Testing", zentry2
                .getComment());
        assertEquals("ZipEntry Created With Incorrect Crc.",
                orgCrc, zentry2.getCrc());
        assertEquals("ZipEntry Created With Incorrect Time.",
                orgTime, zentry2.getTime());
    }

    /**
     * java.util.zip.ZipEntry#clone()
     */
    public void test_clone() {
        // Test for method java.util.zip.ZipEntry.clone()
        Object obj = zentry.clone();
        assertEquals("toString()", zentry.toString(), obj.toString());
        assertEquals("hashCode()", zentry.hashCode(), obj.hashCode());

        // One constructor
        ZipEntry zeInput = new ZipEntry("InputZIP");
        byte[] extraB = { 'a', 'b', 'd', 'e' };
        zeInput.setExtra(extraB);
        assertEquals(extraB, zeInput.getExtra());
        assertEquals(extraB[3], zeInput.getExtra()[3]);
        assertEquals(extraB.length, zeInput.getExtra().length);

        // test Clone()
        ZipEntry zeOutput = (ZipEntry) zeInput.clone();
        assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]);
        assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length);
        assertEquals(extraB[3], zeOutput.getExtra()[3]);
        assertEquals(extraB.length, zeOutput.getExtra().length);
    }

    @Override
    protected void setUp() throws Exception {
        // Create a local copy of the file since some tests want to alter
        // information.
        final File f = File.createTempFile("ZipEntryTest", ".zip");
        InputStream is = Support_Resources.getStream("hyts_ZipFile.zip");

        FileOutputStream fos = new java.io.FileOutputStream(f);
        Streams.copy(is, fos);
        is.close();
        fos.close();

        zfile = new ZipFile(f);
        zentry = zfile.getEntry("File1.txt");

        orgSize = zentry.getSize();
        orgCompressedSize = zentry.getCompressedSize();
        orgCrc = zentry.getCrc();
        orgTime = zentry.getTime();
    }

    @Override
    protected void tearDown() {
        try {
            if (zfile != null) {
                zfile.close();
            }
        } catch (IOException ignored) {
        }
    }
}