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 =
045      HBaseClassTestRule.forClass(TestPBCell.class);
046
047  private static final PBCell CODEC = new PBCell();
048
049  /**
050   * Basic test to verify utility methods in {@link PBType} and delegation to protobuf works.
051   */
052  @Test
053  public void testRoundTrip() {
054    final Cell cell = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("fam"),
055        Bytes.toBytes("qual"), Bytes.toBytes("val"));
056    CellProtos.Cell c = ProtobufUtil.toCell(cell), decoded;
057    PositionedByteRange pbr = new SimplePositionedByteRange(c.getSerializedSize());
058    pbr.setPosition(0);
059    int encodedLength = CODEC.encode(pbr, c);
060    pbr.setPosition(0);
061    decoded = CODEC.decode(pbr);
062    assertEquals(encodedLength, pbr.getPosition());
063    assertTrue(CellUtil.equals(cell, ProtobufUtil
064        .toCell(ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY), decoded)));
065  }
066}