Search in sources :

Example 1 with Handle

use of org.skife.jdbi.v2.Handle in project dropwizard by dropwizard.

the class DBIHealthCheckTest method testItTimesOutProperly.

@Test
public void testItTimesOutProperly() throws Exception {
    String validationQuery = "select 1";
    DBI dbi = mock(DBI.class);
    Handle handle = mock(Handle.class);
    when(dbi.open()).thenReturn(handle);
    Mockito.doAnswer(invocation -> {
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (Exception ignored) {
        }
        return null;
    }).when(handle).execute(validationQuery);
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    DBIHealthCheck dbiHealthCheck = new DBIHealthCheck(executorService, Duration.milliseconds(5), dbi, validationQuery);
    HealthCheck.Result result = dbiHealthCheck.check();
    executorService.shutdown();
    assertThat("is unhealthy", false, is(result.isHealthy()));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) HealthCheck(com.codahale.metrics.health.HealthCheck) DBI(org.skife.jdbi.v2.DBI) Handle(org.skife.jdbi.v2.Handle) Test(org.junit.Test)

Example 2 with Handle

use of org.skife.jdbi.v2.Handle in project dropwizard by dropwizard.

the class JDBITest method setUp.

@Before
public void setUp() throws Exception {
    when(environment.healthChecks()).thenReturn(healthChecks);
    when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
    when(environment.metrics()).thenReturn(metricRegistry);
    when(environment.getHealthCheckExecutorService()).thenReturn(Executors.newSingleThreadExecutor());
    this.dbi = factory.build(environment, hsqlConfig, "hsql");
    final ArgumentCaptor<Managed> managedCaptor = ArgumentCaptor.forClass(Managed.class);
    verify(lifecycleEnvironment).manage(managedCaptor.capture());
    managed.addAll(managedCaptor.getAllValues());
    for (Managed obj : managed) {
        obj.start();
    }
    try (Handle handle = dbi.open()) {
        handle.createCall("DROP TABLE people IF EXISTS").invoke();
        handle.createCall("CREATE TABLE people (name varchar(100) primary key, email varchar(100), age int, created_at timestamp)").invoke();
        handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Coda Hale").bind(1, "chale@yammer-inc.com").bind(2, 30).bind(3, new Timestamp(1365465078000L)).execute();
        handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Kris Gale").bind(1, "kgale@yammer-inc.com").bind(2, 32).bind(3, new Timestamp(1365465078000L)).execute();
        handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Old Guy").bindNull(1, Types.VARCHAR).bind(2, 99).bind(3, new Timestamp(1365465078000L)).execute();
        handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Alice Example").bind(1, "alice@example.org").bind(2, 99).bindNull(3, Types.TIMESTAMP).execute();
    }
}
Also used : Timestamp(java.sql.Timestamp) Managed(io.dropwizard.lifecycle.Managed) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 3 with Handle

use of org.skife.jdbi.v2.Handle in project dropwizard by dropwizard.

the class OptionalDoubleTest method setupTests.

@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:optional-double-" + System.currentTimeMillis() + "?user=sa");
    dataSourceFactory.setInitialSize(1);
    final DBI dbi = new DBIFactory().build(env, dataSourceFactory, "test");
    try (Handle h = dbi.open()) {
        h.execute("CREATE TABLE test (id INT PRIMARY KEY, optional DOUBLE)");
    }
    dao = dbi.onDemand(TestDao.class);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) DBI(org.skife.jdbi.v2.DBI) DBIFactory(io.dropwizard.jdbi.DBIFactory) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 4 with Handle

use of org.skife.jdbi.v2.Handle in project dropwizard by dropwizard.

the class OptionalIntTest method setupTests.

@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:optional-int-" + System.currentTimeMillis() + "?user=sa");
    dataSourceFactory.setInitialSize(1);
    final DBI dbi = new DBIFactory().build(env, dataSourceFactory, "test");
    try (Handle h = dbi.open()) {
        h.execute("CREATE TABLE test (id INT PRIMARY KEY, optional INT)");
    }
    dao = dbi.onDemand(TestDao.class);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) DBI(org.skife.jdbi.v2.DBI) DBIFactory(io.dropwizard.jdbi.DBIFactory) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 5 with Handle

use of org.skife.jdbi.v2.Handle in project dropwizard by dropwizard.

the class GuavaOptionalLocalDateTest method setupTests.

@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:guava-local-date-" + System.currentTimeMillis() + "?user=sa");
    dataSourceFactory.setInitialSize(1);
    final DBI dbi = new DBIFactory().build(env, dataSourceFactory, "test");
    try (Handle h = dbi.open()) {
        h.execute("CREATE TABLE IF NOT EXISTS tasks (" + "id INT PRIMARY KEY, " + "assignee VARCHAR(255) NOT NULL, " + "start_date TIMESTAMP, " + "end_date TIMESTAMP, " + "comments VARCHAR(1024) " + ")");
    }
    dao = dbi.onDemand(TaskDao.class);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) DBI(org.skife.jdbi.v2.DBI) DBIFactory(io.dropwizard.jdbi.DBIFactory) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Aggregations

Handle (org.skife.jdbi.v2.Handle)103 DBI (org.skife.jdbi.v2.DBI)29 IOException (java.io.IOException)23 Before (org.junit.Before)21 SQLException (java.sql.SQLException)19 List (java.util.List)18 Map (java.util.Map)17 DateTime (org.joda.time.DateTime)17 DataSourceFactory (io.dropwizard.db.DataSourceFactory)15 DBIFactory (io.dropwizard.jdbi.DBIFactory)15 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)14 Test (org.testng.annotations.Test)14 ResultSet (java.sql.ResultSet)11 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)11 CallbackFailedException (org.skife.jdbi.v2.exceptions.CallbackFailedException)10 ImmutableList (com.google.common.collect.ImmutableList)9 UUID (java.util.UUID)8 Nullable (javax.annotation.Nullable)8 DataSegment (org.apache.druid.timeline.DataSegment)8