Skip to content

Commit

Permalink
update tests to use mongo java driver API 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaldvogel committed Oct 18, 2015
1 parent ecc46b5 commit 98710eb
Show file tree
Hide file tree
Showing 30 changed files with 1,282 additions and 1,185 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
.project
.settings/
.gradle
target/
build/

*.iml

*.class
bin/

release.properties
*.iml
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ MongoDB, and probably never will.
```java
public class SimpleTest {

private DBCollection collection;
private MongoCollection<Document> collection;
private MongoClient client;
private MongoServer server;

Expand All @@ -40,25 +40,25 @@ public class SimpleTest {
InetSocketAddress serverAddress = server.bind();

client = new MongoClient(new ServerAddress(serverAddress));
collection = client.getDB("testdb").getCollection("testcollection");
collection = client.getDatabase("testdb").getCollection("testcollection");
}

@After
public void tearDown() {
client.close();
server.shutdownNow();
server.shutdown();
}

@Test
public void testSimpleInsertQuery() throws Exception {
assertEquals(0, collection.count());

// creates the database and collection in memory and insert the object
DBObject obj = new BasicDBObject("_id", 1).append("key", "value");
collection.insert(obj);
Document obj = new Document("_id", 1).append("key", "value");
collection.insertOne(obj);

assertEquals(1, collection.count());
assertEquals(obj, collection.findOne());
assertEquals(obj, collection.find().first());
}

}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ allprojects {
compileJava.options.encoding = 'UTF-8'

ext {
nettyVersion = '4.0.30.Final'
mongoJavaDriverVersion = '3.0.3'
nettyVersion = '4.0.32.Final'
mongoJavaDriverVersion = '3.1.0'
slf4jVersion = '1.7.12'
junitVersion = '4.12'
festAssertVersion = '1.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ BSONObject updateDocuments(BSONObject selector, BSONObject update, boolean isMul

BSONObject findAndModify(BSONObject query) throws MongoServerException;

int count(BSONObject query) throws MongoServerException;
int count(BSONObject query, int skip, int limit) throws MongoServerException;

int count();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private BSONObject getLog(String argument) throws MongoServerException {
log.debug("getLog: {}", argument);
BSONObject response = new BasicBSONObject();
if (argument.equals("*")) {
response.put("names", Arrays.asList("startupWarnings"));
response.put("names", Collections.singletonList("startupWarnings"));
Utils.markOkay(response);
} else if (argument.equals("startupWarnings")) {
response.put("totalLinesWritten", Integer.valueOf(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -42,7 +43,8 @@ protected boolean documentMatchesQuery(BSONObject document, BSONObject query) th
return matcher.matches(document, query);
}

protected Iterable<BSONObject> queryDocuments(BSONObject query, BSONObject orderBy, int numberToSkip, int numberToReturn) throws MongoServerException {
protected Iterable<BSONObject> queryDocuments(BSONObject query, BSONObject orderBy, int numberToSkip,
int numberToReturn) throws MongoServerException {
synchronized (indexes) {
for (Index<KEY> index : indexes) {
if (index.canHandle(query)) {
Expand All @@ -55,9 +57,11 @@ protected Iterable<BSONObject> queryDocuments(BSONObject query, BSONObject order
return matchDocuments(query, orderBy, numberToSkip, numberToReturn);
}

protected abstract Iterable<BSONObject> matchDocuments(BSONObject query, BSONObject orderBy, int numberToSkip, int numberToReturn) throws MongoServerException;
protected abstract Iterable<BSONObject> matchDocuments(BSONObject query, BSONObject orderBy, int numberToSkip,
int numberToReturn) throws MongoServerException;

protected abstract Iterable<BSONObject> matchDocuments(BSONObject query, Iterable<KEY> keys, BSONObject orderBy, int numberToSkip, int numberToReturn) throws MongoServerException;
protected abstract Iterable<BSONObject> matchDocuments(BSONObject query, Iterable<KEY> keys, BSONObject orderBy,
int numberToSkip, int numberToReturn) throws MongoServerException;

protected abstract BSONObject getDocument(KEY key);

Expand Down Expand Up @@ -124,7 +128,7 @@ void changeSubdocumentValue(Object document, String key, Object newValue, Atomic
int dotPos = key.indexOf('.');
if (dotPos > 0) {
String mainKey = key.substring(0, dotPos);
String subKey = getSubkey(key, dotPos, matchPos);
String subKey = Utils.getSubkey(key, dotPos, matchPos);

Object subObject = Utils.getFieldValueListSafe(document, mainKey);
if (subObject instanceof BSONObject || subObject instanceof List<?>) {
Expand All @@ -139,21 +143,6 @@ void changeSubdocumentValue(Object document, String key, Object newValue, Atomic
}
}

private String getSubkey(String key, int dotPos, AtomicReference<Integer> matchPos) throws MongoServerError {
String subKey = key.substring(dotPos + 1);

if (subKey.matches("\\$(\\..+)?")) {
if (matchPos == null || matchPos.get() == null) {
throw new MongoServerError(16650, //
"Cannot apply the positional operator without a corresponding query " //
+ "field containing an array.");
}
Integer pos = matchPos.getAndSet(null);
return subKey.replaceFirst("\\$", String.valueOf(pos));
}
return subKey;
}

private Object removeSubdocumentValue(Object document, String key, Integer matchPos) throws MongoServerException {
return removeSubdocumentValue(document, key, new AtomicReference<Integer>(matchPos));
}
Expand All @@ -163,7 +152,7 @@ private Object removeSubdocumentValue(Object document, String key, AtomicReferen
int dotPos = key.indexOf('.');
if (dotPos > 0) {
String mainKey = key.substring(0, dotPos);
String subKey = getSubkey(key, dotPos, matchPos);
String subKey = Utils.getSubkey(key, dotPos, matchPos);
Object subObject = Utils.getFieldValueListSafe(document, mainKey);
if (subObject instanceof BSONObject || subObject instanceof List<?>) {
return removeSubdocumentValue(subObject, subKey, matchPos);
Expand All @@ -184,7 +173,7 @@ private Object getSubdocumentValue(Object document, String key, AtomicReference<
int dotPos = key.indexOf('.');
if (dotPos > 0) {
String mainKey = key.substring(0, dotPos);
String subKey = getSubkey(key, dotPos, matchPos);
String subKey = Utils.getSubkey(key, dotPos, matchPos);
Object subObject = Utils.getFieldValueListSafe(document, mainKey);
if (subObject instanceof BSONObject || subObject instanceof List<?>) {
return getSubdocumentValue(subObject, subKey, matchPos);
Expand All @@ -196,25 +185,8 @@ private Object getSubdocumentValue(Object document, String key, AtomicReference<
}
}

private boolean hasSubdocumentValue(Object document, String key)
throws MongoServerException {
int dotPos = key.indexOf('.');
if (dotPos > 0) {
String mainKey = key.substring(0, dotPos);
String subKey = getSubkey(key, dotPos, new AtomicReference<Integer>());
Object subObject = Utils.getFieldValueListSafe(document, mainKey);
if (subObject instanceof BSONObject || subObject instanceof List<?>) {
return hasSubdocumentValue(subObject, subKey);
} else {
return false;
}
} else {
return Utils.hasFieldValueListSafe(document, key);
}
}

private void modifyField(BSONObject document, String modifier, BSONObject change, Integer matchPos, boolean isUpsert)
throws MongoServerException {
private void modifyField(BSONObject document, String modifier, BSONObject change, Integer matchPos,
boolean isUpsert) throws MongoServerException {

final UpdateOperator op;
try {
Expand Down Expand Up @@ -374,7 +346,7 @@ private void modifyField(BSONObject document, String modifier, BSONObject change
final boolean shouldChange;
// If the field does not exists, the $min/$max operator sets the
// field to the specified value
if (oldValue == null && !hasSubdocumentValue(document, key)) {
if (oldValue == null && !Utils.hasSubdocumentValue(document, key)) {
shouldChange = true;
} else if (op == UpdateOperator.MAX) {
shouldChange = valueComparison > 0;
Expand Down Expand Up @@ -447,8 +419,8 @@ private void modifyField(BSONObject document, String modifier, BSONObject change
throw new MongoServerError(16837, "Cannot update '" + key + "' and '" + key + "' at the same time");
}
if (renames.containsKey(newKey) || renames.containsValue(newKey)) {
throw new MongoServerError(16837, "Cannot update '" + newKey + "' and '" + newKey
+ "' at the same time");
throw new MongoServerError(16837,
"Cannot update '" + newKey + "' and '" + newKey + "' at the same time");
}

renames.put(key, newKey);
Expand All @@ -465,8 +437,8 @@ private void modifyField(BSONObject document, String modifier, BSONObject change
}
}

private void updatePushAllAddToSet(BSONObject document, UpdateOperator updateOperator, BSONObject change, Integer matchPos)
throws MongoServerException {
private void updatePushAllAddToSet(BSONObject document, UpdateOperator updateOperator, BSONObject change,
Integer matchPos) throws MongoServerException {
// http://docs.mongodb.org/manual/reference/operator/push/
for (String key : change.keySet()) {
Object value = getSubdocumentValue(document, key, matchPos);
Expand Down Expand Up @@ -506,7 +478,8 @@ private void updatePushAllAddToSet(BSONObject document, UpdateOperator updateOpe
list.add(val);
}
} else {
throw new MongoServerException("internal server error. illegal modifier here: " + updateOperator);
throw new MongoServerException(
"internal server error. illegal modifier here: " + updateOperator);
}
}
}
Expand Down Expand Up @@ -775,7 +748,7 @@ public ProjectingIterator(Iterator<BSONObject> iterator, BSONObject fieldSelecto

@Override
public boolean hasNext() {
return this.iterator.hasNext();
return this.iterator.hasNext();
}

@Override
Expand All @@ -787,7 +760,7 @@ public BSONObject next() {

@Override
public void remove() {
this.iterator.remove();
this.iterator.remove();
}

}
Expand Down Expand Up @@ -851,8 +824,6 @@ public synchronized int deleteDocuments(BSONObject selector, int limit) throws M
@Override
public synchronized BSONObject updateDocuments(BSONObject selector, BSONObject updateQuery, boolean isMulti,
boolean isUpsert) throws MongoServerException {
int n = 0;
boolean updatedExisting = false;

if (isMulti) {
for (String key : updateQuery.keySet()) {
Expand All @@ -862,11 +833,15 @@ public synchronized BSONObject updateDocuments(BSONObject selector, BSONObject u
}
}

int nMatched = 0;
int nModified = 0;
for (BSONObject document : queryDocuments(selector, null, 0, 0)) {
Integer matchPos = matcher.matchPosition(document, selector);
updateDocument(document, updateQuery, matchPos);
updatedExisting = true;
n++;
BSONObject oldDocument = updateDocument(document, updateQuery, matchPos);
if (!Objects.equals(oldDocument, document)) {
nModified++;
}
nMatched++;

if (!isMulti) {
break;
Expand All @@ -876,16 +851,15 @@ public synchronized BSONObject updateDocuments(BSONObject selector, BSONObject u
BSONObject result = new BasicBSONObject();

// insert?
if (n == 0 && isUpsert) {
if (nModified == 0 && isUpsert) {
BSONObject newDocument = handleUpsert(updateQuery, selector);
if (!selector.containsField(idField)) {
result.put("upserted", newDocument.get(idField));
}
n++;
}

result.put("n", Integer.valueOf(n));
result.put("updatedExisting", Boolean.valueOf(updatedExisting));
result.put("n", Integer.valueOf(nMatched));
result.put("nModified", Integer.valueOf(nModified));
return result;
}

Expand Down Expand Up @@ -989,13 +963,21 @@ public int getNumIndexes() {
}

@Override
public int count(BSONObject query) throws MongoServerException {
public int count(BSONObject query, int skip, int limit) throws MongoServerException {
if (query.keySet().isEmpty()) {
return count();
int count = count();
if (skip > 0) {
count = Math.max(0, count - skip);
}
if (limit > 0) {
return Math.min(limit, count);
}
return count;
}

int numberToReturn = (limit >= 0) ? limit : 0;
int count = 0;
Iterator<?> it = queryDocuments(query, null, 0, 0).iterator();
Iterator<?> it = queryDocuments(query, null, skip, numberToReturn).iterator();
while (it.hasNext()) {
it.next();
count++;
Expand Down Expand Up @@ -1084,6 +1066,7 @@ public void renameTo(String newDatabaseName, String newCollectionName) {
protected abstract KEY findDocument(BSONObject document);

protected abstract int getRecordCount();

protected abstract int getDeletedCount();

}
Loading

0 comments on commit 98710eb

Please sign in to comment.