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.ByteArrayOutputStream;
22  import java.io.InputStream;
23  import java.nio.charset.StandardCharsets;
24  
25  import org.junit.Test;
26  
27  /**
28   * Test cases for reading file content.
29   */
30  public class ContentTests extends AbstractProviderTestCase {
31  
32      /**
33       * Asserts every file in a folder exists and has the expected content.
34       */
35      private void assertSameContent(final FileInfo expected, final FileObject folder) throws Exception {
36          for (final FileInfo fileInfo : expected.children.values()) {
37              final FileObject child = folder.resolveFile(fileInfo.baseName, NameScope.CHILD);
38  
39              assertTrue(child.getName().toString(), child.exists());
40              if (fileInfo.type == FileType.FILE) {
41                  assertSameContent(fileInfo.content, child);
42              } else {
43                  assertSameContent(fileInfo, child);
44              }
45          }
46      }
47  
48      /**
49       * Asserts that every expected file exists, and has the expected content.
50       */
51      @Test
52      public void testAllContent() throws Exception {
53          final FileInfo expectedFileInfo = buildExpectedStructure();
54          final FileObject actualFolder = getReadFolder();
55  
56          assertSameContent(expectedFileInfo, actualFolder);
57      }
58  
59      /**
60       * Tests attributes.
61       */
62      @Test
63      public void testAttributes() throws FileSystemException {
64          getReadFolder().getContent().getAttributes();
65      }
66  
67      /**
68       * Tests that input streams are cleaned up on file close.
69       */
70      @Test
71      public void testByteArrayReadAll() throws Exception {
72          // Get the test file
73          try (FileObject file = getReadFolder().resolveFile("file1.txt")) {
74              assertEquals(FileType.FILE, file.getType());
75              assertTrue(file.isFile());
76  
77              assertEquals(FILE1_CONTENT, new String(file.getContent().getByteArray()));
78          }
79      }
80  
81      /**
82       * Tests that children cannot be listed for non-folders.
83       */
84      @Test
85      public void testChildren() throws FileSystemException {
86          // Check for file
87          FileObject file = getReadFolder().resolveFile("file1.txt");
88          assertSame(FileType.FILE, file.getType());
89          assertTrue(file.isFile());
90          try {
91              file.getChildren();
92              fail();
93          } catch (final FileSystemException e) {
94              assertSameMessage("vfs.provider/list-children-not-folder.error", file, e);
95          }
96  
97          // Should be able to get child by name
98          file = file.resolveFile("some-child");
99          assertNotNull(file);
100 
101         // Check for unknown file
102         file = getReadFolder().resolveFile("unknown-file");
103         assertFalse(file.exists());
104         try {
105             file.getChildren();
106             fail();
107         } catch (final FileSystemException e) {
108             assertSameMessage("vfs.provider/list-children-not-folder.error", file, e);
109         }
110 
111         // Should be able to get child by name
112         final FileObject child = file.resolveFile("some-child");
113         assertNotNull(child);
114     }
115 
116     /**
117      * Tests content.
118      */
119     @Test
120     public void testContent() throws Exception {
121         // Test non-empty file
122         FileObject file = getReadFolder().resolveFile("file1.txt");
123         assertSameContent(FILE1_CONTENT, file);
124 
125         // Test empty file
126         file = getReadFolder().resolveFile("empty.txt");
127         assertSameContent("", file);
128     }
129 
130     /**
131      * Tests existence determination.
132      */
133     @Test
134     public void testExists() throws Exception {
135         // Test a file
136         FileObject file = getReadFolder().resolveFile("file1.txt");
137         assertTrue("file exists", file.exists());
138         assertNotSame("file exists", file.getType(), FileType.IMAGINARY);
139 
140         // Test a folder
141         file = getReadFolder().resolveFile("dir1");
142         assertTrue("folder exists", file.exists());
143         assertNotSame("folder exists", file.getType(), FileType.IMAGINARY);
144 
145         // Test an unknown file
146         file = getReadFolder().resolveFile("unknown-child");
147         assertFalse("unknown file does not exist", file.exists());
148         assertSame("unknown file does not exist", file.getType(), FileType.IMAGINARY);
149 
150         // Test an unknown file in an unknown folder
151         file = getReadFolder().resolveFile("unknown-folder/unknown-child");
152         assertFalse("unknown file does not exist", file.exists());
153         assertSame("unknown file does not exist", file.getType(), FileType.IMAGINARY);
154     }
155 
156     @Test
157     public void testGetStringCharset() throws Exception {
158         // Get the test file
159         try (FileObject file = getReadFolder().resolveFile("file1.txt")) {
160             assertEquals(FileType.FILE, file.getType());
161             assertTrue(file.isFile());
162 
163             assertEquals(FILE1_CONTENT, file.getContent().getString(StandardCharsets.UTF_8));
164         }
165     }
166 
167     @Test
168     public void testGetStringString() throws Exception {
169         // Get the test file
170         try (FileObject file = getReadFolder().resolveFile("file1.txt")) {
171             assertEquals(FileType.FILE, file.getType());
172             assertTrue(file.isFile());
173 
174             assertEquals(FILE1_CONTENT, file.getContent().getString(StandardCharsets.UTF_8.name()));
175         }
176     }
177 
178     /**
179      * Tests that input streams are cleaned up on file close.
180      */
181     @Test
182     public void testInputStreamMultipleCleanup() throws Exception {
183         // Get the test file
184         final FileObject file = getReadFolder().resolveFile("file1.txt");
185         assertEquals(FileType.FILE, file.getType());
186         assertTrue(file.isFile());
187 
188         // Open some input streams
189         final InputStream instr1 = file.getContent().getInputStream();
190         assertEquals(instr1.read(), FILE1_CONTENT.charAt(0));
191         final InputStream instr2 = file.getContent().getInputStream();
192         assertEquals(instr2.read(), FILE1_CONTENT.charAt(0));
193 
194         // Close the file
195         file.close();
196 
197         // Check
198         assertEquals(instr1.read(), -1);
199         assertEquals(instr2.read(), -1);
200     }
201 
202     /**
203      * Tests that input streams are cleaned up on file close.
204      */
205     @Test
206     public void testInputStreamReadAll() throws Exception {
207         // Get the test file
208         try (FileObject file = getReadFolder().resolveFile("file1.txt")) {
209             assertEquals(FileType.FILE, file.getType());
210             assertTrue(file.isFile());
211 
212             final ByteArrayOutputStream output = new ByteArrayOutputStream();
213             file.getContent().write(output);
214             assertEquals(FILE1_CONTENT, new String(output.toByteArray()));
215         }
216     }
217 
218     /**
219      * Tests that input streams are cleaned up on file close.
220      */
221     @Test
222     public void testInputStreamSingleCleanup() throws Exception {
223         // Get the test file
224         final FileObject file = getReadFolder().resolveFile("file1.txt");
225         assertEquals(FileType.FILE, file.getType());
226         assertTrue(file.isFile());
227 
228         // Open some input streams
229         final InputStream instr1 = file.getContent().getInputStream();
230         assertEquals(instr1.read(), FILE1_CONTENT.charAt(0));
231 
232         // Close the file
233         file.close();
234 
235         // Check
236         assertEquals(instr1.read(), -1);
237     }
238 
239     /**
240      * Tests parent identity.
241      */
242     @Test
243     public void testParent() throws FileSystemException {
244         // Test when both exist
245         FileObject folder = getReadFolder().resolveFile("dir1");
246         FileObject child = folder.resolveFile("file3.txt");
247         assertTrue("folder exists", folder.exists());
248         assertTrue("child exists", child.exists());
249         assertSame(folder, child.getParent());
250 
251         // Test when file does not exist
252         child = folder.resolveFile("unknown-file");
253         assertTrue("folder exists", folder.exists());
254         assertFalse("child does not exist", child.exists());
255         assertSame(folder, child.getParent());
256 
257         // Test when neither exists
258         folder = getReadFolder().resolveFile("unknown-folder");
259         child = folder.resolveFile("unknown-file");
260         assertFalse("folder does not exist", folder.exists());
261         assertFalse("child does not exist", child.exists());
262         assertSame(folder, child.getParent());
263 
264         // Test the parent of the root of the file system
265         // TODO - refactor out test cases for layered vs originating fs
266         final FileSystem fileSystem = getFileSystem();
267         final FileObject root = fileSystem.getRoot();
268         if (fileSystem.getParentLayer() == null) {
269             // No parent layer, so parent should be null
270             assertNull("root has null parent", root.getParent());
271         } else {
272             // Parent should be parent of parent layer.
273             assertSame(fileSystem.getParentLayer().getParent(), root.getParent());
274         }
275     }
276 
277     /**
278      * Tests concurrent reads on different files works.
279      */
280     @Test
281     public void testReadMultipleConcurrent() throws Exception {
282         final FileObject file = getReadFolder().resolveFile("file1.txt");
283         assertTrue(file.exists());
284         final FileObject emptyFile = getReadFolder().resolveFile("empty.txt");
285         assertTrue(emptyFile.exists());
286 
287         // Start reading from the file
288         try (InputStream instr = file.getContent().getInputStream()) {
289             // Try to read from other file
290             assertSameContent("", emptyFile);
291         }
292     }
293 
294     /**
295      * Tests concurrent reads on a file.
296      */
297     @Test
298     public void testReadSingleConcurrent() throws Exception {
299         final FileObject file = getReadFolder().resolveFile("file1.txt");
300         assertTrue(file.exists());
301 
302         // Start reading from the file
303         try (InputStream instr = file.getContent().getInputStream()) {
304             // Start reading again
305             file.getContent().getInputStream().close();
306         }
307     }
308 
309     /**
310      * Tests concurrent reads on a file.
311      */
312     @Test
313     public void testReadSingleSequential() throws Exception {
314         final FileObject file = getReadFolder().resolveFile("file1.txt");
315         assertTrue(file.exists());
316 
317         file.getContent().getInputStream().close();
318         file.getContent().getInputStream().close();
319     }
320 
321     /**
322      * Tests that content and file objects are usable after being closed.
323      */
324     @Test
325     public void testReuse() throws Exception {
326         // Get the test file
327         final FileObject file = getReadFolder().resolveFile("file1.txt");
328         assertEquals(FileType.FILE, file.getType());
329         assertTrue(file.isFile());
330 
331         // Get the file content
332         assertSameContent(FILE1_CONTENT, file);
333 
334         // Read the content again
335         assertSameContent(FILE1_CONTENT, file);
336 
337         // Close the content + file
338         file.getContent().close();
339         file.close();
340 
341         // Read the content again
342         assertSameContent(FILE1_CONTENT, file);
343     }
344 
345     private void testRoot(final FileObject root) throws FileSystemException {
346         assertTrue(root.exists());
347         assertNotSame(root.getType(), FileType.IMAGINARY);
348     }
349 
350     /**
351      * Tests root of file system exists.
352      */
353     @Test
354     public void testRootAPI() throws FileSystemException {
355         if (!getProviderConfig().isFileSystemRootAccessible()) {
356             return;
357         }
358         testRoot(getFileSystem().getRoot());
359     }
360 
361     /**
362      * Tests root of file system exists.
363      */
364     @Test
365     public void testRootURI() throws FileSystemException {
366         if (!getProviderConfig().isFileSystemRootAccessible()) {
367             return;
368         }
369         final FileSystem fileSystem = getFileSystem();
370         final String uri = fileSystem.getRootURI();
371         testRoot(getManager().resolveFile(uri, fileSystem.getFileSystemOptions()));
372     }
373 
374     /**
375      * Tests that unknown files have no content.
376      */
377     @Test
378     public void testUnknownContent() throws Exception {
379 
380         // Try getting the content of an unknown file
381         final FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
382         final FileContent content = unknownFile.getContent();
383         try {
384             content.getInputStream();
385             fail();
386         } catch (final FileSystemException e) {
387             assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e);
388         }
389         try {
390             content.getSize();
391             fail();
392         } catch (final FileSystemException e) {
393             assertSameMessage("vfs.provider/get-size-not-file.error", unknownFile, e);
394         }
395     }
396 
397 }