use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.
the class HelloWorldApplication method initialize.
@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
// Enable variable substitution with environment variables
bootstrap.setConfigurationSourceProvider(new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false)));
bootstrap.addCommand(new RenderCommand());
bootstrap.addBundle(new AssetsBundle());
bootstrap.addBundle(new MigrationsBundle<HelloWorldConfiguration>() {
@Override
public DataSourceFactory getDataSourceFactory(HelloWorldConfiguration configuration) {
return configuration.getDataSourceFactory();
}
});
bootstrap.addBundle(hibernateBundle);
bootstrap.addBundle(new ViewBundle<HelloWorldConfiguration>() {
@Override
public Map<String, Map<String, String>> getViewConfiguration(HelloWorldConfiguration configuration) {
return configuration.getViewRendererConfiguration();
}
});
}
use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.
the class UnitOfWorkAwareProxyFactoryTest method setUp.
@Before
public void setUp() throws Exception {
final HibernateBundle<?> bundle = mock(HibernateBundle.class);
final Environment environment = mock(Environment.class);
when(environment.lifecycle()).thenReturn(mock(LifecycleEnvironment.class));
when(environment.metrics()).thenReturn(new MetricRegistry());
final DataSourceFactory dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setUrl("jdbc:hsqldb:mem:unit-of-work-" + UUID.randomUUID().toString());
dataSourceFactory.setUser("sa");
dataSourceFactory.setDriverClass("org.hsqldb.jdbcDriver");
dataSourceFactory.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
dataSourceFactory.setProperties(ImmutableMap.of("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"));
dataSourceFactory.setInitialSize(1);
dataSourceFactory.setMinSize(1);
sessionFactory = new SessionFactoryFactory().build(bundle, environment, dataSourceFactory, ImmutableList.of());
try (Session session = sessionFactory.openSession()) {
Transaction transaction = session.beginTransaction();
session.createNativeQuery("create table user_sessions (token varchar(64) primary key, username varchar(16))").executeUpdate();
session.createNativeQuery("insert into user_sessions values ('67ab89d', 'jeff_28')").executeUpdate();
transaction.commit();
}
}
use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.
the class CloseableLiquibaseTest method setUp.
@Before
public void setUp() throws Exception {
DataSourceFactory factory = new DataSourceFactory();
factory.setDriverClass(org.h2.Driver.class.getName());
factory.setUrl("jdbc:h2:mem:DbTest-" + System.currentTimeMillis());
factory.setUser("DbTest");
dataSource = (ManagedPooledDataSource) factory.build(new MetricRegistry(), "DbTest");
liquibase = new CloseableLiquibaseWithClassPathMigrationsFile(dataSource, "migrations.xml");
}
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;
}
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);
}
Aggregations