1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
47 when(mockMultiplexer.put(any(TableName.class), any(Put.class), anyInt())).thenCallRealMethod();
48
49
50 when(mockMultiplexer.getConnection()).thenReturn(mockConnection);
51 }
52
53 @SuppressWarnings("deprecation")
54 @Test public void testConnectionClosing() throws IOException {
55 doCallRealMethod().when(mockMultiplexer).close();
56
57 when(mockConnection.isClosed()).thenReturn(false);
58
59 mockMultiplexer.close();
60
61
62 verify(mockConnection).close();
63 }
64
65 @SuppressWarnings("deprecation")
66 @Test public void testClosingAlreadyClosedConnection() throws IOException {
67 doCallRealMethod().when(mockMultiplexer).close();
68
69 when(mockConnection.isClosed()).thenReturn(true);
70
71 mockMultiplexer.close();
72
73
74 verify(mockConnection, times(0)).close();
75 }
76 }