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
043   * and regionserver class and then cast back to those classes; also that
044   * the cluster will launch and terminate cleanly. See HBASE-6011. Uses the
045   * HBaseTestingUtility facilities for creating a LocalHBaseCluster with
046   * custom master and regionserver classes.
047   */
048  @Test
049  public void testLocalHBaseCluster() throws Exception {
050    // Set Master class and RegionServer class, and use default values for other options.
051    StartMiniClusterOption option = StartMiniClusterOption.builder()
052        .masterClass(MyHMaster.class).rsClass(MyHRegionServer.class).build();
053    TEST_UTIL.startMiniCluster(option);
054    // Can we cast back to our master class?
055    try {
056      int val = ((MyHMaster)TEST_UTIL.getHBaseCluster().getMaster(0)).echo(42);
057      assertEquals(42, val);
058    } catch (ClassCastException e) {
059      fail("Could not cast master to our class");
060    }
061    // Can we cast back to our regionserver class?
062    try {
063      int val = ((MyHRegionServer)TEST_UTIL.getHBaseCluster().getRegionServer(0)).echo(42);
064      assertEquals(42, val);
065    } catch (ClassCastException e) {
066      fail("Could not cast regionserver to our class");
067    }
068    TEST_UTIL.shutdownMiniCluster();
069  }
070
071  /**
072   * A private master class similar to that used by HMasterCommandLine when
073   * running in local mode.
074   */
075  public static class MyHMaster extends HMaster {
076    public MyHMaster(Configuration conf) throws IOException, KeeperException,
077        InterruptedException {
078      super(conf);
079    }
080
081    public int echo(int val) {
082      return val;
083    }
084  }
085
086  /**
087   * A private regionserver class with a dummy method for testing casts
088   */
089  public static class MyHRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
090
091    public MyHRegionServer(Configuration conf) throws IOException, InterruptedException {
092      super(conf);
093    }
094
095    public int echo(int val) {
096      return val;
097    }
098  }
099}