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 java.io.IOException;
021import java.util.List;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HRegionLocation;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.testclassification.ClientTests;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.util.Pair;
028import org.junit.AfterClass;
029import org.junit.BeforeClass;
030import org.junit.ClassRule;
031import org.junit.experimental.categories.Category;
032
033@Category({ MediumTests.class, ClientTests.class })
034public class TestRegionLocator extends AbstractTestRegionLocator {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestRegionLocator.class);
039
040  @BeforeClass
041  public static void setUp() throws Exception {
042    startClusterAndCreateTable();
043  }
044
045  @AfterClass
046  public static void tearDown() throws Exception {
047    UTIL.shutdownMiniCluster();
048  }
049
050  @Override
051  protected byte[][] getStartKeys(TableName tableName) throws IOException {
052    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
053      return locator.getStartKeys();
054    }
055  }
056
057  @Override
058  protected byte[][] getEndKeys(TableName tableName) throws IOException {
059    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
060      return locator.getEndKeys();
061    }
062  }
063
064  @Override
065  protected Pair<byte[][], byte[][]> getStartEndKeys(TableName tableName) throws IOException {
066    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
067      return locator.getStartEndKeys();
068    }
069  }
070
071  @Override
072  protected HRegionLocation getRegionLocation(TableName tableName, byte[] row, int replicaId)
073    throws IOException {
074    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
075      return locator.getRegionLocation(row, replicaId);
076    }
077  }
078
079  @Override
080  protected List<HRegionLocation> getRegionLocations(TableName tableName, byte[] row)
081    throws IOException {
082    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
083      return locator.getRegionLocations(row);
084    }
085  }
086
087  @Override
088  protected List<HRegionLocation> getAllRegionLocations(TableName tableName) throws IOException {
089    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
090      return locator.getAllRegionLocations();
091    }
092  }
093
094  @Override
095  protected void clearCache(TableName tableName) throws IOException {
096    try (RegionLocator locator = UTIL.getConnection().getRegionLocator(tableName)) {
097      locator.clearRegionLocationCache();
098    }
099  }
100}