@ -51,6 +51,7 @@ import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter ;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter ;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor ;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor ;
import org.springframework.transaction.PlatformTransactionManager ;
import org.springframework.transaction.PlatformTransactionManager ;
import org.springframework.util.ObjectUtils ;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext ;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
@ -60,44 +61,46 @@ import static org.assertj.core.api.Assertions.assertThat;
*
*
* @author Phillip Webb
* @author Phillip Webb
* @author Dave Syer
* @author Dave Syer
* @author Stephane Nicoll
* /
* /
public abstract class AbstractJpaAutoConfigurationTests {
public abstract class AbstractJpaAutoConfigurationTests {
@Rule
@Rule
public ExpectedException expected = ExpectedException . none ( ) ;
public ExpectedException expected = ExpectedException . none ( ) ;
protected AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
protected AnnotationConfigApplicationContext context ;
@After
@After
public void close ( ) {
public void close ( ) {
this . context . close ( ) ;
if ( this . context ! = null ) {
this . context . close ( ) ;
}
}
}
protected abstract Class < ? > getAutoConfigureClass ( ) ;
protected abstract Class < ? > getAutoConfigureClass ( ) ;
@Test
@Test
public void testNoDataSource ( ) throws Exception {
public void testNoDataSource ( ) throws Exception {
this . context . register ( PropertyPlaceholderAutoConfiguration . class ,
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( PropertyPlaceholderAutoConfiguration . class ,
getAutoConfigureClass ( ) ) ;
getAutoConfigureClass ( ) ) ;
this . expected . expect ( BeanCreationException . class ) ;
this . expected . expect ( BeanCreationException . class ) ;
this . expected . expectMessage ( "No qualifying bean" ) ;
this . expected . expectMessage ( "No qualifying bean" ) ;
this . expected . expectMessage ( "DataSource" ) ;
this . expected . expectMessage ( "DataSource" ) ;
this . context . refresh ( ) ;
ctx . refresh ( ) ;
}
}
@Test
@Test
public void testEntityManagerCreated ( ) throws Exception {
public void testEntityManagerCreated ( ) throws Exception {
setupTestConfiguration ( ) ;
load ( ) ;
this . context . refresh ( ) ;
assertThat ( this . context . getBean ( DataSource . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( DataSource . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( JpaTransactionManager . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( JpaTransactionManager . class ) ) . isNotNull ( ) ;
}
}
@Test
@Test
public void testDataSourceTransactionManagerNotCreated ( ) throws Exception {
public void testDataSourceTransactionManagerNotCreated ( ) throws Exception {
this . context . register ( DataSourceTransactionManagerAutoConfiguration . class ) ;
load ( new Class < ? > [ 0 ] ,
setupTestConfiguration ( ) ;
new Class < ? > [ ] { DataSourceTransactionManagerAutoConfiguration . class } ) ;
this . context . refresh ( ) ;
assertThat ( this . context . getBean ( DataSource . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( DataSource . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( "transactionManager" ) )
assertThat ( this . context . getBean ( "transactionManager" ) )
. isInstanceOf ( JpaTransactionManager . class ) ;
. isInstanceOf ( JpaTransactionManager . class ) ;
@ -140,10 +143,8 @@ public abstract class AbstractJpaAutoConfigurationTests {
@Test
@Test
public void customJpaProperties ( ) throws Exception {
public void customJpaProperties ( ) throws Exception {
TestPropertyValues . of ( "spring.jpa.properties.a:b" , "spring.jpa.properties.a.b:c" ,
load ( "spring.jpa.properties.a:b" , "spring.jpa.properties.a.b:c" ,
"spring.jpa.properties.c:d" ) . applyTo ( this . context ) ;
"spring.jpa.properties.c:d" ) ;
setupTestConfiguration ( ) ;
this . context . refresh ( ) ;
LocalContainerEntityManagerFactoryBean bean = this . context
LocalContainerEntityManagerFactoryBean bean = this . context
. getBean ( LocalContainerEntityManagerFactoryBean . class ) ;
. getBean ( LocalContainerEntityManagerFactoryBean . class ) ;
Map < String , Object > map = bean . getJpaPropertyMap ( ) ;
Map < String , Object > map = bean . getJpaPropertyMap ( ) ;
@ -154,10 +155,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
@Test
@Test
public void usesManuallyDefinedLocalContainerEntityManagerFactoryBeanIfAvailable ( ) {
public void usesManuallyDefinedLocalContainerEntityManagerFactoryBeanIfAvailable ( ) {
TestPropertyValues . of ( "spring.datasource.initialize:false" ) ;
load ( TestConfigurationWithLocalContainerEntityManagerFactoryBean . class ) ;
setupTestConfiguration (
TestConfigurationWithLocalContainerEntityManagerFactoryBean . class ) ;
this . context . refresh ( ) ;
LocalContainerEntityManagerFactoryBean factoryBean = this . context
LocalContainerEntityManagerFactoryBean factoryBean = this . context
. getBean ( LocalContainerEntityManagerFactoryBean . class ) ;
. getBean ( LocalContainerEntityManagerFactoryBean . class ) ;
Map < String , Object > map = factoryBean . getJpaPropertyMap ( ) ;
Map < String , Object > map = factoryBean . getJpaPropertyMap ( ) ;
@ -166,9 +164,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
@Test
@Test
public void usesManuallyDefinedEntityManagerFactoryIfAvailable ( ) {
public void usesManuallyDefinedEntityManagerFactoryIfAvailable ( ) {
TestPropertyValues . of ( "spring.datasource.initialize:false" ) . applyTo ( this . context ) ;
load ( TestConfigurationWithLocalContainerEntityManagerFactoryBean . class ) ;
setupTestConfiguration ( TestConfigurationWithEntityManagerFactory . class ) ;
this . context . refresh ( ) ;
EntityManagerFactory factoryBean = this . context
EntityManagerFactory factoryBean = this . context
. getBean ( EntityManagerFactory . class ) ;
. getBean ( EntityManagerFactory . class ) ;
Map < String , Object > map = factoryBean . getProperties ( ) ;
Map < String , Object > map = factoryBean . getProperties ( ) ;
@ -177,8 +173,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
@Test
@Test
public void usesManuallyDefinedTransactionManagerBeanIfAvailable ( ) {
public void usesManuallyDefinedTransactionManagerBeanIfAvailable ( ) {
setupTestConfiguration ( TestConfigurationWithTransactionManager . class ) ;
load ( TestConfigurationWithTransactionManager . class ) ;
this . context . refresh ( ) ;
PlatformTransactionManager txManager = this . context
PlatformTransactionManager txManager = this . context
. getBean ( PlatformTransactionManager . class ) ;
. getBean ( PlatformTransactionManager . class ) ;
assertThat ( txManager ) . isInstanceOf ( CustomJpaTransactionManager . class ) ;
assertThat ( txManager ) . isInstanceOf ( CustomJpaTransactionManager . class ) ;
@ -186,8 +181,7 @@ public abstract class AbstractJpaAutoConfigurationTests {
@Test
@Test
public void customPersistenceUnitManager ( ) throws Exception {
public void customPersistenceUnitManager ( ) throws Exception {
setupTestConfiguration ( TestConfigurationWithCustomPersistenceUnitManager . class ) ;
load ( TestConfigurationWithCustomPersistenceUnitManager . class ) ;
this . context . refresh ( ) ;
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = this . context
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = this . context
. getBean ( LocalContainerEntityManagerFactoryBean . class ) ;
. getBean ( LocalContainerEntityManagerFactoryBean . class ) ;
Field field = LocalContainerEntityManagerFactoryBean . class
Field field = LocalContainerEntityManagerFactoryBean . class
@ -197,14 +191,32 @@ public abstract class AbstractJpaAutoConfigurationTests {
. isEqualTo ( this . context . getBean ( PersistenceUnitManager . class ) ) ;
. isEqualTo ( this . context . getBean ( PersistenceUnitManager . class ) ) ;
}
}
protected void setupTestConfiguration ( ) {
protected void load ( String . . . environment ) {
setupTestConfiguration ( TestConfiguration . class ) ;
load ( new Class < ? > [ 0 ] , new Class < ? > [ 0 ] , environment ) ;
}
protected void load ( Class < ? > config , String . . . environment ) {
Class < ? > [ ] configs = config ! = null ? new Class < ? > [ ] { config } : null ;
load ( configs , new Class < ? > [ 0 ] , environment ) ;
}
}
protected void setupTestConfiguration ( Class < ? > configClass ) {
protected void load ( Class < ? > [ ] configs , Class < ? > [ ] autoConfigs , String . . . environment ) {
this . context . register ( configClass , EmbeddedDataSourceConfiguration . class ,
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
TestPropertyValues . of ( environment )
. and ( "spring.datasource.generate-unique-name" , "true" )
. applyTo ( ctx ) ;
ctx . register ( TestConfiguration . class ) ;
if ( ! ObjectUtils . isEmpty ( configs ) ) {
ctx . register ( configs ) ;
}
ctx . register (
DataSourceAutoConfiguration . class , TransactionAutoConfiguration . class ,
DataSourceAutoConfiguration . class , TransactionAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class , getAutoConfigureClass ( ) ) ;
PropertyPlaceholderAutoConfiguration . class , getAutoConfigureClass ( ) ) ;
if ( ! ObjectUtils . isEmpty ( autoConfigs ) ) {
ctx . register ( autoConfigs ) ;
}
ctx . refresh ( ) ;
this . context = ctx ;
}
}
private String [ ] getInterceptorBeans ( ApplicationContext context ) {
private String [ ] getInterceptorBeans ( ApplicationContext context ) {