Skip to content

Commit 03f9f17

Browse files
authored
HBASE-30059 Upgrade hbase-server to use junit5 Part6 (#8028)
Signed-off-by: Duo Zhang <zhangduo@apache.org>
1 parent 80dd0c2 commit 03f9f17

56 files changed

Lines changed: 854 additions & 1034 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
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-common/src/test/java/org/apache/hadoop/hbase/TableNameTestRule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
/**
2424
* Returns a {@code TableName} based on currently running test method name. Supports tests built on
2525
* the {@link org.junit.runners.Parameterized} runner.
26+
* @deprecated Use {@link TableNameTestExtension} instead, once we finish the migration of JUnit5,
27+
* which means we do not need {@link TableNameTestRule} any more, we can remove these
28+
* dependencies, see
29+
* <a href="https://issues.apache.org/jira/browse/HBASE-23671">HBASE-23671</a> for more
30+
* details.
2631
*/
32+
@Deprecated
2733
public class TableNameTestRule extends TestWatcher {
2834

2935
private TableName tableName;

hbase-server/src/test/java/org/apache/hadoop/hbase/TestMetaTableAccessor.java

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
*/
1818
package org.apache.hadoop.hbase;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
23-
import static org.junit.Assert.assertTrue;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
2424
import static org.mockito.ArgumentMatchers.any;
2525
import static org.mockito.Mockito.doReturn;
2626
import static org.mockito.Mockito.mock;
@@ -55,13 +55,11 @@
5555
import org.apache.hadoop.hbase.util.Bytes;
5656
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
5757
import org.apache.hadoop.hbase.util.Pair;
58-
import org.junit.AfterClass;
59-
import org.junit.BeforeClass;
60-
import org.junit.ClassRule;
61-
import org.junit.Rule;
62-
import org.junit.Test;
63-
import org.junit.experimental.categories.Category;
64-
import org.junit.rules.TestName;
58+
import org.junit.jupiter.api.AfterAll;
59+
import org.junit.jupiter.api.BeforeAll;
60+
import org.junit.jupiter.api.Tag;
61+
import org.junit.jupiter.api.Test;
62+
import org.junit.jupiter.api.TestInfo;
6563
import org.slf4j.Logger;
6664
import org.slf4j.LoggerFactory;
6765

@@ -70,21 +68,15 @@
7068
/**
7169
* Test {@link org.apache.hadoop.hbase.MetaTableAccessor}.
7270
*/
73-
@Category({ MiscTests.class, MediumTests.class })
71+
@Tag(MiscTests.TAG)
72+
@Tag(MediumTests.TAG)
7473
@SuppressWarnings("deprecation")
7574
public class TestMetaTableAccessor {
76-
@ClassRule
77-
public static final HBaseClassTestRule CLASS_RULE =
78-
HBaseClassTestRule.forClass(TestMetaTableAccessor.class);
79-
8075
private static final Logger LOG = LoggerFactory.getLogger(TestMetaTableAccessor.class);
8176
private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
8277
private static Connection connection;
8378

84-
@Rule
85-
public TestName name = new TestName();
86-
87-
@BeforeClass
79+
@BeforeAll
8880
public static void beforeClass() throws Exception {
8981
UTIL.startMiniCluster(3);
9082

@@ -96,7 +88,7 @@ public static void beforeClass() throws Exception {
9688
connection = ConnectionFactory.createConnection(c);
9789
}
9890

99-
@AfterClass
91+
@AfterAll
10092
public static void afterClass() throws Exception {
10193
connection.close();
10294
UTIL.shutdownMiniCluster();
@@ -122,8 +114,8 @@ public void testIsMetaWhenMetaGoesOffline() throws InterruptedException {
122114
* while its hosted server is restarted to prove our retrying works.
123115
*/
124116
@Test
125-
public void testRetrying() throws IOException, InterruptedException {
126-
final TableName tableName = TableName.valueOf(name.getMethodName());
117+
public void testRetrying(TestInfo testInfo) throws IOException, InterruptedException {
118+
final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
127119
LOG.info("Started " + tableName);
128120
Table t = UTIL.createMultiRegionTable(tableName, HConstants.CATALOG_FAMILY);
129121
int regionCount = -1;
@@ -178,8 +170,8 @@ void metaTask() throws IOException {
178170
}
179171
}
180172

181-
assertTrue("reader: " + reader.toString(), reader.isProgressing());
182-
assertTrue("writer: " + writer.toString(), writer.isProgressing());
173+
assertTrue(reader.isProgressing(), "reader: " + reader.toString());
174+
assertTrue(writer.isProgressing(), "writer: " + writer.toString());
183175
} catch (IOException e) {
184176
throw e;
185177
} finally {
@@ -190,7 +182,7 @@ void metaTask() throws IOException {
190182
t.close();
191183
}
192184
long exeTime = EnvironmentEdgeManager.currentTime() - startTime;
193-
assertTrue("Timeout: test took " + exeTime / 1000 + " sec", exeTime < timeOut);
185+
assertTrue(exeTime < timeOut, "Timeout: test took " + exeTime / 1000 + " sec");
194186
}
195187

196188
/**
@@ -242,8 +234,8 @@ public String toString() {
242234
}
243235

244236
@Test
245-
public void testGetRegion() throws IOException, InterruptedException {
246-
final String name = this.name.getMethodName();
237+
public void testGetRegion(TestInfo testInfo) throws IOException, InterruptedException {
238+
final String name = testInfo.getTestMethod().get().getName();
247239
LOG.info("Started " + name);
248240
// Test get on non-existent region.
249241
Pair<RegionInfo, ServerName> pair =
@@ -254,8 +246,8 @@ public void testGetRegion() throws IOException, InterruptedException {
254246

255247
// Test for the optimization made in HBASE-3650
256248
@Test
257-
public void testScanMetaForTable() throws IOException, InterruptedException {
258-
final TableName tableName = TableName.valueOf(name.getMethodName());
249+
public void testScanMetaForTable(TestInfo testInfo) throws IOException, InterruptedException {
250+
final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
259251
LOG.info("Started " + tableName);
260252

261253
/**
@@ -291,23 +283,26 @@ private static void testGetRegion(final Connection connection, final RegionInfo
291283
}
292284

293285
@Test
294-
public void testMetaLocationsForRegionReplicas() throws IOException {
286+
public void testMetaLocationsForRegionReplicas(TestInfo testInfo) throws IOException {
295287
Random rand = ThreadLocalRandom.current();
296288

297289
ServerName serverName0 = ServerName.valueOf("foo", 60010, rand.nextLong());
298290
ServerName serverName1 = ServerName.valueOf("bar", 60010, rand.nextLong());
299291
ServerName serverName100 = ServerName.valueOf("baz", 60010, rand.nextLong());
300292

301293
long regionId = EnvironmentEdgeManager.currentTime();
302-
RegionInfo primary = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
303-
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
304-
.setRegionId(regionId).setReplicaId(0).build();
305-
RegionInfo replica1 = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
306-
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
307-
.setRegionId(regionId).setReplicaId(1).build();
308-
RegionInfo replica100 = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
309-
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
310-
.setRegionId(regionId).setReplicaId(100).build();
294+
RegionInfo primary =
295+
RegionInfoBuilder.newBuilder(TableName.valueOf(testInfo.getTestMethod().get().getName()))
296+
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
297+
.setRegionId(regionId).setReplicaId(0).build();
298+
RegionInfo replica1 =
299+
RegionInfoBuilder.newBuilder(TableName.valueOf(testInfo.getTestMethod().get().getName()))
300+
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
301+
.setRegionId(regionId).setReplicaId(1).build();
302+
RegionInfo replica100 =
303+
RegionInfoBuilder.newBuilder(TableName.valueOf(testInfo.getTestMethod().get().getName()))
304+
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
305+
.setRegionId(regionId).setReplicaId(100).build();
311306

312307
long seqNum0 = rand.nextLong();
313308
long seqNum1 = rand.nextLong();
@@ -372,11 +367,13 @@ public static void assertEmptyMetaLocation(Table meta, byte[] row, int replicaId
372367
}
373368

374369
@Test
375-
public void testMetaLocationForRegionReplicasIsAddedAtTableCreation() throws IOException {
370+
public void testMetaLocationForRegionReplicasIsAddedAtTableCreation(TestInfo testInfo)
371+
throws IOException {
376372
long regionId = EnvironmentEdgeManager.currentTime();
377-
RegionInfo primary = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
378-
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
379-
.setRegionId(regionId).setReplicaId(0).build();
373+
RegionInfo primary =
374+
RegionInfoBuilder.newBuilder(TableName.valueOf(testInfo.getTestMethod().get().getName()))
375+
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
376+
.setRegionId(regionId).setReplicaId(0).build();
380377

381378
Table meta = MetaTableAccessor.getMetaHTable(connection);
382379
try {
@@ -391,10 +388,10 @@ public void testMetaLocationForRegionReplicasIsAddedAtTableCreation() throws IOE
391388
}
392389

393390
@Test
394-
public void testMetaScanner() throws Exception {
395-
LOG.info("Starting " + name.getMethodName());
391+
public void testMetaScanner(TestInfo testInfo) throws Exception {
392+
LOG.info("Starting " + testInfo.getTestMethod().get().getName());
396393

397-
final TableName tableName = TableName.valueOf(name.getMethodName());
394+
final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
398395
final byte[] FAMILY = Bytes.toBytes("family");
399396
final byte[][] SPLIT_KEYS =
400397
new byte[][] { Bytes.toBytes("region_a"), Bytes.toBytes("region_b") };
@@ -437,11 +434,12 @@ public void testMetaScanner() throws Exception {
437434
* Tests whether maximum of masters system time versus RSs local system time is used
438435
*/
439436
@Test
440-
public void testMastersSystemTimeIsUsedInUpdateLocations() throws IOException {
437+
public void testMastersSystemTimeIsUsedInUpdateLocations(TestInfo testInfo) throws IOException {
441438
long regionId = EnvironmentEdgeManager.currentTime();
442-
RegionInfo regionInfo = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
443-
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
444-
.setRegionId(regionId).setReplicaId(0).build();
439+
RegionInfo regionInfo =
440+
RegionInfoBuilder.newBuilder(TableName.valueOf(testInfo.getTestMethod().get().getName()))
441+
.setStartKey(HConstants.EMPTY_START_ROW).setEndKey(HConstants.EMPTY_END_ROW).setSplit(false)
442+
.setRegionId(regionId).setReplicaId(0).build();
445443

446444
ServerName sn = ServerName.valueOf("bar", 0, 0);
447445
try (Table meta = MetaTableAccessor.getMetaHTable(connection)) {

hbase-server/src/test/java/org/apache/hadoop/hbase/TestMetaUpdatesGoToPriorityQueue.java

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

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

2222
import java.io.IOException;
2323
import java.util.Arrays;
@@ -38,36 +38,32 @@
3838
import org.apache.hadoop.hbase.util.Bytes;
3939
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
4040
import org.apache.hadoop.hbase.util.FutureUtils;
41-
import org.junit.AfterClass;
42-
import org.junit.BeforeClass;
43-
import org.junit.ClassRule;
44-
import org.junit.Test;
45-
import org.junit.experimental.categories.Category;
41+
import org.junit.jupiter.api.AfterAll;
42+
import org.junit.jupiter.api.BeforeAll;
43+
import org.junit.jupiter.api.Tag;
44+
import org.junit.jupiter.api.Test;
4645

4746
import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
4847
import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
4948
import org.apache.hadoop.hbase.shaded.protobuf.generated.MultiRowMutationProtos.MultiRowMutationService;
5049
import org.apache.hadoop.hbase.shaded.protobuf.generated.MultiRowMutationProtos.MutateRowsRequest;
5150
import org.apache.hadoop.hbase.shaded.protobuf.generated.MultiRowMutationProtos.MutateRowsResponse;
5251

53-
@Category({ MiscTests.class, MediumTests.class })
52+
@Tag(MiscTests.TAG)
53+
@Tag(MediumTests.TAG)
5454
public class TestMetaUpdatesGoToPriorityQueue {
5555

56-
@ClassRule
57-
public static final HBaseClassTestRule CLASS_RULE =
58-
HBaseClassTestRule.forClass(TestMetaUpdatesGoToPriorityQueue.class);
59-
6056
private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
6157

62-
@BeforeClass
58+
@BeforeAll
6359
public static void beforeClass() throws Exception {
6460
// This test has to be end-to-end, and do the verification from the server side
6561
UTIL.getConfiguration().set(RSRpcServices.REGION_SERVER_RPC_SCHEDULER_FACTORY_CLASS,
6662
SpyingRpcSchedulerFactory.class.getName());
6763
UTIL.startMiniCluster();
6864
}
6965

70-
@AfterClass
66+
@AfterAll
7167
public static void afterClass() throws Exception {
7268
UTIL.shutdownMiniCluster();
7369
}

hbase-server/src/test/java/org/apache/hadoop/hbase/master/assignment/AssignmentTestingUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818
package org.apache.hadoop.hbase.master.assignment;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2222

2323
import java.io.IOException;
2424
import java.util.Set;

hbase-server/src/test/java/org/apache/hadoop/hbase/master/assignment/TestAMAssignWithRandExec.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,19 @@
1717
*/
1818
package org.apache.hadoop.hbase.master.assignment;
1919

20-
import org.apache.hadoop.hbase.HBaseClassTestRule;
2120
import org.apache.hadoop.hbase.TableName;
2221
import org.apache.hadoop.hbase.client.RegionInfo;
2322
import org.apache.hadoop.hbase.testclassification.LargeTests;
2423
import org.apache.hadoop.hbase.testclassification.MasterTests;
25-
import org.junit.ClassRule;
26-
import org.junit.Test;
27-
import org.junit.experimental.categories.Category;
24+
import org.junit.jupiter.api.Tag;
25+
import org.junit.jupiter.api.Test;
2826
import org.slf4j.Logger;
2927
import org.slf4j.LoggerFactory;
3028

31-
@Category({ MasterTests.class, LargeTests.class })
29+
@Tag(MasterTests.TAG)
30+
@Tag(LargeTests.TAG)
3231
public class TestAMAssignWithRandExec extends TestAssignmentManagerBase {
3332

34-
@ClassRule
35-
public static final HBaseClassTestRule CLASS_RULE =
36-
HBaseClassTestRule.forClass(TestAMAssignWithRandExec.class);
37-
3833
private static final Logger LOG = LoggerFactory.getLogger(TestAMAssignWithRandExec.class);
3934

4035
@Test

0 commit comments

Comments
 (0)