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.apache.commons.vfs2.VfsTestUtils.assertSameMessage;
20
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import org.junit.Test;
28 import org.junit.jupiter.api.Assertions;
29
30
31
32
33
34
35 public class ProviderReadTests extends AbstractProviderTestCase {
36
37
38
39
40 protected void assertSameStructure(final FileObject folder, final FileInfo expected) throws Exception {
41
42 final List<FileInfo> queueExpected = new ArrayList<>();
43 queueExpected.add(expected);
44
45 final List<FileObject> queueActual = new ArrayList<>();
46 queueActual.add(folder);
47
48 while (!queueActual.isEmpty()) {
49 final FileObject file = queueActual.remove(0);
50 final FileInfo info = queueExpected.remove(0);
51
52
53 assertSame(info.type, file.getType());
54
55 if (info.type == FileType.FILE) {
56 continue;
57 }
58
59
60 final FileObject[] children = file.getChildren();
61
62
63 assertNotNull(children);
64 int length = children.length;
65 if (info.children.size() != children.length) {
66 for (final FileObject element : children) {
67 if (element.getName().getBaseName().startsWith(".")) {
68 --length;
69 continue;
70 }
71 System.out.println(element.getName());
72 }
73 }
74
75 assertEquals("count children of \"" + file.getName() + "\"", info.children.size(), length);
76
77
78 for (final FileObject child : children) {
79 final String childName = child.getName().getBaseName();
80 if (childName.startsWith(".")) {
81 continue;
82 }
83 final FileInfo childInfo = info.children.get(childName);
84
85
86 assertNotNull(childInfo);
87
88
89 queueExpected.add(childInfo);
90 queueActual.add(child);
91 }
92 }
93 }
94
95
96
97
98
99
100
101 protected FileObject getReadFolderDir1() throws FileSystemException {
102 return getReadFolder().resolveFile("dir1");
103 }
104
105
106
107
108 @Override
109 protected Capability[] getRequiredCapabilities() {
110 return new Capability[] { Capability.GET_TYPE, Capability.LIST_CHILDREN, Capability.READ_CONTENT };
111 }
112
113 private FileObject resolveFile1Txt() throws FileSystemException {
114 return getReadFolder().resolveFile("file1.txt");
115 }
116
117
118
119
120 @Test
121 public void testConcurrentReadFolder() throws Exception {
122 final FileObject file = resolveFile1Txt();
123 assertTrue(file.exists());
124 final FileObject folder = getReadFolderDir1();
125 assertTrue(folder.exists());
126
127
128 try (InputStream instr = file.getContent().getInputStream()) {
129
130 folder.exists();
131 folder.getType();
132 folder.getChildren();
133 }
134 }
135
136
137
138
139 @Test
140 public void testDotJarFolderName() throws Exception {
141 final FileObject folder = getReadFolderDir1().resolveFile("subdir4.jar");
142 Assertions.assertTrue(folder.exists());
143 final FileObject file = folder.resolveFile("file1.txt");
144 Assertions.assertTrue(file.exists());
145 }
146
147
148
149
150 @Test
151 public void testDotJarFolderNameLayer() throws Exception {
152 final FileObject folder = getReadFolderDir1().resolveFile("subdir4.jar");
153 Assertions.assertTrue(folder.isFolder(), "subdir4.jar/ must exist as folder, check test setup.");
154 Assertions.assertFalse(getManager().canCreateFileSystem(folder), "subdir4.jar/ must not be layerable");
155 try {
156 final FileObject ignored = getManager().createFileSystem(folder);
157 fail("Should not be able to create a layered filesystem on a directory. " + ignored);
158 } catch (final FileSystemException e) {
159 assertSame("Creation of layered filesystem should fail" + e, "vfs.impl/no-provider-for-file.error",
160 e.getCode());
161 }
162 }
163
164
165
166
167 @Test
168 public void testFindFiles() throws Exception {
169 final FileInfo fileInfo = buildExpectedStructure();
170 final VerifyingFileSelector selector = new VerifyingFileSelector(fileInfo);
171
172
173 final FileObject[] actualFiles = getReadFolder().findFiles(selector);
174
175
176 final List<FileObject> expectedFiles = selector.finish();
177 assertEquals(expectedFiles.size(), actualFiles.length);
178 final int count = expectedFiles.size();
179 for (int i = 0; i < count; i++) {
180 final FileObject expected = expectedFiles.get(i);
181 final FileObject actual = actualFiles[i];
182 assertEquals(expected, actual);
183 }
184 }
185
186
187
188
189 @Test
190 public void testFolderContent() throws Exception {
191 if (getFileSystem().hasCapability(Capability.DIRECTORY_READ_CONTENT)) {
192
193 return;
194 }
195
196
197 final FileObject folder = getReadFolderDir1();
198 try {
199 folder.getContent().getInputStream();
200 fail();
201 } catch (final FileSystemException e) {
202 assertSameMessage("vfs.provider/read-not-file.error", folder, e);
203 }
204 }
205
206
207
208
209 @Test
210 public void testFolderIsHidden() throws Exception {
211 final FileObject folder = getReadFolderDir1();
212 Assertions.assertFalse(folder.isHidden());
213 }
214
215
216
217
218 @Test
219 public void testFolderIsReadable() throws Exception {
220 final FileObject folder = getReadFolderDir1();
221 Assertions.assertTrue(folder.isReadable());
222 }
223
224
225
226
227 @Test
228 public void testFolderIsSymbolicLink() throws Exception {
229 final FileObject folder = getReadFolderDir1();
230 Assertions.assertFalse(folder.isSymbolicLink());
231 }
232
233 @Test
234 public void testGetContent() throws Exception {
235 final FileObject file = resolveFile1Txt();
236 assertTrue(file.exists());
237 final FileContent content = file.getContent();
238 assertNotNull(content);
239 }
240
241 @Test
242 public void testGetContentInfo() throws Exception {
243 final FileObject file = resolveFile1Txt();
244 assertTrue(file.exists());
245 final FileContent content = file.getContent();
246 assertNotNull(content);
247 final FileContentInfo contentInfo = content.getContentInfo();
248 assertNotNull(contentInfo);
249 }
250
251
252
253
254 @Test
255 public void testReadEmptyMultipleEOF() throws Exception {
256 final FileObject file = getReadFolder().resolveFile("empty.txt");
257 assertTrue(file.exists());
258
259
260 try (InputStream instr = file.getContent().getInputStream()) {
261 assertEquals("read() from empty file should return EOF", -1, instr.read());
262
263 for (int i = 0; i < 5; i++) {
264 assertEquals("multiple read() at EOF should return EOF", -1, instr.read());
265 }
266 }
267 }
268
269
270
271
272 @Test
273 public void testReadFileEOFMultiple() throws Exception {
274 final FileObject file = getReadFolder().resolveFile("file1.txt");
275 assertTrue(file.exists());
276 assertEquals("Expecting 20 bytes test-data file1.txt", 20, file.getContent().getSize());
277
278
279 try (InputStream instr = file.getContent().getInputStream()) {
280 final byte[] buf = new byte[25];
281 assertEquals(20, instr.read(buf));
282
283 for (int i = 0; i < 5; i++) {
284 assertEquals("multiple read(byte[]) at EOF should return EOF", -1, instr.read(buf));
285 }
286 }
287 }
288
289
290
291
292 @Test
293 public void testRoot() throws FileSystemException {
294 if (!getProviderConfig().isFileSystemRootAccessible()) {
295 return;
296 }
297 final FileSystem fs = getFileSystem();
298 final String uri = fs.getRootURI();
299 final FileObject file = getManager().resolveFile(uri, fs.getFileSystemOptions());
300 file.getChildren();
301 }
302
303
304
305
306 @Test
307 public void testSort() throws FileSystemException {
308 final FileInfo fileInfo = buildExpectedStructure();
309 final VerifyingFileSelector selector = new VerifyingFileSelector(fileInfo);
310
311
312 final FileObject[] actualFiles = getReadFolder().findFiles(selector);
313 Arrays.sort(actualFiles);
314 FileObject prevActualFile = actualFiles[0];
315 for (final FileObject actualFile : actualFiles) {
316 assertTrue(prevActualFile.toString().compareTo(actualFile.toString()) <= 0);
317 prevActualFile = actualFile;
318 }
319
320
321 final List<FileObject> expectedFiles = selector.finish();
322 Collections.sort(expectedFiles);
323 assertEquals(expectedFiles.size(), actualFiles.length);
324 final int count = expectedFiles.size();
325 for (int i = 0; i < count; i++) {
326 final FileObject expected = expectedFiles.get(i);
327 final FileObject actual = actualFiles[i];
328 assertEquals(expected, actual);
329 }
330 }
331
332
333
334
335 @Test
336 public void testStructure() throws Exception {
337 final FileInfo baseInfo = buildExpectedStructure();
338 assertSameStructure(getReadFolder(), baseInfo);
339 }
340
341
342
343
344 @Test
345 public void testType() throws Exception {
346
347 FileObject file = resolveFile1Txt();
348 assertSame(FileType.FILE, file.getType());
349 assertTrue(file.isFile());
350
351
352 file = getReadFolderDir1();
353 assertSame(FileType.FOLDER, file.getType());
354 assertTrue(file.isFolder());
355
356
357 file = getReadFolder().resolveFile("unknown-child");
358 assertSame(FileType.IMAGINARY, file.getType());
359 }
360
361 }