@ -14,7 +14,7 @@
* limitations under the License .
* /
package org.springframework.boot. jdbc .init.dependency;
package org.springframework.boot. sql .init.dependency;
import java.io.File ;
import java.io.FileWriter ;
@ -51,55 +51,54 @@ import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify ;
/ * *
* Tests for { @link Data Sourc eInitializationDependencyConfigurer} .
* Tests for { @link Data bas eInitializationDependencyConfigurer} .
*
* @author Andy Wilkinson
* /
class Data Sourc eInitializationDependencyConfigurerTests {
class Data bas eInitializationDependencyConfigurerTests {
private final ConfigurableEnvironment environment = new MockEnvironment ( ) ;
Data SourceInitializerDetector dataSourceInitializerDetector = MockedDataSourc eInitializerDetector. mock ;
Data baseInitializerDetector databaseInitializerDetector = MockedDatabas eInitializerDetector. mock ;
DependsOnData SourceInitializationDetector dependsOnDataSourceInitializationDetector = MockedDependsOnDataSourc eInitializationDetector. mock ;
DependsOnData baseInitializationDetector dependsOnDatabaseInitializationDetector = MockedDependsOnDatabas eInitializationDetector. mock ;
@TempDir
File temp ;
@BeforeEach
void resetMocks ( ) {
reset ( MockedData SourceInitializerDetector. mock , MockedDependsOnDataSourc eInitializationDetector. mock ) ;
reset ( MockedData baseInitializerDetector. mock , MockedDependsOnDatabas eInitializationDetector. mock ) ;
}
@Test
void whenDetectorsAreCreatedThenTheEnvironmentCanBeInjected ( ) {
performDetection ( Arrays . asList ( ConstructorInjectionData Sourc eInitializerDetector. class ,
ConstructorInjectionDependsOnData Sourc eInitializationDetector. class ) , ( context ) - > {
performDetection ( Arrays . asList ( ConstructorInjectionData bas eInitializerDetector. class ,
ConstructorInjectionDependsOnData bas eInitializationDetector. class ) , ( context ) - > {
context . refresh ( ) ;
assertThat ( ConstructorInjectionDataSourceInitializerDetector . environment )
. isEqualTo ( this . environment ) ;
assertThat ( ConstructorInjectionDependsOnDataSourceInitializationDetector . environment )
assertThat ( ConstructorInjectionDatabaseInitializerDetector . environment ) . isEqualTo ( this . environment ) ;
assertThat ( ConstructorInjectionDependsOnDatabaseInitializationDetector . environment )
. isEqualTo ( this . environment ) ;
} ) ;
}
@Test
void whenDependenciesAreConfiguredThenBeansThatDependUponData SourceInitializationDependUponDetectedDataSourc eInitializers( ) {
void whenDependenciesAreConfiguredThenBeansThatDependUponData baseInitializationDependUponDetectedDatabas eInitializers( ) {
BeanDefinition alpha = BeanDefinitionBuilder . genericBeanDefinition ( String . class ) . getBeanDefinition ( ) ;
BeanDefinition bravo = BeanDefinitionBuilder . genericBeanDefinition ( String . class ) . getBeanDefinition ( ) ;
performDetection ( Arrays . asList ( MockedData Sourc eInitializerDetector. class ,
MockedDependsOnData Sourc eInitializationDetector. class ) , ( context ) - > {
performDetection ( Arrays . asList ( MockedData bas eInitializerDetector. class ,
MockedDependsOnData bas eInitializationDetector. class ) , ( context ) - > {
context . registerBeanDefinition ( "alpha" , alpha ) ;
context . registerBeanDefinition ( "bravo" , bravo ) ;
given ( this . data Sourc eInitializerDetector. detect ( context . getBeanFactory ( ) ) )
given ( this . data bas eInitializerDetector. detect ( context . getBeanFactory ( ) ) )
. willReturn ( Collections . singleton ( "alpha" ) ) ;
given ( this . dependsOnData Sourc eInitializationDetector. detect ( context . getBeanFactory ( ) ) )
given ( this . dependsOnData bas eInitializationDetector. detect ( context . getBeanFactory ( ) ) )
. willReturn ( Collections . singleton ( "bravo" ) ) ;
context . refresh ( ) ;
assertThat ( alpha . getAttribute ( Data Sourc eInitializerDetector. class . getName ( ) ) )
. isEqualTo ( MockedData Sourc eInitializerDetector. class . getName ( ) ) ;
assertThat ( bravo . getAttribute ( Data Sourc eInitializerDetector. class . getName ( ) ) ) . isNull ( ) ;
verify ( this . data Sourc eInitializerDetector) . detectionComplete ( context . getBeanFactory ( ) ,
assertThat ( alpha . getAttribute ( Data bas eInitializerDetector. class . getName ( ) ) )
. isEqualTo ( MockedData bas eInitializerDetector. class . getName ( ) ) ;
assertThat ( bravo . getAttribute ( Data bas eInitializerDetector. class . getName ( ) ) ) . isNull ( ) ;
verify ( this . data bas eInitializerDetector) . detectionComplete ( context . getBeanFactory ( ) ,
Collections . singleton ( "alpha" ) ) ;
assertThat ( bravo . getDependsOn ( ) ) . containsExactly ( "alpha" ) ;
} ) ;
@ -118,17 +117,17 @@ class DataSourceInitializationDependencyConfigurerTests {
}
@Configuration ( proxyBeanMethods = false )
@Import ( Data Sourc eInitializationDependencyConfigurer. class )
@Import ( Data bas eInitializationDependencyConfigurer. class )
static class DependencyConfigurerConfiguration {
}
static class ConstructorInjectionData SourceInitializerDetector implements DataSourc eInitializerDetector {
static class ConstructorInjectionData baseInitializerDetector implements Databas eInitializerDetector {
private static Environment environment ;
ConstructorInjectionData Sourc eInitializerDetector( Environment environment ) {
ConstructorInjectionData Sourc eInitializerDetector. environment = environment ;
ConstructorInjectionData bas eInitializerDetector( Environment environment ) {
ConstructorInjectionData bas eInitializerDetector. environment = environment ;
}
@Override
@ -138,13 +137,13 @@ class DataSourceInitializationDependencyConfigurerTests {
}
static class ConstructorInjectionDependsOnData Sourc eInitializationDetector
implements DependsOnData Sourc eInitializationDetector {
static class ConstructorInjectionDependsOnData bas eInitializationDetector
implements DependsOnData bas eInitializationDetector {
private static Environment environment ;
ConstructorInjectionDependsOnData Sourc eInitializationDetector( Environment environment ) {
ConstructorInjectionDependsOnData Sourc eInitializationDetector. environment = environment ;
ConstructorInjectionDependsOnData bas eInitializationDetector( Environment environment ) {
ConstructorInjectionDependsOnData bas eInitializationDetector. environment = environment ;
}
@Override
@ -154,40 +153,40 @@ class DataSourceInitializationDependencyConfigurerTests {
}
static class MockedData SourceInitializerDetector implements DataSourc eInitializerDetector {
static class MockedData baseInitializerDetector implements Databas eInitializerDetector {
private static Data Sourc eInitializerDetector mock = Mockito . mock ( Data Sourc eInitializerDetector. class ) ;
private static Data bas eInitializerDetector mock = Mockito . mock ( Data bas eInitializerDetector. class ) ;
@Override
public Set < String > detect ( ConfigurableListableBeanFactory beanFactory ) {
return MockedData Sourc eInitializerDetector. mock . detect ( beanFactory ) ;
return MockedData bas eInitializerDetector. mock . detect ( beanFactory ) ;
}
@Override
public void detectionComplete ( ConfigurableListableBeanFactory beanFactory ,
Set < String > data Sourc eInitializerNames) {
mock . detectionComplete ( beanFactory , data Sourc eInitializerNames) ;
Set < String > data bas eInitializerNames) {
mock . detectionComplete ( beanFactory , data bas eInitializerNames) ;
}
}
static class MockedDependsOnData SourceInitializationDetector implements DependsOnDataSourc eInitializationDetector {
static class MockedDependsOnData baseInitializationDetector implements DependsOnDatabas eInitializationDetector {
private static DependsOnData Sourc eInitializationDetector mock = Mockito
. mock ( DependsOnData Sourc eInitializationDetector. class ) ;
private static DependsOnData bas eInitializationDetector mock = Mockito
. mock ( DependsOnData bas eInitializationDetector. class ) ;
@Override
public Set < String > detect ( ConfigurableListableBeanFactory beanFactory ) {
return MockedDependsOnData Sourc eInitializationDetector. mock . detect ( beanFactory ) ;
return MockedDependsOnData bas eInitializationDetector. mock . detect ( beanFactory ) ;
}
}
static class DetectorSpringFactoriesClassLoader extends ClassLoader {
private final Set < Class < Data SourceInitializerDetector> > dataSourc eInitializerDetectors = new HashSet < > ( ) ;
private final Set < Class < Data baseInitializerDetector> > databas eInitializerDetectors = new HashSet < > ( ) ;
private final Set < Class < DependsOnData SourceInitializationDetector> > dependsOnDataSourc eInitializationDetectors = new HashSet < > ( ) ;
private final Set < Class < DependsOnData baseInitializationDetector> > dependsOnDatabas eInitializationDetectors = new HashSet < > ( ) ;
private final File temp ;
@ -197,12 +196,12 @@ class DataSourceInitializationDependencyConfigurerTests {
@SuppressWarnings ( "unchecked" )
void register ( Class < ? > detector ) {
if ( Data Sourc eInitializerDetector. class . isAssignableFrom ( detector ) ) {
this . data SourceInitializerDetectors. add ( ( Class < DataSourc eInitializerDetector> ) detector ) ;
if ( Data bas eInitializerDetector. class . isAssignableFrom ( detector ) ) {
this . data baseInitializerDetectors. add ( ( Class < Databas eInitializerDetector> ) detector ) ;
}
else if ( DependsOnData Sourc eInitializationDetector. class . isAssignableFrom ( detector ) ) {
this . dependsOnData Sourc eInitializationDetectors
. add ( ( Class < DependsOnData Sourc eInitializationDetector> ) detector ) ;
else if ( DependsOnData bas eInitializationDetector. class . isAssignableFrom ( detector ) ) {
this . dependsOnData bas eInitializationDetectors
. add ( ( Class < DependsOnData bas eInitializationDetector> ) detector ) ;
}
else {
throw new IllegalArgumentException ( "Unsupported detector type '" + detector . getName ( ) + "'" ) ;
@ -215,10 +214,10 @@ class DataSourceInitializationDependencyConfigurerTests {
return super . findResources ( name ) ;
}
Properties properties = new Properties ( ) ;
properties . put ( Data Sourc eInitializerDetector. class . getName ( ) , String . join ( "," ,
this . data Sourc eInitializerDetectors. stream ( ) . map ( Class : : getName ) . collect ( Collectors . toList ( ) ) ) ) ;
properties . put ( DependsOnData Sourc eInitializationDetector. class . getName ( ) ,
String . join ( "," , this . dependsOnData Sourc eInitializationDetectors. stream ( ) . map ( Class : : getName )
properties . put ( Data bas eInitializerDetector. class . getName ( ) , String . join ( "," ,
this . data bas eInitializerDetectors. stream ( ) . map ( Class : : getName ) . collect ( Collectors . toList ( ) ) ) ) ;
properties . put ( DependsOnData bas eInitializationDetector. class . getName ( ) ,
String . join ( "," , this . dependsOnData bas eInitializationDetectors. stream ( ) . map ( Class : : getName )
. collect ( Collectors . toList ( ) ) ) ) ;
File springFactories = new File ( this . temp , "spring.factories" ) ;
try ( FileWriter writer = new FileWriter ( springFactories ) ) {