KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileObject;
20 import org.apache.commons.vfs.Selectors;
21
22 import java.io.OutputStream JavaDoc;
23
24 /**
25  * File system test that check that a file system can be renamed.
26  *
27  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
28  */

29 public class ProviderRenameTests
30     extends AbstractProviderTestCase
31 {
32     /**
33      * Returns the capabilities required by the tests of this test case.
34      */

35     protected Capability[] getRequiredCaps()
36     {
37         return new Capability[]
38         {
39             Capability.CREATE,
40             Capability.DELETE,
41             Capability.GET_TYPE,
42             Capability.LIST_CHILDREN,
43             Capability.READ_CONTENT,
44             Capability.WRITE_CONTENT,
45             Capability.RENAME
46         };
47     }
48
49     /**
50      * Sets up a scratch folder for the test to use.
51      */

52     protected FileObject createScratchFolder() throws Exception JavaDoc
53     {
54         FileObject scratchFolder = getWriteFolder();
55
56         // Make sure the test folder is empty
57
scratchFolder.delete(Selectors.EXCLUDE_SELF);
58         scratchFolder.createFolder();
59
60         return scratchFolder;
61     }
62
63     /**
64      * Tests create-delete-create-a-file sequence on the same file system.
65      */

66     public void testRenameFile() throws Exception JavaDoc
67     {
68         final FileObject scratchFolder = createScratchFolder();
69
70         // Create direct child of the test folder
71
final FileObject file = scratchFolder.resolveFile("file1.txt");
72         assertTrue(!file.exists());
73
74         // Create the source file
75
final String JavaDoc content = "Here is some sample content for the file. Blah Blah Blah.";
76
77         final OutputStream JavaDoc os = file.getContent().getOutputStream();
78         try
79         {
80             os.write(content.getBytes("utf-8"));
81         }
82         finally
83         {
84             os.close();
85         }
86         assertSameContent(content, file);
87
88
89         // Make sure we can move the new file to another file on the same filesystem
90

91         FileObject fileMove = scratchFolder.resolveFile("file1move.txt");
92         assertTrue(!fileMove.exists());
93
94         file.moveTo(fileMove);
95
96         assertTrue(!file.exists());
97         assertTrue(fileMove.exists());
98
99         assertSameContent(content, fileMove);
100
101         // Delete the file.
102
assertTrue(fileMove.exists());
103         assertTrue(fileMove.delete());
104     }
105 }
106
Popular Tags