1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.hadoop.hbase;
19
20 import static org.junit.Assert.*;
21
22 import java.io.IOException;
23
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.master.HMaster;
26 import org.apache.zookeeper.KeeperException;
27
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 @Category(MediumTests.class)
32 public class TestLocalHBaseCluster {
33 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
34
35 /**
36 * Check that we can start a local HBase cluster specifying a custom master
37 * and regionserver class and then cast back to those classes; also that
38 * the cluster will launch and terminate cleanly. See HBASE-6011. Uses the
39 * HBaseTestingUtility facilities for creating a LocalHBaseCluster with
40 * custom master and regionserver classes.
41 */
42 @Test
43 public void testLocalHBaseCluster() throws Exception {
44 TEST_UTIL.startMiniCluster(1, 1, null, MyHMaster.class, MyHRegionServer.class);
45 // Can we cast back to our master class?
46 try {
47 int val = ((MyHMaster)TEST_UTIL.getHBaseCluster().getMaster(0)).echo(42);
48 assertEquals(42, val);
49 } catch (ClassCastException e) {
50 fail("Could not cast master to our class");
51 }
52 // Can we cast back to our regionserver class?
53 try {
54 int val = ((MyHRegionServer)TEST_UTIL.getHBaseCluster().getRegionServer(0)).echo(42);
55 assertEquals(42, val);
56 } catch (ClassCastException e) {
57 fail("Could not cast regionserver to our class");
58 }
59 TEST_UTIL.shutdownMiniCluster();
60 }
61
62 /**
63 * A private master class similar to that used by HMasterCommandLine when
64 * running in local mode.
65 */
66 public static class MyHMaster extends HMaster {
67 public MyHMaster(Configuration conf) throws IOException, KeeperException,
68 InterruptedException {
69 super(conf);
70 }
71
72 public int echo(int val) {
73 return val;
74 }
75 }
76
77 /**
78 * A private regionserver class with a dummy method for testing casts
79 */
80 public static class MyHRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
81
82 public MyHRegionServer(Configuration conf) throws IOException,
83 InterruptedException {
84 super(conf);
85 }
86
87 public int echo(int val) {
88 return val;
89 }
90 }
91 }