@@ -646,41 +646,52 @@ public void testFindAndReplaceA16MDocument() {
646646 @ Test
647647 public void testWriteConcernExceptionOnInsert () throws UnknownHostException {
648648 assumeTrue (isReplicaSet (getMongoClient ()));
649- MongoClient mongoClient = new MongoClient (getMongoClientURI ());
650649 try {
651- DBCollection localCollection = mongoClient .getDB (collection .getDB ().getName ()).getCollection (collection .getName ());
652- WriteResult writeResult = localCollection .insert (new BasicDBObject (), new WriteConcern (5 , 1 , false , false ));
650+ WriteResult writeResult = collection .insert (new BasicDBObject (), new WriteConcern (5 , 1 ));
653651 fail ("Expected update to error. Instead, succeeded with: " + writeResult );
654652 } catch (WriteConcernException e ) {
655653 assertNotNull (e .getServerAddress ());
656654 assertNotNull (e .getErrorMessage ());
657655 assertNotNull (e .getCommandResult ().get ("err" ));
658656 assertEquals (0 , e .getCommandResult ().get ("n" ));
659- } finally {
660- mongoClient .close ();
661657 }
662658 }
663659
664660 @ Test
665661 public void testWriteConcernExceptionOnUpdate () throws UnknownHostException {
666662 assumeTrue (isReplicaSet (getMongoClient ()));
667- MongoClient mongoClient = new MongoClient (getMongoClientURI ());
668663 ObjectId id = new ObjectId ();
669664 try {
670- DBCollection localCollection = mongoClient .getDB (collection .getDB ().getName ()).getCollection (collection .getName ());
671- WriteResult writeResult = localCollection .update (new BasicDBObject ("_id" , id ),
672- new BasicDBObject ("$set" , new BasicDBObject ("x" , 1 )),
673- true , false ,
674- new WriteConcern (5 , 1 , false , false ));
665+ WriteResult writeResult = collection .update (new BasicDBObject ("_id" , id ),
666+ new BasicDBObject ("$set" , new BasicDBObject ("x" , 1 )),
667+ true , false ,
668+ new WriteConcern (5 , 1 ));
675669 fail ("Expected update to error. Instead, succeeded with: " + writeResult );
676670 } catch (WriteConcernException e ) {
677- assertNotNull (e .getServerAddress ());
678671 assertNotNull (e .getErrorMessage ());
679- assertNotNull (e .getCommandResult (). get ( "err" ) );
672+ assertTrue (e .getCode () > 0 );
680673 assertEquals (1 , e .getCommandResult ().get ("n" ));
681674 assertEquals (id , e .getCommandResult ().get ("upserted" ));
675+ }
676+ }
677+
678+ @ Test
679+ public void testWriteConcernExceptionOnFindAndModify () throws UnknownHostException {
680+ checkServerVersion (3.2 );
681+ assumeTrue (isReplicaSet (getMongoClient ()));
682+ ObjectId id = new ObjectId ();
683+ collection .setWriteConcern (new WriteConcern (5 , 1 ));
684+ try {
685+ collection .findAndModify (new BasicDBObject ("_id" , id ), null , null , false ,
686+ new BasicDBObject ("$set" , new BasicDBObject ("x" , 1 )),
687+ true , true );
688+ fail ("Expected findAndModify to error" );
689+ } catch (WriteConcernException e ) {
690+ assertNotNull (e .getServerAddress ());
691+ assertNotNull (e .getErrorMessage ());
692+ assertTrue (e .getCode () > 0 );
682693 } finally {
683- mongoClient . close ( );
694+ collection . setWriteConcern ( WriteConcern . ACKNOWLEDGED );
684695 }
685696 }
686697
0 commit comments