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 byte[] ROW_BEFORE = ConnectionUtils.createCloseRowBefore(ROW); 061 062 Configuration conf = Mockito.mock(Configuration.class); 063 HRegionLocation regionLocation = Mockito.mock(HRegionLocation.class); 064 ServerName serverName = Mockito.mock(ServerName.class); 065 066 Mockito.when(connection.getConfiguration()).thenReturn(conf); 067 Mockito.when(regionLocations.size()).thenReturn(1); 068 Mockito.when(regionLocations.getRegionLocation(0)).thenReturn(regionLocation); 069 Mockito.when(regionLocation.getHostname()).thenReturn("localhost"); 070 Mockito.when(regionLocation.getServerName()).thenReturn(serverName); 071 Mockito.when(scan.includeStartRow()).thenReturn(true); 072 Mockito.when(scan.getStartRow()).thenReturn(ROW); 073 } 074 075 @Test 076 public void testPrepareDoesNotUseCache() throws Exception { 077 TableName tableName = TableName.valueOf("MyTable"); 078 Mockito.when(connection.relocateRegion(tableName, ROW, 0)).thenReturn(regionLocations); 079 080 ReversedScannerCallable callable = 081 new ReversedScannerCallable(connection, tableName, scan, null, rpcFactory); 082 callable.prepare(true); 083 084 Mockito.verify(connection).relocateRegion(tableName, ROW, 0); 085 } 086 087 @Test 088 public void testPrepareUsesCache() throws Exception { 089 TableName tableName = TableName.valueOf("MyTable"); 090 Mockito.when(connection.locateRegion(tableName, ROW, true, true, 0)) 091 .thenReturn(regionLocations); 092 093 ReversedScannerCallable callable = 094 new ReversedScannerCallable(connection, tableName, scan, null, rpcFactory); 095 callable.prepare(false); 096 097 Mockito.verify(connection).locateRegion(tableName, ROW, true, true, 0); 098 } 099}