@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2012 - 201 7 the original author or authors .
* Copyright 2012 - 201 8 the original author or authors .
*
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -18,23 +18,25 @@ package org.springframework.boot.autoconfigure.data.jpa;
import javax.persistence.EntityManagerFactory ;
import javax.persistence.EntityManagerFactory ;
import org.junit.After ;
import org.junit.Test ;
import org.junit.Test ;
import org.springframework.b eans.factory.NoSuchBeanDefinitionException ;
import org.springframework.b oot.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.context.PropertyPlaceholderAutoConfiguration ;
import org.springframework.boot.autoconfigure.data.alt.jpa.CityJpaRepository ;
import org.springframework.boot.autoconfigure.data.alt.mongo.CityMongoDbRepository ;
import org.springframework.boot.autoconfigure.data.alt.mongo.CityMongoDbRepository ;
import org.springframework.boot.autoconfigure.data.alt.solr.CitySolrRepository ;
import org.springframework.boot.autoconfigure.data.alt.solr.CitySolrRepository ;
import org.springframework.boot.autoconfigure.data.jpa.city.City ;
import org.springframework.boot.autoconfigure.data.jpa.city.City ;
import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository ;
import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository ;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration ;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration ;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration ;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration ;
import org.springframework.boot.test.context.runner.ApplicationContextRunner ;
import org.springframework.context.annotation.ComponentScan.Filter ;
import org.springframework.context.annotation.ComponentScan.Filter ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.FilterType ;
import org.springframework.context.annotation.FilterType ;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories ;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories ;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ;
import org.springframework.transaction.PlatformTransactionManager ;
import org.springframework.transaction.PlatformTransactionManager ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
@ -47,47 +49,69 @@ import static org.assertj.core.api.Assertions.assertThat;
* /
* /
public class JpaRepositoriesAutoConfigurationTests {
public class JpaRepositoriesAutoConfigurationTests {
private AnnotationConfigApplicationContext context ;
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ( )
. withConfiguration ( AutoConfigurations . of ( HibernateJpaAutoConfiguration . class ,
@After
JpaRepositoriesAutoConfiguration . class ,
public void close ( ) {
PropertyPlaceholderAutoConfiguration . class ,
this . context . close ( ) ;
TaskExecutionAutoConfiguration . class ) )
}
. withUserConfiguration ( EmbeddedDataSourceConfiguration . class ) ;
@Test
@Test
public void testDefaultRepositoryConfiguration ( ) {
public void testDefaultRepositoryConfiguration ( ) {
prepareApplicationContext ( TestConfiguration . class ) ;
this . contextRunner . withUserConfiguration ( TestConfiguration . class )
. run ( ( context ) - > {
assertThat ( this . context . getBean ( CityRepository . class ) ) . isNotNull ( ) ;
assertThat ( context ) . hasSingleBean ( CityRepository . class ) ;
assertThat ( this . context . getBean ( PlatformTransactionManager . class ) ) . isNotNull ( ) ;
assertThat ( context ) . hasSingleBean ( PlatformTransactionManager . class ) ;
assertThat ( this . context . getBean ( EntityManagerFactory . class ) ) . isNotNull ( ) ;
assertThat ( context ) . hasSingleBean ( EntityManagerFactory . class ) ;
assertThat (
context . getBean ( LocalContainerEntityManagerFactoryBean . class )
. getBootstrapExecutor ( ) ) . isNull ( ) ;
} ) ;
}
}
@Test
@Test
public void testOverrideRepositoryConfiguration ( ) {
public void testOverrideRepositoryConfiguration ( ) {
prepareApplicationContext ( CustomConfiguration . class ) ;
this . contextRunner . withUserConfiguration ( CustomConfiguration . class )
assertThat ( this . context . getBean (
. run ( ( context ) - > {
org . springframework . boot . autoconfigure . data . alt . jpa . CityJpaRepository . class ) )
assertThat ( context ) . hasSingleBean ( CityJpaRepository . class ) ;
. isNotNull ( ) ;
assertThat ( context ) . hasSingleBean ( PlatformTransactionManager . class ) ;
assertThat ( this . context . getBean ( PlatformTransactionManager . class ) ) . isNotNull ( ) ;
assertThat ( context ) . hasSingleBean ( EntityManagerFactory . class ) ;
assertThat ( this . context . getBean ( EntityManagerFactory . class ) ) . isNotNull ( ) ;
} ) ;
}
}
@Test ( expected = NoSuchBeanDefinitionException . class )
@Test
public void autoConfigurationShouldNotKickInEvenIfManualConfigDidNotCreateAnyRepositories ( ) {
public void autoConfigurationShouldNotKickInEvenIfManualConfigDidNotCreateAnyRepositories ( ) {
prepareApplicationContext ( SortOfInvalidCustomConfiguration . class ) ;
this . contextRunner . withUserConfiguration ( SortOfInvalidCustomConfiguration . class )
. run ( ( context ) - > assertThat ( context )
. doesNotHaveBean ( CityRepository . class ) ) ;
}
this . context . getBean ( CityRepository . class ) ;
@Test
public void whenBootstrappingModeIsLazyBoostrapExecutorIsConfigured ( ) {
this . contextRunner . withUserConfiguration ( TestConfiguration . class )
. withPropertyValues ( "spring.data.jpa.repositories.bootstrap-mode=lazy" )
. run ( ( context ) - > assertThat (
context . getBean ( LocalContainerEntityManagerFactoryBean . class )
. getBootstrapExecutor ( ) ) . isNotNull ( ) ) ;
}
}
private void prepareApplicationContext ( Class < ? > . . . configurationClasses ) {
@Test
this . context = new AnnotationConfigApplicationContext ( ) ;
public void whenBootstrappingModeIsDeferredBoostrapExecutorIsConfigured ( ) {
this . context . register ( configurationClasses ) ;
this . contextRunner . withUserConfiguration ( TestConfiguration . class )
this . context . register ( EmbeddedDataSourceConfiguration . class ,
. withPropertyValues (
HibernateJpaAutoConfiguration . class ,
"spring.data.jpa.repositories.bootstrap-mode=deferred" )
JpaRepositoriesAutoConfiguration . class ,
. run ( ( context ) - > assertThat (
PropertyPlaceholderAutoConfiguration . class ) ;
context . getBean ( LocalContainerEntityManagerFactoryBean . class )
this . context . refresh ( ) ;
. getBootstrapExecutor ( ) ) . isNotNull ( ) ) ;
}
@Test
public void whenBootstrappingModeIsDefaultBoostrapExecutorIsNotConfigured ( ) {
this . contextRunner . withUserConfiguration ( TestConfiguration . class )
. withPropertyValues ( "spring.data.jpa.repositories.bootstrap-mode=default" )
. run ( ( context ) - > assertThat (
context . getBean ( LocalContainerEntityManagerFactoryBean . class )
. getBootstrapExecutor ( ) ) . isNull ( ) ) ;
}
}
@Configuration
@Configuration