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
The following commit(s) were added to refs/heads/master by this push:
new d36381fb Use assertThrows (#475)
d36381fb is described below
commit d36381fbba8c3f106dab55f9a777c21632a1f2dc
Author: Jakub Kupczyk <[email protected]>
AuthorDate: Sat Sep 9 19:59:03 2023 +0200
Use assertThrows (#475)
---
.../commons/io/FileUtilsCleanDirectoryTest.java | 6 +-
.../io/FileUtilsDeleteDirectoryLinuxTest.java | 13 +-
.../org/apache/commons/io/FilenameUtilsTest.java | 16 +-
.../commons/io/input/NullInputStreamTest.java | 58 +---
.../apache/commons/io/input/NullReaderTest.java | 25 +-
.../commons/io/input/TaggedInputStreamTest.java | 50 +---
.../apache/commons/io/input/TaggedReaderTest.java | 50 +---
.../commons/io/input/TeeInputStreamTest.java | 26 +-
.../org/apache/commons/io/input/TeeReaderTest.java | 29 +-
.../commons/io/input/XmlStreamReaderTest.java | 9 +-
.../commons/io/output/TeeOutputStreamTest.java | 20 +-
.../apache/commons/io/output/TeeWriterTest.java | 303 ++++++++-------------
.../commons/io/output/UncheckedAppendableTest.java | 26 +-
13 files changed, 208 insertions(+), 423 deletions(-)
diff --git
a/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java
b/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java
index 138ebdca..2e28f364 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java
@@ -17,8 +17,8 @@
package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
+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.assumeTrue;
import java.io.File;
@@ -123,9 +123,7 @@ public class FileUtilsCleanDirectoryTest extends
AbstractTempDirTest {
try {
// cleanDirectory calls forceDelete
- FileUtils.cleanDirectory(tempDirFile);
- fail("expected IOException");
- } catch (final IOException e) {
+ final IOException e = assertThrows(IOException.class, () ->
FileUtils.cleanDirectory(tempDirFile));
assertEquals("Unknown I/O error listing contents of directory: " +
tempDirFile.getAbsolutePath(), e.getMessage());
} finally {
chmod(tempDirFile, 755, false);
diff --git
a/src/test/java/org/apache/commons/io/FileUtilsDeleteDirectoryLinuxTest.java
b/src/test/java/org/apache/commons/io/FileUtilsDeleteDirectoryLinuxTest.java
index 36139017..60600ff5 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsDeleteDirectoryLinuxTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsDeleteDirectoryLinuxTest.java
@@ -17,8 +17,8 @@
package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
+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.assumeTrue;
import java.io.File;
@@ -87,11 +87,8 @@ public class FileUtilsDeleteDirectoryLinuxTest extends
AbstractFileUtilsDeleteDi
try {
// deleteDirectory calls forceDelete
- FileUtils.deleteDirectory(nested);
- fail("expected IOException");
- } catch (final IOException e) {
- final IOExceptionList list = (IOExceptionList) e;
- assertTrue(list.getCause(0).getMessage().endsWith("Cannot delete
file: " + file.getAbsolutePath()));
+ final IOExceptionList ioExceptionList = (IOExceptionList)
assertThrows(IOException.class, () -> FileUtils.deleteDirectory(nested));
+
assertTrue(ioExceptionList.getCause(0).getMessage().endsWith("Cannot delete
file: " + file.getAbsolutePath()));
} finally {
chmod(nested, 755, false);
FileUtils.deleteDirectory(nested);
@@ -110,9 +107,7 @@ public class FileUtilsDeleteDirectoryLinuxTest extends
AbstractFileUtilsDeleteDi
try {
// cleanDirectory calls forceDelete
- FileUtils.deleteDirectory(nested);
- fail("expected IOException");
- } catch (final IOException e) {
+ final IOException e = assertThrows(IOException.class, () ->
FileUtils.deleteDirectory(nested));
assertEquals("Unknown I/O error listing contents of directory: " +
nested.getAbsolutePath(), e.getMessage());
} finally {
chmod(nested, 755, false);
diff --git a/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
b/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
index 21891cc3..31b750ef 100644
--- a/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FilenameUtilsTest.java
@@ -249,12 +249,8 @@ public class FilenameUtilsTest {
if (FilenameUtils.isSystemWindows()) {
// Special case handling for NTFS ADS names
- try {
- FilenameUtils.getExtension("foo.exe:bar.txt");
- throw new AssertionError("Expected Exception");
- } catch (final IllegalArgumentException e) {
- assertEquals("NTFS ADS separator (':') in file name is
forbidden.", e.getMessage());
- }
+ final IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () ->
FilenameUtils.getExtension("foo.exe:bar.txt"));
+ assertEquals("NTFS ADS separator (':') in file name is
forbidden.", e.getMessage());
} else {
// Upwards compatibility:
assertEquals("txt", FilenameUtils.getExtension("foo.exe:bar.txt"));
@@ -585,12 +581,8 @@ public class FilenameUtilsTest {
if (FilenameUtils.isSystemWindows()) {
// Special case handling for NTFS ADS names
- try {
- FilenameUtils.indexOfExtension("foo.exe:bar.txt");
- throw new AssertionError("Expected Exception");
- } catch (final IllegalArgumentException e) {
- assertEquals("NTFS ADS separator (':') in file name is
forbidden.", e.getMessage());
- }
+ final IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () ->
FilenameUtils.indexOfExtension("foo.exe:bar.txt"));
+ assertEquals("NTFS ADS separator (':') in file name is
forbidden.", e.getMessage());
} else {
// Upwards compatibility on other systems
assertEquals(11,
FilenameUtils.indexOfExtension("foo.exe:bar.txt"));
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 77f3c074..219c5308 100644
--- a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java
@@ -20,7 +20,6 @@ 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.EOFException;
import java.io.IOException;
@@ -75,12 +74,8 @@ public class NullInputStreamTest {
assertTrue(input.markSupported(), "Mark Should be Supported");
// No Mark
- try {
- input.reset();
- fail("Read limit exceeded, expected IOException ");
- } catch (final IOException e) {
- assertEquals("No position has been marked", e.getMessage(),
"No Mark IOException message");
- }
+ final IOException noMarkException =
assertThrows(IOException.class, input::reset);
+ assertEquals("No position has been marked",
noMarkException.getMessage(), "No Mark IOException message");
for (; position < 3; position++) {
assertEquals(position, input.read(), "Read Before Mark [" +
position + "]");
@@ -103,13 +98,9 @@ public class NullInputStreamTest {
}
// Reset after read limit passed
- try {
- input.reset();
- fail("Read limit exceeded, expected IOException ");
- } catch (final IOException e) {
- assertEquals("Marked position [" + position + "] is no longer
valid - passed the read limit [" + readlimit + "]", e.getMessage(),
+ final IOException resetException = assertThrows(IOException.class,
input::reset, "Read limit exceeded, expected IOException");
+ assertEquals("Marked position [" + position + "] is no longer
valid - passed the read limit [" + readlimit + "]", resetException.getMessage(),
"Read limit IOException message");
- }
}
}
@@ -118,19 +109,11 @@ public class NullInputStreamTest {
final InputStream input = new TestNullInputStream(100, false, true);
assertFalse(input.markSupported(), "Mark Should NOT be Supported");
- try {
- input.mark(5);
- fail("mark() should throw UnsupportedOperationException");
- } catch (final UnsupportedOperationException e) {
- assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "mark()
error message");
- }
+ final UnsupportedOperationException markException =
assertThrows(UnsupportedOperationException.class, () -> input.mark(5));
+ assertEquals(MARK_RESET_NOT_SUPPORTED, markException.getMessage(),
"mark() error message");
- try {
- input.reset();
- fail("reset() should throw UnsupportedOperationException");
- } catch (final UnsupportedOperationException e) {
- assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "reset()
error message");
- }
+ final UnsupportedOperationException resetException =
assertThrows(UnsupportedOperationException.class, input::reset);
+ assertEquals(MARK_RESET_NOT_SUPPORTED, resetException.getMessage(),
"reset() error message");
input.close();
}
@@ -149,12 +132,8 @@ public class NullInputStreamTest {
assertEquals(0, input.available(), "Available after End of File");
// Test reading after the end of file
- try {
- final int result = input.read();
- fail("Should have thrown an IOException, byte=[" + result + "]");
- } catch (final IOException e) {
- assertEquals("Read after end of file", e.getMessage());
- }
+ final IOException e = assertThrows(IOException.class, input::read);
+ assertEquals("Read after end of file", e.getMessage());
// Close - should reset
input.close();
@@ -185,12 +164,8 @@ public class NullInputStreamTest {
assertEquals(-1, count3, "Read 3 (EOF)");
// Test reading after the end of file
- try {
- final int count4 = input.read(bytes);
- fail("Should have thrown an IOException, byte=[" + count4 + "]");
- } catch (final IOException e) {
- assertEquals("Read after end of file", e.getMessage());
- }
+ final IOException e = assertThrows(IOException.class, () ->
input.read(bytes));
+ assertEquals("Read after end of file", e.getMessage());
// reset by closing
input.close();
@@ -214,12 +189,9 @@ public class NullInputStreamTest {
assertEquals(7, input.read(), "Read 3");
assertEquals(2, input.skip(5), "Skip 2"); // only 2 left to skip
assertEquals(-1, input.skip(5), "Skip 3 (EOF)"); // End of file
- try {
- input.skip(5); //
- fail("Expected IOException for skipping after end of file");
- } catch (final IOException e) {
- assertEquals("Skip after end of file", e.getMessage(), "Skip after
EOF IOException message");
- }
+
+ final IOException e = assertThrows(IOException.class, () ->
input.skip(5), "Expected IOException for skipping after end of file");
+ assertEquals("Skip after end of file", e.getMessage(), "Skip after EOF
IOException message");
input.close();
}
}
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 d2d055fb..236a0f95 100644
--- a/src/test/java/org/apache/commons/io/input/NullReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/NullReaderTest.java
@@ -75,12 +75,8 @@ public class NullReaderTest {
assertTrue(reader.markSupported(), "Mark Should be Supported");
// No Mark
- try {
- reader.reset();
- fail("Read limit exceeded, expected IOException ");
- } catch (final IOException e) {
- assertEquals("No position has been marked", e.getMessage(),
"No Mark IOException message");
- }
+ final IOException resetException = assertThrows(IOException.class,
reader::reset);
+ assertEquals("No position has been marked",
resetException.getMessage(), "No Mark IOException message");
for (; position < 3; position++) {
assertEquals(position, reader.read(), "Read Before Mark [" +
position + "]");
@@ -103,13 +99,9 @@ public class NullReaderTest {
}
// Reset after read limit passed
- try {
- reader.reset();
- fail("Read limit exceeded, expected IOException ");
- } catch (final IOException e) {
- assertEquals("Marked position [" + position + "] is no longer
valid - passed the read limit [" + readlimit + "]", e.getMessage(),
+ final IOException e = assertThrows(IOException.class,
reader::reset);
+ assertEquals("Marked position [" + position + "] is no longer
valid - passed the read limit [" + readlimit + "]", e.getMessage(),
"Read limit IOException message");
- }
}
}
@@ -211,12 +203,9 @@ public class NullReaderTest {
assertEquals(7, reader.read(), "Read 3");
assertEquals(2, reader.skip(5), "Skip 2"); // only 2 left to skip
assertEquals(-1, reader.skip(5), "Skip 3 (EOF)"); // End of file
- try {
- reader.skip(5); //
- fail("Expected IOException for skipping after end of file");
- } catch (final IOException e) {
- assertEquals("Skip after end of file", e.getMessage(), "Skip
after EOF IOException message");
- }
+
+ final IOException e = assertThrows(IOException.class, () ->
reader.skip(5));
+ assertEquals("Skip after end of file", e.getMessage(), "Skip after
EOF IOException message");
}
}
}
diff --git
a/src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java
b/src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java
index 01fbbb94..a61c0fc8 100644
--- a/src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java
@@ -18,8 +18,8 @@ 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;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -41,46 +41,22 @@ public class TaggedInputStreamTest {
new TaggedInputStream(new BrokenInputStream(exception));
// Test the available() method
- try {
- stream.available();
- fail("Expected exception not thrown.");
- } catch (final IOException e) {
- assertTrue(stream.isCauseOf(e));
- try {
- stream.throwIfCauseOf(e);
- fail("Expected exception not thrown.");
- } catch (final IOException e2) {
- assertEquals(exception, e2);
- }
- }
+ final IOException exceptionAvailable = assertThrows(IOException.class,
stream::available);
+ assertTrue(stream.isCauseOf(exceptionAvailable));
+ final IOException exceptionAvailableCause =
assertThrows(IOException.class, () ->
stream.throwIfCauseOf(exceptionAvailable));
+ assertEquals(exception, exceptionAvailableCause);
// Test the read() method
- try {
- stream.read();
- fail("Expected exception not thrown.");
- } catch (final IOException e) {
- assertTrue(stream.isCauseOf(e));
- try {
- stream.throwIfCauseOf(e);
- fail("Expected exception not thrown.");
- } catch (final IOException e2) {
- assertEquals(exception, e2);
- }
- }
+ final IOException exceptionRead = assertThrows(IOException.class,
stream::read);
+ assertTrue(stream.isCauseOf(exceptionRead));
+ final IOException exceptionReadCause = assertThrows(IOException.class,
() -> stream.throwIfCauseOf(exceptionRead));
+ assertEquals(exception, exceptionReadCause);
// Test the close() method
- try {
- stream.close();
- fail("Expected exception not thrown.");
- } catch (final IOException e) {
- assertTrue(stream.isCauseOf(e));
- try {
- stream.throwIfCauseOf(e);
- fail("Expected exception not thrown.");
- } catch (final IOException e2) {
- assertEquals(exception, e2);
- }
- }
+ final IOException exceptionClose = assertThrows(IOException.class,
stream::close);
+ assertTrue(stream.isCauseOf(exceptionClose));
+ final IOException exceptionCloseCause =
assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionClose));
+ assertEquals(exception, exceptionCloseCause);
}
@Test
diff --git a/src/test/java/org/apache/commons/io/input/TaggedReaderTest.java
b/src/test/java/org/apache/commons/io/input/TaggedReaderTest.java
index 63d23bf8..e5bd83ca 100644
--- a/src/test/java/org/apache/commons/io/input/TaggedReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/TaggedReaderTest.java
@@ -18,8 +18,8 @@ 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;
import java.io.IOException;
import java.io.Reader;
@@ -40,46 +40,22 @@ public class TaggedReaderTest {
final TaggedReader reader = new TaggedReader(new
BrokenReader(exception));
// Test the ready() method
- try {
- reader.ready();
- fail("Expected exception not thrown.");
- } catch (final IOException e) {
- assertTrue(reader.isCauseOf(e));
- try {
- reader.throwIfCauseOf(e);
- fail("Expected exception not thrown.");
- } catch (final IOException e2) {
- assertEquals(exception, e2);
- }
- }
+ final IOException readyException = assertThrows(IOException.class,
reader::ready);
+ assertTrue(reader.isCauseOf(readyException));
+ final IOException rethrownReadyException =
assertThrows(IOException.class, () -> reader.throwIfCauseOf(readyException));
+ assertEquals(exception, rethrownReadyException);
// Test the read() method
- try {
- reader.read();
- fail("Expected exception not thrown.");
- } catch (final IOException e) {
- assertTrue(reader.isCauseOf(e));
- try {
- reader.throwIfCauseOf(e);
- fail("Expected exception not thrown.");
- } catch (final IOException e2) {
- assertEquals(exception, e2);
- }
- }
+ final IOException readException = assertThrows(IOException.class,
reader::read);
+ assertTrue(reader.isCauseOf(readException));
+ final IOException rethrownReadException =
assertThrows(IOException.class, () -> reader.throwIfCauseOf(readException));
+ assertEquals(exception, rethrownReadException);
// Test the close() method
- try {
- reader.close();
- fail("Expected exception not thrown.");
- } catch (final IOException e) {
- assertTrue(reader.isCauseOf(e));
- try {
- reader.throwIfCauseOf(e);
- fail("Expected exception not thrown.");
- } catch (final IOException e2) {
- assertEquals(exception, e2);
- }
- }
+ final IOException closeException = assertThrows(IOException.class,
reader::close);
+ assertTrue(reader.isCauseOf(closeException));
+ final IOException rethrownCloseException =
assertThrows(IOException.class, () -> reader.throwIfCauseOf(closeException));
+ assertEquals(exception, rethrownCloseException);
}
@Test
diff --git a/src/test/java/org/apache/commons/io/input/TeeInputStreamTest.java
b/src/test/java/org/apache/commons/io/input/TeeInputStreamTest.java
index e7b7391f..49527a92 100644
--- a/src/test/java/org/apache/commons/io/input/TeeInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/TeeInputStreamTest.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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -67,12 +67,8 @@ public class TeeInputStreamTest {
verify(goodIs).close();
final TeeInputStream closingTis = new TeeInputStream(goodIs, badOs,
true);
- try {
- closingTis.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOException e) {
- verify(goodIs, times(2)).close();
- }
+ assertThrows(IOException.class, closingTis::close);
+ verify(goodIs, times(2)).close();
}
/**
@@ -85,20 +81,12 @@ public class TeeInputStreamTest {
final ByteArrayOutputStream goodOs = mock(ByteArrayOutputStream.class);
final TeeInputStream nonClosingTis = new TeeInputStream(badIs, goodOs,
false);
- try {
- nonClosingTis.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOException e) {
- verify(goodOs, never()).close();
- }
+ assertThrows(IOException.class, nonClosingTis::close);
+ verify(goodOs, never()).close();
final TeeInputStream closingTis = new TeeInputStream(badIs, goodOs,
true);
- try {
- closingTis.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOException e) {
- verify(goodOs).close();
- }
+ assertThrows(IOException.class, closingTis::close);
+ verify(goodOs).close();
}
@Test
diff --git a/src/test/java/org/apache/commons/io/input/TeeReaderTest.java
b/src/test/java/org/apache/commons/io/input/TeeReaderTest.java
index 4b694fcd..c4945258 100644
--- a/src/test/java/org/apache/commons/io/input/TeeReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/TeeReaderTest.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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -66,12 +66,8 @@ public class TeeReaderTest {
verify(goodR).close();
final TeeReader closingTr = new TeeReader(goodR, badW, true);
- try {
- closingTr.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOException e) {
- verify(goodR, times(2)).close();
- }
+ final IOException e = assertThrows(IOException.class,
closingTr::close);
+ verify(goodR, times(2)).close();
}
/**
@@ -84,21 +80,12 @@ public class TeeReaderTest {
final StringWriter goodW = mock(StringWriter.class);
final TeeReader nonClosingTr = new TeeReader(badR, goodW, false);
- try {
- nonClosingTr.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOException e) {
- verify(goodW, never()).close();
- }
+ assertThrows(IOException.class, nonClosingTr::close);
+ verify(goodW, never()).close();
final TeeReader closingTr = new TeeReader(badR, goodW, true);
- try {
- closingTr.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOException e) {
- //Assert.assertTrue(goodW.closed);
- verify(goodW).close();
- }
+ assertThrows(IOException.class, closingTr::close);
+ verify(goodW).close();
}
@Test
@@ -179,4 +166,4 @@ public class TeeReaderTest {
assertEquals("ac", output.toString());
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
index 2cfc87f1..555cf7ca 100644
--- a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
@@ -530,12 +530,9 @@ public class XmlStreamReaderTest {
protected void testRawNoBomInvalid(final String encoding) throws Exception
{
try (final InputStream is = getXmlInputStream("no-bom", XML3,
encoding, encoding)) {
- try {
- new XmlStreamReader(is, false).close();
- fail("It should have failed");
- } catch (final IOException ex) {
- assertTrue(ex.getMessage().contains("Invalid encoding,"));
- }
+ final XmlStreamReader xmlStreamReader = new XmlStreamReader(is,
false);
+ final IOException ex = assertThrows(IOException.class,
xmlStreamReader::close);
+ assertTrue(ex.getMessage().contains("Invalid encoding,"));
}
}
diff --git
a/src/test/java/org/apache/commons/io/output/TeeOutputStreamTest.java
b/src/test/java/org/apache/commons/io/output/TeeOutputStreamTest.java
index 1cfa7b12..00b0a924 100644
--- a/src/test/java/org/apache/commons/io/output/TeeOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/TeeOutputStreamTest.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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -49,12 +49,9 @@ public class TeeOutputStreamTest {
final OutputStream badOs = new ThrowOnCloseOutputStream();
final ByteArrayOutputStream goodOs = mock(ByteArrayOutputStream.class);
final TeeOutputStream tos = new TeeOutputStream(badOs, goodOs);
- try {
- tos.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOException e) {
- verify(goodOs).close();
- }
+
+ assertThrows(IOException.class, tos::close);
+ verify(goodOs).close();
}
/**
@@ -66,12 +63,9 @@ public class TeeOutputStreamTest {
final OutputStream badOs = new ThrowOnCloseOutputStream();
final ByteArrayOutputStream goodOs = mock(ByteArrayOutputStream.class);
final TeeOutputStream tos = new TeeOutputStream(goodOs, badOs);
- try {
- tos.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOException e) {
- verify(goodOs).close();
- }
+
+ assertThrows(IOException.class, tos::close);
+ verify(goodOs).close();
}
@Test
diff --git a/src/test/java/org/apache/commons/io/output/TeeWriterTest.java
b/src/test/java/org/apache/commons/io/output/TeeWriterTest.java
index 0b5af708..8f0f8ee7 100644
--- a/src/test/java/org/apache/commons/io/output/TeeWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/TeeWriterTest.java
@@ -17,8 +17,8 @@
package org.apache.commons.io.output;
import static org.junit.jupiter.api.Assertions.assertEquals;
+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.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -39,99 +39,81 @@ import org.junit.jupiter.api.Test;
public class TeeWriterTest {
@Test
- public void testArrayIOExceptionOnAppendChar1() throws IOException {
+ public void testArrayIOExceptionOnAppendChar1() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final ProxyCollectionWriter tw = new ProxyCollectionWriter(badW,
goodW, null);
final char data = 'A';
- try {
- tw.append(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).append(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.append(data));
+ verify(goodW).append(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnAppendChar2() throws IOException {
+ public void testArrayIOExceptionOnAppendChar2() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final ProxyCollectionWriter tw = new ProxyCollectionWriter(goodW,
badW, null);
final char data = 'A';
- try {
- tw.append(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).append(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.append(data));
+ verify(goodW).append(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnAppendCharSequence1() throws IOException
{
+ public void testArrayIOExceptionOnAppendCharSequence1() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
final String data = "A";
- try {
- tw.append(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).append(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.append(data));
+ verify(goodW).append(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnAppendCharSequence2() throws IOException
{
+ public void testArrayIOExceptionOnAppendCharSequence2() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
final String data = "A";
- try {
- tw.append(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).append(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.append(data));
+ verify(goodW).append(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnAppendCharSequenceIntInt1() throws
IOException {
+ public void testArrayIOExceptionOnAppendCharSequenceIntInt1() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
final String data = "A";
- try {
- tw.append(data, 0, 0);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).append(data, 0, 0);
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.append(data, 0, 0));
+ verify(goodW).append(data, 0, 0);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnAppendCharSequenceIntInt2() throws
IOException {
+ public void testArrayIOExceptionOnAppendCharSequenceIntInt2() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
final String data = "A";
- try {
- tw.append(data, 0, 0);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).append(data, 0, 0);
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.append(data, 0, 0));
+ verify(goodW).append(data, 0, 0);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
@@ -139,14 +121,11 @@ public class TeeWriterTest {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
- try {
- tw.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).close();
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class,
tw::close);
+ verify(goodW).close();
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
@@ -154,44 +133,35 @@ public class TeeWriterTest {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
- try {
- tw.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).close();
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class,
tw::close);
+ verify(goodW).close();
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnFlush1() throws IOException {
+ public void testArrayIOExceptionOnFlush1() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
- try {
- tw.flush();
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).flush();
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class,
tw::flush);
+ verify(goodW).flush();
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnFlush2() throws IOException {
+ public void testArrayIOExceptionOnFlush2() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
- try {
- tw.flush();
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).flush();
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class,
tw::flush);
+ verify(goodW).flush();
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
@@ -200,14 +170,11 @@ public class TeeWriterTest {
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
final char[] data = { 'a' };
- try {
- tw.write(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data));
+ verify(goodW).write(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
@@ -216,144 +183,114 @@ public class TeeWriterTest {
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
final char[] data = { 'a' };
- try {
- tw.write(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data));
+ verify(goodW).write(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnWriteCharArrayIntInt1() throws
IOException {
+ public void testArrayIOExceptionOnWriteCharArrayIntInt1() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
final char[] data = { 'a' };
- try {
- tw.write(data, 0, 0);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data, 0, 0);
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data, 0, 0));
+ verify(goodW).write(data, 0, 0);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnWriteCharArrayIntInt2() throws
IOException {
+ public void testArrayIOExceptionOnWriteCharArrayIntInt2() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
final char[] data = { 'a' };
- try {
- tw.write(data, 0, 0);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data, 0, 0);
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data, 0, 0));
+ verify(goodW).write(data, 0, 0);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnWriteInt1() throws IOException {
+ public void testArrayIOExceptionOnWriteInt1() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
final int data = 32;
- try {
- tw.write(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data));
+ verify(goodW).write(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnWriteInt2() throws IOException {
+ public void testArrayIOExceptionOnWriteInt2() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
- try {
- tw.write(32);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(32);
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(32));
+ verify(goodW).write(32);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnWriteString1() throws IOException {
+ public void testArrayIOExceptionOnWriteString1() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
final String data = "A";
- try {
- tw.write(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data));
+ verify(goodW).write(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnWriteString2() throws IOException {
+ public void testArrayIOExceptionOnWriteString2() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
final String data = "A";
- try {
- tw.write(data);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data);
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data));
+ verify(goodW).write(data);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnWriteStringIntInt1() throws IOException {
+ public void testArrayIOExceptionOnWriteStringIntInt1() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(badW, goodW, null);
final String data = "A";
- try {
- tw.write(data, 0, 0);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data, 0, 0);
- assertEquals(1, e.getCauseList().size());
- assertEquals(0, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data, 0, 0));
+ verify(goodW).write(data, 0, 0);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(0, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
- public void testArrayIOExceptionOnWriteStringIntInt2() throws IOException {
+ public void testArrayIOExceptionOnWriteStringIntInt2() {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(goodW, badW, null);
final String data = "A";
- try {
- tw.write(data, 0, 0);
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).write(data, 0, 0);
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+ final IOExceptionList e = assertThrows(IOExceptionList.class, () ->
tw.write(data, 0, 0));
+ verify(goodW).write(data, 0, 0);
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
@@ -361,15 +298,11 @@ public class TeeWriterTest {
final Writer badW = BrokenWriter.INSTANCE;
final StringWriter goodW = mock(StringWriter.class);
final TeeWriter tw = new TeeWriter(Arrays.asList(goodW, badW, null));
- try {
- tw.close();
- fail("Expected " + IOException.class.getName());
- } catch (final IOExceptionList e) {
- verify(goodW).close();
- assertEquals(1, e.getCauseList().size());
- assertEquals(1, e.getCause(0,
IOIndexedException.class).getIndex());
- }
+ final IOExceptionList e = assertThrows(IOExceptionList.class,
tw::close);
+ verify(goodW).close();
+ assertEquals(1, e.getCauseList().size());
+ assertEquals(1, e.getCause(0, IOIndexedException.class).getIndex());
}
@Test
diff --git
a/src/test/java/org/apache/commons/io/output/UncheckedAppendableTest.java
b/src/test/java/org/apache/commons/io/output/UncheckedAppendableTest.java
index 75a64373..e22f85d5 100644
--- a/src/test/java/org/apache/commons/io/output/UncheckedAppendableTest.java
+++ b/src/test/java/org/apache/commons/io/output/UncheckedAppendableTest.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.IOException;
import java.io.StringWriter;
@@ -64,32 +64,20 @@ public class UncheckedAppendableTest {
@Test
public void testAppendCharSequenceIndexedThrows() {
- try {
- appendableBroken.append("a", 0, 1);
- fail("Expected exception not thrown.");
- } catch (final UncheckedIOException e) {
- assertEquals(exception, e.getCause());
- }
+ final UncheckedIOException e =
assertThrows(UncheckedIOException.class, () -> appendableBroken.append("a", 0,
1));
+ assertEquals(exception, e.getCause());
}
@Test
public void testAppendCharSequenceThrows() {
- try {
- appendableBroken.append("a");
- fail("Expected exception not thrown.");
- } catch (final UncheckedIOException e) {
- assertEquals(exception, e.getCause());
- }
+ final UncheckedIOException e =
assertThrows(UncheckedIOException.class, () -> appendableBroken.append("a"));
+ assertEquals(exception, e.getCause());
}
@Test
public void testAppendCharThrows() {
- try {
- appendableBroken.append('a');
- fail("Expected exception not thrown.");
- } catch (final UncheckedIOException e) {
- assertEquals(exception, e.getCause());
- }
+ final UncheckedIOException e2 =
assertThrows(UncheckedIOException.class, () -> appendableBroken.append('a'));
+ assertEquals(exception, e2.getCause());
}
@Test