Skip to content

Commit c21f9bd

Browse files
zhtttylzcnauroth
authored andcommitted
HDFS-12431. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-hdfs Part9.
Closes #7818 Signed-off-by: Chris Nauroth <[email protected]>
1 parent 1539e85 commit c21f9bd

File tree

50 files changed

+1092
-1087
lines changed

Some content is hidden

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

50 files changed

+1092
-1087
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBPOfferService.java

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
import static org.apache.hadoop.test.MetricsAsserts.assertCounter;
3131
import static org.apache.hadoop.test.MetricsAsserts.getLongCounter;
3232
import static org.apache.hadoop.test.MetricsAsserts.getMetrics;
33-
import static org.junit.Assert.assertEquals;
34-
import static org.junit.Assert.assertFalse;
35-
import static org.junit.Assert.assertNull;
36-
import static org.junit.Assert.assertNotNull;
37-
import static org.junit.Assert.assertSame;
38-
import static org.junit.Assert.assertTrue;
39-
import static org.junit.Assert.fail;
33+
import static org.junit.jupiter.api.Assertions.assertEquals;
34+
import static org.junit.jupiter.api.Assertions.assertFalse;
35+
import static org.junit.jupiter.api.Assertions.assertNull;
36+
import static org.junit.jupiter.api.Assertions.assertNotNull;
37+
import static org.junit.jupiter.api.Assertions.assertSame;
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
import static org.junit.jupiter.api.Assertions.fail;
4040

4141
import java.io.File;
4242
import java.io.IOException;
@@ -87,11 +87,11 @@
8787
import org.apache.hadoop.test.PathUtils;
8888
import org.apache.hadoop.util.Lists;
8989
import org.apache.hadoop.util.Time;
90-
import org.junit.Before;
91-
import org.junit.After;
92-
import org.junit.Rule;
93-
import org.junit.Test;
94-
import org.junit.rules.TemporaryFolder;
90+
import org.junit.jupiter.api.BeforeEach;
91+
import org.junit.jupiter.api.AfterEach;
92+
import org.junit.jupiter.api.Test;
93+
import org.junit.jupiter.api.Timeout;
94+
import org.junit.jupiter.api.io.TempDir;
9595
import org.mockito.ArgumentCaptor;
9696
import org.mockito.Mockito;
9797
import org.mockito.invocation.InvocationOnMock;
@@ -122,8 +122,9 @@ public class TestBPOfferService {
122122
GenericTestUtils.setLogLevel(DataNode.LOG, Level.TRACE);
123123
}
124124

125-
@Rule
126-
public TemporaryFolder baseDir = new TemporaryFolder();
125+
@SuppressWarnings("checkstyle:VisibilityModifier")
126+
@TempDir
127+
java.nio.file.Path baseDir;
127128

128129
private DatanodeProtocolClientSideTranslatorPB mockNN1;
129130
private DatanodeProtocolClientSideTranslatorPB mockNN2;
@@ -138,7 +139,7 @@ public class TestBPOfferService {
138139
private boolean isSlownode;
139140
private String mockStorageID;
140141

141-
@Before
142+
@BeforeEach
142143
public void setupMocks() throws Exception {
143144
mockNN1 = setupNNMock(0);
144145
mockNN2 = setupNNMock(1);
@@ -166,7 +167,7 @@ public void setupMocks() throws Exception {
166167
Mockito.doReturn(dataSetLockManager).when(mockDn).getDataSetLockManager();
167168
}
168169

169-
@After
170+
@AfterEach
170171
public void checkDataSetLockManager() {
171172
dataSetLockManager.lockLeakCheck();
172173
// make sure no lock Leak.
@@ -831,9 +832,8 @@ public void testReportBadBlockWhenStandbyNNTimesOut() throws Exception {
831832
.getStorageType());
832833
Thread.sleep(10000);
833834
long difference = secondCallTime - firstCallTime;
834-
assertTrue("Active namenode reportBadBlock processing should be "
835-
+ "independent of standby namenode reportBadBlock processing ",
836-
difference < 5000);
835+
assertTrue(difference < 5000, "Active namenode reportBadBlock processing should be "
836+
+ "independent of standby namenode reportBadBlock processing ");
837837
} finally {
838838
bpos.stop();
839839
bpos.join();
@@ -871,9 +871,9 @@ public void testTrySendErrorReportWhenStandbyNNTimesOut() throws Exception {
871871
bpos.trySendErrorReport(DatanodeProtocol.INVALID_BLOCK, errorString);
872872
Thread.sleep(10000);
873873
long difference = secondCallTime - firstCallTime;
874-
assertTrue("Active namenode trySendErrorReport processing "
874+
assertTrue(difference < 5000, "Active namenode trySendErrorReport processing "
875875
+ "should be independent of standby namenode trySendErrorReport"
876-
+ " processing ", difference < 5000);
876+
+ " processing ");
877877
} finally {
878878
bpos.stop();
879879
bpos.join();
@@ -911,8 +911,8 @@ public void testTrySendErrorReportWhenNNThrowsIOException()
911911
String errorString = "Can't send invalid block " + FAKE_BLOCK;
912912
bpos.trySendErrorReport(DatanodeProtocol.INVALID_BLOCK, errorString);
913913
GenericTestUtils.waitFor(() -> secondCallTime != 0, 100, 20000);
914-
assertTrue("Active namenode didn't add the report back to the queue "
915-
+ "when errorReport threw IOException", secondCallTime != 0);
914+
assertTrue(secondCallTime != 0, "Active namenode didn't add the report back to the queue "
915+
+ "when errorReport threw IOException");
916916
} finally {
917917
bpos.stop();
918918
bpos.join();
@@ -1025,9 +1025,7 @@ public Boolean get() {
10251025
// Send register command back to Datanode to reRegister().
10261026
// After reRegister IBRs should be cleared.
10271027
datanodeCommands[1] = new DatanodeCommand[] { new RegisterCommand() };
1028-
assertEquals(
1029-
"IBR size before reRegister should be non-0", 1, getStandbyIBRSize(
1030-
bpos));
1028+
assertEquals(1, getStandbyIBRSize(bpos), "IBR size before reRegister should be non-0");
10311029
bpos.triggerHeartbeatForTests();
10321030
GenericTestUtils.waitFor(new Supplier<Boolean>() {
10331031
@Override
@@ -1082,7 +1080,8 @@ public void testNNHAStateUpdateFromVersionRequest() throws Exception {
10821080

10831081
}
10841082

1085-
@Test(timeout = 30000)
1083+
@Test
1084+
@Timeout(value = 30)
10861085
public void testRefreshNameNodes() throws Exception {
10871086

10881087
BPOfferService bpos = setupBPOSForNNs(mockDn, mockNN1, mockNN2);
@@ -1156,7 +1155,8 @@ public void testRefreshNameNodes() throws Exception {
11561155
}
11571156
}
11581157

1159-
@Test(timeout = 15000)
1158+
@Test
1159+
@Timeout(value = 15)
11601160
public void testRefreshLeaseId() throws Exception {
11611161
Mockito.when(mockNN1.sendHeartbeat(
11621162
Mockito.any(DatanodeRegistration.class),
@@ -1221,7 +1221,8 @@ public Object answer(InvocationOnMock invocation)
12211221
}
12221222
}
12231223

1224-
@Test(timeout = 15000)
1224+
@Test
1225+
@Timeout(value = 15)
12251226
public void testSetIsSlownode() throws Exception {
12261227
assertEquals(mockDn.isSlownode(), false);
12271228
Mockito.when(mockNN1.sendHeartbeat(
@@ -1259,10 +1260,11 @@ public void testSetIsSlownode() throws Exception {
12591260
}
12601261
}
12611262

1262-
@Test(timeout = 15000)
1263+
@Test
1264+
@Timeout(value = 15)
12631265
public void testCommandProcessingThread() throws Exception {
12641266
Configuration conf = new HdfsConfiguration();
1265-
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf, baseDir.getRoot()).build()) {
1267+
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf, baseDir.toFile()).build()) {
12661268
List<DataNode> datanodes = cluster.getDataNodes();
12671269
assertEquals(datanodes.size(), 1);
12681270
DataNode datanode = datanodes.get(0);
@@ -1273,19 +1275,20 @@ public void testCommandProcessingThread() throws Exception {
12731275
DFSTestUtil.createFile(fs, file, 10240L, (short)1, 0L);
12741276

12751277
MetricsRecordBuilder mrb = getMetrics(datanode.getMetrics().name());
1276-
assertTrue("Process command nums is not expected.",
1277-
getLongCounter("NumProcessedCommands", mrb) > 0);
1278+
assertTrue(getLongCounter("NumProcessedCommands", mrb) > 0,
1279+
"Process command nums is not expected.");
12781280
assertEquals(0, getLongCounter("SumOfActorCommandQueueLength", mrb));
12791281
// Check new metric result about processedCommandsOp.
12801282
// One command send back to DataNode here is #FinalizeCommand.
12811283
assertCounter("ProcessedCommandsOpNumOps", 1L, mrb);
12821284
}
12831285
}
12841286

1285-
@Test(timeout = 5000)
1287+
@Test
1288+
@Timeout(value = 5)
12861289
public void testCommandProcessingThreadExit() throws Exception {
12871290
Configuration conf = new HdfsConfiguration();
1288-
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf, baseDir.getRoot()).
1291+
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf, baseDir.toFile()).
12891292
numDataNodes(1).build()) {
12901293
List<DataNode> datanodes = cluster.getDataNodes();
12911294
DataNode dataNode = datanodes.get(0);

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBatchIbr.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
import org.apache.hadoop.test.MetricsAsserts;
4646
import org.apache.hadoop.util.Time;
4747
import org.slf4j.event.Level;
48-
import org.junit.Assert;
49-
import org.junit.Test;
48+
import org.junit.jupiter.api.Assertions;
49+
import org.junit.jupiter.api.Test;
5050

5151
/**
5252
* This test verifies that incremental block reports are sent in batch mode
@@ -161,7 +161,7 @@ public Boolean call() throws Exception {
161161
});
162162
}
163163
for(int i = 0; i < NUM_FILES; i++) {
164-
Assert.assertTrue(verifyService.take().get());
164+
Assertions.assertTrue(verifyService.take().get());
165165
}
166166
final long testEndTime = Time.monotonicNow();
167167

@@ -247,7 +247,7 @@ static boolean verifyFile(Path f, DistributedFileSystem dfs) {
247247
for(int i = 0; i < numBlocks; i++) {
248248
in.read(computed);
249249
nextBytes(i, seed, expected);
250-
Assert.assertArrayEquals(expected, computed);
250+
Assertions.assertArrayEquals(expected, computed);
251251
}
252252
return true;
253253
} catch(Exception e) {

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBlockCountersInPendingIBR.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.apache.hadoop.hdfs.server.protocol.ReceivedDeletedBlockInfo.BlockStatus;
3737
import org.apache.hadoop.metrics2.MetricsRecordBuilder;
3838
import org.apache.hadoop.test.MetricsAsserts;
39-
import org.junit.Test;
39+
import org.junit.jupiter.api.Test;
4040
import org.mockito.Mockito;
4141

4242
/**

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBlockHasMultipleReplicasOnSameDN.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@
4040
import org.apache.hadoop.hdfs.server.protocol.DatanodeStorage;
4141
import org.apache.hadoop.hdfs.server.protocol.StorageBlockReport;
4242
import org.apache.hadoop.test.GenericTestUtils;
43-
import org.junit.After;
44-
import org.junit.Before;
45-
import org.junit.Test;
43+
import org.junit.jupiter.api.AfterEach;
44+
import org.junit.jupiter.api.BeforeEach;
45+
import org.junit.jupiter.api.Test;
4646

47-
import static org.hamcrest.CoreMatchers.not;
48-
import static org.hamcrest.core.Is.is;
49-
import static org.junit.Assert.assertThat;
47+
import static org.assertj.core.api.Assertions.assertThat;
5048

5149
/**
5250
* This test verifies NameNode behavior when it gets unexpected block reports
@@ -68,7 +66,7 @@ public class TestBlockHasMultipleReplicasOnSameDN {
6866
private DFSClient client;
6967
private String bpid;
7068

71-
@Before
69+
@BeforeEach
7270
public void startUpCluster() throws IOException {
7371
conf = new HdfsConfiguration();
7472
cluster = new MiniDFSCluster.Builder(conf)
@@ -79,7 +77,7 @@ public void startUpCluster() throws IOException {
7977
bpid = cluster.getNamesystem().getBlockPoolId();
8078
}
8179

82-
@After
80+
@AfterEach
8381
public void shutDownCluster() throws IOException {
8482
if (cluster != null) {
8583
fs.close();
@@ -142,8 +140,8 @@ public void testBlockHasMultipleReplicasOnSameDN() throws IOException {
142140
// Make sure that each block has two replicas, one on each DataNode.
143141
for (LocatedBlock locatedBlock : locatedBlocks.getLocatedBlocks()) {
144142
DatanodeInfo[] locations = locatedBlock.getLocations();
145-
assertThat(locations.length, is((int) NUM_DATANODES));
146-
assertThat(locations[0].getDatanodeUuid(), not(locations[1].getDatanodeUuid()));
143+
assertThat(locations.length).isEqualTo((int) NUM_DATANODES);
144+
assertThat(locations[0].getDatanodeUuid()).isNotEqualTo(locations[1].getDatanodeUuid());
147145
}
148146
}
149147
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBlockPoolManager.java

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

20-
import static org.junit.Assert.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
2121

2222
import java.io.IOException;
2323
import java.net.InetSocketAddress;
@@ -31,9 +31,9 @@
3131
import org.apache.hadoop.conf.Configuration;
3232
import org.apache.hadoop.hdfs.DFSConfigKeys;
3333
import org.apache.hadoop.hdfs.DFSUtil;
34-
import org.junit.Assert;
35-
import org.junit.Before;
36-
import org.junit.Test;
34+
import org.junit.jupiter.api.Assertions;
35+
import org.junit.jupiter.api.BeforeEach;
36+
import org.junit.jupiter.api.Test;
3737
import org.mockito.Mockito;
3838
import org.mockito.invocation.InvocationOnMock;
3939
import org.mockito.stubbing.Answer;
@@ -47,7 +47,7 @@ public class TestBlockPoolManager {
4747
private final StringBuilder log = new StringBuilder();
4848
private int mockIdx = 1;
4949

50-
@Before
50+
@BeforeEach
5151
public void setupBPM() {
5252
bpm = new BlockPoolManager(mockDN){
5353

@@ -159,9 +159,9 @@ public void testInternalNameService() throws Exception {
159159
bpm.refreshNamenodes(conf);
160160
assertEquals("create #1\n", log.toString());
161161
Map<String, BPOfferService> map = bpm.getBpByNameserviceId();
162-
Assert.assertFalse(map.containsKey("ns2"));
163-
Assert.assertFalse(map.containsKey("ns3"));
164-
Assert.assertTrue(map.containsKey("ns1"));
162+
Assertions.assertFalse(map.containsKey("ns2"));
163+
Assertions.assertFalse(map.containsKey("ns3"));
164+
Assertions.assertTrue(map.containsKey("ns1"));
165165
log.setLength(0);
166166
}
167167

@@ -179,18 +179,18 @@ public void testNameServiceNeedToBeResolved() throws Exception {
179179
"create #2\n" +
180180
"create #3\n", log.toString());
181181
Map<String, BPOfferService> map = bpm.getBpByNameserviceId();
182-
Assert.assertTrue(map.containsKey("ns1"));
183-
Assert.assertTrue(map.containsKey("ns2"));
184-
Assert.assertTrue(map.containsKey("ns3"));
185-
Assert.assertEquals(2, map.get("ns3").getBPServiceActors().size());
186-
Assert.assertEquals("ns3-" + MockDomainNameResolver.FQDN_1 + "-8020",
182+
Assertions.assertTrue(map.containsKey("ns1"));
183+
Assertions.assertTrue(map.containsKey("ns2"));
184+
Assertions.assertTrue(map.containsKey("ns3"));
185+
Assertions.assertEquals(2, map.get("ns3").getBPServiceActors().size());
186+
Assertions.assertEquals("ns3-" + MockDomainNameResolver.FQDN_1 + "-8020",
187187
map.get("ns3").getBPServiceActors().get(0).getNnId());
188-
Assert.assertEquals("ns3-" + MockDomainNameResolver.FQDN_2 + "-8020",
188+
Assertions.assertEquals("ns3-" + MockDomainNameResolver.FQDN_2 + "-8020",
189189
map.get("ns3").getBPServiceActors().get(1).getNnId());
190-
Assert.assertEquals(
190+
Assertions.assertEquals(
191191
new InetSocketAddress(MockDomainNameResolver.FQDN_1, 8020),
192192
map.get("ns3").getBPServiceActors().get(0).getNNSocketAddress());
193-
Assert.assertEquals(
193+
Assertions.assertEquals(
194194
new InetSocketAddress(MockDomainNameResolver.FQDN_2, 8020),
195195
map.get("ns3").getBPServiceActors().get(1).getNNSocketAddress());
196196
log.setLength(0);

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestBlockPoolSliceStorage.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
package org.apache.hadoop.hdfs.server.datanode;
1919

2020
import org.apache.hadoop.hdfs.server.common.Storage;
21-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.Timeout;
2223
import org.mockito.Mockito;
2324

2425
import java.io.File;
@@ -27,8 +28,7 @@
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
2930

30-
import static org.hamcrest.core.Is.is;
31-
import static org.junit.Assert.assertThat;
31+
import static org.assertj.core.api.Assertions.assertThat;
3232

3333
/**
3434
* Test that BlockPoolSliceStorage can correctly generate trash and
@@ -52,7 +52,7 @@ private static class StubBlockPoolSliceStorage extends BlockPoolSliceStorage {
5252
String clusterId) {
5353
super(namespaceID, bpID, cTime, clusterId);
5454
addStorageDir(new StorageDirectory(new File("/tmp/dontcare/" + bpID)));
55-
assertThat(getStorageDirs().size(), is(1));
55+
assertThat(getStorageDirs().size()).isEqualTo(1);
5656
}
5757
}
5858

@@ -111,7 +111,7 @@ public void getTrashDirectoryForBlockFile(String fileName, int nestingLevel) {
111111

112112
ReplicaInfo info = Mockito.mock(ReplicaInfo.class);
113113
Mockito.when(info.getBlockURI()).thenReturn(new File(testFilePath).toURI());
114-
assertThat(storage.getTrashDirectory(info), is(expectedTrashPath));
114+
assertThat(storage.getTrashDirectory(info)).isEqualTo(expectedTrashPath);
115115
}
116116

117117
/*
@@ -134,12 +134,13 @@ public void getRestoreDirectoryForBlockFile(String fileName, int nestingLevel) {
134134
blockFileSubdir.substring(0, blockFileSubdir.length() - 1);
135135

136136
LOG.info("Generated deleted file path {}", deletedFilePath);
137-
assertThat(storage.getRestoreDirectory(new File(deletedFilePath)),
138-
is(expectedRestorePath));
137+
assertThat(storage.getRestoreDirectory(new File(deletedFilePath)))
138+
.isEqualTo(expectedRestorePath);
139139

140140
}
141141

142-
@Test (timeout=300000)
142+
@Test
143+
@Timeout(value = 300)
143144
public void testGetTrashAndRestoreDirectories() {
144145
storage = makeBlockPoolStorage();
145146

0 commit comments

Comments
 (0)