Skip to content

Commit ffc0dc4

Browse files
committed
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
1 parent a240fba commit ffc0dc4

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointAutoConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,6 +57,7 @@
5757
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
5858
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
5959
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
60+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
6061
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
6162
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
6263
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
@@ -175,7 +176,7 @@ public ConfigurationPropertiesReportEndpoint configurationPropertiesReportEndpoi
175176
}
176177

177178
@Configuration
178-
@ConditionalOnBean(Flyway.class)
179+
@ConditionalOnSingleCandidate(Flyway.class)
179180
@ConditionalOnClass(Flyway.class)
180181
static class FlywayEndpointConfiguration {
181182

@@ -188,7 +189,7 @@ public FlywayEndpoint flywayEndpoint(Flyway flyway) {
188189
}
189190

190191
@Configuration
191-
@ConditionalOnBean(SpringLiquibase.class)
192+
@ConditionalOnSingleCandidate(SpringLiquibase.class)
192193
@ConditionalOnClass(SpringLiquibase.class)
193194
static class LiquibaseEndpointConfiguration {
194195

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointAutoConfigurationTests.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
2020
import java.util.Collections;
2121
import java.util.Map;
2222

23+
import liquibase.integration.spring.SpringLiquibase;
24+
import org.flywaydb.core.Flyway;
2325
import org.junit.After;
2426
import org.junit.Test;
2527

@@ -47,10 +49,14 @@
4749
import org.springframework.context.annotation.Bean;
4850
import org.springframework.context.annotation.Configuration;
4951

52+
import static org.hamcrest.Matchers.equalTo;
53+
import static org.hamcrest.Matchers.is;
5054
import static org.junit.Assert.assertEquals;
5155
import static org.junit.Assert.assertNotNull;
5256
import static org.junit.Assert.assertNull;
57+
import static org.junit.Assert.assertThat;
5358
import static org.junit.Assert.assertTrue;
59+
import static org.mockito.Mockito.mock;
5460

5561
/**
5662
* Tests for {@link EndpointAutoConfiguration}.
@@ -173,6 +179,16 @@ public void testFlywayEndpoint() {
173179
assertEquals(1, endpoint.invoke().size());
174180
}
175181

182+
@Test
183+
public void flywayEndpointIsDisabledWhenThereAreMultipleFlywayBeans() {
184+
this.context = new AnnotationConfigApplicationContext();
185+
this.context.register(MultipleFlywayBeansConfig.class,
186+
EndpointAutoConfiguration.class);
187+
this.context.refresh();
188+
assertThat(this.context.getBeansOfType(FlywayEndpoint.class).size(),
189+
is(equalTo(0)));
190+
}
191+
176192
@Test
177193
public void testLiquibaseEndpoint() {
178194
this.context = new AnnotationConfigApplicationContext();
@@ -184,6 +200,16 @@ public void testLiquibaseEndpoint() {
184200
assertEquals(1, endpoint.invoke().size());
185201
}
186202

203+
@Test
204+
public void liquibaseEndpointIsDisabledWhenThereAreMultipleSpringLiquibaseBeans() {
205+
this.context = new AnnotationConfigApplicationContext();
206+
this.context.register(MultipleLiquibaseBeansConfig.class,
207+
EndpointAutoConfiguration.class);
208+
this.context.refresh();
209+
assertThat(this.context.getBeansOfType(LiquibaseEndpoint.class).size(),
210+
is(equalTo(0)));
211+
}
212+
187213
private void load(Class<?>... config) {
188214
this.context = new AnnotationConfigApplicationContext();
189215
this.context.register(config);
@@ -205,4 +231,35 @@ public Collection<Metric<?>> metrics() {
205231
}
206232

207233
}
234+
235+
@Configuration
236+
static class MultipleFlywayBeansConfig {
237+
238+
@Bean
239+
Flyway flywayOne() {
240+
return mock(Flyway.class);
241+
}
242+
243+
@Bean
244+
Flyway flywayTwo() {
245+
return mock(Flyway.class);
246+
}
247+
248+
}
249+
250+
@Configuration
251+
static class MultipleLiquibaseBeansConfig {
252+
253+
@Bean
254+
SpringLiquibase liquibaseOne() {
255+
return mock(SpringLiquibase.class);
256+
}
257+
258+
@Bean
259+
SpringLiquibase liquibaseTwo() {
260+
return mock(SpringLiquibase.class);
261+
}
262+
263+
}
264+
208265
}

0 commit comments

Comments
 (0)