4040 */
4141public class RandomAccessFileModeTest {
4242
43+ private static final byte [] BYTES_FIXTURE = "Foo" .getBytes (StandardCharsets .US_ASCII );
44+
45+ private static final String FIXTURE = "test.txt" ;
46+
4347 /**
4448 * Temporary directory.
4549 */
@@ -53,8 +57,8 @@ private byte[] read(final RandomAccessFile randomAccessFile) throws IOException
5357 @ ParameterizedTest
5458 @ EnumSource (RandomAccessFileMode .class )
5559 public void testCreateFile (final RandomAccessFileMode randomAccessFileMode ) throws IOException {
56- final byte [] expected = "Foo" . getBytes ( StandardCharsets . US_ASCII ) ;
57- final Path fixture = Files . write ( tempDir . resolve ( "test.txt" ), expected );
60+ final byte [] expected = BYTES_FIXTURE ;
61+ final Path fixture = writeFixture ( expected );
5862 try (RandomAccessFile randomAccessFile = randomAccessFileMode .create (fixture .toFile ())) {
5963 assertArrayEquals (expected , read (randomAccessFile ));
6064 }
@@ -63,8 +67,8 @@ public void testCreateFile(final RandomAccessFileMode randomAccessFileMode) thro
6367 @ ParameterizedTest
6468 @ EnumSource (RandomAccessFileMode .class )
6569 public void testCreatePath (final RandomAccessFileMode randomAccessFileMode ) throws IOException {
66- final byte [] expected = "Foo" . getBytes ( StandardCharsets . US_ASCII ) ;
67- final Path fixture = Files . write ( tempDir . resolve ( "test.txt" ), expected );
70+ final byte [] expected = BYTES_FIXTURE ;
71+ final Path fixture = writeFixture ( expected );
6872 try (RandomAccessFile randomAccessFile = randomAccessFileMode .create (fixture )) {
6973 assertArrayEquals (expected , read (randomAccessFile ));
7074 }
@@ -73,8 +77,8 @@ public void testCreatePath(final RandomAccessFileMode randomAccessFileMode) thro
7377 @ ParameterizedTest
7478 @ EnumSource (RandomAccessFileMode .class )
7579 public void testCreateString (final RandomAccessFileMode randomAccessFileMode ) throws IOException {
76- final byte [] expected = "Foo" . getBytes ( StandardCharsets . US_ASCII ) ;
77- final Path fixture = Files . write ( tempDir . resolve ( "test.txt" ), expected );
80+ final byte [] expected = BYTES_FIXTURE ;
81+ final Path fixture = writeFixture ( expected );
7882 try (RandomAccessFile randomAccessFile = randomAccessFileMode .create (fixture .toString ())) {
7983 assertArrayEquals (expected , read (randomAccessFile ));
8084 }
@@ -96,6 +100,16 @@ public void testImplies() {
96100 assertFalse (RandomAccessFileMode .READ_ONLY .implies (RandomAccessFileMode .READ_WRITE_SYNC_ALL ));
97101 }
98102
103+ @ ParameterizedTest
104+ @ EnumSource (RandomAccessFileMode .class )
105+ public void testIoString (final RandomAccessFileMode randomAccessFileMode ) throws IOException {
106+ final byte [] expected = BYTES_FIXTURE ;
107+ final Path fixture = writeFixture (expected );
108+ try (IORandomAccessFile randomAccessFile = randomAccessFileMode .io (fixture .toString ())) {
109+ assertArrayEquals (expected , read (randomAccessFile ));
110+ }
111+ }
112+
99113 /**
100114 * Tests the standard {@link Enum#toString()} behavior.
101115 */
@@ -145,4 +159,8 @@ public void testValueOfOpenOptions() {
145159 assertEquals (RandomAccessFileMode .READ_WRITE_SYNC_ALL ,
146160 RandomAccessFileMode .valueOf (StandardOpenOption .READ , StandardOpenOption .WRITE , StandardOpenOption .SYNC ));
147161 }
162+
163+ private Path writeFixture (final byte [] bytes ) throws IOException {
164+ return Files .write (tempDir .resolve (FIXTURE ), bytes , StandardOpenOption .CREATE , StandardOpenOption .TRUNCATE_EXISTING );
165+ }
148166}
0 commit comments