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