1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
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
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
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
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 }