use assertThrows in testAsBufferedNull
MODIFY
src/test/java/org/apache/commons/io/IOUtilsTest.java
src/test/java/org/apache/commons/io/IOUtilsTest.java
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@@ -177,30 +176,15 @@ public int read() throws IOException {
@Test
public void testAsBufferedNull() {
- try {
- IOUtils.buffer((InputStream) null);
- fail("Expected NullPointerException");
- } catch (final NullPointerException npe) {
- // expected
- }
- try {
- IOUtils.buffer((OutputStream) null);
- fail("Expected NullPointerException");
- } catch (final NullPointerException npe) {
- // expected
- }
- try {
- IOUtils.buffer((Reader) null);
- fail("Expected NullPointerException");
- } catch (final NullPointerException npe) {
- // expected
- }
- try {
- IOUtils.buffer((Writer) null);
- fail("Expected NullPointerException");
- } catch (final NullPointerException npe) {
- // expected
- }
+ final String npeExpectedMessage = "Expected NullPointerException";
+ assertThrows(NullPointerException.class, ()->IOUtils.buffer((InputStream) null),
+ npeExpectedMessage );
+ assertThrows(NullPointerException.class, ()->IOUtils.buffer((OutputStream) null),
+ npeExpectedMessage);
+ assertThrows(NullPointerException.class, ()->IOUtils.buffer((Reader) null),
+ npeExpectedMessage);
+ assertThrows(NullPointerException.class, ()->IOUtils.buffer((Writer) null),
+ npeExpectedMessage);
}
@Test
@@ -926,12 +910,8 @@ public void testRead_ReadableByteChannel() throws Exception {
assertEquals(0, buffer.remaining());
assertEquals(0, input.read(buffer));
buffer.clear();
- try {
- IOUtils.readFully(input, buffer);
- fail("Should have failed with EOFxception");
- } catch (final EOFException expected) {
- // expected
- }
+ assertThrows(EOFException.class, ()->IOUtils.readFully(input, buffer),
+ "Should have failed with EOFException");
} finally {
IOUtils.closeQuietly(input, fileInputStream);
}
@@ -952,26 +932,17 @@ public void testReadFully_InputStream__ReturnByteArray() throws Exception {
@Test
public void testReadFully_InputStream_ByteArray() throws Exception {
final int size = 1027;
-
final byte[] buffer = new byte[size];
-
final InputStream input = new ByteArrayInputStream(new byte[size]);
- try {
- IOUtils.readFully(input, buffer, 0, -1);
- fail("Should have failed with IllegalArgumentException");
- } catch (final IllegalArgumentException expected) {
- // expected
- }
+
+ assertThrows(IllegalArgumentException.class, ()-> IOUtils.readFully(input, buffer, 0, -1),
+ "Should have failed with IllegalArgumentException");
+
IOUtils.readFully(input, buffer, 0, 0);
IOUtils.readFully(input, buffer, 0, size - 1);
- try {
- IOUtils.readFully(input, buffer, 0, 2);
- fail("Should have failed with EOFxception");
- } catch (final EOFException expected) {
- // expected
- }
+ assertThrows(EOFException.class, ()-> IOUtils.readFully(input, buffer, 0, 2),
+ "Should have failed with EOFException");
IOUtils.closeQuietly(input);
-
}
@Test
@@ -999,12 +970,8 @@ public void testReadFully_ReadableByteChannel() throws Exception {
assertEquals(0, input.read(buffer));
IOUtils.readFully(input, buffer);
buffer.clear();
- try {
- IOUtils.readFully(input, buffer);
- fail("Should have failed with EOFxception");
- } catch (final EOFException expected) {
- // expected
- }
+ assertThrows(EOFException.class, ()->IOUtils.readFully(input, buffer),
+ "Should have failed with EOFxception");
} finally {
IOUtils.closeQuietly(input, fileInputStream);
}
@@ -1013,24 +980,15 @@ public void testReadFully_ReadableByteChannel() throws Exception {
@Test
public void testReadFully_Reader() throws Exception {
final int size = 1027;
-
final char[] buffer = new char[size];
-
final Reader input = new CharArrayReader(new char[size]);
+
IOUtils.readFully(input, buffer, 0, 0);
IOUtils.readFully(input, buffer, 0, size - 3);
- try {
- IOUtils.readFully(input, buffer, 0, -1);
- fail("Should have failed with IllegalArgumentException");
- } catch (final IllegalArgumentException expected) {
- // expected
- }
- try {
- IOUtils.readFully(input, buffer, 0, 5);
- fail("Should have failed with EOFException");
- } catch (final EOFException expected) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, ()->IOUtils.readFully(input, buffer, 0, -1),
+ "Should have failed with IllegalArgumentException" );
+ assertThrows(EOFException.class, ()->IOUtils.readFully(input, buffer, 0, 5),
+ "Should have failed with EOFException" );
IOUtils.closeQuietly(input);
}
@@ -1318,22 +1276,14 @@ public void testSkipFully_InputStream() throws Exception {
final int size = 1027;
final InputStream input = new ByteArrayInputStream(new byte[size]);
- try {
- IOUtils.skipFully(input, -1);
- fail("Should have failed with IllegalArgumentException");
- } catch (final IllegalArgumentException expected) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, ()->IOUtils.skipFully(input, -1),
+ "Should have failed with IllegalArgumentException" );
+
IOUtils.skipFully(input, 0);
IOUtils.skipFully(input, size - 1);
- try {
- IOUtils.skipFully(input, 2);
- fail("Should have failed with IOException");
- } catch (final IOException expected) {
- // expected
- }
+ assertThrows(IOException.class, ()-> IOUtils.skipFully(input, 2),
+ "Should have failed with IOException" );
IOUtils.closeQuietly(input);
-
}
@Test
@@ -1341,20 +1291,12 @@ public void testSkipFully_ReadableByteChannel() throws Exception {
final FileInputStream fileInputStream = new FileInputStream(testFile);
final FileChannel fileChannel = fileInputStream.getChannel();
try {
- try {
- IOUtils.skipFully(fileChannel, -1);
- fail("Should have failed with IllegalArgumentException");
- } catch (final IllegalArgumentException expected) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, ()->IOUtils.skipFully(fileChannel, -1),
+ "Should have failed with IllegalArgumentException" );
IOUtils.skipFully(fileChannel, 0);
IOUtils.skipFully(fileChannel, FILE_SIZE - 1);
- try {
- IOUtils.skipFully(fileChannel, 2);
- fail("Should have failed with IOException");
- } catch (final IOException expected) {
- // expected
- }
+ assertThrows(IOException.class, ()->IOUtils.skipFully(fileChannel, 2),
+ "Should have failed with IOException" );
} finally {
IOUtils.closeQuietly(fileChannel, fileInputStream);
}
@@ -1363,22 +1305,14 @@ public void testSkipFully_ReadableByteChannel() throws Exception {
@Test
public void testSkipFully_Reader() throws Exception {
final int size = 1027;
-
final Reader input = new CharArrayReader(new char[size]);
+
IOUtils.skipFully(input, 0);
IOUtils.skipFully(input, size - 3);
- try {
- IOUtils.skipFully(input, -1);
- fail("Should have failed with IllegalArgumentException");
- } catch (final IllegalArgumentException expected) {
- // expected
- }
- try {
- IOUtils.skipFully(input, 5);
- fail("Should have failed with IOException");
- } catch (final IOException expected) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, ()->IOUtils.skipFully(input, -1),
+ "Should have failed with IllegalArgumentException" );
+ assertThrows(IOException.class, ()->IOUtils.skipFully(input, 5),
+ "Should have failed with IOException" );
IOUtils.closeQuietly(input);
}
@@ -1452,13 +1386,11 @@ public void testToByteArray_InputStream_LongerThanIntegerMaxValue() throws Excep
public void testToByteArray_InputStream_NegativeSize() throws Exception {
try (InputStream fin = Files.newInputStream(testFilePath)) {
- IOUtils.toByteArray(fin, -1);
- fail("IllegalArgumentException expected");
- } catch (final IllegalArgumentException exc) {
+ final IllegalArgumentException exc = assertThrows(IllegalArgumentException.class,
+ ()->IOUtils.toByteArray(fin, -1), "Should have failed with IllegalArgumentException" );
assertTrue(exc.getMessage().startsWith("Size must be equal or greater than zero"),
"Exception message does not start with \"Size must be equal or greater than zero\"");
}
-
}
@Test
@@ -1476,26 +1408,23 @@ public void testToByteArray_InputStream_Size() throws Exception {
public void testToByteArray_InputStream_SizeIllegal() throws Exception {
try (InputStream fin = Files.newInputStream(testFilePath)) {
- IOUtils.toByteArray(fin, testFile.length() + 1);
- fail("IOException expected");
- } catch (final IOException exc) {
+ final IOException exc = assertThrows(IOException.class,
+ ()->IOUtils.toByteArray(fin, testFile.length() + 1), "Should have failed with IOException" );
assertTrue(exc.getMessage().startsWith("Unexpected read size"),
"Exception message does not start with \"Unexpected read size\"");
}
-
}
@Test
public void testToByteArray_InputStream_SizeLong() throws Exception {
try (InputStream fin = Files.newInputStream(testFilePath)) {
- IOUtils.toByteArray(fin, (long) Integer.MAX_VALUE + 1);
- fail("IOException expected");
- } catch (final IllegalArgumentException exc) {
+ final IllegalArgumentException exc = assertThrows(IllegalArgumentException.class,
+ ()-> IOUtils.toByteArray(fin, (long) Integer.MAX_VALUE + 1),
+ "Should have failed with IllegalArgumentException" );
assertTrue(exc.getMessage().startsWith("Size cannot be greater than Integer max value"),
"Exception message does not start with \"Size cannot be greater than Integer max value\"");
}
-
}
@Test

use assertThrows in testAsBufferedNull