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.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.io.IOException;
26 import java.util.regex.Pattern;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.hbase.client.Durability;
31 import org.apache.hadoop.hbase.exceptions.DeserializationException;
32 import org.apache.hadoop.hbase.testclassification.SmallTests;
33 import org.apache.hadoop.hbase.util.BuilderStyleTest;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37
38
39
40
41 @Category(SmallTests.class)
42 public class TestHTableDescriptor {
43 private static final Log LOG = LogFactory.getLog(TestHTableDescriptor.class);
44
45 @Test (expected=IOException.class)
46 public void testAddCoprocessorTwice() throws IOException {
47 HTableDescriptor htd = new HTableDescriptor(TableName.META_TABLE_NAME);
48 String cpName = "a.b.c.d";
49 htd.addCoprocessor(cpName);
50 htd.addCoprocessor(cpName);
51 }
52
53 @Test
54 public void testAddCoprocessorWithSpecStr() throws IOException {
55 HTableDescriptor htd = new HTableDescriptor(TableName.META_TABLE_NAME);
56 String cpName = "a.b.c.d";
57 try {
58 htd.addCoprocessorWithSpec(cpName);
59 fail();
60 } catch (IllegalArgumentException iae) {
61
62 }
63
64
65 try {
66 htd.addCoprocessorWithSpec("file:///some/path" + "|" + cpName);
67 fail();
68 } catch (IllegalArgumentException iae) {
69
70 }
71
72
73 String spec = "hdfs:///foo.jar|com.foo.FooRegionObserver|1001|arg1=1,arg2=2";
74 try {
75 htd.addCoprocessorWithSpec(spec);
76 } catch (IllegalArgumentException iae) {
77 fail();
78 }
79
80
81 try {
82 htd.addCoprocessorWithSpec(spec);
83 fail();
84 } catch (IOException ioe) {
85
86 }
87 }
88
89 @Test
90 public void testPb() throws DeserializationException, IOException {
91 HTableDescriptor htd = new HTableDescriptor(HTableDescriptor.META_TABLEDESC);
92 final int v = 123;
93 htd.setMaxFileSize(v);
94 htd.setDurability(Durability.ASYNC_WAL);
95 htd.setReadOnly(true);
96 htd.setRegionReplication(2);
97 byte [] bytes = htd.toByteArray();
98 HTableDescriptor deserializedHtd = HTableDescriptor.parseFrom(bytes);
99 assertEquals(htd, deserializedHtd);
100 assertEquals(v, deserializedHtd.getMaxFileSize());
101 assertTrue(deserializedHtd.isReadOnly());
102 assertEquals(Durability.ASYNC_WAL, deserializedHtd.getDurability());
103 assertEquals(deserializedHtd.getRegionReplication(), 2);
104 }
105
106
107
108
109
110
111 @Test
112 public void testGetSetRemoveCP() throws Exception {
113 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
114
115 String className = "org.apache.hadoop.hbase.coprocessor.BaseRegionObserver";
116
117 desc.addCoprocessor(className);
118 assertTrue(desc.hasCoprocessor(className));
119
120 desc.removeCoprocessor(className);
121 assertFalse(desc.hasCoprocessor(className));
122 }
123
124
125
126
127
128
129 @Test
130 public void testSetListRemoveCP() throws Exception {
131 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("testGetSetRemoveCP"));
132
133 String className1 = "org.apache.hadoop.hbase.coprocessor.BaseRegionObserver";
134 String className2 = "org.apache.hadoop.hbase.coprocessor.SampleRegionWALObserver";
135
136 assertTrue(desc.getCoprocessors().size() == 0);
137
138
139 desc.addCoprocessor(className1);
140 assertTrue(desc.getCoprocessors().size() == 1);
141 assertTrue(desc.getCoprocessors().contains(className1));
142
143
144
145 desc.addCoprocessor(className2);
146 assertTrue(desc.getCoprocessors().size() == 2);
147 assertTrue(desc.getCoprocessors().contains(className2));
148
149
150 desc.removeCoprocessor(className1);
151 assertTrue(desc.getCoprocessors().size() == 1);
152 assertFalse(desc.getCoprocessors().contains(className1));
153 assertTrue(desc.getCoprocessors().contains(className2));
154
155
156 desc.removeCoprocessor(className2);
157 assertTrue(desc.getCoprocessors().size() == 0);
158 assertFalse(desc.getCoprocessors().contains(className1));
159 assertFalse(desc.getCoprocessors().contains(className2));
160 }
161
162
163
164
165 @Test
166 public void testRemoveString() {
167 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
168 String key = "Some";
169 String value = "value";
170 desc.setValue(key, value);
171 assertEquals(value, desc.getValue(key));
172 desc.remove(key);
173 assertEquals(null, desc.getValue(key));
174 }
175
176 String[] legalTableNames = { "foo", "with-dash_under.dot", "_under_start_ok",
177 "with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02",
178 "dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
179 "trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
180 String[] illegalTableNames = { ".dot_start_illegal", "-dash_start_illegal", "spaces not ok",
181 "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
182 "new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};
183
184 @Test
185 public void testLegalHTableNames() {
186 for (String tn : legalTableNames) {
187 TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
188 }
189 }
190
191 @Test
192 public void testIllegalHTableNames() {
193 for (String tn : illegalTableNames) {
194 try {
195 TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
196 fail("invalid tablename " + tn + " should have failed");
197 } catch (Exception e) {
198
199 }
200 }
201 }
202
203 @Test
204 public void testLegalHTableNamesRegex() {
205 for (String tn : legalTableNames) {
206 TableName tName = TableName.valueOf(tn);
207 assertTrue("Testing: '" + tn + "'", Pattern.matches(TableName.VALID_USER_TABLE_REGEX,
208 tName.getNameAsString()));
209 }
210 }
211
212 @Test
213 public void testIllegalHTableNamesRegex() {
214 for (String tn : illegalTableNames) {
215 LOG.info("Testing: '" + tn + "'");
216 assertFalse(Pattern.matches(TableName.VALID_USER_TABLE_REGEX, tn));
217 }
218 }
219
220
221
222
223 @Test
224 public void testGetMaxFileSize() {
225 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
226 assertEquals(-1, desc.getMaxFileSize());
227 desc.setMaxFileSize(1111L);
228 assertEquals(1111L, desc.getMaxFileSize());
229 }
230
231
232
233
234 @Test
235 public void testGetMemStoreFlushSize() {
236 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
237 assertEquals(-1, desc.getMemStoreFlushSize());
238 desc.setMemStoreFlushSize(1111L);
239 assertEquals(1111L, desc.getMemStoreFlushSize());
240 }
241
242
243
244
245 @Test
246 public void testAddGetRemoveConfiguration() throws Exception {
247 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
248 String key = "Some";
249 String value = "value";
250 desc.setConfiguration(key, value);
251 assertEquals(value, desc.getConfigurationValue(key));
252 desc.removeConfiguration(key);
253 assertEquals(null, desc.getConfigurationValue(key));
254 }
255
256 @Test
257 public void testClassMethodsAreBuilderStyle() {
258
259
260
261
262
263
264
265
266
267
268
269
270 BuilderStyleTest.assertClassesAreBuilderStyle(HTableDescriptor.class);
271 }
272
273 @Test
274 public void testModifyFamily() {
275 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("table"));
276 byte[] familyName = Bytes.toBytes("cf");
277 HColumnDescriptor hcd = new HColumnDescriptor(familyName);
278 hcd.setBlocksize(1000);
279 hcd.setDFSReplication((short) 3);
280 htd.addFamily(hcd);
281 assertEquals(1000, htd.getFamily(familyName).getBlocksize());
282 assertEquals(3, htd.getFamily(familyName).getDFSReplication());
283 hcd = new HColumnDescriptor(familyName);
284 hcd.setBlocksize(2000);
285 hcd.setDFSReplication((short) 1);
286 htd.modifyFamily(hcd);
287 assertEquals(2000, htd.getFamily(familyName).getBlocksize());
288 assertEquals(1, htd.getFamily(familyName).getDFSReplication());
289 }
290
291 @Test(expected=IllegalArgumentException.class)
292 public void testModifyInexistentFamily() {
293 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("table"));
294 byte[] familyName = Bytes.toBytes("cf");
295 HColumnDescriptor hcd = new HColumnDescriptor(familyName);
296 htd.modifyFamily(hcd);
297 }
298
299 @Test(expected=IllegalArgumentException.class)
300 public void testAddDuplicateFamilies() {
301 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("table"));
302 byte[] familyName = Bytes.toBytes("cf");
303 HColumnDescriptor hcd = new HColumnDescriptor(familyName);
304 hcd.setBlocksize(1000);
305 htd.addFamily(hcd);
306 assertEquals(1000, htd.getFamily(familyName).getBlocksize());
307 hcd = new HColumnDescriptor(familyName);
308 hcd.setBlocksize(2000);
309 htd.addFamily(hcd);
310 }
311
312 @Test
313 public void testPriority() {
314 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("table"));
315 htd.setPriority(42);
316 assertEquals(42, htd.getPriority());
317 }
318 }