@ -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" ) ;
* 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 org.junit.After ;
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.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.solr.CitySolrRepository ;
import org.springframework.boot.autoconfigure.data.jpa.city.City ;
import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository ;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration ;
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.Configuration ;
import org.springframework.context.annotation.FilterType ;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories ;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ;
import org.springframework.transaction.PlatformTransactionManager ;
import static org.assertj.core.api.Assertions.assertThat ;
@ -47,47 +49,69 @@ import static org.assertj.core.api.Assertions.assertThat;
* /
public class JpaRepositoriesAutoConfigurationTests {
private AnnotationConfigApplicationContext context ;
@After
public void close ( ) {
this . context . close ( ) ;
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ( )
. withConfiguration ( AutoConfigurations . of ( HibernateJpaAutoConfiguration . class ,
JpaRepositoriesAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
TaskExecutionAutoConfiguration . class ) )
. withUserConfiguration ( EmbeddedDataSourceConfiguration . class ) ;
@Test
public void testDefaultRepositoryConfiguration ( ) {
prepareApplicationContext ( TestConfiguration . class ) ;
assertThat ( this . context . getBean ( CityRepository . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( PlatformTransactionManager . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( EntityManagerFactory . class ) ) . isNotNull ( ) ;
this . contextRunner . withUserConfiguration ( TestConfiguration . class )
. run ( ( context ) - > {
assertThat ( context ) . hasSingleBean ( CityRepository . class ) ;
assertThat ( context ) . hasSingleBean ( PlatformTransactionManager . class ) ;
assertThat ( context ) . hasSingleBean ( EntityManagerFactory . class ) ;
assertThat (
context . getBean ( LocalContainerEntityManagerFactoryBean . class )
. getBootstrapExecutor ( ) ) . isNull ( ) ;
} ) ;
}
@Test
public void testOverrideRepositoryConfiguration ( ) {
prepareApplicationContext ( CustomConfiguration . class ) ;
assertThat ( this . context . getBean (
org . springframework . boot . autoconfigure . data . alt . jpa . CityJpaRepository . class ) )
. isNotNull ( ) ;
assertThat ( this . context . getBean ( PlatformTransactionManager . class ) ) . isNotNull ( ) ;
assertThat ( this . context . getBean ( EntityManagerFactory . class ) ) . isNotNull ( ) ;
this . contextRunner . withUserConfiguration ( CustomConfiguration . class )
. run ( ( context ) - > {
assertThat ( context ) . hasSingleBean ( CityJpaRepository . class ) ;
assertThat ( context ) . hasSingleBean ( PlatformTransactionManager . class ) ;
assertThat ( context ) . hasSingleBean ( EntityManagerFactory . class ) ;
} ) ;
}
@Test ( expected = NoSuchBeanDefinitionException . class )
@Test
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 ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
this . context . register ( configurationClasses ) ;
this . context . register ( EmbeddedDataSourceConfiguration . class ,
HibernateJpaAutoConfiguration . class ,
JpaRepositoriesAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
@Test
public void whenBootstrappingModeIsDeferredBoostrapExecutorIsConfigured ( ) {
this . contextRunner . withUserConfiguration ( TestConfiguration . class )
. withPropertyValues (
"spring.data.jpa.repositories.bootstrap-mode=deferred" )
. run ( ( context ) - > assertThat (
context . getBean ( LocalContainerEntityManagerFactoryBean . class )
. 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