Merge pull request #27108 from bono007

* gh-27108:
  Polish "Remove default spring.mongodb.embedded.version"
  Remove default spring.mongodb.embedded.version

Closes gh-27108
This commit is contained in:
Andy Wilkinson
2021-07-13 10:47:04 +01:00
10 changed files with 49 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* 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.
@@ -71,6 +71,7 @@ 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.
@@ -81,6 +82,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean;
* @author Mark Paluch
* @author Issam El-atif
* @author Paulius Dambrauskas
* @author Chris Bono
* @since 1.3.0
*/
@Configuration(proxyBeanMethods = false)
@@ -143,6 +145,8 @@ public class EmbeddedMongoAutoConfiguration {
}
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");
if (embeddedProperties.getFeatures() == null) {
for (Version version : Version.values()) {
if (version.asInDownloadPath().equals(embeddedProperties.getVersion())) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
@@ -30,6 +30,7 @@ import org.springframework.util.unit.DataUnit;
*
* @author Andy Wilkinson
* @author Yogesh Lonkar
* @author Chris Bono
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "spring.mongodb.embedded")
@@ -38,7 +39,7 @@ public class EmbeddedMongoProperties {
/**
* Version of Mongo to use.
*/
private String version = "3.5.5";
private String version;
private final Storage storage = new Storage();

View File

@@ -52,6 +52,7 @@ 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}.
@@ -60,6 +61,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Issam El-atif
* @author Chris Bono
*/
class EmbeddedMongoAutoConfigurationTests {
@@ -73,8 +75,14 @@ class EmbeddedMongoAutoConfigurationTests {
}
@Test
void defaultVersion() {
assertVersionConfiguration(null, "3.5.5");
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
@@ -95,7 +103,7 @@ class EmbeddedMongoAutoConfigurationTests {
if (isWindows()) {
features.add(Feature.ONLY_WINDOWS_2008_SERVER);
}
load("spring.mongodb.embedded.features="
loadWithValidVersion("spring.mongodb.embedded.features="
+ features.stream().map(Feature::name).collect(Collectors.joining(", ")));
assertThat(this.context.getBean(EmbeddedMongoProperties.class).getFeatures())
.containsExactlyElementsOf(features);
@@ -103,7 +111,7 @@ class EmbeddedMongoAutoConfigurationTests {
@Test
void useRandomPortByDefault() {
load();
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"));
@@ -112,7 +120,7 @@ class EmbeddedMongoAutoConfigurationTests {
@Test
void specifyPortToZeroAllocateRandomPort() {
load("spring.data.mongodb.port=0");
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"));
@@ -121,7 +129,7 @@ class EmbeddedMongoAutoConfigurationTests {
@Test
void randomlyAllocatedPortIsAvailableWhenCreatingMongoClient() {
load(MongoClientConfiguration.class);
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);
@@ -130,6 +138,7 @@ class EmbeddedMongoAutoConfigurationTests {
@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);
@@ -141,7 +150,7 @@ class EmbeddedMongoAutoConfigurationTests {
@Test
void defaultStorageConfiguration() {
load(MongoClientConfiguration.class);
loadWithValidVersion(MongoClientConfiguration.class);
Storage replication = this.context.getBean(MongodConfig.class).replication();
assertThat(replication.getOplogSize()).isEqualTo(0);
assertThat(replication.getDatabaseDir()).isNull();
@@ -152,32 +161,32 @@ class EmbeddedMongoAutoConfigurationTests {
void mongoWritesToCustomDatabaseDir(@TempDir Path temp) {
File customDatabaseDir = new File(temp.toFile(), "custom-database-dir");
FileSystemUtils.deleteRecursively(customDatabaseDir);
load("spring.mongodb.embedded.storage.databaseDir=" + customDatabaseDir.getPath());
loadWithValidVersion("spring.mongodb.embedded.storage.databaseDir=" + customDatabaseDir.getPath());
assertThat(customDatabaseDir).isDirectory();
assertThat(customDatabaseDir.listFiles()).isNotEmpty();
}
@Test
void customOpLogSizeIsAppliedToConfiguration() {
load("spring.mongodb.embedded.storage.oplogSize=1024KB");
loadWithValidVersion("spring.mongodb.embedded.storage.oplogSize=1024KB");
assertThat(this.context.getBean(MongodConfig.class).replication().getOplogSize()).isEqualTo(1);
}
@Test
void customOpLogSizeUsesMegabytesPerDefault() {
load("spring.mongodb.embedded.storage.oplogSize=10");
loadWithValidVersion("spring.mongodb.embedded.storage.oplogSize=10");
assertThat(this.context.getBean(MongodConfig.class).replication().getOplogSize()).isEqualTo(10);
}
@Test
void customReplicaSetNameIsAppliedToConfiguration() {
load("spring.mongodb.embedded.storage.replSetName=testing");
loadWithValidVersion("spring.mongodb.embedded.storage.replSetName=testing");
assertThat(this.context.getBean(MongodConfig.class).replication().getReplSetName()).isEqualTo("testing");
}
@Test
void customizeDownloadConfiguration() {
load(DownloadConfigBuilderCustomizerConfiguration.class);
loadWithValidVersion(DownloadConfigBuilderCustomizerConfiguration.class);
RuntimeConfig runtimeConfig = this.context.getBean(RuntimeConfig.class);
DownloadConfig downloadConfig = (DownloadConfig) new DirectFieldAccessor(runtimeConfig.artifactStore())
.getPropertyValue("downloadConfig");
@@ -186,13 +195,13 @@ class EmbeddedMongoAutoConfigurationTests {
@Test
void shutdownHookIsNotRegistered() {
load();
loadWithValidVersion();
assertThat(this.context.getBean(MongodExecutable.class).isRegisteredJobKiller()).isFalse();
}
@Test
void customMongoServerConfiguration() {
load(CustomMongoConfiguration.class);
loadWithValidVersion(CustomMongoConfiguration.class);
Map<String, MongoClient> mongoClients = this.context.getBeansOfType(MongoClient.class);
assertThat(mongoClients).isNotEmpty();
for (String mongoClientBeanName : mongoClients.keySet()) {
@@ -216,15 +225,16 @@ class EmbeddedMongoAutoConfigurationTests {
assertThat(buildInfo.getString("version")).isEqualTo(expectedVersion);
}
private void load(String... environment) {
load(null, environment);
private void loadWithValidVersion(String... environment) {
loadWithValidVersion(null, environment);
}
private void load(Class<?> config, String... 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);

View File

@@ -41,7 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat;
class ReactiveSessionAutoConfigurationMongoTests extends AbstractSessionAutoConfigurationTests {
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SessionAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(SessionAutoConfiguration.class))
.withPropertyValues("spring.mongodb.embedded.version=3.5.5");
@Test
void defaultConfig() {

View File

@@ -54,7 +54,8 @@ 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());
.withPropertyValues("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl(),
"spring.mongodb.embedded.version=3.5.5");
@Test
void defaultConfig() {

View File

@@ -220,7 +220,8 @@ tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {
}
doFirst {
def versionConstraints = dependencyVersions.versionConstraints
attributes "jetty-version": versionConstraints["org.eclipse.jetty:jetty-server"],
attributes "embedded-mongo-version": versionConstraints["de.flapdoodle.embed:de.flapdoodle.embed.mongo"],
"jetty-version": versionConstraints["org.eclipse.jetty:jetty-server"],
"jooq-version": versionConstraints["org.jooq:jooq"],
"spring-amqp-version": versionConstraints["org.springframework.amqp:spring-amqp"],
"spring-batch-version": versionConstraints["org.springframework.batch:spring-batch-core"],

View File

@@ -93,6 +93,7 @@
:spring-webservices-docs: https://docs.spring.io/spring-ws/docs/{spring-webservices-version}/reference/html/
:ant-docs: https://ant.apache.org/manual
:dependency-management-plugin-code: https://github.com/spring-gradle-plugins/dependency-management-plugin
: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

@@ -149,7 +149,11 @@ 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`.
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.
@@ -163,7 +167,6 @@ You can declare your own `IMongodConfig` and `IRuntimeConfig` beans to take cont
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

@@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
*/
@ExtendWith(OutputCaptureExtension.class)
@SpringBootTest
@SpringBootTest(properties = "spring.mongodb.embedded.version=3.5.5")
class SampleMongoApplicationTests {
@Test

View File

@@ -1,2 +1,3 @@
spring.security.user.name=user
spring.security.user.password=password
spring.mongodb.embedded.version=3.5.5