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  
23  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
24  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
25  import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertNull;
30  import static org.junit.jupiter.api.Assertions.assertTrue;
31  
32  /**
33   * from org.apache.ant.tools.tar.TarRoundTripTest v1.6
34   */
35  class TarRoundTripTest {
36  
37      private static final String LONG_NAME = "this/path/name/contains/more/than/one/hundred/characters/in/order/"
38              + "to/test/the/GNU/long/file/name/capability/round/tripped";
39  
40      /**
41       * test round-tripping long (GNU) entries
42       */
43      @Test
44      void testLongRoundTripping() throws IOException {
45          TarArchiveEntry original = new TarArchiveEntry(LONG_NAME);
46          assertTrue(LONG_NAME.length() > 100, "over 100 chars");
47          assertEquals(LONG_NAME, original.getName(), "original name");
48  
49          ByteArrayOutputStream buff = new ByteArrayOutputStream();
50          TarArchiveOutputStream tos = new TarArchiveOutputStream(buff);
51          tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
52          tos.putArchiveEntry(original);
53          tos.closeArchiveEntry();
54          tos.close();
55  
56          TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(buff.toByteArray()));
57          TarArchiveEntry tripped = tis.getNextTarEntry();
58          assertEquals(LONG_NAME, tripped.getName(), "round-tripped name");
59          assertNull(tis.getNextEntry(), "no more entries");
60          tis.close();
61      }
62  }