Skip to content

Commit a0c4abb

Browse files
authored
HBASE-30071 Upgrade hbase-server to use junit5 Part11 (#8068) (#8074) (#8079)
(cherry picked from commit 235ea44) (cherry picked from commit 1e47a52) Signed-off-by: Xiao Liu <liuxiaocs@apache.org>
1 parent 47a9c6f commit a0c4abb

File tree

63 files changed

+1533
-1738
lines changed

Some content is hidden

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

63 files changed

+1533
-1738
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase;
19+
20+
import org.junit.jupiter.api.extension.BeforeEachCallback;
21+
import org.junit.jupiter.api.extension.ExtensionContext;
22+
23+
/**
24+
* Returns a {@code TableName} based on currently running test method name.
25+
*/
26+
public class TableNameTestExtension implements BeforeEachCallback {
27+
28+
private TableName tableName;
29+
30+
/**
31+
* Helper to handle parameterized method names. Unlike regular test methods, parameterized method
32+
* names look like 'foo[x]'. This is problematic for tests that use this name for HBase tables.
33+
* This helper strips out the parameter suffixes.
34+
* @return current test method name without parameterized suffixes.
35+
*/
36+
public static String cleanUpTestName(String methodName) {
37+
int index = methodName.indexOf('[');
38+
if (index == -1) {
39+
return methodName;
40+
}
41+
return methodName.substring(0, index);
42+
}
43+
44+
public TableName getTableName() {
45+
return tableName;
46+
}
47+
48+
@Override
49+
public void beforeEach(ExtensionContext context) {
50+
tableName = TableName.valueOf(cleanUpTestName(context.getRequiredTestMethod().getName()));
51+
}
52+
}

hbase-server/src/test/java/org/apache/hadoop/hbase/security/AbstractTestMutualTls.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
package org.apache.hadoop.hbase.security;
1919

2020
import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE;
21-
import static org.hamcrest.MatcherAssert.assertThat;
22-
import static org.hamcrest.Matchers.instanceOf;
23-
import static org.junit.Assert.assertThrows;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
import static org.junit.jupiter.api.Assertions.fail;
2423

2524
import java.io.File;
2625
import java.io.IOException;
@@ -50,13 +49,13 @@
5049
import org.bouncycastle.asn1.x500.style.BCStyle;
5150
import org.bouncycastle.jce.provider.BouncyCastleProvider;
5251
import org.bouncycastle.operator.OperatorCreationException;
53-
import org.junit.After;
54-
import org.junit.AfterClass;
55-
import org.junit.Before;
56-
import org.junit.BeforeClass;
57-
import org.junit.Test;
58-
import org.junit.runners.Parameterized;
52+
import org.junit.jupiter.api.AfterAll;
53+
import org.junit.jupiter.api.AfterEach;
54+
import org.junit.jupiter.api.BeforeAll;
55+
import org.junit.jupiter.api.BeforeEach;
56+
import org.junit.jupiter.api.TestTemplate;
5957

58+
import org.apache.hbase.thirdparty.com.google.common.base.Throwables;
6059
import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
6160
import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
6261
import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
@@ -76,24 +75,20 @@ public abstract class AbstractTestMutualTls {
7675
protected RpcServer rpcServer;
7776

7877
protected RpcClient rpcClient;
78+
7979
private TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface stub;
8080

81-
@Parameterized.Parameter(0)
82-
public X509KeyType caKeyType;
81+
protected X509KeyType caKeyType;
82+
83+
protected X509KeyType certKeyType;
8384

84-
@Parameterized.Parameter(1)
85-
public X509KeyType certKeyType;
85+
protected String keyPassword;
8686

87-
@Parameterized.Parameter(2)
88-
public String keyPassword;
89-
@Parameterized.Parameter(3)
90-
public boolean expectSuccess;
87+
protected boolean expectSuccess;
9188

92-
@Parameterized.Parameter(4)
93-
public boolean validateHostnames;
89+
protected boolean validateHostnames;
9490

95-
@Parameterized.Parameter(5)
96-
public CertConfig certConfig;
91+
protected CertConfig certConfig;
9792

9893
public enum CertConfig {
9994
// For no cert, we literally pass no certificate to the server. It's possible (assuming server
@@ -112,7 +107,17 @@ public enum CertConfig {
112107
VERIFIABLE_CERT_WITH_BAD_HOST
113108
}
114109

115-
@BeforeClass
110+
protected AbstractTestMutualTls(X509KeyType caKeyType, X509KeyType certKeyType,
111+
String keyPassword, boolean expectSuccess, boolean validateHostnames, CertConfig certConfig) {
112+
this.caKeyType = caKeyType;
113+
this.certKeyType = certKeyType;
114+
this.keyPassword = keyPassword;
115+
this.expectSuccess = expectSuccess;
116+
this.validateHostnames = validateHostnames;
117+
this.certConfig = certConfig;
118+
}
119+
120+
@BeforeAll
116121
public static void setUpBeforeClass() throws IOException {
117122
UTIL = new HBaseCommonTestingUtility();
118123
Security.addProvider(new BouncyCastleProvider());
@@ -131,7 +136,7 @@ public static void setUpBeforeClass() throws IOException {
131136
PROVIDER = new X509TestContextProvider(conf, DIR);
132137
}
133138

134-
@AfterClass
139+
@AfterAll
135140
public static void cleanUp() {
136141
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
137142
UTIL.cleanupTestDir();
@@ -140,7 +145,7 @@ public static void cleanUp() {
140145
protected abstract void initialize(Configuration serverConf, Configuration clientConf)
141146
throws IOException, GeneralSecurityException, OperatorCreationException;
142147

143-
@Before
148+
@BeforeEach
144149
public void setUp() throws Exception {
145150
x509TestContext = PROVIDER.get(caKeyType, certKeyType, keyPassword.toCharArray());
146151
x509TestContext.setConfigurations(KeyStoreFileType.JKS, KeyStoreFileType.JKS);
@@ -192,7 +197,7 @@ protected void handleCertConfig(Configuration confToSet)
192197
}
193198
}
194199

195-
@After
200+
@AfterEach
196201
public void tearDown() throws IOException {
197202
if (rpcServer != null) {
198203
rpcServer.stop();
@@ -208,14 +213,23 @@ public void tearDown() throws IOException {
208213
Security.setProperty("com.sun.security.enableCRLDP", Boolean.FALSE.toString());
209214
}
210215

211-
@Test
216+
@TestTemplate
212217
public void testClientAuth() throws Exception {
213218
if (expectSuccess) {
214219
// we expect no exception, so if one is thrown the test will fail
215220
submitRequest();
216221
} else {
217222
ServiceException se = assertThrows(ServiceException.class, this::submitRequest);
218-
assertThat(se.getCause(), instanceOf(SSLHandshakeException.class));
223+
// The SSLHandshakeException is encapsulated differently depending on the TLS version
224+
Throwable current = se;
225+
do {
226+
if (current instanceof SSLHandshakeException) {
227+
return;
228+
}
229+
current = current.getCause();
230+
} while (current != null);
231+
fail("Exception chain does not include SSLHandshakeException: "
232+
+ Throwables.getStackTraceAsString(se));
219233
}
220234
}
221235

hbase-server/src/test/java/org/apache/hadoop/hbase/security/AbstractTestSecureIPC.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
import static org.hamcrest.MatcherAssert.assertThat;
2727
import static org.hamcrest.Matchers.either;
2828
import static org.hamcrest.Matchers.instanceOf;
29-
import static org.junit.Assert.assertEquals;
30-
import static org.junit.Assert.assertNotEquals;
31-
import static org.junit.Assert.assertNotSame;
32-
import static org.junit.Assert.assertSame;
33-
import static org.junit.Assert.assertThrows;
34-
import static org.junit.Assert.fail;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
31+
import static org.junit.jupiter.api.Assertions.assertNotSame;
32+
import static org.junit.jupiter.api.Assertions.assertSame;
33+
import static org.junit.jupiter.api.Assertions.assertThrows;
34+
import static org.junit.jupiter.api.Assertions.fail;
3535

3636
import java.io.EOFException;
3737
import java.io.File;

hbase-server/src/test/java/org/apache/hadoop/hbase/security/AbstractTestTlsRejectPlainText.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE;
2121
import static org.hamcrest.MatcherAssert.assertThat;
2222
import static org.hamcrest.Matchers.instanceOf;
23-
import static org.junit.Assert.assertThrows;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
2424

2525
import java.io.File;
2626
import java.io.IOException;
2727
import java.net.InetSocketAddress;
2828
import java.security.Security;
2929
import java.util.ArrayList;
3030
import java.util.List;
31+
import java.util.stream.Stream;
3132
import org.apache.commons.io.FileUtils;
3233
import org.apache.hadoop.conf.Configuration;
3334
import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
@@ -45,10 +46,10 @@
4546
import org.apache.hadoop.hbase.ipc.RpcServer;
4647
import org.apache.hadoop.hbase.ipc.RpcServerFactory;
4748
import org.bouncycastle.jce.provider.BouncyCastleProvider;
48-
import org.junit.After;
49-
import org.junit.Before;
50-
import org.junit.Test;
51-
import org.junit.runners.Parameterized;
49+
import org.junit.jupiter.api.AfterEach;
50+
import org.junit.jupiter.api.BeforeEach;
51+
import org.junit.jupiter.api.TestTemplate;
52+
import org.junit.jupiter.params.provider.Arguments;
5253

5354
import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
5455
import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
@@ -65,32 +66,35 @@ public abstract class AbstractTestTlsRejectPlainText {
6566

6667
protected static X509TestContextProvider PROVIDER;
6768

68-
@Parameterized.Parameter(0)
69-
public X509KeyType caKeyType;
69+
protected X509KeyType caKeyType;
7070

71-
@Parameterized.Parameter(1)
72-
public X509KeyType certKeyType;
71+
protected X509KeyType certKeyType;
7372

74-
@Parameterized.Parameter(2)
75-
public char[] keyPassword;
73+
protected char[] keyPassword;
7674

7775
private X509TestContext x509TestContext;
7876

7977
protected RpcServer rpcServer;
8078

8179
protected RpcClient rpcClient;
8280

83-
@Parameterized.Parameters(name = "{index}: caKeyType={0}, certKeyType={1}, keyPassword={2}")
84-
public static List<Object[]> data() {
85-
List<Object[]> params = new ArrayList<>();
81+
public static Stream<Arguments> parameters() {
82+
List<Arguments> params = new ArrayList<>();
8683
for (X509KeyType caKeyType : X509KeyType.values()) {
8784
for (X509KeyType certKeyType : X509KeyType.values()) {
8885
for (char[] keyPassword : new char[][] { "".toCharArray(), "pa$$w0rd".toCharArray() }) {
89-
params.add(new Object[] { caKeyType, certKeyType, keyPassword });
86+
params.add(Arguments.of(caKeyType, certKeyType, keyPassword));
9087
}
9188
}
9289
}
93-
return params;
90+
return params.stream();
91+
}
92+
93+
protected AbstractTestTlsRejectPlainText(X509KeyType caKeyType, X509KeyType certKeyType,
94+
char[] keyPassword) {
95+
this.caKeyType = caKeyType;
96+
this.certKeyType = certKeyType;
97+
this.keyPassword = keyPassword;
9498
}
9599

96100
protected static void initialize() throws IOException {
@@ -115,7 +119,7 @@ protected static void cleanUp() {
115119
UTIL.cleanupTestDir();
116120
}
117121

118-
@Before
122+
@BeforeEach
119123
public void setUp() throws Exception {
120124
x509TestContext = PROVIDER.get(caKeyType, certKeyType, keyPassword);
121125
x509TestContext.setConfigurations(KeyStoreFileType.JKS, KeyStoreFileType.JKS);
@@ -127,7 +131,7 @@ public void setUp() throws Exception {
127131
rpcClient = new NettyRpcClient(conf);
128132
}
129133

130-
@After
134+
@AfterEach
131135
public void tearDown() throws IOException {
132136
if (rpcServer != null) {
133137
rpcServer.stop();
@@ -145,7 +149,7 @@ public void tearDown() throws IOException {
145149

146150
protected abstract BlockingInterface createStub() throws Exception;
147151

148-
@Test
152+
@TestTemplate
149153
public void testReject() throws Exception {
150154
BlockingInterface stub = createStub();
151155
ServiceException se = assertThrows(ServiceException.class,

0 commit comments

Comments
 (0)