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.assertTrue;
22  
23  import org.apache.hadoop.hbase.exceptions.DeserializationException;
24  import org.apache.hadoop.hbase.io.compress.Compression;
25  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
26  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
27  import org.apache.hadoop.hbase.regionserver.BloomType;
28  import org.junit.experimental.categories.Category;
29  
30  import org.junit.Test;
31  
32  /** Tests the HColumnDescriptor with appropriate arguments */
33  @Category(SmallTests.class)
34  public class TestHColumnDescriptor {
35    @Test
36    public void testPb() throws DeserializationException {
37      HColumnDescriptor hcd = new HColumnDescriptor(
38        HTableDescriptor.META_TABLEDESC.getColumnFamilies()[0]);
39      final int v = 123;
40      hcd.setBlocksize(v);
41      hcd.setTimeToLive(v);
42      hcd.setBlockCacheEnabled(!HColumnDescriptor.DEFAULT_BLOCKCACHE);
43      hcd.setValue("a", "b");
44      hcd.setMaxVersions(v);
45      assertEquals(v, hcd.getMaxVersions());
46      hcd.setMinVersions(v);
47      assertEquals(v, hcd.getMinVersions());
48      hcd.setKeepDeletedCells(!HColumnDescriptor.DEFAULT_KEEP_DELETED);
49      hcd.setInMemory(!HColumnDescriptor.DEFAULT_IN_MEMORY);
50      boolean inmemory = hcd.isInMemory();
51      hcd.setScope(v);
52      hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
53      hcd.setBloomFilterType(BloomType.ROW);
54      hcd.setCompressionType(Algorithm.SNAPPY);
55  
56  
57      byte [] bytes = hcd.toByteArray();
58      HColumnDescriptor deserializedHcd = HColumnDescriptor.parseFrom(bytes);
59      assertTrue(hcd.equals(deserializedHcd));
60      assertEquals(v, hcd.getBlocksize());
61      assertEquals(v, hcd.getTimeToLive());
62      assertEquals(hcd.getValue("a"), deserializedHcd.getValue("a"));
63      assertEquals(hcd.getMaxVersions(), deserializedHcd.getMaxVersions());
64      assertEquals(hcd.getMinVersions(), deserializedHcd.getMinVersions());
65      assertEquals(hcd.getKeepDeletedCells(), deserializedHcd.getKeepDeletedCells());
66      assertEquals(inmemory, deserializedHcd.isInMemory());
67      assertEquals(hcd.getScope(), deserializedHcd.getScope());
68      assertTrue(deserializedHcd.getCompressionType().equals(Compression.Algorithm.SNAPPY));
69      assertTrue(deserializedHcd.getDataBlockEncoding().equals(DataBlockEncoding.FAST_DIFF));
70      assertTrue(deserializedHcd.getBloomFilterType().equals(BloomType.ROW));
71    }
72  
73    @Test
74    /** Tests HColumnDescriptor with empty familyName*/
75    public void testHColumnDescriptorShouldThrowIAEWhenFamiliyNameEmpty()
76        throws Exception {
77      try {
78        new HColumnDescriptor("".getBytes());
79      } catch (IllegalArgumentException e) {
80        assertEquals("Family name can not be empty", e.getLocalizedMessage());
81      }
82    }
83  
84    /**
85     * Test that we add and remove strings from configuration properly.
86     */
87    @Test
88    public void testAddGetRemoveConfiguration() throws Exception {
89      HColumnDescriptor desc = new HColumnDescriptor("foo");
90      String key = "Some";
91      String value = "value";
92      desc.setConfiguration(key, value);
93      assertEquals(value, desc.getConfigurationValue(key));
94      desc.removeConfiguration(key);
95      assertEquals(null, desc.getConfigurationValue(key));
96    }
97  }