View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.client;
18  
19  import org.apache.hadoop.hbase.TableName;
20  import org.apache.hadoop.hbase.testclassification.SmallTests;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.junit.experimental.categories.Category;
24  
25  import java.io.IOException;
26  
27  import static org.mockito.Matchers.any;
28  import static org.mockito.Matchers.anyInt;
29  import static org.mockito.Mockito.doCallRealMethod;
30  import static org.mockito.Mockito.mock;
31  import static org.mockito.Mockito.times;
32  import static org.mockito.Mockito.verify;
33  import static org.mockito.Mockito.when;
34  
35  @Category(SmallTests.class)
36  public class TestHTableMultiplexerViaMocks {
37  
38    private HTableMultiplexer mockMultiplexer;
39    private ClusterConnection mockConnection;
40  
41    @Before
42    public void setupTest() {
43      mockMultiplexer = mock(HTableMultiplexer.class);
44      mockConnection = mock(ClusterConnection.class);
45  
46      // Call the real put(TableName, Put, int) method
47      when(mockMultiplexer.put(any(TableName.class), any(Put.class), anyInt())).thenCallRealMethod();
48  
49      // Return the mocked ClusterConnection
50      when(mockMultiplexer.getConnection()).thenReturn(mockConnection);
51    }
52  
53    @SuppressWarnings("deprecation")
54    @Test public void testConnectionClosing() throws IOException {
55      doCallRealMethod().when(mockMultiplexer).close();
56      // If the connection is not closed
57      when(mockConnection.isClosed()).thenReturn(false);
58  
59      mockMultiplexer.close();
60  
61      // We should close it
62      verify(mockConnection).close();
63    }
64  
65    @SuppressWarnings("deprecation")
66    @Test public void testClosingAlreadyClosedConnection() throws IOException {
67      doCallRealMethod().when(mockMultiplexer).close();
68      // If the connection is already closed
69      when(mockConnection.isClosed()).thenReturn(true);
70  
71      mockMultiplexer.close();
72  
73      // We should not close it again
74      verify(mockConnection, times(0)).close();
75    }
76  }