Last active
September 3, 2018 13:14
-
-
Save Romeh/a235f916ba746e8c5e4ce9376cf4e049 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableTransactionManagement | |
@EnableJpaRepositories(basePackageClasses = {CustomerRepository.class}) | |
@Profile("DaoTest") | |
public class DbConfig { | |
private static final List<String> DEFAULT_ADDITIONAL_INIT_DB_PARAMS = Arrays | |
.asList("--nosync", "--locale=en_US.UTF-8"); | |
/** | |
* @param config the PostgresConfig configuration to use to start Postgres db process | |
* @return PostgresProcess , the started db process | |
* @throws IOException | |
*/ | |
@Bean | |
@DependsOn("postgresProcess") | |
public DataSource dataSource(PostgresConfig config) { | |
DriverManagerDataSource ds = new DriverManagerDataSource(); | |
ds.setDriverClassName("org.postgresql.Driver"); | |
ds.setUrl(format("jdbc:postgresql://%s:%s/%s", config.net().host(), config.net().port(), config.storage().dbName())); | |
ds.setUsername(config.credentials().username()); | |
ds.setPassword(config.credentials().password()); | |
return ds; | |
} | |
/** | |
* @param dataSource the db data source | |
* @return the local entity manager factory bean | |
*/ | |
@Bean | |
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { | |
LocalContainerEntityManagerFactoryBean lcemfb | |
= new LocalContainerEntityManagerFactoryBean(); | |
lcemfb.setDataSource(dataSource); | |
// set the packages to scan , it can be useful if you have big project and you just need to local partial entities for testing | |
lcemfb.setPackagesToScan("io.romeh.postgresembeddeddaotesting.domain", "io.romeh.postgresembeddeddaotesting.dao"); | |
HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter(); | |
lcemfb.setJpaVendorAdapter(va); | |
lcemfb.setJpaProperties(getHibernateProperties()); | |
lcemfb.afterPropertiesSet(); | |
return lcemfb; | |
} | |
/** | |
* @param localContainerEntityManagerFactoryBean | |
* @return the JPA transaction manager | |
*/ | |
@Bean | |
public JpaTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean) { | |
JpaTransactionManager transactionManager = new JpaTransactionManager(); | |
transactionManager.setEntityManagerFactory(localContainerEntityManagerFactoryBean.getObject()); | |
return transactionManager; | |
} | |
@Bean | |
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { | |
return new PersistenceExceptionTranslationPostProcessor(); | |
} | |
/** | |
* @return the hibernate properties | |
*/ | |
private Properties getHibernateProperties() { | |
Properties ps = new Properties(); | |
ps.put("hibernate.temp.use_jdbc_metadata_defaults", "false"); | |
ps.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL95Dialect"); | |
ps.put("hibernate.hbm2ddl.auto", "update"); | |
ps.put("hibernate.connection.characterEncoding", "UTF-8"); | |
ps.put("hibernate.connection.charSet", "UTF-8"); | |
ps.put(AvailableSettings.FORMAT_SQL, "true"); | |
ps.put(AvailableSettings.SHOW_SQL, "true"); | |
return ps; | |
} | |
@Bean | |
public PostgresConfig postgresConfig() throws IOException { | |
final PostgresConfig postgresConfig = new PostgresConfig(Version.V9_6_8, | |
new AbstractPostgresConfig.Net("localhost", Network.getFreeServerPort()), | |
new AbstractPostgresConfig.Storage("test"), | |
new AbstractPostgresConfig.Timeout(), | |
new AbstractPostgresConfig.Credentials("user", "pass") | |
); | |
postgresConfig.getAdditionalInitDbParams().addAll(DEFAULT_ADDITIONAL_INIT_DB_PARAMS); | |
return postgresConfig; | |
} | |
@Bean(destroyMethod = "stop") | |
public PostgresProcess postgresProcess(PostgresConfig config) throws IOException { | |
PostgresStarter<PostgresExecutable, PostgresProcess> runtime = PostgresStarter.getDefaultInstance(); | |
PostgresExecutable exec = runtime.prepare(config); | |
PostgresProcess process = exec.start(); | |
return process; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment