Skip to content

Commit 461e7d0

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
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: spring-projects#246.
1 parent 10c37b1 commit 461e7d0

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@
3535
public class Index implements IndexDefinition {
3636

3737
public enum Duplicates {
38-
RETAIN, DROP
38+
RETAIN, //
39+
40+
/**
41+
* Dropping Duplicates was removed in MongoDB Server 2.8.0-rc0.
42+
* <p>
43+
* See https://jira.mongodb.org/browse/SERVER-14710
44+
*
45+
* @deprecated since 1.7.
46+
*/
47+
@Deprecated//
48+
DROP
3949
}
4050

4151
private final Map<String, Direction> fieldSpec = new LinkedHashMap<String, Direction>();

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011 - 2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,8 @@
2626
* Collects the results of executing a group operation.
2727
*
2828
* @author Mark Pollack
29-
* @param <T> The class in which the results are mapped onto, accessible via an interator.
29+
* @author Christoph Strobl
30+
* @param <T> The class in which the results are mapped onto, accessible via an {@link Iterator}.
3031
*/
3132
public class GroupByResults<T> implements Iterable<T> {
3233

@@ -38,6 +39,7 @@ public class GroupByResults<T> implements Iterable<T> {
3839
private String serverUsed;
3940

4041
public GroupByResults(List<T> mappedResults, DBObject rawResults) {
42+
4143
Assert.notNull(mappedResults);
4244
Assert.notNull(rawResults);
4345
this.mappedResults = mappedResults;
@@ -68,21 +70,24 @@ public DBObject getRawResults() {
6870
}
6971

7072
private void parseCount() {
73+
7174
Object object = rawResults.get("count");
72-
if (object instanceof Double) {
73-
count = (Double) object;
75+
if (object instanceof Number) {
76+
count = ((Number) object).doubleValue();
7477
}
7578

7679
}
7780

7881
private void parseKeys() {
82+
7983
Object object = rawResults.get("keys");
80-
if (object instanceof Integer) {
81-
keys = (Integer) object;
84+
if (object instanceof Number) {
85+
keys = ((Number) object).intValue();
8286
}
8387
}
8488

8589
private void parseServerUsed() {
90+
8691
// "serverUsed" : "127.0.0.1:27017"
8792
Object object = rawResults.get("serverUsed");
8893
if (object instanceof String) {

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

+20-9
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public class MongoTemplateTests {
105105

106106
private static final org.springframework.data.util.Version TWO_DOT_FOUR = org.springframework.data.util.Version
107107
.parse("2.4");
108+
private static final org.springframework.data.util.Version TWO_DOT_EIGHT = org.springframework.data.util.Version
109+
.parse("2.8");
108110

109111
@Autowired MongoTemplate template;
110112
@Autowired MongoDbFactory factory;
@@ -115,7 +117,6 @@ public class MongoTemplateTests {
115117
@Rule public ExpectedException thrown = ExpectedException.none();
116118

117119
@Autowired
118-
@SuppressWarnings("unchecked")
119120
public void setMongo(Mongo mongo) throws Exception {
120121

121122
CustomConversions conversions = new CustomConversions(Arrays.asList(DateToDateTimeConverter.INSTANCE,
@@ -235,7 +236,7 @@ public void throwsExceptionForDuplicateIds() {
235236
template.insert(person);
236237
fail("Expected DataIntegrityViolationException!");
237238
} catch (DataIntegrityViolationException e) {
238-
assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$_id_ dup key:"));
239+
assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$_id_"));
239240
}
240241
}
241242

@@ -289,8 +290,7 @@ public void throwsExceptionForIndexViolationIfConfigured() {
289290
template.save(person);
290291
fail("Expected DataIntegrityViolationException!");
291292
} catch (DataIntegrityViolationException e) {
292-
assertThat(e.getMessage(),
293-
containsString("E11000 duplicate key error index: database.person.$firstName_-1 dup key:"));
293+
assertThat(e.getMessage(), containsString("E11000 duplicate key error index: database.person.$firstName_-1"));
294294
}
295295
}
296296

@@ -318,6 +318,7 @@ public void rejectsDuplicateIdInInsertAll() {
318318
}
319319

320320
@Test
321+
@SuppressWarnings("deprecation")
321322
public void testEnsureIndex() throws Exception {
322323

323324
Person p1 = new Person("Oliver");
@@ -339,19 +340,29 @@ public void testEnsureIndex() throws Exception {
339340
if ("age_-1".equals(ix.get("name"))) {
340341
indexKey = ix.get("key").toString();
341342
unique = (Boolean) ix.get("unique");
342-
dropDupes = (Boolean) ix.get("dropDups");
343+
if (mongoVersion.isLessThan(TWO_DOT_EIGHT)) {
344+
dropDupes = (Boolean) ix.get("dropDups");
345+
assertThat(dropDupes, is(true));
346+
} else {
347+
assertThat(ix.get("dropDups"), is(nullValue()));
348+
}
343349
}
344350
}
345351
assertThat(indexKey, is("{ \"age\" : -1}"));
346352
assertThat(unique, is(true));
347-
assertThat(dropDupes, is(true));
348353

349354
List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
350355

351356
assertThat(indexInfoList.size(), is(2));
352357
IndexInfo ii = indexInfoList.get(1);
353358
assertThat(ii.isUnique(), is(true));
354-
assertThat(ii.isDropDuplicates(), is(true));
359+
360+
if (mongoVersion.isLessThan(TWO_DOT_EIGHT)) {
361+
assertThat(ii.isDropDuplicates(), is(true));
362+
} else {
363+
assertThat(ii.isDropDuplicates(), is(false));
364+
}
365+
355366
assertThat(ii.isSparse(), is(false));
356367

357368
List<IndexField> indexFields = ii.getIndexFields();
@@ -677,8 +688,8 @@ public void testFindAndRemove() throws Exception {
677688
Query q = new Query(Criteria.where("text").regex("^Hello.*"));
678689
Message found1 = template.findAndRemove(q, Message.class);
679690
Message found2 = template.findAndRemove(q, Message.class);
680-
// Message notFound = template.findAndRemove(q, Message.class);
681-
DBObject notFound = template.getCollection("").findAndRemove(q.getQueryObject());
691+
692+
Message notFound = template.findAndRemove(q, Message.class);
682693
assertThat(found1, notNullValue());
683694
assertThat(found2, notNullValue());
684695
assertThat(notFound, nullValue());

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
*
5050
* @author Mark Pollack
5151
* @author Oliver Gierke
52+
* @author Christoph Strobl
5253
*/
5354
@RunWith(SpringJUnit4ClassRunner.class)
5455
@ContextConfiguration("classpath:infrastructure.xml")
@@ -60,7 +61,6 @@ public class GroupByTests {
6061
MongoTemplate mongoTemplate;
6162

6263
@Autowired
63-
@SuppressWarnings("unchecked")
6464
public void setMongo(Mongo mongo) throws Exception {
6565

6666
MongoMappingContext mappingContext = new MongoMappingContext();
@@ -116,7 +116,7 @@ public void keyFunctionCreation() {
116116
}
117117

118118
@Test
119-
public void SimpleGroup() {
119+
public void simpleGroupFunction() {
120120

121121
createGroupByData();
122122
GroupByResults<XObject> results = mongoTemplate.group(
@@ -128,7 +128,7 @@ public void SimpleGroup() {
128128
}
129129

130130
@Test
131-
public void SimpleGroupWithKeyFunction() {
131+
public void simpleGroupWithKeyFunction() {
132132

133133
createGroupByData();
134134
GroupByResults<XObject> results = mongoTemplate.group(
@@ -140,7 +140,7 @@ public void SimpleGroupWithKeyFunction() {
140140
}
141141

142142
@Test
143-
public void SimpleGroupWithFunctionsAsResources() {
143+
public void simpleGroupWithFunctionsAsResources() {
144144

145145
createGroupByData();
146146
GroupByResults<XObject> results = mongoTemplate.group(
@@ -152,7 +152,7 @@ public void SimpleGroupWithFunctionsAsResources() {
152152
}
153153

154154
@Test
155-
public void SimpleGroupWithQueryAndFunctionsAsResources() {
155+
public void simpleGroupWithQueryAndFunctionsAsResources() {
156156

157157
createGroupByData();
158158
GroupByResults<XObject> results = mongoTemplate.group(

0 commit comments

Comments
 (0)