KickJava   Java API By Example, From Geeks To Geeks.

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


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.Capability;
19 import org.apache.commons.vfs.FileChangeEvent;
20 import org.apache.commons.vfs.FileListener;
21 import org.apache.commons.vfs.FileObject;
22 import org.apache.commons.vfs.FileSystem;
23 import org.apache.commons.vfs.FileSystemException;
24 import org.apache.commons.vfs.FileType;
25 import org.apache.commons.vfs.Selectors;
26
27 import java.io.OutputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Set JavaDoc;
31
32 /**
33  * File system test that check that a file system can be modified.
34  *
35  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
36  */

37 public class ProviderWriteTests
38     extends AbstractProviderTestCase
39 {
40     /**
41      * Returns the capabilities required by the tests of this test case.
42      */

43     protected Capability[] getRequiredCaps()
44     {
45         return new Capability[]
46         {
47             Capability.CREATE,
48             Capability.DELETE,
49             Capability.GET_TYPE,
50             Capability.LIST_CHILDREN,
51             Capability.READ_CONTENT,
52             Capability.WRITE_CONTENT
53         };
54     }
55
56     /**
57      * Sets up a scratch folder for the test to use.
58      */

59     protected FileObject createScratchFolder() throws Exception JavaDoc
60     {
61         FileObject scratchFolder = getWriteFolder();
62
63         // Make sure the test folder is empty
64
scratchFolder.delete(Selectors.EXCLUDE_SELF);
65         scratchFolder.createFolder();
66
67         return scratchFolder;
68     }
69
70     /**
71      * Tests folder creation.
72      */

73     public void testFolderCreate() throws Exception JavaDoc
74     {
75         FileObject scratchFolder = createScratchFolder();
76
77         // Create direct child of the test folder
78
FileObject folder = scratchFolder.resolveFile("dir1");
79         assertTrue(!folder.exists());
80         folder.createFolder();
81         assertTrue(folder.exists());
82         assertSame(FileType.FOLDER, folder.getType());
83         assertEquals(0, folder.getChildren().length);
84
85         // Create a descendant, where the intermediate folders don't exist
86
folder = scratchFolder.resolveFile("dir2/dir1/dir1");
87         assertTrue(!folder.exists());
88         assertTrue(!folder.getParent().exists());
89         assertTrue(!folder.getParent().getParent().exists());
90         folder.createFolder();
91         assertTrue(folder.exists());
92         assertSame(FileType.FOLDER, folder.getType());
93         assertEquals(0, folder.getChildren().length);
94         assertTrue(folder.getParent().exists());
95         assertTrue(folder.getParent().getParent().exists());
96
97         // Test creating a folder that already exists
98
assertTrue(folder.exists());
99         folder.createFolder();
100     }
101
102     /**
103      * Tests file creation
104      */

105     public void testFileCreate() throws Exception JavaDoc
106     {
107         FileObject scratchFolder = createScratchFolder();
108
109         // Create direct child of the test folder
110
FileObject file = scratchFolder.resolveFile("file1.txt");
111         assertTrue(!file.exists());
112         file.createFile();
113         assertTrue(file.exists());
114         assertSame(FileType.FILE, file.getType());
115         assertEquals(0, file.getContent().getSize());
116         assertFalse(file.isHidden());
117         assertTrue(file.isReadable());
118         assertTrue(file.isWriteable());
119         
120         // Create direct child of the test folder - special name
121
file = scratchFolder.resolveFile("file1%25.txt");
122         assertTrue(!file.exists());
123         file.createFile();
124         assertTrue(file.exists());
125         assertSame(FileType.FILE, file.getType());
126         assertEquals(0, file.getContent().getSize());
127         assertFalse(file.isHidden());
128         assertTrue(file.isReadable());
129         assertTrue(file.isWriteable());
130
131         // Create a descendant, where the intermediate folders don't exist
132
file = scratchFolder.resolveFile("dir1/dir1/file1.txt");
133         assertTrue(!file.exists());
134         assertTrue(!file.getParent().exists());
135         assertTrue(!file.getParent().getParent().exists());
136         file.createFile();
137         assertTrue(file.exists());
138         assertSame(FileType.FILE, file.getType());
139         assertEquals(0, file.getContent().getSize());
140         assertTrue(file.getParent().exists());
141         assertTrue(file.getParent().getParent().exists());
142         assertFalse(file.getParent().isHidden());
143         assertFalse(file.getParent().getParent().isHidden());
144         
145         // Test creating a file that already exists
146
assertTrue(file.exists());
147         file.createFile();
148         assertTrue(file.exists());
149         assertTrue(file.isReadable());
150         assertTrue(file.isWriteable());
151     }
152
153     /**
154      * Tests file/folder creation with mismatched types.
155      */

156     public void testFileCreateMismatched() throws Exception JavaDoc
157     {
158         FileObject scratchFolder = createScratchFolder();
159
160         // Create a test file and folder
161
FileObject file = scratchFolder.resolveFile("dir1/file1.txt");
162         file.createFile();
163         assertEquals(FileType.FILE, file.getType());
164
165         FileObject folder = scratchFolder.resolveFile("dir1/dir2");
166         folder.createFolder();
167         assertEquals(FileType.FOLDER, folder.getType());
168
169         // Attempt to create a file that already exists as a folder
170
try
171         {
172             folder.createFile();
173             fail();
174         }
175         catch (FileSystemException exc)
176         {
177         }
178
179         // Attempt to create a folder that already exists as a file
180
try
181         {
182             file.createFolder();
183             fail();
184         }
185         catch (FileSystemException exc)
186         {
187         }
188
189         // Attempt to create a folder as a child of a file
190
FileObject folder2 = file.resolveFile("some-child");
191         try
192         {
193             folder2.createFolder();
194             fail();
195         }
196         catch (FileSystemException exc)
197         {
198         }
199     }
200
201     /**
202      * Tests deletion
203      */

204     public void testDelete() throws Exception JavaDoc
205     {
206         // Set-up the test structure
207
FileObject folder = createScratchFolder();
208         folder.resolveFile("file1.txt").createFile();
209         folder.resolveFile("file%25.txt").createFile();
210         folder.resolveFile("emptydir").createFolder();
211         folder.resolveFile("dir1/file1.txt").createFile();
212         folder.resolveFile("dir1/dir2/file2.txt").createFile();
213
214         // Delete a file
215
FileObject file = folder.resolveFile("file1.txt");
216         assertTrue(file.exists());
217         file.delete(Selectors.SELECT_ALL);
218         assertTrue(!file.exists());
219
220         // Delete a special name file
221
file = folder.resolveFile("file%25.txt");
222         assertTrue(file.exists());
223         file.delete(Selectors.SELECT_ALL);
224         assertTrue(!file.exists());
225
226         // Delete an empty folder
227
file = folder.resolveFile("emptydir");
228         assertTrue(file.exists());
229         file.delete(Selectors.SELECT_ALL);
230         assertTrue(!file.exists());
231
232         // Recursive delete
233
file = folder.resolveFile("dir1");
234         FileObject file2 = file.resolveFile("dir2/file2.txt");
235         assertTrue(file.exists());
236         assertTrue(file2.exists());
237         file.delete(Selectors.SELECT_ALL);
238         assertTrue(!file.exists());
239         assertTrue(!file2.exists());
240
241         // Delete a file that does not exist
242
file = folder.resolveFile("some-folder/some-file");
243         assertTrue(!file.exists());
244         file.delete(Selectors.SELECT_ALL);
245         assertTrue(!file.exists());
246     }
247
248     /**
249      * Tests concurrent read and write on the same file fails.
250      */

251     /* imario@apache.org leave this to some sort of LockManager
252     public void testConcurrentReadWrite() throws Exception
253     {
254         final FileObject scratchFolder = createScratchFolder();
255
256         final FileObject file = scratchFolder.resolveFile("file1.txt");
257         file.createFile();
258
259         // Start reading from the file
260         final InputStream instr = file.getContent().getInputStream();
261
262         try
263         {
264             // Try to write to the file
265             file.getContent().getOutputStream();
266             fail();
267         }
268         catch (final FileSystemException e)
269         {
270             // Check error message
271             assertSameMessage("vfs.provider/write-in-use.error", file, e);
272         }
273         finally
274         {
275             instr.close();
276         }
277     }
278     */

279
280     /**
281      * Tests concurrent writes on the same file fails.
282      */

283     /* imario@apache.org leave this to some sort of LockManager
284     public void testConcurrentWrite() throws Exception
285     {
286         final FileObject scratchFolder = createScratchFolder();
287
288         final FileObject file = scratchFolder.resolveFile("file1.txt");
289         file.createFile();
290
291         // Start writing to the file
292         final OutputStream outstr = file.getContent().getOutputStream();
293         final String testContent = "some content";
294         try
295         {
296             // Write some content to the first stream
297             outstr.write(testContent.getBytes());
298
299             // Try to open another output stream
300             file.getContent().getOutputStream();
301             fail();
302         }
303         catch (final FileSystemException e)
304         {
305             // Check error message
306             assertSameMessage("vfs.provider/write-in-use.error", file, e);
307         }
308         finally
309         {
310             outstr.close();
311         }
312
313         // Make sure that the content written to the first stream is actually applied
314         assertSameContent(testContent, file);
315     }
316     */

317
318     /**
319      * Tests file copy to and from the same filesystem type. This was a problem
320      * w/ FTP.
321      */

322     public void testCopySameFileSystem() throws Exception JavaDoc
323     {
324         final FileObject scratchFolder = createScratchFolder();
325
326         // Create direct child of the test folder
327
final FileObject file = scratchFolder.resolveFile("file1.txt");
328         assertTrue(!file.exists());
329
330         // Create the source file
331
final String JavaDoc content = "Here is some sample content for the file. Blah Blah Blah.";
332         final OutputStream JavaDoc os = file.getContent().getOutputStream();
333         try
334         {
335             os.write(content.getBytes("utf-8"));
336         }
337         finally
338         {
339             os.close();
340         }
341
342         assertSameContent(content, file);
343
344         // Make sure we can copy the new file to another file on the same filesystem
345
FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
346         assertTrue(!fileCopy.exists());
347         fileCopy.copyFrom(file, Selectors.SELECT_SELF);
348
349         assertSameContent(content, fileCopy);
350     }
351
352     /**
353      * Tests overwriting a file on the same file system.
354      */

355     public void testOverwriteSameFileSystem() throws Exception JavaDoc
356     {
357         final FileObject scratchFolder = createScratchFolder();
358
359         // Create direct child of the test folder
360
final FileObject file = scratchFolder.resolveFile("file1.txt");
361         assertTrue(!file.exists());
362
363         // Create the source file
364
final String JavaDoc content = "Here is some sample content for the file. Blah Blah Blah.";
365         final OutputStream JavaDoc os = file.getContent().getOutputStream();
366         try
367         {
368             os.write(content.getBytes("utf-8"));
369         }
370         finally
371         {
372             os.close();
373         }
374
375         assertSameContent(content, file);
376
377         // Make sure we can copy the new file to another file on the same filesystem
378
FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
379         assertTrue(!fileCopy.exists());
380         fileCopy.copyFrom(file, Selectors.SELECT_SELF);
381
382         assertSameContent(content, fileCopy);
383         
384         // Make sure we can copy the same new file to the same target file on the same filesystem
385
assertTrue(fileCopy.exists());
386         fileCopy.copyFrom(file, Selectors.SELECT_SELF);
387
388         assertSameContent(content, fileCopy);
389     }
390
391     /**
392      * Tests create-delete-create-a-file sequence on the same file system.
393      */

394     public void testCreateDeleteCreateSameFileSystem() throws Exception JavaDoc
395     {
396         final FileObject scratchFolder = createScratchFolder();
397
398         // Create direct child of the test folder
399
final FileObject file = scratchFolder.resolveFile("file1.txt");
400         assertTrue(!file.exists());
401
402         // Create the source file
403
final String JavaDoc content = "Here is some sample content for the file. Blah Blah Blah.";
404         final OutputStream JavaDoc os = file.getContent().getOutputStream();
405         try
406         {
407             os.write(content.getBytes("utf-8"));
408         }
409         finally
410         {
411             os.close();
412         }
413
414         assertSameContent(content, file);
415
416         // Make sure we can copy the new file to another file on the same filesystem
417
FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
418         assertTrue(!fileCopy.exists());
419         fileCopy.copyFrom(file, Selectors.SELECT_SELF);
420
421         assertSameContent(content, fileCopy);
422
423         // Delete the file.
424
assertTrue(fileCopy.exists());
425         assertTrue(fileCopy.delete());
426         
427         // Make sure we can copy the same new file to the same target file on the same filesystem
428
assertTrue(!fileCopy.exists());
429         fileCopy.copyFrom(file, Selectors.SELECT_SELF);
430
431         assertSameContent(content, fileCopy);
432     }
433
434     /**
435      * Test that children are handled correctly by create and delete.
436      */

437     public void testListChildren() throws Exception JavaDoc
438     {
439         FileObject folder = createScratchFolder();
440         HashSet JavaDoc names = new HashSet JavaDoc();
441
442         // Make sure the folder is empty
443
assertEquals(0, folder.getChildren().length);
444
445         // Create a child folder
446
folder.resolveFile("dir1").createFolder();
447         names.add("dir1");
448         assertSameFileSet(names, folder.getChildren());
449
450         // Create a child file
451
folder.resolveFile("file1.html").createFile();
452         names.add("file1.html");
453         assertSameFileSet(names, folder.getChildren());
454
455         // Create a descendent
456
folder.resolveFile("dir2/file1.txt").createFile();
457         names.add("dir2");
458         assertSameFileSet(names, folder.getChildren());
459
460         // Create a child file via an output stream
461
OutputStream JavaDoc outstr = folder.resolveFile("file2.txt").getContent().getOutputStream();
462         outstr.close();
463         names.add("file2.txt");
464         assertSameFileSet(names, folder.getChildren());
465
466         // Delete a child folder
467
folder.resolveFile("dir1").delete(Selectors.SELECT_ALL);
468         names.remove("dir1");
469         assertSameFileSet(names, folder.getChildren());
470
471         // Delete a child file
472
folder.resolveFile("file1.html").delete(Selectors.SELECT_ALL);
473         names.remove("file1.html");
474         assertSameFileSet(names, folder.getChildren());
475
476         // Recreate the folder
477
folder.delete(Selectors.SELECT_ALL);
478         folder.createFolder();
479         assertEquals(0, folder.getChildren().length);
480     }
481
482     /**
483      * Check listeners are notified of changes.
484      */

485     public void testListener() throws Exception JavaDoc
486     {
487         final FileObject baseFile = createScratchFolder();
488
489         FileObject child = baseFile.resolveFile("newfile.txt");
490         assertTrue(!child.exists());
491
492         FileSystem fs = baseFile.getFileSystem();
493         TestListener listener = new TestListener(child);
494         fs.addListener(child, listener);
495
496         // Create as a folder
497
listener.addCreateEvent();
498         child.createFolder();
499         listener.assertFinished();
500
501         // Create the folder again. Should not get an event.
502
child.createFolder();
503
504         // Delete
505
listener.addDeleteEvent();
506         child.delete();
507         listener.assertFinished();
508
509         // Delete again. Should not get an event
510
child.delete();
511
512         // Create as a file
513
listener.addCreateEvent();
514         child.createFile();
515         listener.assertFinished();
516
517         // Create the file again. Should not get an event
518
child.createFile();
519
520         listener.addDeleteEvent();
521         child.delete();
522
523         // Create as a file, by writing to it.
524
listener.addCreateEvent();
525         child.getContent().getOutputStream().close();
526         listener.assertFinished();
527
528         // Recreate the file by writing to it
529
child.getContent().getOutputStream().close();
530
531         // Copy another file over the top
532
final FileObject otherChild = baseFile.resolveFile("folder1");
533         otherChild.createFolder();
534         listener.addDeleteEvent();
535         listener.addCreateEvent();
536         child.copyFrom(otherChild, Selectors.SELECT_SELF);
537         listener.assertFinished();
538
539         fs.removeListener(child, listener);
540     }
541
542     /**
543      * Ensures the names of a set of files match an expected set.
544      */

545     private void assertSameFileSet(Set JavaDoc names, FileObject[] files)
546     {
547         // Make sure the sets are the same length
548
assertEquals(names.size(), files.length);
549
550         // Check for unexpected names
551
for (int i = 0; i < files.length; i++)
552         {
553             FileObject file = files[i];
554             assertTrue(names.contains(file.getName().getBaseName()));
555         }
556     }
557
558     /**
559      * A test listener.
560      */

561     private static class TestListener implements FileListener
562     {
563         private final FileObject file;
564         private final ArrayList JavaDoc events = new ArrayList JavaDoc();
565         private static final Object JavaDoc CREATE = "create";
566         private static final Object JavaDoc DELETE = "delete";
567         private static final Object JavaDoc CHANGED = "changed";
568
569         public TestListener(final FileObject file)
570         {
571             this.file = file;
572         }
573
574         /**
575          * Called when a file is created.
576          */

577         public void fileCreated(final FileChangeEvent event)
578         {
579             assertTrue("Unexpected create event", events.size() > 0);
580             assertSame("Expecting a create event", CREATE, events.remove(0));
581             assertSame(file, event.getFile());
582             try
583             {
584                 assertTrue(file.exists());
585             }
586             catch (FileSystemException e)
587             {
588                 fail();
589             }
590         }
591
592         /**
593          * Called when a file is deleted.
594          */

595         public void fileDeleted(final FileChangeEvent event)
596         {
597             assertTrue("Unexpected delete event", events.size() > 0);
598             assertSame("Expecting a delete event", DELETE, events.remove(0));
599             assertSame(file, event.getFile());
600             try
601             {
602                 assertTrue(!file.exists());
603             }
604             catch (FileSystemException e)
605             {
606                 fail();
607             }
608         }
609
610         public void fileChanged(FileChangeEvent event) throws Exception JavaDoc
611         {
612             assertTrue("Unexpected changed event", events.size() > 0);
613             assertSame("Expecting a changed event", CHANGED, events.remove(0));
614             assertSame(file, event.getFile());
615             try
616             {
617                 assertTrue(!file.exists());
618             }
619             catch (FileSystemException e)
620             {
621                 fail();
622             }
623         }
624
625         public void addCreateEvent()
626         {
627             events.add(CREATE);
628         }
629
630         public void addDeleteEvent()
631         {
632             events.add(DELETE);
633         }
634
635         public void assertFinished()
636         {
637             assertEquals("Missing event", 0, events.size());
638         }
639     }
640 }
641
Popular Tags