1818
1919
2020
21+
22+
2123package com.mongodb
2224
2325import org.mongodb.Document
2426import org.mongodb.codecs.DocumentCodec
2527import org.mongodb.command.MongoCommandFailureException
2628import org.mongodb.connection.Cluster
29+ import org.mongodb.connection.ClusterConnectionMode
30+ import org.mongodb.connection.ClusterDescription
2731import org.mongodb.session.Session
2832import spock.lang.Specification
2933import spock.lang.Subject
3034
35+ import static com.mongodb.MongoExceptions.mapException
3136import static com.mongodb.ReadPreference.primary
3237import static com.mongodb.WriteConcern.ACKNOWLEDGED
3338
3439class DBCollectionSpecification extends Specification {
35- private final Mongo mongo = Mock ()
36- private final DB database = new DB(mongo, ' myDatabase ' , new DocumentCodec () )
40+ // private final Mongo mongo = Mock()
41+ private final DB database = Mock ( )
3742 private final Session session = Mock ()
3843 private final Cluster cluster = Mock ()
3944
4045 @Subject
4146 private final DBCollection collection = new DBCollection (' collectionName' , database, new DocumentCodec ())
4247
4348 def setup () {
44- mongo. getCluster() >> { cluster }
45- mongo. getSession() >> { session }
49+ database. getSession() >> { session }
50+ database. getCluster() >> { cluster }
51+ database. getName() >> { ' TheDatabase' }
52+ database. getClusterDescription() >> { cluster. getDescription() }
53+ cluster. getDescription() >> { new ClusterDescription (ClusterConnectionMode.Direct ) }
4654
4755 // TODO: this shouldn't be required. I think.
4856 database. setReadPreference(primary())
4957 }
5058
5159 def ' should throw com.mongodb.MongoException if rename fails' () {
5260 given :
53- session. execute (_) >> { throw new org.mongodb.MongoException (' The error from the new Java layer' ) }
61+ session. createServerConnectionProvider (_) >> { throw new org.mongodb.MongoException (' The error from the new Java layer' ) }
5462
5563 when :
5664 collection. rename(' newCollectionName' );
@@ -61,7 +69,7 @@ class DBCollectionSpecification extends Specification {
6169
6270 def ' should throw com.mongodb.MongoException when update fails' () {
6371 given :
64- session. execute (_) >> { throw new org.mongodb.MongoException (' The error from the new Java layer' ) }
72+ session. createServerConnectionProvider (_) >> { throw new org.mongodb.MongoException (' The error from the new Java layer' ) }
6573
6674 when :
6775 collection. update(new BasicDBObject (), new BasicDBObject (), false , false , ACKNOWLEDGED );
@@ -72,11 +80,11 @@ class DBCollectionSpecification extends Specification {
7280
7381 def ' should throw MongoDuplicateKeyException when insert fails' () {
7482 given :
75- session. execute (_) >> {
83+ session. createServerConnectionProvider (_) >> {
7684 throw new org.mongodb.command.MongoDuplicateKeyException (new org.mongodb.operation.CommandResult (new Document (),
77- new org.mongodb.connection.ServerAddress (),
78- new Document (),
79- 15L ))
85+ new org.mongodb.connection.ServerAddress (),
86+ new Document (),
87+ 15L ))
8088 }
8189
8290 when :
@@ -88,7 +96,7 @@ class DBCollectionSpecification extends Specification {
8896
8997 def ' should wrap org.mongodb.MongoException as a com.mongodb.MongoException when insert fails' () {
9098 given :
91- session. execute (_) >> { throw new org.mongodb.MongoInternalException (' Exception that should not escape' ) }
99+ session. createServerConnectionProvider (_) >> { throw new org.mongodb.MongoInternalException (' Exception that should not escape' ) }
92100
93101 when :
94102 collection. insert(new BasicDBObject (), ACKNOWLEDGED );
@@ -99,11 +107,13 @@ class DBCollectionSpecification extends Specification {
99107
100108 def ' should throw com.mongodb.CommandFailureException when group fails' () {
101109 given :
102- session. execute(_) >> {
103- throw new MongoCommandFailureException (new org.mongodb.operation.CommandResult (new Document (),
104- new org.mongodb.connection.ServerAddress (),
105- new Document (),
106- 15L ))
110+ database. executeCommand(_) >> {
111+ Exception exception = new MongoCommandFailureException (new org.mongodb.operation.CommandResult (new Document (),
112+ new org.mongodb.connection.ServerAddress (),
113+ new Document (),
114+ 15L ))
115+
116+ throw mapException(exception)
107117 }
108118
109119 when :
@@ -115,11 +125,11 @@ class DBCollectionSpecification extends Specification {
115125
116126 def ' should throw MongoDuplicateKeyException when createIndex fails' () {
117127 given :
118- session. execute (_) >> {
128+ session. createServerConnectionProvider (_) >> {
119129 throw new org.mongodb.command.MongoDuplicateKeyException (new org.mongodb.operation.CommandResult (new Document (),
120- new org.mongodb.connection.ServerAddress (),
121- new Document (),
122- 15L ))
130+ new org.mongodb.connection.ServerAddress (),
131+ new Document (),
132+ 15L ))
123133 }
124134
125135 when :
@@ -131,7 +141,7 @@ class DBCollectionSpecification extends Specification {
131141
132142 def ' should wrap org.mongodb.MongoException as com.mongodb.MongoException when createIndex fails' () {
133143 given :
134- session. execute (_) >> { throw new org.mongodb.MongoInternalException (' Exception that should not leak' ) }
144+ session. createServerConnectionProvider (_) >> { throw new org.mongodb.MongoInternalException (' Exception that should not leak' ) }
135145
136146 when :
137147 collection. createIndex(new BasicDBObject ());
@@ -142,11 +152,12 @@ class DBCollectionSpecification extends Specification {
142152
143153 def ' should throw com.mongodb.MongoException when drop fails' () {
144154 given :
145- session. execute(_) >> {
146- throw new MongoCommandFailureException (new org.mongodb.operation.CommandResult (new Document (),
147- new org.mongodb.connection.ServerAddress (),
148- new Document (),
149- 15L ))
155+ database. executeCommand(_) >> {
156+ Exception exception = new MongoCommandFailureException (new org.mongodb.operation.CommandResult (new Document (),
157+ new org.mongodb.connection.ServerAddress (),
158+ new Document (),
159+ 15L ))
160+ throw mapException(exception)
150161 }
151162
152163 when :
@@ -162,7 +173,7 @@ class DBCollectionSpecification extends Specification {
162173 org.mongodb.MongoException exception = new MongoCommandFailureException (new org.mongodb.operation.CommandResult (
163174 new Document (),
164175 new org.mongodb.connection.ServerAddress (),
165- new Document (' errmsg' ,' ns not found' ),
176+ new Document (' errmsg' , ' ns not found' ),
166177 15L ));
167178
168179 throw mapException(exception);
@@ -177,7 +188,7 @@ class DBCollectionSpecification extends Specification {
177188
178189 def ' should wrap org.mongodb.MongoException as a com.mongodb.MongoException for findAndModify' () {
179190 given :
180- session. execute (_) >> { throw new org.mongodb.MongoInternalException (' Exception that should not escape' ) }
191+ session. createServerConnectionProvider (_) >> { throw new org.mongodb.MongoInternalException (' Exception that should not escape' ) }
181192
182193 when :
183194 collection. findAndModify(new BasicDBObject (), new BasicDBObject ());
@@ -188,7 +199,7 @@ class DBCollectionSpecification extends Specification {
188199
189200 def ' should wrap org.mongodb.MongoException as a com.mongodb.MongoException for getIndexInfo' () {
190201 given :
191- session. execute (_) >> { throw new org.mongodb.MongoInternalException (' Exception that should not escape' ) }
202+ session. createServerConnectionProvider (_) >> { throw new org.mongodb.MongoInternalException (' Exception that should not escape' ) }
192203
193204 when :
194205 collection. getIndexInfo();
0 commit comments