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.types;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellBuilderType;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.ExtendedCellBuilderFactory;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.KeyValue;
029import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
030import org.apache.hadoop.hbase.protobuf.generated.CellProtos;
031import org.apache.hadoop.hbase.testclassification.MiscTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.hbase.util.PositionedByteRange;
035import org.apache.hadoop.hbase.util.SimplePositionedByteRange;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040@Category({ SmallTests.class, MiscTests.class })
041public class TestPBCell {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestPBCell.class);
045
046  private static final PBCell CODEC = new PBCell();
047
048  /**
049   * Basic test to verify utility methods in {@link PBType} and delegation to protobuf works.
050   */
051  @Test
052  public void testRoundTrip() {
053    final Cell cell = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("fam"),
054      Bytes.toBytes("qual"), Bytes.toBytes("val"));
055    CellProtos.Cell c = ProtobufUtil.toCell(cell), decoded;
056    PositionedByteRange pbr = new SimplePositionedByteRange(c.getSerializedSize());
057    pbr.setPosition(0);
058    int encodedLength = CODEC.encode(pbr, c);
059    pbr.setPosition(0);
060    decoded = CODEC.decode(pbr);
061    assertEquals(encodedLength, pbr.getPosition());
062    assertTrue(CellUtil.equals(cell, ProtobufUtil
063      .toCell(ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY), decoded)));
064  }
065}