Skip to content

Commit 885eb26

Browse files
committed
introduce EmbeddedMongoDownloadConfigBuilderCustomizer for easy customization of EmbeddedMongo DownloadConfig
1 parent 0ad72d5 commit 885eb26

File tree

3 files changed

+110
-13
lines changed

3 files changed

+110
-13
lines changed

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import de.flapdoodle.embed.mongo.distribution.Versions;
4040
import de.flapdoodle.embed.process.config.IRuntimeConfig;
4141
import de.flapdoodle.embed.process.config.io.ProcessOutput;
42+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
4243
import de.flapdoodle.embed.process.distribution.GenericVersion;
4344
import de.flapdoodle.embed.process.io.Processors;
4445
import de.flapdoodle.embed.process.io.Slf4jLevel;
@@ -48,6 +49,7 @@
4849
import org.slf4j.Logger;
4950
import org.slf4j.LoggerFactory;
5051

52+
import org.springframework.beans.factory.ObjectProvider;
5153
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
5254
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5355
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -202,24 +204,43 @@ private Map<String, Object> getMongoPorts(MutablePropertySources sources) {
202204
@ConditionalOnMissingBean(IRuntimeConfig.class)
203205
static class RuntimeConfigConfiguration {
204206

207+
private static Logger EMBEDDED_MONGO_LOGGER = LoggerFactory
208+
.getLogger(RuntimeConfigConfiguration.class.getPackage().getName()
209+
+ ".EmbeddedMongo");
210+
205211
@Bean
206-
public IRuntimeConfig embeddedMongoRuntimeConfig() {
207-
Logger logger = LoggerFactory
208-
.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
212+
public IRuntimeConfig embeddedMongoRuntimeConfig(
213+
IDownloadConfig embeddedMongoDownloadConfig) {
209214
ProcessOutput processOutput = new ProcessOutput(
210-
Processors.logTo(logger, Slf4jLevel.INFO),
211-
Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named(
212-
"[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
213-
return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger)
214-
.processOutput(processOutput).artifactStore(getArtifactStore(logger))
215-
.build();
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();
223+
}
224+
225+
@Bean
226+
@ConditionalOnMissingBean
227+
public IDownloadConfig embeddedMongoDownloadConfig(
228+
ObjectProvider<EmbeddedMongoDownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizer) {
229+
DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder()
230+
.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();
216239
}
217240

218-
private ArtifactStoreBuilder getArtifactStore(Logger logger) {
241+
private ArtifactStoreBuilder getArtifactStore(IDownloadConfig downloadConfig) {
219242
return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD)
220-
.download(new DownloadConfigBuilder()
221-
.defaultsForCommand(Command.MongoD)
222-
.progressListener(new Slf4jProgressListener(logger)).build());
243+
.download(downloadConfig);
223244
}
224245

225246
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.mongo.embedded;
18+
19+
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder;
20+
21+
/**
22+
* Callback interface that can be implemented by beans wishing to customize the
23+
* EmbeddedMongo {@link DownloadConfigBuilder} outcome whilst retaining default
24+
* auto-configuration.
25+
*
26+
* @author Michael Gmeiner
27+
* @since 2.2.0
28+
*/
29+
@FunctionalInterface
30+
public interface EmbeddedMongoDownloadConfigBuilderCustomizer {
31+
32+
/**
33+
* Customize the {@link DownloadConfigBuilder}.
34+
* @param downloadConfigBuilder the {@link DownloadConfigBuilder} to customize
35+
*/
36+
void customize(DownloadConfigBuilder downloadConfigBuilder);
37+
38+
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
import java.util.stream.Collectors;
2323

2424
import com.mongodb.MongoClient;
25+
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder;
2526
import de.flapdoodle.embed.mongo.config.IMongodConfig;
2627
import de.flapdoodle.embed.mongo.config.Storage;
2728
import de.flapdoodle.embed.mongo.distribution.Feature;
2829
import de.flapdoodle.embed.mongo.distribution.Version;
30+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
31+
import de.flapdoodle.embed.process.io.progress.Slf4jProgressListener;
2932
import org.bson.Document;
3033
import org.junit.After;
3134
import org.junit.Rule;
@@ -180,6 +183,30 @@ public void customReplicaSetNameIsAppliedToConfiguration() {
180183
.isEqualTo("testing");
181184
}
182185

186+
@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");
205+
assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent");
206+
assertThat(downloadConfig.getProgressListener())
207+
.isInstanceOf(Slf4jProgressListener.class);
208+
}
209+
183210
private void assertVersionConfiguration(String configuredVersion,
184211
String expectedVersion) {
185212
this.context = new AnnotationConfigApplicationContext();
@@ -227,4 +254,15 @@ public MongoClient mongoClient(@Value("${local.mongo.port}") int port) {
227254

228255
}
229256

257+
private static class TestEmbeddedMongoDownloadConfigBuilderCustomizer
258+
implements EmbeddedMongoDownloadConfigBuilderCustomizer {
259+
260+
@Override
261+
public void customize(DownloadConfigBuilder downloadConfigBuilder) {
262+
downloadConfigBuilder.downloadPath("test");
263+
downloadConfigBuilder.userAgent("Test User Agent");
264+
}
265+
266+
}
267+
230268
}

0 commit comments

Comments
 (0)