Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding embedded mongo for testing

  • Loading branch information...
commit c0aa579924a0d9ea80f27682bf95803a619ccaa2 1 parent a6b7ff7
@trisberg trisberg authored
View
4 mongodb/build.gradle
@@ -4,6 +4,8 @@ dependencies {
compile("org.springframework:spring-tx:$springVersion") { force = true }
compile("org.springframework:spring-orm:$springVersion") { force = true }
+
+ testCompile "de.flapdoodle.embedmongo:de.flapdoodle.embedmongo:1.16"
runtime "org.slf4j:slf4j-log4j12:$slf4jVersion"
runtime "log4j:log4j:$log4jVersion"
@@ -11,3 +13,5 @@ dependencies {
runtime "org.mongodb:mongo-java-driver:$mongoDbVersion"
}
+
+test.forkEvery = 1
View
2  mongodb/src/test/java/com/oreilly/springdata/mongodb/AbstractIntegrationTest.java
@@ -35,7 +35,7 @@
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(classes = { ApplicationConfig.class })
+@ContextConfiguration(classes = { TestConfig.class })
public abstract class AbstractIntegrationTest {
@Autowired
View
6 mongodb/src/test/java/com/oreilly/springdata/mongodb/ApplicationConfigTest.java
@@ -34,14 +34,14 @@
@Test
public void bootstrapAppFromJavaConfig() {
- ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
+ ApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class);
assertThat(context, is(notNullValue()));
}
@Test
- public void bootsrapAppFromXml() {
+ public void bootstrapAppFromXml() {
- ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/application-context.xml");
+ ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/test-context.xml");
assertThat(context, is(notNullValue()));
}
}
View
89 mongodb/src/test/java/com/oreilly/springdata/mongodb/TestConfig.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2012 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 com.oreilly.springdata.mongodb;
+
+import com.mongodb.Mongo;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.FilterType;
+import org.springframework.core.convert.converter.Converter;
+import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
+import org.springframework.data.mongodb.core.convert.CustomConversions;
+import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
+import org.springframework.data.mongodb.test.MongodExeFactoryBean;
+
+import java.util.List;
+
+/**
+ * Spring JavaConfig configuration class to setup a Spring container and infrastructure components like a
+ * {@link javax.sql.DataSource}, and a {@link org.springframework.transaction.PlatformTransactionManager}.
+ *
+ * @author Oliver Gierke
+ */
+@Configuration
+@ComponentScan(basePackageClasses = TestConfig.class, excludeFilters = {
+ @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Configuration.class)})
+@EnableMongoRepositories
+class TestConfig extends AbstractMongoConfiguration {
+
+ @Autowired
+ private List<Converter<?, ?>> converters;
+
+ private MongodExeFactoryBean mongoFactoryBean = new MongodExeFactoryBean(12347);
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#getDatabaseName()
+ */
+ @Override
+ protected String getDatabaseName() {
+ return "e-store";
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#mongo()
+ */
+ @Override
+ public Mongo mongo() throws Exception {
+ return mongoFactory().getObject();
+ }
+
+ @Bean
+ public MongodExeFactoryBean mongoFactory() throws Exception {
+ return mongoFactoryBean;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#getMappingBasePackage()
+ */
+ @Override
+ protected String getMappingBasePackage() {
+ return getClass().getPackage().getName();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#customConversions()
+ */
+ @Override
+ public CustomConversions customConversions() {
+ return new CustomConversions(converters);
+ }
+}
View
63 mongodb/src/test/java/org/springframework/data/mongodb/test/MongodExeFactoryBean.java
@@ -0,0 +1,63 @@
+package org.springframework.data.mongodb.test;
+
+import com.mongodb.Mongo;
+
+import de.flapdoodle.embedmongo.MongoDBRuntime;
+import de.flapdoodle.embedmongo.MongodExecutable;
+import de.flapdoodle.embedmongo.MongodProcess;
+import de.flapdoodle.embedmongo.config.MongodConfig;
+import de.flapdoodle.embedmongo.distribution.Version;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
+
+/**
+ */
+public class MongodExeFactoryBean implements FactoryBean<Mongo>, InitializingBean, DisposableBean {
+
+ private MongodExecutable mongodExe;
+ private MongodProcess mongod;
+
+ private Mongo mongo;
+
+ private int port = 12345;
+
+ public MongodExeFactoryBean() {
+ }
+
+ public MongodExeFactoryBean(int port) {
+ this.port = port;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ @Override
+ public Mongo getObject() throws Exception {
+ return mongo;
+ }
+
+ @Override
+ public Class<?> getObjectType() {
+ return Mongo.class;
+ }
+
+ @Override
+ public boolean isSingleton() {
+ return true;
+ }
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ mongodExe = MongoDBRuntime.getDefaultInstance().prepare(new MongodConfig(Version.V2_0, port, false));
+ mongod = mongodExe.start();
+ mongo = new Mongo("localhost", port);
+ }
+
+ @Override
+ public void destroy() throws Exception {
+ mongod.stop();
+ mongodExe.cleanup();
+ }
+}
View
28 mongodb/src/test/resources/META-INF/spring/test-context.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:mongo="http://www.springframework.org/schema/data/mongo"
+ xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+ <bean id="mongo" class="org.springframework.data.mongodb.test.MongodExeFactoryBean">
+ <property name="port" value="12346"/>
+ </bean>
+
+ <bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory" depends-on="mongo">
+ <constructor-arg name="mongo" ref="mongo"/>
+ <constructor-arg name="databaseName" value="db"/>
+ </bean>
+
+ <mongo:mapping-converter id="mongoConverter" base-package="com.oreilly.springdata.mongodb">
+ <mongo:custom-converters base-package="com.oreilly.springdata.mongodb" />
+ </mongo:mapping-converter>
+
+ <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
+ <constructor-arg ref="mongoDbFactory" />
+ <constructor-arg ref="mongoConverter" />
+ </bean>
+
+ <mongo:repositories base-package="com.oreilly.springdata.mongodb" />
+
+</beans>

0 comments on commit c0aa579

Please sign in to comment.
Something went wrong with that request. Please try again.