Remove support for embedded MongoDB

This commit removes auto-configuration and dependency management
for Flapdoodle embedded MongoDB in favor of the Spring Boot support
provided by Flapdoodle.

Closes gh-30863
This commit is contained in:
Scott Frederick 2022-05-06 15:08:35 -05:00
parent 4a8e48f07a
commit 7e089a6b81
24 changed files with 8 additions and 889 deletions

View File

@ -26,7 +26,6 @@ dependencies {
optional("com.oracle.database.jdbc:ucp")
optional("com.samskivert:jmustache")
optional("com.sun.mail:jakarta.mail")
optional("de.flapdoodle.embed:de.flapdoodle.embed.mongo")
optional("io.lettuce:lettuce-core")
optional("io.projectreactor.netty:reactor-netty-http")
optional("io.r2dbc:r2dbc-spi")

View File

@ -1,48 +0,0 @@
/*
* Copyright 2012-2020 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
*
* https://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.data.mongo;
import com.mongodb.client.MongoClient;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
/**
* {@link BeanFactoryPostProcessor} to automatically set up the recommended
* {@link BeanDefinition#setDependsOn(String[]) dependsOn} configuration for Mongo clients
* when used embedded Mongo.
*
* @author Andy Wilkinson
* @since 1.3.0
*/
@Order(Ordered.LOWEST_PRECEDENCE)
public class MongoClientDependsOnBeanFactoryPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor {
/**
* Creates a new {@code MongoClientDependsOnBeanFactoryPostProcessor} that will set up
* dependencies upon beans with the given types.
* @param dependsOn types of the beans to depend upon
*/
public MongoClientDependsOnBeanFactoryPostProcessor(Class<?>... dependsOn) {
super(MongoClient.class, MongoClientFactoryBean.class, dependsOn);
}
}

View File

@ -1,49 +0,0 @@
/*
* 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
*
* https://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.data.mongo;
import com.mongodb.reactivestreams.client.MongoClient;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean;
/**
* {@link BeanFactoryPostProcessor} to automatically set up the recommended
* {@link BeanDefinition#setDependsOn(String[]) dependsOn} configuration for Mongo clients
* when used embedded Mongo.
*
* @author Mark Paluch
* @since 2.0.0
*/
@Order(Ordered.LOWEST_PRECEDENCE)
public class ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor
extends AbstractDependsOnBeanFactoryPostProcessor {
/**
* Creates a new {@code ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor}
* that will set up dependencies upon beans with the given types.
* @param dependsOn types of the beans to depend upon
*/
public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(Class<?>... dependsOn) {
super(MongoClient.class, ReactiveMongoClientFactoryBean.class, dependsOn);
}
}

View File

@ -1,40 +0,0 @@
/*
* Copyright 2012-2021 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
*
* https://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.process.config.store.DownloadConfig;
import de.flapdoodle.embed.process.config.store.ImmutableDownloadConfig;
/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link DownloadConfig} via an {@link ImmutableDownloadConfig.Builder} whilst retaining
* default auto-configuration.
*
* @author Michael Gmeiner
* @since 2.2.0
*/
@FunctionalInterface
public interface DownloadConfigBuilderCustomizer {
/**
* Customize the {@link ImmutableDownloadConfig.Builder}.
* @param downloadConfigBuilder the {@link ImmutableDownloadConfig.Builder} to
* customize
*/
void customize(ImmutableDownloadConfig.Builder downloadConfigBuilder);
}

View File

@ -1,249 +0,0 @@
/*
* Copyright 2012-2022 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
*
* https://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 java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import com.mongodb.MongoClientSettings;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.Defaults;
import de.flapdoodle.embed.mongo.config.ImmutableMongodConfig;
import de.flapdoodle.embed.mongo.config.MongodConfig;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.config.Storage;
import de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.mongo.distribution.Versions;
import de.flapdoodle.embed.mongo.packageresolver.Command;
import de.flapdoodle.embed.process.config.RuntimeConfig;
import de.flapdoodle.embed.process.config.process.ProcessOutput;
import de.flapdoodle.embed.process.config.store.DownloadConfig;
import de.flapdoodle.embed.process.config.store.ImmutableDownloadConfig;
import de.flapdoodle.embed.process.distribution.Version.GenericVersion;
import de.flapdoodle.embed.process.io.Processors;
import de.flapdoodle.embed.process.io.Slf4jLevel;
import de.flapdoodle.embed.process.io.progress.Slf4jProgressListener;
import de.flapdoodle.embed.process.runtime.Network;
import de.flapdoodle.embed.process.store.ExtractedArtifactStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.mongo.MongoClientDependsOnBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.data.mongo.ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration.EmbeddedMongoClientDependsOnBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration.EmbeddedReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
import org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean;
import org.springframework.util.Assert;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Embedded Mongo.
*
* @author Henryk Konsek
* @author Andy Wilkinson
* @author Yogesh Lonkar
* @author Mark Paluch
* @author Issam El-atif
* @author Paulius Dambrauskas
* @author Chris Bono
* @since 1.3.0
*/
@AutoConfiguration(before = MongoAutoConfiguration.class)
@EnableConfigurationProperties({ MongoProperties.class, EmbeddedMongoProperties.class })
@ConditionalOnClass({ MongoClientSettings.class, MongodStarter.class })
@Import({ EmbeddedMongoClientDependsOnBeanFactoryPostProcessor.class,
EmbeddedReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor.class })
public class EmbeddedMongoAutoConfiguration {
private static final byte[] IP4_LOOPBACK_ADDRESS = { 127, 0, 0, 1 };
private static final byte[] IP6_LOOPBACK_ADDRESS = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
private final MongoProperties properties;
public EmbeddedMongoAutoConfiguration(MongoProperties properties) {
this.properties = properties;
}
@Bean(initMethod = "start", destroyMethod = "stop")
@ConditionalOnMissingBean
public MongodExecutable embeddedMongoServer(MongodConfig mongodConfig, RuntimeConfig runtimeConfig,
ApplicationContext context) {
Integer configuredPort = this.properties.getPort();
if (configuredPort == null || configuredPort == 0) {
setEmbeddedPort(context, mongodConfig.net().getPort());
}
MongodStarter mongodStarter = getMongodStarter(runtimeConfig);
return mongodStarter.prepare(mongodConfig);
}
private MongodStarter getMongodStarter(RuntimeConfig runtimeConfig) {
if (runtimeConfig == null) {
return MongodStarter.getDefaultInstance();
}
return MongodStarter.getInstance(runtimeConfig);
}
@Bean
@ConditionalOnMissingBean
public MongodConfig embeddedMongoConfiguration(EmbeddedMongoProperties embeddedProperties) throws IOException {
ImmutableMongodConfig.Builder builder = MongodConfig.builder().version(determineVersion(embeddedProperties));
EmbeddedMongoProperties.Storage storage = embeddedProperties.getStorage();
if (storage != null) {
String databaseDir = storage.getDatabaseDir();
String replSetName = storage.getReplSetName();
int oplogSize = (storage.getOplogSize() != null) ? (int) storage.getOplogSize().toMegabytes() : 0;
builder.replication(new Storage(databaseDir, replSetName, oplogSize));
}
Integer configuredPort = this.properties.getPort();
if (configuredPort != null && configuredPort > 0) {
builder.net(new Net(getHost().getHostAddress(), configuredPort, Network.localhostIsIPv6()));
}
else {
builder.net(
new Net(getHost().getHostAddress(), Network.freeServerPort(getHost()), Network.localhostIsIPv6()));
}
return builder.build();
}
private IFeatureAwareVersion determineVersion(EmbeddedMongoProperties embeddedProperties) {
Assert.state(embeddedProperties.getVersion() != null, "Set the spring.mongodb.embedded.version property or "
+ "define your own MongodConfig bean to use embedded MongoDB");
for (Version version : Version.values()) {
if (version.asInDownloadPath().equals(embeddedProperties.getVersion())) {
return version;
}
}
return Versions.withFeatures(createEmbeddedMongoVersion(embeddedProperties));
}
private GenericVersion createEmbeddedMongoVersion(EmbeddedMongoProperties embeddedProperties) {
return de.flapdoodle.embed.process.distribution.Version.of(embeddedProperties.getVersion());
}
private InetAddress getHost() throws UnknownHostException {
if (this.properties.getHost() == null) {
return InetAddress.getByAddress(Network.localhostIsIPv6() ? IP6_LOOPBACK_ADDRESS : IP4_LOOPBACK_ADDRESS);
}
return InetAddress.getByName(this.properties.getHost());
}
private void setEmbeddedPort(ApplicationContext context, int port) {
setPortProperty(context, port);
}
private void setPortProperty(ApplicationContext currentContext, int port) {
if (currentContext instanceof ConfigurableApplicationContext) {
MutablePropertySources sources = ((ConfigurableApplicationContext) currentContext).getEnvironment()
.getPropertySources();
getMongoPorts(sources).put("local.mongo.port", port);
}
if (currentContext.getParent() != null) {
setPortProperty(currentContext.getParent(), port);
}
}
@SuppressWarnings("unchecked")
private Map<String, Object> getMongoPorts(MutablePropertySources sources) {
PropertySource<?> propertySource = sources.get("mongo.ports");
if (propertySource == null) {
propertySource = new MapPropertySource("mongo.ports", new HashMap<>());
sources.addFirst(propertySource);
}
return (Map<String, Object>) propertySource.getSource();
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Logger.class)
@ConditionalOnMissingBean(RuntimeConfig.class)
static class RuntimeConfigConfiguration {
@Bean
RuntimeConfig embeddedMongoRuntimeConfig(
ObjectProvider<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
Logger logger = LoggerFactory.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
ProcessOutput processOutput = ProcessOutput.builder().output(Processors.logTo(logger, Slf4jLevel.INFO))
.error(Processors.logTo(logger, Slf4jLevel.ERROR))
.commands(Processors.named("[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG))).build();
return Defaults.runtimeConfigFor(Command.MongoD, logger).processOutput(processOutput)
.artifactStore(getArtifactStore(logger, downloadConfigBuilderCustomizers.orderedStream()))
.isDaemonProcess(false).build();
}
private ExtractedArtifactStore getArtifactStore(Logger logger,
Stream<DownloadConfigBuilderCustomizer> downloadConfigBuilderCustomizers) {
ImmutableDownloadConfig.Builder downloadConfigBuilder = Defaults.downloadConfigFor(Command.MongoD);
downloadConfigBuilder.progressListener(new Slf4jProgressListener(logger));
downloadConfigBuilderCustomizers.forEach((customizer) -> customizer.customize(downloadConfigBuilder));
DownloadConfig downloadConfig = downloadConfigBuilder.build();
return Defaults.extractedArtifactStoreFor(Command.MongoD).withDownloadConfig(downloadConfig);
}
}
/**
* Post processor to ensure that {@link com.mongodb.client.MongoClient} beans depend
* on any {@link MongodExecutable} beans.
*/
@ConditionalOnClass({ com.mongodb.client.MongoClient.class, MongoClientFactoryBean.class })
static class EmbeddedMongoClientDependsOnBeanFactoryPostProcessor
extends MongoClientDependsOnBeanFactoryPostProcessor {
EmbeddedMongoClientDependsOnBeanFactoryPostProcessor() {
super(MongodExecutable.class);
}
}
/**
* Post processor to ensure that
* {@link com.mongodb.reactivestreams.client.MongoClient} beans depend on any
* {@link MongodExecutable} beans.
*/
@ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class, ReactiveMongoClientFactoryBean.class })
static class EmbeddedReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor
extends ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor {
EmbeddedReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor() {
super(MongodExecutable.class);
}
}
}

View File

@ -1,98 +0,0 @@
/*
* Copyright 2012-2022 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
*
* https://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 org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
/**
* Configuration properties for Embedded Mongo.
*
* @author Andy Wilkinson
* @author Yogesh Lonkar
* @author Chris Bono
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "spring.mongodb.embedded")
public class EmbeddedMongoProperties {
/**
* Version of Mongo to use.
*/
private String version;
private final Storage storage = new Storage();
public String getVersion() {
return this.version;
}
public void setVersion(String version) {
this.version = version;
}
public Storage getStorage() {
return this.storage;
}
public static class Storage {
/**
* Maximum size of the oplog.
*/
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize oplogSize;
/**
* Name of the replica set.
*/
private String replSetName;
/**
* Directory used for data storage.
*/
private String databaseDir;
public DataSize getOplogSize() {
return this.oplogSize;
}
public void setOplogSize(DataSize oplogSize) {
this.oplogSize = oplogSize;
}
public String getReplSetName() {
return this.replSetName;
}
public void setReplSetName(String replSetName) {
this.replSetName = replSetName;
}
public String getDatabaseDir() {
return this.databaseDir;
}
public void setDatabaseDir(String databaseDir) {
this.databaseDir = databaseDir;
}
}
}

View File

@ -1,20 +0,0 @@
/*
* 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
*
* https://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.
*/
/**
* Auto-configuration for embedded MongoDB.
*/
package org.springframework.boot.autoconfigure.mongo.embedded;

View File

@ -1684,13 +1684,6 @@
"type": "java.lang.Boolean",
"defaultValue": false
},
{
"name": "spring.mongodb.embedded.features",
"deprecation": {
"level": "error",
"reason": "Feature support has been removed from Embedded Mongo. A custom MongodConfig bean should be defined instead."
}
},
{
"name": "spring.mustache.prefix",
"defaultValue": "classpath:/templates/"

View File

@ -73,7 +73,6 @@ org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration

View File

@ -1,269 +0,0 @@
/*
* Copyright 2012-2022 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
*
* https://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 java.io.File;
import java.nio.file.Path;
import java.util.Map;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfig;
import de.flapdoodle.embed.mongo.config.Storage;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.config.RuntimeConfig;
import de.flapdoodle.embed.process.config.store.DownloadConfig;
import org.bson.Document;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.junit.DisabledOnOs;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Tests for {@link EmbeddedMongoAutoConfiguration}.
*
* @author Henryk Konsek
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Issam El-atif
* @author Chris Bono
*/
@DisabledOnOs(os = OS.LINUX, architecture = "aarch64",
disabledReason = "Embedded Mongo doesn't support Linux aarch64, see https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/379")
class EmbeddedMongoAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@AfterEach
void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
void noVersion() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.port=0").applyTo(this.context);
this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
EmbeddedMongoAutoConfiguration.class);
assertThatThrownBy(() -> this.context.refresh()).hasRootCauseExactlyInstanceOf(IllegalStateException.class)
.hasRootCauseMessage("Set the spring.mongodb.embedded.version property or define your own MongodConfig "
+ "bean to use embedded MongoDB");
}
@Test
void customVersion() {
String version = Version.V3_4_15.asInDownloadPath();
assertVersionConfiguration(version, version);
}
@Test
void customUnknownVersion() {
assertVersionConfiguration("3.4.1", "3.4.1");
}
@Test
void useRandomPortByDefault() {
loadWithValidVersion();
assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1);
MongoClient client = this.context.getBean(MongoClient.class);
Integer mongoPort = Integer.valueOf(this.context.getEnvironment().getProperty("local.mongo.port"));
assertThat(getPort(client)).isEqualTo(mongoPort);
}
@Test
void specifyPortToZeroAllocateRandomPort() {
loadWithValidVersion("spring.data.mongodb.port=0");
assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1);
MongoClient client = this.context.getBean(MongoClient.class);
Integer mongoPort = Integer.valueOf(this.context.getEnvironment().getProperty("local.mongo.port"));
assertThat(getPort(client)).isEqualTo(mongoPort);
}
@Test
void randomlyAllocatedPortIsAvailableWhenCreatingMongoClient() {
loadWithValidVersion(MongoClientConfiguration.class);
MongoClient client = this.context.getBean(MongoClient.class);
Integer mongoPort = Integer.valueOf(this.context.getEnvironment().getProperty("local.mongo.port"));
assertThat(getPort(client)).isEqualTo(mongoPort);
}
@Test
void portIsAvailableInParentContext() {
try (ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext()) {
TestPropertyValues.of("spring.mongodb.embedded.version=3.5.5").applyTo(parent);
parent.refresh();
this.context = new AnnotationConfigApplicationContext();
this.context.setParent(parent);
this.context.register(EmbeddedMongoAutoConfiguration.class, MongoClientConfiguration.class);
this.context.refresh();
assertThat(parent.getEnvironment().getProperty("local.mongo.port")).isNotNull();
}
}
@Test
void defaultStorageConfiguration() {
loadWithValidVersion(MongoClientConfiguration.class);
Storage replication = this.context.getBean(MongodConfig.class).replication();
assertThat(replication.getOplogSize()).isEqualTo(0);
assertThat(replication.getDatabaseDir()).isNull();
assertThat(replication.getReplSetName()).isNull();
}
@Test
void mongoWritesToCustomDatabaseDir(@TempDir Path temp) {
File customDatabaseDir = new File(temp.toFile(), "custom-database-dir");
FileSystemUtils.deleteRecursively(customDatabaseDir);
loadWithValidVersion("spring.mongodb.embedded.storage.databaseDir=" + customDatabaseDir.getPath());
assertThat(customDatabaseDir).isDirectory();
assertThat(customDatabaseDir.listFiles()).isNotEmpty();
}
@Test
void customOpLogSizeIsAppliedToConfiguration() {
loadWithValidVersion("spring.mongodb.embedded.storage.oplogSize=1024KB");
assertThat(this.context.getBean(MongodConfig.class).replication().getOplogSize()).isEqualTo(1);
}
@Test
void customOpLogSizeUsesMegabytesPerDefault() {
loadWithValidVersion("spring.mongodb.embedded.storage.oplogSize=10");
assertThat(this.context.getBean(MongodConfig.class).replication().getOplogSize()).isEqualTo(10);
}
@Test
void customReplicaSetNameIsAppliedToConfiguration() {
loadWithValidVersion("spring.mongodb.embedded.storage.replSetName=testing");
assertThat(this.context.getBean(MongodConfig.class).replication().getReplSetName()).isEqualTo("testing");
}
@Test
void customizeDownloadConfiguration() {
loadWithValidVersion(DownloadConfigBuilderCustomizerConfiguration.class);
RuntimeConfig runtimeConfig = this.context.getBean(RuntimeConfig.class);
DownloadConfig downloadConfig = (DownloadConfig) new DirectFieldAccessor(runtimeConfig.artifactStore())
.getPropertyValue("downloadConfig");
assertThat(downloadConfig.getUserAgent()).isEqualTo("Test User Agent");
}
@Test
void shutdownHookIsNotRegistered() {
loadWithValidVersion();
assertThat(this.context.getBean(MongodExecutable.class).isRegisteredJobKiller()).isFalse();
}
@Test
void customMongoServerConfiguration() {
loadWithValidVersion(CustomMongoConfiguration.class);
Map<String, MongoClient> mongoClients = this.context.getBeansOfType(MongoClient.class);
assertThat(mongoClients).isNotEmpty();
for (String mongoClientBeanName : mongoClients.keySet()) {
BeanDefinition beanDefinition = this.context.getBeanFactory().getBeanDefinition(mongoClientBeanName);
assertThat(beanDefinition.getDependsOn()).contains("customMongoServer");
}
}
private void assertVersionConfiguration(String configuredVersion, String expectedVersion) {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.port=0").applyTo(this.context);
if (configuredVersion != null) {
TestPropertyValues.of("spring.mongodb.embedded.version=" + configuredVersion).applyTo(this.context);
}
this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
EmbeddedMongoAutoConfiguration.class);
this.context.refresh();
MongoTemplate mongo = this.context.getBean(MongoTemplate.class);
Document buildInfo = mongo.executeCommand("{ buildInfo: 1 }");
assertThat(buildInfo.getString("version")).isEqualTo(expectedVersion);
}
private void loadWithValidVersion(String... environment) {
loadWithValidVersion(null, environment);
}
private void loadWithValidVersion(Class<?> config, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
if (config != null) {
ctx.register(config);
}
TestPropertyValues.of("spring.mongodb.embedded.version=3.5.5").applyTo(ctx);
TestPropertyValues.of(environment).applyTo(ctx);
ctx.register(EmbeddedMongoAutoConfiguration.class, MongoAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
private int getPort(MongoClient client) {
return client.getClusterDescription().getClusterSettings().getHosts().get(0).getPort();
}
@Configuration(proxyBeanMethods = false)
static class MongoClientConfiguration {
@Bean
MongoClient mongoClient(@Value("${local.mongo.port}") int port) {
return MongoClients.create("mongodb://localhost:" + port);
}
}
@Configuration(proxyBeanMethods = false)
static class DownloadConfigBuilderCustomizerConfiguration {
@Bean
DownloadConfigBuilderCustomizer testDownloadConfigBuilderCustomizer() {
return (downloadConfigBuilder) -> downloadConfigBuilder.userAgent("Test User Agent");
}
}
@Configuration(proxyBeanMethods = false)
static class CustomMongoConfiguration {
@Bean(initMethod = "start", destroyMethod = "stop")
MongodExecutable customMongoServer(RuntimeConfig runtimeConfig, MongodConfig mongodConfig) {
MongodStarter mongodStarter = MongodStarter.getInstance(runtimeConfig);
return mongodStarter.prepare(mongodConfig);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@ -54,8 +54,7 @@ class SessionAutoConfigurationMongoTests extends AbstractSessionAutoConfiguratio
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
SessionAutoConfiguration.class))
.withPropertyValues("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl(),
"spring.mongodb.embedded.version=3.5.5");
.withPropertyValues("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl());
@Test
void defaultConfig() {

View File

@ -240,13 +240,6 @@ bom {
]
}
}
library("Embedded Mongo", "3.4.5") {
group("de.flapdoodle.embed") {
modules = [
"de.flapdoodle.embed.mongo"
]
}
}
library("Flyway", "8.5.9") {
group("org.flywaydb") {
modules = [

View File

@ -265,8 +265,7 @@ tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {
if (securityVersion.endsWith("-SNAPSHOT")) {
securityVersion = securityVersion.substring(0, securityVersion.length() - "-SNAPSHOT".length())
}
attributes "embedded-mongo-version": versionConstraints["de.flapdoodle.embed:de.flapdoodle.embed.mongo"],
"jetty-version": versionConstraints["org.eclipse.jetty:jetty-server"],
attributes "jetty-version": versionConstraints["org.eclipse.jetty:jetty-server"],
"jooq-version": versionConstraints["org.jooq:jooq"],
"lettuce-version": versionConstraints["io.lettuce:lettuce-core"],
"spring-amqp-version": versionConstraints["org.springframework.amqp:spring-amqp"],

View File

@ -249,7 +249,6 @@ boot-features-connecting-to-mongodb=features.nosql.mongodb.connecting
boot-features-mongo-template=features.nosql.mongodb.template
boot-features-spring-data-mongodb-repositories=features.nosql.mongodb.repositories
boot-features-spring-data-mongo-repositories=features.nosql.mongodb.repositories
boot-features-mongo-embedded=features.nosql.mongodb.embedded
boot-features-neo4j=features.nosql.neo4j
boot-features-connecting-to-neo4j=features.nosql.neo4j.connecting
boot-features-spring-data-neo4j-repositories=features.nosql.neo4j.repositories
@ -806,7 +805,6 @@ features.nosql.mongodb=data.nosql.mongodb
features.nosql.mongodb.connecting=data.nosql.mongodb.connecting
features.nosql.mongodb.template=data.nosql.mongodb.template
features.nosql.mongodb.repositories=data.nosql.mongodb.repositories
features.nosql.mongodb.embedded=data.nosql.mongodb.embedded
features.nosql.neo4j=data.nosql.neo4j
features.nosql.neo4j.connecting=data.nosql.neo4j.connecting
features.nosql.neo4j.repositories=data.nosql.neo4j.repositories

View File

@ -96,7 +96,6 @@
:ant-docs: https://ant.apache.org/manual
:dependency-management-plugin-code: https://github.com/spring-gradle-plugins/dependency-management-plugin
:dynatrace-help: https://www.dynatrace.com/support/help
:embedded-mongo-code: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/de.flapdoodle.embed.mongo-{embedded-mongo-version}
:gradle-docs: https://docs.gradle.org/current/userguide
:hibernate-docs: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html
:java-api: https://docs.oracle.com/javase/8/docs/api

View File

@ -136,27 +136,6 @@ TIP: For complete details of Spring Data MongoDB, including its rich object mapp
[[data.nosql.mongodb.embedded]]
==== Embedded Mongo
Spring Boot offers auto-configuration for https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo[Embedded Mongo].
To use it in your Spring Boot application, add a dependency on `de.flapdoodle.embed:de.flapdoodle.embed.mongo` and set the configprop:spring.mongodb.embedded.version[] property to match the version of MongoDB that your application will use in production.
NOTE: The default download configuration allows access to most of the versions listed in {embedded-mongo-code}/src/main/java/de/flapdoodle/embed/mongo/distribution/Version.java[Embedded Mongo's `Version` class] as well as some others.
Configuring an inaccessible version will result in an error when attempting to download the server.
Such an error can be corrected by defining an appropriately configured `DownloadConfigBuilderCustomizer` bean.
The port that Mongo listens on can be configured by setting the configprop:spring.data.mongodb.port[] property.
To use a randomly allocated free port, use a value of 0.
The `MongoClient` created by `MongoAutoConfiguration` is automatically configured to use the randomly allocated port.
NOTE: If you do not configure a custom port, the embedded support uses a random port (rather than 27017) by default.
If you have SLF4J on the classpath, the output produced by Mongo is automatically routed to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`.
You can declare your own `IMongodConfig` and `IRuntimeConfig` beans to take control of the Mongo instance's configuration and logging routing.
The download configuration can be customized by declaring a `DownloadConfigBuilderCustomizer` bean.
[[data.nosql.neo4j]]
=== Neo4j
https://neo4j.com/[Neo4j] is an open-source NoSQL graph database that uses a rich data model of nodes connected by first class relationships, which is better suited for connected big data than traditional RDBMS approaches.

View File

@ -558,14 +558,7 @@ TIP: A list of the auto-configuration settings that are enabled by `@DataMongoTe
The following class shows the `@DataMongoTest` annotation in use:
include::code:withoutdb/MyDataMongoDbTests[]
In-memory embedded MongoDB generally works well for tests, since it is fast and does not require any developer installation.
If, however, you prefer to run tests against a real MongoDB server, you should exclude the embedded MongoDB auto-configuration, as shown in the following example:
include::code:withdb/MyDataMongoDbTests[]
include::code:MyDataMongoDbTests[]
[[features.testing.spring-boot-applications.autoconfigured-spring-data-neo4j]]
==== Auto-configured Data Neo4j Tests

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdatamongodb.withoutdb;
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdatamongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;

View File

@ -1,27 +0,0 @@
/*
* Copyright 2012-2021 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
*
* https://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.docs.features.testing.springbootapplications.autoconfiguredspringdatamongodb.withdb;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
class MyDataMongoDbTests {
// ...
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdatamongodb.withoutdb
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdatamongodb
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest

View File

@ -1,27 +0,0 @@
/*
* Copyright 2012-2022 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
*
* https://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.docs.features.testing.springbootapplications.autoconfiguredspringdatamongodb.withdb
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest
@DataMongoTest(excludeAutoConfiguration = [EmbeddedMongoAutoConfiguration::class])
class MyDataMongoDbTests {
// ...
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 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.
@ -43,9 +43,6 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* Using this annotation will disable full auto-configuration and instead apply only
* configuration relevant to MongoDB tests.
* <p>
* By default, tests annotated with {@code @DataMongoTest} will use an embedded in-memory
* MongoDB process (if available).
* <p>
* When using JUnit 4, this annotation should be used in combination with
* {@code @RunWith(SpringRunner.class)}.
*

View File

@ -5,5 +5,4 @@ org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoC
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

View File

@ -36,7 +36,6 @@
<suppress files="Direction\.java" checks="JavadocVariable" />
<suppress files="LoggingApplicationListenerIntegrationTests\.java" checks="IllegalImport" />
<suppress files="SpringBootJoranConfiguratorTests\.java" checks="IllegalImport" />
<suppress files="EmbeddedMongoAutoConfiguration\.java" checks="IllegalImport" />
<suppress files="LogbackMetricsAutoConfiguration\.java" checks="IllegalImport" />
<suppress files="RemoteUrlPropertyExtractorTests\.java" checks="IllegalImport" />
<suppress files="SampleLogbackApplication\.java" checks="IllegalImport" />