Skip to content

Commit b6682d3

Browse files
slfan1989cnauroth
andauthored
HADOOP-19419. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-registry. (#7624)
* HADOOP-19419. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-registry. Co-authored-by: Chris Nauroth <[email protected]> Reviewed-by: Chris Nauroth <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent 809b1b5 commit b6682d3

19 files changed

+379
-344
lines changed

hadoop-common-project/hadoop-registry/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,32 @@
146146
<scope>provided</scope>
147147
</dependency>
148148

149+
<dependency>
150+
<groupId>org.junit.jupiter</groupId>
151+
<artifactId>junit-jupiter-api</artifactId>
152+
<scope>test</scope>
153+
</dependency>
154+
<dependency>
155+
<groupId>org.junit.jupiter</groupId>
156+
<artifactId>junit-jupiter-params</artifactId>
157+
<scope>test</scope>
158+
</dependency>
159+
<dependency>
160+
<groupId>org.junit.jupiter</groupId>
161+
<artifactId>junit-jupiter-engine</artifactId>
162+
<scope>test</scope>
163+
</dependency>
164+
<dependency>
165+
<groupId>org.junit.vintage</groupId>
166+
<artifactId>junit-vintage-engine</artifactId>
167+
<scope>test</scope>
168+
</dependency>
169+
<dependency>
170+
<groupId>org.junit.platform</groupId>
171+
<artifactId>junit-platform-launcher</artifactId>
172+
<scope>test</scope>
173+
</dependency>
174+
149175
</dependencies>
150176

151177
<build>

hadoop-common-project/hadoop-registry/src/test/java/org/apache/hadoop/registry/AbstractRegistryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.apache.hadoop.registry.client.types.ServiceRecord;
2626

2727
import org.apache.hadoop.registry.server.services.RegistryAdminService;
28-
import org.junit.Before;
28+
import org.junit.jupiter.api.BeforeEach;
2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
3131

@@ -39,7 +39,7 @@ public class AbstractRegistryTest extends AbstractZKRegistryTest {
3939
protected RegistryAdminService registry;
4040
protected RegistryOperations operations;
4141

42-
@Before
42+
@BeforeEach
4343
public void setupRegistry() throws IOException {
4444
registry = new RegistryAdminService("yarnRegistry");
4545
operations = registry;

hadoop-common-project/hadoop-registry/src/test/java/org/apache/hadoop/registry/AbstractZKRegistryTest.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@
2626
import org.apache.hadoop.registry.server.services.AddingCompositeService;
2727
import org.apache.hadoop.registry.server.services.MicroZookeeperService;
2828
import org.apache.hadoop.registry.server.services.MicroZookeeperServiceKeys;
29-
import org.junit.AfterClass;
30-
import org.junit.Before;
31-
import org.junit.BeforeClass;
32-
import org.junit.Rule;
33-
import org.junit.rules.TestName;
34-
import org.junit.rules.Timeout;
29+
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.BeforeAll;
32+
import org.junit.jupiter.api.Timeout;
3533
import org.slf4j.Logger;
3634
import org.slf4j.LoggerFactory;
3735

3836
import java.io.File;
3937
import java.io.IOException;
4038

39+
@Timeout(10)
4140
public class AbstractZKRegistryTest extends RegistryTestHelper {
4241
private static final Logger LOG =
4342
LoggerFactory.getLogger(AbstractZKRegistryTest.class);
@@ -51,17 +50,11 @@ public class AbstractZKRegistryTest extends RegistryTestHelper {
5150
servicesToTeardown.start();
5251
}
5352

54-
@Rule
55-
public final Timeout testTimeout = new Timeout(10000);
56-
57-
@Rule
58-
public TestName methodName = new TestName();
59-
6053
protected static void addToTeardown(Service svc) {
6154
servicesToTeardown.addService(svc);
6255
}
6356

64-
@AfterClass
57+
@AfterAll
6558
public static void teardownServices() throws IOException {
6659
describe(LOG, "teardown of static services");
6760
servicesToTeardown.close();
@@ -70,7 +63,7 @@ public static void teardownServices() throws IOException {
7063
protected static MicroZookeeperService zookeeper;
7164

7265

73-
@BeforeClass
66+
@BeforeAll
7467
public static void createZKServer() throws Exception {
7568
File zkDir = new File("target/zookeeper");
7669
FileUtils.deleteDirectory(zkDir);
@@ -86,7 +79,7 @@ public static void createZKServer() throws Exception {
8679
/**
8780
* give our thread a name
8881
*/
89-
@Before
82+
@BeforeEach
9083
public void nameThread() {
9184
Thread.currentThread().setName("JUnit");
9285
}

hadoop-common-project/hadoop-registry/src/test/java/org/apache/hadoop/registry/RegistryTestHelper.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.apache.hadoop.registry.client.types.yarn.YarnRegistryAttributes;
3131
import org.apache.hadoop.registry.secure.AbstractSecureRegistryTest;
3232
import org.apache.zookeeper.common.PathUtils;
33-
import org.junit.Assert;
33+
import org.junit.jupiter.api.Assertions;
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

@@ -52,7 +52,7 @@
5252
* The methods can be imported statically —or the class used as a base
5353
* class for tests.
5454
*/
55-
public class RegistryTestHelper extends Assert {
55+
public class RegistryTestHelper extends Assertions {
5656
public static final String SC_HADOOP = "org-apache-hadoop";
5757
public static final String USER = "devteam/";
5858
public static final String NAME = "hdfs";
@@ -109,7 +109,7 @@ public static void assertNotEmpty(String check) {
109109
*/
110110
public static void logLoginDetails(String name,
111111
LoginContext loginContext) {
112-
assertNotNull("Null login context", loginContext);
112+
assertNotNull(loginContext, "Null login context");
113113
Subject subject = loginContext.getSubject();
114114
LOG.info("Logged in as {}:\n {}", name, subject);
115115
}
@@ -135,7 +135,7 @@ public static void disableKerberosDebugging() {
135135
* @param record instance to check
136136
*/
137137
public static void validateEntry(ServiceRecord record) {
138-
assertNotNull("null service record", record);
138+
assertNotNull(record, "null service record");
139139
List<Endpoint> endpoints = record.external;
140140
assertEquals(2, endpoints.size());
141141

@@ -150,8 +150,8 @@ public static void validateEntry(ServiceRecord record) {
150150
assertTrue(addr.contains(":8020"));
151151

152152
Endpoint nnipc = findEndpoint(record, NNIPC, false, 1,2);
153-
assertEquals("wrong protocol in " + nnipc, ProtocolTypes.PROTOCOL_THRIFT,
154-
nnipc.protocolType);
153+
assertEquals(ProtocolTypes.PROTOCOL_THRIFT,
154+
nnipc.protocolType, "wrong protocol in " + nnipc);
155155

156156
Endpoint ipc2 = findEndpoint(record, IPC2, false, 1,2);
157157
assertNotNull(ipc2);
@@ -184,26 +184,25 @@ public static void assertMatches(Endpoint endpoint,
184184
* @param resolved the one that resolved.
185185
*/
186186
public static void assertMatches(ServiceRecord source, ServiceRecord resolved) {
187-
assertNotNull("Null source record ", source);
188-
assertNotNull("Null resolved record ", resolved);
187+
assertNotNull(source, "Null source record ");
188+
assertNotNull(resolved, "Null resolved record ");
189189
assertEquals(source.description, resolved.description);
190190

191191
Map<String, String> srcAttrs = source.attributes();
192192
Map<String, String> resolvedAttrs = resolved.attributes();
193193
String sourceAsString = source.toString();
194194
String resolvedAsString = resolved.toString();
195-
assertEquals("Wrong count of attrs in \n" + sourceAsString
196-
+ "\nfrom\n" + resolvedAsString,
197-
srcAttrs.size(),
198-
resolvedAttrs.size());
195+
assertEquals(srcAttrs.size(),
196+
resolvedAttrs.size(), "Wrong count of attrs in \n" + sourceAsString
197+
+ "\nfrom\n" + resolvedAsString);
199198
for (Map.Entry<String, String> entry : srcAttrs.entrySet()) {
200199
String attr = entry.getKey();
201-
assertEquals("attribute "+ attr, entry.getValue(), resolved.get(attr));
200+
assertEquals(entry.getValue(), resolved.get(attr), "attribute "+ attr);
202201
}
203-
assertEquals("wrong external endpoint count",
204-
source.external.size(), resolved.external.size());
205-
assertEquals("wrong external endpoint count",
206-
source.internal.size(), resolved.internal.size());
202+
assertEquals(source.external.size(), resolved.external.size(),
203+
"wrong external endpoint count");
204+
assertEquals(source.internal.size(), resolved.internal.size(),
205+
"wrong external endpoint count");
207206
}
208207

209208
/**
@@ -220,10 +219,10 @@ public static Endpoint findEndpoint(ServiceRecord record,
220219
Endpoint epr = external ? record.getExternalEndpoint(api)
221220
: record.getInternalEndpoint(api);
222221
if (epr != null) {
223-
assertEquals("wrong # of addresses",
224-
addressElements, epr.addresses.size());
225-
assertEquals("wrong # of elements in an address tuple",
226-
addressTupleSize, epr.addresses.get(0).size());
222+
assertEquals(addressElements, epr.addresses.size(),
223+
"wrong # of addresses");
224+
assertEquals(addressTupleSize, epr.addresses.get(0).size(),
225+
"wrong # of elements in an address tuple");
227226
return epr;
228227
}
229228
List<Endpoint> endpoints = external ? record.external : record.internal;

hadoop-common-project/hadoop-registry/src/test/java/org/apache/hadoop/registry/cli/TestRegistryCli.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
import org.apache.hadoop.registry.AbstractRegistryTest;
2424
import org.apache.hadoop.registry.operations.TestRegistryOperations;
25-
import org.junit.After;
26-
import org.junit.Before;
27-
import org.junit.Test;
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
3030

@@ -38,7 +38,7 @@ public class TestRegistryCli extends AbstractRegistryTest {
3838
private PrintStream sysErr;
3939
private RegistryCli cli;
4040

41-
@Before
41+
@BeforeEach
4242
public void setUp() throws Exception {
4343
sysOutStream = new ByteArrayOutputStream();
4444
sysOut = new PrintStream(sysOutStream);
@@ -48,7 +48,7 @@ public void setUp() throws Exception {
4848
cli = new RegistryCli(operations, createRegistryConfiguration(), sysOut, sysErr);
4949
}
5050

51-
@After
51+
@AfterEach
5252
public void tearDown() throws Exception {
5353
cli.close();
5454
}

hadoop-common-project/hadoop-registry/src/test/java/org/apache/hadoop/registry/client/binding/TestMarshalling.java

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,23 @@
2323
import org.apache.hadoop.registry.client.exceptions.NoRecordException;
2424
import org.apache.hadoop.registry.client.types.ServiceRecord;
2525
import org.apache.hadoop.registry.client.types.yarn.PersistencePolicies;
26-
import org.junit.BeforeClass;
27-
import org.junit.Rule;
28-
import org.junit.Test;
29-
import org.junit.rules.TestName;
30-
import org.junit.rules.Timeout;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.Timeout;
3129
import org.slf4j.Logger;
3230
import org.slf4j.LoggerFactory;
3331

3432
/**
3533
* Test record marshalling
3634
*/
35+
@Timeout(10)
3736
public class TestMarshalling extends RegistryTestHelper {
3837
private static final Logger
3938
LOG = LoggerFactory.getLogger(TestMarshalling.class);
4039

41-
@Rule
42-
public final Timeout testTimeout = new Timeout(10000);
43-
@Rule
44-
public TestName methodName = new TestName();
45-
4640
private static RegistryUtils.ServiceRecordMarshal marshal;
4741

48-
@BeforeClass
42+
@BeforeAll
4943
public static void setupClass() {
5044
marshal = new RegistryUtils.ServiceRecordMarshal();
5145
}
@@ -65,53 +59,67 @@ public void testRoundTrip() throws Throwable {
6559
}
6660

6761

68-
@Test(expected = NoRecordException.class)
62+
@Test
6963
public void testUnmarshallNoData() throws Throwable {
70-
marshal.fromBytes("src", new byte[]{});
64+
assertThrows(NoRecordException.class, () -> {
65+
marshal.fromBytes("src", new byte[]{});
66+
});
7167
}
7268

73-
@Test(expected = NoRecordException.class)
69+
@Test
7470
public void testUnmarshallNotEnoughData() throws Throwable {
7571
// this is nominally JSON -but without the service record header
76-
marshal.fromBytes("src", new byte[]{'{','}'}, ServiceRecord.RECORD_TYPE);
72+
assertThrows(NoRecordException.class, () -> {
73+
marshal.fromBytes("src", new byte[]{'{', '}'}, ServiceRecord.RECORD_TYPE);
74+
});
7775
}
7876

79-
@Test(expected = InvalidRecordException.class)
77+
@Test
8078
public void testUnmarshallNoBody() throws Throwable {
81-
byte[] bytes = "this is not valid JSON at all and should fail".getBytes();
82-
marshal.fromBytes("src", bytes);
79+
assertThrows(InvalidRecordException.class, () -> {
80+
byte[] bytes = "this is not valid JSON at all and should fail".getBytes();
81+
marshal.fromBytes("src", bytes);
82+
});
8383
}
8484

85-
@Test(expected = InvalidRecordException.class)
85+
@Test
8686
public void testUnmarshallWrongType() throws Throwable {
87-
byte[] bytes = "{'type':''}".getBytes();
88-
ServiceRecord serviceRecord = marshal.fromBytes("marshalling", bytes);
89-
RegistryTypeUtils.validateServiceRecord("validating", serviceRecord);
87+
assertThrows(InvalidRecordException.class, () -> {
88+
byte[] bytes = "{'type':''}".getBytes();
89+
ServiceRecord serviceRecord = marshal.fromBytes("marshalling", bytes);
90+
RegistryTypeUtils.validateServiceRecord("validating", serviceRecord);
91+
});
9092
}
9193

92-
@Test(expected = NoRecordException.class)
94+
@Test
9395
public void testUnmarshallWrongLongType() throws Throwable {
94-
ServiceRecord record = new ServiceRecord();
95-
record.type = "ThisRecordHasALongButNonMatchingType";
96-
byte[] bytes = marshal.toBytes(record);
97-
ServiceRecord serviceRecord = marshal.fromBytes("marshalling",
98-
bytes, ServiceRecord.RECORD_TYPE);
96+
assertThrows(NoRecordException.class, () -> {
97+
ServiceRecord record = new ServiceRecord();
98+
record.type = "ThisRecordHasALongButNonMatchingType";
99+
byte[] bytes = marshal.toBytes(record);
100+
ServiceRecord serviceRecord = marshal.fromBytes("marshalling",
101+
bytes, ServiceRecord.RECORD_TYPE);
102+
});
99103
}
100104

101-
@Test(expected = NoRecordException.class)
105+
@Test
102106
public void testUnmarshallNoType() throws Throwable {
103-
ServiceRecord record = new ServiceRecord();
104-
record.type = "NoRecord";
105-
byte[] bytes = marshal.toBytes(record);
106-
ServiceRecord serviceRecord = marshal.fromBytes("marshalling",
107-
bytes, ServiceRecord.RECORD_TYPE);
107+
assertThrows(NoRecordException.class, () -> {
108+
ServiceRecord record = new ServiceRecord();
109+
record.type = "NoRecord";
110+
byte[] bytes = marshal.toBytes(record);
111+
ServiceRecord serviceRecord = marshal.fromBytes("marshalling",
112+
bytes, ServiceRecord.RECORD_TYPE);
113+
});
108114
}
109115

110-
@Test(expected = InvalidRecordException.class)
116+
@Test
111117
public void testRecordValidationWrongType() throws Throwable {
112-
ServiceRecord record = new ServiceRecord();
113-
record.type = "NotAServiceRecordType";
114-
RegistryTypeUtils.validateServiceRecord("validating", record);
118+
assertThrows(InvalidRecordException.class, () -> {
119+
ServiceRecord record = new ServiceRecord();
120+
record.type = "NotAServiceRecordType";
121+
RegistryTypeUtils.validateServiceRecord("validating", record);
122+
});
115123
}
116124

117125
@Test

hadoop-common-project/hadoop-registry/src/test/java/org/apache/hadoop/registry/client/binding/TestRegistryOperationUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@
1919
package org.apache.hadoop.registry.client.binding;
2020

2121
import org.apache.hadoop.security.UserGroupInformation;
22-
import org.junit.Assert;
23-
import org.junit.Test;
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
2424

2525
/**
2626
* Tests for the {@link RegistryUtils} class
2727
*/
28-
public class TestRegistryOperationUtils extends Assert {
28+
public class TestRegistryOperationUtils extends Assertions {
2929

3030
@Test
3131
public void testUsernameExtractionEnvVarOverrride() throws Throwable {
3232
String whoami = RegistryUtils.getCurrentUsernameUnencoded("drwho");
3333
assertEquals("drwho", whoami);
34-
3534
}
3635

3736
@Test

0 commit comments

Comments
 (0)