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 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   * The Class FlexyPoolDatasourceAccessorTest.
36   */
37  class FlexyPoolDatasourceAccessorTest {
38  
39    /** The accessor. */
40    private FlexyPoolDatasourceAccessor accessor;
41  
42    /**
43     * Sets the up.
44     */
45    @BeforeEach
46    void setUp() {
47      accessor = new FlexyPoolDatasourceAccessor();
48    }
49  
50    /**
51     * Test can map with flexy pool wrapper.
52     */
53    @Test
54    void testCanMapWithFlexyPoolWrapper() {
55      assertTrue(accessor.canMap(new FlexyPoolDataSource<>(new Object())));
56    }
57  
58    /**
59     * Test can map with invalid resource.
60     */
61    @Test
62    void testCanMapWithInvalidResource() {
63      assertFalse(accessor.canMap(new Object()));
64    }
65  
66    /**
67     * Test get info delegates to wrapped datasource accessor.
68     *
69     * @throws SQLException the sql exception
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     * Test get info with unsupported wrapped datasource.
95     *
96     * @throws SQLException the sql exception
97     */
98    @Test
99    void testGetInfoWithUnsupportedWrappedDatasource() throws SQLException {
100     assertNull(accessor.getInfo(new FlexyPoolDataSource<>(new Object())));
101   }
102 
103   /**
104    * Test reset delegates to wrapped datasource accessor.
105    *
106    * @throws SQLException the sql exception
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 }