001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.zip;
020:
021: import junit.framework.TestCase;
022:
023: /**
024: * JUnit 3 testcases for org.apache.tools.zip.ZipEntry.
025: *
026: */
027: public class ZipEntryTest extends TestCase {
028:
029: public ZipEntryTest(String name) {
030: super (name);
031: }
032:
033: /**
034: * test handling of extra fields
035: *
036: * @since 1.1
037: */
038: public void testExtraFields() {
039: AsiExtraField a = new AsiExtraField();
040: a.setDirectory(true);
041: a.setMode(0755);
042: UnrecognizedExtraField u = new UnrecognizedExtraField();
043: u.setHeaderId(new ZipShort(1));
044: u.setLocalFileDataData(new byte[0]);
045:
046: ZipEntry ze = new ZipEntry("test/");
047: ze.setExtraFields(new ZipExtraField[] { a, u });
048: byte[] data1 = ze.getExtra();
049: ZipExtraField[] result = ze.getExtraFields();
050: assertEquals("first pass", 2, result.length);
051: assertSame(a, result[0]);
052: assertSame(u, result[1]);
053:
054: UnrecognizedExtraField u2 = new UnrecognizedExtraField();
055: u2.setHeaderId(new ZipShort(1));
056: u2.setLocalFileDataData(new byte[] { 1 });
057:
058: ze.addExtraField(u2);
059: byte[] data2 = ze.getExtra();
060: result = ze.getExtraFields();
061: assertEquals("second pass", 2, result.length);
062: assertSame(a, result[0]);
063: assertSame(u2, result[1]);
064: assertEquals("length second pass", data1.length + 1,
065: data2.length);
066:
067: UnrecognizedExtraField u3 = new UnrecognizedExtraField();
068: u3.setHeaderId(new ZipShort(2));
069: u3.setLocalFileDataData(new byte[] { 1 });
070: ze.addExtraField(u3);
071: result = ze.getExtraFields();
072: assertEquals("third pass", 3, result.length);
073:
074: ze.removeExtraField(new ZipShort(1));
075: byte[] data3 = ze.getExtra();
076: result = ze.getExtraFields();
077: assertEquals("fourth pass", 2, result.length);
078: assertSame(a, result[0]);
079: assertSame(u3, result[1]);
080: assertEquals("length fourth pass", data2.length, data3.length);
081:
082: try {
083: ze.removeExtraField(new ZipShort(1));
084: fail("should be no such element");
085: } catch (java.util.NoSuchElementException nse) {
086: }
087: }
088:
089: public void testUnixMode() {
090: ZipEntry ze = new ZipEntry("foo");
091: assertEquals(0, ze.getPlatform());
092: ze.setUnixMode(0755);
093: assertEquals(3, ze.getPlatform());
094: assertEquals(0755, (ze.getExternalAttributes() >> 16) & 0xFFFF);
095: assertEquals(0, ze.getExternalAttributes() & 0xFFFF);
096:
097: ze.setUnixMode(0444);
098: assertEquals(3, ze.getPlatform());
099: assertEquals(0444, (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, (ze.getExternalAttributes() >> 16) & 0xFFFF);
107: assertEquals(0x10, ze.getExternalAttributes() & 0xFFFF);
108:
109: ze.setUnixMode(0577);
110: assertEquals(3, ze.getPlatform());
111: assertEquals(0577, (ze.getExternalAttributes() >> 16) & 0xFFFF);
112: assertEquals(0x11, ze.getExternalAttributes() & 0xFFFF);
113: }
114:
115: }
|