Skip to content

Commit 3b704f4

Browse files
authored
Merge branch 'master' into slf4j-logger
2 parents a6f1666 + a8c778c commit 3b704f4

File tree

3 files changed

+102
-30
lines changed

3 files changed

+102
-30
lines changed

doma-spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
<artifactId>h2</artifactId>
6161
<scope>test</scope>
6262
</dependency>
63+
<dependency>
64+
<groupId>org.postgresql</groupId>
65+
<artifactId>postgresql</artifactId>
66+
<scope>test</scope>
67+
</dependency>
6368
<dependency>
6469
<groupId>com.zaxxer</groupId>
6570
<artifactId>HikariCP</artifactId>

doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaAutoConfiguration.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.seasar.doma.jdbc.dialect.StandardDialect;
4040
import org.seasar.doma.jdbc.statistic.DefaultStatisticManager;
4141
import org.seasar.doma.jdbc.statistic.StatisticManager;
42+
import org.springframework.beans.factory.BeanCreationException;
4243
import org.springframework.beans.factory.ObjectProvider;
4344
import org.springframework.beans.factory.annotation.Autowired;
4445
import org.springframework.beans.factory.annotation.Qualifier;
@@ -47,11 +48,11 @@
4748
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4849
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4950
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
51+
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
5052
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5153
import org.springframework.boot.jdbc.DatabaseDriver;
5254
import org.springframework.context.annotation.Bean;
5355
import org.springframework.context.annotation.Configuration;
54-
import org.springframework.core.env.Environment;
5556
import org.springframework.core.io.ResourceLoader;
5657
import org.springframework.dao.support.PersistenceExceptionTranslator;
5758
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
@@ -76,39 +77,41 @@ public class DomaAutoConfiguration {
7677

7778
@Bean
7879
@ConditionalOnMissingBean
79-
public Dialect dialect(Environment environment) {
80+
public Dialect dialect(ObjectProvider<JdbcConnectionDetails> connectionDetailsProvider) {
8081
DialectType dialectType = domaProperties.getDialect();
8182
if (dialectType != null) {
8283
return dialectType.create();
8384
}
84-
String url = environment.getProperty("spring.datasource.url");
85-
if (url != null) {
86-
DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(url);
87-
switch (databaseDriver) {
88-
case DB2:
89-
return new Db2Dialect();
90-
case H2:
91-
return new H2Dialect();
92-
case HSQLDB:
93-
return new HsqldbDialect();
94-
case SQLSERVER:
95-
case JTDS:
96-
return new MssqlDialect();
97-
case MYSQL:
98-
return new MysqlDialect();
99-
case ORACLE:
100-
return new OracleDialect();
101-
case POSTGRESQL:
102-
return new PostgresDialect();
103-
case SQLITE:
104-
return new SqliteDialect();
105-
default:
106-
break;
107-
}
85+
JdbcConnectionDetails connectionDetails = connectionDetailsProvider.getIfAvailable();
86+
if (connectionDetails == null) {
87+
throw new BeanCreationException(
88+
"No connection details available. You will probably have to set 'doma.dialect' explicitly.");
89+
}
90+
DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(connectionDetails.getJdbcUrl());
91+
switch (databaseDriver) {
92+
case DB2:
93+
return new Db2Dialect();
94+
case H2:
95+
return new H2Dialect();
96+
case HSQLDB:
97+
return new HsqldbDialect();
98+
case SQLSERVER:
99+
case JTDS:
100+
return new MssqlDialect();
101+
case MYSQL:
102+
return new MysqlDialect();
103+
case ORACLE:
104+
return new OracleDialect();
105+
case POSTGRESQL:
106+
return new PostgresDialect();
107+
case SQLITE:
108+
return new SqliteDialect();
109+
default:
110+
break;
108111
}
109112
if (logger.isWarnEnabled()) {
110113
logger.warn(
111-
"StandardDialect was selected because no explicit configuration and it is not possible to guess from 'spring.datasource.url property'");
114+
"StandardDialect was selected because no explicit configuration and it is not possible to guess from the connection details.");
112115
}
113116
return new StandardDialect();
114117
}

doma-spring-boot-autoconfigure/src/test/java/org/seasar/doma/boot/autoconfigure/DomaAutoConfigurationTest.java

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.seasar.doma.boot.autoconfigure;
22

3+
import static org.hamcrest.CoreMatchers.containsString;
34
import static org.hamcrest.CoreMatchers.instanceOf;
45
import static org.hamcrest.CoreMatchers.is;
56
import static org.hamcrest.CoreMatchers.notNullValue;
67
import static org.hamcrest.MatcherAssert.assertThat;
78
import static org.junit.jupiter.api.Assertions.assertEquals;
89
import static org.junit.jupiter.api.Assertions.assertFalse;
910
import static org.junit.jupiter.api.Assertions.assertNotNull;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
1012
import static org.junit.jupiter.api.Assertions.assertTrue;
1113
import static org.mockito.ArgumentMatchers.anyString;
1214
import static org.mockito.Mockito.mock;
@@ -49,16 +51,17 @@
4951
import org.seasar.doma.jdbc.criteria.NativeSql;
5052
import org.seasar.doma.jdbc.criteria.QueryDsl;
5153
import org.seasar.doma.jdbc.dialect.Dialect;
52-
import org.seasar.doma.jdbc.dialect.H2Dialect;
5354
import org.seasar.doma.jdbc.dialect.MysqlDialect;
5455
import org.seasar.doma.jdbc.dialect.PostgresDialect;
5556
import org.seasar.doma.jdbc.dialect.StandardDialect;
5657
import org.seasar.doma.jdbc.statistic.DefaultStatisticManager;
5758
import org.seasar.doma.jdbc.statistic.StatisticManager;
5859
import org.seasar.doma.message.Message;
5960
import org.seasar.doma.slf4j.Slf4jJdbcLogger;
61+
import org.springframework.beans.factory.BeanCreationException;
6062
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
6163
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
64+
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
6265
import org.springframework.context.ConfigurableApplicationContext;
6366
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6467
import org.springframework.context.annotation.Bean;
@@ -70,6 +73,7 @@
7073
import org.springframework.dao.DataIntegrityViolationException;
7174
import org.springframework.dao.QueryTimeoutException;
7275
import org.springframework.dao.support.PersistenceExceptionTranslator;
76+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
7377
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
7478

7579
public class DomaAutoConfigurationTest {
@@ -264,11 +268,71 @@ public void testDialectByDataSourceUrl() {
264268
MutablePropertySources sources = context.getEnvironment()
265269
.getPropertySources();
266270
sources.addFirst(new MapPropertySource("test",
267-
Collections.singletonMap("spring.datasource.url", "jdbc:h2:mem:example")));
271+
Map.of("spring.datasource.url", "jdbc:postgresql://localhost:1234/example",
272+
"doma.exception-translation-enabled",
273+
"false" /* prevent database connections */)));
268274
this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class);
269275
this.context.refresh();
270276
Dialect dialect = this.context.getBean(Dialect.class);
271-
assertThat(dialect, is(instanceOf(H2Dialect.class)));
277+
assertThat(dialect, is(instanceOf(PostgresDialect.class)));
278+
}
279+
280+
@Test
281+
public void testDialectByJdbConnectionDetails() {
282+
MutablePropertySources sources = context.getEnvironment()
283+
.getPropertySources();
284+
sources.addFirst(new MapPropertySource("test",
285+
Map.of("doma.exception-translation-enabled",
286+
"false"/* prevent database connections */)));
287+
this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class);
288+
this.context.registerBean(JdbcConnectionDetails.class, () -> new JdbcConnectionDetails() {
289+
@Override
290+
public String getUsername() {
291+
return "dummy";
292+
}
293+
294+
@Override
295+
public String getPassword() {
296+
return "dummy";
297+
}
298+
299+
@Override
300+
public String getJdbcUrl() {
301+
return "jdbc:postgresql://localhost:1234/example";
302+
}
303+
});
304+
this.context.refresh();
305+
Dialect dialect = this.context.getBean(Dialect.class);
306+
assertThat(dialect, is(instanceOf(PostgresDialect.class)));
307+
}
308+
309+
@Test
310+
public void testDialectMissingJdbConnectionDetails() {
311+
MutablePropertySources sources = context.getEnvironment()
312+
.getPropertySources();
313+
sources.addFirst(new MapPropertySource("test",
314+
Map.of("doma.exception-translation-enabled",
315+
"false"/* prevent database connections */)));
316+
this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class);
317+
this.context.registerBean(DataSource.class, SimpleDriverDataSource::new);
318+
BeanCreationException exception = assertThrows(BeanCreationException.class,
319+
() -> this.context.refresh());
320+
assertThat(exception.getMessage(), containsString(
321+
"No connection details available. You will probably have to set 'doma.dialect' explicitly."));
322+
}
323+
324+
@Test
325+
public void testDialectMissingJdbConnectionDetailsExplicitDialect() {
326+
MutablePropertySources sources = context.getEnvironment()
327+
.getPropertySources();
328+
sources.addFirst(new MapPropertySource("test",
329+
Map.of("doma.dialect", "POSTGRES", "doma.exception-translation-enabled",
330+
"false"/* prevent database connections */)));
331+
this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class);
332+
this.context.registerBean(DataSource.class, SimpleDriverDataSource::new);
333+
this.context.refresh();
334+
Dialect dialect = this.context.getBean(Dialect.class);
335+
assertThat(dialect, is(instanceOf(PostgresDialect.class)));
272336
}
273337

274338
@Test

0 commit comments

Comments
 (0)