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.client;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HRegionInfo;
023import org.apache.hadoop.hbase.HRegionLocation;
024import org.apache.hadoop.hbase.RegionLocations;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
028import org.apache.hadoop.hbase.testclassification.ClientTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.Before;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.runner.RunWith;
036import org.mockito.Mock;
037import org.mockito.Mockito;
038import org.mockito.runners.MockitoJUnitRunner;
039
040@RunWith(MockitoJUnitRunner.class)
041@Category({ ClientTests.class, SmallTests.class })
042public class TestReversedScannerCallable {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestReversedScannerCallable.class);
047
048  @Mock
049  private ClusterConnection connection;
050  @Mock
051  private Scan scan;
052  @Mock
053  private RpcControllerFactory rpcFactory;
054  @Mock
055  private RegionLocations regionLocations;
056
057  private final byte[] ROW = Bytes.toBytes("row1");
058
059  @Before
060  public void setUp() throws Exception {
061    byte[] ROW_BEFORE = ConnectionUtils.createCloseRowBefore(ROW);
062
063    Configuration conf = Mockito.mock(Configuration.class);
064    HRegionLocation regionLocation = Mockito.mock(HRegionLocation.class);
065    ServerName serverName = Mockito.mock(ServerName.class);
066
067    Mockito.when(connection.getConfiguration()).thenReturn(conf);
068    Mockito.when(regionLocations.size()).thenReturn(1);
069    Mockito.when(regionLocations.getRegionLocation(0)).thenReturn(regionLocation);
070    Mockito.when(regionLocation.getHostname()).thenReturn("localhost");
071    Mockito.when(regionLocation.getServerName()).thenReturn(serverName);
072    Mockito.when(scan.includeStartRow()).thenReturn(true);
073    Mockito.when(scan.getStartRow()).thenReturn(ROW);
074  }
075
076  @Test
077  public void testPrepareDoesNotUseCache() throws Exception {
078    TableName tableName = TableName.valueOf("MyTable");
079    Mockito.when(connection.relocateRegion(tableName, ROW, 0)).thenReturn(regionLocations);
080
081    ReversedScannerCallable callable =
082        new ReversedScannerCallable(connection, tableName, scan, null, rpcFactory);
083    callable.prepare(true);
084
085    Mockito.verify(connection).relocateRegion(tableName, ROW, 0);
086  }
087
088  @Test
089  public void testPrepareUsesCache() throws Exception {
090    TableName tableName = TableName.valueOf("MyTable");
091    Mockito.when(connection.locateRegion(tableName, ROW, true, true, 0))
092        .thenReturn(regionLocations);
093
094    ReversedScannerCallable callable =
095        new ReversedScannerCallable(connection, tableName, scan, null, rpcFactory);
096    callable.prepare(false);
097
098    Mockito.verify(connection).locateRegion(tableName, ROW, true, true, 0);
099  }
100}