1 | /*
|
---|
2 | * Copyright 2001-2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.zip;
|
---|
19 |
|
---|
20 | import junit.framework.TestCase;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * JUnit 3 testcases for org.apache.tools.zip.ZipEntry.
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class ZipEntryTest extends TestCase {
|
---|
27 |
|
---|
28 | public ZipEntryTest(String name) {
|
---|
29 | super(name);
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * test handling of extra fields
|
---|
34 | *
|
---|
35 | * @since 1.1
|
---|
36 | */
|
---|
37 | public void testExtraFields() {
|
---|
38 | AsiExtraField a = new AsiExtraField();
|
---|
39 | a.setDirectory(true);
|
---|
40 | a.setMode(0755);
|
---|
41 | UnrecognizedExtraField u = new UnrecognizedExtraField();
|
---|
42 | u.setHeaderId(new ZipShort(1));
|
---|
43 | u.setLocalFileDataData(new byte[0]);
|
---|
44 |
|
---|
45 | ZipEntry ze = new ZipEntry("test/");
|
---|
46 | ze.setExtraFields(new ZipExtraField[] {a, u});
|
---|
47 | byte[] data1 = ze.getExtra();
|
---|
48 | ZipExtraField[] result = ze.getExtraFields();
|
---|
49 | assertEquals("first pass", 2, result.length);
|
---|
50 | assertSame(a, result[0]);
|
---|
51 | assertSame(u, result[1]);
|
---|
52 |
|
---|
53 | UnrecognizedExtraField u2 = new UnrecognizedExtraField();
|
---|
54 | u2.setHeaderId(new ZipShort(1));
|
---|
55 | u2.setLocalFileDataData(new byte[] {1});
|
---|
56 |
|
---|
57 | ze.addExtraField(u2);
|
---|
58 | byte[] data2 = ze.getExtra();
|
---|
59 | result = ze.getExtraFields();
|
---|
60 | assertEquals("second pass", 2, result.length);
|
---|
61 | assertSame(a, result[0]);
|
---|
62 | assertSame(u2, result[1]);
|
---|
63 | assertEquals("length second pass", data1.length+1, data2.length);
|
---|
64 |
|
---|
65 | UnrecognizedExtraField u3 = new UnrecognizedExtraField();
|
---|
66 | u3.setHeaderId(new ZipShort(2));
|
---|
67 | u3.setLocalFileDataData(new byte[] {1});
|
---|
68 | ze.addExtraField(u3);
|
---|
69 | result = ze.getExtraFields();
|
---|
70 | assertEquals("third pass", 3, result.length);
|
---|
71 |
|
---|
72 | ze.removeExtraField(new ZipShort(1));
|
---|
73 | byte[] data3 = ze.getExtra();
|
---|
74 | result = ze.getExtraFields();
|
---|
75 | assertEquals("fourth pass", 2, result.length);
|
---|
76 | assertSame(a, result[0]);
|
---|
77 | assertSame(u3, result[1]);
|
---|
78 | assertEquals("length fourth pass", data2.length, data3.length);
|
---|
79 |
|
---|
80 | try {
|
---|
81 | ze.removeExtraField(new ZipShort(1));
|
---|
82 | fail("should be no such element");
|
---|
83 | } catch (java.util.NoSuchElementException nse) {
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void testUnixMode() {
|
---|
88 | ZipEntry ze = new ZipEntry("foo");
|
---|
89 | assertEquals(0, ze.getPlatform());
|
---|
90 | ze.setUnixMode(0755);
|
---|
91 | assertEquals(3, ze.getPlatform());
|
---|
92 | assertEquals(0755,
|
---|
93 | (ze.getExternalAttributes() >> 16) & 0xFFFF);
|
---|
94 | assertEquals(0, ze.getExternalAttributes() & 0xFFFF);
|
---|
95 |
|
---|
96 | ze.setUnixMode(0444);
|
---|
97 | assertEquals(3, ze.getPlatform());
|
---|
98 | assertEquals(0444,
|
---|
99 | (ze.getExternalAttributes() >> 16) & 0xFFFF);
|
---|
100 | assertEquals(1, ze.getExternalAttributes() & 0xFFFF);
|
---|
101 |
|
---|
102 | ze = new ZipEntry("foo/");
|
---|
103 | assertEquals(0, ze.getPlatform());
|
---|
104 | ze.setUnixMode(0777);
|
---|
105 | assertEquals(3, ze.getPlatform());
|
---|
106 | assertEquals(0777,
|
---|
107 | (ze.getExternalAttributes() >> 16) & 0xFFFF);
|
---|
108 | assertEquals(0x10, ze.getExternalAttributes() & 0xFFFF);
|
---|
109 |
|
---|
110 | ze.setUnixMode(0577);
|
---|
111 | assertEquals(3, ze.getPlatform());
|
---|
112 | assertEquals(0577,
|
---|
113 | (ze.getExternalAttributes() >> 16) & 0xFFFF);
|
---|
114 | assertEquals(0x11, ze.getExternalAttributes() & 0xFFFF);
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|