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