Commit ffc0dc44 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent a240fbae
/* /*
* 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"); * 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.
...@@ -57,6 +57,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionEvaluationRepor ...@@ -57,6 +57,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionEvaluationRepor
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 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.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
...@@ -175,7 +176,7 @@ public class EndpointAutoConfiguration { ...@@ -175,7 +176,7 @@ public class EndpointAutoConfiguration {
} }
@Configuration @Configuration
@ConditionalOnBean(Flyway.class) @ConditionalOnSingleCandidate(Flyway.class)
@ConditionalOnClass(Flyway.class) @ConditionalOnClass(Flyway.class)
static class FlywayEndpointConfiguration { static class FlywayEndpointConfiguration {
...@@ -188,7 +189,7 @@ public class EndpointAutoConfiguration { ...@@ -188,7 +189,7 @@ public class EndpointAutoConfiguration {
} }
@Configuration @Configuration
@ConditionalOnBean(SpringLiquibase.class) @ConditionalOnSingleCandidate(SpringLiquibase.class)
@ConditionalOnClass(SpringLiquibase.class) @ConditionalOnClass(SpringLiquibase.class)
static class LiquibaseEndpointConfiguration { static class LiquibaseEndpointConfiguration {
......
/* /*
* 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"); * 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.
...@@ -20,6 +20,8 @@ import java.util.Collection; ...@@ -20,6 +20,8 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import liquibase.integration.spring.SpringLiquibase;
import org.flywaydb.core.Flyway;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
...@@ -47,10 +49,14 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext ...@@ -47,10 +49,14 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link EndpointAutoConfiguration}. * Tests for {@link EndpointAutoConfiguration}.
...@@ -173,6 +179,16 @@ public class EndpointAutoConfigurationTests { ...@@ -173,6 +179,16 @@ public class EndpointAutoConfigurationTests {
assertEquals(1, endpoint.invoke().size()); 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 @Test
public void testLiquibaseEndpoint() { public void testLiquibaseEndpoint() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
...@@ -184,6 +200,16 @@ public class EndpointAutoConfigurationTests { ...@@ -184,6 +200,16 @@ public class EndpointAutoConfigurationTests {
assertEquals(1, endpoint.invoke().size()); 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) { private void load(Class<?>... config) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(config); this.context.register(config);
...@@ -205,4 +231,35 @@ public class EndpointAutoConfigurationTests { ...@@ -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);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment