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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.CellComparator;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.KeyValueTestUtil;
029import org.apache.hadoop.hbase.KeyValueUtil;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ RegionServerTests.class, SmallTests.class })
038public class TestKeyValueScanFixture {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestKeyValueScanFixture.class);
043
044  @Test
045  public void testKeyValueScanFixture() throws IOException {
046    KeyValue kvs[] = new KeyValue[] {
047      KeyValueTestUtil.create("RowA", "family", "qf1", 1, KeyValue.Type.Put, "value-1"),
048      KeyValueTestUtil.create("RowA", "family", "qf2", 1, KeyValue.Type.Put, "value-2"),
049      KeyValueTestUtil.create("RowB", "family", "qf1", 10, KeyValue.Type.Put, "value-10") };
050    KeyValueScanner scan = new KeyValueScanFixture(CellComparator.getInstance(), kvs);
051
052    KeyValue kv = KeyValueUtil.createFirstOnRow(Bytes.toBytes("RowA"));
053    // should seek to this:
054    assertTrue(scan.seek(kv));
055    Cell res = scan.peek();
056    assertEquals(kvs[0], res);
057
058    kv = KeyValueUtil.createFirstOnRow(Bytes.toBytes("RowB"));
059    assertTrue(scan.seek(kv));
060    res = scan.peek();
061    assertEquals(kvs[2], res);
062
063    // ensure we pull things out properly:
064    kv = KeyValueUtil.createFirstOnRow(Bytes.toBytes("RowA"));
065    assertTrue(scan.seek(kv));
066    assertEquals(kvs[0], scan.peek());
067    assertEquals(kvs[0], scan.next());
068    assertEquals(kvs[1], scan.peek());
069    assertEquals(kvs[1], scan.next());
070    assertEquals(kvs[2], scan.peek());
071    assertEquals(kvs[2], scan.next());
072    assertEquals(null, scan.peek());
073    assertEquals(null, scan.next());
074  }
075
076}