View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.web.health;
17  
18  import bitronix.tm.resource.jdbc.PoolingDataSource;
19  import com.codahale.metrics.Gauge;
20  import com.codahale.metrics.Metric;
21  import org.apache.commons.dbcp.BasicDataSource;
22  import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  import org.mockito.runners.MockitoJUnitRunner;
26  
27  import javax.sql.DataSource;
28  import java.sql.SQLException;
29  import java.util.Map;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertTrue;
33  import static org.mockito.Mockito.mock;
34  import static org.mockito.Mockito.when;
35  
36  /**
37   * Unit test for {@link DatabaseConnectionPoolMetricSet}
38   *
39   * @author Eric Westfall
40   */
41  @RunWith(MockitoJUnitRunner.class)
42  public class DatabaseConnectionPoolMetricSetTest {
43  
44      @Test
45      public void testGetMetrics_XAPool() throws Exception {
46          StandardXAPoolDataSource dataSource = mock(StandardXAPoolDataSource.class);
47          when(dataSource.getLockedObjectCount()).thenReturn(10);
48          when(dataSource.getMinSize()).thenReturn(5);
49          when(dataSource.getMaxSize()).thenReturn(20);
50  
51          DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("xapool:", dataSource);
52          Map<String, Metric> metrics = metricSet.getMetrics();
53          assertEquals(10, ((Gauge)metrics.get("xapool:pool.active")).getValue());
54          assertEquals(5, ((Gauge)metrics.get("xapool:pool.min")).getValue());
55          assertEquals(20, ((Gauge)metrics.get("xapool:pool.max")).getValue());
56          assertEquals(0.5, ((Gauge)metrics.get("xapool:pool.usage")).getValue());
57      }
58  
59      @Test
60      public void testGetMetrics_Bitronix() throws Exception {
61          PoolingDataSource dataSource = mock(PoolingDataSource.class);
62          when(dataSource.getTotalPoolSize()).thenReturn(15L);
63          when(dataSource.getInPoolSize()).thenReturn(5L);
64          when(dataSource.getMinPoolSize()).thenReturn(5);
65          when(dataSource.getMaxPoolSize()).thenReturn(20);
66  
67          DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("btm:", dataSource);
68          Map<String, Metric> metrics = metricSet.getMetrics();
69          assertEquals(10, ((Gauge)metrics.get("btm:pool.active")).getValue());
70          assertEquals(5, ((Gauge)metrics.get("btm:pool.min")).getValue());
71          assertEquals(20, ((Gauge)metrics.get("btm:pool.max")).getValue());
72          assertEquals(0.5, ((Gauge)metrics.get("btm:pool.usage")).getValue());
73      }
74  
75      @Test
76      public void testGetMetrics_DBCP() throws Exception {
77          BasicDataSource dataSource = mock(BasicDataSource.class);
78          when(dataSource.getNumActive()).thenReturn(10);
79          when(dataSource.getMinIdle()).thenReturn(5);
80          when(dataSource.getMaxActive()).thenReturn(20);
81  
82          DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("dbcp:", dataSource);
83          Map<String, Metric> metrics = metricSet.getMetrics();
84          assertEquals(10, ((Gauge)metrics.get("dbcp:pool.active")).getValue());
85          assertEquals(5, ((Gauge)metrics.get("dbcp:pool.min")).getValue());
86          assertEquals(20, ((Gauge)metrics.get("dbcp:pool.max")).getValue());
87          assertEquals(0.5, ((Gauge)metrics.get("dbcp:pool.usage")).getValue());
88      }
89  
90      @Test
91      public void testGetMetrics_Unknown() throws Exception {
92          DataSource dataSource = mock(DataSource.class);
93          DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("unknown:", dataSource);
94          // in this case, getMetrics returns an empty map
95          assertTrue("Metrics map should be empty", metricSet.getMetrics().isEmpty());
96      }
97  
98      @Test
99      public void testGetMetrics_Wrapped_XAPool() throws Exception {
100         StandardXAPoolDataSource dataSource = mock(StandardXAPoolDataSource.class);
101         when(dataSource.getLockedObjectCount()).thenReturn(10);
102         when(dataSource.getMinSize()).thenReturn(5);
103         when(dataSource.getMaxSize()).thenReturn(20);
104 
105         // create the wrapper
106         DataSource wrapperDataSource = mock(DataSource.class);
107         when(wrapperDataSource.isWrapperFor(StandardXAPoolDataSource.class)).thenReturn(true);
108         when(wrapperDataSource.unwrap(StandardXAPoolDataSource.class)).thenReturn(dataSource);
109 
110         DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("xapool:", wrapperDataSource);
111         Map<String, Metric> metrics = metricSet.getMetrics();
112         assertEquals(10, ((Gauge)metrics.get("xapool:pool.active")).getValue());
113         assertEquals(5, ((Gauge)metrics.get("xapool:pool.min")).getValue());
114         assertEquals(20, ((Gauge)metrics.get("xapool:pool.max")).getValue());
115         assertEquals(0.5, ((Gauge)metrics.get("xapool:pool.usage")).getValue());
116     }
117 
118     /**
119      * Tests the case where the isWrapperFor or unwrap methods throw SQLException (which they are allowed to do though
120      * not sure why they ever would)
121      */
122     @Test
123     public void testGetMetrics_Wrapped_SQLException() throws Exception {
124         StandardXAPoolDataSource dataSource = mock(StandardXAPoolDataSource.class);
125         // create the wrapper
126         DataSource wrapperDataSource = mock(DataSource.class);
127         when(wrapperDataSource.isWrapperFor(StandardXAPoolDataSource.class)).thenThrow(new SQLException());
128 
129         DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("xapool:", wrapperDataSource);
130         Map<String, Metric> metrics = metricSet.getMetrics();
131         // in this case, getMetrics returns an empty map since we failed when trying to unwrap the connection
132         assertTrue("Metrics map should be empty", metricSet.getMetrics().isEmpty());
133     }
134 
135 
136 }