|
1 | 1 | package org.seasar.doma.boot.autoconfigure; |
2 | 2 |
|
| 3 | +import static org.hamcrest.CoreMatchers.containsString; |
3 | 4 | import static org.hamcrest.CoreMatchers.instanceOf; |
4 | 5 | import static org.hamcrest.CoreMatchers.is; |
5 | 6 | import static org.hamcrest.CoreMatchers.notNullValue; |
6 | 7 | import static org.hamcrest.MatcherAssert.assertThat; |
7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; |
8 | 9 | import static org.junit.jupiter.api.Assertions.assertFalse; |
9 | 10 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
10 | 12 | import static org.junit.jupiter.api.Assertions.assertTrue; |
11 | 13 | import static org.mockito.ArgumentMatchers.anyString; |
12 | 14 | import static org.mockito.Mockito.mock; |
|
49 | 51 | import org.seasar.doma.jdbc.criteria.NativeSql; |
50 | 52 | import org.seasar.doma.jdbc.criteria.QueryDsl; |
51 | 53 | import org.seasar.doma.jdbc.dialect.Dialect; |
52 | | -import org.seasar.doma.jdbc.dialect.H2Dialect; |
53 | 54 | import org.seasar.doma.jdbc.dialect.MysqlDialect; |
54 | 55 | import org.seasar.doma.jdbc.dialect.PostgresDialect; |
55 | 56 | import org.seasar.doma.jdbc.dialect.StandardDialect; |
56 | 57 | import org.seasar.doma.jdbc.statistic.DefaultStatisticManager; |
57 | 58 | import org.seasar.doma.jdbc.statistic.StatisticManager; |
58 | 59 | import org.seasar.doma.message.Message; |
| 60 | +import org.springframework.beans.factory.BeanCreationException; |
59 | 61 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
60 | 62 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| 63 | +import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; |
61 | 64 | import org.springframework.context.ConfigurableApplicationContext; |
62 | 65 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
63 | 66 | import org.springframework.context.annotation.Bean; |
|
69 | 72 | import org.springframework.dao.DataIntegrityViolationException; |
70 | 73 | import org.springframework.dao.QueryTimeoutException; |
71 | 74 | import org.springframework.dao.support.PersistenceExceptionTranslator; |
| 75 | +import org.springframework.jdbc.datasource.SimpleDriverDataSource; |
72 | 76 | import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; |
73 | 77 |
|
74 | 78 | public class DomaAutoConfigurationTest { |
@@ -263,11 +267,71 @@ public void testDialectByDataSourceUrl() { |
263 | 267 | MutablePropertySources sources = context.getEnvironment() |
264 | 268 | .getPropertySources(); |
265 | 269 | sources.addFirst(new MapPropertySource("test", |
266 | | - Collections.singletonMap("spring.datasource.url", "jdbc:h2:mem:example"))); |
| 270 | + Map.of("spring.datasource.url", "jdbc:postgresql://localhost:1234/example", |
| 271 | + "doma.exception-translation-enabled", |
| 272 | + "false" /* prevent database connections */))); |
267 | 273 | this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class); |
268 | 274 | this.context.refresh(); |
269 | 275 | Dialect dialect = this.context.getBean(Dialect.class); |
270 | | - assertThat(dialect, is(instanceOf(H2Dialect.class))); |
| 276 | + assertThat(dialect, is(instanceOf(PostgresDialect.class))); |
| 277 | + } |
| 278 | + |
| 279 | + @Test |
| 280 | + public void testDialectByJdbConnectionDetails() { |
| 281 | + MutablePropertySources sources = context.getEnvironment() |
| 282 | + .getPropertySources(); |
| 283 | + sources.addFirst(new MapPropertySource("test", |
| 284 | + Map.of("doma.exception-translation-enabled", |
| 285 | + "false"/* prevent database connections */))); |
| 286 | + this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class); |
| 287 | + this.context.registerBean(JdbcConnectionDetails.class, () -> new JdbcConnectionDetails() { |
| 288 | + @Override |
| 289 | + public String getUsername() { |
| 290 | + return "dummy"; |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public String getPassword() { |
| 295 | + return "dummy"; |
| 296 | + } |
| 297 | + |
| 298 | + @Override |
| 299 | + public String getJdbcUrl() { |
| 300 | + return "jdbc:postgresql://localhost:1234/example"; |
| 301 | + } |
| 302 | + }); |
| 303 | + this.context.refresh(); |
| 304 | + Dialect dialect = this.context.getBean(Dialect.class); |
| 305 | + assertThat(dialect, is(instanceOf(PostgresDialect.class))); |
| 306 | + } |
| 307 | + |
| 308 | + @Test |
| 309 | + public void testDialectMissingJdbConnectionDetails() { |
| 310 | + MutablePropertySources sources = context.getEnvironment() |
| 311 | + .getPropertySources(); |
| 312 | + sources.addFirst(new MapPropertySource("test", |
| 313 | + Map.of("doma.exception-translation-enabled", |
| 314 | + "false"/* prevent database connections */))); |
| 315 | + this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class); |
| 316 | + this.context.registerBean(DataSource.class, SimpleDriverDataSource::new); |
| 317 | + BeanCreationException exception = assertThrows(BeanCreationException.class, |
| 318 | + () -> this.context.refresh()); |
| 319 | + assertThat(exception.getMessage(), containsString( |
| 320 | + "No connection details available. You will probably have to set 'doma.dialect' explicitly.")); |
| 321 | + } |
| 322 | + |
| 323 | + @Test |
| 324 | + public void testDialectMissingJdbConnectionDetailsExplicitDialect() { |
| 325 | + MutablePropertySources sources = context.getEnvironment() |
| 326 | + .getPropertySources(); |
| 327 | + sources.addFirst(new MapPropertySource("test", |
| 328 | + Map.of("doma.dialect", "POSTGRES", "doma.exception-translation-enabled", |
| 329 | + "false"/* prevent database connections */))); |
| 330 | + this.context.register(DomaAutoConfiguration.class, DataSourceAutoConfiguration.class); |
| 331 | + this.context.registerBean(DataSource.class, SimpleDriverDataSource::new); |
| 332 | + this.context.refresh(); |
| 333 | + Dialect dialect = this.context.getBean(Dialect.class); |
| 334 | + assertThat(dialect, is(instanceOf(PostgresDialect.class))); |
271 | 335 | } |
272 | 336 |
|
273 | 337 | @Test |
|
0 commit comments