Skip to content

Commit e8a64d0

Browse files
zhtttylzcnaurothslfan1989
authored
HDFS-17718. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-hdfs-client. (#7563)
* HDFS-17718. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-hdfs-client. Co-authored-by: Chris Nauroth <[email protected]> Co-authored-by: Shilun Fan <[email protected]> Reviewed-by: Chris Nauroth <[email protected]> Reviewed-by: Shilun Fan <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent af38897 commit e8a64d0

28 files changed

+366
-361
lines changed

hadoop-hdfs-project/hadoop-hdfs-client/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,31 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
128128
<groupId>com.fasterxml.jackson.core</groupId>
129129
<artifactId>jackson-databind</artifactId>
130130
</dependency>
131+
<dependency>
132+
<groupId>org.junit.jupiter</groupId>
133+
<artifactId>junit-jupiter-api</artifactId>
134+
<scope>test</scope>
135+
</dependency>
136+
<dependency>
137+
<groupId>org.junit.jupiter</groupId>
138+
<artifactId>junit-jupiter-engine</artifactId>
139+
<scope>test</scope>
140+
</dependency>
141+
<dependency>
142+
<groupId>org.junit.jupiter</groupId>
143+
<artifactId>junit-jupiter-params</artifactId>
144+
<scope>test</scope>
145+
</dependency>
146+
<dependency>
147+
<groupId>org.junit.platform</groupId>
148+
<artifactId>junit-platform-launcher</artifactId>
149+
<scope>test</scope>
150+
</dependency>
151+
<dependency>
152+
<groupId>org.junit.vintage</groupId>
153+
<artifactId>junit-vintage-engine</artifactId>
154+
<scope>test</scope>
155+
</dependency>
131156
</dependencies>
132157

133158
<build>

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/fs/TestUrlStreamHandlerFactory.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
package org.apache.hadoop.fs;
2020

2121
import org.apache.hadoop.test.GenericTestUtils;
22-
import org.junit.Rule;
23-
import org.junit.Test;
24-
import org.junit.rules.Timeout;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.Timeout;
2524

2625
import java.io.File;
2726
import java.io.IOException;
@@ -35,15 +34,13 @@
3534
/**
3635
* Test of the URL stream handler factory.
3736
*/
37+
@Timeout(30)
3838
public class TestUrlStreamHandlerFactory {
3939

4040
private static final int RUNS = 20;
4141
private static final int THREADS = 10;
4242
private static final int TASKS = 200;
4343

44-
@Rule
45-
public Timeout globalTimeout = new Timeout(30000);
46-
4744
@Test
4845
public void testConcurrency() throws Exception {
4946
for (int i = 0; i < RUNS; i++) {

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/fs/TestXAttr.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818

1919
package org.apache.hadoop.fs;
2020

21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertNotSame;
23-
import static org.junit.Assert.assertNotEquals;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotSame;
23+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2424

25-
import org.junit.BeforeClass;
26-
import org.junit.Test;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Test;
2727

2828
/**
2929
* Tests for <code>XAttr</code> objects.
3030
*/
3131
public class TestXAttr {
3232
private static XAttr XATTR, XATTR1, XATTR2, XATTR3, XATTR4, XATTR5;
3333

34-
@BeforeClass
34+
@BeforeAll
3535
public static void setUp() throws Exception {
3636
byte[] value = {0x31, 0x32, 0x33};
3737
XATTR = new XAttr.Builder()

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/TestDFSOpsCountStatistics.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323

2424
import org.apache.hadoop.hdfs.DFSOpsCountStatistics.OpType;
2525

26-
import org.junit.Before;
27-
import org.junit.Rule;
28-
import org.junit.Test;
29-
30-
import org.junit.rules.ExpectedException;
31-
import org.junit.rules.Timeout;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
3228

29+
import org.junit.jupiter.api.Timeout;
3330
import org.slf4j.Logger;
3431
import org.slf4j.LoggerFactory;
3532

@@ -44,15 +41,16 @@
4441
import java.util.concurrent.atomic.AtomicReference;
4542

4643
import static org.apache.hadoop.util.concurrent.HadoopExecutors.newFixedThreadPool;
47-
import static org.junit.Assert.assertEquals;
48-
import static org.junit.Assert.assertFalse;
49-
import static org.junit.Assert.assertNotNull;
50-
import static org.junit.Assert.assertNull;
51-
import static org.junit.Assert.assertTrue;
44+
import static org.junit.jupiter.api.Assertions.assertEquals;
45+
import static org.junit.jupiter.api.Assertions.assertFalse;
46+
import static org.junit.jupiter.api.Assertions.assertNotNull;
47+
import static org.junit.jupiter.api.Assertions.assertNull;
48+
import static org.junit.jupiter.api.Assertions.assertTrue;
5249

5350
/**
5451
* This tests basic operations of {@link DFSOpsCountStatistics} class.
5552
*/
53+
@Timeout(10)
5654
public class TestDFSOpsCountStatistics {
5755

5856
private static final Logger LOG = LoggerFactory.getLogger(
@@ -63,12 +61,7 @@ public class TestDFSOpsCountStatistics {
6361
new DFSOpsCountStatistics();
6462
private final Map<OpType, AtomicLong> expectedOpsCountMap = new HashMap<>();
6563

66-
@Rule
67-
public final Timeout globalTimeout = new Timeout(10 * 1000);
68-
@Rule
69-
public final ExpectedException exception = ExpectedException.none();
70-
71-
@Before
64+
@BeforeEach
7265
public void setup() {
7366
for (OpType opType : OpType.values()) {
7467
expectedOpsCountMap.put(opType, new AtomicLong());
@@ -178,7 +171,7 @@ public void run() {
178171
startBlocker.countDown(); // all threads start making directories
179172
allDone.await(); // wait until all threads are done
180173

181-
assertNull("Child failed with exception.", childError.get());
174+
assertNull(childError.get(), "Child failed with exception.");
182175
verifyStatistics();
183176
} finally {
184177
threadPool.shutdownNow();
@@ -207,9 +200,9 @@ private void verifyStatistics() {
207200
for (OpType opType : OpType.values()) {
208201
assertNotNull(expectedOpsCountMap.get(opType));
209202
assertNotNull(statistics.getLong(opType.getSymbol()));
210-
assertEquals("Not expected count for operation " + opType.getSymbol(),
211-
expectedOpsCountMap.get(opType).longValue(),
212-
statistics.getLong(opType.getSymbol()).longValue());
203+
assertEquals(expectedOpsCountMap.get(opType).longValue(),
204+
statistics.getLong(opType.getSymbol()).longValue(),
205+
"Not expected count for operation " + opType.getSymbol());
213206
}
214207
}
215208

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/TestDFSPacket.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
import java.util.Random;
2121
import org.apache.hadoop.hdfs.protocol.datatransfer.PacketHeader;
2222
import org.apache.hadoop.io.DataOutputBuffer;
23-
import org.junit.Assert;
24-
import org.junit.Test;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.junit.jupiter.api.Assertions.fail;
2526

2627
public class TestDFSPacket {
2728
private static final int chunkSize = 512;
@@ -59,7 +60,7 @@ public static void assertArrayRegionsEqual(byte []buf1, int off1, byte []buf2,
5960
int off2, int len) {
6061
for (int i = 0; i < len; i++) {
6162
if (buf1[off1 + i] != buf2[off2 + i]) {
62-
Assert.fail("arrays differ at byte " + i + ". " +
63+
fail("arrays differ at byte " + i + ". " +
6364
"The first array has " + (int) buf1[off1 + i] +
6465
", but the second array has " + (int) buf2[off2 + i]);
6566
}

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/TestDefaultNameNodePort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import org.apache.hadoop.conf.Configuration;
2121
import org.apache.hadoop.fs.FileSystem;
2222
import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys;
23-
import org.junit.Test;
23+
import org.junit.jupiter.api.Test;
2424

2525
import java.net.InetSocketAddress;
2626
import java.net.URI;
2727

28-
import static org.junit.Assert.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
2929

3030
/** Test NameNode port defaulting code. */
3131
public class TestDefaultNameNodePort {

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/TestPeerCache.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.apache.hadoop.hdfs.net.Peer;
2222
import org.apache.hadoop.hdfs.protocol.DatanodeID;
2323
import org.apache.hadoop.net.unix.DomainSocket;
24-
import org.junit.Test;
24+
import org.junit.jupiter.api.Test;
2525
import org.mockito.Mockito;
2626
import org.mockito.invocation.InvocationOnMock;
2727
import org.mockito.stubbing.Answer;
@@ -33,9 +33,9 @@
3333
import java.io.OutputStream;
3434
import java.nio.channels.ReadableByteChannel;
3535

36-
import static org.junit.Assert.assertEquals;
37-
import static org.junit.Assert.assertSame;
38-
import static org.junit.Assert.assertTrue;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertSame;
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
3939

4040
public class TestPeerCache {
4141
static final Logger LOG = LoggerFactory.getLogger(TestPeerCache.class);

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/client/impl/TestLeaseRenewer.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
import org.apache.hadoop.security.UserGroupInformation;
2424
import org.apache.hadoop.test.GenericTestUtils;
2525
import org.apache.hadoop.util.Time;
26-
import org.junit.Assert;
27-
import org.junit.Before;
28-
import org.junit.Test;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
2928
import org.mockito.Mockito;
3029
import org.mockito.invocation.InvocationOnMock;
3130
import org.mockito.stubbing.Answer;
@@ -37,7 +36,12 @@
3736
import java.util.concurrent.atomic.AtomicInteger;
3837
import java.util.regex.Pattern;
3938

40-
import static org.junit.Assert.assertSame;
39+
import static org.junit.jupiter.api.Assertions.assertEquals;
40+
import static org.junit.jupiter.api.Assertions.assertFalse;
41+
import static org.junit.jupiter.api.Assertions.assertNotSame;
42+
import static org.junit.jupiter.api.Assertions.assertSame;
43+
import static org.junit.jupiter.api.Assertions.assertTrue;
44+
import static org.junit.jupiter.api.Assertions.fail;
4145

4246
public class TestLeaseRenewer {
4347
private final String FAKE_AUTHORITY="hdfs://nn1/";
@@ -54,7 +58,7 @@ public class TestLeaseRenewer {
5458
/** Cause renewals often so test runs quickly. */
5559
private static final long FAST_GRACE_PERIOD = 100L;
5660

57-
@Before
61+
@BeforeEach
5862
public void setupMocksAndRenewer() throws IOException {
5963
MOCK_DFSCLIENT = createMockClient();
6064

@@ -82,19 +86,19 @@ public void testInstanceSharing() throws IOException {
8286
FAKE_AUTHORITY, FAKE_UGI_A, MOCK_DFSCLIENT);
8387
LeaseRenewer lr2 = LeaseRenewer.getInstance(
8488
FAKE_AUTHORITY, FAKE_UGI_A, MOCK_DFSCLIENT);
85-
Assert.assertSame(lr, lr2);
89+
assertSame(lr, lr2);
8690

8791
// But a different UGI should return a different instance
8892
LeaseRenewer lr3 = LeaseRenewer.getInstance(
8993
FAKE_AUTHORITY, FAKE_UGI_B, MOCK_DFSCLIENT);
90-
Assert.assertNotSame(lr, lr3);
94+
assertNotSame(lr, lr3);
9195

9296
// A different authority with same UGI should also be a different
9397
// instance.
9498
LeaseRenewer lr4 = LeaseRenewer.getInstance(
9599
"someOtherAuthority", FAKE_UGI_B, MOCK_DFSCLIENT);
96-
Assert.assertNotSame(lr, lr4);
97-
Assert.assertNotSame(lr3, lr4);
100+
assertNotSame(lr, lr4);
101+
assertNotSame(lr3, lr4);
98102
}
99103

100104
@Test
@@ -122,7 +126,7 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
122126
Thread.sleep(50);
123127
}
124128
if (leaseRenewalCount.get() == 0) {
125-
Assert.fail("Did not renew lease at all!");
129+
fail("Did not renew lease at all!");
126130
}
127131

128132
renewer.closeClient(MOCK_DFSCLIENT);
@@ -176,23 +180,21 @@ public Boolean get() {
176180

177181
// Make sure renewer is not running due to expiration.
178182
Thread.sleep(FAST_GRACE_PERIOD * 2);
179-
Assert.assertTrue(!renewer.isRunning());
183+
assertTrue(!renewer.isRunning());
180184
}
181185

182186
@Test
183187
public void testThreadName() throws Exception {
184-
Assert.assertFalse("Renewer not initially running",
185-
renewer.isRunning());
188+
assertFalse(renewer.isRunning(), "Renewer not initially running");
186189

187190
// Pretend to open a file
188191
renewer.put(MOCK_DFSCLIENT);
189192

190-
Assert.assertTrue("Renewer should have started running",
191-
renewer.isRunning());
193+
assertTrue(renewer.isRunning(), "Renewer should have started running");
192194

193195
// Check the thread name is reasonable
194196
String threadName = renewer.getDaemonName();
195-
Assert.assertEquals("LeaseRenewer:myuser@hdfs://nn1/", threadName);
197+
assertEquals("LeaseRenewer:myuser@hdfs://nn1/", threadName);
196198

197199
// Pretend to close the file
198200
renewer.closeClient(MOCK_DFSCLIENT);
@@ -203,7 +205,7 @@ public void testThreadName() throws Exception {
203205
while (renewer.isRunning() && Time.monotonicNow() < failTime) {
204206
Thread.sleep(50);
205207
}
206-
Assert.assertFalse(renewer.isRunning());
208+
assertFalse(renewer.isRunning());
207209
}
208210

209211
/**
@@ -213,24 +215,23 @@ public void testThreadName() throws Exception {
213215
*/
214216
@Test
215217
public void testDaemonThreadLeak() throws Exception {
216-
Assert.assertFalse("Renewer not initially running", renewer.isRunning());
218+
assertFalse(renewer.isRunning(), "Renewer not initially running");
217219

218220
// Pretend to create a file#1, daemon#1 starts
219221
renewer.put(MOCK_DFSCLIENT);
220-
Assert.assertTrue("Renewer should have started running",
221-
renewer.isRunning());
222+
assertTrue(renewer.isRunning(), "Renewer should have started running");
222223
Pattern daemonThreadNamePattern = Pattern.compile("LeaseRenewer:\\S+");
223-
Assert.assertEquals(1, countThreadMatching(daemonThreadNamePattern));
224+
assertEquals(1, countThreadMatching(daemonThreadNamePattern));
224225

225226
// Pretend to create file#2, daemon#2 starts due to expiration
226227
LeaseRenewer lastRenewer = renewer;
227228
renewer =
228229
LeaseRenewer.getInstance(FAKE_AUTHORITY, FAKE_UGI_A, MOCK_DFSCLIENT);
229-
Assert.assertEquals(lastRenewer, renewer);
230+
assertEquals(lastRenewer, renewer);
230231

231232
// Pretend to close file#1
232233
renewer.closeClient(MOCK_DFSCLIENT);
233-
Assert.assertEquals(1, countThreadMatching(daemonThreadNamePattern));
234+
assertEquals(1, countThreadMatching(daemonThreadNamePattern));
234235

235236
// Pretend to be expired
236237
renewer.setEmptyTime(0);
@@ -249,7 +250,7 @@ public void testDaemonThreadLeak() throws Exception {
249250

250251
int threadCount = countThreadMatching(daemonThreadNamePattern);
251252
//Sometimes old LR#Daemon gets closed and lead to count 1 (rare scenario)
252-
Assert.assertTrue(1 == threadCount || 2 == threadCount);
253+
assertTrue(1 == threadCount || 2 == threadCount);
253254

254255
// After grace period, both daemon#1 and renewer#1 will be removed due to
255256
// expiration, then daemon#2 will leak before HDFS-14575.
@@ -259,14 +260,14 @@ public void testDaemonThreadLeak() throws Exception {
259260
lastRenewer = renewer;
260261
renewer =
261262
LeaseRenewer.getInstance(FAKE_AUTHORITY, FAKE_UGI_A, MOCK_DFSCLIENT);
262-
Assert.assertEquals(lastRenewer, renewer);
263+
assertEquals(lastRenewer, renewer);
263264
renewer.setGraceSleepPeriod(FAST_GRACE_PERIOD);
264265
renewer.closeClient(MOCK_DFSCLIENT);
265266
renewer.setEmptyTime(0);
266267
// Make sure LeaseRenewer#daemon threads will terminate after grace period
267268
Thread.sleep(FAST_GRACE_PERIOD * 2);
268-
Assert.assertEquals("LeaseRenewer#daemon thread leaks", 0,
269-
countThreadMatching(daemonThreadNamePattern));
269+
assertEquals(0, countThreadMatching(daemonThreadNamePattern),
270+
"LeaseRenewer#daemon thread leaks");
270271
}
271272

272273
private static int countThreadMatching(Pattern pattern) {

hadoop-hdfs-project/hadoop-hdfs-client/src/test/java/org/apache/hadoop/hdfs/protocol/TestBlockType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
*/
1818
package org.apache.hadoop.hdfs.protocol;
1919

20-
import org.junit.Test;
20+
import org.junit.jupiter.api.Test;
2121

2222
import static org.apache.hadoop.hdfs.protocol.BlockType.CONTIGUOUS;
2323
import static org.apache.hadoop.hdfs.protocol.BlockType.STRIPED;
24-
import static org.junit.Assert.*;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
2525

2626
/**
2727
* Test the BlockType class.

0 commit comments

Comments
 (0)