Example usage for javax.sql DataSource isWrapperFor

List of usage examples for javax.sql DataSource isWrapperFor

Introduction

In this page you can find the example usage for javax.sql DataSource isWrapperFor.

Prototype

boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException;

Source Link

Document

Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.

Usage

From source file:com.micromux.cassandra.jdbc.DataSourceTest.java

@Test
public void testIsWrapperFor() throws Exception {
    DataSource ds = new CassandraDataSource(HOST, PORT, KEYSPACE, USER, PASSWORD, VERSION, CONSISTENCY,
            TRUST_STORE, TRUST_PASS);/*from  w  w  w .ja va  2 s .  c  om*/

    boolean isIt = false;

    // it is a wrapper for DataSource
    isIt = ds.isWrapperFor(DataSource.class);
    assertTrue(isIt);

    // it is not a wrapper for this test class
    isIt = ds.isWrapperFor(this.getClass());
    assertFalse(isIt);
}

From source file:org.kuali.rice.web.health.DatabaseConnectionPoolMetricSet.java

private <T> T tryUnwrap(DataSource dataSource, Class<T> targetType) {
    if (targetType.isInstance(dataSource)) {
        return targetType.cast(dataSource);
    }/* w  ww.  ja  va  2 s  . co m*/
    try {
        if (dataSource.isWrapperFor(targetType)) {
            return dataSource.unwrap(targetType);
        }
    } catch (SQLException e) {
        LOG.warn("Exception when trying to unwrap datasource as " + targetType, e);
    }
    return null;
}

From source file:org.kuali.rice.web.health.DatabaseConnectionPoolMetricSetTest.java

@Test
public void testGetMetrics_Wrapped_XAPool() throws Exception {
    StandardXAPoolDataSource dataSource = mock(StandardXAPoolDataSource.class);
    when(dataSource.getLockedObjectCount()).thenReturn(10);
    when(dataSource.getMinSize()).thenReturn(5);
    when(dataSource.getMaxSize()).thenReturn(20);

    // create the wrapper
    DataSource wrapperDataSource = mock(DataSource.class);
    when(wrapperDataSource.isWrapperFor(StandardXAPoolDataSource.class)).thenReturn(true);
    when(wrapperDataSource.unwrap(StandardXAPoolDataSource.class)).thenReturn(dataSource);

    DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("xapool:",
            wrapperDataSource);/*from  w w  w .j  a  v  a2s.c  o m*/
    Map<String, Metric> metrics = metricSet.getMetrics();
    assertEquals(10, ((Gauge) metrics.get("xapool:pool.active")).getValue());
    assertEquals(5, ((Gauge) metrics.get("xapool:pool.min")).getValue());
    assertEquals(20, ((Gauge) metrics.get("xapool:pool.max")).getValue());
    assertEquals(0.5, ((Gauge) metrics.get("xapool:pool.usage")).getValue());
}

From source file:org.kuali.rice.web.health.DatabaseConnectionPoolMetricSetTest.java

/**
 * Tests the case where the isWrapperFor or unwrap methods throw SQLException (which they are allowed to do though
 * not sure why they ever would)/*from   ww  w.java  2 s .  c  o  m*/
 */
@Test
public void testGetMetrics_Wrapped_SQLException() throws Exception {
    StandardXAPoolDataSource dataSource = mock(StandardXAPoolDataSource.class);
    // create the wrapper
    DataSource wrapperDataSource = mock(DataSource.class);
    when(wrapperDataSource.isWrapperFor(StandardXAPoolDataSource.class)).thenThrow(new SQLException());

    DatabaseConnectionPoolMetricSet metricSet = new DatabaseConnectionPoolMetricSet("xapool:",
            wrapperDataSource);
    Map<String, Metric> metrics = metricSet.getMetrics();
    // in this case, getMetrics returns an empty map since we failed when trying to unwrap the connection
    assertTrue("Metrics map should be empty", metricSet.getMetrics().isEmpty());
}