View Javadoc

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.hadoop.hbase.testclassification.MediumTests;
27  import org.apache.zookeeper.KeeperException;
28  
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(MediumTests.class)
33  public class TestLocalHBaseCluster {
34    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
35  
36    /**
37     * Check that we can start a local HBase cluster specifying a custom master
38     * and regionserver class and then cast back to those classes; also that
39     * the cluster will launch and terminate cleanly. See HBASE-6011. Uses the
40     * HBaseTestingUtility facilities for creating a LocalHBaseCluster with
41     * custom master and regionserver classes.
42     */
43    @Test
44    public void testLocalHBaseCluster() throws Exception {
45      TEST_UTIL.startMiniCluster(1, 1, null, MyHMaster.class, MyHRegionServer.class);
46      // Can we cast back to our master class?
47      try {
48        int val = ((MyHMaster)TEST_UTIL.getHBaseCluster().getMaster(0)).echo(42);
49        assertEquals(42, val);
50      } catch (ClassCastException e) {
51        fail("Could not cast master to our class");
52      }
53      // Can we cast back to our regionserver class?
54      try {
55        int val = ((MyHRegionServer)TEST_UTIL.getHBaseCluster().getRegionServer(0)).echo(42);
56        assertEquals(42, val);
57      } catch (ClassCastException e) {
58        fail("Could not cast regionserver to our class");
59      }
60      TEST_UTIL.shutdownMiniCluster();
61    }
62  
63    /**
64     * A private master class similar to that used by HMasterCommandLine when
65     * running in local mode.
66     */
67    public static class MyHMaster extends HMaster {
68      public MyHMaster(Configuration conf) throws IOException, KeeperException,
69          InterruptedException {
70        super(conf);
71      }
72  
73      public int echo(int val) {
74        return val;
75      }
76    }
77  
78    /**
79     * A private regionserver class with a dummy method for testing casts
80     */
81    public static class MyHRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
82  
83      public MyHRegionServer(Configuration conf) throws IOException,
84          InterruptedException {
85        super(conf);
86      }
87  
88      public int echo(int val) {
89        return val;
90      }
91    }
92  }