#139 - Switched to embedded MongoDB for integration tests.
Removed MongoDB dependency by adding Flapdoodle embedded MongoDB. That allows us to get rid of the utility module we had in place for the MongoDB samples guarding the tests to only run when a MongoDB instance is running. Tweaked Travis setup to not require the MongoDB service anymore.
This commit is contained in:
@@ -4,7 +4,6 @@ jdk:
|
||||
- oraclejdk8
|
||||
|
||||
services:
|
||||
- mongodb
|
||||
- redis-server
|
||||
|
||||
cache:
|
||||
|
||||
@@ -11,14 +11,5 @@
|
||||
<artifactId>spring-data-mongodb-examples</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -22,20 +22,12 @@ import static org.junit.Assert.*;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.mongodb.aggregation.ApplicationConfiguration;
|
||||
import example.springdata.mongodb.aggregation.Invoice;
|
||||
import example.springdata.mongodb.aggregation.LineItem;
|
||||
import example.springdata.mongodb.aggregation.Order;
|
||||
import example.springdata.mongodb.aggregation.OrderRepository;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link OrderRepository}.
|
||||
*
|
||||
@@ -46,8 +38,6 @@ import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class OrderRepositoryIntegrationTests {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
|
||||
|
||||
@Autowired OrderRepository repository;
|
||||
|
||||
private final static LineItem product1 = new LineItem("p1", 1.23);
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework.data.mongodb.core.aggregation" level="debug" />
|
||||
|
||||
<root level="error">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -12,15 +12,6 @@
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -18,8 +18,9 @@ package example.springdata.mongodb.advanced;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import example.springdata.mongodb.customer.Customer;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,16 +28,12 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Meta;
|
||||
import org.springframework.data.util.Version;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
import example.springdata.mongodb.customer.Customer;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@@ -44,8 +41,6 @@ import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class AdvancedIntegrationTests {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.atLeast(new Version(2, 6));
|
||||
|
||||
@Autowired AdvancedRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
@@ -76,8 +71,8 @@ public class AdvancedIntegrationTests {
|
||||
// execute another finder without meta attributes that should not be picked up
|
||||
repository.findByLastname(dave.getLastname(), new Sort("firstname"));
|
||||
|
||||
DBCursor cursor = operations.getCollection(ApplicationConfiguration.SYSTEM_PROFILE_DB).find(
|
||||
new BasicDBObject("query.$comment", AdvancedRepository.META_COMMENT));
|
||||
DBCursor cursor = operations.getCollection(ApplicationConfiguration.SYSTEM_PROFILE_DB)
|
||||
.find(new BasicDBObject("query.$comment", AdvancedRepository.META_COMMENT));
|
||||
|
||||
while (cursor.hasNext()) {
|
||||
|
||||
|
||||
@@ -20,12 +20,10 @@ import static org.hamcrest.core.IsNull.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import example.springdata.mongodb.customer.Customer;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -33,7 +31,6 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.script.ExecutableMongoScript;
|
||||
import org.springframework.data.mongodb.core.script.NamedMongoScript;
|
||||
import org.springframework.data.util.Version;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
@@ -46,8 +43,6 @@ import com.mongodb.DBObject;
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class ServersideScriptTests {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.atLeast(new Version(2, 6));
|
||||
|
||||
@Autowired AdvancedRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import static org.junit.Assert.*;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -35,8 +34,6 @@ import org.springframework.data.mongodb.core.index.GeospatialIndex;
|
||||
import org.springframework.data.querydsl.QSort;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration test for {@link CustomerRepository}.
|
||||
*
|
||||
@@ -46,8 +43,6 @@ import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class CustomerRepositoryIntegrationTest {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
|
||||
|
||||
@Autowired CustomerRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# Random port for embedded MongoDB
|
||||
spring.data.mongodb.port=0
|
||||
@@ -12,23 +12,12 @@
|
||||
<name>Spring Data MongoDB - GeoJson support</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -17,7 +17,6 @@ package example.springdata.mongodb.geojson;
|
||||
|
||||
import static com.fasterxml.jackson.databind.DeserializationFeature.*;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
@@ -37,11 +36,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
public class ApplicationConfiguration {
|
||||
|
||||
/**
|
||||
* Read JSON data from disk and insert those stores.
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package example.springdata.mongodb.geojson;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -28,17 +27,11 @@ import org.springframework.data.mongodb.core.geo.GeoJson;
|
||||
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
|
||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.util.Version;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
import example.springdata.mongodb.geojson.Application;
|
||||
import example.springdata.mongodb.geojson.Store;
|
||||
import example.springdata.mongodb.geojson.StoreRepository;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link StoreRepository}.
|
||||
*
|
||||
@@ -46,14 +39,12 @@ import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = { Application.class })
|
||||
@SpringApplicationConfiguration(classes = { ApplicationConfiguration.class })
|
||||
public class StoreRepositoryTests {
|
||||
|
||||
private static final GeoJsonPolygon GEO_JSON_POLYGON = new GeoJsonPolygon(new Point(-73.992514, 40.758934),
|
||||
new Point(-73.961138, 40.760348), new Point(-73.991658, 40.730006), new Point(-73.992514, 40.758934));
|
||||
|
||||
public static @ClassRule RequiresMongoDB requiresMongoDB_2_6 = RequiresMongoDB.atLeast(new Version(2, 6, 0));
|
||||
|
||||
@Autowired StoreRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
@@ -114,9 +105,8 @@ public class StoreRepositoryTests {
|
||||
*/
|
||||
@Test
|
||||
public void findWithinLegacyPolygon() {
|
||||
repository.findByLocationWithin(
|
||||
new Polygon(new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348),
|
||||
new Point(-73.991658, 40.730006))).forEach(System.out::println);
|
||||
repository.findByLocationWithin(new Polygon(new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348),
|
||||
new Point(-73.991658, 40.730006))).forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,8 +120,8 @@ public class StoreRepositoryTests {
|
||||
|
||||
operations.getConverter().write(GEO_JSON_POLYGON, geoJsonDbo);
|
||||
|
||||
BasicQuery bq = new BasicQuery(new BasicDBObject("location", new BasicDBObject("$geoIntersects", new BasicDBObject(
|
||||
"$geometry", geoJsonDbo))));
|
||||
BasicQuery bq = new BasicQuery(
|
||||
new BasicDBObject("location", new BasicDBObject("$geoIntersects", new BasicDBObject("$geometry", geoJsonDbo))));
|
||||
|
||||
operations.find(bq, Store.class).forEach(System.out::println);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# Random port for embedded MongoDB
|
||||
spring.data.mongodb.port=0
|
||||
@@ -11,19 +11,4 @@
|
||||
<artifactId>spring-data-mongodb-java8</artifactId>
|
||||
<name>Spring Data MongoDB - Java 8 specific features</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -18,7 +18,6 @@ package example.springdata.mongodb.people;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,8 +26,6 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration test for {@link PersonRepository}.
|
||||
*
|
||||
@@ -39,8 +36,6 @@ import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class PersonRepositoryIntegrationTest {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
|
||||
|
||||
@Autowired PersonRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
|
||||
2
mongodb/java8/src/test/resources/application.properties
Normal file
2
mongodb/java8/src/test/resources/application.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
# Random port for embedded MongoDB
|
||||
spring.data.mongodb.port=0
|
||||
@@ -22,7 +22,6 @@
|
||||
<module>text-search</module>
|
||||
<module>java8</module>
|
||||
<module>security</module>
|
||||
<module>util</module>
|
||||
<module>geo-json</module>
|
||||
</modules>
|
||||
|
||||
@@ -44,6 +43,12 @@
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -13,13 +13,6 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.springframework.security.data.repository.query.SecurityEvaluationCont
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@SpringBootApplication
|
||||
class Application {
|
||||
class ApplicationConfiguration {
|
||||
|
||||
@Bean
|
||||
public EvaluationContextExtension securityExtension() {
|
||||
@@ -18,16 +18,10 @@ package example.springdata.mongodb.security;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import example.springdata.mongodb.security.Application;
|
||||
import example.springdata.mongodb.security.Person;
|
||||
import example.springdata.mongodb.security.PersonRepository;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -41,14 +35,12 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* Integration test for {@link PersonRepository}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @authot Oliver Gierke
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class PersonRepositoryIntegrationTest {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
|
||||
|
||||
@Autowired PersonRepository repository;
|
||||
|
||||
Person dave, oliver, carter, admin;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# Random port for embedded MongoDB
|
||||
spring.data.mongodb.port=0
|
||||
@@ -13,15 +13,11 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-2015 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.
|
||||
@@ -19,18 +19,14 @@ import static example.springdata.mongodb.util.ConsoleResultPrinter.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.mapping.TextScore;
|
||||
import org.springframework.data.mongodb.core.query.TextCriteria;
|
||||
import org.springframework.data.util.Version;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration tests showing the text search functionality using repositories.
|
||||
*
|
||||
@@ -44,8 +40,6 @@ public class TextSearchRepositoryTests {
|
||||
|
||||
@Autowired BlogPostRepository repo;
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.atLeast(new Version(2, 6));
|
||||
|
||||
/**
|
||||
* Show how to do simple matching. <br />
|
||||
* Note that text search is case insensitive and will also find entries like {@literal releases}.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2014-2015 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.
|
||||
@@ -20,47 +20,41 @@ import static org.springframework.data.mongodb.core.query.Query.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.index.TextIndexDefinition;
|
||||
import org.springframework.data.mongodb.core.index.TextIndexDefinition.TextIndexDefinitionBuilder;
|
||||
import org.springframework.data.mongodb.core.query.TextCriteria;
|
||||
import org.springframework.data.mongodb.core.query.TextQuery;
|
||||
import org.springframework.data.util.Version;
|
||||
|
||||
import example.springdata.mongodb.util.BlogPostInitializer;
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { MongoTestConfiguration.class })
|
||||
public class TextSearchTemplateTests {
|
||||
|
||||
MongoOperations operations;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.atLeast(new Version(2, 6));
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
MongoProperties properties = new MongoProperties();
|
||||
|
||||
operations = new MongoTemplate(properties.createMongoClient(null), properties.getMongoClientDatabase());
|
||||
operations.dropCollection(BlogPost.class);
|
||||
|
||||
createIndex();
|
||||
|
||||
BlogPostInitializer.INSTANCE.initialize(this.operations);
|
||||
}
|
||||
// @Before
|
||||
// public void setUp() throws Exception {
|
||||
//
|
||||
// MongoProperties properties = new MongoProperties();
|
||||
//
|
||||
// operations = new MongoTemplate(properties.createMongoClient(null), properties.getMongoClientDatabase());
|
||||
// operations.dropCollection(BlogPost.class);
|
||||
//
|
||||
// createIndex();
|
||||
//
|
||||
// BlogPostInitializer.INSTANCE.initialize(this.operations);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Show how to do simple matching. <br />
|
||||
* Note that text search is case insensitive and will also find entries like {@literal releases}.
|
||||
* Show how to do simple matching. Note that text search is case insensitive and will also find entries like
|
||||
* {@literal releases}.
|
||||
*/
|
||||
@Test
|
||||
public void findAllBlogPostsWithRelease() {
|
||||
@@ -87,36 +81,4 @@ public class TextSearchTemplateTests {
|
||||
|
||||
printResult(blogPosts, criteria);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the mongodb text index for {@link BlogPost}. <br />
|
||||
*
|
||||
* <pre>
|
||||
* <code>
|
||||
* db.collection.ensureIndex(
|
||||
* {
|
||||
* "title" : "text"
|
||||
* "content" : "text"
|
||||
* "categories" : "text",
|
||||
* },
|
||||
* {
|
||||
* weights : {
|
||||
* "title" : 3,
|
||||
* "content" : 2
|
||||
* }
|
||||
* }
|
||||
* )
|
||||
* </code>
|
||||
* </pre>
|
||||
*/
|
||||
private void createIndex() {
|
||||
|
||||
TextIndexDefinition textIndex = new TextIndexDefinitionBuilder()//
|
||||
.onField("title", 3F) //
|
||||
.onField("content", 2F) //
|
||||
.onField("categories") //
|
||||
.build();
|
||||
|
||||
operations.indexOps(BlogPost.class).ensureIndex(textIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
|
||||
<name>Spring Data MongoDB - Example utilities</name>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-mongodb-examples</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014-2015 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 example.springdata.mongodb.util;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.AssumptionViolatedException;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.ExternalResource;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.springframework.data.util.Version;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.BasicDBObjectBuilder;
|
||||
import com.mongodb.CommandResult;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
/**
|
||||
* {@link TestRule} verifying server tests are executed against match a given version. This one can be used as
|
||||
* {@link ClassRule} e.g. in context depending tests run with {@link SpringJUnit4ClassRunner} when the context would
|
||||
* fail to start in case of invalid version, or as simple {@link Rule} on specific tests.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Alexander Golonzovsky
|
||||
* @since 1.6
|
||||
*/
|
||||
public class RequiresMongoDB extends ExternalResource {
|
||||
|
||||
private String host = "localhost";
|
||||
private int port = 27017;
|
||||
|
||||
private final Version minVersion;
|
||||
private final Version maxVersion;
|
||||
|
||||
private Version currentVersion;
|
||||
|
||||
public RequiresMongoDB(Version min, Version max) {
|
||||
this.minVersion = min;
|
||||
this.maxVersion = max;
|
||||
}
|
||||
|
||||
public static RequiresMongoDB anyVersion() {
|
||||
return new RequiresMongoDB(new Version(0, 0, 0), new Version(9999, 9999, 9999));
|
||||
}
|
||||
|
||||
public static RequiresMongoDB atLeast(Version minVersion) {
|
||||
return new RequiresMongoDB(minVersion, new Version(9999, 9999, 9999));
|
||||
}
|
||||
|
||||
public static RequiresMongoDB atMost(Version maxVersion) {
|
||||
return new RequiresMongoDB(new Version(0, 0, 0), maxVersion);
|
||||
}
|
||||
|
||||
public RequiresMongoDB serverRunningAt(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void before() throws Throwable {
|
||||
|
||||
initCurrentVersion();
|
||||
|
||||
if (currentVersion.isLessThan(minVersion) || currentVersion.isGreaterThan(maxVersion)) {
|
||||
throw new AssumptionViolatedException(String.format(
|
||||
"Expected mongodb server to be in range %s to %s but found %s", minVersion, maxVersion, currentVersion));
|
||||
}
|
||||
}
|
||||
|
||||
private void initCurrentVersion() {
|
||||
|
||||
if (currentVersion == null) {
|
||||
try {
|
||||
DB db = new MongoClient(host, port).getDB("test");
|
||||
CommandResult result = db.command(new BasicDBObjectBuilder().add("buildInfo", 1).get());
|
||||
this.currentVersion = Version.parse(result.get("version").toString());
|
||||
} catch (com.mongodb.MongoTimeoutException | UnknownHostException e) {
|
||||
throw new AssumptionViolatedException("Seems as mongodb server is not running.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
@@ -31,6 +32,13 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -19,6 +19,12 @@
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<artifactId>querydsl-mongodb</artifactId>
|
||||
@@ -28,9 +34,13 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-rest-hal-browser</artifactId>
|
||||
<version>2.4.0.DATAREST-627-SNAPSHOT</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-parameter-names</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
@@ -75,7 +85,6 @@
|
||||
<dependency>
|
||||
<groupId>org.webjars</groupId>
|
||||
<artifactId>webjars-locator</artifactId>
|
||||
<version>0.22</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
@@ -17,6 +17,10 @@ package example.springdata.rest.stores;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import com.fasterxml.jackson.databind.Module;
|
||||
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
||||
|
||||
/**
|
||||
* Spring configuration class main application bootstrap point.
|
||||
@@ -29,4 +33,9 @@ public class StoreApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(StoreApp.class, args);
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/spring-projects/spring-boot/issues/4336
|
||||
public @Bean Module namesModule() {
|
||||
return new ParameterNamesModule();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,5 @@
|
||||
# Random port for embedded MongoDB
|
||||
spring.data.mongodb.port=0
|
||||
|
||||
# Spring Data REST
|
||||
spring.data.rest.base-path=/api
|
||||
|
||||
Reference in New Issue
Block a user