diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.java index 0c1915d12e15..2ab6fad362b2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.java @@ -35,6 +35,7 @@ * @author Stephane Nicoll * @author Moritz Halbritter * @author Phillip Webb + * @author Scott Frederick */ @Configuration(proxyBeanMethods = false) @ConditionalOnMissingBean(MongoDatabaseFactory.class) @@ -44,7 +45,11 @@ class MongoDatabaseFactoryConfiguration { @Bean MongoDatabaseFactorySupport mongoDatabaseFactory(MongoClient mongoClient, MongoProperties properties, MongoConnectionDetails connectionDetails) { - return new SimpleMongoClientDatabaseFactory(mongoClient, connectionDetails.getConnectionString().getDatabase()); + String database = properties.getDatabase(); + if (database == null) { + database = connectionDetails.getConnectionString().getDatabase(); + } + return new SimpleMongoClientDatabaseFactory(mongoClient, database); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.java index b2480e747e54..ecdf179b611c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.java @@ -67,6 +67,7 @@ * @author Moritz Halbritter * @author Andy Wilkinson * @author Phillip Webb + * @author Scott Frederick * @since 2.0.0 */ @AutoConfiguration(after = MongoReactiveAutoConfiguration.class) @@ -84,9 +85,13 @@ public class MongoReactiveDataAutoConfiguration { @Bean @ConditionalOnMissingBean(ReactiveMongoDatabaseFactory.class) - public SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoClient mongo) { - return new SimpleReactiveMongoDatabaseFactory(mongo, - this.connectionDetails.getConnectionString().getDatabase()); + public SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoClient mongo, + MongoProperties properties) { + String database = properties.getDatabase(); + if (database == null) { + database = this.connectionDetails.getConnectionString().getDatabase(); + } + return new SimpleReactiveMongoDatabaseFactory(mongo, database); } @Bean diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java index 50941bf7f353..590fb36bdf1f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java @@ -64,6 +64,7 @@ * @author Moritz Halbritter * @author Andy Wilkinson * @author Phillip Webb + * @author Scott Frederick */ class MongoDataAutoConfigurationTests { @@ -226,6 +227,64 @@ void autoConfiguresIfUserProvidesMongoDatabaseFactoryButNoClient() { .run((context) -> assertThat(context).hasSingleBean(MongoTemplate.class)); } + @Test + void databaseHasDefault() { + this.contextRunner.run((context) -> { + MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class); + assertThat(factory.getMongoDatabase().getName()).isEqualTo("test"); + }); + } + + @Test + void databasePropertyIsUsed() { + this.contextRunner.withPropertyValues("spring.data.mongodb.database=mydb").run((context) -> { + MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class); + assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb"); + }); + } + + @Test + void databaseInUriPropertyIsUsed() { + this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/mydb") + .run((context) -> { + MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class); + assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb"); + }); + } + + @Test + void databasePropertyOverridesUriProperty() { + this.contextRunner + .withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/notused", + "spring.data.mongodb.database=mydb") + .run((context) -> { + MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class); + assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb"); + }); + } + + @Test + void databasePropertyIsUsedWhenNoDatabaseInUri() { + this.contextRunner + .withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/", + "spring.data.mongodb.database=mydb") + .run((context) -> { + MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class); + assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb"); + }); + } + + @Test + void contextFailsWhenDatabaseNotSet() { + this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/") + .run((context) -> assertThat(context).getFailure().hasMessageContaining("Database name must not be empty")); + } + @Test void definesPropertiesBasedConnectionDetailsByDefault() { this.contextRunner.run((context) -> assertThat(context).hasSingleBean(PropertiesMongoConnectionDetails.class)); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java index ee11af22c99e..df9a5f264ee2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java @@ -29,6 +29,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; +import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory; import org.springframework.data.mongodb.gridfs.ReactiveGridFsTemplate; import org.springframework.test.util.ReflectionTestUtils; @@ -42,6 +43,7 @@ * @author Moritz Halbritter * @author Andy Wilkinson * @author Phillip Webb + * @author Scott Frederick */ class MongoReactiveDataAutoConfigurationTests { @@ -90,6 +92,64 @@ void backsOffIfMongoClientBeanIsNotPresent() { runner.run((context) -> assertThat(context).doesNotHaveBean(MongoReactiveDataAutoConfiguration.class)); } + @Test + void databaseHasDefault() { + this.contextRunner.run((context) -> { + ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class); + assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("test"); + }); + } + + @Test + void databasePropertyIsUsed() { + this.contextRunner.withPropertyValues("spring.data.mongodb.database=mydb").run((context) -> { + ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class); + assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb"); + }); + } + + @Test + void databaseInUriPropertyIsUsed() { + this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/mydb") + .run((context) -> { + ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class); + assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb"); + }); + } + + @Test + void databasePropertyOverridesUriProperty() { + this.contextRunner + .withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/notused", + "spring.data.mongodb.database=mydb") + .run((context) -> { + ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class); + assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb"); + }); + } + + @Test + void databasePropertyIsUsedWhenNoDatabaseInUri() { + this.contextRunner + .withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/", + "spring.data.mongodb.database=mydb") + .run((context) -> { + ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class); + assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class); + assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb"); + }); + } + + @Test + void contextFailsWhenDatabaseNotSet() { + this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/") + .run((context) -> assertThat(context).getFailure().hasMessageContaining("Database name must not be empty")); + } + private String grisFsTemplateDatabaseName(AssertableApplicationContext context) { assertThat(context).hasSingleBean(ReactiveGridFsTemplate.class); ReactiveGridFsTemplate template = context.getBean(ReactiveGridFsTemplate.class);