Skip to content

Commit 75c594a

Browse files
committed
HBASE-23665: Split unit tests from TestTableName into a separate test-only class. (#1032)
Signed-off-by: Nick Dimiduk <[email protected]>
1 parent 99a328f commit 75c594a

File tree

10 files changed

+395
-193
lines changed

10 files changed

+395
-193
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: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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 static org.junit.Assert.assertArrayEquals;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.fail;
24+
import java.nio.ByteBuffer;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import org.apache.hadoop.hbase.testclassification.MiscTests;
28+
import org.apache.hadoop.hbase.testclassification.SmallTests;
29+
import org.apache.hadoop.hbase.util.Bytes;
30+
import org.junit.Test;
31+
import org.junit.experimental.categories.Category;
32+
33+
/**
34+
* Tests for various kinds of TableNames.
35+
*/
36+
@Category({MiscTests.class, SmallTests.class})
37+
public class TestTableName {
38+
39+
private static String[] emptyNames = {"", " "};
40+
private static String[] invalidNamespace = {":a", "%:a"};
41+
private static String[] legalTableNames = {"foo", "with-dash_under.dot", "_under_start_ok",
42+
"with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02",
43+
"dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
44+
"trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
45+
private static String[] illegalTableNames = {".dot_start_illegal", "-dash_start_illegal",
46+
"spaces not ok", "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
47+
"new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};
48+
49+
static class Names {
50+
String ns;
51+
byte[] nsb;
52+
String tn;
53+
byte[] tnb;
54+
String nn;
55+
byte[] nnb;
56+
57+
Names(String ns, String tn) {
58+
this.ns = ns;
59+
nsb = Bytes.toBytes(ns);
60+
this.tn = tn;
61+
tnb = Bytes.toBytes(tn);
62+
nn = this.ns + ":" + this.tn;
63+
nnb = Bytes.toBytes(nn);
64+
}
65+
66+
@Override
67+
public boolean equals(Object o) {
68+
if (this == o) {
69+
return true;
70+
}
71+
if (o == null || getClass() != o.getClass()) {
72+
return false;
73+
}
74+
75+
Names names = (Names) o;
76+
77+
if (!ns.equals(names.ns)) {
78+
return false;
79+
}
80+
if (!tn.equals(names.tn)) {
81+
return false;
82+
}
83+
return true;
84+
}
85+
86+
@Override
87+
public int hashCode() {
88+
int result = ns.hashCode();
89+
result = 31 * result + tn.hashCode();
90+
return result;
91+
}
92+
}
93+
94+
private static Names[] names = new Names[] {
95+
new Names("n1", "n1"),
96+
new Names("n2", "n2"),
97+
new Names("table1", "table1"),
98+
new Names("table2", "table2"),
99+
new Names("table2", "table1"),
100+
new Names("table1", "table2"),
101+
new Names("n1", "table1"),
102+
new Names("n1", "table1"),
103+
new Names("n2", "table2"),
104+
new Names("n2", "table2")
105+
};
106+
107+
@Test public void testInvalidNamespace() {
108+
for (String tn : invalidNamespace) {
109+
try {
110+
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
111+
} catch (IllegalArgumentException ie) {
112+
// expected, pass
113+
continue;
114+
}
115+
fail("Exception not thrown for ns: " + tn);
116+
}
117+
}
118+
119+
@Test public void testEmptyNamespaceName() {
120+
for (String nn : emptyNames) {
121+
try {
122+
TableName.isLegalNamespaceName(Bytes.toBytes(nn));
123+
} catch (IllegalArgumentException iae) {
124+
// expected, pass
125+
continue;
126+
}
127+
fail("Exception not thrown for ns: " + nn);
128+
}
129+
}
130+
131+
@Test public void testEmptyTableName() {
132+
for (String tn : emptyNames) {
133+
try {
134+
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
135+
} catch (IllegalArgumentException iae) {
136+
// expected, pass
137+
continue;
138+
}
139+
fail("Exception not thrown for table name: " + tn);
140+
}
141+
}
142+
143+
@Test public void testLegalHTableNames() {
144+
for (String tn : legalTableNames) {
145+
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
146+
}
147+
}
148+
149+
@Test public void testIllegalHTableNames() {
150+
for (String tn : illegalTableNames) {
151+
try {
152+
TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
153+
} catch (Exception e) {
154+
// expected, pass
155+
continue;
156+
}
157+
fail("Exception not thrown for table name: " + tn);
158+
}
159+
}
160+
161+
@Test public void testValueOf() {
162+
Map<String, TableName> inCache = new HashMap<>();
163+
// fill cache
164+
for (Names name : names) {
165+
inCache.put(name.nn, TableName.valueOf(name.ns, name.tn));
166+
}
167+
for (Names name : names) {
168+
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.ns, name.tn), name));
169+
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nsb, name.tnb), name));
170+
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nn), name));
171+
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(name.nnb), name));
172+
assertSame(inCache.get(name.nn), validateNames(TableName.valueOf(
173+
ByteBuffer.wrap(name.nsb), ByteBuffer.wrap(name.tnb)), name));
174+
}
175+
}
176+
177+
private TableName validateNames(TableName expected, Names names) {
178+
assertEquals(expected.getNameAsString(), names.nn);
179+
assertArrayEquals(expected.getName(), names.nnb);
180+
assertEquals(expected.getQualifierAsString(), names.tn);
181+
assertArrayEquals(expected.getQualifier(), names.tnb);
182+
assertEquals(expected.getNamespaceAsString(), names.ns);
183+
assertArrayEquals(expected.getNamespace(), names.nsb);
184+
return expected;
185+
}
186+
}

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,36 @@
1818

1919
package org.apache.hadoop.hbase.regionserver;
2020

21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertTrue;
2123
import java.io.IOException;
22-
import java.util.ArrayList;
2324
import java.util.List;
24-
2525
import org.apache.commons.logging.Log;
2626
import org.apache.commons.logging.LogFactory;
2727
import org.apache.hadoop.conf.Configuration;
2828
import org.apache.hadoop.fs.FileSystem;
2929
import org.apache.hadoop.fs.Path;
3030
import org.apache.hadoop.hbase.HBaseTestingUtility;
3131
import org.apache.hadoop.hbase.TableName;
32-
import org.apache.hadoop.hbase.client.Scan;
33-
import org.apache.hadoop.hbase.client.Result;
34-
import org.apache.hadoop.hbase.client.ResultScanner;
32+
import org.apache.hadoop.hbase.TableNameTestRule;
3533
import org.apache.hadoop.hbase.client.Durability;
3634
import org.apache.hadoop.hbase.client.Put;
35+
import org.apache.hadoop.hbase.client.Result;
36+
import org.apache.hadoop.hbase.client.ResultScanner;
37+
import org.apache.hadoop.hbase.client.Scan;
3738
import org.apache.hadoop.hbase.client.Table;
38-
import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
3939
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
40+
import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
4041
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
4142
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
4243
import org.apache.hadoop.hbase.testclassification.LargeTests;
43-
import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
4444
import org.apache.hadoop.hbase.util.Bytes;
45-
import org.apache.hadoop.hbase.util.FSUtils;
46-
import org.apache.hadoop.hbase.util.FSVisitor;
47-
import org.apache.hadoop.hbase.util.TestTableName;
48-
4945
import org.junit.AfterClass;
5046
import org.junit.BeforeClass;
5147
import org.junit.Rule;
5248
import org.junit.Test;
5349
import org.junit.experimental.categories.Category;
5450

55-
import static org.junit.Assert.assertEquals;
56-
import static org.junit.Assert.assertFalse;
57-
import static org.junit.Assert.assertTrue;
58-
import static org.junit.Assert.fail;
59-
6051
@Category(LargeTests.class)
6152
public class TestScannerRetriableFailure {
6253
private static final Log LOG = LogFactory.getLog(TestScannerRetriableFailure.class);
@@ -66,7 +57,7 @@ public class TestScannerRetriableFailure {
6657
private static final String FAMILY_NAME_STR = "f";
6758
private static final byte[] FAMILY_NAME = Bytes.toBytes(FAMILY_NAME_STR);
6859

69-
@Rule public TestTableName TEST_TABLE = new TestTableName();
60+
@Rule public TableNameTestRule testTable = new TableNameTestRule();
7061

7162
public static class FaultyScannerObserver extends BaseRegionObserver {
7263
private int faults = 0;
@@ -109,7 +100,7 @@ public static void tearDown() throws Exception {
109100

110101
@Test(timeout=180000)
111102
public void testFaultyScanner() throws Exception {
112-
TableName tableName = TEST_TABLE.getTableName();
103+
TableName tableName = testTable.getTableName();
113104
Table table = UTIL.createTable(tableName, FAMILY_NAME);
114105
try {
115106
final int NUM_ROWS = 100;
@@ -158,7 +149,9 @@ private void checkTableRows(final Table table, int numRows) throws Exception {
158149

159150
while (true) {
160151
Result result = scanner.next();
161-
if (result == null) break;
152+
if (result == null) {
153+
break;
154+
}
162155
count++;
163156
}
164157
assertEquals(numRows, count);

0 commit comments

Comments
 (0)