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