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   *   https://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.zip;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.IOException;
27  import java.nio.charset.Charset;
28  import java.nio.charset.StandardCharsets;
29  import java.util.zip.Deflater;
30  import java.util.zip.ZipEntry;
31  
32  import org.apache.commons.compress.AbstractTempDirTest;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests {@link ZipArchiveOutputStream}.
37   */
38  class ZipArchiveOutputStreamTest extends AbstractTempDirTest {
39  
40      @Test
41      void testFileBasics() throws IOException {
42          final ZipArchiveOutputStream ref;
43          try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(createTempFile())) {
44              ref = outputStream;
45              assertTrue(outputStream.isSeekable());
46          }
47          assertTrue(ref.isClosed());
48      }
49  
50      @Test
51      void testOptionDefaults() throws IOException {
52          final ZipArchiveOutputStream ref;
53          try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(createTempFile())) {
54              ref = outputStream;
55              assertTrue(outputStream.isSeekable());
56              outputStream.setComment("");
57              outputStream.setLevel(Deflater.DEFAULT_COMPRESSION);
58              outputStream.setMethod(ZipEntry.DEFLATED);
59              outputStream.setFallbackToUTF8(false);
60          }
61          assertTrue(ref.isClosed());
62      }
63  
64      @Test
65      void testOutputStreamBasics() throws IOException {
66          try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(new ByteArrayOutputStream())) {
67              assertFalse(outputStream.isSeekable());
68          }
69      }
70  
71      @Test
72      void testSetEncoding() throws IOException {
73          try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(createTempFile())) {
74              outputStream.setEncoding(StandardCharsets.UTF_8.name());
75              assertEquals(StandardCharsets.UTF_8.name(), outputStream.getEncoding());
76              outputStream.setEncoding(null);
77              assertEquals(Charset.defaultCharset().name(), outputStream.getEncoding());
78          }
79      }
80  }