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.client;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.mockito.Mockito;
023
024/**
025 * {@link Connection} testing utility.
026 */
027public class HConnectionTestingUtility {
028
029  /*
030   * Not part of {@link HBaseTestingUtility} because this class is not in same package as {@link
031   * ConnectionImplementation}. Would have to reveal ugly {@link ConnectionImplementation} innards
032   * to HBaseTestingUtility to give it access.
033   */
034  /**
035   * Get a Mocked {@link Connection} that goes with the passed <code>conf</code> configuration
036   * instance. Minimally the mock will return &lt;code>conf&lt;/conf> when
037   * {@link Connection#getConfiguration()} is invoked. Be sure to shutdown the connection when done
038   * by calling {@link Connection#close()} else it will stick around; this is probably not what you
039   * want.
040   * @param conf configuration
041   * @return ConnectionImplementation object for <code>conf</code>
042   */
043  public static Connection getMockedConnection(final Configuration conf) throws IOException {
044    Connection connection = Mockito.mock(Connection.class);
045    Mockito.when(connection.getConfiguration()).thenReturn(conf);
046
047    // Some test cases need Mock of getTable and getScanner
048    Table t = Mockito.mock(Table.class);
049    Mockito.when(connection.getTable(Mockito.any())).thenReturn(t);
050    ResultScanner rs = Mockito.mock(ResultScanner.class);
051    Mockito.when(t.getScanner((Scan) Mockito.any())).thenReturn(rs);
052
053    return connection;
054  }
055}