View Javadoc
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.vfs2;
18  
19  import java.io.OutputStream;
20  import java.nio.charset.StandardCharsets;
21  
22  import org.junit.Test;
23  
24  /**
25   * File system test that check that a file system can be modified.
26   */
27  public class ProviderWriteAppendTests extends AbstractProviderTestCase {
28      /**
29       * Sets up a scratch folder for the test to use.
30       */
31      protected FileObject createScratchFolder() throws Exception {
32          final FileObject scratchFolder = getWriteFolder();
33  
34          // Make sure the test folder is empty
35          scratchFolder.delete(Selectors.EXCLUDE_SELF);
36          scratchFolder.createFolder();
37  
38          return scratchFolder;
39      }
40  
41      /**
42       * Returns the capabilities required by the tests of this test case.
43       */
44      @Override
45      protected Capability[] getRequiredCapabilities() {
46          return new Capability[] { Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN,
47                  Capability.READ_CONTENT, Capability.WRITE_CONTENT, Capability.APPEND_CONTENT };
48      }
49  
50      /**
51       * Tests create-delete-create-a-file sequence on the same file system.
52       */
53      @Test
54      public void testAppendContent() throws Exception {
55          final FileObject scratchFolder = createScratchFolder();
56  
57          // Create direct child of the test folder
58          final FileObject file = scratchFolder.resolveFile("file1.txt");
59          assertFalse(file.exists());
60  
61          // Create the source file
62          final String content = "Here is some sample content for the file.  Blah Blah Blah.";
63          final String contentAppend = content + content;
64  
65          try (OutputStream os = file.getContent().getOutputStream()) {
66              os.write(content.getBytes(StandardCharsets.UTF_8));
67          }
68          assertSameContent(content, file);
69  
70          // Append to the new file
71          try (OutputStream os2 = file.getContent().getOutputStream(true)) {
72              os2.write(content.getBytes(StandardCharsets.UTF_8));
73          }
74          assertSameContent(contentAppend, file);
75  
76          // Make sure we can copy the new file to another file on the same filesystem
77          final FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
78          assertFalse(fileCopy.exists());
79          fileCopy.copyFrom(file, Selectors.SELECT_SELF);
80  
81          assertSameContent(contentAppend, fileCopy);
82  
83          // Delete the file.
84          assertTrue(fileCopy.exists());
85          assertTrue(fileCopy.delete());
86      }
87  }