View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans.accessors;
12  
13  import com.zaxxer.hikari.HikariDataSource;
14  import com.zaxxer.hikari.HikariPoolMXBean;
15  
16  import java.sql.SQLException;
17  
18  import org.junit.jupiter.api.Assertions;
19  import org.junit.jupiter.api.BeforeEach;
20  import org.junit.jupiter.api.Test;
21  import org.junit.jupiter.api.extension.ExtendWith;
22  import org.mockito.Mock;
23  import org.mockito.Mockito;
24  import org.mockito.junit.jupiter.MockitoExtension;
25  import org.vibur.dbcp.ViburDBCPDataSource;
26  
27  /**
28   * The Class HikariCpDatasourceAccessorTest.
29   */
30  @ExtendWith(MockitoExtension.class)
31  class HikariCpDatasourceAccessorTest {
32  
33    /** The accessor. */
34    HikariCpDatasourceAccessor accessor;
35  
36    /** The source. */
37    @Mock
38    HikariDataSource source;
39  
40    /** The bad source. */
41    ViburDBCPDataSource badSource;
42  
43    /**
44     * Before.
45     */
46    @BeforeEach
47    void before() {
48      accessor = new HikariCpDatasourceAccessor();
49      badSource = new ViburDBCPDataSource();
50    }
51  
52    /**
53     * Can map test.
54     */
55    @Test
56    void canMapTest() {
57      Assertions.assertTrue(accessor.canMap(source));
58    }
59  
60    /**
61     * Cannot map test.
62     */
63    @Test
64    void cannotMapTest() {
65      Assertions.assertFalse(accessor.canMap(badSource));
66    }
67  
68    /**
69     * Gets the info test.
70     *
71     * @throws SQLException the sql exception
72     */
73    @Test
74    void getInfoTest() throws SQLException {
75      var poolMxBean = Mockito.mock(HikariPoolMXBean.class);
76      Mockito.when(source.getHikariPoolMXBean()).thenReturn(poolMxBean);
77      Mockito.when(poolMxBean.getActiveConnections()).thenReturn(1);
78      Mockito.when(poolMxBean.getTotalConnections()).thenReturn(2);
79  
80      Assertions.assertNotNull(accessor.getInfo(source));
81    }
82  
83  }