Merge pull request #15496 from mgmeiner

* pr/15496:
  Polish "Allow easy customization of EmbeddedMongo DownloadConfig"
  Allow easy customization of EmbeddedMongo DownloadConfig
pull/15603/head
Stephane Nicoll 6 years ago
commit 08757d77f5

@ -0,0 +1,39 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.mongo.embedded;
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
* {@link IDownloadConfig} via a {@link DownloadConfigBuilder} whilst retaining default
* auto-configuration.
*
* @author Michael Gmeiner
* @since 2.2.0
*/
@FunctionalInterface
public interface DownloadConfigBuilderCustomizer {
/**
* Customize the {@link DownloadConfigBuilder}.
* @param downloadConfigBuilder the {@link DownloadConfigBuilder} to customize
*/
void customize(DownloadConfigBuilder 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;
@ -39,6 +40,7 @@ import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.mongo.distribution.Versions; import de.flapdoodle.embed.mongo.distribution.Versions;
import de.flapdoodle.embed.process.config.IRuntimeConfig; import de.flapdoodle.embed.process.config.IRuntimeConfig;
import de.flapdoodle.embed.process.config.io.ProcessOutput; import de.flapdoodle.embed.process.config.io.ProcessOutput;
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
import de.flapdoodle.embed.process.distribution.GenericVersion; import de.flapdoodle.embed.process.distribution.GenericVersion;
import de.flapdoodle.embed.process.io.Processors; import de.flapdoodle.embed.process.io.Processors;
import de.flapdoodle.embed.process.io.Slf4jLevel; import de.flapdoodle.embed.process.io.Slf4jLevel;
@ -48,6 +50,7 @@ import de.flapdoodle.embed.process.store.ArtifactStoreBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -203,7 +206,8 @@ public class EmbeddedMongoAutoConfiguration {
static class RuntimeConfigConfiguration { static class RuntimeConfigConfiguration {
@Bean @Bean
public IRuntimeConfig embeddedMongoRuntimeConfig() { public IRuntimeConfig embeddedMongoRuntimeConfig(
ObjectProvider<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
Logger logger = LoggerFactory Logger logger = LoggerFactory
.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo"); .getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
ProcessOutput processOutput = new ProcessOutput( ProcessOutput processOutput = new ProcessOutput(
@ -211,15 +215,21 @@ public class EmbeddedMongoAutoConfiguration {
Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named( Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named(
"[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG))); "[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger) return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger)
.processOutput(processOutput).artifactStore(getArtifactStore(logger)) .processOutput(processOutput).artifactStore(getArtifactStore(logger,
downloadConfigBuilderCustomizers.orderedStream()))
.build(); .build();
} }
private ArtifactStoreBuilder getArtifactStore(Logger logger) { private ArtifactStoreBuilder getArtifactStore(Logger logger,
Stream<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder()
.defaultsForCommand(Command.MongoD);
downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger));
downloadConfigBuilderCustomizers
.forEach((customizer) -> customizer.customize(downloadConfigBuilder));
IDownloadConfig downloadConfig = downloadConfigBuilder.build();
return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD) return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD)
.download(new DownloadConfigBuilder() .download(downloadConfig);
.defaultsForCommand(Command.MongoD)
.progressListener(new Slf4jProgressListener(logger)).build());
} }
} }

@ -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.
@ -26,12 +26,15 @@ 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 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;
@ -180,6 +183,15 @@ public class EmbeddedMongoAutoConfigurationTests {
.isEqualTo("testing"); .isEqualTo("testing");
} }
@Test
public void customizeDownloadConfiguration() {
load(DownloadConfigBuilderCustomizerConfiguration.class);
IRuntimeConfig runtimeConfig = this.context.getBean(IRuntimeConfig.class);
IDownloadConfig downloadConfig = (IDownloadConfig) new DirectFieldAccessor(
runtimeConfig.getArtifactStore()).getPropertyValue("downloadConfig");
assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent");
}
private void assertVersionConfiguration(String configuredVersion, private void assertVersionConfiguration(String configuredVersion,
String expectedVersion) { String expectedVersion) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
@ -227,4 +239,16 @@ public class EmbeddedMongoAutoConfigurationTests {
} }
@Configuration
static class DownloadConfigBuilderCustomizerConfiguration {
@Bean
public DownloadConfigBuilderCustomizer testDownloadConfigBuilderCustomizer() {
return (downloadConfigBuilder) -> {
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