Allow easy customization of EmbeddedMongo DownloadConfig
See gh-15496
This commit is contained in:
parent
4f207d2426
commit
b5b6889601
|
@ -39,6 +39,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 +49,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;
|
||||||
|
@ -202,24 +204,43 @@ 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(
|
||||||
Logger logger = LoggerFactory
|
IDownloadConfig embeddedMongoDownloadConfig) {
|
||||||
.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
|
|
||||||
ProcessOutput processOutput = new ProcessOutput(
|
ProcessOutput processOutput = new ProcessOutput(
|
||||||
Processors.logTo(logger, Slf4jLevel.INFO),
|
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.INFO),
|
||||||
Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named(
|
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.ERROR),
|
||||||
"[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
|
Processors.named("[console>]",
|
||||||
return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger)
|
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.DEBUG)));
|
||||||
.processOutput(processOutput).artifactStore(getArtifactStore(logger))
|
return new RuntimeConfigBuilder()
|
||||||
.build();
|
.defaultsWithLogger(Command.MongoD, EMBEDDED_MONGO_LOGGER)
|
||||||
|
.processOutput(processOutput)
|
||||||
|
.artifactStore(getArtifactStore(embeddedMongoDownloadConfig)).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArtifactStoreBuilder getArtifactStore(Logger logger) {
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public IDownloadConfig embeddedMongoDownloadConfig(
|
||||||
|
ObjectProvider<EmbeddedMongoDownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizer) {
|
||||||
|
DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder()
|
||||||
|
.defaultsForCommand(Command.MongoD);
|
||||||
|
|
||||||
|
downloadConfigBuilder
|
||||||
|
.progressListener(new Slf4jProgressListener(EMBEDDED_MONGO_LOGGER));
|
||||||
|
|
||||||
|
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(new DownloadConfigBuilder()
|
.download(downloadConfig);
|
||||||
.defaultsForCommand(Command.MongoD)
|
|
||||||
.progressListener(new Slf4jProgressListener(logger)).build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2018 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback interface that can be implemented by beans wishing to customize the
|
||||||
|
* EmbeddedMongo {@link DownloadConfigBuilder} outcome whilst retaining default
|
||||||
|
* auto-configuration.
|
||||||
|
*
|
||||||
|
* @author Michael Gmeiner
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface EmbeddedMongoDownloadConfigBuilderCustomizer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customize the {@link DownloadConfigBuilder}.
|
||||||
|
* @param downloadConfigBuilder the {@link DownloadConfigBuilder} to customize
|
||||||
|
*/
|
||||||
|
void customize(DownloadConfigBuilder downloadConfigBuilder);
|
||||||
|
|
||||||
|
}
|
|
@ -22,10 +22,13 @@ 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.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;
|
||||||
|
@ -180,6 +183,30 @@ public class EmbeddedMongoAutoConfigurationTests {
|
||||||
.isEqualTo("testing");
|
.isEqualTo("testing");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void defaultDownloadConfiguration() {
|
||||||
|
load();
|
||||||
|
IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class);
|
||||||
|
|
||||||
|
assertThat(downloadConfig.getDownloadPath().getClass().getSimpleName())
|
||||||
|
.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.getProgressListener())
|
||||||
|
.isInstanceOf(Slf4jProgressListener.class);
|
||||||
|
}
|
||||||
|
|
||||||
private void assertVersionConfiguration(String configuredVersion,
|
private void assertVersionConfiguration(String configuredVersion,
|
||||||
String expectedVersion) {
|
String expectedVersion) {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
@ -227,4 +254,15 @@ public class EmbeddedMongoAutoConfigurationTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class TestEmbeddedMongoDownloadConfigBuilderCustomizer
|
||||||
|
implements EmbeddedMongoDownloadConfigBuilderCustomizer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void customize(DownloadConfigBuilder downloadConfigBuilder) {
|
||||||
|
downloadConfigBuilder.downloadPath("test");
|
||||||
|
downloadConfigBuilder.userAgent("Test User Agent");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue