Skip to content

Commit d36381f

Browse files
authored
Use assertThrows (#475)
1 parent 2d27d95 commit d36381f

13 files changed

Lines changed: 208 additions & 423 deletions

src/test/java/org/apache/commons/io/FileUtilsCleanDirectoryTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package org.apache.commons.io;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021
import static org.junit.jupiter.api.Assertions.assertTrue;
21-
import static org.junit.jupiter.api.Assertions.fail;
2222
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2323

2424
import java.io.File;
@@ -123,9 +123,7 @@ public void testThrowsOnNullList() throws Exception {
123123

124124
try {
125125
// cleanDirectory calls forceDelete
126-
FileUtils.cleanDirectory(tempDirFile);
127-
fail("expected IOException");
128-
} catch (final IOException e) {
126+
final IOException e = assertThrows(IOException.class, () -> FileUtils.cleanDirectory(tempDirFile));
129127
assertEquals("Unknown I/O error listing contents of directory: " + tempDirFile.getAbsolutePath(), e.getMessage());
130128
} finally {
131129
chmod(tempDirFile, 755, false);

src/test/java/org/apache/commons/io/FileUtilsDeleteDirectoryLinuxTest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package org.apache.commons.io;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021
import static org.junit.jupiter.api.Assertions.assertTrue;
21-
import static org.junit.jupiter.api.Assertions.fail;
2222
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2323

2424
import java.io.File;
@@ -87,11 +87,8 @@ public void testThrowsOnCannotDeleteFile() throws Exception {
8787

8888
try {
8989
// deleteDirectory calls forceDelete
90-
FileUtils.deleteDirectory(nested);
91-
fail("expected IOException");
92-
} catch (final IOException e) {
93-
final IOExceptionList list = (IOExceptionList) e;
94-
assertTrue(list.getCause(0).getMessage().endsWith("Cannot delete file: " + file.getAbsolutePath()));
90+
final IOExceptionList ioExceptionList = (IOExceptionList) assertThrows(IOException.class, () -> FileUtils.deleteDirectory(nested));
91+
assertTrue(ioExceptionList.getCause(0).getMessage().endsWith("Cannot delete file: " + file.getAbsolutePath()));
9592
} finally {
9693
chmod(nested, 755, false);
9794
FileUtils.deleteDirectory(nested);
@@ -110,9 +107,7 @@ public void testThrowsOnNullList() throws Exception {
110107

111108
try {
112109
// cleanDirectory calls forceDelete
113-
FileUtils.deleteDirectory(nested);
114-
fail("expected IOException");
115-
} catch (final IOException e) {
110+
final IOException e = assertThrows(IOException.class, () -> FileUtils.deleteDirectory(nested));
116111
assertEquals("Unknown I/O error listing contents of directory: " + nested.getAbsolutePath(), e.getMessage());
117112
} finally {
118113
chmod(nested, 755, false);

src/test/java/org/apache/commons/io/FilenameUtilsTest.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,8 @@ public void testGetExtension() {
249249

250250
if (FilenameUtils.isSystemWindows()) {
251251
// Special case handling for NTFS ADS names
252-
try {
253-
FilenameUtils.getExtension("foo.exe:bar.txt");
254-
throw new AssertionError("Expected Exception");
255-
} catch (final IllegalArgumentException e) {
256-
assertEquals("NTFS ADS separator (':') in file name is forbidden.", e.getMessage());
257-
}
252+
final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> FilenameUtils.getExtension("foo.exe:bar.txt"));
253+
assertEquals("NTFS ADS separator (':') in file name is forbidden.", e.getMessage());
258254
} else {
259255
// Upwards compatibility:
260256
assertEquals("txt", FilenameUtils.getExtension("foo.exe:bar.txt"));
@@ -585,12 +581,8 @@ public void testIndexOfExtension() {
585581

586582
if (FilenameUtils.isSystemWindows()) {
587583
// Special case handling for NTFS ADS names
588-
try {
589-
FilenameUtils.indexOfExtension("foo.exe:bar.txt");
590-
throw new AssertionError("Expected Exception");
591-
} catch (final IllegalArgumentException e) {
592-
assertEquals("NTFS ADS separator (':') in file name is forbidden.", e.getMessage());
593-
}
584+
final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> FilenameUtils.indexOfExtension("foo.exe:bar.txt"));
585+
assertEquals("NTFS ADS separator (':') in file name is forbidden.", e.getMessage());
594586
} else {
595587
// Upwards compatibility on other systems
596588
assertEquals(11, FilenameUtils.indexOfExtension("foo.exe:bar.txt"));

src/test/java/org/apache/commons/io/input/NullInputStreamTest.java

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
2222
import static org.junit.jupiter.api.Assertions.assertTrue;
23-
import static org.junit.jupiter.api.Assertions.fail;
2423

2524
import java.io.EOFException;
2625
import java.io.IOException;
@@ -75,12 +74,8 @@ public void testMarkAndReset() throws Exception {
7574
assertTrue(input.markSupported(), "Mark Should be Supported");
7675

7776
// No Mark
78-
try {
79-
input.reset();
80-
fail("Read limit exceeded, expected IOException ");
81-
} catch (final IOException e) {
82-
assertEquals("No position has been marked", e.getMessage(), "No Mark IOException message");
83-
}
77+
final IOException noMarkException = assertThrows(IOException.class, input::reset);
78+
assertEquals("No position has been marked", noMarkException.getMessage(), "No Mark IOException message");
8479

8580
for (; position < 3; position++) {
8681
assertEquals(position, input.read(), "Read Before Mark [" + position + "]");
@@ -103,13 +98,9 @@ public void testMarkAndReset() throws Exception {
10398
}
10499

105100
// Reset after read limit passed
106-
try {
107-
input.reset();
108-
fail("Read limit exceeded, expected IOException ");
109-
} catch (final IOException e) {
110-
assertEquals("Marked position [" + position + "] is no longer valid - passed the read limit [" + readlimit + "]", e.getMessage(),
101+
final IOException resetException = assertThrows(IOException.class, input::reset, "Read limit exceeded, expected IOException");
102+
assertEquals("Marked position [" + position + "] is no longer valid - passed the read limit [" + readlimit + "]", resetException.getMessage(),
111103
"Read limit IOException message");
112-
}
113104
}
114105
}
115106

@@ -118,19 +109,11 @@ public void testMarkNotSupported() throws Exception {
118109
final InputStream input = new TestNullInputStream(100, false, true);
119110
assertFalse(input.markSupported(), "Mark Should NOT be Supported");
120111

121-
try {
122-
input.mark(5);
123-
fail("mark() should throw UnsupportedOperationException");
124-
} catch (final UnsupportedOperationException e) {
125-
assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "mark() error message");
126-
}
112+
final UnsupportedOperationException markException = assertThrows(UnsupportedOperationException.class, () -> input.mark(5));
113+
assertEquals(MARK_RESET_NOT_SUPPORTED, markException.getMessage(), "mark() error message");
127114

128-
try {
129-
input.reset();
130-
fail("reset() should throw UnsupportedOperationException");
131-
} catch (final UnsupportedOperationException e) {
132-
assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "reset() error message");
133-
}
115+
final UnsupportedOperationException resetException = assertThrows(UnsupportedOperationException.class, input::reset);
116+
assertEquals(MARK_RESET_NOT_SUPPORTED, resetException.getMessage(), "reset() error message");
134117
input.close();
135118
}
136119

@@ -149,12 +132,8 @@ public void testRead() throws Exception {
149132
assertEquals(0, input.available(), "Available after End of File");
150133

151134
// Test reading after the end of file
152-
try {
153-
final int result = input.read();
154-
fail("Should have thrown an IOException, byte=[" + result + "]");
155-
} catch (final IOException e) {
156-
assertEquals("Read after end of file", e.getMessage());
157-
}
135+
final IOException e = assertThrows(IOException.class, input::read);
136+
assertEquals("Read after end of file", e.getMessage());
158137

159138
// Close - should reset
160139
input.close();
@@ -185,12 +164,8 @@ public void testReadByteArray() throws Exception {
185164
assertEquals(-1, count3, "Read 3 (EOF)");
186165

187166
// Test reading after the end of file
188-
try {
189-
final int count4 = input.read(bytes);
190-
fail("Should have thrown an IOException, byte=[" + count4 + "]");
191-
} catch (final IOException e) {
192-
assertEquals("Read after end of file", e.getMessage());
193-
}
167+
final IOException e = assertThrows(IOException.class, () -> input.read(bytes));
168+
assertEquals("Read after end of file", e.getMessage());
194169

195170
// reset by closing
196171
input.close();
@@ -214,12 +189,9 @@ public void testSkip() throws Exception {
214189
assertEquals(7, input.read(), "Read 3");
215190
assertEquals(2, input.skip(5), "Skip 2"); // only 2 left to skip
216191
assertEquals(-1, input.skip(5), "Skip 3 (EOF)"); // End of file
217-
try {
218-
input.skip(5); //
219-
fail("Expected IOException for skipping after end of file");
220-
} catch (final IOException e) {
221-
assertEquals("Skip after end of file", e.getMessage(), "Skip after EOF IOException message");
222-
}
192+
193+
final IOException e = assertThrows(IOException.class, () -> input.skip(5), "Expected IOException for skipping after end of file");
194+
assertEquals("Skip after end of file", e.getMessage(), "Skip after EOF IOException message");
223195
input.close();
224196
}
225197
}

src/test/java/org/apache/commons/io/input/NullReaderTest.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,8 @@ public void testMarkAndReset() throws Exception {
7575
assertTrue(reader.markSupported(), "Mark Should be Supported");
7676

7777
// No Mark
78-
try {
79-
reader.reset();
80-
fail("Read limit exceeded, expected IOException ");
81-
} catch (final IOException e) {
82-
assertEquals("No position has been marked", e.getMessage(), "No Mark IOException message");
83-
}
78+
final IOException resetException = assertThrows(IOException.class, reader::reset);
79+
assertEquals("No position has been marked", resetException.getMessage(), "No Mark IOException message");
8480

8581
for (; position < 3; position++) {
8682
assertEquals(position, reader.read(), "Read Before Mark [" + position + "]");
@@ -103,13 +99,9 @@ public void testMarkAndReset() throws Exception {
10399
}
104100

105101
// Reset after read limit passed
106-
try {
107-
reader.reset();
108-
fail("Read limit exceeded, expected IOException ");
109-
} catch (final IOException e) {
110-
assertEquals("Marked position [" + position + "] is no longer valid - passed the read limit [" + readlimit + "]", e.getMessage(),
102+
final IOException e = assertThrows(IOException.class, reader::reset);
103+
assertEquals("Marked position [" + position + "] is no longer valid - passed the read limit [" + readlimit + "]", e.getMessage(),
111104
"Read limit IOException message");
112-
}
113105
}
114106
}
115107

@@ -211,12 +203,9 @@ public void testSkip() throws Exception {
211203
assertEquals(7, reader.read(), "Read 3");
212204
assertEquals(2, reader.skip(5), "Skip 2"); // only 2 left to skip
213205
assertEquals(-1, reader.skip(5), "Skip 3 (EOF)"); // End of file
214-
try {
215-
reader.skip(5); //
216-
fail("Expected IOException for skipping after end of file");
217-
} catch (final IOException e) {
218-
assertEquals("Skip after end of file", e.getMessage(), "Skip after EOF IOException message");
219-
}
206+
207+
final IOException e = assertThrows(IOException.class, () -> reader.skip(5));
208+
assertEquals("Skip after end of file", e.getMessage(), "Skip after EOF IOException message");
220209
}
221210
}
222211
}

src/test/java/org/apache/commons/io/input/TaggedInputStreamTest.java

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
22-
import static org.junit.jupiter.api.Assertions.fail;
2323

2424
import java.io.ByteArrayInputStream;
2525
import java.io.IOException;
@@ -41,46 +41,22 @@ public void testBrokenStream() {
4141
new TaggedInputStream(new BrokenInputStream(exception));
4242

4343
// Test the available() method
44-
try {
45-
stream.available();
46-
fail("Expected exception not thrown.");
47-
} catch (final IOException e) {
48-
assertTrue(stream.isCauseOf(e));
49-
try {
50-
stream.throwIfCauseOf(e);
51-
fail("Expected exception not thrown.");
52-
} catch (final IOException e2) {
53-
assertEquals(exception, e2);
54-
}
55-
}
44+
final IOException exceptionAvailable = assertThrows(IOException.class, stream::available);
45+
assertTrue(stream.isCauseOf(exceptionAvailable));
46+
final IOException exceptionAvailableCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionAvailable));
47+
assertEquals(exception, exceptionAvailableCause);
5648

5749
// Test the read() method
58-
try {
59-
stream.read();
60-
fail("Expected exception not thrown.");
61-
} catch (final IOException e) {
62-
assertTrue(stream.isCauseOf(e));
63-
try {
64-
stream.throwIfCauseOf(e);
65-
fail("Expected exception not thrown.");
66-
} catch (final IOException e2) {
67-
assertEquals(exception, e2);
68-
}
69-
}
50+
final IOException exceptionRead = assertThrows(IOException.class, stream::read);
51+
assertTrue(stream.isCauseOf(exceptionRead));
52+
final IOException exceptionReadCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionRead));
53+
assertEquals(exception, exceptionReadCause);
7054

7155
// Test the close() method
72-
try {
73-
stream.close();
74-
fail("Expected exception not thrown.");
75-
} catch (final IOException e) {
76-
assertTrue(stream.isCauseOf(e));
77-
try {
78-
stream.throwIfCauseOf(e);
79-
fail("Expected exception not thrown.");
80-
} catch (final IOException e2) {
81-
assertEquals(exception, e2);
82-
}
83-
}
56+
final IOException exceptionClose = assertThrows(IOException.class, stream::close);
57+
assertTrue(stream.isCauseOf(exceptionClose));
58+
final IOException exceptionCloseCause = assertThrows(IOException.class, () -> stream.throwIfCauseOf(exceptionClose));
59+
assertEquals(exception, exceptionCloseCause);
8460
}
8561

8662
@Test

src/test/java/org/apache/commons/io/input/TaggedReaderTest.java

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
22-
import static org.junit.jupiter.api.Assertions.fail;
2323

2424
import java.io.IOException;
2525
import java.io.Reader;
@@ -40,46 +40,22 @@ public void testBrokenReader() {
4040
final TaggedReader reader = new TaggedReader(new BrokenReader(exception));
4141

4242
// Test the ready() method
43-
try {
44-
reader.ready();
45-
fail("Expected exception not thrown.");
46-
} catch (final IOException e) {
47-
assertTrue(reader.isCauseOf(e));
48-
try {
49-
reader.throwIfCauseOf(e);
50-
fail("Expected exception not thrown.");
51-
} catch (final IOException e2) {
52-
assertEquals(exception, e2);
53-
}
54-
}
43+
final IOException readyException = assertThrows(IOException.class, reader::ready);
44+
assertTrue(reader.isCauseOf(readyException));
45+
final IOException rethrownReadyException = assertThrows(IOException.class, () -> reader.throwIfCauseOf(readyException));
46+
assertEquals(exception, rethrownReadyException);
5547

5648
// Test the read() method
57-
try {
58-
reader.read();
59-
fail("Expected exception not thrown.");
60-
} catch (final IOException e) {
61-
assertTrue(reader.isCauseOf(e));
62-
try {
63-
reader.throwIfCauseOf(e);
64-
fail("Expected exception not thrown.");
65-
} catch (final IOException e2) {
66-
assertEquals(exception, e2);
67-
}
68-
}
49+
final IOException readException = assertThrows(IOException.class, reader::read);
50+
assertTrue(reader.isCauseOf(readException));
51+
final IOException rethrownReadException = assertThrows(IOException.class, () -> reader.throwIfCauseOf(readException));
52+
assertEquals(exception, rethrownReadException);
6953

7054
// Test the close() method
71-
try {
72-
reader.close();
73-
fail("Expected exception not thrown.");
74-
} catch (final IOException e) {
75-
assertTrue(reader.isCauseOf(e));
76-
try {
77-
reader.throwIfCauseOf(e);
78-
fail("Expected exception not thrown.");
79-
} catch (final IOException e2) {
80-
assertEquals(exception, e2);
81-
}
82-
}
55+
final IOException closeException = assertThrows(IOException.class, reader::close);
56+
assertTrue(reader.isCauseOf(closeException));
57+
final IOException rethrownCloseException = assertThrows(IOException.class, () -> reader.throwIfCauseOf(closeException));
58+
assertEquals(exception, rethrownCloseException);
8359
}
8460

8561
@Test

0 commit comments

Comments
 (0)