1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38 class RandomAccessFileOutputStreamTest {
39
40 private static final String EXPECTED = "Put the message in the box";
41
42
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
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
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
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
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
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
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
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
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
127 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
128 .setPath(fixturePath)
129 .setOpenOptions(StandardOpenOption.WRITE)
130 .get()) {
131 validateState(os);
132 }
133
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
147
148
149
150 @Test
151 void testWriteGetPathOnly() throws IOException {
152 final Path fixturePath = temporaryFolder.resolve("testWriteGet.txt");
153
154 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
155 .setPath(fixturePath)
156 .get()) {
157 validateState(os);
158 }
159
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
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
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 }