pull/10700/head
Stephane Nicoll 7 years ago
parent ccca943e33
commit c4026806a6

@ -23,16 +23,13 @@ import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.test.City; import org.springframework.boot.autoconfigure.orm.jpa.test.City;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.vendor.Database; import org.springframework.orm.jpa.vendor.Database;
@ -44,49 +41,45 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link HibernateJpaAutoConfiguration}. * Additional tests for {@link HibernateJpaAutoConfiguration}.
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
*/ */
public class CustomHibernateJpaAutoConfigurationTests { public class CustomHibernateJpaAutoConfigurationTests {
protected AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(TestConfiguration.class)
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class));
@After
public void close() {
this.context.close();
}
@Test @Test
public void testNamingStrategyDelegatorTakesPrecedence() { public void namingStrategyDelegatorTakesPrecedence() {
TestPropertyValues this.contextRunner
.of("spring.jpa.properties.hibernate.ejb.naming_strategy_delegator:" .withPropertyValues(
+ "org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator"); "spring.jpa.properties.hibernate.ejb.naming_strategy_delegator:"
this.context.register(TestConfiguration.class, + "org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator"
EmbeddedDataSourceConfiguration.class, ).run((context) -> {
PropertyPlaceholderAutoConfiguration.class, JpaProperties bean = context.getBean(JpaProperties.class);
HibernateJpaAutoConfiguration.class); Map<String, String> hibernateProperties = bean
this.context.refresh(); .getHibernateProperties("create-drop");
JpaProperties bean = this.context.getBean(JpaProperties.class); assertThat(hibernateProperties.get("hibernate.ejb.naming_strategy")).isNull();
Map<String, String> hibernateProperties = bean });
.getHibernateProperties("create-drop");
assertThat(hibernateProperties.get("hibernate.ejb.naming_strategy")).isNull();
} }
@Test @Test
public void testDefaultDatabaseForH2() throws Exception { public void defaultDatabaseForH2() {
TestPropertyValues.of("spring.datasource.url:jdbc:h2:mem:testdb", this.contextRunner.withPropertyValues(
"spring.datasource.initialize:false").applyTo(this.context); "spring.datasource.url:jdbc:h2:mem:testdb",
this.context.register(TestConfiguration.class, DataSourceAutoConfiguration.class, "spring.datasource.initialize:false").run((context) -> {
PropertyPlaceholderAutoConfiguration.class, HibernateJpaVendorAdapter bean = context
HibernateJpaAutoConfiguration.class); .getBean(HibernateJpaVendorAdapter.class);
this.context.refresh(); Database database = (Database) ReflectionTestUtils.getField(bean, "database");
HibernateJpaVendorAdapter bean = this.context assertThat(database).isEqualTo(Database.H2);
.getBean(HibernateJpaVendorAdapter.class); });
Database database = (Database) ReflectionTestUtils.getField(bean, "database");
assertThat(database).isEqualTo(Database.H2);
} }
@Configuration @Configuration

Loading…
Cancel
Save