Skip to content

Commit fd05aab

Browse files
bharathvndimiduk
authored andcommitted
HBASE-23665: Split unit tests from TestTableName into a separate test-only class. (#1032)
Signed-off-by: Nick Dimiduk <ndimiduk@apache.org>
1 parent cb78b10 commit fd05aab

File tree

10 files changed

+231
-234
lines changed

10 files changed

+231
-234
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.rules.TestWatcher;
21+
import org.junit.runner.Description;
22+
23+
/**
24+
* Returns a {@code TableName} based on currently running test method name.
25+
*/
26+
public class TableNameTestRule extends TestWatcher {
27+
28+
private TableName tableName;
29+
30+
@Override
31+
protected void starting(Description description) {
32+
tableName = TableName.valueOf(description.getMethodName());
33+
}
34+
35+
public TableName getTableName() {
36+
return tableName;
37+
}
38+
}
Lines changed: 46 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -20,99 +20,36 @@
2020
import static org.junit.Assert.assertArrayEquals;
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertSame;
23-
import static org.junit.Assert.fail;
24-
23+
import static org.junit.Assert.assertThrows;
2524
import java.nio.ByteBuffer;
2625
import java.util.HashMap;
2726
import java.util.Map;
28-
import org.apache.hadoop.hbase.testclassification.MediumTests;
2927
import org.apache.hadoop.hbase.testclassification.MiscTests;
28+
import org.apache.hadoop.hbase.testclassification.SmallTests;
3029
import org.apache.hadoop.hbase.util.Bytes;
3130
import org.junit.ClassRule;
3231
import org.junit.Test;
3332
import org.junit.experimental.categories.Category;
34-
import org.junit.rules.TestWatcher;
35-
import org.junit.runner.Description;
3633

3734
/**
38-
* Returns a {@code byte[]} containing the name of the currently running test method.
35+
* Tests for various kinds of TableNames.
3936
*/
40-
@Category({MiscTests.class, MediumTests.class})
41-
public class TestTableName extends TestWatcher {
42-
37+
@Category({MiscTests.class, SmallTests.class})
38+
public class TestTableName {
4339
@ClassRule
4440
public static final HBaseClassTestRule CLASS_RULE =
4541
HBaseClassTestRule.forClass(TestTableName.class);
4642

47-
private TableName tableName;
48-
49-
/**
50-
* Invoked when a test is about to start
51-
*/
52-
@Override
53-
protected void starting(Description description) {
54-
tableName = TableName.valueOf(description.getMethodName());
55-
}
56-
57-
public TableName getTableName() {
58-
return tableName;
59-
}
60-
61-
String[] emptyNames = {"", " "};
62-
String[] invalidNamespace = {":a", "%:a"};
63-
String[] legalTableNames = {"foo", "with-dash_under.dot", "_under_start_ok",
43+
private static String[] emptyNames = {"", " "};
44+
private static String[] invalidNamespace = {":a", "%:a"};
45+
private static String[] legalTableNames = {"foo", "with-dash_under.dot", "_under_start_ok",
6446
"with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02",
6547
"dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
6648
"trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
67-
String[] illegalTableNames = {".dot_start_illegal", "-dash_start_illegal", "spaces not ok",
68-
"-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
49+
private static String[] illegalTableNames = {".dot_start_illegal", "-dash_start_illegal",
50+
"spaces not ok", "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
6951
"new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};
7052

71-
72-
@Test(expected = IllegalArgumentException.class)
73-
public void testInvalidNamespace() {
74-
for (String tn : invalidNamespace) {
75-
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
76-
fail("invalid namespace " + tn
77-
+ " should have failed with IllegalArgumentException for namespace");
78-
}
79-
}
80-
81-
@Test(expected = IllegalArgumentException.class)
82-
public void testEmptyNamespaceName() {
83-
for (String nn : emptyNames) {
84-
TableName.isLegalNamespaceName(Bytes.toBytes(nn));
85-
fail("invalid Namespace name " + nn + " should have failed with IllegalArgumentException");
86-
}
87-
}
88-
89-
@Test(expected = IllegalArgumentException.class)
90-
public void testEmptyTableName() {
91-
for (String tn : emptyNames) {
92-
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
93-
fail("invalid tablename " + tn + " should have failed with IllegalArgumentException");
94-
}
95-
}
96-
97-
@Test
98-
public void testLegalHTableNames() {
99-
for (String tn : legalTableNames) {
100-
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
101-
}
102-
}
103-
104-
@Test
105-
public void testIllegalHTableNames() {
106-
for (String tn : illegalTableNames) {
107-
try {
108-
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
109-
fail("invalid tablename " + tn + " should have failed");
110-
} catch (Exception e) {
111-
// expected
112-
}
113-
}
114-
}
115-
11653
static class Names {
11754
String ns;
11855
byte[] nsb;
@@ -147,7 +84,6 @@ public boolean equals(Object o) {
14784
if (!tn.equals(names.tn)) {
14885
return false;
14986
}
150-
15187
return true;
15288
}
15389

@@ -159,7 +95,7 @@ public int hashCode() {
15995
}
16096
}
16197

162-
Names[] names = new Names[] {
98+
private static Names[] names = new Names[] {
16399
new Names("n1", "n1"),
164100
new Names("n2", "n2"),
165101
new Names("table1", "table1"),
@@ -172,9 +108,41 @@ public int hashCode() {
172108
new Names("n2", "table2")
173109
};
174110

175-
@Test
176-
public void testValueOf() {
111+
@Test public void testInvalidNamespace() {
112+
for (String tn : invalidNamespace) {
113+
assertThrows(IllegalArgumentException.class,
114+
() -> TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn)));
115+
}
116+
}
117+
118+
@Test public void testEmptyNamespaceName() {
119+
for (String nn : emptyNames) {
120+
assertThrows(IllegalArgumentException.class,
121+
() -> TableName.isLegalNamespaceName(Bytes.toBytes(nn)));
122+
}
123+
}
124+
125+
@Test public void testEmptyTableName() {
126+
for (String tn : emptyNames) {
127+
assertThrows(IllegalArgumentException.class,
128+
() -> TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn)));
129+
}
130+
}
131+
132+
@Test public void testLegalHTableNames() {
133+
for (String tn : legalTableNames) {
134+
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
135+
}
136+
}
177137

138+
@Test public void testIllegalHTableNames() {
139+
for (String tn : illegalTableNames) {
140+
assertThrows(Exception.class,
141+
() -> TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn)));
142+
}
143+
}
144+
145+
@Test public void testValueOf() {
178146
Map<String, TableName> inCache = new HashMap<>();
179147
// fill cache
180148
for (Names name : names) {
@@ -188,7 +156,6 @@ public void testValueOf() {
188156
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(
189157
ByteBuffer.wrap(name.nsb), ByteBuffer.wrap(name.tnb)), name));
190158
}
191-
192159
}
193160

194161
private TableName validateNames(TableName expected, Names names) {
@@ -200,5 +167,4 @@ private TableName validateNames(TableName expected, Names names) {
200167
assertArrayEquals(expected.getNamespace(), names.nsb);
201168
return expected;
202169
}
203-
204170
}

hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestScannerRetriableFailure.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.apache.hadoop.hbase.HBaseClassTestRule;
3131
import org.apache.hadoop.hbase.HBaseTestingUtility;
3232
import org.apache.hadoop.hbase.TableName;
33-
import org.apache.hadoop.hbase.TestTableName;
33+
import org.apache.hadoop.hbase.TableNameTestRule;
3434
import org.apache.hadoop.hbase.client.Durability;
3535
import org.apache.hadoop.hbase.client.Put;
3636
import org.apache.hadoop.hbase.client.Result;
@@ -68,7 +68,7 @@ public class TestScannerRetriableFailure {
6868
private static final String FAMILY_NAME_STR = "f";
6969
private static final byte[] FAMILY_NAME = Bytes.toBytes(FAMILY_NAME_STR);
7070

71-
@Rule public TestTableName TEST_TABLE = new TestTableName();
71+
@Rule public TableNameTestRule testTable = new TableNameTestRule();
7272

7373
public static class FaultyScannerObserver implements RegionCoprocessor, RegionObserver {
7474
private int faults = 0;
@@ -116,7 +116,7 @@ public static void tearDown() throws Exception {
116116

117117
@Test
118118
public void testFaultyScanner() throws Exception {
119-
TableName tableName = TEST_TABLE.getTableName();
119+
TableName tableName = testTable.getTableName();
120120
Table table = UTIL.createTable(tableName, FAMILY_NAME);
121121
try {
122122
final int NUM_ROWS = 100;

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertNotNull;
2424
import static org.junit.Assert.assertTrue;
25-
2625
import java.util.Arrays;
2726
import java.util.List;
2827
import org.apache.hadoop.conf.Configuration;
@@ -34,8 +33,8 @@
3433
import org.apache.hadoop.hbase.HTableDescriptor;
3534
import org.apache.hadoop.hbase.NamespaceDescriptor;
3635
import org.apache.hadoop.hbase.TableName;
36+
import org.apache.hadoop.hbase.TableNameTestRule;
3737
import org.apache.hadoop.hbase.TableNotFoundException;
38-
import org.apache.hadoop.hbase.TestTableName;
3938
import org.apache.hadoop.hbase.client.Admin;
4039
import org.apache.hadoop.hbase.client.Connection;
4140
import org.apache.hadoop.hbase.client.ConnectionFactory;
@@ -105,7 +104,7 @@ public class TestAccessController2 extends SecureTestUtil {
105104
private static User TESTGROUP2_USER1;
106105

107106
@Rule
108-
public TestTableName TEST_TABLE = new TestTableName();
107+
public TableNameTestRule testTable = new TableNameTestRule();
109108
private String namespace = "testNamespace";
110109
private String tname = namespace + ":testtable1";
111110
private TableName tableName = TableName.valueOf(tname);
@@ -187,7 +186,7 @@ public void testCreateWithCorrectOwner() throws Exception {
187186
verifyAllowed(new AccessTestAction() {
188187
@Override
189188
public Object run() throws Exception {
190-
HTableDescriptor desc = new HTableDescriptor(TEST_TABLE.getTableName());
189+
HTableDescriptor desc = new HTableDescriptor(testTable.getTableName());
191190
desc.addFamily(new HColumnDescriptor(TEST_FAMILY));
192191
try (Connection connection =
193192
ConnectionFactory.createConnection(TEST_UTIL.getConfiguration(), testUser)) {
@@ -198,11 +197,11 @@ public Object run() throws Exception {
198197
return null;
199198
}
200199
}, testUser);
201-
TEST_UTIL.waitTableAvailable(TEST_TABLE.getTableName());
200+
TEST_UTIL.waitTableAvailable(testTable.getTableName());
202201
// Verify that owner permissions have been granted to the test user on the
203202
// table just created
204203
List<UserPermission> perms = PermissionStorage
205-
.getTablePermissions(conf, TEST_TABLE.getTableName()).get(testUser.getShortName());
204+
.getTablePermissions(conf, testTable.getTableName()).get(testUser.getShortName());
206205
assertNotNull(perms);
207206
assertFalse(perms.isEmpty());
208207
// Should be RWXCA
@@ -220,7 +219,7 @@ public void testCreateTableWithGroupPermissions() throws Exception {
220219
AccessTestAction createAction = new AccessTestAction() {
221220
@Override
222221
public Object run() throws Exception {
223-
HTableDescriptor desc = new HTableDescriptor(TEST_TABLE.getTableName());
222+
HTableDescriptor desc = new HTableDescriptor(testTable.getTableName());
224223
desc.addFamily(new HColumnDescriptor(TEST_FAMILY));
225224
try (Connection connection =
226225
ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
@@ -261,27 +260,27 @@ public void testACLTableAccess() throws Exception {
261260
User nsCreate = User.createUserForTesting(conf, "nsCreate", new String[0]);
262261
User nsAdmin = User.createUserForTesting(conf, "nsAdmin", new String[0]);
263262
SecureTestUtil.grantOnNamespace(TEST_UTIL, nsRead.getShortName(),
264-
TEST_TABLE.getTableName().getNamespaceAsString(), Action.READ);
263+
testTable.getTableName().getNamespaceAsString(), Action.READ);
265264
SecureTestUtil.grantOnNamespace(TEST_UTIL, nsWrite.getShortName(),
266-
TEST_TABLE.getTableName().getNamespaceAsString(), Action.WRITE);
265+
testTable.getTableName().getNamespaceAsString(), Action.WRITE);
267266
SecureTestUtil.grantOnNamespace(TEST_UTIL, nsCreate.getShortName(),
268-
TEST_TABLE.getTableName().getNamespaceAsString(), Action.CREATE);
267+
testTable.getTableName().getNamespaceAsString(), Action.CREATE);
269268
SecureTestUtil.grantOnNamespace(TEST_UTIL, nsAdmin.getShortName(),
270-
TEST_TABLE.getTableName().getNamespaceAsString(), Action.ADMIN);
269+
testTable.getTableName().getNamespaceAsString(), Action.ADMIN);
271270

272271
// Table users
273272
User tableRead = User.createUserForTesting(conf, "tableRead", new String[0]);
274273
User tableWrite = User.createUserForTesting(conf, "tableWrite", new String[0]);
275274
User tableCreate = User.createUserForTesting(conf, "tableCreate", new String[0]);
276275
User tableAdmin = User.createUserForTesting(conf, "tableAdmin", new String[0]);
277276
SecureTestUtil.grantOnTable(TEST_UTIL, tableRead.getShortName(),
278-
TEST_TABLE.getTableName(), null, null, Action.READ);
277+
testTable.getTableName(), null, null, Action.READ);
279278
SecureTestUtil.grantOnTable(TEST_UTIL, tableWrite.getShortName(),
280-
TEST_TABLE.getTableName(), null, null, Action.WRITE);
279+
testTable.getTableName(), null, null, Action.WRITE);
281280
SecureTestUtil.grantOnTable(TEST_UTIL, tableCreate.getShortName(),
282-
TEST_TABLE.getTableName(), null, null, Action.CREATE);
281+
testTable.getTableName(), null, null, Action.CREATE);
283282
SecureTestUtil.grantOnTable(TEST_UTIL, tableAdmin.getShortName(),
284-
TEST_TABLE.getTableName(), null, null, Action.ADMIN);
283+
testTable.getTableName(), null, null, Action.ADMIN);
285284

286285
grantGlobal(TEST_UTIL, TESTGROUP_1_NAME, Action.WRITE);
287286
try {

0 commit comments

Comments
 (0)