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  package org.apache.commons.vfs2.provider.tar;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  import java.io.File;
23  
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSystemException;
26  import org.apache.commons.vfs2.FileSystemManager;
27  import org.apache.commons.vfs2.VFS;
28  import org.junit.jupiter.api.Test;
29  
30  public class TarFileObjectTest {
31  
32      private void testReadSpecialNameFileInFile(final String testFilePath, final String scheme) throws FileSystemException {
33  
34          final File testFile = new File(testFilePath);
35          final String[] fileNames = {"file.txt", "file^.txt", "file~.txt", "file?.txt", "file@.txt", "file$.txt",
36                                      "file*.txt", "file&.txt", "file#.txt", "file%.txt", "file!.txt"};
37          final FileSystemManager manager = VFS.getManager();
38          final String baseUrl = scheme + ":file:" + testFile.getAbsolutePath();
39  
40          // test
41          try (FileObject fileObject = manager.resolveFile(baseUrl)) {
42              // test getChildren() number equal
43              assertEquals(fileObject.getChildren().length, fileNames.length);
44  
45              // test getChild(String)
46              for (final String fileName : fileNames) {
47                  assertNotNull(fileObject.getChild(fileName), "can't read file " + fileName);
48              }
49          }
50      }
51  
52      /**
53       * Test read file with special name in a tar file.
54       */
55      @Test
56      public void testReadSpecialNameFileInTarFile() throws FileSystemException {
57  
58          testReadSpecialNameFileInFile("src/test/resources/test-data/special_fileName.tar", "tar");
59      }
60  
61      /**
62       * Test read file with special name in a tbz2 file.
63       */
64      @Test
65      public void testReadSpecialNameFileInTbz2File() throws FileSystemException {
66  
67          testReadSpecialNameFileInFile("src/test/resources/test-data/special_fileName.tbz2", "tbz2");
68      }
69  
70      /**
71       * Test read file with special name in a tgz file.
72       */
73      @Test
74      public void testReadSpecialNameFileInTgzFile() throws FileSystemException {
75  
76          testReadSpecialNameFileInFile("src/test/resources/test-data/special_fileName.tgz", "tgz");
77      }
78  
79  }