1   /**
2    * Copyright 2010 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  package org.apache.hadoop.hbase.client;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.AfterClass;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  
32  import static org.mockito.Mockito.*;
33  
34  public class TestMetaScanner {
35    final Log LOG = LogFactory.getLog(getClass());
36    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
37  
38    @BeforeClass
39    public static void setUpBeforeClass() throws Exception {
40      TEST_UTIL.startMiniCluster(1);
41    }
42  
43    /**
44     * @throws java.lang.Exception
45     */
46    @AfterClass
47    public static void tearDownAfterClass() throws Exception {
48      TEST_UTIL.shutdownMiniCluster();
49    }
50  
51    @Test
52    public void testMetaScanner() throws Exception {
53      LOG.info("Starting testMetaScanner");
54      final byte[] TABLENAME = Bytes.toBytes("testMetaScanner");
55      final byte[] FAMILY = Bytes.toBytes("family");
56      TEST_UTIL.createTable(TABLENAME, FAMILY);
57      Configuration conf = TEST_UTIL.getConfiguration();
58      HTable table = new HTable(conf, TABLENAME);
59      TEST_UTIL.createMultiRegions(conf, table, FAMILY,
60          new byte[][]{
61            HConstants.EMPTY_START_ROW,
62            Bytes.toBytes("region_a"),
63            Bytes.toBytes("region_b")});
64      // Make sure all the regions are deployed
65      TEST_UTIL.countRows(table);
66      
67      MetaScanner.MetaScannerVisitor visitor = 
68        mock(MetaScanner.MetaScannerVisitor.class);
69      doReturn(true).when(visitor).processRow((Result)anyObject());
70  
71      // Scanning the entire table should give us three rows
72      MetaScanner.metaScan(conf, visitor, TABLENAME);
73      verify(visitor, times(3)).processRow((Result)anyObject());
74      
75      // Scanning the table with a specified empty start row should also
76      // give us three META rows
77      reset(visitor);
78      doReturn(true).when(visitor).processRow((Result)anyObject());
79      MetaScanner.metaScan(conf, visitor, TABLENAME, HConstants.EMPTY_BYTE_ARRAY, 1000);
80      verify(visitor, times(3)).processRow((Result)anyObject());
81      
82      // Scanning the table starting in the middle should give us two rows:
83      // region_a and region_b
84      reset(visitor);
85      doReturn(true).when(visitor).processRow((Result)anyObject());
86      MetaScanner.metaScan(conf, visitor, TABLENAME, Bytes.toBytes("region_ac"), 1000);
87      verify(visitor, times(2)).processRow((Result)anyObject());
88      
89      // Scanning with a limit of 1 should only give us one row
90      reset(visitor);
91      doReturn(true).when(visitor).processRow((Result)anyObject());
92      MetaScanner.metaScan(conf, visitor, TABLENAME, Bytes.toBytes("region_ac"), 1);
93      verify(visitor, times(1)).processRow((Result)anyObject());
94          
95    }
96  }