Skip to content

HADOOP-19419. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-registry. #7624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions hadoop-common-project/hadoop-registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,32 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.hadoop.registry.client.types.ServiceRecord;

import org.apache.hadoop.registry.server.services.RegistryAdminService;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,7 +39,7 @@ public class AbstractRegistryTest extends AbstractZKRegistryTest {
protected RegistryAdminService registry;
protected RegistryOperations operations;

@Before
@BeforeEach
public void setupRegistry() throws IOException {
registry = new RegistryAdminService("yarnRegistry");
operations = registry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
import org.apache.hadoop.registry.server.services.AddingCompositeService;
import org.apache.hadoop.registry.server.services.MicroZookeeperService;
import org.apache.hadoop.registry.server.services.MicroZookeeperServiceKeys;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

@Timeout(10)
public class AbstractZKRegistryTest extends RegistryTestHelper {
private static final Logger LOG =
LoggerFactory.getLogger(AbstractZKRegistryTest.class);
Expand All @@ -51,17 +50,11 @@ public class AbstractZKRegistryTest extends RegistryTestHelper {
servicesToTeardown.start();
}

@Rule
public final Timeout testTimeout = new Timeout(10000);

@Rule
public TestName methodName = new TestName();

protected static void addToTeardown(Service svc) {
servicesToTeardown.addService(svc);
}

@AfterClass
@AfterAll
public static void teardownServices() throws IOException {
describe(LOG, "teardown of static services");
servicesToTeardown.close();
Expand All @@ -70,7 +63,7 @@ public static void teardownServices() throws IOException {
protected static MicroZookeeperService zookeeper;


@BeforeClass
@BeforeAll
public static void createZKServer() throws Exception {
File zkDir = new File("target/zookeeper");
FileUtils.deleteDirectory(zkDir);
Expand All @@ -86,7 +79,7 @@ public static void createZKServer() throws Exception {
/**
* give our thread a name
*/
@Before
@BeforeEach
public void nameThread() {
Thread.currentThread().setName("JUnit");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.hadoop.registry.client.types.yarn.YarnRegistryAttributes;
import org.apache.hadoop.registry.secure.AbstractSecureRegistryTest;
import org.apache.zookeeper.common.PathUtils;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -52,7 +52,7 @@
* The methods can be imported statically —or the class used as a base
* class for tests.
*/
public class RegistryTestHelper extends Assert {
public class RegistryTestHelper extends Assertions {
public static final String SC_HADOOP = "org-apache-hadoop";
public static final String USER = "devteam/";
public static final String NAME = "hdfs";
Expand Down Expand Up @@ -109,7 +109,7 @@ public static void assertNotEmpty(String check) {
*/
public static void logLoginDetails(String name,
LoginContext loginContext) {
assertNotNull("Null login context", loginContext);
assertNotNull(loginContext, "Null login context");
Subject subject = loginContext.getSubject();
LOG.info("Logged in as {}:\n {}", name, subject);
}
Expand All @@ -135,7 +135,7 @@ public static void disableKerberosDebugging() {
* @param record instance to check
*/
public static void validateEntry(ServiceRecord record) {
assertNotNull("null service record", record);
assertNotNull(record, "null service record");
List<Endpoint> endpoints = record.external;
assertEquals(2, endpoints.size());

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

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

Endpoint ipc2 = findEndpoint(record, IPC2, false, 1,2);
assertNotNull(ipc2);
Expand Down Expand Up @@ -184,26 +184,25 @@ public static void assertMatches(Endpoint endpoint,
* @param resolved the one that resolved.
*/
public static void assertMatches(ServiceRecord source, ServiceRecord resolved) {
assertNotNull("Null source record ", source);
assertNotNull("Null resolved record ", resolved);
assertNotNull(source, "Null source record ");
assertNotNull(resolved, "Null resolved record ");
assertEquals(source.description, resolved.description);

Map<String, String> srcAttrs = source.attributes();
Map<String, String> resolvedAttrs = resolved.attributes();
String sourceAsString = source.toString();
String resolvedAsString = resolved.toString();
assertEquals("Wrong count of attrs in \n" + sourceAsString
+ "\nfrom\n" + resolvedAsString,
srcAttrs.size(),
resolvedAttrs.size());
assertEquals(srcAttrs.size(),
resolvedAttrs.size(), "Wrong count of attrs in \n" + sourceAsString
+ "\nfrom\n" + resolvedAsString);
for (Map.Entry<String, String> entry : srcAttrs.entrySet()) {
String attr = entry.getKey();
assertEquals("attribute "+ attr, entry.getValue(), resolved.get(attr));
assertEquals(entry.getValue(), resolved.get(attr), "attribute "+ attr);
}
assertEquals("wrong external endpoint count",
source.external.size(), resolved.external.size());
assertEquals("wrong external endpoint count",
source.internal.size(), resolved.internal.size());
assertEquals(source.external.size(), resolved.external.size(),
"wrong external endpoint count");
assertEquals(source.internal.size(), resolved.internal.size(),
"wrong external endpoint count");
}

/**
Expand All @@ -220,10 +219,10 @@ public static Endpoint findEndpoint(ServiceRecord record,
Endpoint epr = external ? record.getExternalEndpoint(api)
: record.getInternalEndpoint(api);
if (epr != null) {
assertEquals("wrong # of addresses",
addressElements, epr.addresses.size());
assertEquals("wrong # of elements in an address tuple",
addressTupleSize, epr.addresses.get(0).size());
assertEquals(addressElements, epr.addresses.size(),
"wrong # of addresses");
assertEquals(addressTupleSize, epr.addresses.get(0).size(),
"wrong # of elements in an address tuple");
return epr;
}
List<Endpoint> endpoints = external ? record.external : record.internal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

import org.apache.hadoop.registry.AbstractRegistryTest;
import org.apache.hadoop.registry.operations.TestRegistryOperations;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -38,7 +38,7 @@ public class TestRegistryCli extends AbstractRegistryTest {
private PrintStream sysErr;
private RegistryCli cli;

@Before
@BeforeEach
public void setUp() throws Exception {
sysOutStream = new ByteArrayOutputStream();
sysOut = new PrintStream(sysOutStream);
Expand All @@ -48,7 +48,7 @@ public void setUp() throws Exception {
cli = new RegistryCli(operations, createRegistryConfiguration(), sysOut, sysErr);
}

@After
@AfterEach
public void tearDown() throws Exception {
cli.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,23 @@
import org.apache.hadoop.registry.client.exceptions.NoRecordException;
import org.apache.hadoop.registry.client.types.ServiceRecord;
import org.apache.hadoop.registry.client.types.yarn.PersistencePolicies;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Test record marshalling
*/
@Timeout(10)
public class TestMarshalling extends RegistryTestHelper {
private static final Logger
LOG = LoggerFactory.getLogger(TestMarshalling.class);

@Rule
public final Timeout testTimeout = new Timeout(10000);
@Rule
public TestName methodName = new TestName();

private static RegistryUtils.ServiceRecordMarshal marshal;

@BeforeClass
@BeforeAll
public static void setupClass() {
marshal = new RegistryUtils.ServiceRecordMarshal();
}
Expand All @@ -65,53 +59,67 @@ public void testRoundTrip() throws Throwable {
}


@Test(expected = NoRecordException.class)
@Test
public void testUnmarshallNoData() throws Throwable {
marshal.fromBytes("src", new byte[]{});
assertThrows(NoRecordException.class, () -> {
marshal.fromBytes("src", new byte[]{});
});
}

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

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

@Test(expected = InvalidRecordException.class)
@Test
public void testUnmarshallWrongType() throws Throwable {
byte[] bytes = "{'type':''}".getBytes();
ServiceRecord serviceRecord = marshal.fromBytes("marshalling", bytes);
RegistryTypeUtils.validateServiceRecord("validating", serviceRecord);
assertThrows(InvalidRecordException.class, () -> {
byte[] bytes = "{'type':''}".getBytes();
ServiceRecord serviceRecord = marshal.fromBytes("marshalling", bytes);
RegistryTypeUtils.validateServiceRecord("validating", serviceRecord);
});
}

@Test(expected = NoRecordException.class)
@Test
public void testUnmarshallWrongLongType() throws Throwable {
ServiceRecord record = new ServiceRecord();
record.type = "ThisRecordHasALongButNonMatchingType";
byte[] bytes = marshal.toBytes(record);
ServiceRecord serviceRecord = marshal.fromBytes("marshalling",
bytes, ServiceRecord.RECORD_TYPE);
assertThrows(NoRecordException.class, () -> {
ServiceRecord record = new ServiceRecord();
record.type = "ThisRecordHasALongButNonMatchingType";
byte[] bytes = marshal.toBytes(record);
ServiceRecord serviceRecord = marshal.fromBytes("marshalling",
bytes, ServiceRecord.RECORD_TYPE);
});
}

@Test(expected = NoRecordException.class)
@Test
public void testUnmarshallNoType() throws Throwable {
ServiceRecord record = new ServiceRecord();
record.type = "NoRecord";
byte[] bytes = marshal.toBytes(record);
ServiceRecord serviceRecord = marshal.fromBytes("marshalling",
bytes, ServiceRecord.RECORD_TYPE);
assertThrows(NoRecordException.class, () -> {
ServiceRecord record = new ServiceRecord();
record.type = "NoRecord";
byte[] bytes = marshal.toBytes(record);
ServiceRecord serviceRecord = marshal.fromBytes("marshalling",
bytes, ServiceRecord.RECORD_TYPE);
});
}

@Test(expected = InvalidRecordException.class)
@Test
public void testRecordValidationWrongType() throws Throwable {
ServiceRecord record = new ServiceRecord();
record.type = "NotAServiceRecordType";
RegistryTypeUtils.validateServiceRecord("validating", record);
assertThrows(InvalidRecordException.class, () -> {
ServiceRecord record = new ServiceRecord();
record.type = "NotAServiceRecordType";
RegistryTypeUtils.validateServiceRecord("validating", record);
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@
package org.apache.hadoop.registry.client.binding;

import org.apache.hadoop.security.UserGroupInformation;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Tests for the {@link RegistryUtils} class
*/
public class TestRegistryOperationUtils extends Assert {
public class TestRegistryOperationUtils extends Assertions {

@Test
public void testUsernameExtractionEnvVarOverrride() throws Throwable {
String whoami = RegistryUtils.getCurrentUsernameUnencoded("drwho");
assertEquals("drwho", whoami);

}

@Test
Expand Down
Loading