Skip to content

Commit 547e8c9

Browse files
author
Gary Gregory
committed
Use assertThrows() in tests instead of custom try/catch blocks.
1 parent dcccdc8 commit 547e8c9

10 files changed

Lines changed: 165 additions & 632 deletions

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

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
2121
import static org.junit.jupiter.api.Assertions.assertNull;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
2324
import static org.junit.jupiter.api.Assertions.fail;
2425

@@ -32,6 +33,7 @@
3233
import java.util.ArrayList;
3334
import java.util.List;
3435

36+
import org.apache.commons.io.filefilter.PrefixFileFilter;
3537
import org.apache.commons.io.test.TestUtils;
3638
import org.junit.jupiter.api.AfterEach;
3739
import org.junit.jupiter.api.BeforeEach;
@@ -183,12 +185,8 @@ public void testFileCleanerExitWhenFinished_NoTrackAfter() {
183185

184186
final String path = testFile.getPath();
185187
final Object marker = new Object();
186-
try {
187-
theInstance.track(path, marker);
188-
fail();
189-
} catch (final IllegalStateException ex) {
190-
// expected
191-
}
188+
189+
assertThrows(IllegalStateException.class, () -> theInstance.track(path, marker));
192190
assertTrue(theInstance.exitWhenFinished);
193191
assertNull(theInstance.reaper);
194192
}
@@ -298,30 +296,10 @@ public void testFileCleanerFile() throws Exception {
298296
}
299297
@Test
300298
public void testFileCleanerNull() {
301-
try {
302-
theInstance.track((File) null, new Object());
303-
fail();
304-
} catch (final NullPointerException ex) {
305-
// expected
306-
}
307-
try {
308-
theInstance.track((File) null, new Object(), FileDeleteStrategy.NORMAL);
309-
fail();
310-
} catch (final NullPointerException ex) {
311-
// expected
312-
}
313-
try {
314-
theInstance.track((String) null, new Object());
315-
fail();
316-
} catch (final NullPointerException ex) {
317-
// expected
318-
}
319-
try {
320-
theInstance.track((String) null, new Object(), FileDeleteStrategy.NORMAL);
321-
fail();
322-
} catch (final NullPointerException ex) {
323-
// expected
324-
}
299+
assertThrows(NullPointerException.class, () -> theInstance.track((File) null, new Object()));
300+
assertThrows(NullPointerException.class, () -> theInstance.track((File) null, new Object(), FileDeleteStrategy.NORMAL));
301+
assertThrows(NullPointerException.class, () -> theInstance.track((String) null, new Object()));
302+
assertThrows(NullPointerException.class, () -> theInstance.track((String) null, new Object(), FileDeleteStrategy.NORMAL));
325303
}
326304

327305
private void waitUntilTrackCount() throws Exception {

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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;
2223
import static org.junit.jupiter.api.Assertions.fail;
2324

@@ -83,12 +84,7 @@ public void testDeleteNormal() throws Exception {
8384
assertTrue(subDir.exists());
8485
assertTrue(subFile.exists());
8586
// delete dir
86-
try {
87-
FileDeleteStrategy.NORMAL.delete(subDir);
88-
fail();
89-
} catch (final IOException ex) {
90-
// expected
91-
}
87+
assertThrows(IOException.class, () -> FileDeleteStrategy.NORMAL.delete(subDir));
9288
assertTrue(subDir.exists());
9389
assertTrue(subFile.exists());
9490
// delete file
@@ -105,12 +101,7 @@ public void testDeleteNormal() throws Exception {
105101

106102
@Test
107103
public void testDeleteNull() throws Exception {
108-
try {
109-
FileDeleteStrategy.NORMAL.delete(null);
110-
fail();
111-
} catch (final NullPointerException ex) {
112-
// expected
113-
}
104+
assertThrows(NullPointerException.class, () -> FileDeleteStrategy.NORMAL.delete(null));
114105
assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(null));
115106
}
116107

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

Lines changed: 32 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -147,46 +147,23 @@ public void testGetFreeSpace_String() throws Exception {
147147
@Test
148148
public void testGetFreeSpaceOS_String_InitError() throws Exception {
149149
final FileSystemUtils fsu = new FileSystemUtils();
150-
try {
151-
fsu.freeSpaceOS("", -1, false, NEG_1_TIMEOUT);
152-
fail();
153-
} catch (final IllegalStateException ignore) {
154-
}
155-
try {
156-
fsu.freeSpaceOS("", -1, true, NEG_1_TIMEOUT);
157-
fail();
158-
} catch (final IllegalStateException ignore) {
159-
}
150+
assertThrows(IllegalStateException.class, () -> fsu.freeSpaceOS("", -1, false, NEG_1_TIMEOUT));
151+
assertThrows(IllegalStateException.class, () -> fsu.freeSpaceOS("", -1, true, NEG_1_TIMEOUT));
160152
}
161153

162154
@Test
163155
public void testGetFreeSpaceOS_String_NullPath() throws Exception {
164156
final FileSystemUtils fsu = new FileSystemUtils();
165-
try {
166-
fsu.freeSpaceOS(null, 1, false, NEG_1_TIMEOUT);
167-
fail();
168-
} catch (final IllegalArgumentException ignore) {
169-
}
170-
try {
171-
fsu.freeSpaceOS(null, 1, true, NEG_1_TIMEOUT);
172-
fail();
173-
} catch (final IllegalArgumentException ignore) {
174-
}
157+
assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceOS(null, 1, false, NEG_1_TIMEOUT));
158+
assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceOS(null, 1, true, NEG_1_TIMEOUT));
175159
}
176160

177161
@Test
178162
public void testGetFreeSpaceOS_String_Other() throws Exception {
179163
final FileSystemUtils fsu = new FileSystemUtils();
180-
try {
181-
fsu.freeSpaceOS("", 0, false, NEG_1_TIMEOUT);
182-
fail();
183-
} catch (final IllegalStateException ignore) {
184-
}
185-
try {
186-
fsu.freeSpaceOS("", 0, true, NEG_1_TIMEOUT);
187-
fail();
188-
} catch (final IllegalStateException ignore) {
189-
}
164+
assertThrows(IllegalStateException.class, () -> fsu.freeSpaceOS("", 0, false, NEG_1_TIMEOUT));
165+
assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceOS(null, 1, true, NEG_1_TIMEOUT));
166+
assertThrows(IllegalStateException.class, () -> fsu.freeSpaceOS("", 0, true, NEG_1_TIMEOUT));
190167
}
191168

192169
@Test
@@ -219,54 +196,21 @@ public void testGetFreeSpaceUnix_String_EmptyPath() throws Exception {
219196
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
220197
"xxx:/home/users/s 14428928 12956424 1472504 90% /home/users/s";
221198
final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
222-
try {
223-
fsu.freeSpaceUnix("", false, false, NEG_1_TIMEOUT);
224-
fail();
225-
} catch (final IllegalArgumentException ignore) {
226-
}
227-
try {
228-
fsu.freeSpaceUnix("", true, false, NEG_1_TIMEOUT);
229-
fail();
230-
} catch (final IllegalArgumentException ignore) {
231-
}
232-
try {
233-
fsu.freeSpaceUnix("", true, true, NEG_1_TIMEOUT);
234-
fail();
235-
} catch (final IllegalArgumentException ignore) {
236-
}
237-
try {
238-
fsu.freeSpaceUnix("", false, true, NEG_1_TIMEOUT);
239-
fail();
240-
} catch (final IllegalArgumentException ignore) {
241-
}
242-
199+
assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceUnix("", false, false, NEG_1_TIMEOUT));
200+
assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceUnix("", true, false, NEG_1_TIMEOUT));
201+
assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceUnix("", true, true, NEG_1_TIMEOUT));
202+
assertThrows(IllegalArgumentException.class, () -> fsu.freeSpaceUnix("", false, true, NEG_1_TIMEOUT));
243203
}
244204

245205
@Test
246206

247207
public void testGetFreeSpaceUnix_String_EmptyResponse() {
248208
final String lines = "";
249209
final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
250-
try {
251-
fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT);
252-
fail();
253-
} catch (final IOException ignore) {
254-
}
255-
try {
256-
fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT);
257-
fail();
258-
} catch (final IOException ignore) {
259-
}
260-
try {
261-
fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT);
262-
fail();
263-
} catch (final IOException ignore) {
264-
}
265-
try {
266-
fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT);
267-
fail();
268-
} catch (final IOException ignore) {
269-
}
210+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT));
211+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT));
212+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT));
213+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT));
270214
}
271215

272216
@Test
@@ -275,26 +219,10 @@ public void testGetFreeSpaceUnix_String_InvalidResponse1() {
275219
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
276220
" 14428928 12956424 100";
277221
final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
278-
try {
279-
fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT);
280-
fail();
281-
} catch (final IOException ignore) {
282-
}
283-
try {
284-
fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT);
285-
fail();
286-
} catch (final IOException ignore) {
287-
}
288-
try {
289-
fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT);
290-
fail();
291-
} catch (final IOException ignore) {
292-
}
293-
try {
294-
fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT);
295-
fail();
296-
} catch (final IOException ignore) {
297-
}
222+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT));
223+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT));
224+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT));
225+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT));
298226
}
299227

300228
@Test
@@ -303,26 +231,10 @@ public void testGetFreeSpaceUnix_String_InvalidResponse2() {
303231
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
304232
"xxx:/home/users/s 14428928 12956424 nnnnnnn 90% /home/users/s";
305233
final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
306-
try {
307-
fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT);
308-
fail();
309-
} catch (final IOException ignore) {
310-
}
311-
try {
312-
fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT);
313-
fail();
314-
} catch (final IOException ignore) {
315-
}
316-
try {
317-
fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT);
318-
fail();
319-
} catch (final IOException ignore) {
320-
}
321-
try {
322-
fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT);
323-
fail();
324-
} catch (final IOException ignore) {
325-
}
234+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT));
235+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT));
236+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT));
237+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT));
326238
}
327239

328240
@Test
@@ -331,26 +243,10 @@ public void testGetFreeSpaceUnix_String_InvalidResponse3() {
331243
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
332244
"xxx:/home/users/s 14428928 12956424 -1 90% /home/users/s";
333245
final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
334-
try {
335-
fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT);
336-
fail();
337-
} catch (final IOException ignore) {
338-
}
339-
try {
340-
fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT);
341-
fail();
342-
} catch (final IOException ignore) {
343-
}
344-
try {
345-
fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT);
346-
fail();
347-
} catch (final IOException ignore) {
348-
}
349-
try {
350-
fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT);
351-
fail();
352-
} catch (final IOException ignore) {
353-
}
246+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT));
247+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT));
248+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT));
249+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT));
354250
}
355251

356252
@Test
@@ -359,26 +255,10 @@ public void testGetFreeSpaceUnix_String_InvalidResponse4() {
359255
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
360256
"xxx-yyyyyyy-zzz:/home/users/s";
361257
final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
362-
try {
363-
fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT);
364-
fail();
365-
} catch (final IOException ignore) {
366-
}
367-
try {
368-
fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT);
369-
fail();
370-
} catch (final IOException ignore) {
371-
}
372-
try {
373-
fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT);
374-
fail();
375-
} catch (final IOException ignore) {
376-
}
377-
try {
378-
fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT);
379-
fail();
380-
} catch (final IOException ignore) {
381-
}
258+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, false, NEG_1_TIMEOUT));
259+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, false, NEG_1_TIMEOUT));
260+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", false, true, NEG_1_TIMEOUT));
261+
assertThrows(IOException.class, () -> fsu.freeSpaceUnix("/home/users/s", true, true, NEG_1_TIMEOUT));
382262
}
383263

384264
@Test
@@ -483,11 +363,7 @@ public void testGetFreeSpaceWindows_String_EmptyPath() throws Exception {
483363
public void testGetFreeSpaceWindows_String_EmptyResponse() {
484364
final String lines = "";
485365
final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
486-
try {
487-
fsu.freeSpaceWindows("C:", NEG_1_TIMEOUT);
488-
fail();
489-
} catch (final IOException ignore) {
490-
}
366+
assertThrows(IOException.class, () -> fsu.freeSpaceWindows("C:", NEG_1_TIMEOUT));
491367
}
492368

493369
@Test

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,7 @@ public void test_openInputStream_existsButIsDirectory() {
364364
@Test
365365
public void test_openInputStream_notExists() {
366366
final File directory = new File(temporaryFolder, "test.txt");
367-
try (FileInputStream in = FileUtils.openInputStream(directory)) {
368-
fail();
369-
} catch (final IOException ioe) {
370-
// expected
371-
}
367+
assertThrows(IOException.class, () -> FileUtils.openInputStream(directory));
372368
}
373369

374370
@Test
@@ -418,11 +414,7 @@ public void test_openOutputStream_notExistsCannotCreate() {
418414
"abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
419415
"abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz"; // 300 chars
420416
final File file = new File(temporaryFolder, "a/" + longStr + "/test.txt");
421-
try (FileOutputStream out = FileUtils.openOutputStream(file)) {
422-
fail();
423-
} catch (final IOException ioe) {
424-
// expected
425-
}
417+
assertThrows(IOException.class, () -> FileUtils.openOutputStream(file));
426418
}
427419

428420
// byteCountToDisplaySize

0 commit comments

Comments
 (0)