1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
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
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
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
120
121
122 @Test
123 public void testGetMetrics_Wrapped_SQLException() throws Exception {
124 StandardXAPoolDataSource dataSource = mock(StandardXAPoolDataSource.class);
125
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
132 assertTrue("Metrics map should be empty", metricSet.getMetrics().isEmpty());
133 }
134
135
136 }