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