Skip to content

Commit d929d05

Browse files
committed
Migrate from JUnit 4 to 5
Exception cleanups in tests.
1 parent 6320b11 commit d929d05

24 files changed

Lines changed: 142 additions & 416 deletions

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2323
import static org.junit.jupiter.api.Assertions.assertNotNull;
24-
import static org.junit.jupiter.api.Assertions.fail;
25-
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
2625
import java.nio.charset.Charset;
2726

2827
import org.junit.jupiter.api.Test;
@@ -59,30 +58,10 @@ public void constantCharsetNames() {
5958
/** Test Errors */
6059
@Test
6160
public void errors() {
62-
try {
63-
new ByteOrderMark(null, 1,2,3);
64-
fail("null charset name, expected IllegalArgumentException");
65-
} catch (final IllegalArgumentException e) {
66-
// expected
67-
}
68-
try {
69-
new ByteOrderMark("", 1,2,3);
70-
fail("no charset name, expected IllegalArgumentException");
71-
} catch (final IllegalArgumentException e) {
72-
// expected
73-
}
74-
try {
75-
new ByteOrderMark("a", (int[])null);
76-
fail("null bytes, expected IllegalArgumentException");
77-
} catch (final IllegalArgumentException e) {
78-
// expected
79-
}
80-
try {
81-
new ByteOrderMark("b");
82-
fail("empty bytes, expected IllegalArgumentException");
83-
} catch (final IllegalArgumentException e) {
84-
// expected
85-
}
61+
assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark(null, 1, 2, 3));
62+
assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("", 1, 2, 3));
63+
assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("a", (int[]) null));
64+
assertThrows(IllegalArgumentException.class, () -> new ByteOrderMark("b"));
8665
}
8766

8867
/** Test {@link ByteOrderMark#get(int)} */

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,12 @@ private void doStart() {
138138
private String getInput(final String threadName) {
139139
final ReaderThread thread = (ReaderThread) threadMap.get(threadName);
140140
assertNotNull(thread, "getInput()");
141-
142141
return thread.getData();
143142
}
144143

145144
private String getOutput(final String threadName) {
146-
final ByteArrayOutputStream output =
147-
outputMap.get(threadName);
145+
final ByteArrayOutputStream output = outputMap.get(threadName);
148146
assertNotNull(output, "getOutput()");
149-
150147
return output.toString(StandardCharsets.UTF_8);
151148
}
152149

@@ -163,8 +160,7 @@ private void startWriter(final String name,
163160
final DemuxOutputStream demux) {
164161
final ByteArrayOutputStream output = new ByteArrayOutputStream();
165162
outputMap.put(name, output);
166-
final WriterThread thread =
167-
new WriterThread(name, data, output, demux);
163+
final WriterThread thread = new WriterThread(name, data, output, demux);
168164
threadMap.put(name, thread);
169165
}
170166

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

Lines changed: 2 additions & 6 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

@@ -513,12 +514,7 @@ public void testMissingStartDirectory() {
513514
assertEquals(1, results.size(), "Result Size");
514515
assertTrue(results.contains(invalidDir), "Current Dir");
515516

516-
try {
517-
new TestFileFinder(null, -1).find(null);
518-
fail("Null start directory didn't throw Exception");
519-
} catch (final NullPointerException ignore) {
520-
// expected result
521-
}
517+
assertThrows(NullPointerException.class, () -> new TestFileFinder(null, -1).find(null));
522518
}
523519

524520
/**

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

Lines changed: 3 additions & 13 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

@@ -448,8 +449,6 @@ public void testFilterDirAndFile4() {
448449
checkContainsFiles("[DirAndFile4] File", ioFiles, resultFiles);
449450
}
450451

451-
// ------------ Test DirectoryWalker implementation --------------------------
452-
453452
/**
454453
* test an invalid start directory
455454
*/
@@ -461,8 +460,6 @@ public void testHandleStartDirectoryFalse() {
461460

462461
}
463462

464-
// ------------ Test DirectoryWalker implementation --------------------------
465-
466463
/**
467464
* Test Limiting to current directory
468465
*/
@@ -473,10 +470,8 @@ public void testLimitToCurrent() {
473470
assertTrue(results.contains(FileUtils.current()), "Current Dir");
474471
}
475472

476-
// ------------ Test DirectoryWalker implementation --------------------------
477-
478473
/**
479-
* test an invalid start directory
474+
* Test an invalid start directory
480475
*/
481476
@Test
482477
public void testMissingStartDirectory() {
@@ -487,12 +482,7 @@ public void testMissingStartDirectory() {
487482
assertEquals(1, results.size(), "Result Size");
488483
assertTrue(results.contains(invalidDir), "Current Dir");
489484

490-
try {
491-
new TestFileFinder(null, -1).find(null);
492-
fail("Null start directory didn't throw Exception");
493-
} catch (final NullPointerException ignore) {
494-
// expected result
495-
}
485+
assertThrows(NullPointerException.class, () -> new TestFileFinder(null, -1).find(null));
496486
}
497487

498488
/**

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

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

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
import static org.junit.jupiter.api.Assertions.fail;
21-
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2221
import java.io.ByteArrayInputStream;
2322
import java.io.ByteArrayOutputStream;
2423
import java.io.EOFException;
@@ -40,12 +39,7 @@ public void testCtor() {
4039
@Test
4140
public void testEOFException() throws IOException {
4241
final ByteArrayInputStream input = new ByteArrayInputStream(new byte[] {});
43-
try {
44-
EndianUtils.readSwappedDouble(input);
45-
fail("Expected EOFException");
46-
} catch (final EOFException e) {
47-
// expected
48-
}
42+
assertThrows(EOFException.class, () -> EndianUtils.readSwappedDouble(input));
4943
}
5044

5145
@Test

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

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

1919
import static org.junit.jupiter.api.Assertions.assertFalse;
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;
22-
2322
import java.io.File;
2423
import java.io.IOException;
2524

@@ -115,12 +114,7 @@ public void testDirectoryDoesNotContainsDirectory() throws IOException {
115114
public void testDirectoryDoesNotExist() throws IOException {
116115
final File dir = new File("DOESNOTEXIST");
117116
assertFalse(dir.exists());
118-
try {
119-
assertFalse(FileUtils.directoryContains(dir, file1));
120-
fail("Expected " + IllegalArgumentException.class.getName());
121-
} catch (final IllegalArgumentException e) {
122-
// expected
123-
}
117+
assertThrows(IllegalArgumentException.class, () -> FileUtils.directoryContains(dir, file1));
124118
}
125119

126120
@Test
@@ -164,12 +158,7 @@ public void testIO466() throws IOException {
164158

165159
@Test
166160
public void testSameFile() throws IOException {
167-
try {
168-
assertTrue(FileUtils.directoryContains(file1, file1));
169-
fail("Expected " + IllegalArgumentException.class.getName());
170-
} catch (final IllegalArgumentException e) {
171-
// expected
172-
}
161+
assertThrows(IllegalArgumentException.class, () -> FileUtils.directoryContains(file1, file1));
173162
}
174163

175164
@Test
@@ -178,10 +167,6 @@ public void testUnrealizedContainment() throws IOException {
178167
final File file = new File(dir, "DOESNOTEXIST2");
179168
assertFalse(dir.exists());
180169
assertFalse(file.exists());
181-
try {
182-
assertTrue(FileUtils.directoryContains(dir, file));
183-
} catch (final IllegalArgumentException e) {
184-
// expected
185-
}
170+
assertThrows(IllegalArgumentException.class, () -> FileUtils.directoryContains(dir, file));
186171
}
187172
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +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;
23-
2423
import java.io.File;
2524
import java.util.ArrayList;
2625
import java.util.Collection;
@@ -156,12 +155,7 @@ public void testListFiles() {
156155
assertTrue(filenames.contains("dummy-index.html"), "'dummy-index.html' is missing");
157156
assertFalse(filenames.contains("Entries"), "'Entries' shouldn't be found");
158157

159-
try {
160-
FileUtils.listFiles(temporaryFolder, null, null);
161-
fail("Expected error about null parameter");
162-
} catch (final NullPointerException e) {
163-
// expected
164-
}
158+
assertThrows(NullPointerException.class, () -> FileUtils.listFiles(temporaryFolder, null, null));
165159
}
166160

167161
@Test

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import static org.junit.jupiter.api.Assertions.assertNull;
2222
import static org.junit.jupiter.api.Assertions.assertThrows;
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
24-
import static org.junit.jupiter.api.Assertions.fail;
25-
2624
import java.io.BufferedOutputStream;
2725
import java.io.File;
2826
import java.io.IOException;
@@ -457,11 +455,7 @@ public void testGetPathNoEndSeparator() {
457455

458456
@Test
459457
public void testGetPathNoEndSeparator_with_null_character() {
460-
try {
461-
assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~user/a\u0000/b/c.txt"));
462-
} catch (final IllegalArgumentException ignore) {
463-
464-
}
458+
assertThrows(IllegalArgumentException.class, () -> FilenameUtils.getPathNoEndSeparator("~user/a\u0000/b/c.txt"));
465459
}
466460

467461
@Test
@@ -511,11 +505,7 @@ public void testGetPrefix() {
511505

512506
@Test
513507
public void testGetPrefix_with_null_character() {
514-
try {
515-
assertEquals("~user\\", FilenameUtils.getPrefix("~u\u0000ser\\a\\b\\c.txt"));
516-
} catch (final IllegalArgumentException ignore) {
517-
518-
}
508+
assertThrows(IllegalArgumentException.class, () -> FilenameUtils.getPrefix("~u\u0000ser\\a\\b\\c.txt"));
519509
}
520510

521511
@Test
@@ -617,11 +607,7 @@ public void testIndexOfLastSeparator() {
617607

618608
@Test
619609
public void testInjectionFailure() {
620-
try {
621-
assertEquals("c", FilenameUtils.getName("a\\b\\\u0000c"));
622-
} catch (final IllegalArgumentException ignore) {
623-
624-
}
610+
assertThrows(IllegalArgumentException.class, () -> FilenameUtils.getName("a\\b\\\u0000c"));
625611
}
626612

627613
@Test
@@ -659,11 +645,7 @@ public void testIsExtension() {
659645

660646
@Test
661647
public void testIsExtension_injection() {
662-
try {
663-
FilenameUtils.isExtension("a.b\\fi\u0000le.txt", "TXT");
664-
fail("Should throw IAE");
665-
} catch (final IllegalArgumentException ignore) {
666-
}
648+
assertThrows(IllegalArgumentException.class, () -> FilenameUtils.isExtension("a.b\\fi\u0000le.txt", "TXT"));
667649
}
668650

669651
@Test
@@ -953,15 +935,8 @@ public void testNormalize() {
953935
*/
954936
@Test
955937
public void testNormalize_with_null_character() {
956-
try {
957-
assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("a\\b/c\u0000.txt"));
958-
} catch (final IllegalArgumentException ignore) {
959-
}
960-
961-
try {
962-
assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("\u0000a\\b/c.txt"));
963-
} catch (final IllegalArgumentException ignore) {
964-
}
938+
assertThrows(IllegalArgumentException.class, () -> FilenameUtils.normalize("a\\b/c\u0000.txt"));
939+
assertThrows(IllegalArgumentException.class, () -> FilenameUtils.normalize("\u0000a\\b/c.txt"));
965940
}
966941

967942
@Test

0 commit comments

Comments
 (0)