问题
===EDIT===
Before editing the contents, let me tell you that this question is the exact duplicate of Trying to create a mongodb collection in grails using mongodb plugin. However, the solution given doesn't work for me, and the context in which I am working may be different. So, this question.
==========
I am getting the following capped collections related error while running tests for capped collections over mongo. Following are the version of softwares that I am using:
- Grails 2.1.2
- MongoDB 2.4.0
- Java 1.6.0.45
- Groovy 1.8.8
- Mongo Java Driver 2.12.3
====EDIT=====
I have a class CappedCollectionCreator
as follows:
class CappedCollectionCreator {
public void createCappedPublishedCollection(DBApiLayer db, String publishedCollectionName, String archiveCollectionName, int size, int max) {
if (db.collectionExists(publishedCollectionName)) {
def publishedCollection = db.getCollection(publishedCollectionName)
if (!publishedCollection.getStats().get("capped")) {
if (!db.collectionExists(archiveCollectionName)) {
def documents = publishedCollection.find().toArray()
if (documents.size() > 0) {
def archiveCollection = db.createCollection(archiveCollectionName, null)
archiveCollection.insert(publishedCollection.find().toArray())
}
}
def firstDocumentInserted = publishedCollection.find().sort(new BasicDBObject("createdOn", 1)).limit(1).toArray()
publishedCollection.drop()
db.createCollection(publishedCollectionName, BasicDBObjectBuilder.start().add("capped", true).add("size", size).add("max", max).get())
publishedCollection = db.getCollection(publishedCollectionName)
publishedCollection.insert(firstDocumentInserted)
}
} else {
db.createCollection(publishedCollectionName, BasicDBObjectBuilder.start().add("capped", true).add("size", size).add("max", max).get())
}
}
}
The code that I am having problem with is in the following snippet:
@TestMixin(GroovyTestCase)
class CappedCollectionCreatorTests {
def mongo
def db
def mongoCollectionProcessor
def collectionName = "cappedCollectionTest"
def archiveName = "cappedCollectionTestArchive"
def collection, archiveCollection
@Before
void setUp() {
mongo = mongo.getMongo()
db = mongo.getDB("test")
mongoCollectionProcessor = new CappedCollectionCreator()
if (db.collectionExists(collectionName)) {
collection = db.getCollection(collectionName);
collection.drop();
}
if (db.collectionExists(archiveName)) {
archiveCollection = db.getCollection(archiveName);
archiveCollection.drop();
}
}
@Test
public void testShouldCreateCappedCollectionWhenDoesNotExist() {
mongoCollectionProcessor.createCappedPublishedCollection(db, collectionName, archiveName, 1000, 1)
assert db.collectionExists(collectionName) == true
assert db.collectionExists(archiveName) == false
assert db.getCollection(collectionName).isCapped() == true
}
}
==========
I searched over the Internet and tried most of the first searches such as using DBObject and Map interchageably, casting DBObject to Map, etc. But none seems to work.
| Running 339 integration tests... 245 of 339
| Running 339 integration tests... 246 of 339
| Failure: testShouldCreateCappedCollectionWhenDoesNotExist(uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests)
| groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.DBApiLayer#createCollection.
Cannot resolve which method to invoke for [class java.lang.String, class com.mongodb.BasicDBObject] due to overlapping prototypes between:
[class java.lang.String, interface com.mongodb.DBObject]
[class java.lang.String, interface java.util.Map]
at uk.co.o2.pm.manager.infrastructure.CappedCollectionCreator.createCappedPublishedCollection(CappedCollectionCreator.groovy:34)
at uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests.testShouldCreateCappedCollectionWhenDoesNotExist(CappedCollectionCreatorTests.groovy:43)
| Running 339 integration tests... 247 of 339
| Failure: testShouldNotCreateCappedCollectionWhenExists(uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests)
| groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.DBApiLayer#createCollection.
Cannot resolve which method to invoke for [class java.lang.String, class com.mongodb.BasicDBObject] due to overlapping prototypes between:
[class java.lang.String, interface com.mongodb.DBObject]
[class java.lang.String, interface java.util.Map]
at uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests.testShouldNotCreateCappedCollectionWhenExists(CappedCollectionCreatorTests.groovy:54)
| Running 339 integration tests... 248 of 339
| Failure: testShouldCreateCappedCollectionAndArchiveWhenExistsNoCappedCollection(uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests)
| groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.DBApiLayer#createCollection.
Cannot resolve which method to invoke for [class java.lang.String, null] due to overlapping prototypes between:
[class java.lang.String, interface com.mongodb.DBObject]
[class java.lang.String, interface java.util.Map]
at uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests.testShouldCreateCappedCollectionAndArchiveWhenExistsNoCappedCollection(CappedCollectionCreatorTests.groovy:70)
| Running 339 integration tests... 249 of 339
| Failure: testShouldCreateCappedCollectionAndNoArchiveWhenExistsEmptyNoCappedCollection(uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests)
| groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.DBApiLayer#createCollection.
Cannot resolve which method to invoke for [class java.lang.String, null] due to overlapping prototypes between:
[class java.lang.String, interface com.mongodb.DBObject]
[class java.lang.String, interface java.util.Map]
at uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests.testShouldCreateCappedCollectionAndNoArchiveWhenExistsEmptyNoCappedCollection(CappedCollectionCreatorTests.groovy:90)
| Running 339 integration tests... 250 of 339
| Failure: testShouldCreateCappedCollectionAndNoArchiveWhenExistsNoCappedCollectionAndArchiveCollection(uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests)
| groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.DBApiLayer#createCollection.
Cannot resolve which method to invoke for [class java.lang.String, null] due to overlapping prototypes between:
[class java.lang.String, interface com.mongodb.DBObject]
[class java.lang.String, interface java.util.Map]
at uk.co.o2.pm.manager.infrastructure.CappedCollectionCreatorTests.testShouldCreateCappedCollectionAndNoArchiveWhenExistsNoCappedCollectionAndArchiveCollection(CappedCollectionCreatorTests.groovy:103)
| Running 339 integration tests... 251 of 339
| Running 339 integration tests... 252 of 339
Any help would be appreciated.
Seems to be a problem with Mongo Groovy driver not being able to make out the method signature dynamically. Any ideas?
来源:https://stackoverflow.com/questions/25545865/randomly-getting-capped-collection-error-in-tests