View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  
19  package org.apache.commons.compress.archivers.ar;
20  
21  import static org.junit.Assert.*;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.commons.compress.AbstractTestCase;
31  import org.junit.Test;
32  
33  public class ArArchiveOutputStreamTest extends AbstractTestCase {
34  
35      @Test
36      public void testLongFileNamesCauseExceptionByDefault() {
37          try (ArArchiveOutputStream os = new ArArchiveOutputStream(new ByteArrayOutputStream())) {
38              final ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt", 0);
39              os.putArchiveEntry(ae);
40              fail("Expected an exception");
41          } catch (final IOException ex) {
42              assertTrue(ex.getMessage().startsWith("filename too long"));
43          }
44      }
45  
46      @Test
47      public void testLongFileNamesWorkUsingBSDDialect() throws Exception {
48          final File[] df = createTempDirAndFile();
49          try (FileOutputStream fos = new FileOutputStream(df[1]);
50                  ArArchiveOutputStream os = new ArArchiveOutputStream(fos)) {
51              os.setLongFileMode(ArArchiveOutputStream.LONGFILE_BSD);
52              final ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt", 14);
53              os.putArchiveEntry(ae);
54              os.write(new byte[] { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n' });
55              os.closeArchiveEntry();
56  
57              final List<String> expected = new ArrayList<>();
58              expected.add("this_is_a_long_name.txt");
59              checkArchiveContent(df[1], expected);
60          } finally {
61              rmdir(df[0]);
62          }
63      }
64  }