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.util;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.hbase.ChoreService;
024import org.apache.hadoop.hbase.CoordinatedStateManager;
025import org.apache.hadoop.hbase.HBaseTestingUtility;
026import org.apache.hadoop.hbase.Server;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.ZooKeeperConnectionException;
029import org.apache.hadoop.hbase.client.ClusterConnection;
030import org.apache.hadoop.hbase.client.Connection;
031import org.apache.hadoop.hbase.log.HBaseMarkers;
032import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * Basic mock Server for handler tests.
038 */
039public class MockServer implements Server {
040  private static final Logger LOG = LoggerFactory.getLogger(MockServer.class);
041  final static ServerName NAME = ServerName.valueOf("MockServer", 123, -1);
042
043  boolean stopped;
044  boolean aborted;
045  final ZKWatcher zk;
046  final HBaseTestingUtility htu;
047
048  public MockServer() throws ZooKeeperConnectionException, IOException {
049    // Shutdown default constructor by making it private.
050    this(null);
051  }
052
053  public MockServer(final HBaseTestingUtility htu)
054    throws ZooKeeperConnectionException, IOException {
055    this(htu, true);
056  }
057
058  /**
059   * @param htu Testing utility to use
060   * @param zkw If true, create a zkw. nn
061   */
062  public MockServer(final HBaseTestingUtility htu, final boolean zkw)
063    throws ZooKeeperConnectionException, IOException {
064    this.htu = htu;
065    this.zk = zkw ? new ZKWatcher(htu.getConfiguration(), NAME.toString(), this, true) : null;
066  }
067
068  @Override
069  public void abort(String why, Throwable e) {
070    LOG.error(HBaseMarkers.FATAL, "Abort why=" + why, e);
071    stop(why);
072    this.aborted = true;
073  }
074
075  @Override
076  public void stop(String why) {
077    LOG.debug("Stop why=" + why);
078    this.stopped = true;
079  }
080
081  @Override
082  public boolean isStopped() {
083    return this.stopped;
084  }
085
086  @Override
087  public Configuration getConfiguration() {
088    return this.htu.getConfiguration();
089  }
090
091  @Override
092  public ZKWatcher getZooKeeper() {
093    return this.zk;
094  }
095
096  @Override
097  public CoordinatedStateManager getCoordinatedStateManager() {
098    return null;
099  }
100
101  @Override
102  public ClusterConnection getConnection() {
103    return null;
104  }
105
106  @Override
107  public ServerName getServerName() {
108    return NAME;
109  }
110
111  @Override
112  public boolean isAborted() {
113    // TODO Auto-generated method stub
114    return this.aborted;
115  }
116
117  @Override
118  public ChoreService getChoreService() {
119    return null;
120  }
121
122  @Override
123  public ClusterConnection getClusterConnection() {
124    // TODO Auto-generated method stub
125    return null;
126  }
127
128  @Override
129  public FileSystem getFileSystem() {
130    return null;
131  }
132
133  @Override
134  public boolean isStopping() {
135    return false;
136  }
137
138  @Override
139  public Connection createConnection(Configuration conf) throws IOException {
140    return null;
141  }
142}