Upgrade to Embedded Mongo 3.0.0

See gh-24254
pull/24582/head
paulius 4 years ago committed by Stephane Nicoll
parent bb78b6ab00
commit 2f6b51babd

@ -16,13 +16,13 @@
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.process.config.store.DownloadConfig;
import de.flapdoodle.embed.process.config.store.IDownloadConfig; import de.flapdoodle.embed.process.config.store.ImmutableDownloadConfig;
/** /**
* Callback interface that can be implemented by beans wishing to customize the * Callback interface that can be implemented by beans wishing to customize the
* {@link IDownloadConfig} via a {@link DownloadConfigBuilder} whilst retaining default * {@link DownloadConfig} via a {@link ImmutableDownloadConfig.Builder} whilst retaining
* auto-configuration. * default auto-configuration.
* *
* @author Michael Gmeiner * @author Michael Gmeiner
* @since 2.2.0 * @since 2.2.0
@ -31,9 +31,10 @@ import de.flapdoodle.embed.process.config.store.IDownloadConfig;
public interface DownloadConfigBuilderCustomizer { public interface DownloadConfigBuilderCustomizer {
/** /**
* Customize the {@link DownloadConfigBuilder}. * Customize the {@link ImmutableDownloadConfig.Builder}.
* @param downloadConfigBuilder the {@link DownloadConfigBuilder} to customize * @param downloadConfigBuilder the {@link ImmutableDownloadConfig.Builder} to
* customize
*/ */
void customize(DownloadConfigBuilder downloadConfigBuilder); void customize(ImmutableDownloadConfig.Builder downloadConfigBuilder);
} }

@ -27,26 +27,24 @@ import com.mongodb.MongoClientSettings;
import de.flapdoodle.embed.mongo.Command; import de.flapdoodle.embed.mongo.Command;
import de.flapdoodle.embed.mongo.MongodExecutable; import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodStarter; import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.DownloadConfigBuilder; import de.flapdoodle.embed.mongo.config.Defaults;
import de.flapdoodle.embed.mongo.config.ExtractedArtifactStoreBuilder; import de.flapdoodle.embed.mongo.config.ImmutableMongodConfig;
import de.flapdoodle.embed.mongo.config.IMongodConfig; import de.flapdoodle.embed.mongo.config.MongodConfig;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net; import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder;
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.IFeatureAwareVersion; import de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion;
import de.flapdoodle.embed.mongo.distribution.Version; 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.RuntimeConfig;
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.config.store.DownloadConfig;
import de.flapdoodle.embed.process.distribution.GenericVersion; import de.flapdoodle.embed.process.config.store.ImmutableDownloadConfig;
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;
import de.flapdoodle.embed.process.io.progress.Slf4jProgressListener; import de.flapdoodle.embed.process.io.progress.Slf4jProgressListener;
import de.flapdoodle.embed.process.runtime.Network; import de.flapdoodle.embed.process.runtime.Network;
import de.flapdoodle.embed.process.store.ArtifactStoreBuilder; import de.flapdoodle.embed.process.store.ExtractedArtifactStore;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -81,6 +79,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean;
* @author Yogesh Lonkar * @author Yogesh Lonkar
* @author Mark Paluch * @author Mark Paluch
* @author Issam El-atif * @author Issam El-atif
* @author Paulius Dambrauskas
* @since 1.3.0 * @since 1.3.0
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ -97,14 +96,14 @@ public class EmbeddedMongoAutoConfiguration {
private final MongoProperties properties; private final MongoProperties properties;
public EmbeddedMongoAutoConfiguration(MongoProperties properties, EmbeddedMongoProperties embeddedProperties) { public EmbeddedMongoAutoConfiguration(MongoProperties properties) {
this.properties = properties; this.properties = properties;
} }
@Bean(initMethod = "start", destroyMethod = "stop") @Bean(initMethod = "start", destroyMethod = "stop")
@ConditionalOnMissingBean @ConditionalOnMissingBean
public MongodExecutable embeddedMongoServer(IMongodConfig mongodConfig, IRuntimeConfig runtimeConfig, public MongodExecutable embeddedMongoServer(MongodConfig mongodConfig, RuntimeConfig runtimeConfig,
ApplicationContext context) throws IOException { ApplicationContext context) {
Integer configuredPort = this.properties.getPort(); Integer configuredPort = this.properties.getPort();
if (configuredPort == null || configuredPort == 0) { if (configuredPort == null || configuredPort == 0) {
setEmbeddedPort(context, mongodConfig.net().getPort()); setEmbeddedPort(context, mongodConfig.net().getPort());
@ -113,7 +112,7 @@ public class EmbeddedMongoAutoConfiguration {
return mongodStarter.prepare(mongodConfig); return mongodStarter.prepare(mongodConfig);
} }
private MongodStarter getMongodStarter(IRuntimeConfig runtimeConfig) { private MongodStarter getMongodStarter(RuntimeConfig runtimeConfig) {
if (runtimeConfig == null) { if (runtimeConfig == null) {
return MongodStarter.getDefaultInstance(); return MongodStarter.getDefaultInstance();
} }
@ -122,8 +121,8 @@ public class EmbeddedMongoAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public IMongodConfig embeddedMongoConfiguration(EmbeddedMongoProperties embeddedProperties) throws IOException { public MongodConfig embeddedMongoConfiguration(EmbeddedMongoProperties embeddedProperties) throws IOException {
MongodConfigBuilder builder = new MongodConfigBuilder().version(determineVersion(embeddedProperties)); ImmutableMongodConfig.Builder builder = MongodConfig.builder().version(determineVersion(embeddedProperties));
EmbeddedMongoProperties.Storage storage = embeddedProperties.getStorage(); EmbeddedMongoProperties.Storage storage = embeddedProperties.getStorage();
if (storage != null) { if (storage != null) {
String databaseDir = storage.getDatabaseDir(); String databaseDir = storage.getDatabaseDir();
@ -149,9 +148,11 @@ public class EmbeddedMongoAutoConfiguration {
return version; return version;
} }
} }
return Versions.withFeatures(new GenericVersion(embeddedProperties.getVersion())); return Versions
.withFeatures(de.flapdoodle.embed.process.distribution.Version.of(embeddedProperties.getVersion()));
} }
return Versions.withFeatures(new GenericVersion(embeddedProperties.getVersion()), return Versions.withFeatures(
de.flapdoodle.embed.process.distribution.Version.of(embeddedProperties.getVersion()),
embeddedProperties.getFeatures().toArray(new Feature[0])); embeddedProperties.getFeatures().toArray(new Feature[0]));
} }
@ -189,29 +190,29 @@ public class EmbeddedMongoAutoConfiguration {
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Logger.class) @ConditionalOnClass(Logger.class)
@ConditionalOnMissingBean(IRuntimeConfig.class) @ConditionalOnMissingBean(RuntimeConfig.class)
static class RuntimeConfigConfiguration { static class RuntimeConfigConfiguration {
@Bean @Bean
IRuntimeConfig embeddedMongoRuntimeConfig( RuntimeConfig embeddedMongoRuntimeConfig(
ObjectProvider<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) { ObjectProvider<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
Logger logger = LoggerFactory.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo"); Logger logger = LoggerFactory.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
ProcessOutput processOutput = new ProcessOutput(Processors.logTo(logger, Slf4jLevel.INFO), ProcessOutput processOutput = new ProcessOutput(Processors.logTo(logger, Slf4jLevel.INFO),
Processors.logTo(logger, Slf4jLevel.ERROR), Processors.logTo(logger, Slf4jLevel.ERROR),
Processors.named("[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG))); Processors.named("[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger).processOutput(processOutput)
return Defaults.runtimeConfigFor(Command.MongoD, logger).processOutput(processOutput)
.artifactStore(getArtifactStore(logger, downloadConfigBuilderCustomizers.orderedStream())) .artifactStore(getArtifactStore(logger, downloadConfigBuilderCustomizers.orderedStream()))
.daemonProcess(false).build(); .isDaemonProcess(false).build();
} }
private ArtifactStoreBuilder getArtifactStore(Logger logger, private ExtractedArtifactStore getArtifactStore(Logger logger,
Stream<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) { Stream<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder() ImmutableDownloadConfig.Builder downloadConfigBuilder = Defaults.downloadConfigFor(Command.MongoD);
.defaultsForCommand(Command.MongoD);
downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger)); downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger));
downloadConfigBuilderCustomizers.forEach((customizer) -> customizer.customize(downloadConfigBuilder)); downloadConfigBuilderCustomizers.forEach((customizer) -> customizer.customize(downloadConfigBuilder));
IDownloadConfig downloadConfig = downloadConfigBuilder.build(); DownloadConfig downloadConfig = downloadConfigBuilder.build();
return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD).download(downloadConfig); return Defaults.extractedArtifactStoreFor(Command.MongoD).withDownloadConfig(downloadConfig);
} }
} }

@ -27,12 +27,12 @@ import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClients;
import de.flapdoodle.embed.mongo.MongodExecutable; import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodStarter; import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.IMongodConfig; import de.flapdoodle.embed.mongo.config.MongodConfig;
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.RuntimeConfig;
import de.flapdoodle.embed.process.config.store.IDownloadConfig; import de.flapdoodle.embed.process.config.store.DownloadConfig;
import org.bson.Document; import org.bson.Document;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -143,7 +143,7 @@ class EmbeddedMongoAutoConfigurationTests {
@Test @Test
void defaultStorageConfiguration() { void defaultStorageConfiguration() {
load(MongoClientConfiguration.class); load(MongoClientConfiguration.class);
Storage replication = this.context.getBean(IMongodConfig.class).replication(); Storage replication = this.context.getBean(MongodConfig.class).replication();
assertThat(replication.getOplogSize()).isEqualTo(0); assertThat(replication.getOplogSize()).isEqualTo(0);
assertThat(replication.getDatabaseDir()).isNull(); assertThat(replication.getDatabaseDir()).isNull();
assertThat(replication.getReplSetName()).isNull(); assertThat(replication.getReplSetName()).isNull();
@ -161,26 +161,26 @@ class EmbeddedMongoAutoConfigurationTests {
@Test @Test
void customOpLogSizeIsAppliedToConfiguration() { void customOpLogSizeIsAppliedToConfiguration() {
load("spring.mongodb.embedded.storage.oplogSize=1024KB"); load("spring.mongodb.embedded.storage.oplogSize=1024KB");
assertThat(this.context.getBean(IMongodConfig.class).replication().getOplogSize()).isEqualTo(1); assertThat(this.context.getBean(MongodConfig.class).replication().getOplogSize()).isEqualTo(1);
} }
@Test @Test
void customOpLogSizeUsesMegabytesPerDefault() { void customOpLogSizeUsesMegabytesPerDefault() {
load("spring.mongodb.embedded.storage.oplogSize=10"); load("spring.mongodb.embedded.storage.oplogSize=10");
assertThat(this.context.getBean(IMongodConfig.class).replication().getOplogSize()).isEqualTo(10); assertThat(this.context.getBean(MongodConfig.class).replication().getOplogSize()).isEqualTo(10);
} }
@Test @Test
void customReplicaSetNameIsAppliedToConfiguration() { void customReplicaSetNameIsAppliedToConfiguration() {
load("spring.mongodb.embedded.storage.replSetName=testing"); load("spring.mongodb.embedded.storage.replSetName=testing");
assertThat(this.context.getBean(IMongodConfig.class).replication().getReplSetName()).isEqualTo("testing"); assertThat(this.context.getBean(MongodConfig.class).replication().getReplSetName()).isEqualTo("testing");
} }
@Test @Test
void customizeDownloadConfiguration() { void customizeDownloadConfiguration() {
load(DownloadConfigBuilderCustomizerConfiguration.class); load(DownloadConfigBuilderCustomizerConfiguration.class);
IRuntimeConfig runtimeConfig = this.context.getBean(IRuntimeConfig.class); RuntimeConfig runtimeConfig = this.context.getBean(RuntimeConfig.class);
IDownloadConfig downloadConfig = (IDownloadConfig) new DirectFieldAccessor(runtimeConfig.getArtifactStore()) DownloadConfig downloadConfig = (DownloadConfig) new DirectFieldAccessor(runtimeConfig.artifactStore())
.getPropertyValue("downloadConfig"); .getPropertyValue("downloadConfig");
assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent"); assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent");
} }
@ -265,7 +265,7 @@ class EmbeddedMongoAutoConfigurationTests {
static class CustomMongoConfiguration { static class CustomMongoConfiguration {
@Bean(initMethod = "start", destroyMethod = "stop") @Bean(initMethod = "start", destroyMethod = "stop")
MongodExecutable customMongoServer(IRuntimeConfig runtimeConfig, IMongodConfig mongodConfig) { MongodExecutable customMongoServer(RuntimeConfig runtimeConfig, MongodConfig mongodConfig) {
MongodStarter mongodStarter = MongodStarter.getInstance(runtimeConfig); MongodStarter mongodStarter = MongodStarter.getInstance(runtimeConfig);
return mongodStarter.prepare(mongodConfig); return mongodStarter.prepare(mongodConfig);
} }

@ -300,7 +300,7 @@ bom {
] ]
} }
} }
library("Embedded Mongo", "2.2.0") { library("Embedded Mongo", "3.0.0") {
group("de.flapdoodle.embed") { group("de.flapdoodle.embed") {
modules = [ modules = [
"de.flapdoodle.embed.mongo" "de.flapdoodle.embed.mongo"

Loading…
Cancel
Save