1616
1717package io .zonky .test .db .postgres ;
1818
19+ import com .google .common .collect .ImmutableMap ;
1920import io .zonky .test .db .AutoConfigureEmbeddedDatabase ;
2021import io .zonky .test .db .AutoConfigureEmbeddedDatabase .EmbeddedDatabaseType ;
2122import io .zonky .test .db .AutoConfigureEmbeddedDatabase .Replace ;
2223import io .zonky .test .db .flyway .FlywayDataSourceContext ;
2324import org .apache .commons .lang3 .StringUtils ;
2425import org .flywaydb .core .Flyway ;
2526import org .flywaydb .test .annotation .FlywayTest ;
27+ import org .flywaydb .test .annotation .FlywayTests ;
2628import org .slf4j .Logger ;
2729import org .slf4j .LoggerFactory ;
2830import org .springframework .beans .BeansException ;
3739import org .springframework .context .ConfigurableApplicationContext ;
3840import org .springframework .context .support .AbstractApplicationContext ;
3941import org .springframework .core .annotation .AnnotatedElementUtils ;
42+ import org .springframework .core .annotation .AnnotationUtils ;
4043import org .springframework .test .context .ContextConfigurationAttributes ;
4144import org .springframework .test .context .ContextCustomizer ;
4245import org .springframework .test .context .ContextCustomizerFactory ;
4346import org .springframework .test .context .MergedContextConfiguration ;
4447import org .springframework .util .ObjectUtils ;
4548
4649import javax .sql .DataSource ;
50+ import java .lang .reflect .AnnotatedElement ;
4751import java .util .List ;
52+ import java .util .Map ;
4853
4954/**
5055 * Implementation of the {@link org.springframework.test.context.ContextCustomizerFactory} interface,
@@ -81,7 +86,7 @@ private PreloadableEmbeddedPostgresContextCustomizer(AutoConfigureEmbeddedDataba
8186 @ Override
8287 public void customizeContext (ConfigurableApplicationContext context , MergedContextConfiguration mergedConfig ) {
8388 Class <?> testClass = mergedConfig .getTestClass ();
84- FlywayTest flywayAnnotation = AnnotatedElementUtils . findMergedAnnotation (testClass , FlywayTest . class );
89+ FlywayTests flywayAnnotations = findFlywayTestAnnotations (testClass );
8590
8691 BeanDefinitionRegistry registry = getBeanDefinitionRegistry (context );
8792 RootBeanDefinition registrarDefinition = new RootBeanDefinition ();
@@ -90,7 +95,7 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
9095 registrarDefinition .getConstructorArgumentValues ()
9196 .addIndexedArgumentValue (0 , databaseAnnotation );
9297 registrarDefinition .getConstructorArgumentValues ()
93- .addIndexedArgumentValue (1 , flywayAnnotation );
98+ .addIndexedArgumentValue (1 , flywayAnnotations );
9499
95100 registry .registerBeanDefinition ("preloadableEmbeddedPostgresRegistrar" , registrarDefinition );
96101 }
@@ -115,11 +120,11 @@ public int hashCode() {
115120 private static class PreloadableEmbeddedPostgresRegistrar implements BeanDefinitionRegistryPostProcessor {
116121
117122 private final AutoConfigureEmbeddedDatabase databaseAnnotation ;
118- private final FlywayTest flywayAnnotation ;
123+ private final FlywayTests flywayAnnotations ;
119124
120- private PreloadableEmbeddedPostgresRegistrar (AutoConfigureEmbeddedDatabase databaseAnnotation , FlywayTest flywayAnnotation ) {
125+ private PreloadableEmbeddedPostgresRegistrar (AutoConfigureEmbeddedDatabase databaseAnnotation , FlywayTests flywayAnnotations ) {
121126 this .databaseAnnotation = databaseAnnotation ;
122- this .flywayAnnotation = flywayAnnotation ;
127+ this .flywayAnnotations = flywayAnnotations ;
123128 }
124129
125130 @ Override
@@ -131,7 +136,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) t
131136 RootBeanDefinition dataSourceDefinition = new RootBeanDefinition ();
132137 dataSourceDefinition .setPrimary (dataSourceInfo .getBeanDefinition ().isPrimary ());
133138
134- BeanDefinitionHolder flywayInfo = getFlywayBeanDefinition (beanFactory , flywayAnnotation );
139+ BeanDefinitionHolder flywayInfo = getFlywayBeanDefinition (beanFactory , flywayAnnotations );
135140 if (flywayInfo == null ) {
136141 dataSourceDefinition .setBeanClass (EmptyEmbeddedPostgresDataSourceFactoryBean .class );
137142 } else {
@@ -201,7 +206,13 @@ protected static BeanDefinitionHolder getDataSourceBeanDefinition(ConfigurableLi
201206 throw new IllegalStateException ("No primary DataSource found, embedded version will not be used" );
202207 }
203208
204- protected static BeanDefinitionHolder getFlywayBeanDefinition (ConfigurableListableBeanFactory beanFactory , FlywayTest annotation ) {
209+ protected static BeanDefinitionHolder getFlywayBeanDefinition (ConfigurableListableBeanFactory beanFactory , FlywayTests annotations ) {
210+ if (annotations != null && annotations .value ().length > 1 ) {
211+ return null ; // optimized loading is not supported yet when using multiple flyway test annotations
212+ }
213+
214+ FlywayTest annotation = annotations != null && annotations .value ().length == 1 ? annotations .value ()[0 ] : null ;
215+
205216 if (annotation != null && StringUtils .isNotBlank (annotation .flywayName ())) {
206217 BeanDefinition beanDefinition = beanFactory .getBeanDefinition (annotation .flywayName ());
207218 return new BeanDefinitionHolder (beanDefinition , annotation .flywayName ());
@@ -228,4 +239,19 @@ protected static BeanDefinitionHolder getFlywayBeanDefinition(ConfigurableListab
228239
229240 return null ;
230241 }
242+
243+ private static FlywayTests findFlywayTestAnnotations (AnnotatedElement element ) {
244+ FlywayTests flywayContainerAnnotation = AnnotatedElementUtils .findMergedAnnotation (element , FlywayTests .class );
245+ FlywayTest flywayAnnotation = AnnotatedElementUtils .findMergedAnnotation (element , FlywayTest .class );
246+
247+ if (flywayContainerAnnotation != null ) {
248+ return flywayContainerAnnotation ;
249+ }
250+ if (flywayAnnotation == null ) {
251+ return null ;
252+ }
253+
254+ Map <String , Object > attributes = ImmutableMap .of ("value" , new FlywayTest [] { flywayAnnotation });
255+ return AnnotationUtils .synthesizeAnnotation (attributes , FlywayTests .class , null );
256+ }
231257}
0 commit comments