Search in sources :

Example 1 with DataSourceFactory

use of io.bootique.jdbc.DataSourceFactory in project bootique-jdbc by bootique.

the class HikariCPInstrumentedModuleIT method testMetrics_TurnedOn.

@Test
public void testMetrics_TurnedOn() {
    BQRuntime runtime = TEST_FACTORY.app("-c", "classpath:io/bootique/jdbc/instrumented/hikaricp/hikaricp-ds-health.yml").autoLoadModules().createRuntime();
    DataSourceFactory factory = runtime.getInstance(DataSourceFactory.class);
    DataSource ds = factory.forName("derby1");
    assertNotNull(ds);
    assertTrue(ds instanceof HikariDataSource);
    HikariDataSource hikariDS = (HikariDataSource) ds;
    String poolName = hikariDS.getPoolName();
    MetricRegistry metricRegistry = runtime.getInstance(MetricRegistry.class);
    assertEquals(metricRegistry.getTimers().size(), 1);
    assertEquals(metricRegistry.getTimers().firstKey(), MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_WAIT));
    assertEquals(metricRegistry.getGauges().size(), 4);
    assertEquals(metricRegistry.getGauges().keySet(), new HashSet<String>() {

        {
            add(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_TOTAL_CONNECTIONS));
            add(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_IDLE_CONNECTIONS));
            add(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_ACTIVE_CONNECTIONS));
            add(MetricRegistry.name(poolName, METRIC_CATEGORY, METRIC_NAME_PENDING_CONNECTIONS));
        }
    });
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) BQRuntime(io.bootique.BQRuntime) MetricRegistry(com.codahale.metrics.MetricRegistry) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 2 with DataSourceFactory

use of io.bootique.jdbc.DataSourceFactory in project bootique-jdbc by bootique.

the class HikariCPInstrumentedModuleIT method testHealthChecks_HealthNoParam_ChecksTurnedOff.

@Test
public void testHealthChecks_HealthNoParam_ChecksTurnedOff() {
    BQRuntime runtime = TEST_FACTORY.app("-c", "classpath:io/bootique/jdbc/instrumented/hikaricp/dummy-ds.yml").autoLoadModules().createRuntime();
    DataSourceFactory factory = runtime.getInstance(DataSourceFactory.class);
    String dataSourceName = "DerbyDatabaseIT";
    HikariDataSource dataSource = (HikariDataSource) factory.forName(dataSourceName);
    assertNotNull(dataSource);
    HealthCheckRegistry registry = runtime.getInstance(HealthCheckRegistry.class);
    /**
     * embedded health check {@link io.bootique.jdbc.instrumented.healthcheck.DataSourceHealthCheck}
     */
    assertTrue(registry.containsHealthCheck(DataSourceHealthCheck.healthCheckName(dataSourceName)));
    Map<String, HealthCheckOutcome> results = registry.runHealthChecks();
    assertEquals(results.size(), 1);
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) HealthCheckOutcome(io.bootique.metrics.health.HealthCheckOutcome) BQRuntime(io.bootique.BQRuntime) HealthCheckRegistry(io.bootique.metrics.health.HealthCheckRegistry) Test(org.junit.Test)

Example 3 with DataSourceFactory

use of io.bootique.jdbc.DataSourceFactory in project bootique-jdbc by bootique.

the class TomcatInstrumentedJdbcModuleIT method testMetrics.

@Test
public void testMetrics() {
    BQRuntime runtime = TEST_FACTORY.app("-c", "classpath:io/bootique/jdbc/instrumented/dummy-ds.yml").autoLoadModules().createRuntime();
    DataSourceFactory factory = runtime.getInstance(DataSourceFactory.class);
    DataSource dataSource = factory.forName("DerbyDatabaseIT");
    assertNotNull(dataSource);
    MetricRegistry metricRegistry = runtime.getInstance(MetricRegistry.class);
    assertNotEquals(metricRegistry.getGauges().size(), 0);
}
Also used : DataSourceFactory(io.bootique.jdbc.DataSourceFactory) BQRuntime(io.bootique.BQRuntime) MetricRegistry(com.codahale.metrics.MetricRegistry) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 4 with DataSourceFactory

use of io.bootique.jdbc.DataSourceFactory in project bootique-jdbc by bootique.

the class ManagedDataSourceFactoryProxyIT method testFactory_MultiLevel.

@Test
public void testFactory_MultiLevel() {
    BQRuntime runtime = testFactory.app("-c", "classpath:io/bootique/jdbc/factory-type.yml").autoLoadModules().module(b -> JdbcModule.extend(b).addFactoryType(ManagedDataSourceFactoryX1.class).addFactoryType(ManagedDataSourceFactoryX2.class).addFactoryType(ManagedDataSourceFactoryX3.class).addFactoryType(ManagedDataSourceFactoryX4.class)).createRuntime();
    ConfigurationFactory configFactory = runtime.getInstance(ConfigurationFactory.class);
    Map<String, ManagedDataSourceFactory> configs = configFactory.config(new TypeRef<Map<String, ManagedDataSourceFactory>>() {
    }, configPrefix);
    assertTrue(configs.get("ds1") instanceof ManagedDataSourceFactoryX4);
    assertTrue(configs.get("ds2") instanceof ManagedDataSourceFactoryX1);
    DataSourceFactory factory = runtime.getInstance(DataSourceFactory.class);
    DataSource ds1 = factory.forName("ds1");
    assertNotNull(ds1);
    DataSource ds2 = factory.forName("ds2");
    assertNotNull(ds2);
}
Also used : JdbcModule(io.bootique.jdbc.JdbcModule) ConfigurationFactory(io.bootique.config.ConfigurationFactory) Assert.assertNotNull(org.junit.Assert.assertNotNull) BQRuntime(io.bootique.BQRuntime) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) BQTestFactory(io.bootique.test.junit.BQTestFactory) Injector(com.google.inject.Injector) TypeRef(io.bootique.type.TypeRef) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) BootiqueException(io.bootique.BootiqueException) Rule(org.junit.Rule) Map(java.util.Map) DataSource(javax.sql.DataSource) Optional(java.util.Optional) Assert.fail(org.junit.Assert.fail) ProvisionException(com.google.inject.ProvisionException) DataSourceFactory(io.bootique.jdbc.DataSourceFactory) Mockito.mock(org.mockito.Mockito.mock) DataSourceFactory(io.bootique.jdbc.DataSourceFactory) BQRuntime(io.bootique.BQRuntime) DataSource(javax.sql.DataSource) ConfigurationFactory(io.bootique.config.ConfigurationFactory) Map(java.util.Map) Test(org.junit.Test)

Example 5 with DataSourceFactory

use of io.bootique.jdbc.DataSourceFactory in project bootique-jdbc by bootique.

the class ManagedDataSourceFactoryProxyIT method testFactories_Hierarchical.

@Test
public void testFactories_Hierarchical() {
    BQRuntime runtime = testFactory.app("-c", "classpath:io/bootique/jdbc/factories-type.yml").autoLoadModules().module(b -> JdbcModule.extend(b).addFactoryType(ManagedDataSourceFactoryX1.class).addFactoryType(ManagedDataSourceFactoryX2.class).addFactoryType(ManagedDataSourceFactoryX3.class).addFactoryType(ManagedDataSourceFactoryX33.class).addFactoryType(ManagedDataSourceFactoryY1.class).addFactoryType(ManagedDataSourceFactoryY2.class)).createRuntime();
    ConfigurationFactory configFactory = runtime.getInstance(ConfigurationFactory.class);
    Map<String, ManagedDataSourceFactory> configs = configFactory.config(new TypeRef<Map<String, ManagedDataSourceFactory>>() {
    }, configPrefix);
    assertTrue(configs.get("ds1") instanceof ManagedDataSourceFactoryX33);
    assertTrue(configs.get("ds2") instanceof ManagedDataSourceFactoryY2);
    DataSourceFactory factory = runtime.getInstance(DataSourceFactory.class);
    DataSource ds1 = factory.forName("ds1");
    assertNotNull(ds1);
    DataSource ds2 = factory.forName("ds2");
    assertNotNull(ds2);
}
Also used : JdbcModule(io.bootique.jdbc.JdbcModule) ConfigurationFactory(io.bootique.config.ConfigurationFactory) Assert.assertNotNull(org.junit.Assert.assertNotNull) BQRuntime(io.bootique.BQRuntime) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) BQTestFactory(io.bootique.test.junit.BQTestFactory) Injector(com.google.inject.Injector) TypeRef(io.bootique.type.TypeRef) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) BootiqueException(io.bootique.BootiqueException) Rule(org.junit.Rule) Map(java.util.Map) DataSource(javax.sql.DataSource) Optional(java.util.Optional) Assert.fail(org.junit.Assert.fail) ProvisionException(com.google.inject.ProvisionException) DataSourceFactory(io.bootique.jdbc.DataSourceFactory) Mockito.mock(org.mockito.Mockito.mock) DataSourceFactory(io.bootique.jdbc.DataSourceFactory) BQRuntime(io.bootique.BQRuntime) DataSource(javax.sql.DataSource) ConfigurationFactory(io.bootique.config.ConfigurationFactory) Map(java.util.Map) Test(org.junit.Test)

Aggregations

BQRuntime (io.bootique.BQRuntime)9 DataSourceFactory (io.bootique.jdbc.DataSourceFactory)9 Test (org.junit.Test)9 DataSource (javax.sql.DataSource)6 JsonTypeName (com.fasterxml.jackson.annotation.JsonTypeName)4 Injector (com.google.inject.Injector)4 ProvisionException (com.google.inject.ProvisionException)4 HikariDataSource (com.zaxxer.hikari.HikariDataSource)4 BootiqueException (io.bootique.BootiqueException)4 ConfigurationFactory (io.bootique.config.ConfigurationFactory)4 JdbcModule (io.bootique.jdbc.JdbcModule)4 BQTestFactory (io.bootique.test.junit.BQTestFactory)4 TypeRef (io.bootique.type.TypeRef)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Assert.assertNotNull (org.junit.Assert.assertNotNull)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Assert.fail (org.junit.Assert.fail)4 Rule (org.junit.Rule)4 Mockito.mock (org.mockito.Mockito.mock)4