Browse Source

DATAMONGO-1092 - Ensure compatibility with MongoDB 2.8.0.rc0 and java driver 2.13.0-rc0.

We updated GroupByResults to allow working with changed data types returned for count and keys and fixed assertion on error message for duplicate keys.
Using java-driver 2.12.x when connecting to an 2.8.0.rc-0 instance is likely to cause trouble with authentication. This is the intended behavior.

2.8.0-rc0 throws error when removing elements from a collection that does not yet exist, which is different to what 2.6.x does.

The java-driver 2.13.0-rc0 works perfectly fine with a 2.6.x Server instance.
We deprecated Index.Duplicates#DROP since it has been removed in MongoDB 2.8

Original pull request: #246.
pull/248/head
Christoph Strobl 11 years ago committed by Thomas Darimont
parent
commit
461e7d05d7
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java
  2. 17
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/GroupByResults.java
  3. 29
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java
  4. 10
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/GroupByTests.java

12
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java

@ -35,7 +35,17 @@ import com.mongodb.DBObject; @@ -35,7 +35,17 @@ import com.mongodb.DBObject;
public class Index implements IndexDefinition {
public enum Duplicates {
RETAIN, DROP
RETAIN, //
/**
* Dropping Duplicates was removed in MongoDB Server 2.8.0-rc0.
* <p>
* See https://jira.mongodb.org/browse/SERVER-14710
*
* @deprecated since 1.7.
*/
@Deprecated//
DROP
}
private final Map<String, Direction> fieldSpec = new LinkedHashMap<String, Direction>();

17
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/GroupByResults.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011 - 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.
@ -26,7 +26,8 @@ import com.mongodb.DBObject; @@ -26,7 +26,8 @@ import com.mongodb.DBObject;
* Collects the results of executing a group operation.
*
* @author Mark Pollack
* @param <T> The class in which the results are mapped onto, accessible via an interator.
* @author Christoph Strobl
* @param <T> The class in which the results are mapped onto, accessible via an {@link Iterator}.
*/
public class GroupByResults<T> implements Iterable<T> {
@ -38,6 +39,7 @@ public class GroupByResults<T> implements Iterable<T> { @@ -38,6 +39,7 @@ public class GroupByResults<T> implements Iterable<T> {
private String serverUsed;
public GroupByResults(List<T> mappedResults, DBObject rawResults) {
Assert.notNull(mappedResults);
Assert.notNull(rawResults);
this.mappedResults = mappedResults;
@ -68,21 +70,24 @@ public class GroupByResults<T> implements Iterable<T> { @@ -68,21 +70,24 @@ public class GroupByResults<T> implements Iterable<T> {
}
private void parseCount() {
Object object = rawResults.get("count");
if (object instanceof Double) {
count = (Double) object;
if (object instanceof Number) {
count = ((Number) object).doubleValue();
}
}
private void parseKeys() {
Object object = rawResults.get("keys");
if (object instanceof Integer) {
keys = (Integer) object;
if (object instanceof Number) {
keys = ((Number) object).intValue();
}
}
private void parseServerUsed() {
// "serverUsed" : "127.0.0.1:27017"
Object object = rawResults.get("serverUsed");
if (object instanceof String) {

29
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -105,6 +105,8 @@ public class MongoTemplateTests { @@ -105,6 +105,8 @@ public class MongoTemplateTests {
private static final org.springframework.data.util.Version TWO_DOT_FOUR = org.springframework.data.util.Version
.parse("2.4");
private static final org.springframework.data.util.Version TWO_DOT_EIGHT = org.springframework.data.util.Version
.parse("2.8");
@Autowired MongoTemplate template;
@Autowired MongoDbFactory factory;
@ -115,7 +117,6 @@ public class MongoTemplateTests { @@ -115,7 +117,6 @@ public class MongoTemplateTests {
@Rule public ExpectedException thrown = ExpectedException.none();
@Autowired
@SuppressWarnings("unchecked")
public void setMongo(Mongo mongo) throws Exception {
CustomConversions conversions = new CustomConversions(Arrays.asList(DateToDateTimeConverter.INSTANCE,
@ -235,7 +236,7 @@ public class MongoTemplateTests { @@ -235,7 +236,7 @@ public class MongoTemplateTests {
template.insert(person);
fail("Expected DataIntegrityViolationException!");
} catch (DataIntegrityViolationException e) {
assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$_id_ dup key:"));
assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$_id_"));
}
}
@ -289,8 +290,7 @@ public class MongoTemplateTests { @@ -289,8 +290,7 @@ public class MongoTemplateTests {
template.save(person);
fail("Expected DataIntegrityViolationException!");
} catch (DataIntegrityViolationException e) {
assertThat(e.getMessage(),
containsString("E11000 duplicate key error index: database.person.$firstName_-1 dup key:"));
assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$firstName_-1"));
}
}
@ -318,6 +318,7 @@ public class MongoTemplateTests { @@ -318,6 +318,7 @@ public class MongoTemplateTests {
}
@Test
@SuppressWarnings("deprecation")
public void testEnsureIndex() throws Exception {
Person p1 = new Person("Oliver");
@ -339,19 +340,29 @@ public class MongoTemplateTests { @@ -339,19 +340,29 @@ public class MongoTemplateTests {
if ("age_-1".equals(ix.get("name"))) {
indexKey = ix.get("key").toString();
unique = (Boolean) ix.get("unique");
dropDupes = (Boolean) ix.get("dropDups");
if (mongoVersion.isLessThan(TWO_DOT_EIGHT)) {
dropDupes = (Boolean) ix.get("dropDups");
assertThat(dropDupes, is(true));
} else {
assertThat(ix.get("dropDups"), is(nullValue()));
}
}
}
assertThat(indexKey, is("{ \"age\" : -1}"));
assertThat(unique, is(true));
assertThat(dropDupes, is(true));
List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
assertThat(indexInfoList.size(), is(2));
IndexInfo ii = indexInfoList.get(1);
assertThat(ii.isUnique(), is(true));
assertThat(ii.isDropDuplicates(), is(true));
if (mongoVersion.isLessThan(TWO_DOT_EIGHT)) {
assertThat(ii.isDropDuplicates(), is(true));
} else {
assertThat(ii.isDropDuplicates(), is(false));
}
assertThat(ii.isSparse(), is(false));
List<IndexField> indexFields = ii.getIndexFields();
@ -677,8 +688,8 @@ public class MongoTemplateTests { @@ -677,8 +688,8 @@ public class MongoTemplateTests {
Query q = new Query(Criteria.where("text").regex("^Hello.*"));
Message found1 = template.findAndRemove(q, Message.class);
Message found2 = template.findAndRemove(q, Message.class);
// Message notFound = template.findAndRemove(q, Message.class);
DBObject notFound = template.getCollection("").findAndRemove(q.getQueryObject());
Message notFound = template.findAndRemove(q, Message.class);
assertThat(found1, notNullValue());
assertThat(found2, notNullValue());
assertThat(notFound, nullValue());

10
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapreduce/GroupByTests.java

@ -49,6 +49,7 @@ import com.mongodb.Mongo; @@ -49,6 +49,7 @@ import com.mongodb.Mongo;
*
* @author Mark Pollack
* @author Oliver Gierke
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
@ -60,7 +61,6 @@ public class GroupByTests { @@ -60,7 +61,6 @@ public class GroupByTests {
MongoTemplate mongoTemplate;
@Autowired
@SuppressWarnings("unchecked")
public void setMongo(Mongo mongo) throws Exception {
MongoMappingContext mappingContext = new MongoMappingContext();
@ -116,7 +116,7 @@ public class GroupByTests { @@ -116,7 +116,7 @@ public class GroupByTests {
}
@Test
public void SimpleGroup() {
public void simpleGroupFunction() {
createGroupByData();
GroupByResults<XObject> results = mongoTemplate.group(
@ -128,7 +128,7 @@ public class GroupByTests { @@ -128,7 +128,7 @@ public class GroupByTests {
}
@Test
public void SimpleGroupWithKeyFunction() {
public void simpleGroupWithKeyFunction() {
createGroupByData();
GroupByResults<XObject> results = mongoTemplate.group(
@ -140,7 +140,7 @@ public class GroupByTests { @@ -140,7 +140,7 @@ public class GroupByTests {
}
@Test
public void SimpleGroupWithFunctionsAsResources() {
public void simpleGroupWithFunctionsAsResources() {
createGroupByData();
GroupByResults<XObject> results = mongoTemplate.group(
@ -152,7 +152,7 @@ public class GroupByTests { @@ -152,7 +152,7 @@ public class GroupByTests {
}
@Test
public void SimpleGroupWithQueryAndFunctionsAsResources() {
public void simpleGroupWithQueryAndFunctionsAsResources() {
createGroupByData();
GroupByResults<XObject> results = mongoTemplate.group(

Loading…
Cancel
Save