1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
47
48 public static final String TEST_FILE_CONTENT = "aaa";
49
50
51
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
77
78 @Test
79 public void testProviderGetPathGreat() throws URISyntaxException {
80 testProviderGetPath(REL_PATH_GREAT);
81 }
82
83
84
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 }