View Javadoc

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.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   * Tests the HColumnDescriptor with appropriate arguments
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      // Valid values for replication scope are 0 or 1.
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    /** Tests HColumnDescriptor with empty familyName*/
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     * Test that we add and remove strings from configuration properly.
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     /* HColumnDescriptor should have a builder style setup where setXXX/addXXX methods
110      * can be chainable together:
111      * . For example:
112      * HColumnDescriptor hcd
113      *   = new HColumnDescriptor()
114      *     .setFoo(foo)
115      *     .setBar(bar)
116      *     .setBuz(buz)
117      *
118      * This test ensures that all methods starting with "set" returns the declaring object
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     // test set value
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     // test set configuration
143     hcd.setConfiguration(testConf, "1");
144     assertEquals("1", hcd.getConfigurationValue(testConf));
145     hcd.setConfiguration(testConf, "");
146     assertNull(hcd.getConfigurationValue(testConf));
147   }
148 }