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 static org.junit.Assert.*;
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  
27  import org.junit.Test;
28  import org.apache.commons.compress.AbstractTestCase;
29  import org.apache.commons.compress.archivers.zip.JarMarker;
30  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
31  import org.apache.commons.compress.archivers.zip.ZipExtraField;
32  import org.apache.commons.compress.archivers.zip.ZipFile;
33  
34  public class JarArchiveOutputStreamTest {
35  
36      @Test
37      public void testJarMarker() throws IOException {
38          File testArchive = File.createTempFile("jar-aostest", ".jar");
39          testArchive.deleteOnExit();
40          JarArchiveOutputStream out = null;
41          ZipFile zf = null;
42          try {
43  
44              out = new JarArchiveOutputStream(new FileOutputStream(testArchive));
45              out.putArchiveEntry(new ZipArchiveEntry("foo/"));
46              out.closeArchiveEntry();
47              out.putArchiveEntry(new ZipArchiveEntry("bar/"));
48              out.closeArchiveEntry();
49              out.finish();
50              out.close();
51              out = null;
52  
53              zf = new ZipFile(testArchive);
54              ZipArchiveEntry ze = zf.getEntry("foo/");
55              assertNotNull(ze);
56              ZipExtraField[] fes = ze.getExtraFields();
57              assertEquals(1, fes.length);
58              assertTrue(fes[0] instanceof JarMarker);
59  
60              ze = zf.getEntry("bar/");
61              assertNotNull(ze);
62              fes = ze.getExtraFields();
63              assertEquals(0, fes.length);
64          } finally {
65              if (out != null) {
66                  try {
67                      out.close();
68                  } catch (IOException e) { /* swallow */ }
69              }
70              ZipFile.closeQuietly(zf);
71              AbstractTestCase.tryHardToDelete(testArchive);
72          }
73      }
74  
75  }