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 static org.mockito.Matchers.any;
021import static org.mockito.Matchers.anyInt;
022import static org.mockito.Mockito.doCallRealMethod;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.times;
025import static org.mockito.Mockito.verify;
026import static org.mockito.Mockito.when;
027
028import java.io.IOException;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.Before;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category(SmallTests.class)
038public class TestHTableMultiplexerViaMocks {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestHTableMultiplexerViaMocks.class);
043
044  private HTableMultiplexer mockMultiplexer;
045  private ClusterConnection mockConnection;
046
047  @Before
048  public void setupTest() {
049    mockMultiplexer = mock(HTableMultiplexer.class);
050    mockConnection = mock(ClusterConnection.class);
051
052    // Call the real put(TableName, Put, int) method
053    when(mockMultiplexer.put(any(TableName.class), any(), anyInt())).thenCallRealMethod();
054
055    // Return the mocked ClusterConnection
056    when(mockMultiplexer.getConnection()).thenReturn(mockConnection);
057  }
058
059  @SuppressWarnings("deprecation")
060  @Test public void testConnectionClosing() throws IOException {
061    doCallRealMethod().when(mockMultiplexer).close();
062    // If the connection is not closed
063    when(mockConnection.isClosed()).thenReturn(false);
064
065    mockMultiplexer.close();
066
067    // We should close it
068    verify(mockConnection).close();
069  }
070
071  @SuppressWarnings("deprecation")
072  @Test public void testClosingAlreadyClosedConnection() throws IOException {
073    doCallRealMethod().when(mockMultiplexer).close();
074    // If the connection is already closed
075    when(mockConnection.isClosed()).thenReturn(true);
076
077    mockMultiplexer.close();
078
079    // We should not close it again
080    verify(mockConnection, times(0)).close();
081  }
082}