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.ArgumentMatchers.any;
021import static org.mockito.ArgumentMatchers.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
061  public void testConnectionClosing() throws IOException {
062    doCallRealMethod().when(mockMultiplexer).close();
063    // If the connection is not closed
064    when(mockConnection.isClosed()).thenReturn(false);
065
066    mockMultiplexer.close();
067
068    // We should close it
069    verify(mockConnection).close();
070  }
071
072  @SuppressWarnings("deprecation")
073  @Test
074  public void testClosingAlreadyClosedConnection() throws IOException {
075    doCallRealMethod().when(mockMultiplexer).close();
076    // If the connection is already closed
077    when(mockConnection.isClosed()).thenReturn(true);
078
079    mockMultiplexer.close();
080
081    // We should not close it again
082    verify(mockConnection, times(0)).close();
083  }
084}