2525import static org .junit .jupiter .api .Assertions .assertSame ;
2626import static org .junit .jupiter .api .Assertions .assertThrows ;
2727import static org .junit .jupiter .api .Assertions .assertTrue ;
28- import static org .junit .jupiter .api .Assertions .fail ;
2928
3029import java .io .BufferedInputStream ;
3130import java .io .BufferedOutputStream ;
@@ -177,30 +176,15 @@ public int read() throws IOException {
177176
178177 @ Test
179178 public void testAsBufferedNull () {
180- try {
181- IOUtils .buffer ((InputStream ) null );
182- fail ("Expected NullPointerException" );
183- } catch (final NullPointerException npe ) {
184- // expected
185- }
186- try {
187- IOUtils .buffer ((OutputStream ) null );
188- fail ("Expected NullPointerException" );
189- } catch (final NullPointerException npe ) {
190- // expected
191- }
192- try {
193- IOUtils .buffer ((Reader ) null );
194- fail ("Expected NullPointerException" );
195- } catch (final NullPointerException npe ) {
196- // expected
197- }
198- try {
199- IOUtils .buffer ((Writer ) null );
200- fail ("Expected NullPointerException" );
201- } catch (final NullPointerException npe ) {
202- // expected
203- }
179+ final String npeExpectedMessage = "Expected NullPointerException" ;
180+ assertThrows (NullPointerException .class , ()->IOUtils .buffer ((InputStream ) null ),
181+ npeExpectedMessage );
182+ assertThrows (NullPointerException .class , ()->IOUtils .buffer ((OutputStream ) null ),
183+ npeExpectedMessage );
184+ assertThrows (NullPointerException .class , ()->IOUtils .buffer ((Reader ) null ),
185+ npeExpectedMessage );
186+ assertThrows (NullPointerException .class , ()->IOUtils .buffer ((Writer ) null ),
187+ npeExpectedMessage );
204188 }
205189
206190 @ Test
@@ -926,12 +910,8 @@ public void testRead_ReadableByteChannel() throws Exception {
926910 assertEquals (0 , buffer .remaining ());
927911 assertEquals (0 , input .read (buffer ));
928912 buffer .clear ();
929- try {
930- IOUtils .readFully (input , buffer );
931- fail ("Should have failed with EOFxception" );
932- } catch (final EOFException expected ) {
933- // expected
934- }
913+ assertThrows (EOFException .class , ()->IOUtils .readFully (input , buffer ),
914+ "Should have failed with EOFException" );
935915 } finally {
936916 IOUtils .closeQuietly (input , fileInputStream );
937917 }
@@ -952,26 +932,17 @@ public void testReadFully_InputStream__ReturnByteArray() throws Exception {
952932 @ Test
953933 public void testReadFully_InputStream_ByteArray () throws Exception {
954934 final int size = 1027 ;
955-
956935 final byte [] buffer = new byte [size ];
957-
958936 final InputStream input = new ByteArrayInputStream (new byte [size ]);
959- try {
960- IOUtils .readFully (input , buffer , 0 , -1 );
961- fail ("Should have failed with IllegalArgumentException" );
962- } catch (final IllegalArgumentException expected ) {
963- // expected
964- }
937+
938+ assertThrows (IllegalArgumentException .class , ()-> IOUtils .readFully (input , buffer , 0 , -1 ),
939+ "Should have failed with IllegalArgumentException" );
940+
965941 IOUtils .readFully (input , buffer , 0 , 0 );
966942 IOUtils .readFully (input , buffer , 0 , size - 1 );
967- try {
968- IOUtils .readFully (input , buffer , 0 , 2 );
969- fail ("Should have failed with EOFxception" );
970- } catch (final EOFException expected ) {
971- // expected
972- }
943+ assertThrows (EOFException .class , ()-> IOUtils .readFully (input , buffer , 0 , 2 ),
944+ "Should have failed with EOFException" );
973945 IOUtils .closeQuietly (input );
974-
975946 }
976947
977948 @ Test
@@ -999,12 +970,8 @@ public void testReadFully_ReadableByteChannel() throws Exception {
999970 assertEquals (0 , input .read (buffer ));
1000971 IOUtils .readFully (input , buffer );
1001972 buffer .clear ();
1002- try {
1003- IOUtils .readFully (input , buffer );
1004- fail ("Should have failed with EOFxception" );
1005- } catch (final EOFException expected ) {
1006- // expected
1007- }
973+ assertThrows (EOFException .class , ()->IOUtils .readFully (input , buffer ),
974+ "Should have failed with EOFxception" );
1008975 } finally {
1009976 IOUtils .closeQuietly (input , fileInputStream );
1010977 }
@@ -1013,24 +980,15 @@ public void testReadFully_ReadableByteChannel() throws Exception {
1013980 @ Test
1014981 public void testReadFully_Reader () throws Exception {
1015982 final int size = 1027 ;
1016-
1017983 final char [] buffer = new char [size ];
1018-
1019984 final Reader input = new CharArrayReader (new char [size ]);
985+
1020986 IOUtils .readFully (input , buffer , 0 , 0 );
1021987 IOUtils .readFully (input , buffer , 0 , size - 3 );
1022- try {
1023- IOUtils .readFully (input , buffer , 0 , -1 );
1024- fail ("Should have failed with IllegalArgumentException" );
1025- } catch (final IllegalArgumentException expected ) {
1026- // expected
1027- }
1028- try {
1029- IOUtils .readFully (input , buffer , 0 , 5 );
1030- fail ("Should have failed with EOFException" );
1031- } catch (final EOFException expected ) {
1032- // expected
1033- }
988+ assertThrows (IllegalArgumentException .class , ()->IOUtils .readFully (input , buffer , 0 , -1 ),
989+ "Should have failed with IllegalArgumentException" );
990+ assertThrows (EOFException .class , ()->IOUtils .readFully (input , buffer , 0 , 5 ),
991+ "Should have failed with EOFException" );
1034992 IOUtils .closeQuietly (input );
1035993 }
1036994
@@ -1318,43 +1276,27 @@ public void testSkipFully_InputStream() throws Exception {
13181276 final int size = 1027 ;
13191277
13201278 final InputStream input = new ByteArrayInputStream (new byte [size ]);
1321- try {
1322- IOUtils .skipFully (input , -1 );
1323- fail ("Should have failed with IllegalArgumentException" );
1324- } catch (final IllegalArgumentException expected ) {
1325- // expected
1326- }
1279+ assertThrows (IllegalArgumentException .class , ()->IOUtils .skipFully (input , -1 ),
1280+ "Should have failed with IllegalArgumentException" );
1281+
13271282 IOUtils .skipFully (input , 0 );
13281283 IOUtils .skipFully (input , size - 1 );
1329- try {
1330- IOUtils .skipFully (input , 2 );
1331- fail ("Should have failed with IOException" );
1332- } catch (final IOException expected ) {
1333- // expected
1334- }
1284+ assertThrows (IOException .class , ()-> IOUtils .skipFully (input , 2 ),
1285+ "Should have failed with IOException" );
13351286 IOUtils .closeQuietly (input );
1336-
13371287 }
13381288
13391289 @ Test
13401290 public void testSkipFully_ReadableByteChannel () throws Exception {
13411291 final FileInputStream fileInputStream = new FileInputStream (testFile );
13421292 final FileChannel fileChannel = fileInputStream .getChannel ();
13431293 try {
1344- try {
1345- IOUtils .skipFully (fileChannel , -1 );
1346- fail ("Should have failed with IllegalArgumentException" );
1347- } catch (final IllegalArgumentException expected ) {
1348- // expected
1349- }
1294+ assertThrows (IllegalArgumentException .class , ()->IOUtils .skipFully (fileChannel , -1 ),
1295+ "Should have failed with IllegalArgumentException" );
13501296 IOUtils .skipFully (fileChannel , 0 );
13511297 IOUtils .skipFully (fileChannel , FILE_SIZE - 1 );
1352- try {
1353- IOUtils .skipFully (fileChannel , 2 );
1354- fail ("Should have failed with IOException" );
1355- } catch (final IOException expected ) {
1356- // expected
1357- }
1298+ assertThrows (IOException .class , ()->IOUtils .skipFully (fileChannel , 2 ),
1299+ "Should have failed with IOException" );
13581300 } finally {
13591301 IOUtils .closeQuietly (fileChannel , fileInputStream );
13601302 }
@@ -1363,22 +1305,14 @@ public void testSkipFully_ReadableByteChannel() throws Exception {
13631305 @ Test
13641306 public void testSkipFully_Reader () throws Exception {
13651307 final int size = 1027 ;
1366-
13671308 final Reader input = new CharArrayReader (new char [size ]);
1309+
13681310 IOUtils .skipFully (input , 0 );
13691311 IOUtils .skipFully (input , size - 3 );
1370- try {
1371- IOUtils .skipFully (input , -1 );
1372- fail ("Should have failed with IllegalArgumentException" );
1373- } catch (final IllegalArgumentException expected ) {
1374- // expected
1375- }
1376- try {
1377- IOUtils .skipFully (input , 5 );
1378- fail ("Should have failed with IOException" );
1379- } catch (final IOException expected ) {
1380- // expected
1381- }
1312+ assertThrows (IllegalArgumentException .class , ()->IOUtils .skipFully (input , -1 ),
1313+ "Should have failed with IllegalArgumentException" );
1314+ assertThrows (IOException .class , ()->IOUtils .skipFully (input , 5 ),
1315+ "Should have failed with IOException" );
13821316 IOUtils .closeQuietly (input );
13831317 }
13841318
@@ -1452,13 +1386,11 @@ public void testToByteArray_InputStream_LongerThanIntegerMaxValue() throws Excep
14521386 public void testToByteArray_InputStream_NegativeSize () throws Exception {
14531387
14541388 try (InputStream fin = Files .newInputStream (testFilePath )) {
1455- IOUtils .toByteArray (fin , -1 );
1456- fail ("IllegalArgumentException expected" );
1457- } catch (final IllegalArgumentException exc ) {
1389+ final IllegalArgumentException exc = assertThrows (IllegalArgumentException .class ,
1390+ ()->IOUtils .toByteArray (fin , -1 ), "Should have failed with IllegalArgumentException" );
14581391 assertTrue (exc .getMessage ().startsWith ("Size must be equal or greater than zero" ),
14591392 "Exception message does not start with \" Size must be equal or greater than zero\" " );
14601393 }
1461-
14621394 }
14631395
14641396 @ Test
@@ -1476,26 +1408,23 @@ public void testToByteArray_InputStream_Size() throws Exception {
14761408 public void testToByteArray_InputStream_SizeIllegal () throws Exception {
14771409
14781410 try (InputStream fin = Files .newInputStream (testFilePath )) {
1479- IOUtils .toByteArray (fin , testFile .length () + 1 );
1480- fail ("IOException expected" );
1481- } catch (final IOException exc ) {
1411+ final IOException exc = assertThrows (IOException .class ,
1412+ ()->IOUtils .toByteArray (fin , testFile .length () + 1 ), "Should have failed with IOException" );
14821413 assertTrue (exc .getMessage ().startsWith ("Unexpected read size" ),
14831414 "Exception message does not start with \" Unexpected read size\" " );
14841415 }
1485-
14861416 }
14871417
14881418 @ Test
14891419 public void testToByteArray_InputStream_SizeLong () throws Exception {
14901420
14911421 try (InputStream fin = Files .newInputStream (testFilePath )) {
1492- IOUtils . toByteArray ( fin , ( long ) Integer . MAX_VALUE + 1 );
1493- fail ( "IOException expected" );
1494- } catch ( final IllegalArgumentException exc ) {
1422+ final IllegalArgumentException exc = assertThrows ( IllegalArgumentException . class ,
1423+ ()-> IOUtils . toByteArray ( fin , ( long ) Integer . MAX_VALUE + 1 ),
1424+ "Should have failed with IllegalArgumentException" );
14951425 assertTrue (exc .getMessage ().startsWith ("Size cannot be greater than Integer max value" ),
14961426 "Exception message does not start with \" Size cannot be greater than Integer max value\" " );
14971427 }
1498-
14991428 }
15001429
15011430 @ Test
0 commit comments