Search in sources :

Example 21 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.

the class GuavaOptionalOffsetDateTimeTest method setupTests.

@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:guava-offset-date-time-" + 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)

Example 22 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.

the class OptionalLongTest method setupTests.

@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:optional-long-" + 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 BIGINT)");
    }
    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 23 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.

the class AbstractMigrationTest method createConfiguration.

protected static TestMigrationConfiguration createConfiguration(String databaseUrl) {
    final DataSourceFactory dataSource = new DataSourceFactory();
    dataSource.setDriverClass("org.h2.Driver");
    dataSource.setUser("sa");
    dataSource.setUrl(databaseUrl);
    return new TestMigrationConfiguration(dataSource);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory)

Example 24 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.

the class JerseyIntegrationTest method configure.

@Override
protected Application configure() {
    forceSet(TestProperties.CONTAINER_PORT, "0");
    final MetricRegistry metricRegistry = new MetricRegistry();
    final SessionFactoryFactory factory = new SessionFactoryFactory();
    final DataSourceFactory dbConfig = new DataSourceFactory();
    final HibernateBundle<?> bundle = mock(HibernateBundle.class);
    final Environment environment = mock(Environment.class);
    final LifecycleEnvironment lifecycleEnvironment = mock(LifecycleEnvironment.class);
    when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
    when(environment.metrics()).thenReturn(metricRegistry);
    dbConfig.setUrl("jdbc:hsqldb:mem:DbTest-" + System.nanoTime() + "?hsqldb.translate_dti_types=false");
    dbConfig.setUser("sa");
    dbConfig.setDriverClass("org.hsqldb.jdbcDriver");
    dbConfig.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
    this.sessionFactory = factory.build(bundle, environment, dbConfig, ImmutableList.of(Person.class));
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        session.createNativeQuery("DROP TABLE people IF EXISTS").executeUpdate();
        session.createNativeQuery("CREATE TABLE people (name varchar(100) primary key, email varchar(16), birthday timestamp with time zone)").executeUpdate();
        session.createNativeQuery("INSERT INTO people VALUES ('Coda', 'coda@example.com', '1979-01-02 00:22:00+0:00')").executeUpdate();
        transaction.commit();
    }
    final DropwizardResourceConfig config = DropwizardResourceConfig.forTesting(new MetricRegistry());
    config.register(new UnitOfWorkApplicationListener("hr-db", sessionFactory));
    config.register(new PersonResource(new PersonDAO(sessionFactory)));
    config.register(new JacksonMessageBodyProvider(Jackson.newObjectMapper()));
    config.register(new PersistenceExceptionMapper());
    config.register(new DataExceptionMapper());
    config.register(new EmptyOptionalExceptionMapper());
    return config;
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) EmptyOptionalExceptionMapper(io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper) JacksonMessageBodyProvider(io.dropwizard.jersey.jackson.JacksonMessageBodyProvider) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Transaction(org.hibernate.Transaction) DropwizardResourceConfig(io.dropwizard.jersey.DropwizardResourceConfig) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Session(org.hibernate.Session)

Aggregations

DataSourceFactory (io.dropwizard.db.DataSourceFactory)24 Before (org.junit.Before)18 DBIFactory (io.dropwizard.jdbi.DBIFactory)16 DBI (org.skife.jdbi.v2.DBI)15 Handle (org.skife.jdbi.v2.Handle)15 MetricRegistry (com.codahale.metrics.MetricRegistry)4 Environment (io.dropwizard.setup.Environment)3 Provides (com.google.inject.Provides)2 Singleton (com.google.inject.Singleton)2 ManagedDataSource (io.dropwizard.db.ManagedDataSource)2 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)2 Session (org.hibernate.Session)2 Transaction (org.hibernate.Transaction)2 RenderCommand (com.example.helloworld.cli.RenderCommand)1 AssetsBundle (io.dropwizard.assets.AssetsBundle)1 EnvironmentVariableSubstitutor (io.dropwizard.configuration.EnvironmentVariableSubstitutor)1 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)1 DropwizardResourceConfig (io.dropwizard.jersey.DropwizardResourceConfig)1 JacksonMessageBodyProvider (io.dropwizard.jersey.jackson.JacksonMessageBodyProvider)1 EmptyOptionalExceptionMapper (io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper)1