Skip to content

Commit

Permalink
HBASE-23665: Split unit tests from TestTableName into a separate test…
Browse files Browse the repository at this point in the history
…-only class. (#1032)

Signed-off-by: Nick Dimiduk <ndimiduk@apache.org>
  • Loading branch information
bharathv authored and ndimiduk committed Jan 15, 2020
1 parent 9a708a3 commit 31b4fbd
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 234 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase;

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
* Returns a {@code TableName} based on currently running test method name.
*/
public class TableNameTestRule extends TestWatcher {

private TableName tableName;

@Override
protected void starting(Description description) {
tableName = TableName.valueOf(description.getMethodName());
}

public TableName getTableName() {
return tableName;
}
}
126 changes: 46 additions & 80 deletions hbase-common/src/test/java/org/apache/hadoop/hbase/TestTableName.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -20,99 +20,36 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import static org.junit.Assert.assertThrows;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
* Returns a {@code byte[]} containing the name of the currently running test method.
* Tests for various kinds of TableNames.
*/
@Category({MiscTests.class, MediumTests.class})
public class TestTableName extends TestWatcher {

@Category({MiscTests.class, SmallTests.class})
public class TestTableName {
@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestTableName.class);

private TableName tableName;

/**
* Invoked when a test is about to start
*/
@Override
protected void starting(Description description) {
tableName = TableName.valueOf(description.getMethodName());
}

public TableName getTableName() {
return tableName;
}

String[] emptyNames = {"", " "};
String[] invalidNamespace = {":a", "%:a"};
String[] legalTableNames = {"foo", "with-dash_under.dot", "_under_start_ok",
private static String[] emptyNames = {"", " "};
private static String[] invalidNamespace = {":a", "%:a"};
private static String[] legalTableNames = {"foo", "with-dash_under.dot", "_under_start_ok",
"with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02",
"dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
"trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
String[] illegalTableNames = {".dot_start_illegal", "-dash_start_illegal", "spaces not ok",
"-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
private static String[] illegalTableNames = {".dot_start_illegal", "-dash_start_illegal",
"spaces not ok", "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
"new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};


@Test(expected = IllegalArgumentException.class)
public void testInvalidNamespace() {
for (String tn : invalidNamespace) {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
fail("invalid namespace " + tn
+ " should have failed with IllegalArgumentException for namespace");
}
}

@Test(expected = IllegalArgumentException.class)
public void testEmptyNamespaceName() {
for (String nn : emptyNames) {
TableName.isLegalNamespaceName(Bytes.toBytes(nn));
fail("invalid Namespace name " + nn + " should have failed with IllegalArgumentException");
}
}

@Test(expected = IllegalArgumentException.class)
public void testEmptyTableName() {
for (String tn : emptyNames) {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
fail("invalid tablename " + tn + " should have failed with IllegalArgumentException");
}
}

@Test
public void testLegalHTableNames() {
for (String tn : legalTableNames) {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
}
}

@Test
public void testIllegalHTableNames() {
for (String tn : illegalTableNames) {
try {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
fail("invalid tablename " + tn + " should have failed");
} catch (Exception e) {
// expected
}
}
}

static class Names {
String ns;
byte[] nsb;
Expand Down Expand Up @@ -147,7 +84,6 @@ public boolean equals(Object o) {
if (!tn.equals(names.tn)) {
return false;
}

return true;
}

Expand All @@ -159,7 +95,7 @@ public int hashCode() {
}
}

Names[] names = new Names[] {
private static Names[] names = new Names[] {
new Names("n1", "n1"),
new Names("n2", "n2"),
new Names("table1", "table1"),
Expand All @@ -172,9 +108,41 @@ public int hashCode() {
new Names("n2", "table2")
};

@Test
public void testValueOf() {
@Test public void testInvalidNamespace() {
for (String tn : invalidNamespace) {
assertThrows(IllegalArgumentException.class,
() -> TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn)));
}
}

@Test public void testEmptyNamespaceName() {
for (String nn : emptyNames) {
assertThrows(IllegalArgumentException.class,
() -> TableName.isLegalNamespaceName(Bytes.toBytes(nn)));
}
}

@Test public void testEmptyTableName() {
for (String tn : emptyNames) {
assertThrows(IllegalArgumentException.class,
() -> TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn)));
}
}

@Test public void testLegalHTableNames() {
for (String tn : legalTableNames) {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
}
}

@Test public void testIllegalHTableNames() {
for (String tn : illegalTableNames) {
assertThrows(Exception.class,
() -> TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn)));
}
}

@Test public void testValueOf() {
Map<String, TableName> inCache = new HashMap<>();
// fill cache
for (Names name : names) {
Expand All @@ -188,7 +156,6 @@ public void testValueOf() {
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(
ByteBuffer.wrap(name.nsb), ByteBuffer.wrap(name.tnb)), name));
}

}

private TableName validateNames(TableName expected, Names names) {
Expand All @@ -200,5 +167,4 @@ private TableName validateNames(TableName expected, Names names) {
assertArrayEquals(expected.getNamespace(), names.nsb);
return expected;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TestTableName;
import org.apache.hadoop.hbase.TableNameTestRule;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class TestScannerRetriableFailure {
private static final String FAMILY_NAME_STR = "f";
private static final byte[] FAMILY_NAME = Bytes.toBytes(FAMILY_NAME_STR);

@Rule public TestTableName TEST_TABLE = new TestTableName();
@Rule public TableNameTestRule testTable = new TableNameTestRule();

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

@Test
public void testFaultyScanner() throws Exception {
TableName tableName = TEST_TABLE.getTableName();
TableName tableName = testTable.getTableName();
Table table = UTIL.createTable(tableName, FAMILY_NAME);
try {
final int NUM_ROWS = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
Expand All @@ -34,8 +33,8 @@
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNameTestRule;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.TestTableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
Expand Down Expand Up @@ -105,7 +104,7 @@ public class TestAccessController2 extends SecureTestUtil {
private static User TESTGROUP2_USER1;

@Rule
public TestTableName TEST_TABLE = new TestTableName();
public TableNameTestRule testTable = new TableNameTestRule();
private String namespace = "testNamespace";
private String tname = namespace + ":testtable1";
private TableName tableName = TableName.valueOf(tname);
Expand Down Expand Up @@ -187,7 +186,7 @@ public void testCreateWithCorrectOwner() throws Exception {
verifyAllowed(new AccessTestAction() {
@Override
public Object run() throws Exception {
HTableDescriptor desc = new HTableDescriptor(TEST_TABLE.getTableName());
HTableDescriptor desc = new HTableDescriptor(testTable.getTableName());
desc.addFamily(new HColumnDescriptor(TEST_FAMILY));
try (Connection connection =
ConnectionFactory.createConnection(TEST_UTIL.getConfiguration(), testUser)) {
Expand All @@ -198,11 +197,11 @@ public Object run() throws Exception {
return null;
}
}, testUser);
TEST_UTIL.waitTableAvailable(TEST_TABLE.getTableName());
TEST_UTIL.waitTableAvailable(testTable.getTableName());
// Verify that owner permissions have been granted to the test user on the
// table just created
List<UserPermission> perms = PermissionStorage
.getTablePermissions(conf, TEST_TABLE.getTableName()).get(testUser.getShortName());
.getTablePermissions(conf, testTable.getTableName()).get(testUser.getShortName());
assertNotNull(perms);
assertFalse(perms.isEmpty());
// Should be RWXCA
Expand All @@ -220,7 +219,7 @@ public void testCreateTableWithGroupPermissions() throws Exception {
AccessTestAction createAction = new AccessTestAction() {
@Override
public Object run() throws Exception {
HTableDescriptor desc = new HTableDescriptor(TEST_TABLE.getTableName());
HTableDescriptor desc = new HTableDescriptor(testTable.getTableName());
desc.addFamily(new HColumnDescriptor(TEST_FAMILY));
try (Connection connection =
ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
Expand Down Expand Up @@ -261,27 +260,27 @@ public void testACLTableAccess() throws Exception {
User nsCreate = User.createUserForTesting(conf, "nsCreate", new String[0]);
User nsAdmin = User.createUserForTesting(conf, "nsAdmin", new String[0]);
SecureTestUtil.grantOnNamespace(TEST_UTIL, nsRead.getShortName(),
TEST_TABLE.getTableName().getNamespaceAsString(), Action.READ);
testTable.getTableName().getNamespaceAsString(), Action.READ);
SecureTestUtil.grantOnNamespace(TEST_UTIL, nsWrite.getShortName(),
TEST_TABLE.getTableName().getNamespaceAsString(), Action.WRITE);
testTable.getTableName().getNamespaceAsString(), Action.WRITE);
SecureTestUtil.grantOnNamespace(TEST_UTIL, nsCreate.getShortName(),
TEST_TABLE.getTableName().getNamespaceAsString(), Action.CREATE);
testTable.getTableName().getNamespaceAsString(), Action.CREATE);
SecureTestUtil.grantOnNamespace(TEST_UTIL, nsAdmin.getShortName(),
TEST_TABLE.getTableName().getNamespaceAsString(), Action.ADMIN);
testTable.getTableName().getNamespaceAsString(), Action.ADMIN);

// Table users
User tableRead = User.createUserForTesting(conf, "tableRead", new String[0]);
User tableWrite = User.createUserForTesting(conf, "tableWrite", new String[0]);
User tableCreate = User.createUserForTesting(conf, "tableCreate", new String[0]);
User tableAdmin = User.createUserForTesting(conf, "tableAdmin", new String[0]);
SecureTestUtil.grantOnTable(TEST_UTIL, tableRead.getShortName(),
TEST_TABLE.getTableName(), null, null, Action.READ);
testTable.getTableName(), null, null, Action.READ);
SecureTestUtil.grantOnTable(TEST_UTIL, tableWrite.getShortName(),
TEST_TABLE.getTableName(), null, null, Action.WRITE);
testTable.getTableName(), null, null, Action.WRITE);
SecureTestUtil.grantOnTable(TEST_UTIL, tableCreate.getShortName(),
TEST_TABLE.getTableName(), null, null, Action.CREATE);
testTable.getTableName(), null, null, Action.CREATE);
SecureTestUtil.grantOnTable(TEST_UTIL, tableAdmin.getShortName(),
TEST_TABLE.getTableName(), null, null, Action.ADMIN);
testTable.getTableName(), null, null, Action.ADMIN);

grantGlobal(TEST_UTIL, TESTGROUP_1_NAME, Action.WRITE);
try {
Expand Down
Loading

0 comments on commit 31b4fbd

Please sign in to comment.