1
2
3
4
5
6
7
8
9
10
11 package psiprobe.beans.accessors;
12
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertFalse;
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.assertNull;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.when;
21
22 import com.mchange.v2.c3p0.ComboPooledDataSource;
23 import com.vladmihalcea.flexypool.FlexyPoolDataSource;
24 import com.zaxxer.hikari.HikariDataSource;
25 import com.zaxxer.hikari.HikariPoolMXBean;
26
27 import java.sql.SQLException;
28
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32 import psiprobe.model.DataSourceInfo;
33
34
35
36
37 class FlexyPoolDatasourceAccessorTest {
38
39
40 private FlexyPoolDatasourceAccessor accessor;
41
42
43
44
45 @BeforeEach
46 void setUp() {
47 accessor = new FlexyPoolDatasourceAccessor();
48 }
49
50
51
52
53 @Test
54 void testCanMapWithFlexyPoolWrapper() {
55 assertTrue(accessor.canMap(new FlexyPoolDataSource<>(new Object())));
56 }
57
58
59
60
61 @Test
62 void testCanMapWithInvalidResource() {
63 assertFalse(accessor.canMap(new Object()));
64 }
65
66
67
68
69
70
71 @Test
72 void testGetInfoDelegatesToWrappedDatasourceAccessor() throws SQLException {
73 HikariDataSource source = mock(HikariDataSource.class);
74 HikariPoolMXBean poolMxBean = mock(HikariPoolMXBean.class);
75 when(source.getHikariPoolMXBean()).thenReturn(poolMxBean);
76 when(poolMxBean.getActiveConnections()).thenReturn(2);
77 when(poolMxBean.getTotalConnections()).thenReturn(5);
78 when(source.getMaximumPoolSize()).thenReturn(10);
79 when(source.getJdbcUrl()).thenReturn("jdbc:h2:mem:test");
80 when(source.getUsername()).thenReturn("user");
81
82 DataSourceInfo info = accessor.getInfo(new FlexyPoolDataSource<>(source));
83
84 assertNotNull(info);
85 assertEquals(2, info.getBusyConnections());
86 assertEquals(5, info.getEstablishedConnections());
87 assertEquals(10, info.getMaxConnections());
88 assertEquals("jdbc:h2:mem:test", info.getJdbcUrl());
89 assertEquals("user", info.getUsername());
90 assertEquals("hikari", info.getType());
91 }
92
93
94
95
96
97
98 @Test
99 void testGetInfoWithUnsupportedWrappedDatasource() throws SQLException {
100 assertNull(accessor.getInfo(new FlexyPoolDataSource<>(new Object())));
101 }
102
103
104
105
106
107
108 @Test
109 void testResetDelegatesToWrappedDatasourceAccessor() throws SQLException {
110 ComboPooledDataSource source = mock(ComboPooledDataSource.class);
111
112 assertTrue(accessor.reset(new FlexyPoolDataSource<>(source)));
113
114 verify(source).hardReset();
115 }
116 }