You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/06/09 15:55:22 UTC
[commons-io] 06/06: Migrate from JUnit 4 to 5
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit d929d05991ca677efaedb3d51abacb40dd1464d0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 9 11:55:10 2022 -0400
Migrate from JUnit 4 to 5
Exception cleanups in tests.
---
.../org/apache/commons/io/ByteOrderMarkTest.java | 31 +---
src/test/java/org/apache/commons/io/DemuxTest.java | 8 +-
.../org/apache/commons/io/DirectoryWalkerTest.java | 8 +-
.../commons/io/DirectoryWalkerTestCaseJava4.java | 16 +--
.../org/apache/commons/io/EndianUtilsTest.java | 10 +-
.../commons/io/FileUtilsDirectoryContainsTest.java | 23 +--
.../apache/commons/io/FileUtilsListFilesTest.java | 10 +-
.../org/apache/commons/io/FilenameUtilsTest.java | 37 +----
.../java/org/apache/commons/io/HexDumpTest.java | 43 ++----
.../io/file/PathUtilsContentEqualsTest.java | 9 +-
.../commons/io/input/BOMInputStreamTest.java | 42 +-----
.../io/input/ClassLoaderObjectInputStreamTest.java | 4 +-
.../commons/io/input/CountingInputStreamTest.java | 16 +--
.../commons/io/input/NullInputStreamTest.java | 17 +--
.../apache/commons/io/input/NullReaderTest.java | 17 +--
.../io/monitor/FileAlterationMonitorTest.java | 17 +--
.../commons/io/output/ClosedOutputStreamTest.java | 16 +--
.../apache/commons/io/output/ClosedWriterTest.java | 16 +--
.../io/output/CountingOutputStreamTest.java | 16 +--
.../io/output/DeferredFileOutputStreamTest.java | 156 +++++++--------------
.../io/output/ProxyCollectionWriterTest.java | 9 +-
.../commons/io/output/StringBuilderWriterTest.java | 12 +-
.../commons/io/output/TaggedOutputStreamTest.java | 3 +-
.../apache/commons/io/output/TaggedWriterTest.java | 22 +--
24 files changed, 142 insertions(+), 416 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/ByteOrderMarkTest.java b/src/test/java/org/apache/commons/io/ByteOrderMarkTest.java
index 356d4074..eadc74d9 100644
--- a/src/test/java/org/apache/commons/io/ByteOrderMarkTest.java
+++ b/src/test/java/org/apache/commons/io/ByteOrderMarkTest.java
@@ -21,8 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.fail;
-
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.nio.charset.Charset;
import org.junit.jupiter.api.Test;
@@ -59,30 +58,10 @@ public class ByteOrderMarkTest {
/** Test Errors */
@Test
public void errors() {
- try {
- new ByteOrderMark(null, 1,2,3);
- fail("null charset name, expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
- try {
- new ByteOrderMark("", 1,2,3);
- fail("no charset name, expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
- try {
- new ByteOrderMark("a", (int[])null);
- fail("null bytes, expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
- try {
- new ByteOrderMark("b");
- fail("empty bytes, expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark(null, 1, 2, 3));
+ assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("", 1, 2, 3));
+ assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("a", (int[]) null));
+ assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("b"));
}
/** Test {@link ByteOrderMark#get(int)} */
diff --git a/src/test/java/org/apache/commons/io/DemuxTest.java b/src/test/java/org/apache/commons/io/DemuxTest.java
index c705ffd3..a9da9b09 100644
--- a/src/test/java/org/apache/commons/io/DemuxTest.java
+++ b/src/test/java/org/apache/commons/io/DemuxTest.java
@@ -138,15 +138,12 @@ public class DemuxTest {
private String getInput(final String threadName) {
final ReaderThread thread = (ReaderThread) threadMap.get(threadName);
assertNotNull(thread, "getInput()");
-
return thread.getData();
}
private String getOutput(final String threadName) {
- final ByteArrayOutputStream output =
- outputMap.get(threadName);
+ final ByteArrayOutputStream output = outputMap.get(threadName);
assertNotNull(output, "getOutput()");
-
return output.toString(StandardCharsets.UTF_8);
}
@@ -163,8 +160,7 @@ public class DemuxTest {
final DemuxOutputStream demux) {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
outputMap.put(name, output);
- final WriterThread thread =
- new WriterThread(name, data, output, demux);
+ final WriterThread thread = new WriterThread(name, data, output, demux);
threadMap.put(name, thread);
}
diff --git a/src/test/java/org/apache/commons/io/DirectoryWalkerTest.java b/src/test/java/org/apache/commons/io/DirectoryWalkerTest.java
index 6f9031e1..f6524aee 100644
--- a/src/test/java/org/apache/commons/io/DirectoryWalkerTest.java
+++ b/src/test/java/org/apache/commons/io/DirectoryWalkerTest.java
@@ -18,6 +18,7 @@ package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -513,12 +514,7 @@ public class DirectoryWalkerTest {
assertEquals(1, results.size(), "Result Size");
assertTrue(results.contains(invalidDir), "Current Dir");
- try {
- new TestFileFinder(null, -1).find(null);
- fail("Null start directory didn't throw Exception");
- } catch (final NullPointerException ignore) {
- // expected result
- }
+ assertThrows(NullPointerException.class, () -> new TestFileFinder(null, -1).find(null));
}
/**
diff --git a/src/test/java/org/apache/commons/io/DirectoryWalkerTestCaseJava4.java b/src/test/java/org/apache/commons/io/DirectoryWalkerTestCaseJava4.java
index 4452b474..46d2bf4a 100644
--- a/src/test/java/org/apache/commons/io/DirectoryWalkerTestCaseJava4.java
+++ b/src/test/java/org/apache/commons/io/DirectoryWalkerTestCaseJava4.java
@@ -18,6 +18,7 @@ package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -448,8 +449,6 @@ public class DirectoryWalkerTestCaseJava4 {
checkContainsFiles("[DirAndFile4] File", ioFiles, resultFiles);
}
- // ------------ Test DirectoryWalker implementation --------------------------
-
/**
* test an invalid start directory
*/
@@ -461,8 +460,6 @@ public class DirectoryWalkerTestCaseJava4 {
}
- // ------------ Test DirectoryWalker implementation --------------------------
-
/**
* Test Limiting to current directory
*/
@@ -473,10 +470,8 @@ public class DirectoryWalkerTestCaseJava4 {
assertTrue(results.contains(FileUtils.current()), "Current Dir");
}
- // ------------ Test DirectoryWalker implementation --------------------------
-
/**
- * test an invalid start directory
+ * Test an invalid start directory
*/
@Test
public void testMissingStartDirectory() {
@@ -487,12 +482,7 @@ public class DirectoryWalkerTestCaseJava4 {
assertEquals(1, results.size(), "Result Size");
assertTrue(results.contains(invalidDir), "Current Dir");
- try {
- new TestFileFinder(null, -1).find(null);
- fail("Null start directory didn't throw Exception");
- } catch (final NullPointerException ignore) {
- // expected result
- }
+ assertThrows(NullPointerException.class, () -> new TestFileFinder(null, -1).find(null));
}
/**
diff --git a/src/test/java/org/apache/commons/io/EndianUtilsTest.java b/src/test/java/org/apache/commons/io/EndianUtilsTest.java
index 4902c4d7..e8adbe51 100644
--- a/src/test/java/org/apache/commons/io/EndianUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/EndianUtilsTest.java
@@ -17,8 +17,7 @@
package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
-
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
@@ -40,12 +39,7 @@ public class EndianUtilsTest {
@Test
public void testEOFException() throws IOException {
final ByteArrayInputStream input = new ByteArrayInputStream(new byte[] {});
- try {
- EndianUtils.readSwappedDouble(input);
- fail("Expected EOFException");
- } catch (final EOFException e) {
- // expected
- }
+ assertThrows(EOFException.class, () -> EndianUtils.readSwappedDouble(input));
}
@Test
diff --git a/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTest.java b/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTest.java
index 8059ee0f..2dc8f337 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTest.java
@@ -17,9 +17,8 @@
package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertFalse;
+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.File;
import java.io.IOException;
@@ -115,12 +114,7 @@ public class FileUtilsDirectoryContainsTest {
public void testDirectoryDoesNotExist() throws IOException {
final File dir = new File("DOESNOTEXIST");
assertFalse(dir.exists());
- try {
- assertFalse(FileUtils.directoryContains(dir, file1));
- fail("Expected " + IllegalArgumentException.class.getName());
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> FileUtils.directoryContains(dir, file1));
}
@Test
@@ -164,12 +158,7 @@ public class FileUtilsDirectoryContainsTest {
@Test
public void testSameFile() throws IOException {
- try {
- assertTrue(FileUtils.directoryContains(file1, file1));
- fail("Expected " + IllegalArgumentException.class.getName());
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> FileUtils.directoryContains(file1, file1));
}
@Test
@@ -178,10 +167,6 @@ public class FileUtilsDirectoryContainsTest {
final File file = new File(dir, "DOESNOTEXIST2");
assertFalse(dir.exists());
assertFalse(file.exists());
- try {
- assertTrue(FileUtils.directoryContains(dir, file));
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> FileUtils.directoryContains(dir, file));
}
}
diff --git a/src/test/java/org/apache/commons/io/FileUtilsListFilesTest.java b/src/test/java/org/apache/commons/io/FileUtilsListFilesTest.java
index 31ad761b..f13ff7c4 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsListFilesTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsListFilesTest.java
@@ -18,9 +18,8 @@ package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+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.File;
import java.util.ArrayList;
import java.util.Collection;
@@ -156,12 +155,7 @@ public class FileUtilsListFilesTest {
assertTrue(filenames.contains("dummy-index.html"), "'dummy-index.html' is missing");
assertFalse(filenames.contains("Entries"), "'Entries' shouldn't be found");
- try {
- FileUtils.listFiles(temporaryFolder, null, null);
- fail("Expected error about null parameter");
- } catch (final NullPointerException e) {
- // expected
- }
+ assertThrows(NullPointerException.class, () -> FileUtils.listFiles(temporaryFolder, null, null));
}
@Test
diff --git a/src/test/java/org/apache/commons/io/FilenameUtilsTest.java b/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
index 4e411868..166a78b2 100644
--- a/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
@@ -21,8 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
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.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
@@ -457,11 +455,7 @@ public class FilenameUtilsTest {
@Test
public void testGetPathNoEndSeparator_with_null_character() {
- try {
- assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~user/a\u0000/b/c.txt"));
- } catch (final IllegalArgumentException ignore) {
-
- }
+ assertThrows(IllegalArgumentException.class, () -> FilenameUtils.getPathNoEndSeparator("~user/a\u0000/b/c.txt"));
}
@Test
@@ -511,11 +505,7 @@ public class FilenameUtilsTest {
@Test
public void testGetPrefix_with_null_character() {
- try {
- assertEquals("~user\\", FilenameUtils.getPrefix("~u\u0000ser\\a\\b\\c.txt"));
- } catch (final IllegalArgumentException ignore) {
-
- }
+ assertThrows(IllegalArgumentException.class, () -> FilenameUtils.getPrefix("~u\u0000ser\\a\\b\\c.txt"));
}
@Test
@@ -617,11 +607,7 @@ public class FilenameUtilsTest {
@Test
public void testInjectionFailure() {
- try {
- assertEquals("c", FilenameUtils.getName("a\\b\\\u0000c"));
- } catch (final IllegalArgumentException ignore) {
-
- }
+ assertThrows(IllegalArgumentException.class, () -> FilenameUtils.getName("a\\b\\\u0000c"));
}
@Test
@@ -659,11 +645,7 @@ public class FilenameUtilsTest {
@Test
public void testIsExtension_injection() {
- try {
- FilenameUtils.isExtension("a.b\\fi\u0000le.txt", "TXT");
- fail("Should throw IAE");
- } catch (final IllegalArgumentException ignore) {
- }
+ assertThrows(IllegalArgumentException.class, () -> FilenameUtils.isExtension("a.b\\fi\u0000le.txt", "TXT"));
}
@Test
@@ -953,15 +935,8 @@ public class FilenameUtilsTest {
*/
@Test
public void testNormalize_with_null_character() {
- try {
- assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("a\\b/c\u0000.txt"));
- } catch (final IllegalArgumentException ignore) {
- }
-
- try {
- assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("\u0000a\\b/c.txt"));
- } catch (final IllegalArgumentException ignore) {
- }
+ assertThrows(IllegalArgumentException.class, () -> FilenameUtils.normalize("a\\b/c\u0000.txt"));
+ assertThrows(IllegalArgumentException.class, () -> FilenameUtils.normalize("\u0000a\\b/c.txt"));
}
@Test
diff --git a/src/test/java/org/apache/commons/io/HexDumpTest.java b/src/test/java/org/apache/commons/io/HexDumpTest.java
index 36a042ab..bb8c072e 100644
--- a/src/test/java/org/apache/commons/io/HexDumpTest.java
+++ b/src/test/java/org/apache/commons/io/HexDumpTest.java
@@ -17,8 +17,7 @@
package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
-
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -61,7 +60,7 @@ public class HexDumpTest {
outputArray[offset++] = (byte) ' ';
}
for (int k = 0; k < 16; k++) {
- outputArray[offset++] = (byte) toAscii((j * 16) + k);
+ outputArray[offset++] = (byte) toAscii(j * 16 + k);
}
System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, HexDump.EOL.getBytes().length);
}
@@ -94,7 +93,7 @@ public class HexDumpTest {
outputArray[offset++] = (byte) ' ';
}
for (int k = 0; k < 16; k++) {
- outputArray[offset++] = (byte) toAscii((j * 16) + k);
+ outputArray[offset++] = (byte) toAscii(j * 16 + k);
}
System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset,
HexDump.EOL.getBytes().length);
@@ -127,7 +126,7 @@ public class HexDumpTest {
outputArray[offset++] = (byte) ' ';
}
for (int k = 0; k < 16; k++) {
- outputArray[offset++] = (byte) toAscii((j * 16) + k);
+ outputArray[offset++] = (byte) toAscii(j * 16 + k);
}
System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset,
HexDump.EOL.getBytes().length);
@@ -141,7 +140,7 @@ public class HexDumpTest {
// verify proper behavior with non-zero index
stream = new ByteArrayOutputStream();
HexDump.dump(testArray, 0x10000000, stream, 0x81);
- outputArray = new byte[(8 * (73 + HexDump.EOL.length())) - 1];
+ outputArray = new byte[8 * (73 + HexDump.EOL.length()) - 1];
for (int j = 0; j < 8; j++) {
int offset = (73 + HexDump.EOL.length()) * j;
@@ -155,7 +154,7 @@ public class HexDumpTest {
outputArray[offset++] = (byte) '1';
outputArray[offset++] = (byte) ' ';
for (int k = 0; k < 16; k++) {
- final int index = 0x81 + (j * 16) + k;
+ final int index = 0x81 + j * 16 + k;
if (index < 0x100) {
outputArray[offset++] = (byte) toHex(index / 16);
@@ -167,7 +166,7 @@ public class HexDumpTest {
outputArray[offset++] = (byte) ' ';
}
for (int k = 0; k < 16; k++) {
- final int index = 0x81 + (j * 16) + k;
+ final int index = 0x81 + j * 16 + k;
if (index < 0x100) {
outputArray[offset++] = (byte) toAscii(index);
@@ -183,39 +182,19 @@ public class HexDumpTest {
}
// verify proper behavior with negative index
- try {
- HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(),
- -1);
- fail("should have caught ArrayIndexOutOfBoundsException on negative index");
- } catch (final ArrayIndexOutOfBoundsException ignored_exception) {
-
- // as expected
- }
+ assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(), -1));
// verify proper behavior with index that is too large
- try {
- HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(),
- testArray.length);
- fail("should have caught ArrayIndexOutOfBoundsException on large index");
- } catch (final ArrayIndexOutOfBoundsException ignored_exception) {
-
- // as expected
- }
+ assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(), testArray.length));
// verify proper behavior with null stream
- try {
- HexDump.dump(testArray, 0x10000000, null, 0);
- fail("should have caught IllegalArgumentException on negative index");
- } catch (final IllegalArgumentException ignored_exception) {
-
- // as expected
- }
+ assertThrows(IllegalArgumentException.class, () -> HexDump.dump(testArray, 0x10000000, null, 0));
}
private char toAscii(final int c) {
char rval = '.';
- if ((c >= 32) && (c <= 126)) {
+ if (c >= 32 && c <= 126) {
rval = (char) c;
}
return rval;
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsContentEqualsTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsContentEqualsTest.java
index 71203b6b..23347d67 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsContentEqualsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsContentEqualsTest.java
@@ -18,8 +18,8 @@
package org.apache.commons.io.file;
import static org.junit.jupiter.api.Assertions.assertFalse;
+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.File;
import java.io.IOException;
@@ -166,12 +166,7 @@ public class PathUtilsContentEqualsTest {
assertTrue(PathUtils.fileContentEquals(path2, path1));
// Directories
- try {
- PathUtils.fileContentEquals(temporaryFolder.toPath(), temporaryFolder.toPath());
- fail("Comparing directories should fail with an IOException");
- } catch (final IOException ioe) {
- // expected
- }
+ assertThrows(IOException.class, () -> PathUtils.fileContentEquals(temporaryFolder.toPath(), temporaryFolder.toPath()));
// Different files
final Path objFile1 = Paths.get(temporaryFolder.getAbsolutePath(), getName() + ".object");
diff --git a/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java b/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
index 3529025f..7841c4f6 100644
--- a/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
@@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
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 static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@@ -461,8 +460,7 @@ public class BOMInputStreamTest {
@Test
public void testReadWithBOMUtf16Be() throws Exception {
final byte[] data = "ABC".getBytes(StandardCharsets.UTF_16BE);
- try (final BOMInputStream in = new BOMInputStream(createUtf16BeDataStream(data, true),
- ByteOrderMark.UTF_16BE)) {
+ try (final BOMInputStream in = new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE)) {
assertEquals(0, in.read());
assertEquals('A', in.read());
assertEquals(0, in.read());
@@ -473,20 +471,14 @@ public class BOMInputStreamTest {
assertTrue(in.hasBOM(), "hasBOM()");
assertTrue(in.hasBOM(ByteOrderMark.UTF_16BE), "hasBOM(UTF-16BE)");
assertEquals(ByteOrderMark.UTF_16BE, in.getBOM(), "getBOM");
- try {
- in.hasBOM(ByteOrderMark.UTF_16LE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected - not configured for UTF-16LE
- }
+ assertThrows(IllegalArgumentException.class, () -> in.hasBOM(ByteOrderMark.UTF_16LE));
}
}
@Test
public void testReadWithBOMUtf16Le() throws Exception {
final byte[] data = "ABC".getBytes(StandardCharsets.UTF_16LE);
- try (final BOMInputStream in = new BOMInputStream(createUtf16LeDataStream(data, true),
- ByteOrderMark.UTF_16LE)) {
+ try (final BOMInputStream in = new BOMInputStream(createUtf16LeDataStream(data, true), ByteOrderMark.UTF_16LE)) {
assertEquals('A', in.read());
assertEquals(0, in.read());
assertEquals('B', in.read());
@@ -497,12 +489,7 @@ public class BOMInputStreamTest {
assertTrue(in.hasBOM(), "hasBOM()");
assertTrue(in.hasBOM(ByteOrderMark.UTF_16LE), "hasBOM(UTF-16LE)");
assertEquals(ByteOrderMark.UTF_16LE, in.getBOM(), "getBOM");
- try {
- in.hasBOM(ByteOrderMark.UTF_16BE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected - not configured for UTF-16BE
- }
+ assertThrows(IllegalArgumentException.class, () -> in.hasBOM(ByteOrderMark.UTF_16BE));
}
}
@@ -528,12 +515,7 @@ public class BOMInputStreamTest {
assertTrue(in.hasBOM(), "hasBOM()");
assertTrue(in.hasBOM(ByteOrderMark.UTF_32BE), "hasBOM(UTF-32BE)");
assertEquals(ByteOrderMark.UTF_32BE, in.getBOM(), "getBOM");
- try {
- in.hasBOM(ByteOrderMark.UTF_32LE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected - not configured for UTF-32LE
- }
+ assertThrows(IllegalArgumentException.class, () -> in.hasBOM(ByteOrderMark.UTF_32LE));
}
}
@@ -559,12 +541,7 @@ public class BOMInputStreamTest {
assertTrue(in.hasBOM(), "hasBOM()");
assertTrue(in.hasBOM(ByteOrderMark.UTF_32LE), "hasBOM(UTF-32LE)");
assertEquals(ByteOrderMark.UTF_32LE, in.getBOM(), "getBOM");
- try {
- in.hasBOM(ByteOrderMark.UTF_32BE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected - not configured for UTF-32BE
- }
+ assertThrows(IllegalArgumentException.class, () -> in.hasBOM(ByteOrderMark.UTF_32BE));
}
}
@@ -579,12 +556,7 @@ public class BOMInputStreamTest {
assertTrue(in.hasBOM(), "hasBOM()");
assertTrue(in.hasBOM(ByteOrderMark.UTF_8), "hasBOM(UTF-8)");
assertEquals(ByteOrderMark.UTF_8, in.getBOM(), "getBOM");
- try {
- in.hasBOM(ByteOrderMark.UTF_16BE);
- fail("Expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected - not configured for UTF-16BE
- }
+ assertThrows(IllegalArgumentException.class, () -> in.hasBOM(ByteOrderMark.UTF_16BE));
}
}
diff --git a/src/test/java/org/apache/commons/io/input/ClassLoaderObjectInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ClassLoaderObjectInputStreamTest.java
index 69e0f771..e38ad26c 100644
--- a/src/test/java/org/apache/commons/io/input/ClassLoaderObjectInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ClassLoaderObjectInputStreamTest.java
@@ -66,8 +66,8 @@ public class ClassLoaderObjectInputStreamTest {
public boolean equals(final Object other) {
if (other instanceof Test) {
final Test tother = (Test) other;
- return (this.i == tother.i)
- & (this.e == tother.e)
+ return this.i == tother.i
+ & this.e == tother.e
& equalObject(tother.o);
}
return false;
diff --git a/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
index 08871469..3eb726a5 100644
--- a/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
@@ -17,7 +17,7 @@
package org.apache.commons.io.input;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -118,18 +118,8 @@ public class CountingInputStreamTest {
// Test integer methods
IOUtils.consume(cis);
- try {
- cis.getCount();
- fail("Expected getCount() to throw an ArithmeticException");
- } catch (final ArithmeticException ae) {
- // expected result
- }
- try {
- cis.resetCount();
- fail("Expected resetCount() to throw an ArithmeticException");
- } catch (final ArithmeticException ae) {
- // expected result
- }
+ assertThrows(ArithmeticException.class, () -> cis.getCount());
+ assertThrows(ArithmeticException.class, () -> cis.resetCount());
mock.close();
diff --git a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
index cbe390d3..137b2335 100644
--- a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
@@ -18,6 +18,7 @@ package org.apache.commons.io.input;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -59,16 +60,11 @@ public class NullInputStreamTest {
@Test
public void testEOFException() throws Exception {
- final InputStream input = new TestNullInputStream(2, false, true);
- assertEquals(0, input.read(), "Read 1");
- assertEquals(1, input.read(), "Read 2");
- try {
- final int result = input.read();
- fail("Should have thrown an EOFException, byte=[" + result + "]");
- } catch (final EOFException e) {
- // expected
+ try (final InputStream input = new TestNullInputStream(2, false, true)) {
+ assertEquals(0, input.read(), "Read 1");
+ assertEquals(1, input.read(), "Read 2");
+ assertThrows(EOFException.class, () -> input.read());
}
- input.close();
}
@Test
@@ -214,9 +210,6 @@ public class NullInputStreamTest {
}
}
-
- // ------------- Test NullInputStream implementation -------------
-
@Test
public void testSkip() throws Exception {
final InputStream input = new TestNullInputStream(10, true, false);
diff --git a/src/test/java/org/apache/commons/io/input/NullReaderTest.java b/src/test/java/org/apache/commons/io/input/NullReaderTest.java
index 42b66f86..fa1ea083 100644
--- a/src/test/java/org/apache/commons/io/input/NullReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/NullReaderTest.java
@@ -18,6 +18,7 @@ package org.apache.commons.io.input;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -59,16 +60,11 @@ public class NullReaderTest {
@Test
public void testEOFException() throws Exception {
- final Reader reader = new TestNullReader(2, false, true);
- assertEquals(0, reader.read(), "Read 1");
- assertEquals(1, reader.read(), "Read 2");
- try {
- final int result = reader.read();
- fail("Should have thrown an EOFException, value=[" + result + "]");
- } catch (final EOFException e) {
- // expected
+ try (final Reader reader = new TestNullReader(2, false, true)) {
+ assertEquals(0, reader.read(), "Read 1");
+ assertEquals(1, reader.read(), "Read 2");
+ assertThrows(EOFException.class, () -> reader.read());
}
- reader.close();
}
@Test
@@ -211,9 +207,6 @@ public class NullReaderTest {
}
}
-
- // ------------- Test NullReader implementation -------------
-
@Test
public void testSkip() throws Exception {
final Reader reader = new TestNullReader(10, true, false);
diff --git a/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTest.java b/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTest.java
index 147d3855..8972595c 100644
--- a/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTest.java
+++ b/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTest.java
@@ -18,6 +18,7 @@ package org.apache.commons.io.monitor;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -137,12 +138,8 @@ public class FileAlterationMonitorTest extends AbstractMonitorTest {
assertEquals(interval, monitor.getInterval(), "Interval");
monitor.start();
- try {
- monitor.start(); // try and start again
- fail("Expected IllegalStateException");
- } catch (final IllegalStateException e) {
- // expected result, monitor already running
- }
+ // try and start again
+ assertThrows(IllegalStateException.class, () -> monitor.start());
// Create a File
checkCollectionsEmpty("A");
@@ -165,12 +162,8 @@ public class FileAlterationMonitorTest extends AbstractMonitorTest {
// Stop monitoring
monitor.stop();
- try {
- monitor.stop(); // try and stop again
- fail("Expected IllegalStateException");
- } catch (final IllegalStateException e) {
- // expected result, monitor already stopped
- }
+ // try and stop again
+ assertThrows(IllegalStateException.class, () -> monitor.stop());
}
/**
diff --git a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java
index 5d550cb8..3195db49 100644
--- a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java
@@ -16,7 +16,7 @@
*/
package org.apache.commons.io.output;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
@@ -31,12 +31,9 @@ public class ClosedOutputStreamTest {
* Test the {@code flush()} method.
*/
@Test
- public void testFlush() {
+ public void testFlush() throws IOException {
try (ClosedOutputStream cos = new ClosedOutputStream()) {
- cos.flush();
- fail("flush()");
- } catch (final IOException e) {
- // expected
+ assertThrows(IOException.class, () -> cos.flush());
}
}
@@ -44,12 +41,9 @@ public class ClosedOutputStreamTest {
* Test the {@code write(b)} method.
*/
@Test
- public void testWrite() {
+ public void testWrite() throws IOException {
try (ClosedOutputStream cos = new ClosedOutputStream()) {
- cos.write('x');
- fail("write(b)");
- } catch (final IOException e) {
- // expected
+ assertThrows(IOException.class, () -> cos.write('x'));
}
}
diff --git a/src/test/java/org/apache/commons/io/output/ClosedWriterTest.java b/src/test/java/org/apache/commons/io/output/ClosedWriterTest.java
index 4b471926..555f2f6a 100644
--- a/src/test/java/org/apache/commons/io/output/ClosedWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/ClosedWriterTest.java
@@ -16,7 +16,7 @@
*/
package org.apache.commons.io.output;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
@@ -31,12 +31,9 @@ public class ClosedWriterTest {
* Test the {@code flush()} method.
*/
@Test
- public void testFlush() {
+ public void testFlush() throws IOException {
try (ClosedWriter cw = new ClosedWriter()) {
- cw.flush();
- fail("flush()");
- } catch (final IOException e) {
- // expected
+ assertThrows(IOException.class, () -> cw.flush());
}
}
@@ -44,12 +41,9 @@ public class ClosedWriterTest {
* Test the {@code write(cbuf, off, len)} method.
*/
@Test
- public void testWrite() {
+ public void testWrite() throws IOException {
try (ClosedWriter cw = new ClosedWriter()) {
- cw.write(new char[0], 0, 0);
- fail("write(cbuf, off, len)");
- } catch (final IOException e) {
- // expected
+ assertThrows(IOException.class, () -> cw.write(new char[0], 0, 0));
}
}
diff --git a/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
index d9f50e0a..01b8a641 100644
--- a/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
@@ -17,7 +17,7 @@
package org.apache.commons.io.output;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -86,18 +86,8 @@ public class CountingOutputStreamTest {
// Test integer methods
IOUtils.copyLarge(mock, cos);
- try {
- cos.getCount();
- fail("Expected getCount() to throw an ArithmeticException");
- } catch (final ArithmeticException ae) {
- // expected result
- }
- try {
- cos.resetCount();
- fail("Expected resetCount() to throw an ArithmeticException");
- } catch (final ArithmeticException ae) {
- // expected result
- }
+ assertThrows(ArithmeticException.class, () -> cos.getCount());
+ assertThrows(ArithmeticException.class, () -> cos.resetCount());
mock.close();
diff --git a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
index eac71651..30a8ac30 100644
--- a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -65,20 +66,15 @@ public class DeferredFileOutputStreamTest {
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testAboveThreshold(final int initialBufferSize) {
+ public void testAboveThreshold(final int initialBufferSize) throws IOException {
final File testFile = new File("testAboveThreshold.dat");
// Ensure that the test starts from a clean base.
testFile.delete();
- final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize,
- testFile);
- try {
- dfos.write(testBytes, 0, testBytes.length);
- dfos.close();
- } catch (final IOException e) {
- fail("Unexpected IOException");
- }
+ final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, testFile);
+ dfos.write(testBytes, 0, testBytes.length);
+ dfos.close();
assertFalse(dfos.isInMemory());
assertNull(dfos.getData());
@@ -117,14 +113,10 @@ public class DeferredFileOutputStreamTest {
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testAtThreshold(final int initialBufferSize) {
+ public void testAtThreshold(final int initialBufferSize) throws IOException {
final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length, initialBufferSize, null);
- try {
- dfos.write(testBytes, 0, testBytes.length);
- dfos.close();
- } catch (final IOException e) {
- fail("Unexpected IOException");
- }
+ dfos.write(testBytes, 0, testBytes.length);
+ dfos.close();
assertTrue(dfos.isInMemory());
final byte[] resultBytes = dfos.getData();
@@ -134,18 +126,14 @@ public class DeferredFileOutputStreamTest {
/**
* Tests the case where the amount of data falls below the threshold, and is therefore confined to memory.
+ * @throws IOException
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testBelowThreshold(final int initialBufferSize) {
- final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize,
- null);
- try {
- dfos.write(testBytes, 0, testBytes.length);
- dfos.close();
- } catch (final IOException e) {
- fail("Unexpected IOException");
- }
+ public void testBelowThreshold(final int initialBufferSize) throws IOException {
+ final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, null);
+ dfos.write(testBytes, 0, testBytes.length);
+ dfos.close();
assertTrue(dfos.isInMemory());
final byte[] resultBytes = dfos.getData();
@@ -176,20 +164,15 @@ public class DeferredFileOutputStreamTest {
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testTempFileAboveThreshold(final int initialBufferSize) {
+ public void testTempFileAboveThreshold(final int initialBufferSize) throws IOException {
final String prefix = "commons-io-test";
final String suffix = ".out";
final File tempDir = FileUtils.current();
- final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize,
- prefix, suffix, tempDir);
+ final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir);
assertNull(dfos.getFile(), "Check file is null-A");
- try {
- dfos.write(testBytes, 0, testBytes.length);
- dfos.close();
- } catch (final IOException e) {
- fail("Unexpected IOException");
- }
+ dfos.write(testBytes, 0, testBytes.length);
+ dfos.close();
assertFalse(dfos.isInMemory());
assertNull(dfos.getData());
assertNotNull(dfos.getFile(), "Check file not null");
@@ -206,23 +189,19 @@ public class DeferredFileOutputStreamTest {
/**
* Test specifying a temporary file and the threshold is reached.
+ * @throws IOException
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testTempFileAboveThresholdPrefixOnly(final int initialBufferSize) {
+ public void testTempFileAboveThresholdPrefixOnly(final int initialBufferSize) throws IOException {
final String prefix = "commons-io-test";
final String suffix = null;
final File tempDir = null;
- final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize,
- prefix, suffix, tempDir);
+ final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir);
assertNull(dfos.getFile(), "Check file is null-A");
- try {
- dfos.write(testBytes, 0, testBytes.length);
- dfos.close();
- } catch (final IOException e) {
- fail("Unexpected IOException");
- }
+ dfos.write(testBytes, 0, testBytes.length);
+ dfos.close();
assertFalse(dfos.isInMemory());
assertNull(dfos.getData());
assertNotNull(dfos.getFile(), "Check file not null");
@@ -238,23 +217,19 @@ public class DeferredFileOutputStreamTest {
/**
* Test specifying a temporary file and the threshold not reached.
+ * @throws IOException
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testTempFileBelowThreshold(final int initialBufferSize) {
+ public void testTempFileBelowThreshold(final int initialBufferSize) throws IOException {
final String prefix = "commons-io-test";
final String suffix = ".out";
final File tempDir = FileUtils.current();
- final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize,
- prefix, suffix, tempDir);
+ final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, prefix, suffix, tempDir);
assertNull(dfos.getFile(), "Check file is null-A");
- try {
- dfos.write(testBytes, 0, testBytes.length);
- dfos.close();
- } catch (final IOException e) {
- fail("Unexpected IOException");
- }
+ dfos.write(testBytes, 0, testBytes.length);
+ dfos.close();
assertTrue(dfos.isInMemory());
assertNull(dfos.getFile(), "Check file is null-B");
}
@@ -266,42 +241,31 @@ public class DeferredFileOutputStreamTest {
*/
@Test
public void testTempFileError() throws Exception {
-
final String prefix = null;
final String suffix = ".out";
final File tempDir = FileUtils.current();
- try {
- new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir).close();
- fail("Expected IllegalArgumentException ");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir).close());
}
/**
* Tests the case where there are multiple writes beyond the threshold, to ensure that the
* {@code thresholdReached()} method is only called once, as the threshold is crossed for the first time.
+ * @throws IOException
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testThresholdReached(final int initialBufferSize) {
+ public void testThresholdReached(final int initialBufferSize) throws IOException {
final File testFile = new File("testThresholdReached.dat");
// Ensure that the test starts from a clean base.
testFile.delete();
- final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length / 2, initialBufferSize,
- testFile);
+ final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length / 2, initialBufferSize, testFile);
final int chunkSize = testBytes.length / 3;
-
- try {
- dfos.write(testBytes, 0, chunkSize);
- dfos.write(testBytes, chunkSize, chunkSize);
- dfos.write(testBytes, chunkSize * 2, testBytes.length - chunkSize * 2);
- dfos.close();
- } catch (final IOException e) {
- fail("Unexpected IOException");
- }
+ dfos.write(testBytes, 0, chunkSize);
+ dfos.write(testBytes, chunkSize, chunkSize);
+ dfos.write(testBytes, chunkSize * 2, testBytes.length - chunkSize * 2);
+ dfos.close();
assertFalse(dfos.isInMemory());
assertNull(dfos.getData());
@@ -316,31 +280,22 @@ public class DeferredFileOutputStreamTest {
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testWriteToLarge(final int initialBufferSize) {
+ public void testWriteToLarge(final int initialBufferSize) throws IOException {
final File testFile = new File("testWriteToFile.dat");
final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
// Ensure that the test starts from a clean base.
testFile.delete();
final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length / 2, testFile);
- try {
- dfos.write(testBytes);
+ dfos.write(testBytes);
- assertTrue(testFile.exists());
- assertFalse(dfos.isInMemory());
+ assertTrue(testFile.exists());
+ assertFalse(dfos.isInMemory());
- try {
- dfos.writeTo(baos);
- fail("Should not have been able to write before closing");
- } catch (final IOException ioe) {
- // ok, as expected
- }
+ assertThrows(IOException.class, () -> dfos.writeTo(baos));
- dfos.close();
- dfos.writeTo(baos);
- } catch (final IOException ioe) {
- fail("Unexpected IOException");
- }
+ dfos.close();
+ dfos.writeTo(baos);
final byte[] copiedBytes = baos.toByteArray();
assertArrayEquals(testBytes, copiedBytes);
verifyResultFile(testFile);
@@ -349,35 +304,26 @@ public class DeferredFileOutputStreamTest {
/**
* Test whether writeTo() properly writes small content.
+ * @throws IOException
*/
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
- public void testWriteToSmall(final int initialBufferSize) {
+ public void testWriteToSmall(final int initialBufferSize) throws IOException {
final File testFile = new File("testWriteToMem.dat");
final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
// Ensure that the test starts from a clean base.
testFile.delete();
- final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length * 2, initialBufferSize,
- testFile);
- try {
- dfos.write(testBytes);
+ final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length * 2, initialBufferSize, testFile);
+ dfos.write(testBytes);
- assertFalse(testFile.exists());
- assertTrue(dfos.isInMemory());
+ assertFalse(testFile.exists());
+ assertTrue(dfos.isInMemory());
- try {
- dfos.writeTo(baos);
- fail("Should not have been able to write before closing");
- } catch (final IOException ioe) {
- // ok, as expected
- }
+ assertThrows(IOException.class, () -> dfos.writeTo(baos));
- dfos.close();
- dfos.writeTo(baos);
- } catch (final IOException ioe) {
- fail("Unexpected IOException");
- }
+ dfos.close();
+ dfos.writeTo(baos);
final byte[] copiedBytes = baos.toByteArray();
assertArrayEquals(testBytes, copiedBytes);
diff --git a/src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java b/src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java
index 880dd3e9..f87a1a5b 100644
--- a/src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/ProxyCollectionWriterTest.java
@@ -409,11 +409,10 @@ public class ProxyCollectionWriterTest {
@Test
public void testTee() throws IOException {
- final StringBuilderWriter sbw1 = new StringBuilderWriter();
- final StringBuilderWriter sbw2 = new StringBuilderWriter();
- final StringBuilderWriter expected = new StringBuilderWriter();
-
- try (final ProxyCollectionWriter tw = new ProxyCollectionWriter(sbw1, sbw2, null)) {
+ try (StringBuilderWriter sbw1 = new StringBuilderWriter();
+ StringBuilderWriter sbw2 = new StringBuilderWriter();
+ StringBuilderWriter expected = new StringBuilderWriter();
+ ProxyCollectionWriter tw = new ProxyCollectionWriter(sbw1, sbw2, null)) {
for (int i = 0; i < 20; i++) {
tw.write(i);
expected.write(i);
diff --git a/src/test/java/org/apache/commons/io/output/StringBuilderWriterTest.java b/src/test/java/org/apache/commons/io/output/StringBuilderWriterTest.java
index edd3f91b..583629d7 100644
--- a/src/test/java/org/apache/commons/io/output/StringBuilderWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/StringBuilderWriterTest.java
@@ -18,8 +18,6 @@ package org.apache.commons.io.output;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.fail;
-
import java.io.IOException;
import java.io.Writer;
@@ -86,13 +84,9 @@ public class StringBuilderWriterTest {
@Test
public void testClose() throws IOException {
try (final Writer writer = new StringBuilderWriter()) {
- try {
- writer.append("Foo");
- writer.close();
- writer.append("Bar");
- } catch (final Throwable t) {
- fail("Threw: " + t);
- }
+ writer.append("Foo");
+ writer.close();
+ writer.append("Bar");
assertEquals("FooBar", writer.toString());
}
}
diff --git a/src/test/java/org/apache/commons/io/output/TaggedOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/TaggedOutputStreamTest.java
index 342b7dee..0c24d99f 100644
--- a/src/test/java/org/apache/commons/io/output/TaggedOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/TaggedOutputStreamTest.java
@@ -36,8 +36,7 @@ public class TaggedOutputStreamTest {
@Test
public void testBrokenStream() {
final IOException exception = new IOException("test exception");
- final TaggedOutputStream stream =
- new TaggedOutputStream(new BrokenOutputStream(exception));
+ final TaggedOutputStream stream = new TaggedOutputStream(new BrokenOutputStream(exception));
// Test the write() method
try {
diff --git a/src/test/java/org/apache/commons/io/output/TaggedWriterTest.java b/src/test/java/org/apache/commons/io/output/TaggedWriterTest.java
index 98f37b68..6f86bbfe 100644
--- a/src/test/java/org/apache/commons/io/output/TaggedWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/TaggedWriterTest.java
@@ -36,8 +36,7 @@ public class TaggedWriterTest {
@Test
public void testBrokenWriter() {
final IOException exception = new IOException("test exception");
- final TaggedWriter writer =
- new TaggedWriter(new BrokenWriter(exception));
+ final TaggedWriter writer = new TaggedWriter(new BrokenWriter(exception));
// Test the write() method
try {
@@ -83,7 +82,7 @@ public class TaggedWriterTest {
}
@Test
- public void testNormalWriter() {
+ public void testNormalWriter() throws IOException {
try (final StringBuilderWriter buffer = new StringBuilderWriter()) {
try (final Writer writer = new TaggedWriter(buffer)) {
writer.write('a');
@@ -95,8 +94,6 @@ public class TaggedWriterTest {
assertEquals('a', buffer.getBuilder().charAt(0));
assertEquals('b', buffer.getBuilder().charAt(1));
assertEquals('c', buffer.getBuilder().charAt(2));
- } catch (final IOException e) {
- fail("Unexpected exception thrown");
}
}
@@ -104,21 +101,10 @@ public class TaggedWriterTest {
public void testOtherException() throws Exception {
final IOException exception = new IOException("test exception");
try (final TaggedWriter writer = new TaggedWriter(ClosedWriter.INSTANCE)) {
-
assertFalse(writer.isCauseOf(exception));
assertFalse(writer.isCauseOf(new TaggedIOException(exception, UUID.randomUUID())));
-
- try {
- writer.throwIfCauseOf(exception);
- } catch (final IOException e) {
- fail("Unexpected exception thrown");
- }
-
- try {
- writer.throwIfCauseOf(new TaggedIOException(exception, UUID.randomUUID()));
- } catch (final IOException e) {
- fail("Unexpected exception thrown");
- }
+ writer.throwIfCauseOf(exception);
+ writer.throwIfCauseOf(new TaggedIOException(exception, UUID.randomUUID()));
}
}