Browse Source
This commit removes auto-configuration and dependency management for Flapdoodle embedded MongoDB in favor of the Spring Boot support provided by Flapdoodle. Closes gh-30863pull/30893/head
24 changed files with 8 additions and 889 deletions
@ -1,48 +0,0 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -1,49 +0,0 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
@ -1,40 +0,0 @@
@@ -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); |
||||
|
||||
} |
||||
@ -1,249 +0,0 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -1,98 +0,0 @@
@@ -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; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -1,20 +0,0 @@
@@ -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; |
||||
@ -1,269 +0,0 @@
@@ -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); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
4
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatamongodb/withoutdb/MyDataMongoDbTests.java → spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatamongodb/MyDataMongoDbTests.java
4
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatamongodb/withoutdb/MyDataMongoDbTests.java → spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatamongodb/MyDataMongoDbTests.java
@ -1,27 +0,0 @@
@@ -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 { |
||||
|
||||
// ...
|
||||
|
||||
} |
||||
2
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatamongodb/withoutdb/MyDataMongoDbTests.kt → spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatamongodb/MyDataMongoDbTests.kt
2
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatamongodb/withoutdb/MyDataMongoDbTests.kt → spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatamongodb/MyDataMongoDbTests.kt
@ -1,27 +0,0 @@
@@ -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 { |
||||
|
||||
// ... |
||||
|
||||
} |
||||
Loading…
Reference in new issue