Skip to content

Commit a8fffed

Browse files
committed
Renamed ProtocolOperation to Protocol, and same for all implementations.
Fixed broken Spock tests
1 parent cc7f3ad commit a8fffed

26 files changed

Lines changed: 141 additions & 125 deletions

driver-compat/src/main/com/mongodb/DBCollection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,11 +1145,11 @@ public MapReduceOutput mapReduce(final MapReduceCommand command) {
11451145
final org.mongodb.operation.CommandResult executionResult;
11461146

11471147
try {
1148-
executionResult = getSession().execute(new CommandOperation(getDB().getName(),
1148+
executionResult = new CommandOperation(getDB().getName(),
11491149
command.toNew(),
11501150
mapReduceCodec,
11511151
getDB().getClusterDescription(),
1152-
getBufferPool()));
1152+
getBufferPool(), getSession(), false).execute();
11531153
} catch (org.mongodb.MongoException e) {
11541154
throw mapException(e);
11551155
}

driver-compat/src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ private ClusterDescription getClusterDescription() {
7777
}
7878
}
7979

80-
8180
@Override
8281
public String toString() {
8382
return "ReplicaSetStatus{" +

driver-compat/src/test/unit/com/mongodb/DBCollectionSpecification.groovy

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,47 @@
1818

1919

2020

21+
22+
2123
package com.mongodb
2224

2325
import org.mongodb.Document
2426
import org.mongodb.codecs.DocumentCodec
2527
import org.mongodb.command.MongoCommandFailureException
2628
import org.mongodb.connection.Cluster
29+
import org.mongodb.connection.ClusterConnectionMode
30+
import org.mongodb.connection.ClusterDescription
2731
import org.mongodb.session.Session
2832
import spock.lang.Specification
2933
import spock.lang.Subject
3034

35+
import static com.mongodb.MongoExceptions.mapException
3136
import static com.mongodb.ReadPreference.primary
3237
import static com.mongodb.WriteConcern.ACKNOWLEDGED
3338

3439
class 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();

driver-compat/src/test/unit/com/mongodb/DBCursorSpecification.groovy

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@
1616

1717

1818

19+
20+
1921
package com.mongodb
2022

2123
import org.mongodb.Document
2224
import org.mongodb.operation.MongoQueryFailureException
23-
import org.mongodb.session.Session
2425
import spock.lang.Specification
2526
import spock.lang.Subject
26-
import sun.security.pkcs11.Session
2727

2828
import static com.mongodb.ReadPreference.PRIMARY
2929

3030
class DBCursorSpecification extends Specification {
31-
private final org.mongodb.session.Session serverSelectingSession = Mock()
31+
private final org.mongodb.session.Session session = Mock()
3232
private final DBCollection collection = Mock();
3333

3434
@Subject
3535
private final DBCursor dbCursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), PRIMARY);
3636

3737
def setup() {
38-
collection.getSession() >> { serverSelectingSession }
38+
collection.getSession() >> { session }
3939
}
4040

4141
def 'should wrap org.mongodb.MongoException with com.mongodb.MongoException for errors in explain'() {
4242
given:
43-
serverSelectingSession.execute(_) >> { throw new org.mongodb.MongoInternalException('Exception that should not escape') }
43+
session.createServerConnectionProvider(_) >> { throw new org.mongodb.MongoInternalException('Exception that should not escape') }
4444

4545
when:
4646
dbCursor.explain()
@@ -51,9 +51,7 @@ class DBCursorSpecification extends Specification {
5151

5252
def 'should wrap org.mongodb.MongoException with com.mongodb.MongoException for errors in hasNext'() {
5353
given:
54-
Session session = Mock()
55-
serverSelectingSession.getBoundSession(_, _) >> { session }
56-
session.execute(_) >> { throw new MongoQueryFailureException(null, new Document('code', 123)) }
54+
session.createServerConnectionProvider(_) >> { throw new MongoQueryFailureException(null, new Document('code', 123)) }
5755

5856
when:
5957
dbCursor.hasNext()

driver-compat/src/test/unit/com/mongodb/DBSpecification.groovy

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import org.mongodb.command.Command
2222
import org.mongodb.command.MongoCommandFailureException
2323
import org.mongodb.command.Ping
2424
import org.mongodb.connection.Cluster
25+
import org.mongodb.connection.ClusterConnectionMode
26+
import org.mongodb.connection.ClusterDescription
2527
import org.mongodb.connection.MongoTimeoutException
2628
import org.mongodb.session.Session
2729
import org.mongodb.operation.MongoCursorNotFoundException
@@ -42,15 +44,15 @@ class DBSpecification extends Specification {
4244
def setup() {
4345
mongo.getCluster() >> { cluster }
4446
mongo.getSession() >> { session }
45-
4647
//TODO: this shouldn't be required. I think.
4748
database.setReadPreference(primary())
4849
}
4950

5051
@SuppressWarnings('UnnecessaryQualifiedReference')
5152
def 'should throw com.mongodb.MongoException if createCollection fails'() {
5253
given:
53-
session.execute(_) >> {
54+
cluster.getDescription() >> {new ClusterDescription(ClusterConnectionMode.Direct)}
55+
session.createServerConnectionProvider(_) >> {
5456
throw new MongoCommandFailureException(new org.mongodb.operation.CommandResult(new Document(),
5557
new org.mongodb.connection.ServerAddress(),
5658
new Document(),
@@ -67,12 +69,13 @@ class DBSpecification extends Specification {
6769
@SuppressWarnings('UnnecessaryQualifiedReference')
6870
def 'should throw com.mongodb.MongoCursorNotFoundException if cursor not found'() {
6971
given:
70-
session.execute(_) >> {
72+
cluster.getDescription() >> {new ClusterDescription(ClusterConnectionMode.Direct)}
73+
session.createServerConnectionProvider(_) >> {
7174
throw new MongoCursorNotFoundException(new ServerCursor(1, new org.mongodb.connection.ServerAddress()))
7275
}
7376

7477
when:
75-
database.executeCommand(new Command());
78+
database.executeCommand(new Command(new Document("isMaster", 1)));
7679

7780
then:
7881
thrown(com.mongodb.MongoCursorNotFoundException)
@@ -81,7 +84,8 @@ class DBSpecification extends Specification {
8184
@SuppressWarnings('UnnecessaryQualifiedReference')
8285
def 'should throw com.mongodb.MongoException if executeCommand fails'() {
8386
given:
84-
session.execute(_) >> {
87+
cluster.getDescription() >> {new ClusterDescription(ClusterConnectionMode.Direct)}
88+
session.createServerConnectionProvider(_) >> {
8589
throw new MongoCommandFailureException(new org.mongodb.operation.CommandResult(new Document(),
8690
new org.mongodb.connection.ServerAddress(),
8791
new Document(),
@@ -98,7 +102,7 @@ class DBSpecification extends Specification {
98102
@SuppressWarnings('UnnecessaryQualifiedReference')
99103
def 'should throw com.mongodb.MongoException if getCollectionNames fails'() {
100104
given:
101-
session.execute(_) >> {
105+
session.createServerConnectionProvider(_) >> {
102106
throw new MongoCommandFailureException(new org.mongodb.operation.CommandResult(new Document(),
103107
new org.mongodb.connection.ServerAddress(),
104108
new Document(),
@@ -115,7 +119,8 @@ class DBSpecification extends Specification {
115119
@SuppressWarnings('UnnecessaryQualifiedReference')
116120
def 'should throw com.mongodb.MongoException if command fails for a resons that is not a command failure'() {
117121
given:
118-
session.execute(_) >> {
122+
cluster.getDescription() >> {new ClusterDescription(ClusterConnectionMode.Direct)}
123+
session.createServerConnectionProvider(_) >> {
119124
throw new org.mongodb.MongoInternalException('An exception that is not a MongoCommandFailureException')
120125
}
121126

@@ -132,7 +137,8 @@ class DBSpecification extends Specification {
132137
new org.mongodb.connection.ServerAddress(),
133138
new Document(),
134139
15L)
135-
session.execute(_) >> {
140+
cluster.getDescription() >> {new ClusterDescription(ClusterConnectionMode.Direct)}
141+
session.createServerConnectionProvider(_) >> {
136142
throw new MongoCommandFailureException(expectedCommandResult)
137143
}
138144

0 commit comments

Comments
 (0)