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