forked from MorphiaOrg/morphia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue 634] Merged changes that fixes issue MorphiaOrg#634. Added uni…
…t test for the changes, and backed out the changed to countAll that are not necessary.
- Loading branch information
Showing
5 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.mongodb; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Somewhat ugly, but effective, way to stub out the collection. Still needs a real DB, otherwise you get null pointers, | ||
* but it does allow you to override with your own behaviours. | ||
* | ||
* This is currently being used to unit test QueryImpl. | ||
*/ | ||
public class StubDBCollection extends DBCollection { | ||
|
||
private StubDBCursor stubDBCursor; | ||
|
||
public StubDBCollection(final DB db) { | ||
super(db, ""); | ||
} | ||
|
||
@Override | ||
public WriteResult insert(final List<DBObject> list, final WriteConcern concern, final DBEncoder encoder) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
public WriteResult update(final DBObject q, final DBObject o, final boolean upsert, final boolean multi, | ||
final WriteConcern concern, final DBEncoder encoder) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
protected void doapply(final DBObject o) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
public WriteResult remove(final DBObject o, final WriteConcern concern, final DBEncoder encoder) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
QueryResultIterator find(final DBObject ref, final DBObject fields, final int numToSkip, final int batchSize, final int limit, | ||
final int options, final ReadPreference readPref, | ||
final DBDecoder decoder) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
QueryResultIterator find(final DBObject ref, final DBObject fields, final int numToSkip, final int batchSize, final int limit, | ||
final int options, final ReadPreference readPref, | ||
final DBDecoder decoder, final DBEncoder encoder) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
@Deprecated | ||
public void createIndex(final DBObject keys, final DBObject options, final DBEncoder encoder) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
public Cursor aggregate(final List<DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
public List<Cursor> parallelScan(final ParallelScanOptions options) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
BulkWriteResult executeBulkWriteOperation(final boolean ordered, final List<WriteRequest> requests, final WriteConcern writeConcern, | ||
final DBEncoder encoder) { | ||
throw new UnsupportedOperationException("Not implemented yet!"); | ||
} | ||
|
||
@Override | ||
public DBCursor find(final DBObject ref, final DBObject keys) { | ||
return stubDBCursor; | ||
} | ||
|
||
public void setDBCursor(final StubDBCursor stubDBCursor) { | ||
this.stubDBCursor = stubDBCursor; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mongodb; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Somewhat ugly, but effective, way to stub out the collection. Can be supplied with a StubDBCollection. Can override the cursor | ||
* methods to check what's actually being called. | ||
* | ||
* This is currently being used to unit test QueryImpl. | ||
*/ | ||
public class StubDBCursor extends DBCursor { | ||
private long maxTime; | ||
private TimeUnit maxTimeUnit; | ||
|
||
public StubDBCursor(final DBCollection collection) { | ||
super(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()); | ||
} | ||
|
||
@Override | ||
public DBCursor maxTime(final long maxTime, final TimeUnit timeUnit) { | ||
this.maxTime = maxTime; | ||
this.maxTimeUnit = timeUnit; | ||
return this; | ||
} | ||
|
||
public long getMaxTime() { | ||
return maxTime; | ||
} | ||
|
||
public TimeUnit getMaxTimeUnit() { | ||
return maxTimeUnit; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
morphia/src/test/java/org/mongodb/morphia/query/QueryImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.mongodb.morphia.query; | ||
|
||
import com.mongodb.StubDBCollection; | ||
import com.mongodb.StubDBCursor; | ||
import org.junit.Test; | ||
import org.mongodb.morphia.TestBase; | ||
import org.mongodb.morphia.entities.SimpleEntity; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class QueryImplTest extends TestBase { | ||
|
||
@Test | ||
public void testMaxTimeApplied() { | ||
// given | ||
StubDBCollection coll = new StubDBCollection(getDb()); | ||
StubDBCursor stubDBCursor = new StubDBCursor(coll); | ||
coll.setDBCursor(stubDBCursor); | ||
|
||
QueryImpl<SimpleEntity> query = new QueryImpl<SimpleEntity>(SimpleEntity.class, coll, getDs()); | ||
|
||
long maxTime = 123; | ||
TimeUnit maxTimeUnit = TimeUnit.MILLISECONDS; | ||
|
||
// when | ||
query.maxTime(maxTime, maxTimeUnit); | ||
query.prepareCursor(); | ||
|
||
// then | ||
assertThat(stubDBCursor.getMaxTime(), is(maxTime)); | ||
assertThat(stubDBCursor.getMaxTimeUnit(), is(maxTimeUnit)); | ||
} | ||
|
||
} |