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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.fail;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.master.HMaster;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.testclassification.MiscTests;
028import org.apache.zookeeper.KeeperException;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032@Tag(MiscTests.TAG)
033@Tag(MediumTests.TAG)
034public class TestLocalHBaseCluster {
035
036  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
037
038  /**
039   * Check that we can start a local HBase cluster specifying a custom master and regionserver class
040   * and then cast back to those classes; also that the cluster will launch and terminate cleanly.
041   * See HBASE-6011. Uses the HBaseTestingUtility facilities for creating a LocalHBaseCluster with
042   * custom master and regionserver classes.
043   */
044  @Test
045  public void testLocalHBaseCluster() throws Exception {
046    // Set Master class and RegionServer class, and use default values for other options.
047    StartTestingClusterOption option = StartTestingClusterOption.builder()
048      .masterClass(MyHMaster.class).rsClass(MyHRegionServer.class).build();
049    TEST_UTIL.startMiniCluster(option);
050    // Can we cast back to our master class?
051    try {
052      int val = ((MyHMaster) TEST_UTIL.getHBaseCluster().getMaster(0)).echo(42);
053      assertEquals(42, val);
054    } catch (ClassCastException e) {
055      fail("Could not cast master to our class");
056    }
057    // Can we cast back to our regionserver class?
058    try {
059      int val = ((MyHRegionServer) TEST_UTIL.getHBaseCluster().getRegionServer(0)).echo(42);
060      assertEquals(42, val);
061    } catch (ClassCastException e) {
062      fail("Could not cast regionserver to our class");
063    }
064    TEST_UTIL.shutdownMiniCluster();
065  }
066
067  /**
068   * A private master class similar to that used by HMasterCommandLine when running in local mode.
069   */
070  public static class MyHMaster extends HMaster {
071    public MyHMaster(Configuration conf) throws IOException, KeeperException, InterruptedException {
072      super(conf);
073    }
074
075    public int echo(int val) {
076      return val;
077    }
078  }
079
080  /**
081   * A private regionserver class with a dummy method for testing casts
082   */
083  public static class MyHRegionServer
084    extends SingleProcessHBaseCluster.MiniHBaseClusterRegionServer {
085
086    public MyHRegionServer(Configuration conf) throws IOException, InterruptedException {
087      super(conf);
088    }
089
090    public int echo(int val) {
091      return val;
092    }
093  }
094}