|
23 | 23 |
|
24 | 24 | import javax.sql.DataSource; |
25 | 25 |
|
26 | | -import org.junit.After; |
27 | 26 | import org.junit.Test; |
28 | 27 |
|
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
29 | 29 | import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; |
30 | | -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
31 | 30 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
32 | | -import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; |
33 | 31 | import org.springframework.boot.autoconfigure.orm.jpa.test.City; |
34 | | -import org.springframework.boot.test.util.TestPropertyValues; |
35 | | -import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 32 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
36 | 33 | import org.springframework.context.annotation.Bean; |
37 | 34 | import org.springframework.context.annotation.Configuration; |
38 | 35 | import org.springframework.orm.jpa.vendor.Database; |
|
44 | 41 | import static org.mockito.Mockito.mock; |
45 | 42 |
|
46 | 43 | /** |
47 | | - * Tests for {@link HibernateJpaAutoConfiguration}. |
| 44 | + * Additional tests for {@link HibernateJpaAutoConfiguration}. |
48 | 45 | * |
49 | 46 | * @author Dave Syer |
50 | 47 | * @author Phillip Webb |
51 | 48 | * @author Eddú Meléndez |
| 49 | + * @author Stephane Nicoll |
52 | 50 | */ |
53 | 51 | public class CustomHibernateJpaAutoConfigurationTests { |
54 | 52 |
|
55 | | - protected AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 53 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 54 | + .withUserConfiguration(TestConfiguration.class) |
| 55 | + .withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class, |
| 56 | + HibernateJpaAutoConfiguration.class)); |
56 | 57 |
|
57 | | - @After |
58 | | - public void close() { |
59 | | - this.context.close(); |
60 | | - } |
61 | 58 |
|
62 | 59 | @Test |
63 | | - public void testNamingStrategyDelegatorTakesPrecedence() { |
64 | | - TestPropertyValues |
65 | | - .of("spring.jpa.properties.hibernate.ejb.naming_strategy_delegator:" |
66 | | - + "org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator"); |
67 | | - this.context.register(TestConfiguration.class, |
68 | | - EmbeddedDataSourceConfiguration.class, |
69 | | - PropertyPlaceholderAutoConfiguration.class, |
70 | | - HibernateJpaAutoConfiguration.class); |
71 | | - this.context.refresh(); |
72 | | - JpaProperties bean = this.context.getBean(JpaProperties.class); |
73 | | - Map<String, String> hibernateProperties = bean |
74 | | - .getHibernateProperties("create-drop"); |
75 | | - assertThat(hibernateProperties.get("hibernate.ejb.naming_strategy")).isNull(); |
| 60 | + public void namingStrategyDelegatorTakesPrecedence() { |
| 61 | + this.contextRunner |
| 62 | + .withPropertyValues( |
| 63 | + "spring.jpa.properties.hibernate.ejb.naming_strategy_delegator:" |
| 64 | + + "org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator" |
| 65 | + ).run((context) -> { |
| 66 | + JpaProperties bean = context.getBean(JpaProperties.class); |
| 67 | + Map<String, String> hibernateProperties = bean |
| 68 | + .getHibernateProperties("create-drop"); |
| 69 | + assertThat(hibernateProperties.get("hibernate.ejb.naming_strategy")).isNull(); |
| 70 | + }); |
76 | 71 | } |
77 | 72 |
|
78 | 73 | @Test |
79 | | - public void testDefaultDatabaseForH2() throws Exception { |
80 | | - TestPropertyValues.of("spring.datasource.url:jdbc:h2:mem:testdb", |
81 | | - "spring.datasource.initialize:false").applyTo(this.context); |
82 | | - this.context.register(TestConfiguration.class, DataSourceAutoConfiguration.class, |
83 | | - PropertyPlaceholderAutoConfiguration.class, |
84 | | - HibernateJpaAutoConfiguration.class); |
85 | | - this.context.refresh(); |
86 | | - HibernateJpaVendorAdapter bean = this.context |
87 | | - .getBean(HibernateJpaVendorAdapter.class); |
88 | | - Database database = (Database) ReflectionTestUtils.getField(bean, "database"); |
89 | | - assertThat(database).isEqualTo(Database.H2); |
| 74 | + public void defaultDatabaseForH2() { |
| 75 | + this.contextRunner.withPropertyValues( |
| 76 | + "spring.datasource.url:jdbc:h2:mem:testdb", |
| 77 | + "spring.datasource.initialize:false").run((context) -> { |
| 78 | + HibernateJpaVendorAdapter bean = context |
| 79 | + .getBean(HibernateJpaVendorAdapter.class); |
| 80 | + Database database = (Database) ReflectionTestUtils.getField(bean, "database"); |
| 81 | + assertThat(database).isEqualTo(Database.H2); |
| 82 | + }); |
90 | 83 | } |
91 | 84 |
|
92 | 85 | @Configuration |
|
0 commit comments