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  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   * Read-only test cases for file providers.
32   * <p>
33   * TODO - Test getLastModified(), getAttribute().
34   */
35  public class ProviderReadTests extends AbstractProviderTestCase {
36  
37      /**
38       * Walks a folder structure, asserting it contains exactly the expected files and folders.
39       */
40      protected void assertSameStructure(final FileObject folder, final FileInfo expected) throws Exception {
41          // Set up the structure
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              // Check the type is correct
53              assertSame(info.type, file.getType());
54  
55              if (info.type == FileType.FILE) {
56                  continue;
57              }
58  
59              // Check children
60              final FileObject[] children = file.getChildren();
61  
62              // Make sure all children were found
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              // Recursively check each child
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                  // Make sure the child is expected
86                  assertNotNull(childInfo);
87  
88                  // Add to the queue of files to check
89                  queueExpected.add(childInfo);
90                  queueActual.add(child);
91              }
92          }
93      }
94  
95      /**
96       * Returns the read folder named "dir1".
97       *
98       * @return the read folder named "dir1".
99       * @throws FileSystemException
100      */
101     protected FileObject getReadFolderDir1() throws FileSystemException {
102         return getReadFolder().resolveFile("dir1");
103     }
104 
105     /**
106      * Returns the capabilities required by the tests of this test case.
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      * Tests can perform operations on a folder while reading from a different files.
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         // Start reading from the file
128         try (InputStream instr = file.getContent().getInputStream()) {
129             // Do some operations
130             folder.exists();
131             folder.getType();
132             folder.getChildren();
133         }
134     }
135 
136     /**
137      * Tests that we can traverse a folder that has JAR name.
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      * Tests that a folder can't be layered.
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      * Tests that findFiles() works.
166      */
167     @Test
168     public void testFindFiles() throws Exception {
169         final FileInfo fileInfo = buildExpectedStructure();
170         final VerifyingFileSelector selector = new VerifyingFileSelector(fileInfo);
171 
172         // Find the files
173         final FileObject[] actualFiles = getReadFolder().findFiles(selector);
174 
175         // Compare actual and expected list of files
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      * Tests that folders have no content.
188      */
189     @Test
190     public void testFolderContent() throws Exception {
191         if (getFileSystem().hasCapability(Capability.DIRECTORY_READ_CONTENT)) {
192             // test won't fail
193             return;
194         }
195 
196         // Try getting the content of a folder
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      * Tests that test read folder is not hidden.
208      */
209     @Test
210     public void testFolderIsHidden() throws Exception {
211         final FileObject folder = getReadFolderDir1();
212         Assertions.assertFalse(folder.isHidden());
213     }
214 
215     /**
216      * Tests that test read folder is readable.
217      */
218     @Test
219     public void testFolderIsReadable() throws Exception {
220         final FileObject folder = getReadFolderDir1();
221         Assertions.assertTrue(folder.isReadable());
222     }
223 
224     /**
225      * Tests that test read folder is not a symbolic link.
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      * Tests can read multiple time end of stream of empty file.
253      */
254     @Test
255     public void testReadEmptyMultipleEOF() throws Exception {
256         final FileObject file = getReadFolder().resolveFile("empty.txt");
257         assertTrue(file.exists());
258 
259         // Start reading from the file
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      * Tests can read multiple time end of stream.
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         // Start reading from the file
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      * Tests the contents of root of file system can be listed.
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      * Tests that FileObjects can be sorted.
305      */
306     @Test
307     public void testSort() throws FileSystemException {
308         final FileInfo fileInfo = buildExpectedStructure();
309         final VerifyingFileSelector selector = new VerifyingFileSelector(fileInfo);
310 
311         // Find the files
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         // Compare actual and expected list of files
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      * Walks the base folder structure, asserting it contains exactly the expected files and folders.
334      */
335     @Test
336     public void testStructure() throws Exception {
337         final FileInfo baseInfo = buildExpectedStructure();
338         assertSameStructure(getReadFolder(), baseInfo);
339     }
340 
341     /**
342      * Tests type determination.
343      */
344     @Test
345     public void testType() throws Exception {
346         // Test a file
347         FileObject file = resolveFile1Txt();
348         assertSame(FileType.FILE, file.getType());
349         assertTrue(file.isFile());
350 
351         // Test a folder
352         file = getReadFolderDir1();
353         assertSame(FileType.FOLDER, file.getType());
354         assertTrue(file.isFolder());
355 
356         // Test an unknown file
357         file = getReadFolder().resolveFile("unknown-child");
358         assertSame(FileType.IMAGINARY, file.getType());
359     }
360 
361 }