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          ArArchiveOutputStream os = null;
38          try {
39              os = new ArArchiveOutputStream(new ByteArrayOutputStream());
40              ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt",
41                                                     0);
42              os.putArchiveEntry(ae);
43              fail("Expected an exception");
44          } catch (IOException ex) {
45              assertTrue(ex.getMessage().startsWith("filename too long"));
46          } finally {
47              closeQuietly(os);
48          }
49      }
50  
51      @Test
52      public void testLongFileNamesWorkUsingBSDDialect() throws Exception {
53          FileOutputStream fos = null;
54          ArArchiveOutputStream os = null;
55          File[] df = createTempDirAndFile();
56          try {
57              fos = new FileOutputStream(df[1]);
58              os = new ArArchiveOutputStream(fos);
59              os.setLongFileMode(ArArchiveOutputStream.LONGFILE_BSD);
60              ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt",
61                                                     14);
62              os.putArchiveEntry(ae);
63              os.write(new byte[] {
64                      'H', 'e', 'l', 'l', 'o', ',', ' ',
65                      'w', 'o', 'r', 'l', 'd', '!', '\n'
66                  });
67              os.closeArchiveEntry();
68              os.close();
69              os = null;
70              fos = null;
71  
72              List<String> expected = new ArrayList<String>();
73              expected.add("this_is_a_long_name.txt");
74              checkArchiveContent(df[1], expected);
75          } finally {
76              if (os != null) {
77                  os.close();
78              }
79              if (fos != null) {
80                  fos.close();
81              }
82              rmdir(df[0]);
83          }
84      }
85  }