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