1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }