1818package org .apache .commons .io .output ;
1919
2020import static org .junit .jupiter .api .Assertions .assertEquals ;
21+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
2122import static org .junit .jupiter .api .Assertions .assertThrows ;
2223
2324import java .io .IOException ;
25+ import java .io .RandomAccessFile ;
2426import java .nio .charset .Charset ;
2527import java .nio .charset .StandardCharsets ;
2628import java .nio .file .Files ;
@@ -56,6 +58,7 @@ public void testClose() throws IOException {
5658 assertEquals (EXPECTED , new String (Files .readAllBytes (fixturePath ), charset ));
5759 // @formatter:on
5860 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream .builder ().setPath (fixturePath ).get ()) {
61+ validateState (os );
5962 }
6063 }
6164
@@ -77,6 +80,7 @@ public void testFlush() throws IOException {
7780 assertEquals (EXPECTED , new String (Files .readAllBytes (fixturePath ), charset ));
7881 // @formatter:on
7982 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream .builder ().setPath (fixturePath ).get ()) {
83+ validateState (os );
8084 }
8185 }
8286
@@ -94,6 +98,7 @@ public void testWriteByteArray() throws IOException {
9498 assertEquals (EXPECTED , new String (Files .readAllBytes (fixturePath ), charset ));
9599 // @formatter:on
96100 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream .builder ().setPath (fixturePath ).get ()) {
101+ validateState (os );
97102 }
98103 }
99104
@@ -111,6 +116,7 @@ public void testWriteByteArrayAt() throws IOException {
111116 assertEquals (EXPECTED .subSequence (1 , EXPECTED .length () - 1 ), new String (Files .readAllBytes (fixturePath ), charset ));
112117 // @formatter:on
113118 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream .builder ().setPath (fixturePath ).get ()) {
119+ validateState (os );
114120 }
115121 }
116122
@@ -122,7 +128,7 @@ public void testWriteGet() throws IOException {
122128 .setPath (fixturePath )
123129 .setOpenOptions (StandardOpenOption .WRITE )
124130 .get ()) {
125- // doNothing
131+ validateState ( os );
126132 }
127133 // @formatter:on
128134 }
@@ -131,7 +137,7 @@ public void testWriteGet() throws IOException {
131137 public void testWriteGetDefault () throws IOException {
132138 assertThrows (IllegalStateException .class , () -> {
133139 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream .builder ().get ()) {
134- // doNothing
140+ validateState ( os );
135141 }
136142 });
137143 }
@@ -148,7 +154,7 @@ public void testWriteGetPathOnly() throws IOException {
148154 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream .builder ()
149155 .setPath (fixturePath )
150156 .get ()) {
151- // doNothing
157+ validateState ( os );
152158 }
153159 // @formatter:on
154160 }
@@ -162,6 +168,7 @@ public void testWriteInt() throws IOException {
162168 .setPath (fixturePath )
163169 .setOpenOptions (StandardOpenOption .WRITE )
164170 .get ()) {
171+ validateState (os );
165172 final byte [] bytes = EXPECTED .getBytes (charset );
166173 for (final byte element : bytes ) {
167174 os .write (element );
@@ -170,7 +177,15 @@ public void testWriteInt() throws IOException {
170177 assertEquals (EXPECTED , new String (Files .readAllBytes (fixturePath ), charset ));
171178 // @formatter:on
172179 try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream .builder ().setPath (fixturePath ).get ()) {
180+ validateState (os );
173181 }
174182 }
175183
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+
176191}
0 commit comments