Polish jOOQ auto-configuration

This commit is contained in:
Stephane Nicoll 2017-05-31 11:25:26 +02:00
parent efdf451e6e
commit 026682d7e3
3 changed files with 25 additions and 29 deletions

View File

@ -33,8 +33,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
public class JooqProperties {
/**
* SQLDialect JOOQ used when communicating with the configured datasource, for
* instance "POSTGRES".
* Sql dialect to use, auto-detected by default.
*/
private SQLDialect sqlDialect;

View File

@ -32,12 +32,10 @@ import org.jooq.TransactionalRunnable;
import org.jooq.VisitListener;
import org.jooq.VisitListenerProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -46,6 +44,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
@ -56,6 +55,7 @@ import static org.junit.Assert.fail;
* @author Andreas Ahlenstorf
* @author Phillip Webb
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
public class JooqAutoConfigurationTests {
@ -64,12 +64,7 @@ public class JooqAutoConfigurationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Before
public void init() {
TestPropertyValues.of("spring.datasource.name:jooqtest").applyTo(this.context);
}
private AnnotationConfigApplicationContext context;
@After
public void close() {
@ -80,16 +75,14 @@ public class JooqAutoConfigurationTests {
@Test
public void noDataSource() throws Exception {
registerAndRefresh(JooqAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
load();
assertThat(this.context.getBeanNamesForType(DSLContext.class).length)
.isEqualTo(0);
}
@Test
public void jooqWithoutTx() throws Exception {
registerAndRefresh(JooqDataSourceConfiguration.class, JooqAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
load(JooqDataSourceConfiguration.class);
assertThat(getBeanNames(PlatformTransactionManager.class)).isEqualTo(NO_BEANS);
assertThat(getBeanNames(SpringTransactionProvider.class)).isEqualTo(NO_BEANS);
DSLContext dsl = this.context.getBean(DSLContext.class);
@ -115,9 +108,7 @@ public class JooqAutoConfigurationTests {
@Test
public void jooqWithTx() throws Exception {
registerAndRefresh(JooqDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, TxManagerConfiguration.class,
JooqAutoConfiguration.class);
load(JooqDataSourceConfiguration.class, TxManagerConfiguration.class);
this.context.getBean(PlatformTransactionManager.class);
DSLContext dsl = this.context.getBean(DSLContext.class);
assertThat(dsl.configuration().dialect()).isEqualTo(SQLDialect.HSQLDB);
@ -143,11 +134,9 @@ public class JooqAutoConfigurationTests {
@Test
public void customProvidersArePickedUp() {
registerAndRefresh(JooqDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, TxManagerConfiguration.class,
load(JooqDataSourceConfiguration.class, TxManagerConfiguration.class,
TestRecordMapperProvider.class, TestRecordListenerProvider.class,
TestExecuteListenerProvider.class, TestVisitListenerProvider.class,
JooqAutoConfiguration.class);
TestExecuteListenerProvider.class, TestVisitListenerProvider.class);
DSLContext dsl = this.context.getBean(DSLContext.class);
assertThat(dsl.configuration().recordMapperProvider().getClass())
.isEqualTo(TestRecordMapperProvider.class);
@ -158,17 +147,25 @@ public class JooqAutoConfigurationTests {
@Test
public void relaxedBindingOfSqlDialect() {
TestPropertyValues.of(
"spring.jooq.sql-dialect:PoSTGrES").applyTo(this.context);
registerAndRefresh(JooqDataSourceConfiguration.class,
JooqAutoConfiguration.class);
load(new Class<?>[] { JooqDataSourceConfiguration.class }, "spring.jooq.sql-dialect:PoSTGrES");
assertThat(this.context.getBean(org.jooq.Configuration.class).dialect())
.isEqualTo(SQLDialect.POSTGRES);
}
private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses);
this.context.refresh();
private void load(Class<?>... configs) {
load(configs, new String[0]);
}
private void load(Class<?>[] configs, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.datasource.name:jooqtest").applyTo(ctx);
TestPropertyValues.of(environment).applyTo(ctx);
if (!ObjectUtils.isEmpty(configs)) {
ctx.register(configs);
}
ctx.register(JooqAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
private String[] getBeanNames(Class<?> type) {

View File

@ -708,7 +708,7 @@ content into your application; rather pick only the properties that you need.
spring.h2.console.settings.web-allow-others=false # Enable remote access.
# JOOQ ({sc-spring-boot-autoconfigure}/jooq/JooqAutoConfiguration.{sc-ext}[JooqAutoConfiguration])
spring.jooq.sql-dialect= # SQLDialect JOOQ used when communicating with the configured datasource. For instance `POSTGRES`
spring.jooq.sql-dialect= # Sql dialect to use, auto-detected by default.
# JPA ({sc-spring-boot-autoconfigure}/orm/jpa/JpaBaseConfiguration.{sc-ext}[JpaBaseConfiguration], {sc-spring-boot-autoconfigure}/orm/jpa/HibernateJpaAutoConfiguration.{sc-ext}[HibernateJpaAutoConfiguration])
spring.data.jpa.repositories.enabled=true # Enable JPA repositories.