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.util;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.Server;
27  import org.apache.hadoop.hbase.ServerName;
28  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
29  import org.apache.hadoop.hbase.catalog.CatalogTracker;
30  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
31  
32  /**
33   * Basic mock Server for handler tests.
34   */
35  public class MockServer implements Server {
36    static final Log LOG = LogFactory.getLog(MockServer.class);
37    final static ServerName NAME = ServerName.valueOf("MockServer", 123, -1);
38    
39    boolean stopped;
40    boolean aborted;
41    final ZooKeeperWatcher zk;
42    final HBaseTestingUtility htu;
43  
44    @SuppressWarnings("unused")
45    public MockServer() throws ZooKeeperConnectionException, IOException {
46      // Shutdown default constructor by making it private.
47      this(null);
48    }
49  
50    public MockServer(final HBaseTestingUtility htu)
51    throws ZooKeeperConnectionException, IOException {
52      this(htu, true);
53    }
54  
55    /**
56     * @param htu Testing utility to use
57     * @param zkw If true, create a zkw.
58     * @throws ZooKeeperConnectionException
59     * @throws IOException
60     */
61    public MockServer(final HBaseTestingUtility htu, final boolean zkw)
62    throws ZooKeeperConnectionException, IOException {
63      this.htu = htu;
64      this.zk = zkw?
65        new ZooKeeperWatcher(htu.getConfiguration(), NAME.toString(), this, true):
66        null;
67    }
68  
69    @Override
70    public void abort(String why, Throwable e) {
71      LOG.fatal("Abort why=" + why, e);
72      stop(why);
73      this.aborted = true;
74    }
75  
76    @Override
77    public void stop(String why) {
78      LOG.debug("Stop why=" + why);
79      this.stopped = true;
80    }
81  
82    @Override
83    public boolean isStopped() {
84      return this.stopped;
85    }
86  
87    @Override
88    public Configuration getConfiguration() {
89      return this.htu.getConfiguration();
90    }
91  
92    @Override
93    public ZooKeeperWatcher getZooKeeper() {
94      return this.zk;
95    }
96  
97    @Override
98    public CatalogTracker getCatalogTracker() {
99      return null;
100   }
101 
102   @Override
103   public ServerName getServerName() {
104     return NAME;
105   }
106 
107   @Override
108   public boolean isAborted() {
109     // TODO Auto-generated method stub
110     return this.aborted;
111   }
112 }