Skip to content

Commit 4bc931b

Browse files
authored
Merge pull request #80 from zonkyio/spring-cloud-hoxton
#79 Change the way the flyway datasource properties are replaced
2 parents ffce005 + 0df38d7 commit 4bc931b

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ project(':embedded-database-spring-test') {
156156
compile 'org.testcontainers:postgresql:1.12.1', optional
157157
compile 'com.opentable.components:otj-pg-embedded:0.13.1', optional
158158
compile 'ru.yandex.qatools.embed:postgresql-embedded:2.10', optional
159+
compile 'org.springframework.boot:spring-boot-starter-test:1.5.22.RELEASE', optional
159160

160161
compile 'org.springframework:spring-context:4.3.25.RELEASE'
161162
compile 'org.springframework:spring-test:4.3.25.RELEASE'
@@ -167,8 +168,7 @@ project(':embedded-database-spring-test') {
167168
compile('com.cedarsoftware:java-util:1.34.0') {
168169
exclude group: 'org.apache.logging.log4j'
169170
}
170-
171-
testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.22.RELEASE'
171+
172172
testCompile 'ch.qos.logback:logback-classic:1.2.3'
173173
}
174174

embedded-database-spring-test/src/main/java/io/zonky/test/db/postgres/EmbeddedPostgresContextCustomizerFactory.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
import org.springframework.beans.BeansException;
3939
import org.springframework.beans.factory.config.BeanDefinition;
4040
import org.springframework.beans.factory.config.BeanDefinitionHolder;
41+
import org.springframework.beans.factory.config.BeanPostProcessor;
4142
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4243
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
4344
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
4445
import org.springframework.beans.factory.support.RootBeanDefinition;
46+
import org.springframework.boot.autoconfigure.flyway.FlywayProperties;
4547
import org.springframework.context.ApplicationContext;
4648
import org.springframework.context.ConfigurableApplicationContext;
4749
import org.springframework.context.EnvironmentAware;
@@ -143,8 +145,6 @@ public int hashCode() {
143145

144146
protected static class EnvironmentPostProcessor implements BeanDefinitionRegistryPostProcessor {
145147

146-
private static final NullPlaceholder NULL = new NullPlaceholder();
147-
148148
private final ConfigurableEnvironment environment;
149149

150150
public EnvironmentPostProcessor(ConfigurableEnvironment environment) {
@@ -155,31 +155,31 @@ public EnvironmentPostProcessor(ConfigurableEnvironment environment) {
155155
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
156156
environment.getPropertySources().addFirst(new MapPropertySource(
157157
PreloadableEmbeddedPostgresContextCustomizer.class.getSimpleName(),
158-
ImmutableMap.of(
159-
"spring.test.database.replace", "NONE",
160-
"spring.flyway.url", NULL,
161-
"spring.flyway.user", NULL,
162-
"flyway.url", NULL,
163-
"flyway.user", NULL
164-
)));
158+
ImmutableMap.of("spring.test.database.replace", "NONE")));
165159
}
166160

167161
@Override
168162
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
169163
// nothing to do
170164
}
165+
}
171166

172-
protected static class NullPlaceholder {
167+
protected static class FlywayPropertiesPostProcessor implements BeanPostProcessor {
173168

174-
// this method is required to hook up org.springframework.core.convert.support.FallbackObjectToStringConverter
175-
public static NullPlaceholder valueOf(String value) {
176-
throw new IllegalStateException("This method should never be called!");
177-
}
169+
@Override
170+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
171+
return bean;
172+
}
178173

179-
@Override
180-
public String toString() {
181-
return null;
174+
@Override
175+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
176+
if (bean instanceof FlywayProperties) {
177+
FlywayProperties properties = (FlywayProperties) bean;
178+
properties.setUrl(null);
179+
properties.setUser(null);
180+
properties.setPassword(null);
182181
}
182+
return bean;
183183
}
184184
}
185185

@@ -218,6 +218,9 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) t
218218
if (ClassUtils.isPresent("ru.yandex.qatools.embed.postgresql.EmbeddedPostgres", null)) {
219219
registerBeanIfMissing(registry, "yandexPostgresProvider", YandexPostgresDatabaseProvider.class);
220220
}
221+
if (ClassUtils.isPresent("org.springframework.boot.autoconfigure.flyway.FlywayProperties", null)) {
222+
registerBeanIfMissing(registry, "flywayPropertiesPostProcessor", FlywayPropertiesPostProcessor.class);
223+
}
221224

222225
for (AutoConfigureEmbeddedDatabase databaseAnnotation : databaseAnnotations) {
223226
DatabaseDescriptor databaseDescriptor = resolveDatabaseDescriptor(environment, databaseAnnotation);

0 commit comments

Comments
 (0)