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  package org.apache.commons.vfs2;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  import java.nio.charset.StandardCharsets;
29  import java.nio.file.FileSystems;
30  import java.nio.file.Paths;
31  
32  import org.apache.commons.io.FileUtils;
33  import org.apache.commons.io.IOUtils;
34  import org.apache.commons.lang3.SystemUtils;
35  import org.apache.commons.lang3.function.FailableFunction;
36  import org.apache.commons.vfs2.impl.StandardFileSystemManager;
37  import org.junit.Test;
38  
39  public class FileObjectEscapeCharacterInPathTest {
40  
41      private static final String REL_PATH_GREAT = "src/test/resources/test-data/好.txt";
42  
43      private static final String REL_PATH_SPACE = "src/test/resources/test-data/1 1.txt";
44  
45      /**
46       * Expected contents of test files.
47       */
48      public static final String TEST_FILE_CONTENT = "aaa";
49  
50      /**
51       * Test file paths.
52       */
53      public static final String[] TEST_FILE_PATHS = new String[] {REL_PATH_SPACE, REL_PATH_GREAT};
54  
55      private static StandardFileSystemManager loadFileSystemManager() throws FileSystemException {
56          StandardFileSystemManager fileSystemManager = new StandardFileSystemManager();
57          fileSystemManager.setLogger(null);
58          fileSystemManager.init();
59          fileSystemManager.setBaseFile(SystemUtils.getUserDir());
60          return fileSystemManager;
61      }
62  
63      private static File toFile2(FileObject fileObject) throws FileSystemException {
64          if (fileObject == null || !"file".equals(fileObject.getURL().getProtocol())) {
65              return null;
66          }
67          return new File(fileObject.getName().getPathDecoded());
68      }
69  
70      @SuppressWarnings("resource")
71      private void testProviderGetPath(String relPathStr) throws URISyntaxException {
72          FileSystems.getDefault().provider().getPath(new URI(Paths.get(relPathStr).toAbsolutePath().toUri().toString()));
73      }
74  
75      /**
76       * Tests a path with the Chinese character 好.
77       */
78      @Test
79      public void testProviderGetPathGreat() throws URISyntaxException {
80          testProviderGetPath(REL_PATH_GREAT);
81      }
82  
83      /**
84       * Tests a path with the space character.
85       */
86      @Test
87      public void testProviderGetPathSpace() throws URISyntaxException {
88          testProviderGetPath(REL_PATH_SPACE);
89      }
90  
91      @Test
92      public void testToFile() throws IOException {
93          testToFile(fileObject -> fileObject.getPath().toFile());
94      }
95  
96      private void testToFile(FailableFunction<FileObject, File, IOException> function) throws IOException {
97          for (String testFilePath : TEST_FILE_PATHS) {
98              try (FileSystemManager fileSystemManager = loadFileSystemManager();
99                  FileObject fileObject = fileSystemManager.resolveFile(testFilePath)) {
100                 assertNotNull(fileObject);
101                 try (final FileContent content = fileObject.getContent();
102                     InputStream inputStream = content.getInputStream()) {
103                     assertEquals(TEST_FILE_CONTENT, IOUtils.toString(inputStream, StandardCharsets.UTF_8));
104                 }
105                 File file = function.apply(fileObject);
106                 assertNotNull(file);
107                 assertEquals(TEST_FILE_CONTENT, FileUtils.readFileToString(file, StandardCharsets.UTF_8));
108             }
109         }
110     }
111 
112     @Test
113     public void testToFile2() throws IOException {
114         testToFile(FileObjectEscapeCharacterInPathTest::toFile2);
115     }
116 }