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    *      https://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  
18  package org.apache.commons.io.output;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.io.IOException;
25  import java.io.RandomAccessFile;
26  import java.nio.charset.Charset;
27  import java.nio.charset.StandardCharsets;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.StandardOpenOption;
31  
32  import org.junit.jupiter.api.Test;
33  import org.junit.jupiter.api.io.TempDir;
34  
35  /**
36   * Tests {@link RandomAccessFileOutputStream}.
37   */
38  class RandomAccessFileOutputStreamTest {
39  
40      private static final String EXPECTED = "Put the message in the box";
41  
42      /** A temporary folder. */
43      @TempDir
44      public Path temporaryFolder;
45  
46      @Test
47      void testClose() throws IOException {
48          final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
49          final Charset charset = StandardCharsets.US_ASCII;
50          // @formatter:off
51          try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
52                  .setPath(fixturePath)
53                  .setOpenOptions(StandardOpenOption.WRITE)
54                  .get()) {
55              os.write(EXPECTED.getBytes(charset));
56              os.close();
57          }
58          assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
59          // @formatter:on
60          try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
61              validateState(os);
62          }
63      }
64  
65      @Test
66      void testFlush() throws IOException {
67          final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
68          final Charset charset = StandardCharsets.US_ASCII;
69          // @formatter:off
70          try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
71                  .setPath(fixturePath)
72                  .setOpenOptions(StandardOpenOption.WRITE)
73                  .get()) {
74              final byte[] bytes = EXPECTED.getBytes(charset);
75              for (final byte element : bytes) {
76                  os.write(element);
77                  os.flush();
78              }
79          }
80          assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
81          // @formatter:on
82          try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
83              validateState(os);
84          }
85      }
86  
87      @Test
88      void testWriteByteArray() throws IOException {
89          final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
90          final Charset charset = StandardCharsets.US_ASCII;
91          // @formatter:off
92          try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
93                  .setPath(fixturePath)
94                  .setOpenOptions(StandardOpenOption.WRITE)
95                  .get()) {
96              os.write(EXPECTED.getBytes(charset));
97          }
98          assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
99          // @formatter:on
100         try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
101             validateState(os);
102         }
103     }
104 
105     @Test
106     void testWriteByteArrayAt() throws IOException {
107         final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
108         final Charset charset = StandardCharsets.US_ASCII;
109         // @formatter:off
110         try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
111                 .setPath(fixturePath)
112                 .setOpenOptions(StandardOpenOption.WRITE)
113                 .get()) {
114             os.write(EXPECTED.getBytes(charset), 1, EXPECTED.length() - 2);
115         }
116         assertEquals(EXPECTED.subSequence(1, EXPECTED.length() - 1), new String(Files.readAllBytes(fixturePath), charset));
117         // @formatter:on
118         try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
119             validateState(os);
120         }
121     }
122 
123     @Test
124     void testWriteGet() throws IOException {
125         final Path fixturePath = temporaryFolder.resolve("testWriteGet.txt");
126         // @formatter:off
127         try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
128                 .setPath(fixturePath)
129                 .setOpenOptions(StandardOpenOption.WRITE)
130                 .get()) {
131             validateState(os);
132         }
133         // @formatter:on
134     }
135 
136     @Test
137     void testWriteGetDefault() throws IOException {
138         assertThrows(IllegalStateException.class, () -> {
139             try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().get()) {
140                 validateState(os);
141             }
142         });
143     }
144 
145     /**
146      * Tests that the default OpenOption is WRITE.
147      *
148      * @throws IOException Thrown when the test fails.
149      */
150     @Test
151     void testWriteGetPathOnly() throws IOException {
152         final Path fixturePath = temporaryFolder.resolve("testWriteGet.txt");
153         // @formatter:off
154         try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
155                 .setPath(fixturePath)
156                 .get()) {
157             validateState(os);
158         }
159         // @formatter:on
160     }
161 
162     @Test
163     void testWriteInt() throws IOException {
164         final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
165         final Charset charset = StandardCharsets.US_ASCII;
166         // @formatter:off
167         try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
168                 .setPath(fixturePath)
169                 .setOpenOptions(StandardOpenOption.WRITE)
170                 .get()) {
171             validateState(os);
172             final byte[] bytes = EXPECTED.getBytes(charset);
173             for (final byte element : bytes) {
174                 os.write(element);
175             }
176         }
177         assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
178         // @formatter:on
179         try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
180             validateState(os);
181         }
182     }
183 
184     @SuppressWarnings("resource")
185     private void validateState(final RandomAccessFileOutputStream os) throws IOException {
186         final RandomAccessFile randomAccessFile = os.getRandomAccessFile();
187         assertNotNull(randomAccessFile);
188         assertNotNull(randomAccessFile.getFD());
189     }
190 
191 }