aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java
blob: 5a7a3590515cc7b78270cee40fd8c726f42b8ebd (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
/*
 *  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.tools.ant.taskdefs;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.zip.UnixStat;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

import static org.apache.tools.ant.AntAssert.assertContains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;


public class ZipTest {
    
    @Rule
    public BuildFileRule buildRule = new BuildFileRule();
            
    //instance variable to allow cleanup
    ZipFile zfPrefixAddsDir = null;


    @Before
    public void setUp() {
        buildRule.configureProject("src/etc/testcases/taskdefs/zip.xml");
        buildRule.executeTarget("setUp");
    }

    @Test
    public void test1() {
        try {
			buildRule.executeTarget("test1");
			fail("BuildException expected: required argument not specified");
		} catch (BuildException ex) {
			//TODO assert value
		}
    }

    @Test
    public void test2() {
        try {
			buildRule.executeTarget("test2");
			fail("BuildException expected: required argument not specified");
		} catch (BuildException ex) {
			//TODO assert value
		}
    }

    @Test
    public void test3() {
        try {
			buildRule.executeTarget("test3");
			fail("BuildException expected: zip cannot include itself");
		} catch (BuildException ex) {
			//TODO assert value
		}
    }

    @Test
    @Ignore("Previously commented out")
    public void test4() {
        try {
			buildRule.executeTarget("test4");
			fail("BuildException expected: zip cannot include itself");
		} catch (BuildException ex) {
			//TODO assert value
		}
    }

    @After
    public void tearDown() {
        try {
            if ( zfPrefixAddsDir != null) {
                zfPrefixAddsDir.close();
            }

        } catch (IOException e) {
            //ignored
        }
    }

    @Test
    public void test5() {
       buildRule.executeTarget("test5");
    }


    @Test
    public void test6() {
       buildRule.executeTarget("test6");
    }


    @Test
    public void test7() {
       buildRule.executeTarget("test7");
    }

    @Test
    public void test8() {
       buildRule.executeTarget("test8");
    }

    @Test
    public void testZipgroupfileset() throws IOException {
       buildRule.executeTarget("testZipgroupfileset");

        ZipFile zipFile = new ZipFile(new File(buildRule.getProject().getProperty("output"), "zipgroupfileset.zip"));

        assertTrue(zipFile.getEntry("ant.xml") != null);
        assertTrue(zipFile.getEntry("optional/jspc.xml") != null);
        assertTrue(zipFile.getEntry("zip/zipgroupfileset3.zip") != null);

        assertTrue(zipFile.getEntry("test6.mf") == null);
        assertTrue(zipFile.getEntry("test7.mf") == null);

        zipFile.close();
    }

    @Test
    public void testUpdateNotNecessary() {
       buildRule.executeTarget("testUpdateNotNecessary");
        assertEquals(-1, buildRule.getLog().indexOf("Updating"));
    }

    @Test
    public void testUpdateIsNecessary() {
        buildRule.executeTarget("testUpdateIsNecessary");
		assertContains("Updating", buildRule.getLog());
    }

    // Bugzilla Report 18403
    @Test
    public void testPrefixAddsDir() throws IOException {
       buildRule.executeTarget("testPrefixAddsDir");
        File archive = new File(buildRule.getProject().getProperty("output"), "test3.zip");
        zfPrefixAddsDir = new ZipFile(archive);
        ZipEntry ze = zfPrefixAddsDir.getEntry("test/");
        assertNotNull("test/ has been added", ze);

    }

    // Bugzilla Report 19449
    @Test
    public void testFilesOnlyDoesntCauseRecreate() {
        buildRule.executeTarget("testFilesOnlyDoesntCauseRecreateSetup");
        File testFile = new File(buildRule.getOutputDir(), "test3.zip");
        assumeTrue("Could not change file modification time",
                testFile.setLastModified(testFile.lastModified() - (FileUtils.getFileUtils().getFileTimestampGranularity() * 5)));
        long l = testFile.lastModified();

        buildRule.executeTarget("testFilesOnlyDoesntCauseRecreate");
        assertEquals(l, testFile.lastModified());
    }

    // Bugzilla Report 22865
    @Test
    public void testEmptySkip() {
       buildRule.executeTarget("testEmptySkip");
    }
    // Bugzilla Report 30365
    @Test
    public void testZipEmptyDir() {
       buildRule.executeTarget("zipEmptyDir");
    }
    // Bugzilla Report 40258
    @Test
    public void testZipEmptyDirFilesOnly() {
       buildRule.executeTarget("zipEmptyDirFilesOnly");
    }
    @Test
    public void testZipEmptyCreate() {
        buildRule.executeTarget("zipEmptyCreate");
		assertContains("Note: creating empty", buildRule.getLog());
    }
    // Bugzilla Report 25513
    @Test
    public void testCompressionLevel() {
       buildRule.executeTarget("testCompressionLevel");
    }

    // Bugzilla Report 33412
    @Test
    public void testDefaultExcludesAndUpdate()
        throws ZipException, IOException {
       buildRule.executeTarget("testDefaultExcludesAndUpdate");
        ZipFile f = null;
        try {
            f = new ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
            assertNotNull("ziptest~ should be included",
                          f.getEntry("ziptest~"));
        } finally {
            if (f != null) {
                f.close();
            }
        }
    }

    @Test
    public void testFileResource() {
       buildRule.executeTarget("testFileResource");
    }

    @Test
    public void testNonFileResource() {
       buildRule.executeTarget("testNonFileResource");
    }

    @Test
    public void testTarFileSet() throws IOException {
       buildRule.executeTarget("testTarFileSet");
        org.apache.tools.zip.ZipFile zf = null;
        try {
            zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
            org.apache.tools.zip.ZipEntry ze = zf.getEntry("asf-logo.gif");
            assertEquals(UnixStat.FILE_FLAG | 0446, ze.getUnixMode());
        } finally {
            if (zf != null) {
                zf.close();
            }
        }
    }

    @Test
    public void testRewriteZeroPermissions() throws IOException {
       buildRule.executeTarget("rewriteZeroPermissions");
        org.apache.tools.zip.ZipFile zf = null;
        try {
            zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
            org.apache.tools.zip.ZipEntry ze = zf.getEntry("testdir/test.txt");
            assertEquals(UnixStat.FILE_FLAG | 0644, ze.getUnixMode());
        } finally {
            if (zf != null) {
                zf.close();
            }
        }
    }

    @Test
    public void testAcceptZeroPermissions() throws IOException {
       buildRule.executeTarget("acceptZeroPermissions");
        org.apache.tools.zip.ZipFile zf = null;
        try {
            zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
            org.apache.tools.zip.ZipEntry ze = zf.getEntry("testdir/test.txt");
            assertEquals(0000, ze.getUnixMode());
        } finally {
            if (zf != null) {
                zf.close();
            }
        }
    }

    @Test
    public void testForBugzilla34764() throws IOException {
       buildRule.executeTarget("testForBugzilla34764");
        org.apache.tools.zip.ZipFile zf = null;
        try {
            zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"));
            org.apache.tools.zip.ZipEntry ze = zf.getEntry("file1");
            assertEquals(UnixStat.FILE_FLAG | 0644, ze.getUnixMode());
        } finally {
            if (zf != null) {
                zf.close();
            }
        }
    }

}