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    TEST_UTIL.startMiniCluster(1, 1, null, MyHMaster.class, MyHRegionServer.class);
051    // Can we cast back to our master class?
052    try {
053      int val = ((MyHMaster)TEST_UTIL.getHBaseCluster().getMaster(0)).echo(42);
054      assertEquals(42, val);
055    } catch (ClassCastException e) {
056      fail("Could not cast master to our class");
057    }
058    // Can we cast back to our regionserver class?
059    try {
060      int val = ((MyHRegionServer)TEST_UTIL.getHBaseCluster().getRegionServer(0)).echo(42);
061      assertEquals(42, val);
062    } catch (ClassCastException e) {
063      fail("Could not cast regionserver to our class");
064    }
065    TEST_UTIL.shutdownMiniCluster();
066  }
067
068  /**
069   * A private master class similar to that used by HMasterCommandLine when
070   * running in local mode.
071   */
072  public static class MyHMaster extends HMaster {
073    public MyHMaster(Configuration conf) throws IOException, KeeperException,
074        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}