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