KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > test > ContentTests


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.test;
17
18 import org.apache.commons.vfs.FileContent;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystem;
21 import org.apache.commons.vfs.FileSystemException;
22 import org.apache.commons.vfs.FileType;
23 import org.apache.commons.vfs.NameScope;
24
25 import java.io.InputStream JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * Test cases for reading file content.
30  *
31  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
32  */

33 public class ContentTests
34     extends AbstractProviderTestCase
35 {
36     /**
37      * Asserts that every expected file exists, and has the expected content.
38      */

39     public void testAllContent() throws Exception JavaDoc
40     {
41         final FileInfo baseInfo = buildExpectedStructure();
42         final FileObject baseFolder = getReadFolder();
43
44         assertSameContent(baseInfo, baseFolder);
45     }
46
47     /**
48      * Asserts every file in a folder exists and has the expected content.
49      */

50     private void assertSameContent(final FileInfo expected,
51                                    final FileObject folder) throws Exception JavaDoc
52     {
53         for (Iterator JavaDoc iterator = expected.children.values().iterator(); iterator.hasNext();)
54         {
55             final FileInfo fileInfo = (FileInfo) iterator.next();
56             final FileObject child = folder.resolveFile(fileInfo.baseName, NameScope.CHILD);
57
58             assertTrue(child.getName().toString(), child.exists());
59             if (fileInfo.type == FileType.FILE)
60             {
61                 assertSameContent(fileInfo.content, child);
62             }
63             else
64             {
65                 assertSameContent(fileInfo, child);
66             }
67         }
68     }
69
70     /**
71      * Tests existence determination.
72      */

73     public void testExists() throws Exception JavaDoc
74     {
75         // Test a file
76
FileObject file = getReadFolder().resolveFile("file1.txt");
77         assertTrue("file exists", file.exists());
78         assertTrue("file exists", file.getType() != FileType.IMAGINARY);
79
80         // Test a folder
81
file = getReadFolder().resolveFile("dir1");
82         assertTrue("folder exists", file.exists());
83         assertTrue("folder exists", file.getType() != FileType.IMAGINARY);
84
85         // Test an unknown file
86
file = getReadFolder().resolveFile("unknown-child");
87         assertTrue("unknown file does not exist", !file.exists());
88         assertTrue("unknown file does not exist",
89             file.getType() == FileType.IMAGINARY);
90
91         // Test an unknown file in an unknown folder
92
file = getReadFolder().resolveFile("unknown-folder/unknown-child");
93         assertTrue("unknown file does not exist", !file.exists());
94         assertTrue("unknown file does not exist",
95             file.getType() == FileType.IMAGINARY);
96     }
97
98     /**
99      * Tests root of file system exists.
100      */

101     public void testRoot() throws FileSystemException
102     {
103         final FileObject file = getReadFolder().getFileSystem().getRoot();
104         assertTrue(file.exists());
105         assertTrue(file.getType() != FileType.IMAGINARY);
106     }
107
108     /**
109      * Tests parent identity
110      */

111     public void testParent() throws FileSystemException
112     {
113         // Test when both exist
114
FileObject folder = getReadFolder().resolveFile("dir1");
115         FileObject child = folder.resolveFile("file3.txt");
116         assertTrue("folder exists", folder.exists());
117         assertTrue("child exists", child.exists());
118         assertSame(folder, child.getParent());
119
120         // Test when file does not exist
121
child = folder.resolveFile("unknown-file");
122         assertTrue("folder exists", folder.exists());
123         assertTrue("child does not exist", !child.exists());
124         assertSame(folder, child.getParent());
125
126         // Test when neither exists
127
folder = getReadFolder().resolveFile("unknown-folder");
128         child = folder.resolveFile("unknown-file");
129         assertTrue("folder does not exist", !folder.exists());
130         assertTrue("child does not exist", !child.exists());
131         assertSame(folder, child.getParent());
132
133         // Test the parent of the root of the file system
134
// TODO - refactor out test cases for layered vs originating fs
135
final FileSystem fileSystem = getReadFolder().getFileSystem();
136         FileObject root = fileSystem.getRoot();
137         if (fileSystem.getParentLayer() == null)
138         {
139             // No parent layer, so parent should be null
140
assertNull("root has null parent", root.getParent());
141         }
142         else
143         {
144             // Parent should be parent of parent layer.
145
assertSame(fileSystem.getParentLayer().getParent(), root.getParent());
146         }
147     }
148
149     /**
150      * Tests that children cannot be listed for non-folders.
151      */

152     public void testChildren() throws FileSystemException
153     {
154         // Check for file
155
FileObject file = getReadFolder().resolveFile("file1.txt");
156         assertSame(FileType.FILE, file.getType());
157         try
158         {
159             file.getChildren();
160             fail();
161         }
162         catch (FileSystemException e)
163         {
164             assertSameMessage("vfs.provider/list-children-not-folder.error", file, e);
165         }
166
167         // Should be able to get child by name
168
file = file.resolveFile("some-child");
169         assertNotNull(file);
170
171         // Check for unknown file
172
file = getReadFolder().resolveFile("unknown-file");
173         assertTrue(!file.exists());
174         try
175         {
176             file.getChildren();
177             fail();
178         }
179         catch (final FileSystemException e)
180         {
181             assertSameMessage("vfs.provider/list-children-not-folder.error", file, e);
182         }
183
184         // Should be able to get child by name
185
FileObject child = file.resolveFile("some-child");
186         assertNotNull(child);
187     }
188
189     /**
190      * Tests content.
191      */

192     public void testContent() throws Exception JavaDoc
193     {
194         // Test non-empty file
195
FileObject file = getReadFolder().resolveFile("file1.txt");
196         assertSameContent(FILE1_CONTENT, file);
197
198         // Test empty file
199
file = getReadFolder().resolveFile("empty.txt");
200         assertSameContent("", file);
201     }
202
203     /**
204      * Tests that unknown files have no content.
205      */

206     public void testUnknownContent() throws Exception JavaDoc
207     {
208
209         // Try getting the content of an unknown file
210
FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
211         FileContent content = unknownFile.getContent();
212         try
213         {
214             content.getInputStream();
215             fail();
216         }
217         catch (FileSystemException e)
218         {
219             assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e);
220         }
221         try
222         {
223             content.getSize();
224             fail();
225         }
226         catch (final FileSystemException e)
227         {
228             assertSameMessage("vfs.provider/get-size-not-file.error", unknownFile, e);
229         }
230     }
231
232     /**
233      * Tests concurrent reads on a file.
234      */

235     public void testConcurrentRead() throws Exception JavaDoc
236     {
237         final FileObject file = getReadFolder().resolveFile("file1.txt");
238         assertTrue(file.exists());
239
240         // Start reading from the file
241
final InputStream JavaDoc instr = file.getContent().getInputStream();
242         try
243         {
244             // Start reading again
245
file.getContent().getInputStream().close();
246         }
247         finally
248         {
249             instr.close();
250         }
251     }
252
253     /**
254      * Tests concurrent reads on different files works.
255      */

256     public void testConcurrentReadFiles() throws Exception JavaDoc
257     {
258         final FileObject file = getReadFolder().resolveFile("file1.txt");
259         assertTrue(file.exists());
260         final FileObject emptyFile = getReadFolder().resolveFile("empty.txt");
261         assertTrue(emptyFile.exists());
262
263         // Start reading from the file
264
final InputStream JavaDoc instr = file.getContent().getInputStream();
265         try
266         {
267             // Try to read from other file
268
assertSameContent("", emptyFile);
269         }
270         finally
271         {
272             instr.close();
273         }
274     }
275
276     /**
277      * Tests that content and file objects are usable after being closed.
278      */

279     public void testReuse() throws Exception JavaDoc
280     {
281         // Get the test file
282
FileObject file = getReadFolder().resolveFile("file1.txt");
283         assertEquals(FileType.FILE, file.getType());
284
285         // Get the file content
286
assertSameContent(FILE1_CONTENT, file);
287
288         // Read the content again
289
assertSameContent(FILE1_CONTENT, file);
290
291         // Close the content + file
292
file.getContent().close();
293         file.close();
294
295         // Read the content again
296
assertSameContent(FILE1_CONTENT, file);
297     }
298
299     /**
300      * Tests that input streams are cleaned up on file close.
301      */

302     public void testInstrCleanup() throws Exception JavaDoc
303     {
304         // Get the test file
305
FileObject file = getReadFolder().resolveFile("file1.txt");
306         assertEquals(FileType.FILE, file.getType());
307
308         // Open some input streams
309
final InputStream JavaDoc instr1 = file.getContent().getInputStream();
310         assertTrue(instr1.read() == FILE1_CONTENT.charAt(0));
311         final InputStream JavaDoc instr2 = file.getContent().getInputStream();
312         assertTrue(instr2.read() == FILE1_CONTENT.charAt(0));
313
314         // Close the file
315
file.close();
316
317         // Check
318
assertTrue(instr1.read() == -1);
319         assertTrue(instr2.read() == -1);
320     }
321 }
322
Popular Tags