Make Flyway and Liquibase endpoints conditional on a single candidate

Previously, auto-configuration of the Flyway and Liquibase endpoints
would fail if there were multiple Flyway or Spring Liquibase beans
in the application context.

This commit updates them so that they are now conditional on a single
candidate.

Closes gh-6609
This commit is contained in:
Andy Wilkinson 2016-08-15 13:43:03 +01:00
parent a240fbae0d
commit ffc0dc44ed
2 changed files with 62 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -57,6 +57,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionEvaluationRepor
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
@ -175,7 +176,7 @@ public class EndpointAutoConfiguration {
}
@Configuration
@ConditionalOnBean(Flyway.class)
@ConditionalOnSingleCandidate(Flyway.class)
@ConditionalOnClass(Flyway.class)
static class FlywayEndpointConfiguration {
@ -188,7 +189,7 @@ public class EndpointAutoConfiguration {
}
@Configuration
@ConditionalOnBean(SpringLiquibase.class)
@ConditionalOnSingleCandidate(SpringLiquibase.class)
@ConditionalOnClass(SpringLiquibase.class)
static class LiquibaseEndpointConfiguration {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -20,6 +20,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import liquibase.integration.spring.SpringLiquibase;
import org.flywaydb.core.Flyway;
import org.junit.After;
import org.junit.Test;
@ -47,10 +49,14 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link EndpointAutoConfiguration}.
@ -173,6 +179,16 @@ public class EndpointAutoConfigurationTests {
assertEquals(1, endpoint.invoke().size());
}
@Test
public void flywayEndpointIsDisabledWhenThereAreMultipleFlywayBeans() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MultipleFlywayBeansConfig.class,
EndpointAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(FlywayEndpoint.class).size(),
is(equalTo(0)));
}
@Test
public void testLiquibaseEndpoint() {
this.context = new AnnotationConfigApplicationContext();
@ -184,6 +200,16 @@ public class EndpointAutoConfigurationTests {
assertEquals(1, endpoint.invoke().size());
}
@Test
public void liquibaseEndpointIsDisabledWhenThereAreMultipleSpringLiquibaseBeans() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MultipleLiquibaseBeansConfig.class,
EndpointAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(LiquibaseEndpoint.class).size(),
is(equalTo(0)));
}
private void load(Class<?>... config) {
this.context = new AnnotationConfigApplicationContext();
this.context.register(config);
@ -205,4 +231,35 @@ public class EndpointAutoConfigurationTests {
}
}
@Configuration
static class MultipleFlywayBeansConfig {
@Bean
Flyway flywayOne() {
return mock(Flyway.class);
}
@Bean
Flyway flywayTwo() {
return mock(Flyway.class);
}
}
@Configuration
static class MultipleLiquibaseBeansConfig {
@Bean
SpringLiquibase liquibaseOne() {
return mock(SpringLiquibase.class);
}
@Bean
SpringLiquibase liquibaseTwo() {
return mock(SpringLiquibase.class);
}
}
}