1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import static org.junit.Assert.fail;
21
22 import org.apache.hadoop.hbase.SmallTests;
23 import org.apache.hadoop.hbase.TableName;
24 import org.apache.hadoop.hbase.util.Bytes;
25 import org.junit.Test;
26 import org.junit.experimental.categories.Category;
27 import org.junit.rules.TestWatcher;
28 import org.junit.runner.Description;
29
30
31
32
33 @Category(SmallTests.class)
34 public class TestTableName extends TestWatcher {
35 private TableName tableName;
36
37
38
39
40 @Override
41 protected void starting(Description description) {
42 tableName = TableName.valueOf(description.getMethodName());
43 }
44
45 public TableName getTableName() {
46 return tableName;
47 }
48
49 String emptyTableNames[] ={"", " "};
50 String invalidNamespace[] = {":a", "%:a"};
51 String legalTableNames[] = { "foo", "with-dash_under.dot", "_under_start_ok",
52 "with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02"
53 , "dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
54 "trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
55 String illegalTableNames[] = { ".dot_start_illegal", "-dash_start_illegal", "spaces not ok",
56 "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
57 "new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};
58
59
60 @Test(expected = IllegalArgumentException.class)
61 public void testInvalidNamespace() {
62 for (String tn : invalidNamespace) {
63 TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
64 fail("invalid namespace " + tn + " should have failed with IllegalArgumentException for namespace");
65 }
66 }
67
68 @Test(expected = IllegalArgumentException.class)
69 public void testEmptyTableName() {
70 for (String tn : emptyTableNames) {
71 TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
72 fail("invalid tablename " + tn + " should have failed with IllegalArgumentException");
73 }
74 }
75
76 @Test
77 public void testLegalHTableNames() {
78 for (String tn : legalTableNames) {
79 TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
80 }
81 }
82
83 @Test
84 public void testIllegalHTableNames() {
85 for (String tn : illegalTableNames) {
86 try {
87 TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
88 fail("invalid tablename " + tn + " should have failed");
89 } catch (Exception e) {
90
91 }
92 }
93 }
94
95 }