Skip to content

Commit eae791d

Browse files
mureinikgarydgregory
authored andcommitted
IO-628: Migration to JUnit Jupiter (#97)
* Use fail instead of throwing an AssertionFailedError Use JUnit's Assert#fail in order to fail tests with the proper JUnit API, instead of explicitly throwing the underlying exception. This change makes the tests easier to read and maintain, and will help facilitate the migration to JUnit Jupiter. * Remove outdate javadoc references to TestCase#setUp() In JUnit 3, in order to run some code before each test, you would extend junit.framework.TestCase and override its setUp() method. In JUnit 4 this is no longer the case, and you simply need to annotate the method with org.junit.Before, as is done in the classes in question. This patch removes old javadoc comments that reference the TestCase method which are no longer relevant, and are probably outdated remains from the days the project used JUnit 3. * Standardize org.junit.Assert imports The de-facto standard for using org.junit.Assert methods in the project is to statically import its method. However, some places use the class directly and call methods on it. This patch standardizes the approach, and makes sure all the usages of org.junit.Assert statically import methods in order to have a consistent coding standard throughout the project's tests. * Statically import org.junit.Assume methods Statically import methods from org.junit.Assume in order to make its usages consistent with org.junit.Assert's usage pattern. * Migrate test suite to JUnit Jupiter This patch upgrades the project's testing framework from JUnit 4.12 to the modern JUnit Jupiter 5.5.4. Since JUnit 5 Jupiter is not backwards compatible to JUnit 4.x (or even JUnit Vintage), this patch is a bit large, even though a lot of the changes are merely cosmetic (such as changing the argument order, see details below). In order to make the reviewer's task as easy as possible, this patch does not presume to use JUnit Jupiter's best practices and all its new functionality, but only to migrate the existing test with as little change as possible. Following patches may want to improve the tests by using some of JUnit Jupiter's new features. This patch includes the following changes: 1. Maven dependency changes: a. junit:junit was replaced with org.junit.jupiter:junit-jupiter. b. org.junit-pioneer:junit-pioneer was introduced (see details below in section 2.g.). 2. Annotations: a. org.junit.jupiter.api.Test was used as a drop in replacement for org.juit.Test without arguments. See 3.b. and 3.c. for handling of @test annotations with "expected" and "timeout" arguments respectively. b. org.junit.jupiter.params.ParameterizedTest in conjunction with org.junit.jupiter.params.provider.MethodSource was used to replace org.juit.Test in test classes run with org.junit.runners.Parameterized. c. org.junit.runners.Parameterized.Parameters annotations were removed, and the methods annotated with them were written to match the signature expected of a @MethodSource d. org.junit.jupiter.api.BeforeEach was used as an drop in replacement for org.junit.Before. e. org.junit.jupiter.api.AfterEach was used as a drop in replacement for org.junit.After. f. org.junit.jupiter.api.Disabled was used as a drop in replacement for org.junit.Ignore. g. org.junitpioneer.jupiter.DefaultLocale was used as a drop in replacement for org.apache.commons.io.testtools.SystemDefaults. This annotation comes from the JUnit Pioneer project, and while it isn't part of the actual JUnit project, it's widely accepted as an extension library for JUnit, and is used in other Apache Commons projects, such as Apache Commons Lang. 3. Assertions: a. org.junit.jupiter.api.Assertions' methods were used as drop in replacements for org.junit.Assert's methods with the same name in the simple case of an assertion without a message. In the case of an assertion with a message, org.junit.jupiter.api.Assertions' methods were used, but the argument order was changed - Assert's methods take the message as the first argument, while Assertions' methods take the message as the last argument. b. org.junit.jupiter.api.Assertions#assertThrows was used to assert that a specific exception was throws instead of an org.junit.Test annotation with an "expected" argument. This technique has a side bonus of making the tests slightly stricter, as now they can assert the exception was thrown from a specific line and prevent false positives where the test's "set-up" code accidentally threw that exception. The throws clauses of these methods were cleaned up from exceptions that can no longer be thrown in order to avoid compilation warnings. c. org.junit.jupiter.api.Assertions#assertTimeout was used to assert a specific block of code completes before a timeout is reach instead of an org.junit.Test annotation with a "timeout" argument. 4. Assumptions a. org.junit.jupiter.api.Assumptions' methods were used as drop in replacements for org.junit.Assume's methods with the same name, but the argument order was changed - Assume's methods take the message as the first argument, while Assumptions' methods take the message as the last argument. 5. Rules a. org.junit.jupiter.api.io.TempDir was used instead of the org.junit.rules.TemporaryFolder Rule, and the code that used them was adapted accordingly. b. org.apache.commons.io.testtools.SystemDefaultsSwitch was removed from the project, and its functionality replaced by org.junitpioneer.jupiter.DefaultLocale. 6. Specific changes: a. Parameters in JUnit Jupiter (such as those produced by MethodSource) can only be applied to @test methods, not to other lifecycle methods, such as @beforeeach. Thus, the design of ReversedLinesFileReaderTestParamFile had to be changed - instead of receiving constructor arguments and storing a state in data members, it was rewritten to be stateless, and manage all its resources in the single test method it has, testDataIntegrityWithBufferedReader. As a side bonus, this change allows to narrow the scope of open resources, and close them immediately after they are no longer needed, instead of preserving them in data members until the @after method is called. b. JUnit Jupiter's @tempdir is stricter than JUnit 4's @TemporaryFolder, and if it cannot delete any file at the end of the test, it will fail the test. FileUtilsCleanDirectoryTestCase's methods testThrowsOnNullList and testThrowsOnCannotDeleteFile chmod the temporary directory and thus prevent files in it from being deleted at the end of the test. A finally block was added to these tests in order to chmod the temporary directory back to 755 and allow it to be deleted. In addition, the restriction of not running the test on Windows was changed to use JUnit Jupiter's @DisabledOnOs annotation, in order to make the test a tad easier to follow. c. TailerTest has a similar issue to 6.b., where threads opened during the test are only closed in the @AfterEach method (by calling Tailer#stop), which is too late, since the @tempdir's tear down is called (and fails) before hand. Using a shared temp dir (by declaring the member static) causes the directory to be deleted only when the entire class is finished. * Attempt to disable JaCoCo for JDK EA This is done by passing an environment variable in the .travis.yml file, and then using it to set the jacoco.skip property in the pom.xml.
1 parent 8940848 commit eae791d

130 files changed

Lines changed: 2375 additions & 2631 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ jdk:
2323
- openjdk-ea
2424
- oraclejdk11
2525

26+
matrix:
27+
exclude:
28+
- jdk: openjdk-ea
29+
include:
30+
- jdk: openjdk-ea
31+
env: JACOCO_SKIP=true
32+
33+
2634
script:
2735
- mvn
2836

pom.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,15 @@ file comparators, endian transformation classes, and much more.
227227

228228
<dependencies>
229229
<dependency>
230-
<groupId>junit</groupId>
231-
<artifactId>junit</artifactId>
232-
<version>4.12</version>
230+
<groupId>org.junit.jupiter</groupId>
231+
<artifactId>junit-jupiter</artifactId>
232+
<version>5.5.2</version>
233+
<scope>test</scope>
234+
</dependency>
235+
<dependency>
236+
<groupId>org.junit-pioneer</groupId>
237+
<artifactId>junit-pioneer</artifactId>
238+
<version>0.3.0</version>
233239
<scope>test</scope>
234240
</dependency>
235241
<dependency>
@@ -285,6 +291,7 @@ file comparators, endian transformation classes, and much more.
285291
<commons.surefire.version>2.22.2</commons.surefire.version>
286292
<commons.japicmp.version>0.14.1</commons.japicmp.version>
287293
<japicmp.skip>false</japicmp.skip>
294+
<jacoco.skip>${env.JACOCO_SKIP}</jacoco.skip>
288295
</properties>
289296

290297
<build>

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

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

1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertFalse;
22-
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertTrue;
24-
import static org.junit.Assert.fail;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.junit.jupiter.api.Assertions.fail;
2525

2626
import java.nio.charset.Charset;
2727
import java.util.Arrays;
2828

29-
import org.junit.Test;
29+
import org.junit.jupiter.api.Test;
3030

3131

3232
/**
@@ -42,9 +42,9 @@ public class ByteOrderMarkTestCase {
4242
/** Test {@link ByteOrderMark#getCharsetName()} */
4343
@Test
4444
public void charsetName() {
45-
assertEquals("test1 name", "test1", TEST_BOM_1.getCharsetName());
46-
assertEquals("test2 name", "test2", TEST_BOM_2.getCharsetName());
47-
assertEquals("test3 name", "test3", TEST_BOM_3.getCharsetName());
45+
assertEquals("test1", TEST_BOM_1.getCharsetName(), "test1 name");
46+
assertEquals("test2", TEST_BOM_2.getCharsetName(), "test2 name");
47+
assertEquals("test3", TEST_BOM_3.getCharsetName(), "test3 name");
4848
}
4949

5050
/** Tests that {@link ByteOrderMark#getCharsetName()} can be loaded as a {@link java.nio.charset.Charset} as advertised. */
@@ -59,52 +59,53 @@ public void constantCharsetNames() {
5959

6060
/** Test {@link ByteOrderMark#length()} */
6161
@Test
62-
public void testLength() { assertEquals("test1 length", 1, TEST_BOM_1.length());
63-
assertEquals("test2 length", 2, TEST_BOM_2.length());
64-
assertEquals("test3 length", 3, TEST_BOM_3.length());
62+
public void testLength() {
63+
assertEquals(1, TEST_BOM_1.length(), "test1 length");
64+
assertEquals(2, TEST_BOM_2.length(), "test2 length");
65+
assertEquals(3, TEST_BOM_3.length(), "test3 length");
6566
}
6667

6768
/** Test {@link ByteOrderMark#get(int)} */
6869
@Test
6970
public void get() {
70-
assertEquals("test1 get(0)", 1, TEST_BOM_1.get(0));
71-
assertEquals("test2 get(0)", 1, TEST_BOM_2.get(0));
72-
assertEquals("test2 get(1)", 2, TEST_BOM_2.get(1));
73-
assertEquals("test3 get(0)", 1, TEST_BOM_3.get(0));
74-
assertEquals("test3 get(1)", 2, TEST_BOM_3.get(1));
75-
assertEquals("test3 get(2)", 3, TEST_BOM_3.get(2));
71+
assertEquals(1, TEST_BOM_1.get(0), "test1 get(0)");
72+
assertEquals(1, TEST_BOM_2.get(0), "test2 get(0)");
73+
assertEquals(2, TEST_BOM_2.get(1), "test2 get(1)");
74+
assertEquals(1, TEST_BOM_3.get(0), "test3 get(0)");
75+
assertEquals(2, TEST_BOM_3.get(1), "test3 get(1)");
76+
assertEquals(3, TEST_BOM_3.get(2), "test3 get(2)");
7677
}
7778

7879
/** Test {@link ByteOrderMark#getBytes()} */
7980
@Test
8081
public void getBytes() {
81-
assertTrue("test1 bytes", Arrays.equals(TEST_BOM_1.getBytes(), new byte[] {(byte)1}));
82-
assertTrue("test1 bytes", Arrays.equals(TEST_BOM_2.getBytes(), new byte[] {(byte)1, (byte)2}));
83-
assertTrue("test1 bytes", Arrays.equals(TEST_BOM_3.getBytes(), new byte[] {(byte)1, (byte)2, (byte)3}));
82+
assertTrue(Arrays.equals(TEST_BOM_1.getBytes(), new byte[] {(byte)1}), "test1 bytes");
83+
assertTrue(Arrays.equals(TEST_BOM_2.getBytes(), new byte[] {(byte)1, (byte)2}), "test1 bytes");
84+
assertTrue(Arrays.equals(TEST_BOM_3.getBytes(), new byte[] {(byte)1, (byte)2, (byte)3}), "test1 bytes");
8485
}
8586

8687
/** Test {@link ByteOrderMark#equals(Object)} */
8788
@SuppressWarnings("EqualsWithItself")
8889
@Test
8990
public void testEquals() {
90-
assertTrue("test1 equals", TEST_BOM_1.equals(TEST_BOM_1));
91-
assertTrue("test2 equals", TEST_BOM_2.equals(TEST_BOM_2));
92-
assertTrue("test3 equals", TEST_BOM_3.equals(TEST_BOM_3));
93-
94-
assertFalse("Object not equal", TEST_BOM_1.equals(new Object()));
95-
assertFalse("test1-1 not equal", TEST_BOM_1.equals(new ByteOrderMark("1a", 2)));
96-
assertFalse("test1-2 not test2", TEST_BOM_1.equals(new ByteOrderMark("1b", 1, 2)));
97-
assertFalse("test2 not equal", TEST_BOM_2.equals(new ByteOrderMark("2", 1, 1)));
98-
assertFalse("test3 not equal", TEST_BOM_3.equals(new ByteOrderMark("3", 1, 2, 4)));
91+
assertTrue(TEST_BOM_1.equals(TEST_BOM_1), "test1 equals");
92+
assertTrue(TEST_BOM_2.equals(TEST_BOM_2), "test2 equals");
93+
assertTrue(TEST_BOM_3.equals(TEST_BOM_3), "test3 equals");
94+
95+
assertFalse(TEST_BOM_1.equals(new Object()), "Object not equal");
96+
assertFalse(TEST_BOM_1.equals(new ByteOrderMark("1a", 2)), "test1-1 not equal");
97+
assertFalse(TEST_BOM_1.equals(new ByteOrderMark("1b", 1, 2)), "test1-2 not test2");
98+
assertFalse(TEST_BOM_2.equals(new ByteOrderMark("2", 1, 1)), "test2 not equal");
99+
assertFalse(TEST_BOM_3.equals(new ByteOrderMark("3", 1, 2, 4)), "test3 not equal");
99100
}
100101

101102
/** Test {@link ByteOrderMark#hashCode()} */
102103
@Test
103104
public void testHashCode() {
104105
final int bomClassHash = ByteOrderMark.class.hashCode();
105-
assertEquals("hash test1 ", bomClassHash + 1, TEST_BOM_1.hashCode());
106-
assertEquals("hash test2 ", bomClassHash + 3, TEST_BOM_2.hashCode());
107-
assertEquals("hash test3 ", bomClassHash + 6, TEST_BOM_3.hashCode());
106+
assertEquals(bomClassHash + 1, TEST_BOM_1.hashCode(), "hash test1 ");
107+
assertEquals(bomClassHash + 3, TEST_BOM_2.hashCode(), "hash test2 ");
108+
assertEquals(bomClassHash + 6, TEST_BOM_3.hashCode(), "hash test3 ");
108109
}
109110

110111
/** Test Errors */
@@ -139,8 +140,8 @@ public void errors() {
139140
/** Test {@link ByteOrderMark#toString()} */
140141
@Test
141142
public void testToString() {
142-
assertEquals("test1 ", "ByteOrderMark[test1: 0x1]", TEST_BOM_1.toString());
143-
assertEquals("test2 ", "ByteOrderMark[test2: 0x1,0x2]", TEST_BOM_2.toString());
144-
assertEquals("test3 ", "ByteOrderMark[test3: 0x1,0x2,0x3]", TEST_BOM_3.toString());
143+
assertEquals("ByteOrderMark[test1: 0x1]", TEST_BOM_1.toString(), "test1 ");
144+
assertEquals("ByteOrderMark[test2: 0x1,0x2]", TEST_BOM_2.toString(), "test2 ");
145+
assertEquals("ByteOrderMark[test3: 0x1,0x2,0x3]", TEST_BOM_3.toString(), "test3 ");
145146
}
146147
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package org.apache.commons.io;
1818

19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
1922
import java.nio.ByteOrder;
2023

21-
import org.junit.Assert;
22-
import org.junit.Test;
24+
import org.junit.jupiter.api.Test;
2325

2426
public class ByteOrderParserTest {
2527

@@ -29,16 +31,16 @@ private ByteOrder parseByteOrder(final String value) {
2931

3032
@Test
3133
public void testParseBig() {
32-
Assert.assertEquals(ByteOrder.BIG_ENDIAN, parseByteOrder("BIG_ENDIAN"));
34+
assertEquals(ByteOrder.BIG_ENDIAN, parseByteOrder("BIG_ENDIAN"));
3335
}
3436

3537
@Test
3638
public void testParseLittle() {
37-
Assert.assertEquals(ByteOrder.LITTLE_ENDIAN, parseByteOrder("LITTLE_ENDIAN"));
39+
assertEquals(ByteOrder.LITTLE_ENDIAN, parseByteOrder("LITTLE_ENDIAN"));
3840
}
3941

40-
@Test(expected = IllegalArgumentException.class)
41-
public void testThrowsException() throws Exception {
42-
parseByteOrder("some value");
42+
@Test
43+
public void testThrowsException() {
44+
assertThrows(IllegalArgumentException.class, () -> parseByteOrder("some value"));
4345
}
4446
}

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
package org.apache.commons.io;
1919

20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
2022
import java.nio.charset.Charset;
2123
import java.util.SortedMap;
2224

23-
import org.junit.Assert;
24-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
2526

2627
/**
2728
* Tests {@link Charsets}.
@@ -35,50 +36,50 @@ public void testRequiredCharsets() {
3536
final SortedMap<String, Charset> requiredCharsets = Charsets.requiredCharsets();
3637
// test for what we expect to be there as of Java 6
3738
// Make sure the object at the given key is the right one
38-
Assert.assertEquals(requiredCharsets.get("US-ASCII").name(), "US-ASCII");
39-
Assert.assertEquals(requiredCharsets.get("ISO-8859-1").name(), "ISO-8859-1");
40-
Assert.assertEquals(requiredCharsets.get("UTF-8").name(), "UTF-8");
41-
Assert.assertEquals(requiredCharsets.get("UTF-16").name(), "UTF-16");
42-
Assert.assertEquals(requiredCharsets.get("UTF-16BE").name(), "UTF-16BE");
43-
Assert.assertEquals(requiredCharsets.get("UTF-16LE").name(), "UTF-16LE");
39+
assertEquals(requiredCharsets.get("US-ASCII").name(), "US-ASCII");
40+
assertEquals(requiredCharsets.get("ISO-8859-1").name(), "ISO-8859-1");
41+
assertEquals(requiredCharsets.get("UTF-8").name(), "UTF-8");
42+
assertEquals(requiredCharsets.get("UTF-16").name(), "UTF-16");
43+
assertEquals(requiredCharsets.get("UTF-16BE").name(), "UTF-16BE");
44+
assertEquals(requiredCharsets.get("UTF-16LE").name(), "UTF-16LE");
4445
}
4546

4647
@Test
4748
public void testIso8859_1() {
48-
Assert.assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
49+
assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
4950
}
5051

5152
@Test
5253
public void testToCharset() {
53-
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
54-
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
55-
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
56-
Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
54+
assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
55+
assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
56+
assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
57+
assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
5758
}
5859

5960
@Test
6061
public void testUsAscii() {
61-
Assert.assertEquals("US-ASCII", Charsets.US_ASCII.name());
62+
assertEquals("US-ASCII", Charsets.US_ASCII.name());
6263
}
6364

6465
@Test
6566
public void testUtf16() {
66-
Assert.assertEquals("UTF-16", Charsets.UTF_16.name());
67+
assertEquals("UTF-16", Charsets.UTF_16.name());
6768
}
6869

6970
@Test
7071
public void testUtf16Be() {
71-
Assert.assertEquals("UTF-16BE", Charsets.UTF_16BE.name());
72+
assertEquals("UTF-16BE", Charsets.UTF_16BE.name());
7273
}
7374

7475
@Test
7576
public void testUtf16Le() {
76-
Assert.assertEquals("UTF-16LE", Charsets.UTF_16LE.name());
77+
assertEquals("UTF-16LE", Charsets.UTF_16LE.name());
7778
}
7879

7980
@Test
8081
public void testUtf8() {
81-
Assert.assertEquals("UTF-8", Charsets.UTF_8.name());
82+
assertEquals("UTF-8", Charsets.UTF_8.name());
8283
}
8384

8485
}

0 commit comments

Comments
 (0)