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.client;
19
20 import org.apache.hadoop.conf.Configuration;
21 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
22 import org.apache.hadoop.hbase.client.HConnectionManager.HConnectionImplementation;
23 import org.apache.hadoop.hbase.client.HConnectionManager.HConnectionKey;
24 import org.mockito.Mockito;
25
26 /**
27 * {@link HConnection} testing utility.
28 */
29 public class HConnectionTestingUtility {
30 /*
31 * Not part of {@link HBaseTestingUtility} because this class is not
32 * in same package as {@link HConnection}. Would have to reveal ugly
33 * {@link HConnectionManager} innards to HBaseTestingUtility to give it access.
34 */
35 /**
36 * Get a Mocked {@link HConnection} that goes with the passed <code>conf</code>
37 * configuration instance. Minimally the mock will return
38 * <code>conf</conf> when {@link HConnection#getConfiguration()} is invoked.
39 * Be sure to shutdown the connection when done by calling
40 * {@link HConnectionManager#deleteConnection(Configuration, boolean)} else it
41 * will stick around; this is probably not what you want.
42 * @param conf configuration
43 * @return HConnection object for <code>conf</code>
44 * @throws ZooKeeperConnectionException
45 */
46 public static HConnection getMockedConnection(final Configuration conf)
47 throws ZooKeeperConnectionException {
48 HConnectionKey connectionKey = new HConnectionKey(conf);
49 synchronized (HConnectionManager.HBASE_INSTANCES) {
50 HConnectionImplementation connection =
51 HConnectionManager.HBASE_INSTANCES.get(connectionKey);
52 if (connection == null) {
53 connection = Mockito.mock(HConnectionImplementation.class);
54 Mockito.when(connection.getConfiguration()).thenReturn(conf);
55 HConnectionManager.HBASE_INSTANCES.put(connectionKey, connection);
56 }
57 return connection;
58 }
59 }
60
61 /**
62 * Get a Mockito spied-upon {@link HConnection} that goes with the passed
63 * <code>conf</code> configuration instance.
64 * Be sure to shutdown the connection when done by calling
65 * {@link HConnectionManager#deleteConnection(Configuration, boolean)} else it
66 * will stick around; this is probably not what you want.
67 * @param conf configuration
68 * @return HConnection object for <code>conf</code>
69 * @throws ZooKeeperConnectionException
70 * @see http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mockito.html#spy(T)
71 */
72 public static HConnection getSpiedConnection(final Configuration conf)
73 throws ZooKeeperConnectionException {
74 HConnectionKey connectionKey = new HConnectionKey(conf);
75 synchronized (HConnectionManager.HBASE_INSTANCES) {
76 HConnectionImplementation connection =
77 HConnectionManager.HBASE_INSTANCES.get(connectionKey);
78 if (connection == null) {
79 connection = Mockito.spy(new HConnectionImplementation(conf, true));
80 HConnectionManager.HBASE_INSTANCES.put(connectionKey, connection);
81 }
82 return connection;
83 }
84 }
85 }