Commit f2b2c085 authored by Henryk Konsek's avatar Henryk Konsek Committed by Andy Wilkinson

Add auto-configuration support for Embedded MongoDB

See gh-2002
parent 4aace564
...@@ -25,6 +25,11 @@ ...@@ -25,6 +25,11 @@
<artifactId>spring-boot</artifactId> <artifactId>spring-boot</artifactId>
</dependency> </dependency>
<!-- Optional --> <!-- Optional -->
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>com.atomikos</groupId> <groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId> <artifactId>transactions-jdbc</artifactId>
......
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);
}
}
...@@ -46,6 +46,7 @@ org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration ...@@ -46,6 +46,7 @@ org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.EmbedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
......
/*
* 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"));
}
}
...@@ -63,6 +63,7 @@ ...@@ -63,6 +63,7 @@
<derby.version>10.11.1.1</derby.version> <derby.version>10.11.1.1</derby.version>
<dropwizard-metrics.version>3.1.2</dropwizard-metrics.version> <dropwizard-metrics.version>3.1.2</dropwizard-metrics.version>
<ehcache.version>2.10.0</ehcache.version> <ehcache.version>2.10.0</ehcache.version>
<embedmongo.version>1.46.4</embedmongo.version>
<flyway.version>3.2.1</flyway.version> <flyway.version>3.2.1</flyway.version>
<freemarker.version>2.3.22</freemarker.version> <freemarker.version>2.3.22</freemarker.version>
<elasticsearch.version>1.5.2</elasticsearch.version> <elasticsearch.version>1.5.2</elasticsearch.version>
...@@ -473,6 +474,11 @@ ...@@ -473,6 +474,11 @@
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<version>${logback.version}</version> <version>${logback.version}</version>
</dependency> </dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>${embedmongo.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.atomikos</groupId> <groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId> <artifactId>transactions-jdbc</artifactId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment