View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.protobuf;
19  
20  import static org.junit.Assert.*;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.CellScanner;
28  import org.apache.hadoop.hbase.KeyValue;
29  import org.apache.hadoop.hbase.testclassification.SmallTests;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  
35  @Category(SmallTests.class)
36  public class TestReplicationProtobuf {
37    /**
38     * Little test to check we can basically convert list of a list of KVs into a CellScanner
39     * @throws IOException
40     */
41    @Test
42    public void testGetCellScanner() throws IOException {
43      List<Cell> a = new ArrayList<Cell>();
44      KeyValue akv = new KeyValue(Bytes.toBytes("a"), -1L);
45      a.add(akv);
46      // Add a few just to make it less regular.
47      a.add(new KeyValue(Bytes.toBytes("aa"), -1L));
48      a.add(new KeyValue(Bytes.toBytes("aaa"), -1L));
49      List<Cell> b = new ArrayList<Cell>();
50      KeyValue bkv = new KeyValue(Bytes.toBytes("b"), -1L);
51      a.add(bkv);
52      List<Cell> c = new ArrayList<Cell>();
53      KeyValue ckv = new KeyValue(Bytes.toBytes("c"), -1L);
54      c.add(ckv);
55      List<List<? extends Cell>> all = new ArrayList<List<? extends Cell>>();
56      all.add(a);
57      all.add(b);
58      all.add(c);
59      CellScanner scanner = ReplicationProtbufUtil.getCellScanner(all, 0);
60      testAdvancetHasSameRow(scanner, akv);
61      // Skip over aa
62      scanner.advance();
63      // Skip over aaa
64      scanner.advance();
65      testAdvancetHasSameRow(scanner, bkv);
66      testAdvancetHasSameRow(scanner, ckv);
67      assertFalse(scanner.advance());
68    }
69  
70    private void testAdvancetHasSameRow(CellScanner scanner, final KeyValue kv) throws IOException {
71      scanner.advance();
72      assertTrue(Bytes.equals(scanner.current().getRowArray(), scanner.current().getRowOffset(),
73          scanner.current().getRowLength(),
74        kv.getRowArray(), kv.getRowOffset(), kv.getRowLength()));
75    }
76  }