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