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