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