1   /*
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.regionserver;
22  
23  import java.io.IOException;
24  
25  import junit.framework.TestCase;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.KeyValueTestUtil;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  public class TestKeyValueScanFixture extends TestCase {
31  
32  
33    public void testKeyValueScanFixture() throws IOException {
34      KeyValue kvs[] = new KeyValue[]{
35          KeyValueTestUtil.create("RowA", "family", "qf1",
36              1, KeyValue.Type.Put, "value-1"),
37          KeyValueTestUtil.create("RowA", "family", "qf2",
38              1, KeyValue.Type.Put, "value-2"),
39          KeyValueTestUtil.create("RowB", "family", "qf1",
40              10, KeyValue.Type.Put, "value-10")
41      };
42      KeyValueScanner scan = new KeyValueScanFixture(
43          KeyValue.COMPARATOR, kvs);
44  
45      // test simple things.
46      assertNull(scan.peek());
47      KeyValue kv = KeyValue.createFirstOnRow(Bytes.toBytes("RowA"));
48      // should seek to this:
49      assertTrue(scan.seek(kv));
50      KeyValue res = scan.peek();
51      assertEquals(kvs[0], res);
52  
53      kv = KeyValue.createFirstOnRow(Bytes.toBytes("RowB"));
54      assertTrue(scan.seek(kv));
55      res = scan.peek();
56      assertEquals(kvs[2], res);
57  
58      // ensure we pull things out properly:
59      kv = KeyValue.createFirstOnRow(Bytes.toBytes("RowA"));
60      assertTrue(scan.seek(kv));
61      assertEquals(kvs[0], scan.peek());
62      assertEquals(kvs[0], scan.next());
63      assertEquals(kvs[1], scan.peek());
64      assertEquals(kvs[1], scan.next());
65      assertEquals(kvs[2], scan.peek());
66      assertEquals(kvs[2], scan.next());
67      assertEquals(null, scan.peek());
68      assertEquals(null, scan.next());
69    }
70  }