From f2b2c085e9f0253bb3d9721172463ac620f2d2c3 Mon Sep 17 00:00:00 2001 From: Henryk Konsek Date: Tue, 25 Nov 2014 22:28:50 +0100 Subject: [PATCH] Add auto-configuration support for Embedded MongoDB See gh-2002 --- spring-boot-autoconfigure/pom.xml | 5 ++ .../mongo/EmbedMongoAutoConfiguration.java | 35 +++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../EmbedMongoAutoConfigurationTests.java | 52 +++++++++++++++++++ spring-boot-dependencies/pom.xml | 6 +++ 5 files changed, 99 insertions(+) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/EmbedMongoAutoConfiguration.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/EmbedMongoAutoConfigurationTests.java diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index cd63dfc249..f5f3551371 100644 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -25,6 +25,11 @@ spring-boot + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + true + com.atomikos transactions-jdbc diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/EmbedMongoAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/EmbedMongoAutoConfiguration.java new file mode 100644 index 0000000000..b9931a52cb --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/EmbedMongoAutoConfiguration.java @@ -0,0 +1,35 @@ +package org.springframework.boot.autoconfigure.mongo; + +import com.mongodb.Mongo; +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.IMongodConfig; +import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; +import de.flapdoodle.embed.mongo.config.Net; +import de.flapdoodle.embed.mongo.distribution.Version; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.IOException; + +import static de.flapdoodle.embed.process.runtime.Network.localhostIsIPv6; + +@Configuration +@ConditionalOnClass({ Mongo.class, MongodStarter.class}) +public class EmbedMongoAutoConfiguration { + + @Autowired + private MongoProperties properties; + + @Bean(initMethod = "start", destroyMethod = "stop") + public MongodExecutable embedMongoServer() throws IOException { + IMongodConfig mongodConfig = new MongodConfigBuilder() + .version(Version.Main.PRODUCTION) + .net(new Net(properties.getPort(), localhostIsIPv6())) + .build(); + return MongodStarter.getDefaultInstance().prepare(mongodConfig); + } + +} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 7d9ef47e2a..a49ccc9fd2 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -46,6 +46,7 @@ org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\ +org.springframework.boot.autoconfigure.mongo.EmbedMongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/EmbedMongoAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/EmbedMongoAutoConfigurationTests.java new file mode 100644 index 0000000000..d4440825a9 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/EmbedMongoAutoConfigurationTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2014 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 + * + * http://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; + +import com.mongodb.CommandResult; +import org.junit.After; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.mongodb.core.MongoTemplate; + +import static org.junit.Assert.assertEquals; +import static org.springframework.boot.test.EnvironmentTestUtils.addEnvironment; +import static org.springframework.util.SocketUtils.findAvailableTcpPort; + +public class EmbedMongoAutoConfigurationTests { + + private AnnotationConfigApplicationContext context; + + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void shouldReturnVersionOfEmbeddedMongoServer() { + this.context = new AnnotationConfigApplicationContext(); + int mongoPort = findAvailableTcpPort(); + addEnvironment(context, "spring.data.mongodb.host=localhost", "spring.data.mongodb.port=" + mongoPort); + context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EmbedMongoAutoConfiguration.class); + context.refresh(); + MongoTemplate mongo = context.getBean(MongoTemplate.class); + CommandResult buildInfo = mongo.executeCommand("{ buildInfo: 1 }"); + assertEquals("2.6.1", buildInfo.getString("version")); + } + +} diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index fe1fb983d0..83cfaff7a1 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -63,6 +63,7 @@ 10.11.1.1 3.1.2 2.10.0 + 1.46.4 3.2.1 2.3.22 1.5.2 @@ -473,6 +474,11 @@ logback-classic ${logback.version} + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + ${embedmongo.version} + com.atomikos transactions-jdbc