Skip to content

Commit

Permalink
Take into account MongoClient's write concern (fixes #1053)
Browse files Browse the repository at this point in the history
fixes #1061
  • Loading branch information
Justin Lee committed Oct 31, 2016
1 parent d0e5329 commit 04d2554
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
3 changes: 2 additions & 1 deletion morphia/src/main/java/org/mongodb/morphia/DatastoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class DatastoreImpl implements AdvancedDatastore {
private final IndexHelper indexHelper;
private DB db;
private Mapper mapper;
private WriteConcern defConcern = WriteConcern.ACKNOWLEDGED;
private WriteConcern defConcern;
private DBDecoderFactory decoderFactory;

private volatile QueryFactory queryFactory = new DefaultQueryFactory();
Expand Down Expand Up @@ -120,6 +120,7 @@ private DatastoreImpl(final Morphia morphia, final Mapper mapper, final MongoCli
this.mongoClient = mongoClient;
this.database = database;
this.db = mongoClient.getDB(database.getName());
this.defConcern = mongoClient.getWriteConcern();
this.indexHelper = new IndexHelper(mapper, database);
}

Expand Down
16 changes: 12 additions & 4 deletions morphia/src/test/java/org/mongodb/morphia/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ public abstract class TestBase {
private final Datastore ds;

protected TestBase() {
mongoClient = new MongoClient(new MongoClientURI(System.getProperty("MONGO_URI", "mongodb://localhost:27017")));
db = getMongoClient().getDB(TEST_DB_NAME);
database = getMongoClient().getDatabase(TEST_DB_NAME);
ds = getMorphia().createDatastore(getMongoClient(), getDb().getName());
this(new MongoClient(new MongoClientURI(getMongoURI())));
}

protected TestBase(final MongoClient mongoClient) {
this.mongoClient = mongoClient;
this.db = getMongoClient().getDB(TEST_DB_NAME);
this.database = getMongoClient().getDatabase(TEST_DB_NAME);
this.ds = getMorphia().createDatastore(getMongoClient(), getDb().getName());
}

protected static String getMongoURI() {
return System.getProperty("MONGO_URI", "mongodb://localhost:27017");
}

public AdvancedDatastore getAds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* @author Scott Hernandez
*/
//@RunWith(Parameterized.class)
@SuppressWarnings("deprecation")
public class TestDatastore extends TestBase {

@Test(expected = UpdateException.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (C) 2010 Olafur Gauti Gudmundsson
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/


package org.mongodb.morphia;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import org.junit.Test;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;

import static com.mongodb.WriteConcern.UNACKNOWLEDGED;
import static org.junit.Assert.assertEquals;

public class TestMongoClientWriteConcern extends TestBase {

public TestMongoClientWriteConcern() {
super(new MongoClient(new MongoClientURI(getMongoURI(),
MongoClientOptions.builder().writeConcern(UNACKNOWLEDGED))));
}

@Test
public void defaultWriteConcern() throws Exception {
getAds().insert(new SimpleEntity(1));
getAds().insert(new SimpleEntity(1));
assertEquals(1, getDs().getCount(SimpleEntity.class));
}

@Entity
private static class SimpleEntity {

@Id
private int id;

public SimpleEntity(final int id) {
this.id = id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
/**
* @author Scott Hernandez
*/
@SuppressWarnings("UnusedDeclaration") // Morphia uses the fields
@SuppressWarnings({"UnusedDeclaration", "deprecation"}) // Morphia uses the fields
public class TestUpdateOps extends TestBase {
private static final Logger LOG = get(TestUpdateOps.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public Long getValue() {
}
}

@SuppressWarnings("deprecation")
@PrePersist
void prePersist() {
if (myLongId == null) {
Expand Down

0 comments on commit 04d2554

Please sign in to comment.