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 java.io.IOException; 21 22 import org.apache.hadoop.conf.Configuration; 23 import org.apache.hadoop.hbase.TableName; 24 import org.apache.hadoop.hbase.HRegionInfo; 25 import org.apache.hadoop.hbase.HRegionLocation; 26 import org.apache.hadoop.hbase.ServerName; 27 import org.apache.hadoop.hbase.ZooKeeperConnectionException; 28 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos; 29 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos; 30 import org.apache.hadoop.hbase.client.HConnectionManager.HConnectionImplementation; 31 import org.mockito.Mockito; 32 33 /** 34 * {@link HConnection} testing utility. 35 */ 36 public class HConnectionTestingUtility { 37 /* 38 * Not part of {@link HBaseTestingUtility} because this class is not 39 * in same package as {@link HConnection}. Would have to reveal ugly 40 * {@link HConnectionManager} innards to HBaseTestingUtility to give it access. 41 */ 42 /** 43 * Get a Mocked {@link HConnection} that goes with the passed <code>conf</code> 44 * configuration instance. Minimally the mock will return 45 * <code>conf</conf> when {@link HConnection#getConfiguration()} is invoked. 46 * Be sure to shutdown the connection when done by calling 47 * {@link HConnectionManager#deleteConnection(Configuration)} else it 48 * will stick around; this is probably not what you want. 49 * @param conf configuration 50 * @return HConnection object for <code>conf</code> 51 * @throws ZooKeeperConnectionException 52 */ 53 public static HConnection getMockedConnection(final Configuration conf) 54 throws ZooKeeperConnectionException { 55 HConnectionKey connectionKey = new HConnectionKey(conf); 56 synchronized (HConnectionManager.CONNECTION_INSTANCES) { 57 HConnectionImplementation connection = 58 HConnectionManager.CONNECTION_INSTANCES.get(connectionKey); 59 if (connection == null) { 60 connection = Mockito.mock(HConnectionImplementation.class); 61 Mockito.when(connection.getConfiguration()).thenReturn(conf); 62 HConnectionManager.CONNECTION_INSTANCES.put(connectionKey, connection); 63 } 64 return connection; 65 } 66 } 67 68 /** 69 * Calls {@link #getMockedConnection(Configuration)} and then mocks a few 70 * more of the popular {@link HConnection} methods so they do 'normal' 71 * operation (see return doc below for list). Be sure to shutdown the 72 * connection when done by calling 73 * {@link HConnectionManager#deleteConnection(Configuration)} else it 74 * will stick around; this is probably not what you want. 75 * 76 * @param conf Configuration to use 77 * @param admin An AdminProtocol; can be null but is usually 78 * itself a mock. 79 * @param client A ClientProtocol; can be null but is usually 80 * itself a mock. 81 * @param sn ServerName to include in the region location returned by this 82 * <code>connection</code> 83 * @param hri HRegionInfo to include in the location returned when 84 * getRegionLocation is called on the mocked connection 85 * @return Mock up a connection that returns a {@link Configuration} when 86 * {@link HConnection#getConfiguration()} is called, a 'location' when 87 * {@link HConnection#getRegionLocation(org.apache.hadoop.hbase.TableName, byte[], boolean)} is called, 88 * and that returns the passed {@link AdminProtos.AdminService.BlockingInterface} instance when 89 * {@link HConnection#getAdmin(ServerName)} is called, returns the passed 90 * {@link ClientProtos.ClientService.BlockingInterface} instance when 91 * {@link HConnection#getClient(ServerName)} is called (Be sure to call 92 * {@link HConnectionManager#deleteConnection(Configuration)} 93 * when done with this mocked Connection. 94 * @throws IOException 95 */ 96 public static HConnection getMockedConnectionAndDecorate(final Configuration conf, 97 final AdminProtos.AdminService.BlockingInterface admin, 98 final ClientProtos.ClientService.BlockingInterface client, 99 final ServerName sn, final HRegionInfo hri) 100 throws IOException { 101 HConnection c = HConnectionTestingUtility.getMockedConnection(conf); 102 Mockito.doNothing().when(c).close(); 103 // Make it so we return a particular location when asked. 104 final HRegionLocation loc = new HRegionLocation(hri, sn); 105 Mockito.when(c.getRegionLocation((TableName) Mockito.any(), 106 (byte[]) Mockito.any(), Mockito.anyBoolean())). 107 thenReturn(loc); 108 Mockito.when(c.locateRegion((TableName) Mockito.any(), (byte[]) Mockito.any())). 109 thenReturn(loc); 110 if (admin != null) { 111 // If a call to getAdmin, return this implementation. 112 Mockito.when(c.getAdmin(Mockito.any(ServerName.class))). 113 thenReturn(admin); 114 } 115 if (client != null) { 116 // If a call to getClient, return this client. 117 Mockito.when(c.getClient(Mockito.any(ServerName.class))). 118 thenReturn(client); 119 } 120 return c; 121 } 122 123 /** 124 * Get a Mockito spied-upon {@link HConnection} that goes with the passed 125 * <code>conf</code> configuration instance. 126 * Be sure to shutdown the connection when done by calling 127 * {@link HConnectionManager#deleteConnection(Configuration)} else it 128 * will stick around; this is probably not what you want. 129 * @param conf configuration 130 * @return HConnection object for <code>conf</code> 131 * @throws ZooKeeperConnectionException 132 * @see @link 133 * {http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mockito.html#spy(T)} 134 */ 135 public static HConnection getSpiedConnection(final Configuration conf) 136 throws IOException { 137 HConnectionKey connectionKey = new HConnectionKey(conf); 138 synchronized (HConnectionManager.CONNECTION_INSTANCES) { 139 HConnectionImplementation connection = 140 HConnectionManager.CONNECTION_INSTANCES.get(connectionKey); 141 if (connection == null) { 142 connection = Mockito.spy(new HConnectionImplementation(conf, true)); 143 HConnectionManager.CONNECTION_INSTANCES.put(connectionKey, connection); 144 } 145 return connection; 146 } 147 } 148 149 /** 150 * @return Count of extant connection instances 151 */ 152 public static int getConnectionCount() { 153 synchronized (HConnectionManager.CONNECTION_INSTANCES) { 154 return HConnectionManager.CONNECTION_INSTANCES.size(); 155 } 156 } 157 }