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  
20  package org.apache.commons.compress.archivers.ar;
21  
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.File;
27  import java.io.IOException;
28  import java.nio.file.Files;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.apache.commons.compress.AbstractTest;
33  import org.junit.jupiter.api.Test;
34  
35  class ArArchiveOutputStreamTest extends AbstractTest {
36  
37      @Test
38      void testLongFileNamesCauseExceptionByDefault() throws IOException {
39          final ArArchiveOutputStream ref;
40          try (ArArchiveOutputStream outputStream = new ArArchiveOutputStream(new ByteArrayOutputStream())) {
41              ref = outputStream;
42              final ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt", 0);
43              final IOException ex = assertThrows(IOException.class, () -> outputStream.putArchiveEntry(ae));
44              assertTrue(ex.getMessage().startsWith("File name too long"));
45          }
46          assertTrue(ref.isClosed());
47      }
48  
49      @Test
50      void testLongFileNamesWorkUsingBSDDialect() throws Exception {
51          final File file = createTempFile();
52          try (ArArchiveOutputStream outputStream = new ArArchiveOutputStream(Files.newOutputStream(file.toPath()))) {
53              outputStream.setLongFileMode(ArArchiveOutputStream.LONGFILE_BSD);
54              final ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt", 14);
55              outputStream.putArchiveEntry(ae);
56              outputStream.write(new byte[] { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n' });
57              outputStream.closeArchiveEntry();
58              final List<String> expected = new ArrayList<>();
59              expected.add("this_is_a_long_name.txt");
60              checkArchiveContent(file, expected);
61          }
62      }
63  }