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.apache.hadoop.hbase.testclassification.SmallTests;
29  import org.apache.hadoop.hbase.util.BuilderStyleTest;
30  import org.junit.experimental.categories.Category;
31  import org.junit.Test;
32  
33  /** Tests the HColumnDescriptor with appropriate arguments */
34  @Category(SmallTests.class)
35  public class TestHColumnDescriptor {
36    @Test
37    public void testPb() throws DeserializationException {
38      HColumnDescriptor hcd = new HColumnDescriptor(
39        HTableDescriptor.META_TABLEDESC.getColumnFamilies()[0]);
40      final int v = 123;
41      hcd.setBlocksize(v);
42      hcd.setTimeToLive(v);
43      hcd.setBlockCacheEnabled(!HColumnDescriptor.DEFAULT_BLOCKCACHE);
44      hcd.setValue("a", "b");
45      hcd.setMaxVersions(v);
46      assertEquals(v, hcd.getMaxVersions());
47      hcd.setMinVersions(v);
48      assertEquals(v, hcd.getMinVersions());
49      hcd.setKeepDeletedCells(KeepDeletedCells.TRUE);
50      hcd.setInMemory(!HColumnDescriptor.DEFAULT_IN_MEMORY);
51      boolean inmemory = hcd.isInMemory();
52      // Valid values for replication scope are 0 or 1.
53      hcd.setScope(0);
54      hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
55      hcd.setBloomFilterType(BloomType.ROW);
56      hcd.setCompressionType(Algorithm.SNAPPY);
57      hcd.setDFSReplication((short) v);
58  
59      byte [] bytes = hcd.toByteArray();
60      HColumnDescriptor deserializedHcd = HColumnDescriptor.parseFrom(bytes);
61      assertTrue(hcd.equals(deserializedHcd));
62      assertEquals(v, hcd.getBlocksize());
63      assertEquals(v, hcd.getTimeToLive());
64      assertEquals(hcd.getValue("a"), deserializedHcd.getValue("a"));
65      assertEquals(hcd.getMaxVersions(), deserializedHcd.getMaxVersions());
66      assertEquals(hcd.getMinVersions(), deserializedHcd.getMinVersions());
67      assertEquals(hcd.getKeepDeletedCells(), deserializedHcd.getKeepDeletedCells());
68      assertEquals(inmemory, deserializedHcd.isInMemory());
69      assertEquals(hcd.getScope(), deserializedHcd.getScope());
70      assertTrue(deserializedHcd.getCompressionType().equals(Compression.Algorithm.SNAPPY));
71      assertTrue(deserializedHcd.getDataBlockEncoding().equals(DataBlockEncoding.FAST_DIFF));
72      assertTrue(deserializedHcd.getBloomFilterType().equals(BloomType.ROW));
73      assertEquals(v, deserializedHcd.getDFSReplication());
74    }
75  
76    @Test
77    /** Tests HColumnDescriptor with empty familyName*/
78    public void testHColumnDescriptorShouldThrowIAEWhenFamiliyNameEmpty()
79        throws Exception {
80      try {
81        new HColumnDescriptor("".getBytes());
82      } catch (IllegalArgumentException e) {
83        assertEquals("Family name can not be empty", e.getLocalizedMessage());
84      }
85    }
86  
87    /**
88     * Test that we add and remove strings from configuration properly.
89     */
90    @Test
91    public void testAddGetRemoveConfiguration() throws Exception {
92      HColumnDescriptor desc = new HColumnDescriptor("foo");
93      String key = "Some";
94      String value = "value";
95      desc.setConfiguration(key, value);
96      assertEquals(value, desc.getConfigurationValue(key));
97      desc.removeConfiguration(key);
98      assertEquals(null, desc.getConfigurationValue(key));
99    }
100 
101   @Test
102   public void testClassMethodsAreBuilderStyle() {
103     /* HColumnDescriptor should have a builder style setup where setXXX/addXXX methods
104      * can be chainable together:
105      * . For example:
106      * HColumnDescriptor hcd
107      *   = new HColumnDescriptor()
108      *     .setFoo(foo)
109      *     .setBar(bar)
110      *     .setBuz(buz)
111      *
112      * This test ensures that all methods starting with "set" returns the declaring object
113      */
114 
115     BuilderStyleTest.assertClassesAreBuilderStyle(HColumnDescriptor.class);
116   }
117 
118   @Test(expected=IllegalArgumentException.class)
119   public void testInvalidReplicationScope() {
120     HColumnDescriptor column = new HColumnDescriptor("f1");
121     column.setScope(5);
122   }
123 }