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.when;
20  
21  import org.apache.tomee.jdbc.TomEEDataSourceCreator;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  import psiprobe.model.DataSourceInfo;
26  
27  /**
28   * The Class TomEeJdbcPoolDatasourceAccessorTest.
29   */
30  class TomEeJdbcPoolDatasourceAccessorTest {
31  
32    /** The accessor. */
33    private TomEeJdbcPoolDatasourceAccessor accessor;
34  
35    /**
36     * Sets the up.
37     */
38    @BeforeEach
39    void setUp() {
40      accessor = new TomEeJdbcPoolDatasourceAccessor();
41    }
42  
43    /**
44     * Test get info with valid resource.
45     */
46    @Test
47    void testGetInfoWithValidResource() {
48      TomEEDataSourceCreator.TomEEDataSource ds = mock(TomEEDataSourceCreator.TomEEDataSource.class);
49      when(ds.getNumActive()).thenReturn(2);
50      when(ds.getNumIdle()).thenReturn(3);
51      when(ds.getMaxActive()).thenReturn(10);
52      when(ds.getUrl()).thenReturn("jdbc:h2:mem:test");
53      when(ds.getUsername()).thenReturn("user");
54  
55      DataSourceInfo info = accessor.getInfo(ds);
56  
57      assertNotNull(info);
58      assertEquals(2, info.getBusyConnections());
59      assertEquals(5, info.getEstablishedConnections());
60      assertEquals(10, info.getMaxConnections());
61      assertEquals("jdbc:h2:mem:test", info.getJdbcUrl());
62      assertEquals("user", info.getUsername());
63      assertFalse(info.isResettable());
64      assertEquals("tomcat-jdbc", info.getType());
65    }
66  
67    /**
68     * Test get info with invalid resource.
69     */
70    @Test
71    void testGetInfoWithInvalidResource() {
72      Object notDs = new Object();
73      DataSourceInfo info = accessor.getInfo(notDs);
74      assertNull(info);
75    }
76  
77    /**
78     * Test reset always returns false.
79     */
80    @Test
81    void testResetAlwaysReturnsFalse() {
82      Object any = new Object();
83      assertFalse(accessor.reset(any));
84    }
85  
86    /**
87     * Test can map with valid resource.
88     */
89    @Test
90    void testCanMapWithValidResource() {
91      TomEEDataSourceCreator.TomEEDataSource ds = mock(TomEEDataSourceCreator.TomEEDataSource.class);
92      assertTrue(accessor.canMap(ds));
93    }
94  
95    /**
96     * Test can map with invalid resource.
97     */
98    @Test
99    void testCanMapWithInvalidResource() {
100     Object notDs = new Object();
101     assertFalse(accessor.canMap(notDs));
102   }
103 
104 }