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      /**
30       * Sets up a scratch folder for the test to use.
31       */
32      protected FileObject createScratchFolder() throws Exception {
33          final FileObject scratchFolder = getWriteFolder();
34  
35          // Make sure the test folder is empty
36          scratchFolder.delete(Selectors.EXCLUDE_SELF);
37          scratchFolder.createFolder();
38  
39          return scratchFolder;
40      }
41  
42      /**
43       * Returns the capabilities required by the tests of this test case.
44       */
45      @Override
46      protected Capability[] getRequiredCapabilities() {
47          return new Capability[] {Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN, Capability.READ_CONTENT,
48              Capability.WRITE_CONTENT, Capability.APPEND_CONTENT};
49      }
50  
51      /**
52       * Tests create-delete-create-a-file sequence on the same file system.
53       */
54      @Test
55      public void testAppendContent() throws Exception {
56          try (FileObject scratchFolder = createScratchFolder();
57  
58              // Create direct child of the test folder
59              final FileObject file = scratchFolder.resolveFile("file1.txt")) {
60              assertFalse(file.exists());
61  
62              // Create the source file
63              final String content = "Here is some sample content for the file.  Blah Blah Blah.";
64              final String contentAppend = content + content;
65  
66              try (FileContent fileContent = file.getContent(); OutputStream os = fileContent.getOutputStream()) {
67                  os.write(content.getBytes(StandardCharsets.UTF_8));
68              }
69              assertSameContent(content, file);
70  
71              // Append to the new file
72              try (FileContent fileContent = file.getContent(); OutputStream os2 = fileContent.getOutputStream(true)) {
73                  os2.write(content.getBytes(StandardCharsets.UTF_8));
74              }
75              assertSameContent(contentAppend, file);
76  
77              // Make sure we can copy the new file to another file on the same filesystem
78              try (FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt")) {
79                  assertFalse(fileCopy.exists());
80                  fileCopy.copyFrom(file, Selectors.SELECT_SELF);
81  
82                  assertSameContent(contentAppend, fileCopy);
83  
84                  // Delete the file.
85                  assertTrue(fileCopy.exists());
86                  assertTrue(fileCopy.delete());
87              }
88          }
89      }
90  
91      /**
92       * Tests append-write into a non-existing file.
93       *
94       * See [VFS-807].
95       */
96      @Test
97      public void testAppendToNonExisting() throws Exception {
98          try (FileObject scratchFolder = createScratchFolder();
99  
100             // Create direct child of the test folder
101             final FileObject file = scratchFolder.resolveFile("file2.txt")) {
102             assertFalse(file.exists());
103 
104             // Create the source file
105             final String content1 = "Here is some sample content for the file. Blah Blah Blah.";
106 
107             try (FileContent fileContent = file.getContent(); OutputStream os = fileContent.getOutputStream(true)) {
108                 os.write(content1.getBytes(StandardCharsets.UTF_8));
109             }
110             assertSameContent(content1, file);
111         }
112     }
113 
114 }