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.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import org.apache.hadoop.hbase.exceptions.DeserializationException;
26 import org.apache.hadoop.hbase.io.compress.Compression;
27 import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
28 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
29 import org.apache.hadoop.hbase.regionserver.BloomType;
30 import org.apache.hadoop.hbase.testclassification.SmallTests;
31 import org.apache.hadoop.hbase.util.BuilderStyleTest;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36
37
38
39 @Category(SmallTests.class)
40 public class TestHColumnDescriptor {
41 @Test
42 public void testPb() throws DeserializationException {
43 HColumnDescriptor hcd = new HColumnDescriptor(
44 HTableDescriptor.META_TABLEDESC.getColumnFamilies()[0]);
45 final int v = 123;
46 hcd.setBlocksize(v);
47 hcd.setTimeToLive(v);
48 hcd.setBlockCacheEnabled(!HColumnDescriptor.DEFAULT_BLOCKCACHE);
49 hcd.setValue("a", "b");
50 hcd.setMaxVersions(v);
51 assertEquals(v, hcd.getMaxVersions());
52 hcd.setMinVersions(v);
53 assertEquals(v, hcd.getMinVersions());
54 hcd.setKeepDeletedCells(KeepDeletedCells.TRUE);
55 hcd.setInMemory(!HColumnDescriptor.DEFAULT_IN_MEMORY);
56 boolean inmemory = hcd.isInMemory();
57
58 hcd.setScope(0);
59 hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
60 hcd.setBloomFilterType(BloomType.ROW);
61 hcd.setCompressionType(Algorithm.SNAPPY);
62 hcd.setDFSReplication((short) v);
63
64 byte [] bytes = hcd.toByteArray();
65 HColumnDescriptor deserializedHcd = HColumnDescriptor.parseFrom(bytes);
66 assertTrue(hcd.equals(deserializedHcd));
67 assertEquals(v, hcd.getBlocksize());
68 assertEquals(v, hcd.getTimeToLive());
69 assertEquals(hcd.getValue("a"), deserializedHcd.getValue("a"));
70 assertEquals(hcd.getMaxVersions(), deserializedHcd.getMaxVersions());
71 assertEquals(hcd.getMinVersions(), deserializedHcd.getMinVersions());
72 assertEquals(hcd.getKeepDeletedCells(), deserializedHcd.getKeepDeletedCells());
73 assertEquals(inmemory, deserializedHcd.isInMemory());
74 assertEquals(hcd.getScope(), deserializedHcd.getScope());
75 assertTrue(deserializedHcd.getCompressionType().equals(Compression.Algorithm.SNAPPY));
76 assertTrue(deserializedHcd.getDataBlockEncoding().equals(DataBlockEncoding.FAST_DIFF));
77 assertTrue(deserializedHcd.getBloomFilterType().equals(BloomType.ROW));
78 assertEquals(v, deserializedHcd.getDFSReplication());
79 }
80
81 @Test
82
83 public void testHColumnDescriptorShouldThrowIAEWhenFamiliyNameEmpty()
84 throws Exception {
85 try {
86 new HColumnDescriptor(Bytes.toBytes(""));
87 fail("Did not throw");
88 } catch (IllegalArgumentException e) {
89 assertEquals("Family name can not be empty", e.getLocalizedMessage());
90 }
91 }
92
93
94
95
96 @Test
97 public void testAddGetRemoveConfiguration() throws Exception {
98 HColumnDescriptor desc = new HColumnDescriptor("foo");
99 String key = "Some";
100 String value = "value";
101 desc.setConfiguration(key, value);
102 assertEquals(value, desc.getConfigurationValue(key));
103 desc.removeConfiguration(key);
104 assertEquals(null, desc.getConfigurationValue(key));
105 }
106
107 @Test
108 public void testClassMethodsAreBuilderStyle() {
109
110
111
112
113
114
115
116
117
118
119
120
121 BuilderStyleTest.assertClassesAreBuilderStyle(HColumnDescriptor.class);
122 }
123
124 @Test(expected=IllegalArgumentException.class)
125 public void testInvalidReplicationScope() {
126 HColumnDescriptor column = new HColumnDescriptor("f1");
127 column.setScope(5);
128 }
129
130 @Test
131 public void testSetEmptyValue() {
132 HColumnDescriptor hcd = new HColumnDescriptor(
133 HTableDescriptor.META_TABLEDESC.getColumnFamilies()[0]);
134 String testConf = "TestConfiguration";
135 String testValue = "TestValue";
136
137 hcd.setValue(testValue, "2");
138 assertEquals("2", Bytes.toString(hcd.getValue(Bytes.toBytes(testValue))));
139 hcd.setValue(testValue, "");
140 assertNull(hcd.getValue(Bytes.toBytes(testValue)));
141
142
143 hcd.setConfiguration(testConf, "1");
144 assertEquals("1", hcd.getConfigurationValue(testConf));
145 hcd.setConfiguration(testConf, "");
146 assertNull(hcd.getConfigurationValue(testConf));
147 }
148 }