View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.archivers.jar;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.compress.AbstractTestCase;
28  import org.apache.commons.compress.archivers.zip.JarMarker;
29  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
30  import org.apache.commons.compress.archivers.zip.ZipExtraField;
31  import org.apache.commons.compress.archivers.zip.ZipFile;
32  
33  public class JarArchiveOutputStreamTest extends TestCase {
34  
35      public void testJarMarker() throws IOException {
36          File testArchive = File.createTempFile("jar-aostest", ".jar");
37          testArchive.deleteOnExit();
38          JarArchiveOutputStream out = null;
39          ZipFile zf = null;
40          try {
41  
42              out = new JarArchiveOutputStream(new FileOutputStream(testArchive));
43              out.putArchiveEntry(new ZipArchiveEntry("foo/"));
44              out.closeArchiveEntry();
45              out.putArchiveEntry(new ZipArchiveEntry("bar/"));
46              out.closeArchiveEntry();
47              out.finish();
48              out.close();
49              out = null;
50  
51              zf = new ZipFile(testArchive);
52              ZipArchiveEntry ze = zf.getEntry("foo/");
53              assertNotNull(ze);
54              ZipExtraField[] fes = ze.getExtraFields();
55              assertEquals(1, fes.length);
56              assertTrue(fes[0] instanceof JarMarker);
57  
58              ze = zf.getEntry("bar/");
59              assertNotNull(ze);
60              fes = ze.getExtraFields();
61              assertEquals(0, fes.length);
62          } finally {
63              if (out != null) {
64                  try {
65                      out.close();
66                  } catch (IOException e) { /* swallow */ }
67              }
68              ZipFile.closeQuietly(zf);
69              AbstractTestCase.tryHardToDelete(testArchive);
70          }
71      }
72  
73  }