View Javadoc
1   /*
2    * Copyright  2003-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.codehaus.plexus.archiver.tar;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
23  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
24  import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
25  import junit.framework.TestCase;
26  
27  /**
28   * from org.apache.ant.tools.tar.TarRoundTripTest v1.6
29   */
30  public class TarRoundTripTest
31      extends TestCase
32  {
33  
34      private static final String LONG_NAME =
35          "this/path/name/contains/more/than/one/hundred/characters/in/order/"
36              + "to/test/the/GNU/long/file/name/capability/round/tripped";
37  
38      /**
39       * test round-tripping long (GNU) entries
40       */
41      public void testLongRoundTripping()
42          throws IOException
43      {
44          TarArchiveEntry original = new TarArchiveEntry( LONG_NAME );
45          assertEquals( "over 100 chars", true, LONG_NAME.length() > 100 );
46          assertEquals( "original name", LONG_NAME, original.getName() );
47  
48          ByteArrayOutputStream buff = new ByteArrayOutputStream();
49          TarArchiveOutputStream tos = new TarArchiveOutputStream( buff );
50          tos.setLongFileMode( TarArchiveOutputStream.LONGFILE_GNU );
51          tos.putArchiveEntry( original );
52          tos.closeArchiveEntry();
53          tos.close();
54  
55          TarArchiveInputStream tis = new TarArchiveInputStream( new ByteArrayInputStream( buff.toByteArray() ) );
56          TarArchiveEntry tripped = tis.getNextTarEntry();
57          assertEquals( "round-tripped name", LONG_NAME, tripped.getName() );
58          assertNull( "no more entries", tis.getNextEntry() );
59          tis.close();
60      }
61  
62  }