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. 061 * @throws ZooKeeperConnectionException 062 * @throws IOException 063 */ 064 public MockServer(final HBaseTestingUtility htu, final boolean zkw) 065 throws ZooKeeperConnectionException, IOException { 066 this.htu = htu; 067 this.zk = zkw? 068 new ZKWatcher(htu.getConfiguration(), NAME.toString(), this, true): 069 null; 070 } 071 072 @Override 073 public void abort(String why, Throwable e) { 074 LOG.error(HBaseMarkers.FATAL, "Abort why=" + why, e); 075 stop(why); 076 this.aborted = true; 077 } 078 079 @Override 080 public void stop(String why) { 081 LOG.debug("Stop why=" + why); 082 this.stopped = true; 083 } 084 085 @Override 086 public boolean isStopped() { 087 return this.stopped; 088 } 089 090 @Override 091 public Configuration getConfiguration() { 092 return this.htu.getConfiguration(); 093 } 094 095 @Override 096 public ZKWatcher getZooKeeper() { 097 return this.zk; 098 } 099 100 @Override 101 public CoordinatedStateManager getCoordinatedStateManager() { 102 return null; 103 } 104 105 @Override 106 public ClusterConnection getConnection() { 107 return null; 108 } 109 110 @Override 111 public ServerName getServerName() { 112 return NAME; 113 } 114 115 @Override 116 public boolean isAborted() { 117 // TODO Auto-generated method stub 118 return this.aborted; 119 } 120 121 @Override 122 public ChoreService getChoreService() { 123 return null; 124 } 125 126 @Override 127 public ClusterConnection getClusterConnection() { 128 // TODO Auto-generated method stub 129 return null; 130 } 131 132 @Override 133 public FileSystem getFileSystem() { 134 return null; 135 } 136 137 @Override 138 public boolean isStopping() { 139 return false; 140 } 141 142 @Override 143 public Connection createConnection(Configuration conf) throws IOException { 144 return null; 145 } 146}