001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
024import org.apache.hadoop.hbase.exceptions.DeserializationException;
025import org.apache.hadoop.hbase.io.compress.Compression;
026import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
027import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
028import org.apache.hadoop.hbase.regionserver.BloomType;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.BuilderStyleTest;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.PrettyPrinter;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.ExpectedException;
039
040/**
041 * Tests the HColumnDescriptor with appropriate arguments.
042 *
043 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 together with
044 *            {@link HColumnDescriptor}.
045 */
046@Category({MiscTests.class, SmallTests.class})
047@Deprecated
048public class TestHColumnDescriptor {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestHColumnDescriptor.class);
053
054  @Rule
055  public ExpectedException expectedEx = ExpectedException.none();
056  @Test
057  public void testPb() throws DeserializationException {
058    HColumnDescriptor hcd = new HColumnDescriptor(
059      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(HConstants.CATALOG_FAMILY)
060        .setInMemory(true)
061        .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
062        .setBloomFilterType(BloomType.NONE));
063    final int v = 123;
064    hcd.setBlocksize(v);
065    hcd.setTimeToLive(v);
066    hcd.setBlockCacheEnabled(!HColumnDescriptor.DEFAULT_BLOCKCACHE);
067    hcd.setValue("a", "b");
068    hcd.setMaxVersions(v);
069    assertEquals(v, hcd.getMaxVersions());
070    hcd.setMinVersions(v);
071    assertEquals(v, hcd.getMinVersions());
072    hcd.setKeepDeletedCells(KeepDeletedCells.TRUE);
073    hcd.setInMemory(!HColumnDescriptor.DEFAULT_IN_MEMORY);
074    boolean inmemory = hcd.isInMemory();
075    hcd.setScope(v);
076    hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);
077    hcd.setBloomFilterType(BloomType.ROW);
078    hcd.setCompressionType(Algorithm.SNAPPY);
079    hcd.setMobEnabled(true);
080    hcd.setMobThreshold(1000L);
081    hcd.setDFSReplication((short) v);
082
083    byte [] bytes = hcd.toByteArray();
084    HColumnDescriptor deserializedHcd = HColumnDescriptor.parseFrom(bytes);
085    assertTrue(hcd.equals(deserializedHcd));
086    assertEquals(v, hcd.getBlocksize());
087    assertEquals(v, hcd.getTimeToLive());
088    assertEquals(v, hcd.getScope());
089    assertEquals(hcd.getValue("a"), deserializedHcd.getValue("a"));
090    assertEquals(hcd.getMaxVersions(), deserializedHcd.getMaxVersions());
091    assertEquals(hcd.getMinVersions(), deserializedHcd.getMinVersions());
092    assertEquals(hcd.getKeepDeletedCells(), deserializedHcd.getKeepDeletedCells());
093    assertEquals(inmemory, deserializedHcd.isInMemory());
094    assertEquals(hcd.getScope(), deserializedHcd.getScope());
095    assertTrue(deserializedHcd.getCompressionType().equals(Compression.Algorithm.SNAPPY));
096    assertTrue(deserializedHcd.getDataBlockEncoding().equals(DataBlockEncoding.FAST_DIFF));
097    assertTrue(deserializedHcd.getBloomFilterType().equals(BloomType.ROW));
098    assertEquals(hcd.isMobEnabled(), deserializedHcd.isMobEnabled());
099    assertEquals(hcd.getMobThreshold(), deserializedHcd.getMobThreshold());
100    assertEquals(v, deserializedHcd.getDFSReplication());
101  }
102
103  /**
104   * Tests HColumnDescriptor with empty familyName
105   */
106  @Test
107  public void testHColumnDescriptorShouldThrowIAEWhenFamilyNameEmpty() throws Exception {
108    expectedEx.expect(IllegalArgumentException.class);
109    expectedEx.expectMessage("Column Family name can not be empty");
110    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(Bytes.toBytes(""));
111  }
112
113  @Test
114  public void testMobValuesInHColumnDescriptorShouldReadable() {
115    boolean isMob = true;
116    long threshold = 1000;
117    String policy = "weekly";
118    // We unify the format of all values saved in the descriptor.
119    // Each value is stored as bytes of string.
120    String isMobString = PrettyPrinter.format(String.valueOf(isMob),
121            HColumnDescriptor.getUnit(HColumnDescriptor.IS_MOB));
122    String thresholdString = PrettyPrinter.format(String.valueOf(threshold),
123            HColumnDescriptor.getUnit(HColumnDescriptor.MOB_THRESHOLD));
124    String policyString = PrettyPrinter.format(Bytes.toStringBinary(Bytes.toBytes(policy)),
125        HColumnDescriptor.getUnit(HColumnDescriptor.MOB_COMPACT_PARTITION_POLICY));
126    assertEquals(String.valueOf(isMob), isMobString);
127    assertEquals(String.valueOf(threshold), thresholdString);
128    assertEquals(String.valueOf(policy), policyString);
129  }
130
131  @Test
132  public void testClassMethodsAreBuilderStyle() {
133    /* HColumnDescriptor should have a builder style setup where setXXX/addXXX methods
134     * can be chainable together:
135     * . For example:
136     * HColumnDescriptor hcd
137     *   = new HColumnDescriptor()
138     *     .setFoo(foo)
139     *     .setBar(bar)
140     *     .setBuz(buz)
141     *
142     * This test ensures that all methods starting with "set" returns the declaring object
143     */
144
145    BuilderStyleTest.assertClassesAreBuilderStyle(HColumnDescriptor.class);
146  }
147
148}