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.mchange.v2.c3p0.ComboPooledDataSource;
14  import com.mchange.v2.c3p0.jboss.C3P0PooledDataSource;
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.mockito.Mockito;
22  
23  /**
24   * The Class C3P0DatasourceAccessorTest.
25   */
26  class C3P0DatasourceAccessorTest {
27  
28    /** The accessor. */
29    C3P0DatasourceAccessor accessor;
30  
31    /** The source. */
32    ComboPooledDataSource source;
33  
34    /** The bad source. */
35    C3P0PooledDataSource badSource;
36  
37    /**
38     * Before.
39     */
40    @BeforeEach
41    void before() {
42      accessor = new C3P0DatasourceAccessor();
43      source = new ComboPooledDataSource();
44      badSource = new C3P0PooledDataSource();
45    }
46  
47    /**
48     * Can map test.
49     */
50    @Test
51    void canMapTest() {
52      Assertions.assertTrue(accessor.canMap(source));
53    }
54  
55    /**
56     * Cannot map test.
57     */
58    @Test
59    void cannotMapTest() {
60      Assertions.assertFalse(accessor.canMap(badSource));
61    }
62  
63    /**
64     * Gets the info test.
65     *
66     * @throws SQLException the sql exception
67     */
68    @Test
69    void infoTest() throws SQLException {
70      Assertions.assertNotNull(accessor.getInfo(source));
71    }
72  
73    /**
74     * Gets the info with bad source test.
75     *
76     * @return the info with bad source test
77     * @throws SQLException the SQL exception
78     */
79    @Test
80    void getInfoWithBadSourceTest() throws SQLException {
81      Assertions.assertNull(accessor.getInfo(badSource));
82    }
83  
84    /**
85     * Gets the info throws SQL exception test.
86     *
87     * @return the info throws SQL exception test
88     * @throws Exception the exception
89     */
90    @Test
91    void getInfoThrowsSQLExceptionTest() throws Exception {
92      ComboPooledDataSource mockSource = Mockito.mock(ComboPooledDataSource.class);
93      Mockito.when(mockSource.getNumConnectionsDefaultUser())
94          .thenThrow(new SQLException("Test exception"));
95      Assertions.assertNotNull(accessor.getInfo(mockSource));
96    }
97  
98    /**
99     * Reset test.
100    */
101   @Test
102   void resetTest() {
103     ComboPooledDataSource mockSource = Mockito.mock(ComboPooledDataSource.class);
104     Assertions.assertDoesNotThrow(() -> accessor.reset(mockSource));
105   }
106 
107 }