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