DATAMONGO-848 - Added tweaks to be compatible with Java driver 2.12.
Added build profile to be able to build against next Mongo Java driver version (2.12.0-rc0) currently. Tweaked Bundlor version replacements to allow binding non-OSGi compatible Mongo driver versions. Exception translator now handles newly introduced MongoServerSelectionException which the driver throws to indicate it can't connect to a MongoDB instance as of driver version 2.12. GridFsTemplate now uses an empty query object instead of null to indicate that no query should be used. Adapted test cases to be able to deal with the slightly changed representation of serverUsed in command results (2.12 removed leading slash).
This commit is contained in:
11
pom.xml
11
pom.xml
@@ -31,6 +31,7 @@
|
|||||||
<dist.id>spring-data-mongodb</dist.id>
|
<dist.id>spring-data-mongodb</dist.id>
|
||||||
<springdata.commons>1.7.0.RC1</springdata.commons>
|
<springdata.commons>1.7.0.RC1</springdata.commons>
|
||||||
<mongo>2.11.4</mongo>
|
<mongo>2.11.4</mongo>
|
||||||
|
<mongo-osgi>${mongo}</mongo-osgi>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<developers>
|
<developers>
|
||||||
@@ -102,6 +103,16 @@
|
|||||||
</developer>
|
</developer>
|
||||||
</developers>
|
</developers>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>mongo-next</id>
|
||||||
|
<properties>
|
||||||
|
<mongo>2.12.0-rc0</mongo>
|
||||||
|
<mongo-osgi>2.12.0</mongo-osgi>
|
||||||
|
</properties>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- MongoDB -->
|
<!-- MongoDB -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ Import-Package:
|
|||||||
Export-Template:
|
Export-Template:
|
||||||
org.springframework.data.mongodb.crossstore.*;version="${project.version}"
|
org.springframework.data.mongodb.crossstore.*;version="${project.version}"
|
||||||
Import-Template:
|
Import-Template:
|
||||||
com.mongodb.*;version="0",
|
com.mongodb.*;version="${mongo-osgi:[=.=.=,+1.0.0)}",
|
||||||
javax.persistence.*;version="${jpa:[=.=.=,+1.0.0)}",
|
javax.persistence.*;version="${jpa:[=.=.=,+1.0.0)}",
|
||||||
org.aspectj.*;version="${aspectj:[1.0.0, 2.0.0)}",
|
org.aspectj.*;version="${aspectj:[1.0.0, 2.0.0)}",
|
||||||
org.bson.*;version="0",
|
org.bson.*;version="0",
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ Bundle-ManifestVersion: 2
|
|||||||
Import-Package:
|
Import-Package:
|
||||||
sun.reflect;version="0";resolution:=optional
|
sun.reflect;version="0";resolution:=optional
|
||||||
Import-Template:
|
Import-Template:
|
||||||
com.mongodb.*;version="${mongo:[=.=,+1.0.0)}",
|
com.mongodb.*;version="${mongo-osgi:[=.=.=,+1.0.0)}",
|
||||||
org.apache.log4j.*;version="${log4j:[=.=.=,+1.0.0)}"
|
org.apache.log4j.*;version="${log4j:[=.=.=,+1.0.0)}"
|
||||||
|
|||||||
@@ -145,7 +145,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.mysema.maven</groupId>
|
<groupId>com.mysema.maven</groupId>
|
||||||
<artifactId>apt-maven-plugin</artifactId>
|
<artifactId>apt-maven-plugin</artifactId>
|
||||||
<version>1.0.8</version>
|
<version>${apt}</version>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysema.querydsl</groupId>
|
<groupId>com.mysema.querydsl</groupId>
|
||||||
|
|||||||
@@ -60,6 +60,11 @@ public class MongoExceptionTranslator implements PersistenceExceptionTranslator
|
|||||||
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Driver 2.12 throws this to indicate connection problems. String comparison to avoid hard dependency
|
||||||
|
if (ex.getClass().getName().equals("com.mongodb.MongoServerSelectionException")) {
|
||||||
|
return new DataAccessResourceFailureException(ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
|
||||||
if (ex instanceof MongoInternalException) {
|
if (ex instanceof MongoInternalException) {
|
||||||
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
|
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
|||||||
}
|
}
|
||||||
|
|
||||||
private DBObject getMappedQuery(Query query) {
|
private DBObject getMappedQuery(Query query) {
|
||||||
return query == null ? null : getMappedQuery(query.getQueryObject());
|
return query == null ? new Query().getQueryObject() : getMappedQuery(query.getQueryObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
private DBObject getMappedQuery(DBObject query) {
|
private DBObject getMappedQuery(DBObject query) {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2011 by the original author(s).
|
* Copyright 2011-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
@@ -15,33 +15,32 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.mongodb.config;
|
package org.springframework.data.mongodb.config;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.dao.DataAccessResourceFailureException;
|
import org.springframework.dao.DataAccessResourceFailureException;
|
||||||
|
import org.springframework.data.mongodb.MongoDbFactory;
|
||||||
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests for {@link MongoDbFactory}.
|
* Integration tests for {@link MongoDbFactory}.
|
||||||
*
|
*
|
||||||
* @author Thomas Risbergf
|
* @author Thomas Risberg
|
||||||
|
* @author Oliver Gierke
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration
|
@ContextConfiguration
|
||||||
public class MongoDbFactoryNoDatabaseRunningTests {
|
public class MongoDbFactoryNoDatabaseRunningTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired MongoTemplate mongoTemplate;
|
||||||
MongoTemplate mongoTemplate;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATADOC-139
|
* @see DATAMONGO-139
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void startsUpWithoutADatabaseRunning() {
|
public void startsUpWithoutADatabaseRunning() {
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ public class AggregationTests {
|
|||||||
AggregationResults<TagCount> results = mongoTemplate.aggregate(agg, INPUT_COLLECTION, TagCount.class);
|
AggregationResults<TagCount> results = mongoTemplate.aggregate(agg, INPUT_COLLECTION, TagCount.class);
|
||||||
|
|
||||||
assertThat(results, is(notNullValue()));
|
assertThat(results, is(notNullValue()));
|
||||||
assertThat(results.getServerUsed(), is("/127.0.0.1:27017"));
|
assertThat(results.getServerUsed(), endsWith("127.0.0.1:27017"));
|
||||||
|
|
||||||
List<TagCount> tagCount = results.getMappedResults();
|
List<TagCount> tagCount = results.getMappedResults();
|
||||||
|
|
||||||
@@ -217,7 +217,7 @@ public class AggregationTests {
|
|||||||
AggregationResults<TagCount> results = mongoTemplate.aggregate(aggregation, INPUT_COLLECTION, TagCount.class);
|
AggregationResults<TagCount> results = mongoTemplate.aggregate(aggregation, INPUT_COLLECTION, TagCount.class);
|
||||||
|
|
||||||
assertThat(results, is(notNullValue()));
|
assertThat(results, is(notNullValue()));
|
||||||
assertThat(results.getServerUsed(), is("/127.0.0.1:27017"));
|
assertThat(results.getServerUsed(), endsWith("127.0.0.1:27017"));
|
||||||
|
|
||||||
List<TagCount> tagCount = results.getMappedResults();
|
List<TagCount> tagCount = results.getMappedResults();
|
||||||
|
|
||||||
@@ -241,7 +241,7 @@ public class AggregationTests {
|
|||||||
AggregationResults<TagCount> results = mongoTemplate.aggregate(aggregation, INPUT_COLLECTION, TagCount.class);
|
AggregationResults<TagCount> results = mongoTemplate.aggregate(aggregation, INPUT_COLLECTION, TagCount.class);
|
||||||
|
|
||||||
assertThat(results, is(notNullValue()));
|
assertThat(results, is(notNullValue()));
|
||||||
assertThat(results.getServerUsed(), is("/127.0.0.1:27017"));
|
assertThat(results.getServerUsed(), endsWith("127.0.0.1:27017"));
|
||||||
|
|
||||||
List<TagCount> tagCount = results.getMappedResults();
|
List<TagCount> tagCount = results.getMappedResults();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2011-2012 the original author or authors.
|
* Copyright 2011-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.mongodb.core.mapreduce;
|
package org.springframework.data.mongodb.core.mapreduce;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
import static org.springframework.data.mongodb.core.mapreduce.GroupBy.*;
|
import static org.springframework.data.mongodb.core.mapreduce.GroupBy.*;
|
||||||
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||||
|
|
||||||
@@ -42,6 +44,12 @@ import com.mongodb.DBCollection;
|
|||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
import com.mongodb.Mongo;
|
import com.mongodb.Mongo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for group-by operations.
|
||||||
|
*
|
||||||
|
* @author Mark Pollack
|
||||||
|
* @author Oliver Gierke
|
||||||
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:infrastructure.xml")
|
@ContextConfiguration("classpath:infrastructure.xml")
|
||||||
public class GroupByTests {
|
public class GroupByTests {
|
||||||
@@ -84,49 +92,46 @@ public class GroupByTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void singleKeyCreation() {
|
public void singleKeyCreation() {
|
||||||
|
|
||||||
DBObject gc = new GroupBy("a").getGroupByObject();
|
DBObject gc = new GroupBy("a").getGroupByObject();
|
||||||
// String expected =
|
|
||||||
// "{ \"group\" : { \"ns\" : \"test\" , \"key\" : { \"a\" : 1} , \"cond\" : null , \"$reduce\" : null , \"initial\" : null }}";
|
assertThat(gc.toString(), is("{ \"key\" : { \"a\" : 1} , \"$reduce\" : null , \"initial\" : null }"));
|
||||||
String expected = "{ \"key\" : { \"a\" : 1} , \"$reduce\" : null , \"initial\" : null }";
|
|
||||||
Assert.assertEquals(expected, gc.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void multipleKeyCreation() {
|
public void multipleKeyCreation() {
|
||||||
|
|
||||||
DBObject gc = GroupBy.key("a", "b").getGroupByObject();
|
DBObject gc = GroupBy.key("a", "b").getGroupByObject();
|
||||||
// String expected =
|
|
||||||
// "{ \"group\" : { \"ns\" : \"test\" , \"key\" : { \"a\" : 1 , \"b\" : 1} , \"cond\" : null , \"$reduce\" : null , \"initial\" : null }}";
|
assertThat(gc.toString(), is("{ \"key\" : { \"a\" : 1 , \"b\" : 1} , \"$reduce\" : null , \"initial\" : null }"));
|
||||||
String expected = "{ \"key\" : { \"a\" : 1 , \"b\" : 1} , \"$reduce\" : null , \"initial\" : null }";
|
|
||||||
Assert.assertEquals(expected, gc.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void keyFunctionCreation() {
|
public void keyFunctionCreation() {
|
||||||
|
|
||||||
DBObject gc = GroupBy.keyFunction("classpath:keyFunction.js").getGroupByObject();
|
DBObject gc = GroupBy.keyFunction("classpath:keyFunction.js").getGroupByObject();
|
||||||
String expected = "{ \"$keyf\" : \"classpath:keyFunction.js\" , \"$reduce\" : null , \"initial\" : null }";
|
|
||||||
Assert.assertEquals(expected, gc.toString());
|
assertThat(gc.toString(),
|
||||||
|
is("{ \"$keyf\" : \"classpath:keyFunction.js\" , \"$reduce\" : null , \"initial\" : null }"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void SimpleGroup() {
|
public void SimpleGroup() {
|
||||||
createGroupByData();
|
|
||||||
GroupByResults<XObject> results;
|
|
||||||
|
|
||||||
results = mongoTemplate.group(
|
createGroupByData();
|
||||||
|
GroupByResults<XObject> results = mongoTemplate.group(
|
||||||
"group_test_collection",
|
"group_test_collection",
|
||||||
GroupBy.key("x").initialDocument(new BasicDBObject("count", 0))
|
GroupBy.key("x").initialDocument(new BasicDBObject("count", 0))
|
||||||
.reduceFunction("function(doc, prev) { prev.count += 1 }"), XObject.class);
|
.reduceFunction("function(doc, prev) { prev.count += 1 }"), XObject.class);
|
||||||
|
|
||||||
assertMapReduceResults(results);
|
assertMapReduceResults(results);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void SimpleGroupWithKeyFunction() {
|
public void SimpleGroupWithKeyFunction() {
|
||||||
createGroupByData();
|
|
||||||
GroupByResults<XObject> results;
|
|
||||||
|
|
||||||
results = mongoTemplate.group(
|
createGroupByData();
|
||||||
|
GroupByResults<XObject> results = mongoTemplate.group(
|
||||||
"group_test_collection",
|
"group_test_collection",
|
||||||
GroupBy.keyFunction("function(doc) { return { x : doc.x }; }").initialDocument("{ count: 0 }")
|
GroupBy.keyFunction("function(doc) { return { x : doc.x }; }").initialDocument("{ count: 0 }")
|
||||||
.reduceFunction("function(doc, prev) { prev.count += 1 }"), XObject.class);
|
.reduceFunction("function(doc, prev) { prev.count += 1 }"), XObject.class);
|
||||||
@@ -136,30 +141,35 @@ public class GroupByTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void SimpleGroupWithFunctionsAsResources() {
|
public void SimpleGroupWithFunctionsAsResources() {
|
||||||
createGroupByData();
|
|
||||||
GroupByResults<XObject> results;
|
|
||||||
|
|
||||||
results = mongoTemplate.group("group_test_collection", GroupBy.keyFunction("classpath:keyFunction.js")
|
createGroupByData();
|
||||||
.initialDocument("{ count: 0 }").reduceFunction("classpath:groupReduce.js"), XObject.class);
|
GroupByResults<XObject> results = mongoTemplate.group(
|
||||||
|
"group_test_collection",
|
||||||
|
GroupBy.keyFunction("classpath:keyFunction.js").initialDocument("{ count: 0 }")
|
||||||
|
.reduceFunction("classpath:groupReduce.js"), XObject.class);
|
||||||
|
|
||||||
assertMapReduceResults(results);
|
assertMapReduceResults(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void SimpleGroupWithQueryAndFunctionsAsResources() {
|
public void SimpleGroupWithQueryAndFunctionsAsResources() {
|
||||||
createGroupByData();
|
|
||||||
GroupByResults<XObject> results;
|
|
||||||
|
|
||||||
results = mongoTemplate.group(where("x").gt(0), "group_test_collection", keyFunction("classpath:keyFunction.js")
|
createGroupByData();
|
||||||
.initialDocument("{ count: 0 }").reduceFunction("classpath:groupReduce.js"), XObject.class);
|
GroupByResults<XObject> results = mongoTemplate.group(
|
||||||
|
where("x").gt(0),
|
||||||
|
"group_test_collection",
|
||||||
|
keyFunction("classpath:keyFunction.js").initialDocument("{ count: 0 }").reduceFunction(
|
||||||
|
"classpath:groupReduce.js"), XObject.class);
|
||||||
|
|
||||||
assertMapReduceResults(results);
|
assertMapReduceResults(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertMapReduceResults(GroupByResults<XObject> results) {
|
private void assertMapReduceResults(GroupByResults<XObject> results) {
|
||||||
|
|
||||||
DBObject dboRawResults = results.getRawResults();
|
DBObject dboRawResults = results.getRawResults();
|
||||||
String expected = "{ \"serverUsed\" : \"/127.0.0.1:27017\" , \"retval\" : [ { \"x\" : 1.0 , \"count\" : 2.0} , { \"x\" : 2.0 , \"count\" : 1.0} , { \"x\" : 3.0 , \"count\" : 3.0}] , \"count\" : 6.0 , \"keys\" : 3 , \"ok\" : 1.0}";
|
|
||||||
Assert.assertEquals(expected, dboRawResults.toString());
|
assertThat(dboRawResults.containsField("serverUsed"), is(true));
|
||||||
|
assertThat(dboRawResults.get("serverUsed").toString(), endsWith("127.0.0.1:27017"));
|
||||||
|
|
||||||
int numResults = 0;
|
int numResults = 0;
|
||||||
for (XObject xObject : results) {
|
for (XObject xObject : results) {
|
||||||
@@ -174,13 +184,15 @@ public class GroupByTests {
|
|||||||
}
|
}
|
||||||
numResults++;
|
numResults++;
|
||||||
}
|
}
|
||||||
Assert.assertEquals(3, numResults);
|
assertThat(numResults, is(3));
|
||||||
Assert.assertEquals(6, results.getCount(), 0.001);
|
assertThat(results.getKeys(), is(3));
|
||||||
Assert.assertEquals(3, results.getKeys());
|
assertEquals(6, results.getCount(), 0.001);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createGroupByData() {
|
private void createGroupByData() {
|
||||||
|
|
||||||
DBCollection c = mongoTemplate.getDb().getCollection("group_test_collection");
|
DBCollection c = mongoTemplate.getDb().getCollection("group_test_collection");
|
||||||
|
|
||||||
c.save(new BasicDBObject("x", 1));
|
c.save(new BasicDBObject("x", 1));
|
||||||
c.save(new BasicDBObject("x", 1));
|
c.save(new BasicDBObject("x", 1));
|
||||||
c.save(new BasicDBObject("x", 2));
|
c.save(new BasicDBObject("x", 2));
|
||||||
@@ -188,5 +200,4 @@ public class GroupByTests {
|
|||||||
c.save(new BasicDBObject("x", 3));
|
c.save(new BasicDBObject("x", 3));
|
||||||
c.save(new BasicDBObject("x", 3));
|
c.save(new BasicDBObject("x", 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ Export-Template:
|
|||||||
org.springframework.data.mongodb.*;version="${project.version}"
|
org.springframework.data.mongodb.*;version="${project.version}"
|
||||||
Import-Template:
|
Import-Template:
|
||||||
com.google.common.base.*;version="[11.0.0,14.0.0)";resolution:=optional,
|
com.google.common.base.*;version="[11.0.0,14.0.0)";resolution:=optional,
|
||||||
com.mongodb.*;version="0",
|
com.mongodb.*;version="${mongo-osgi:[=.=.=,+1.0.0)}",
|
||||||
com.mysema.query.*;version="[2.1.1, 3.0.0)";resolution:=optional,
|
com.mysema.query.*;version="[2.1.1, 3.0.0)";resolution:=optional,
|
||||||
javax.annotation.processing.*;version="0",
|
javax.annotation.processing.*;version="0",
|
||||||
javax.enterprise.*;version="${cdi:[=.=.=,+1.0.0)}";resolution:=optional,
|
javax.enterprise.*;version="${cdi:[=.=.=,+1.0.0)}";resolution:=optional,
|
||||||
|
|||||||
Reference in New Issue
Block a user