Skip to content

Commit 5bd757e

Browse files
committed
HBASE-30039 Upgrade hbase-server to use junit5 Part3 (#7999)
Signed-off-by: Duo Zhang <zhangduo@apache.org>
1 parent 2d5b5ff commit 5bd757e

55 files changed

Lines changed: 926 additions & 1372 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.

hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/SpaceQuotaHelperForTests.java

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

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
22-
import static org.junit.Assert.fail;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
import static org.junit.jupiter.api.Assertions.fail;
2323

2424
import java.io.IOException;
2525
import java.util.ArrayList;
2626
import java.util.Collection;
27-
import java.util.Collections;
2827
import java.util.List;
28+
import java.util.Map;
2929
import java.util.Map.Entry;
3030
import java.util.Objects;
3131
import java.util.Set;
32+
import java.util.TreeMap;
3233
import java.util.concurrent.atomic.AtomicLong;
34+
import java.util.function.Supplier;
3335
import org.apache.hadoop.conf.Configuration;
3436
import org.apache.hadoop.fs.FileSystem;
3537
import org.apache.hadoop.fs.Path;
3638
import org.apache.hadoop.hbase.HBaseTestingUtility;
37-
import org.apache.hadoop.hbase.HConstants;
38-
import org.apache.hadoop.hbase.MiniHBaseCluster;
3939
import org.apache.hadoop.hbase.NamespaceDescriptor;
40+
import org.apache.hadoop.hbase.MiniHBaseCluster;
4041
import org.apache.hadoop.hbase.TableName;
4142
import org.apache.hadoop.hbase.TableNotEnabledException;
4243
import org.apache.hadoop.hbase.Waiter.Predicate;
4344
import org.apache.hadoop.hbase.client.Admin;
4445
import org.apache.hadoop.hbase.client.Append;
45-
import org.apache.hadoop.hbase.client.ClientServiceCallable;
4646
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
4747
import org.apache.hadoop.hbase.client.Connection;
4848
import org.apache.hadoop.hbase.client.Delete;
@@ -52,20 +52,16 @@
5252
import org.apache.hadoop.hbase.client.Result;
5353
import org.apache.hadoop.hbase.client.ResultScanner;
5454
import org.apache.hadoop.hbase.client.Scan;
55-
import org.apache.hadoop.hbase.client.SecureBulkLoadClient;
5655
import org.apache.hadoop.hbase.client.Table;
5756
import org.apache.hadoop.hbase.client.TableDescriptor;
5857
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
59-
import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
6058
import org.apache.hadoop.hbase.regionserver.HRegion;
6159
import org.apache.hadoop.hbase.regionserver.HStore;
6260
import org.apache.hadoop.hbase.regionserver.HStoreFile;
6361
import org.apache.hadoop.hbase.regionserver.TestHRegionServerBulkLoad;
6462
import org.apache.hadoop.hbase.util.Bytes;
65-
import org.apache.hadoop.hbase.util.Pair;
6663
import org.apache.hadoop.util.StringUtils;
6764
import org.apache.yetus.audience.InterfaceAudience;
68-
import org.junit.rules.TestName;
6965
import org.slf4j.Logger;
7066
import org.slf4j.LoggerFactory;
7167

@@ -84,11 +80,11 @@ public class SpaceQuotaHelperForTests {
8480
public static final long ONE_GIGABYTE = ONE_MEGABYTE * ONE_KILOBYTE;
8581

8682
private final HBaseTestingUtility testUtil;
87-
private final TestName testName;
83+
private final Supplier<String> testName;
8884
private final AtomicLong counter;
8985
private static final int NUM_RETRIES = 10;
9086

91-
public SpaceQuotaHelperForTests(HBaseTestingUtility testUtil, TestName testName,
87+
public SpaceQuotaHelperForTests(HBaseTestingUtility testUtil, Supplier<String> testName,
9288
AtomicLong counter) {
9389
this.testUtil = Objects.requireNonNull(testUtil);
9490
this.testName = Objects.requireNonNull(testName);
@@ -232,12 +228,13 @@ void verifyViolation(SpaceViolationPolicy policyToViolate, TableName tn, Mutatio
232228
assertTrue(
233229
msg.contains("TableNotEnabledException") || msg.contains(policyToViolate.name()));
234230
} else {
235-
assertTrue("Expected exception message to contain the word '" + policyToViolate.name()
236-
+ "', but was " + msg, msg.contains(policyToViolate.name()));
231+
assertTrue(msg.contains(policyToViolate.name()),
232+
"Expected exception message to contain the word '" + policyToViolate.name()
233+
+ "', but was " + msg);
237234
}
238235
}
239-
assertTrue("Expected to see an exception writing data to a table exceeding its quota",
240-
sawError);
236+
assertTrue(sawError,
237+
"Expected to see an exception writing data to a table exceeding its quota");
241238
}
242239

243240
/**
@@ -277,7 +274,7 @@ void verifyNoViolation(TableName tn, Mutation m) throws Exception {
277274
scanner.close();
278275
}
279276
}
280-
assertTrue("Expected to succeed in writing data to a table not having quota ", sawSuccess);
277+
assertTrue(sawSuccess, "Expected to succeed in writing data to a table not having quota ");
281278
}
282279

283280
/**
@@ -290,8 +287,8 @@ void verifyTableUsageSnapshotForSpaceQuotaExist(TableName tn) throws Exception {
290287
ResultScanner rs = quotaTable.getScanner(s);
291288
sawUsageSnapshot = (rs.next() != null);
292289
}
293-
assertTrue("Expected to succeed in getting table usage snapshots for space quota",
294-
sawUsageSnapshot);
290+
assertTrue(sawUsageSnapshot,
291+
"Expected to succeed in getting table usage snapshots for space quota");
295292
}
296293

297294
/**
@@ -324,85 +321,6 @@ void removeQuotaFromtable(final TableName tn) throws Exception {
324321
LOG.debug("Space quota settings removed from the table ", tn);
325322
}
326323

327-
/**
328-
* @param tn the tablename
329-
* @param numFiles number of files
330-
* @param numRowsPerFile number of rows per file
331-
* @return a clientServiceCallable which can be used with the Caller factory for bulk load
332-
* @throws Exception when failed to get connection, table or preparation of the bulk load
333-
*/
334-
ClientServiceCallable<Void> generateFileToLoad(TableName tn, int numFiles, int numRowsPerFile)
335-
throws Exception {
336-
Connection conn = testUtil.getConnection();
337-
FileSystem fs = testUtil.getTestFileSystem();
338-
Configuration conf = testUtil.getConfiguration();
339-
Path baseDir = new Path(fs.getHomeDirectory(), testName.getMethodName() + "_files");
340-
fs.mkdirs(baseDir);
341-
final List<Pair<byte[], String>> famPaths = new ArrayList<Pair<byte[], String>>();
342-
for (int i = 1; i <= numFiles; i++) {
343-
Path hfile = new Path(baseDir, "file" + i);
344-
TestHRegionServerBulkLoad.createHFile(fs, hfile, Bytes.toBytes(SpaceQuotaHelperForTests.F1),
345-
Bytes.toBytes("to"), Bytes.toBytes("reject"), numRowsPerFile);
346-
famPaths.add(new Pair<>(Bytes.toBytes(SpaceQuotaHelperForTests.F1), hfile.toString()));
347-
}
348-
349-
// bulk load HFiles
350-
Table table = conn.getTable(tn);
351-
final String bulkToken = new SecureBulkLoadClient(conf, table).prepareBulkLoad(conn);
352-
return new ClientServiceCallable<Void>(conn, tn, Bytes.toBytes("row"),
353-
new RpcControllerFactory(conf).newController(), HConstants.PRIORITY_UNSET,
354-
Collections.emptyMap()) {
355-
@Override
356-
public Void rpcCall() throws Exception {
357-
SecureBulkLoadClient secureClient = null;
358-
byte[] regionName = getLocation().getRegionInfo().getRegionName();
359-
try (Table table = conn.getTable(getTableName())) {
360-
secureClient = new SecureBulkLoadClient(conf, table);
361-
secureClient.secureBulkLoadHFiles(getStub(), famPaths, regionName, true, null, bulkToken);
362-
}
363-
return null;
364-
}
365-
};
366-
}
367-
368-
/**
369-
* Bulk-loads a number of files with a number of rows to the given table.
370-
*/
371-
// ClientServiceCallable<Boolean> generateFileToLoad(
372-
// TableName tn, int numFiles, int numRowsPerFile) throws Exception {
373-
// Connection conn = testUtil.getConnection();
374-
// FileSystem fs = testUtil.getTestFileSystem();
375-
// Configuration conf = testUtil.getConfiguration();
376-
// Path baseDir = new Path(fs.getHomeDirectory(), testName.getMethodName() + "_files");
377-
// fs.mkdirs(baseDir);
378-
// final List<Pair<byte[], String>> famPaths = new ArrayList<>();
379-
// for (int i = 1; i <= numFiles; i++) {
380-
// Path hfile = new Path(baseDir, "file" + i);
381-
// TestHRegionServerBulkLoad.createHFile(
382-
// fs, hfile, Bytes.toBytes(SpaceQuotaHelperForTests.F1), Bytes.toBytes("my"),
383-
// Bytes.toBytes("file"), numRowsPerFile);
384-
// famPaths.add(new Pair<>(Bytes.toBytes(SpaceQuotaHelperForTests.F1), hfile.toString()));
385-
// }
386-
//
387-
// // bulk load HFiles
388-
// Table table = conn.getTable(tn);
389-
// final String bulkToken = new SecureBulkLoadClient(conf, table).prepareBulkLoad(conn);
390-
// return new ClientServiceCallable<Boolean>(
391-
// conn, tn, Bytes.toBytes("row"), new RpcControllerFactory(conf).newController(),
392-
// HConstants.PRIORITY_UNSET) {
393-
// @Override
394-
// public Boolean rpcCall() throws Exception {
395-
// SecureBulkLoadClient secureClient = null;
396-
// byte[] regionName = getLocation().getRegion().getRegionName();
397-
// try (Table table = conn.getTable(getTableName())) {
398-
// secureClient = new SecureBulkLoadClient(conf, table);
399-
// return secureClient.secureBulkLoadHFiles(getStub(), famPaths, regionName,
400-
// true, null, bulkToken);
401-
// }
402-
// }
403-
// };
404-
// }
405-
406324
/**
407325
* Removes the space quota from the given namespace
408326
*/
@@ -466,14 +384,14 @@ QuotaSettings getTableSpaceQuota(Connection conn, TableName tn) throws IOExcepti
466384
/**
467385
* Waits 30seconds for the HBase quota table to exist.
468386
*/
469-
public void waitForQuotaTable(Connection conn) throws IOException {
387+
void waitForQuotaTable(Connection conn) throws IOException {
470388
waitForQuotaTable(conn, 30_000);
471389
}
472390

473391
/**
474392
* Waits {@code timeout} milliseconds for the HBase quota table to exist.
475393
*/
476-
public void waitForQuotaTable(Connection conn, long timeout) throws IOException {
394+
void waitForQuotaTable(Connection conn, long timeout) throws IOException {
477395
testUtil.waitFor(timeout, 1000, new Predicate<IOException>() {
478396
@Override
479397
public boolean evaluate() throws IOException {
@@ -598,7 +516,7 @@ TableName getNextTableName() {
598516
}
599517

600518
TableName getNextTableName(String namespace) {
601-
return TableName.valueOf(namespace, testName.getMethodName() + counter.getAndIncrement());
519+
return TableName.valueOf(namespace, testName.get() + counter.getAndIncrement());
602520
}
603521

604522
TableName createTable() throws Exception {
@@ -648,7 +566,7 @@ TableName createTableWithRegions(Admin admin, String namespace, int numRegions,
648566
TableName createTableInNamespace(NamespaceDescriptor nd) throws Exception {
649567
final Admin admin = testUtil.getAdmin();
650568
final TableName tn =
651-
TableName.valueOf(nd.getName(), testName.getMethodName() + counter.getAndIncrement());
569+
TableName.valueOf(nd.getName(), testName.get() + counter.getAndIncrement());
652570

653571
// Delete the old table
654572
if (admin.tableExists(tn)) {
@@ -658,7 +576,7 @@ TableName createTableInNamespace(NamespaceDescriptor nd) throws Exception {
658576

659577
// Create the table
660578
TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tn)
661-
.addColumnFamily(ColumnFamilyDescriptorBuilder.of(F1)).build();
579+
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(F1)).build();
662580

663581
admin.createTable(tableDesc);
664582
return tn;
@@ -683,6 +601,26 @@ void partitionTablesByQuotaTarget(Multimap<TableName, QuotaSettings> quotas,
683601
}
684602
}
685603

604+
/**
605+
* Bulk-loads a number of files with a number of rows to the given table.
606+
*/
607+
Map<byte[], List<Path>> generateFileToLoad(TableName tn, int numFiles, int numRowsPerFile)
608+
throws Exception {
609+
FileSystem fs = testUtil.getTestFileSystem();
610+
Path baseDir = new Path(fs.getHomeDirectory(), testName.get() + "_files");
611+
fs.mkdirs(baseDir);
612+
List<Path> hfiles = new ArrayList<>();
613+
for (int i = 1; i <= numFiles; i++) {
614+
Path hfile = new Path(baseDir, "file" + i);
615+
TestHRegionServerBulkLoad.createHFile(fs, hfile, Bytes.toBytes(SpaceQuotaHelperForTests.F1),
616+
Bytes.toBytes("my"), Bytes.toBytes("file"), numRowsPerFile);
617+
hfiles.add(hfile);
618+
}
619+
Map<byte[], List<Path>> family2Files = new TreeMap<>(Bytes.BYTES_COMPARATOR);
620+
family2Files.put(Bytes.toBytes(SpaceQuotaHelperForTests.F1), hfiles);
621+
return family2Files;
622+
}
623+
686624
/**
687625
* Abstraction to simplify the case where a test needs to verify a certain state on a
688626
* {@code SpaceQuotaSnapshot}. This class fails-fast when there is no such snapshot obtained from

hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestActivePolicyEnforcement.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,36 @@
1717
*/
1818
package org.apache.hadoop.hbase.quotas;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertFalse;
22-
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertTrue;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
2424
import static org.mockito.Mockito.mock;
2525

2626
import java.util.Collections;
2727
import java.util.HashMap;
2828
import java.util.Map;
2929
import java.util.Map.Entry;
30-
import org.apache.hadoop.hbase.HBaseClassTestRule;
3130
import org.apache.hadoop.hbase.TableName;
3231
import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot.SpaceQuotaStatus;
3332
import org.apache.hadoop.hbase.quotas.policies.DefaultViolationPolicyEnforcement;
3433
import org.apache.hadoop.hbase.quotas.policies.MissingSnapshotViolationPolicyEnforcement;
3534
import org.apache.hadoop.hbase.quotas.policies.NoWritesViolationPolicyEnforcement;
3635
import org.apache.hadoop.hbase.regionserver.RegionServerServices;
3736
import org.apache.hadoop.hbase.testclassification.SmallTests;
38-
import org.junit.Before;
39-
import org.junit.ClassRule;
40-
import org.junit.Test;
41-
import org.junit.experimental.categories.Category;
37+
import org.junit.jupiter.api.BeforeEach;
38+
import org.junit.jupiter.api.Tag;
39+
import org.junit.jupiter.api.Test;
4240

4341
/**
4442
* Test class for {@link ActivePolicyEnforcement}.
4543
*/
46-
@Category(SmallTests.class)
44+
@Tag(SmallTests.TAG)
4745
public class TestActivePolicyEnforcement {
4846

49-
@ClassRule
50-
public static final HBaseClassTestRule CLASS_RULE =
51-
HBaseClassTestRule.forClass(TestActivePolicyEnforcement.class);
52-
5347
private RegionServerServices rss;
5448

55-
@Before
49+
@BeforeEach
5650
public void setup() {
5751
rss = mock(RegionServerServices.class);
5852
}
@@ -73,8 +67,9 @@ public void testNoPolicyReturnsNoopEnforcement() {
7367
SpaceViolationPolicyEnforcement enforcement =
7468
ape.getPolicyEnforcement(TableName.valueOf("nonexistent"));
7569
assertNotNull(enforcement);
76-
assertTrue("Expected an instance of MissingSnapshotViolationPolicyEnforcement, but got "
77-
+ enforcement.getClass(), enforcement instanceof MissingSnapshotViolationPolicyEnforcement);
70+
assertTrue(enforcement instanceof MissingSnapshotViolationPolicyEnforcement,
71+
"Expected an instance of MissingSnapshotViolationPolicyEnforcement, but got "
72+
+ enforcement.getClass());
7873
}
7974

8075
@Test
@@ -84,7 +79,7 @@ public void testNoBulkLoadChecksOnNoSnapshot() {
8479
Collections.<TableName, SpaceQuotaSnapshot> emptyMap(), mock(RegionServerServices.class));
8580
SpaceViolationPolicyEnforcement enforcement =
8681
ape.getPolicyEnforcement(TableName.valueOf("nonexistent"));
87-
assertFalse("Should not check bulkloads", enforcement.shouldCheckBulkLoads());
82+
assertFalse(enforcement.shouldCheckBulkLoads(), "Should not check bulkloads");
8883
}
8984

9085
@Test
@@ -109,10 +104,10 @@ public void testNonViolatingQuotaCachesPolicyEnforcment() {
109104
final ActivePolicyEnforcement ape =
110105
new ActivePolicyEnforcement(Collections.emptyMap(), snapshots, rss);
111106
SpaceViolationPolicyEnforcement policyEnforcement = ape.getPolicyEnforcement(tableName);
112-
assertTrue("Found the wrong class: " + policyEnforcement.getClass(),
113-
policyEnforcement instanceof DefaultViolationPolicyEnforcement);
107+
assertTrue(policyEnforcement instanceof DefaultViolationPolicyEnforcement,
108+
"Found the wrong class: " + policyEnforcement.getClass());
114109
SpaceViolationPolicyEnforcement copy = ape.getPolicyEnforcement(tableName);
115-
assertTrue("Expected the instance to be cached", policyEnforcement == copy);
110+
assertTrue(policyEnforcement == copy, "Expected the instance to be cached");
116111
Entry<TableName, SpaceViolationPolicyEnforcement> entry =
117112
ape.getLocallyCachedPolicies().entrySet().iterator().next();
118113
assertTrue(policyEnforcement == entry.getValue());

0 commit comments

Comments
 (0)