Skip to content

Commit 6ba1f40

Browse files
committed
Polish "Allow easy customization of EmbeddedMongo DownloadConfig"
Closes gh-15496
1 parent b5b6889 commit 6ba1f40

File tree

4 files changed

+39
-62
lines changed

4 files changed

+39
-62
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -17,17 +17,18 @@
1717
package org.springframework.boot.autoconfigure.mongo.embedded;
1818

1919
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder;
20+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
2021

2122
/**
2223
* Callback interface that can be implemented by beans wishing to customize the
23-
* EmbeddedMongo {@link DownloadConfigBuilder} outcome whilst retaining default
24+
* {@link IDownloadConfig} via a {@link DownloadConfigBuilder} whilst retaining default
2425
* auto-configuration.
2526
*
2627
* @author Michael Gmeiner
2728
* @since 2.2.0
2829
*/
2930
@FunctionalInterface
30-
public interface EmbeddedMongoDownloadConfigBuilderCustomizer {
31+
public interface DownloadConfigBuilderCustomizer {
3132

3233
/**
3334
* Customize the {@link DownloadConfigBuilder}.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -21,6 +21,7 @@
2121
import java.net.UnknownHostException;
2222
import java.util.HashMap;
2323
import java.util.Map;
24+
import java.util.stream.Stream;
2425

2526
import com.mongodb.MongoClient;
2627
import de.flapdoodle.embed.mongo.Command;
@@ -204,41 +205,29 @@ private Map<String, Object> getMongoPorts(MutablePropertySources sources) {
204205
@ConditionalOnMissingBean(IRuntimeConfig.class)
205206
static class RuntimeConfigConfiguration {
206207

207-
private static Logger EMBEDDED_MONGO_LOGGER = LoggerFactory
208-
.getLogger(RuntimeConfigConfiguration.class.getPackage().getName()
209-
+ ".EmbeddedMongo");
210-
211208
@Bean
212209
public IRuntimeConfig embeddedMongoRuntimeConfig(
213-
IDownloadConfig embeddedMongoDownloadConfig) {
210+
ObjectProvider<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
211+
Logger logger = LoggerFactory
212+
.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
214213
ProcessOutput processOutput = new ProcessOutput(
215-
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.INFO),
216-
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.ERROR),
217-
Processors.named("[console>]",
218-
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.DEBUG)));
219-
return new RuntimeConfigBuilder()
220-
.defaultsWithLogger(Command.MongoD, EMBEDDED_MONGO_LOGGER)
221-
.processOutput(processOutput)
222-
.artifactStore(getArtifactStore(embeddedMongoDownloadConfig)).build();
214+
Processors.logTo(logger, Slf4jLevel.INFO),
215+
Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named(
216+
"[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
217+
return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger)
218+
.processOutput(processOutput).artifactStore(getArtifactStore(logger,
219+
downloadConfigBuilderCustomizers.orderedStream()))
220+
.build();
223221
}
224222

225-
@Bean
226-
@ConditionalOnMissingBean
227-
public IDownloadConfig embeddedMongoDownloadConfig(
228-
ObjectProvider<EmbeddedMongoDownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizer) {
223+
private ArtifactStoreBuilder getArtifactStore(Logger logger,
224+
Stream<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
229225
DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder()
230226
.defaultsForCommand(Command.MongoD);
231-
232-
downloadConfigBuilder
233-
.progressListener(new Slf4jProgressListener(EMBEDDED_MONGO_LOGGER));
234-
235-
downloadConfigBuilderCustomizer.stream()
236-
.forEach((c) -> c.customize(downloadConfigBuilder));
237-
238-
return downloadConfigBuilder.build();
239-
}
240-
241-
private ArtifactStoreBuilder getArtifactStore(IDownloadConfig downloadConfig) {
227+
downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger));
228+
downloadConfigBuilderCustomizers
229+
.forEach((customizer) -> customizer.customize(downloadConfigBuilder));
230+
IDownloadConfig downloadConfig = downloadConfigBuilder.build();
242231
return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD)
243232
.download(downloadConfig);
244233
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -22,19 +22,19 @@
2222
import java.util.stream.Collectors;
2323

2424
import com.mongodb.MongoClient;
25-
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder;
2625
import de.flapdoodle.embed.mongo.config.IMongodConfig;
2726
import de.flapdoodle.embed.mongo.config.Storage;
2827
import de.flapdoodle.embed.mongo.distribution.Feature;
2928
import de.flapdoodle.embed.mongo.distribution.Version;
29+
import de.flapdoodle.embed.process.config.IRuntimeConfig;
3030
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
31-
import de.flapdoodle.embed.process.io.progress.Slf4jProgressListener;
3231
import org.bson.Document;
3332
import org.junit.After;
3433
import org.junit.Rule;
3534
import org.junit.Test;
3635
import org.junit.rules.TemporaryFolder;
3736

37+
import org.springframework.beans.DirectFieldAccessor;
3838
import org.springframework.beans.factory.annotation.Value;
3939
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
4040
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
@@ -184,27 +184,12 @@ public void customReplicaSetNameIsAppliedToConfiguration() {
184184
}
185185

186186
@Test
187-
public void defaultDownloadConfiguration() {
188-
load();
189-
IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class);
190-
191-
assertThat(downloadConfig.getDownloadPath().getClass().getSimpleName())
192-
.isEqualTo("PlatformDependentDownloadPath");
193-
assertThat(downloadConfig.getUserAgent()).isEqualTo(
194-
"Mozilla/5.0 (compatible; Embedded MongoDB; +https://github.com/flapdoodle-oss/embedmongo.flapdoodle.de)");
195-
assertThat(downloadConfig.getProgressListener())
196-
.isInstanceOf(Slf4jProgressListener.class);
197-
}
198-
199-
@Test
200-
public void customizedDownloadConfiguration() {
201-
load(TestEmbeddedMongoDownloadConfigBuilderCustomizer.class);
202-
203-
IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class);
204-
assertThat(downloadConfig.getDownloadPath().getPath(null)).isEqualTo("test");
187+
public void customizeDownloadConfiguration() {
188+
load(DownloadConfigBuilderCustomizerConfiguration.class);
189+
IRuntimeConfig runtimeConfig = this.context.getBean(IRuntimeConfig.class);
190+
IDownloadConfig downloadConfig = (IDownloadConfig) new DirectFieldAccessor(
191+
runtimeConfig.getArtifactStore()).getPropertyValue("downloadConfig");
205192
assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent");
206-
assertThat(downloadConfig.getProgressListener())
207-
.isInstanceOf(Slf4jProgressListener.class);
208193
}
209194

210195
private void assertVersionConfiguration(String configuredVersion,
@@ -254,13 +239,14 @@ public MongoClient mongoClient(@Value("${local.mongo.port}") int port) {
254239

255240
}
256241

257-
private static class TestEmbeddedMongoDownloadConfigBuilderCustomizer
258-
implements EmbeddedMongoDownloadConfigBuilderCustomizer {
242+
@Configuration
243+
static class DownloadConfigBuilderCustomizerConfiguration {
259244

260-
@Override
261-
public void customize(DownloadConfigBuilder downloadConfigBuilder) {
262-
downloadConfigBuilder.downloadPath("test");
263-
downloadConfigBuilder.userAgent("Test User Agent");
245+
@Bean
246+
public DownloadConfigBuilderCustomizer testDownloadConfigBuilderCustomizer() {
247+
return (downloadConfigBuilder) -> {
248+
downloadConfigBuilder.userAgent("Test User Agent");
249+
};
264250
}
265251

266252
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4292,7 +4292,8 @@ If you have SLF4J on the classpath, the output produced by Mongo is automaticall
42924292
to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`.
42934293

42944294
You can declare your own `IMongodConfig` and `IRuntimeConfig` beans to take control of
4295-
the Mongo instance's configuration and logging routing.
4295+
the Mongo instance's configuration and logging routing. The download configuration can be
4296+
customized by declaring a `DownloadConfigBuilderCustomizer` bean.
42964297

42974298

42984299

0 commit comments

Comments
 (0)