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 <[email protected]>
  • Loading branch information
bharathv committed Jan 16, 2020
1 parent 99a328f commit 75c594a
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 193 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;
}
}
186 changes: 186 additions & 0 deletions hbase-common/src/test/java/org/apache/hadoop/hbase/TestTableName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* 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 static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import org.junit.experimental.categories.Category;

/**
* Tests for various kinds of TableNames.
*/
@Category({MiscTests.class, SmallTests.class})
public class TestTableName {

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"};
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"};

static class Names {
String ns;
byte[] nsb;
String tn;
byte[] tnb;
String nn;
byte[] nnb;

Names(String ns, String tn) {
this.ns = ns;
nsb = Bytes.toBytes(ns);
this.tn = tn;
tnb = Bytes.toBytes(tn);
nn = this.ns + ":" + this.tn;
nnb = Bytes.toBytes(nn);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Names names = (Names) o;

if (!ns.equals(names.ns)) {
return false;
}
if (!tn.equals(names.tn)) {
return false;
}
return true;
}

@Override
public int hashCode() {
int result = ns.hashCode();
result = 31 * result + tn.hashCode();
return result;
}
}

private static Names[] names = new Names[] {
new Names("n1", "n1"),
new Names("n2", "n2"),
new Names("table1", "table1"),
new Names("table2", "table2"),
new Names("table2", "table1"),
new Names("table1", "table2"),
new Names("n1", "table1"),
new Names("n1", "table1"),
new Names("n2", "table2"),
new Names("n2", "table2")
};

@Test public void testInvalidNamespace() {
for (String tn : invalidNamespace) {
try {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
} catch (IllegalArgumentException ie) {
// expected, pass
continue;
}
fail("Exception not thrown for ns: " + tn);
}
}

@Test public void testEmptyNamespaceName() {
for (String nn : emptyNames) {
try {
TableName.isLegalNamespaceName(Bytes.toBytes(nn));
} catch (IllegalArgumentException iae) {
// expected, pass
continue;
}
fail("Exception not thrown for ns: " + nn);
}
}

@Test public void testEmptyTableName() {
for (String tn : emptyNames) {
try {
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
} catch (IllegalArgumentException iae) {
// expected, pass
continue;
}
fail("Exception not thrown for table name: " + tn);
}
}

@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));
} catch (Exception e) {
// expected, pass
continue;
}
fail("Exception not thrown for table name: " + tn);
}
}

@Test public void testValueOf() {
Map<String, TableName> inCache = new HashMap<>();
// fill cache
for (Names name : names) {
inCache.put(name.nn, TableName.valueOf(name.ns, name.tn));
}
for (Names name : names) {
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.ns, name.tn), name));
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nsb, name.tnb), name));
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nn), name));
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nnb), name));
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(
ByteBuffer.wrap(name.nsb), ByteBuffer.wrap(name.tnb)), name));
}
}

private TableName validateNames(TableName expected, Names names) {
assertEquals(expected.getNameAsString(), names.nn);
assertArrayEquals(expected.getName(), names.nnb);
assertEquals(expected.getQualifierAsString(), names.tn);
assertArrayEquals(expected.getQualifier(), names.tnb);
assertEquals(expected.getNamespaceAsString(), names.ns);
assertArrayEquals(expected.getNamespace(), names.nsb);
return expected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,36 @@

package org.apache.hadoop.hbase.regionserver;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
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;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.testclassification.LargeTests;
import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.FSUtils;
import org.apache.hadoop.hbase.util.FSVisitor;
import org.apache.hadoop.hbase.util.TestTableName;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@Category(LargeTests.class)
public class TestScannerRetriableFailure {
private static final Log LOG = LogFactory.getLog(TestScannerRetriableFailure.class);
Expand All @@ -66,7 +57,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 extends BaseRegionObserver {
private int faults = 0;
Expand Down Expand Up @@ -109,7 +100,7 @@ public static void tearDown() throws Exception {

@Test(timeout=180000)
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 Expand Up @@ -158,7 +149,9 @@ private void checkTableRows(final Table table, int numRows) throws Exception {

while (true) {
Result result = scanner.next();
if (result == null) break;
if (result == null) {
break;
}
count++;
}
assertEquals(numRows, count);
Expand Down
Loading

0 comments on commit 75c594a

Please sign in to comment.