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.vfs.test;
18  
19  import org.apache.commons.vfs.FileContent;
20  import org.apache.commons.vfs.FileObject;
21  import org.apache.commons.vfs.FileSystem;
22  import org.apache.commons.vfs.FileSystemException;
23  import org.apache.commons.vfs.FileType;
24  import org.apache.commons.vfs.NameScope;
25  
26  import java.io.InputStream;
27  import java.util.Iterator;
28  
29  /***
30   * Test cases for reading file content.
31   *
32   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
33   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
34   */
35  public class ContentTests
36      extends AbstractProviderTestCase
37  {
38      /***
39       * Asserts that every expected file exists, and has the expected content.
40       */
41      public void testAllContent() throws Exception
42      {
43          final FileInfo baseInfo = buildExpectedStructure();
44          final FileObject baseFolder = getReadFolder();
45  
46          assertSameContent(baseInfo, baseFolder);
47      }
48  
49      /***
50       * Asserts every file in a folder exists and has the expected content.
51       */
52      private void assertSameContent(final FileInfo expected,
53                                     final FileObject folder) throws Exception
54      {
55          for (Iterator iterator = expected.children.values().iterator(); iterator.hasNext();)
56          {
57              final FileInfo fileInfo = (FileInfo) iterator.next();
58              final FileObject child = folder.resolveFile(fileInfo.baseName, NameScope.CHILD);
59  
60              assertTrue(child.getName().toString(), child.exists());
61              if (fileInfo.type == FileType.FILE)
62              {
63                  assertSameContent(fileInfo.content, child);
64              }
65              else
66              {
67                  assertSameContent(fileInfo, child);
68              }
69          }
70      }
71  
72      /***
73       * Tests existence determination.
74       */
75      public void testExists() throws Exception
76      {
77          // Test a file
78          FileObject file = getReadFolder().resolveFile("file1.txt");
79          assertTrue("file exists", file.exists());
80          assertTrue("file exists", file.getType() != FileType.IMAGINARY);
81  
82          // Test a folder
83          file = getReadFolder().resolveFile("dir1");
84          assertTrue("folder exists", file.exists());
85          assertTrue("folder exists", file.getType() != FileType.IMAGINARY);
86  
87          // Test an unknown file
88          file = getReadFolder().resolveFile("unknown-child");
89          assertTrue("unknown file does not exist", !file.exists());
90          assertTrue("unknown file does not exist",
91              file.getType() == FileType.IMAGINARY);
92  
93          // Test an unknown file in an unknown folder
94          file = getReadFolder().resolveFile("unknown-folder/unknown-child");
95          assertTrue("unknown file does not exist", !file.exists());
96          assertTrue("unknown file does not exist",
97              file.getType() == FileType.IMAGINARY);
98      }
99  
100     /***
101      * Tests root of file system exists.
102      */
103     public void testRoot() throws FileSystemException
104     {
105         final FileObject file = getReadFolder().getFileSystem().getRoot();
106         assertTrue(file.exists());
107         assertTrue(file.getType() != FileType.IMAGINARY);
108     }
109 
110     /***
111      * Tests parent identity
112      */
113     public void testParent() throws FileSystemException
114     {
115         // Test when both exist
116         FileObject folder = getReadFolder().resolveFile("dir1");
117         FileObject child = folder.resolveFile("file3.txt");
118         assertTrue("folder exists", folder.exists());
119         assertTrue("child exists", child.exists());
120         assertSame(folder, child.getParent());
121 
122         // Test when file does not exist
123         child = folder.resolveFile("unknown-file");
124         assertTrue("folder exists", folder.exists());
125         assertTrue("child does not exist", !child.exists());
126         assertSame(folder, child.getParent());
127 
128         // Test when neither exists
129         folder = getReadFolder().resolveFile("unknown-folder");
130         child = folder.resolveFile("unknown-file");
131         assertTrue("folder does not exist", !folder.exists());
132         assertTrue("child does not exist", !child.exists());
133         assertSame(folder, child.getParent());
134 
135         // Test the parent of the root of the file system
136         // TODO - refactor out test cases for layered vs originating fs
137         final FileSystem fileSystem = getReadFolder().getFileSystem();
138         FileObject root = fileSystem.getRoot();
139         if (fileSystem.getParentLayer() == null)
140         {
141             // No parent layer, so parent should be null
142             assertNull("root has null parent", root.getParent());
143         }
144         else
145         {
146             // Parent should be parent of parent layer.
147             assertSame(fileSystem.getParentLayer().getParent(), root.getParent());
148         }
149     }
150 
151     /***
152      * Tests that children cannot be listed for non-folders.
153      */
154     public void testChildren() throws FileSystemException
155     {
156         // Check for file
157         FileObject file = getReadFolder().resolveFile("file1.txt");
158         assertSame(FileType.FILE, file.getType());
159         try
160         {
161             file.getChildren();
162             fail();
163         }
164         catch (FileSystemException e)
165         {
166             assertSameMessage("vfs.provider/list-children-not-folder.error", file, e);
167         }
168 
169         // Should be able to get child by name
170         file = file.resolveFile("some-child");
171         assertNotNull(file);
172 
173         // Check for unknown file
174         file = getReadFolder().resolveFile("unknown-file");
175         assertTrue(!file.exists());
176         try
177         {
178             file.getChildren();
179             fail();
180         }
181         catch (final FileSystemException e)
182         {
183             assertSameMessage("vfs.provider/list-children-not-folder.error", file, e);
184         }
185 
186         // Should be able to get child by name
187         FileObject child = file.resolveFile("some-child");
188         assertNotNull(child);
189     }
190 
191     /***
192      * Tests content.
193      */
194     public void testContent() throws Exception
195     {
196         // Test non-empty file
197         FileObject file = getReadFolder().resolveFile("file1.txt");
198         assertSameContent(FILE1_CONTENT, file);
199 
200         // Test empty file
201         file = getReadFolder().resolveFile("empty.txt");
202         assertSameContent("", file);
203     }
204 
205     /***
206      * Tests that unknown files have no content.
207      */
208     public void testUnknownContent() throws Exception
209     {
210 
211         // Try getting the content of an unknown file
212         FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
213         FileContent content = unknownFile.getContent();
214         try
215         {
216             content.getInputStream();
217             fail();
218         }
219         catch (FileSystemException e)
220         {
221             assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e);
222         }
223         try
224         {
225             content.getSize();
226             fail();
227         }
228         catch (final FileSystemException e)
229         {
230             assertSameMessage("vfs.provider/get-size-not-file.error", unknownFile, e);
231         }
232     }
233 
234     /***
235      * Tests concurrent reads on a file.
236      */
237     public void testConcurrentRead() throws Exception
238     {
239         final FileObject file = getReadFolder().resolveFile("file1.txt");
240         assertTrue(file.exists());
241 
242         // Start reading from the file
243         final InputStream instr = file.getContent().getInputStream();
244         try
245         {
246             // Start reading again
247             file.getContent().getInputStream().close();
248         }
249         finally
250         {
251             instr.close();
252         }
253     }
254 
255     /***
256      * Tests concurrent reads on different files works.
257      */
258     public void testConcurrentReadFiles() throws Exception
259     {
260         final FileObject file = getReadFolder().resolveFile("file1.txt");
261         assertTrue(file.exists());
262         final FileObject emptyFile = getReadFolder().resolveFile("empty.txt");
263         assertTrue(emptyFile.exists());
264 
265         // Start reading from the file
266         final InputStream instr = file.getContent().getInputStream();
267         try
268         {
269             // Try to read from other file
270             assertSameContent("", emptyFile);
271         }
272         finally
273         {
274             instr.close();
275         }
276     }
277 
278     /***
279      * Tests that content and file objects are usable after being closed.
280      */
281     public void testReuse() throws Exception
282     {
283         // Get the test file
284         FileObject file = getReadFolder().resolveFile("file1.txt");
285         assertEquals(FileType.FILE, file.getType());
286 
287         // Get the file content
288         assertSameContent(FILE1_CONTENT, file);
289 
290         // Read the content again
291         assertSameContent(FILE1_CONTENT, file);
292 
293         // Close the content + file
294         file.getContent().close();
295         file.close();
296 
297         // Read the content again
298         assertSameContent(FILE1_CONTENT, file);
299     }
300 
301     /***
302      * Tests that input streams are cleaned up on file close.
303      */
304     public void testInstrCleanup() throws Exception
305     {
306         // Get the test file
307         FileObject file = getReadFolder().resolveFile("file1.txt");
308         assertEquals(FileType.FILE, file.getType());
309 
310         // Open some input streams
311         final InputStream instr1 = file.getContent().getInputStream();
312         assertTrue(instr1.read() == FILE1_CONTENT.charAt(0));
313         final InputStream instr2 = file.getContent().getInputStream();
314         assertTrue(instr2.read() == FILE1_CONTENT.charAt(0));
315 
316         // Close the file
317         file.close();
318 
319         // Check
320         assertTrue(instr1.read() == -1);
321         assertTrue(instr2.read() == -1);
322     }
323 }