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 java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.commons.compress.AbstractTestCase;
29  
30  public class ArArchiveOutputStreamTest extends AbstractTestCase {
31  
32      public void testLongFileNamesCauseExceptionByDefault() {
33          ArArchiveOutputStream os = null;
34          try {
35              os = new ArArchiveOutputStream(new ByteArrayOutputStream());
36              ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt",
37                                                     0);
38              os.putArchiveEntry(ae);
39              fail("Expected an exception");
40          } catch (IOException ex) {
41              assertTrue(ex.getMessage().startsWith("filename too long"));
42          } finally {
43              closeQuietly(os);
44          }
45      }
46  
47      public void testLongFileNamesWorkUsingBSDDialect() throws Exception {
48          FileOutputStream fos = null;
49          ArArchiveOutputStream os = null;
50          File[] df = createTempDirAndFile();
51          try {
52              fos = new FileOutputStream(df[1]);
53              os = new ArArchiveOutputStream(fos);
54              os.setLongFileMode(ArArchiveOutputStream.LONGFILE_BSD);
55              ArArchiveEntry ae = new ArArchiveEntry("this_is_a_long_name.txt",
56                                                     14);
57              os.putArchiveEntry(ae);
58              os.write(new byte[] {
59                      'H', 'e', 'l', 'l', 'o', ',', ' ',
60                      'w', 'o', 'r', 'l', 'd', '!', '\n'
61                  });
62              os.closeArchiveEntry();
63              os.close();
64              os = null;
65              fos = null;
66  
67              List<String> expected = new ArrayList<String>();
68              expected.add("this_is_a_long_name.txt");
69              checkArchiveContent(df[1], expected);
70          } finally {
71              if (os != null) {
72                  os.close();
73              }
74              if (fos != null) {
75                  fos.close();
76              }
77              rmdir(df[0]);
78          }
79      }
80  }