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