Skip to content

Commit e2095ed

Browse files
committed
HADOOP-19416. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-kms.
1 parent 7d474d3 commit e2095ed

File tree

7 files changed

+314
-324
lines changed

7 files changed

+314
-324
lines changed

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMS.java

+214-217
Large diffs are not rendered by default.

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSACLs.java

+33-36
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,21 @@
2424
import org.apache.hadoop.conf.Configuration;
2525
import org.apache.hadoop.security.UserGroupInformation;
2626
import org.apache.hadoop.security.authorize.AccessControlList;
27-
import org.junit.Assert;
28-
import org.junit.Rule;
29-
import org.junit.rules.Timeout;
30-
import org.junit.Test;
27+
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.Timeout;
3130

3231
import java.util.Collection;
3332
import java.util.HashMap;
3433
import java.util.HashSet;
3534

35+
@Timeout(180)
3636
public class TestKMSACLs {
37-
@Rule
38-
public final Timeout globalTimeout = new Timeout(180000);
39-
4037
@Test
4138
public void testDefaults() {
4239
final KMSACLs acls = new KMSACLs(new Configuration(false));
4340
for (KMSACLs.Type type : KMSACLs.Type.values()) {
44-
Assert.assertTrue(acls.hasAccess(type,
41+
Assertions.assertTrue(acls.hasAccess(type,
4542
UserGroupInformation.createRemoteUser("foo")));
4643
}
4744
}
@@ -54,9 +51,9 @@ public void testCustom() {
5451
}
5552
final KMSACLs acls = new KMSACLs(conf);
5653
for (KMSACLs.Type type : KMSACLs.Type.values()) {
57-
Assert.assertTrue(acls.hasAccess(type,
54+
Assertions.assertTrue(acls.hasAccess(type,
5855
UserGroupInformation.createRemoteUser(type.toString())));
59-
Assert.assertFalse(acls.hasAccess(type,
56+
Assertions.assertFalse(acls.hasAccess(type,
6057
UserGroupInformation.createRemoteUser("foo")));
6158
}
6259
}
@@ -72,16 +69,16 @@ public void testKeyAclConfigurationLoad() {
7269
conf.set(DEFAULT_KEY_ACL_PREFIX + "ALL", "invalid");
7370
conf.set(WHITELIST_KEY_ACL_PREFIX + "ALL", "invalid");
7471
final KMSACLs acls = new KMSACLs(conf);
75-
Assert.assertTrue("expected key ACL size is 2 but got "
76-
+ acls.keyAcls.size(), acls.keyAcls.size() == 2);
77-
Assert.assertTrue("expected whitelist ACL size is 1 but got "
78-
+ acls.whitelistKeyAcls.size(), acls.whitelistKeyAcls.size() == 1);
79-
Assert.assertFalse("ALL should not be allowed for whitelist ACLs.",
80-
acls.whitelistKeyAcls.containsKey(KeyOpType.ALL));
81-
Assert.assertTrue("expected default ACL size is 1 but got "
82-
+ acls.defaultKeyAcls.size(), acls.defaultKeyAcls.size() == 1);
83-
Assert.assertTrue("ALL should not be allowed for default ACLs.",
84-
acls.defaultKeyAcls.size() == 1);
72+
Assertions.assertTrue(acls.keyAcls.size() == 2, "expected key ACL size is 2 but got "
73+
+ acls.keyAcls.size());
74+
Assertions.assertTrue(acls.whitelistKeyAcls.size() == 1, "expected whitelist ACL size is 1 but got "
75+
+ acls.whitelistKeyAcls.size());
76+
Assertions.assertFalse(
77+
acls.whitelistKeyAcls.containsKey(KeyOpType.ALL), "ALL should not be allowed for whitelist ACLs.");
78+
Assertions.assertTrue(acls.defaultKeyAcls.size() == 1, "expected default ACL size is 1 but got "
79+
+ acls.defaultKeyAcls.size());
80+
Assertions.assertTrue(
81+
acls.defaultKeyAcls.size() == 1, "ALL should not be allowed for default ACLs.");
8582
}
8683

8784
@Test
@@ -98,15 +95,15 @@ public void testKeyAclDuplicateEntries() {
9895
conf.set(WHITELIST_KEY_ACL_PREFIX + "DECRYPT_EEK", "whitelist1");
9996
conf.set(WHITELIST_KEY_ACL_PREFIX + "DECRYPT_EEK", "*");
10097
final KMSACLs acls = new KMSACLs(conf);
101-
Assert.assertTrue("expected key ACL size is 2 but got "
102-
+ acls.keyAcls.size(), acls.keyAcls.size() == 2);
98+
Assertions.assertTrue(acls.keyAcls.size() == 2, "expected key ACL size is 2 but got "
99+
+ acls.keyAcls.size());
103100
assertKeyAcl("test_key_1", acls, KeyOpType.DECRYPT_EEK, "decrypt2");
104101
assertKeyAcl("test_key_2", acls, KeyOpType.ALL, "all1", "all3");
105102
assertDefaultKeyAcl(acls, KeyOpType.MANAGEMENT);
106103
assertDefaultKeyAcl(acls, KeyOpType.DECRYPT_EEK);
107104
AccessControlList acl = acls.whitelistKeyAcls.get(KeyOpType.DECRYPT_EEK);
108-
Assert.assertNotNull(acl);
109-
Assert.assertTrue(acl.isAllAllowed());
105+
Assertions.assertNotNull(acl);
106+
Assertions.assertTrue(acl.isAllAllowed());
110107
}
111108

112109
@Test
@@ -163,8 +160,8 @@ public void testKeyAclReload() {
163160
conf.set(DEFAULT_KEY_ACL_PREFIX + "DECRYPT_EEK", "*");
164161
acls.setKeyACLs(conf);
165162
AccessControlList acl = acls.defaultKeyAcls.get(KeyOpType.DECRYPT_EEK);
166-
Assert.assertTrue(acl.isAllAllowed());
167-
Assert.assertTrue(acl.getUsers().isEmpty());
163+
Assertions.assertTrue(acl.isAllAllowed());
164+
Assertions.assertTrue(acl.getUsers().isEmpty());
168165
// everything else should still be the same.
169166
assertDefaultKeyAcl(acls, KeyOpType.READ, "read2");
170167
assertDefaultKeyAcl(acls, KeyOpType.MANAGEMENT, "mgmt1", "mgmt2");
@@ -181,10 +178,10 @@ public void testKeyAclReload() {
181178
conf.set(DEFAULT_KEY_ACL_PREFIX + "DECRYPT_EEK", "new");
182179
acls.setKeyACLs(conf);
183180
assertDefaultKeyAcl(acls, KeyOpType.DECRYPT_EEK, "new");
184-
Assert.assertTrue(acls.keyAcls.isEmpty());
185-
Assert.assertTrue(acls.whitelistKeyAcls.isEmpty());
186-
Assert.assertEquals("Got unexpected sized acls:"
187-
+ acls.defaultKeyAcls, 1, acls.defaultKeyAcls.size());
181+
Assertions.assertTrue(acls.keyAcls.isEmpty());
182+
Assertions.assertTrue(acls.whitelistKeyAcls.isEmpty());
183+
Assertions.assertEquals(1, acls.defaultKeyAcls.size(), "Got unexpected sized acls:"
184+
+ acls.defaultKeyAcls);
188185
}
189186

190187
private void assertDefaultKeyAcl(final KMSACLs acls, final KeyOpType op,
@@ -201,23 +198,23 @@ private void assertWhitelistKeyAcl(final KMSACLs acls, final KeyOpType op,
201198

202199
private void assertKeyAcl(final String keyName, final KMSACLs acls,
203200
final KeyOpType op, final String... names) {
204-
Assert.assertTrue(acls.keyAcls.containsKey(keyName));
201+
Assertions.assertTrue(acls.keyAcls.containsKey(keyName));
205202
final HashMap<KeyOpType, AccessControlList> keyacl =
206203
acls.keyAcls.get(keyName);
207-
Assert.assertNotNull(keyacl.get(op));
204+
Assertions.assertNotNull(keyacl.get(op));
208205
assertAcl(keyacl.get(op), op, names);
209206
}
210207

211208
private void assertAcl(final AccessControlList acl,
212209
final KeyOpType op, final String... names) {
213-
Assert.assertNotNull(acl);
214-
Assert.assertFalse(acl.isAllAllowed());
210+
Assertions.assertNotNull(acl);
211+
Assertions.assertFalse(acl.isAllAllowed());
215212
final Collection<String> actual = acl.getUsers();
216213
final HashSet<String> expected = new HashSet<>();
217214
for (String name : names) {
218215
expected.add(name);
219216
}
220-
Assert.assertEquals("defaultKeyAcls don't match for op:" + op,
221-
expected, actual);
217+
Assertions.assertEquals(
218+
expected, actual, "defaultKeyAcls don't match for op:" + op);
222219
}
223220
}

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSAudit.java

+12-19
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@
3535
import org.apache.hadoop.util.ThreadUtil;
3636
import org.apache.log4j.LogManager;
3737
import org.apache.log4j.PropertyConfigurator;
38-
import org.junit.After;
39-
import org.junit.Assert;
40-
import org.junit.Before;
41-
import org.junit.Rule;
42-
import org.junit.Test;
43-
import org.junit.rules.Timeout;
38+
import org.junit.jupiter.api.*;
4439

40+
@Timeout(180)
4541
public class TestKMSAudit {
4642

4743
private PrintStream originalOut;
@@ -63,10 +59,7 @@ public void setOutputStream(OutputStream out) {
6359
}
6460
}
6561

66-
@Rule
67-
public final Timeout testTimeout = new Timeout(180000L, TimeUnit.MILLISECONDS);
68-
69-
@Before
62+
@BeforeEach
7063
public void setUp() throws IOException {
7164
originalOut = System.err;
7265
memOut = new ByteArrayOutputStream();
@@ -81,7 +74,7 @@ public void setUp() throws IOException {
8174
this.kmsAudit = new KMSAudit(conf);
8275
}
8376

84-
@After
77+
@AfterEach
8578
public void cleanUp() {
8679
System.setErr(originalOut);
8780
LogManager.resetConfiguration();
@@ -138,7 +131,7 @@ public void testAggregation() throws Exception {
138131
+ "OK\\[op=REENCRYPT_EEK_BATCH, key=k1, user=luser@REALM\\] testmsg"
139132
+ "OK\\[op=REENCRYPT_EEK_BATCH, key=k1, user=luser@REALM\\] "
140133
+ "testmsg");
141-
Assert.assertTrue(doesMatch);
134+
Assertions.assertTrue(doesMatch);
142135
}
143136

144137
@Test
@@ -179,7 +172,7 @@ public void testAggregationUnauth() throws Exception {
179172
+ " interval=[^m]{1,4}ms\\] testmsg"
180173
+ "OK\\[op=GENERATE_EEK, key=k3, user=luser@REALM, accessCount=1,"
181174
+ " interval=[^m]{1,4}ms\\] testmsg");
182-
Assert.assertTrue(doesMatch);
175+
Assertions.assertTrue(doesMatch);
183176
}
184177

185178
@Test
@@ -192,7 +185,7 @@ public void testAuditLogFormat() throws Exception {
192185
kmsAudit.unauthenticated("remotehost", "method", "url", "testmsg");
193186
String out = getAndResetLogOutput();
194187
System.out.println(out);
195-
Assert.assertTrue(out.matches(
188+
Assertions.assertTrue(out.matches(
196189
"OK\\[op=GENERATE_EEK, key=k4, user=luser@REALM, accessCount=1, "
197190
+ "interval=[^m]{1,4}ms\\] testmsg"
198191
+ "OK\\[op=GENERATE_EEK, user=luser@REALM\\] testmsg"
@@ -211,8 +204,8 @@ public void testInitAuditLoggers() throws Exception {
211204
List<KMSAuditLogger> loggers = (List<KMSAuditLogger>) FieldUtils.
212205
getField(KMSAudit.class, "auditLoggers", true).get(kmsAudit);
213206

214-
Assert.assertEquals(1, loggers.size());
215-
Assert.assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());
207+
Assertions.assertEquals(1, loggers.size());
208+
Assertions.assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());
216209

217210
// Explicitly configure the simple logger. Duplicates are ignored.
218211
final Configuration conf = new Configuration();
@@ -222,15 +215,15 @@ public void testInitAuditLoggers() throws Exception {
222215
final KMSAudit audit = new KMSAudit(conf);
223216
loggers = (List<KMSAuditLogger>) FieldUtils.
224217
getField(KMSAudit.class, "auditLoggers", true).get(kmsAudit);
225-
Assert.assertEquals(1, loggers.size());
226-
Assert.assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());
218+
Assertions.assertEquals(1, loggers.size());
219+
Assertions.assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());
227220

228221
// If any loggers unable to load, init should fail.
229222
conf.set(KMSConfiguration.KMS_AUDIT_LOGGER_KEY,
230223
SimpleKMSAuditLogger.class.getName() + ",unknown");
231224
try {
232225
new KMSAudit(conf);
233-
Assert.fail("loggers configured but invalid, init should fail.");
226+
Assertions.fail("loggers configured but invalid, init should fail.");
234227
} catch (Exception ex) {
235228
GenericTestUtils
236229
.assertExceptionContains(KMSConfiguration.KMS_AUDIT_LOGGER_KEY, ex);

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSAuthenticationFilter.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323
.DelegationTokenAuthenticationHandler;
2424
import org.apache.hadoop.security.token.delegation.web
2525
.PseudoDelegationTokenAuthenticationHandler;
26-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2727
import java.util.Properties;
2828

29-
import static org.junit.Assert.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
3030

3131
/**
3232
* Test KMS Authentication Filter.
3333
*/
3434
public class TestKMSAuthenticationFilter {
3535

36-
@Test public void testConfiguration() throws Exception {
36+
@Test
37+
public void testConfiguration() throws Exception {
3738
Configuration conf = new Configuration();
3839
conf.set("hadoop.kms.authentication.type", "simple");
3940

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSMDCFilter.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818
package org.apache.hadoop.crypto.key.kms.server;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNull;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNull;
2222
import static org.mockito.Mockito.when;
2323

2424
import java.io.IOException;
@@ -30,8 +30,8 @@
3030
import javax.servlet.http.HttpServletRequest;
3131
import javax.servlet.http.HttpServletResponse;
3232

33-
import org.junit.Before;
34-
import org.junit.Test;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
3535
import org.mockito.Mockito;
3636

3737
/**
@@ -48,7 +48,7 @@ public class TestKMSMDCFilter {
4848
private HttpServletRequest httpRequest;
4949
private HttpServletResponse httpResponse;
5050

51-
@Before
51+
@BeforeEach
5252
public void setUp() throws IOException {
5353
filter = new KMSMDCFilter();
5454
httpRequest = Mockito.mock(HttpServletRequest.class);
@@ -66,10 +66,10 @@ public void testFilter() throws IOException, ServletException {
6666
@Override
6767
public void doFilter(ServletRequest request, ServletResponse response)
6868
throws IOException, ServletException {
69-
assertEquals("filter.remoteClientAddress", REMOTE_ADDRESS,
70-
KMSMDCFilter.getRemoteClientAddress());
71-
assertEquals("filter.method", METHOD, KMSMDCFilter.getMethod());
72-
assertEquals("filter.url", URL, KMSMDCFilter.getURL());
69+
assertEquals(REMOTE_ADDRESS
70+
, KMSMDCFilter.getRemoteClientAddress(), "filter.remoteClientAddress");
71+
assertEquals(METHOD, KMSMDCFilter.getMethod(), "filter.method");
72+
assertEquals(URL, KMSMDCFilter.getURL(), "filter.url");
7373
}
7474
};
7575

@@ -79,10 +79,10 @@ public void doFilter(ServletRequest request, ServletResponse response)
7979
}
8080

8181
private void checkMDCValuesAreEmpty() {
82-
assertNull("getRemoteClientAddress", KMSMDCFilter.getRemoteClientAddress());
83-
assertNull("getMethod", KMSMDCFilter.getMethod());
84-
assertNull("getURL", KMSMDCFilter.getURL());
85-
assertNull("getUgi", KMSMDCFilter.getUgi());
82+
assertNull(KMSMDCFilter.getRemoteClientAddress(), "getRemoteClientAddress");
83+
assertNull(KMSMDCFilter.getMethod(), "getMethod");
84+
assertNull(KMSMDCFilter.getURL(), "getURL");
85+
assertNull(KMSMDCFilter.getUgi(), "getUgi");
8686
}
8787

8888
}

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSWithZK.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
2626
import org.apache.hadoop.security.authentication.util.ZKSignerSecretProvider;
2727
import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL;
28-
import org.junit.Assert;
29-
import org.junit.Test;
28+
import org.junit.jupiter.api.Assertions;
29+
import org.junit.jupiter.api.Test;
3030

3131
import java.io.File;
3232
import java.net.HttpURLConnection;
@@ -97,7 +97,7 @@ public void testMultipleKMSInstancesWithZKSigner() throws Exception {
9797
@Override
9898
public Object run() throws Exception {
9999
HttpURLConnection conn = aUrl.openConnection(url1, token);
100-
Assert.assertEquals(HttpURLConnection.HTTP_OK,
100+
Assertions.assertEquals(HttpURLConnection.HTTP_OK,
101101
conn.getResponseCode());
102102
return null;
103103
}
@@ -107,7 +107,7 @@ public Object run() throws Exception {
107107
@Override
108108
public Object run() throws Exception {
109109
HttpURLConnection conn = aUrl.openConnection(url2, token);
110-
Assert.assertEquals(HttpURLConnection.HTTP_OK,
110+
Assertions.assertEquals(HttpURLConnection.HTTP_OK,
111111
conn.getResponseCode());
112112
return null;
113113
}
@@ -119,7 +119,7 @@ public Object run() throws Exception {
119119
final DelegationTokenAuthenticatedURL.Token emptyToken =
120120
new DelegationTokenAuthenticatedURL.Token();
121121
HttpURLConnection conn = aUrl.openConnection(url2, emptyToken);
122-
Assert.assertEquals(HttpURLConnection.HTTP_FORBIDDEN,
122+
Assertions.assertEquals(HttpURLConnection.HTTP_FORBIDDEN,
123123
conn.getResponseCode());
124124
return null;
125125
}

0 commit comments

Comments
 (0)