Polish "Allow easy customization of EmbeddedMongo DownloadConfig"

Closes gh-15496
pull/15603/head
Stephane Nicoll 6 years ago
parent b5b6889601
commit 6ba1f40e59

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
@ -17,17 +17,18 @@
package org.springframework.boot.autoconfigure.mongo.embedded; package org.springframework.boot.autoconfigure.mongo.embedded;
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder; import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder;
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
/** /**
* Callback interface that can be implemented by beans wishing to customize the * Callback interface that can be implemented by beans wishing to customize the
* EmbeddedMongo {@link DownloadConfigBuilder} outcome whilst retaining default * {@link IDownloadConfig} via a {@link DownloadConfigBuilder} whilst retaining default
* auto-configuration. * auto-configuration.
* *
* @author Michael Gmeiner * @author Michael Gmeiner
* @since 2.2.0 * @since 2.2.0
*/ */
@FunctionalInterface @FunctionalInterface
public interface EmbeddedMongoDownloadConfigBuilderCustomizer { public interface DownloadConfigBuilderCustomizer {
/** /**
* Customize the {@link DownloadConfigBuilder}. * Customize the {@link DownloadConfigBuilder}.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
@ -21,6 +21,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import de.flapdoodle.embed.mongo.Command; import de.flapdoodle.embed.mongo.Command;
@ -204,41 +205,29 @@ public class EmbeddedMongoAutoConfiguration {
@ConditionalOnMissingBean(IRuntimeConfig.class) @ConditionalOnMissingBean(IRuntimeConfig.class)
static class RuntimeConfigConfiguration { static class RuntimeConfigConfiguration {
private static Logger EMBEDDED_MONGO_LOGGER = LoggerFactory
.getLogger(RuntimeConfigConfiguration.class.getPackage().getName()
+ ".EmbeddedMongo");
@Bean @Bean
public IRuntimeConfig embeddedMongoRuntimeConfig( public IRuntimeConfig embeddedMongoRuntimeConfig(
IDownloadConfig embeddedMongoDownloadConfig) { ObjectProvider<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
Logger logger = LoggerFactory
.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
ProcessOutput processOutput = new ProcessOutput( ProcessOutput processOutput = new ProcessOutput(
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.INFO), Processors.logTo(logger, Slf4jLevel.INFO),
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.ERROR), Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named(
Processors.named("[console>]", "[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.DEBUG))); return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger)
return new RuntimeConfigBuilder() .processOutput(processOutput).artifactStore(getArtifactStore(logger,
.defaultsWithLogger(Command.MongoD, EMBEDDED_MONGO_LOGGER) downloadConfigBuilderCustomizers.orderedStream()))
.processOutput(processOutput) .build();
.artifactStore(getArtifactStore(embeddedMongoDownloadConfig)).build();
} }
@Bean private ArtifactStoreBuilder getArtifactStore(Logger logger,
@ConditionalOnMissingBean Stream<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
public IDownloadConfig embeddedMongoDownloadConfig(
ObjectProvider<EmbeddedMongoDownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizer) {
DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder() DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder()
.defaultsForCommand(Command.MongoD); .defaultsForCommand(Command.MongoD);
downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger));
downloadConfigBuilder downloadConfigBuilderCustomizers
.progressListener(new Slf4jProgressListener(EMBEDDED_MONGO_LOGGER)); .forEach((customizer) -> customizer.customize(downloadConfigBuilder));
IDownloadConfig downloadConfig = downloadConfigBuilder.build();
downloadConfigBuilderCustomizer.stream()
.forEach((c) -> c.customize(downloadConfigBuilder));
return downloadConfigBuilder.build();
}
private ArtifactStoreBuilder getArtifactStore(IDownloadConfig downloadConfig) {
return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD) return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD)
.download(downloadConfig); .download(downloadConfig);
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
@ -22,19 +22,19 @@ import java.util.EnumSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder;
import de.flapdoodle.embed.mongo.config.IMongodConfig; import de.flapdoodle.embed.mongo.config.IMongodConfig;
import de.flapdoodle.embed.mongo.config.Storage; import de.flapdoodle.embed.mongo.config.Storage;
import de.flapdoodle.embed.mongo.distribution.Feature; import de.flapdoodle.embed.mongo.distribution.Feature;
import de.flapdoodle.embed.mongo.distribution.Version; import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.config.IRuntimeConfig;
import de.flapdoodle.embed.process.config.store.IDownloadConfig; import de.flapdoodle.embed.process.config.store.IDownloadConfig;
import de.flapdoodle.embed.process.io.progress.Slf4jProgressListener;
import org.bson.Document; import org.bson.Document;
import org.junit.After; import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
@ -184,27 +184,12 @@ public class EmbeddedMongoAutoConfigurationTests {
} }
@Test @Test
public void defaultDownloadConfiguration() { public void customizeDownloadConfiguration() {
load(); load(DownloadConfigBuilderCustomizerConfiguration.class);
IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class); IRuntimeConfig runtimeConfig = this.context.getBean(IRuntimeConfig.class);
IDownloadConfig downloadConfig = (IDownloadConfig) new DirectFieldAccessor(
assertThat(downloadConfig.getDownloadPath().getClass().getSimpleName()) runtimeConfig.getArtifactStore()).getPropertyValue("downloadConfig");
.isEqualTo("PlatformDependentDownloadPath");
assertThat(downloadConfig.getUserAgent()).isEqualTo(
"Mozilla/5.0 (compatible; Embedded MongoDB; +https://github.com/flapdoodle-oss/embedmongo.flapdoodle.de)");
assertThat(downloadConfig.getProgressListener())
.isInstanceOf(Slf4jProgressListener.class);
}
@Test
public void customizedDownloadConfiguration() {
load(TestEmbeddedMongoDownloadConfigBuilderCustomizer.class);
IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class);
assertThat(downloadConfig.getDownloadPath().getPath(null)).isEqualTo("test");
assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent"); assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent");
assertThat(downloadConfig.getProgressListener())
.isInstanceOf(Slf4jProgressListener.class);
} }
private void assertVersionConfiguration(String configuredVersion, private void assertVersionConfiguration(String configuredVersion,
@ -254,13 +239,14 @@ public class EmbeddedMongoAutoConfigurationTests {
} }
private static class TestEmbeddedMongoDownloadConfigBuilderCustomizer @Configuration
implements EmbeddedMongoDownloadConfigBuilderCustomizer { static class DownloadConfigBuilderCustomizerConfiguration {
@Override @Bean
public void customize(DownloadConfigBuilder downloadConfigBuilder) { public DownloadConfigBuilderCustomizer testDownloadConfigBuilderCustomizer() {
downloadConfigBuilder.downloadPath("test"); return (downloadConfigBuilder) -> {
downloadConfigBuilder.userAgent("Test User Agent"); downloadConfigBuilder.userAgent("Test User Agent");
};
} }
} }

@ -4292,7 +4292,8 @@ If you have SLF4J on the classpath, the output produced by Mongo is automaticall
to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`. to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`.
You can declare your own `IMongodConfig` and `IRuntimeConfig` beans to take control of You can declare your own `IMongodConfig` and `IRuntimeConfig` beans to take control of
the Mongo instance's configuration and logging routing. the Mongo instance's configuration and logging routing. The download configuration can be
customized by declaring a `DownloadConfigBuilderCustomizer` bean.

Loading…
Cancel
Save