Simplify test fixture creation
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
*/
public class MemoryMappedFileInputStreamTest {
+ private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+
@TempDir
Path tempDir;
@@ -53,11 +53,7 @@ void afterEach() {
}
private Path createTestFile(final int size) throws IOException {
- final Path file = Files.createTempFile(tempDir, null, null);
- try (OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(file))) {
- Files.write(file, RandomUtils.nextBytes(size));
- }
- return file;
+ return Files.write(Files.createTempFile(tempDir, null, null), RandomUtils.nextBytes(size));
}
@Test
@@ -80,7 +76,7 @@ void testEmptyFile() throws IOException {
// test
try (InputStream inputStream = new MemoryMappedFileInputStream(file)) {
// verify
- assertArrayEquals(new byte[0], IOUtils.toByteArray(inputStream));
+ assertArrayEquals(EMPTY_BYTE_ARRAY, IOUtils.toByteArray(inputStream));
}
}
@@ -148,7 +144,7 @@ void testSkipEmpty() throws IOException {
try (InputStream inputStream = new MemoryMappedFileInputStream(file)) {
assertEquals(0, inputStream.skip(5));
// verify
- assertArrayEquals(new byte[0], IOUtils.toByteArray(inputStream));
+ assertArrayEquals(EMPTY_BYTE_ARRAY, IOUtils.toByteArray(inputStream));
}
}
@@ -208,7 +204,7 @@ void testSkipPastEof() throws IOException {
IOUtils.toByteArray(inputStream, 5);
assertEquals(95, inputStream.skip(96));
// verify
- assertArrayEquals(new byte[0], IOUtils.toByteArray(inputStream));
+ assertArrayEquals(EMPTY_BYTE_ARRAY, IOUtils.toByteArray(inputStream));
}
}
