KickJava   Java API By Example, From Geeks To Geeks.

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


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 modified.
26  *
27  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
28  */

29 public class ProviderWriteAppendTests
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.APPEND_CONTENT
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 testAppendContent() 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         final String JavaDoc contentAppend = content + content;
77
78         final OutputStream JavaDoc os = file.getContent().getOutputStream();
79         try
80         {
81             os.write(content.getBytes("utf-8"));
82         }
83         finally
84         {
85             os.close();
86         }
87         assertSameContent(content, file);
88
89         // Append to the new file
90
final OutputStream JavaDoc os2 = file.getContent().getOutputStream(true);
91         try
92         {
93             os2.write(content.getBytes("utf-8"));
94         }
95         finally
96         {
97             os2.close();
98         }
99         assertSameContent(contentAppend, file);
100
101         // Make sure we can copy the new file to another file on the same filesystem
102
FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
103         assertTrue(!fileCopy.exists());
104         fileCopy.copyFrom(file, Selectors.SELECT_SELF);
105
106         assertSameContent(contentAppend, fileCopy);
107
108         // Delete the file.
109
assertTrue(fileCopy.exists());
110         assertTrue(fileCopy.delete());
111     }
112 }
113
Popular Tags