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 { public class JooqProperties {
/** /**
* SQLDialect JOOQ used when communicating with the configured datasource, for * Sql dialect to use, auto-detected by default.
* instance "POSTGRES".
*/ */
private SQLDialect sqlDialect; private SQLDialect sqlDialect;

View File

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