Skip to content

Commit 191e656

Browse files
committed
Completed implementation of MongoClientURI.getOptions() method
1 parent 41c3434 commit 191e656

3 files changed

Lines changed: 230 additions & 9 deletions

File tree

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ public MongoCredential getCredentials() {
236236
* @return the MongoClientOptions based on this URI.
237237
*/
238238
public MongoClientOptions getOptions() {
239-
// TODO: Neither tested nor complete
240239
MongoClientOptions.Builder builder = MongoClientOptions.builder();
241240

242241
if (proxied.getReadPreference() != null) {
@@ -245,6 +244,37 @@ public MongoClientOptions getOptions() {
245244
if (proxied.getWriteConcern() != null) {
246245
builder.writeConcern(proxied.getWriteConcern());
247246
}
247+
if (proxied.getMaxConnectionPoolSize() != null) {
248+
builder.connectionsPerHost(proxied.getMaxConnectionPoolSize());
249+
}
250+
if (proxied.getMinConnectionPoolSize() != null) {
251+
builder.minConnectionsPerHost(proxied.getMinConnectionPoolSize());
252+
}
253+
if (proxied.getMaxWaitTime() != null) {
254+
builder.maxWaitTime(proxied.getMaxWaitTime());
255+
}
256+
if (proxied.getThreadsAllowedToBlockForConnectionMultiplier() != null) {
257+
builder.threadsAllowedToBlockForConnectionMultiplier(proxied.getThreadsAllowedToBlockForConnectionMultiplier());
258+
}
259+
if (proxied.getMaxConnectionIdleTime() != null) {
260+
builder.maxConnectionIdleTime(proxied.getMaxConnectionIdleTime());
261+
}
262+
if (proxied.getMaxConnectionLifeTime() != null) {
263+
builder.maxConnectionLifeTime(proxied.getMaxConnectionLifeTime());
264+
}
265+
if (proxied.getSocketTimeout() != null) {
266+
builder.socketTimeout(proxied.getSocketTimeout());
267+
}
268+
if (proxied.getConnectTimeout() != null) {
269+
builder.connectTimeout(proxied.getConnectTimeout());
270+
}
271+
if (proxied.getRequiredReplicaSetName() != null) {
272+
builder.requiredReplicaSetName(proxied.getRequiredReplicaSetName());
273+
}
274+
if (proxied.getSslEnabled() != null) {
275+
builder.SSLEnabled(proxied.getSslEnabled());
276+
}
277+
248278
return builder.build();
249279
}
250280

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* Copyright (c) 2008-2014 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb
18+
import spock.lang.Specification
19+
import spock.lang.Unroll
20+
21+
import static com.mongodb.MongoCredential.createGSSAPICredential
22+
import static com.mongodb.MongoCredential.createMongoCRCredential
23+
import static com.mongodb.MongoCredential.createMongoX509Credential
24+
import static com.mongodb.MongoCredential.createPlainCredential
25+
import static com.mongodb.ReadPreference.secondaryPreferred
26+
27+
class MongoClientURISpecification extends Specification {
28+
def 'should throw Exception if URI does not have a trailing slash'() {
29+
when:
30+
new MongoClientURI('mongodb://localhost?wTimeout=5');
31+
32+
then:
33+
thrown(IllegalArgumentException)
34+
}
35+
36+
@Unroll
37+
def 'should parse #uri into correct components'() {
38+
expect:
39+
uri.getHosts().size() == num;
40+
uri.getHosts() == hosts;
41+
uri.getDatabase() == database;
42+
uri.getCollection() == collection;
43+
uri.getUsername() == username;
44+
uri.getPassword() == password;
45+
46+
where:
47+
uri | num | hosts | database | collection | username | password
48+
new MongoClientURI('mongodb://db.example.com') | 1 | ['db.example.com'] | null | null | null | null
49+
new MongoClientURI('mongodb://10.0.0.1') | 1 | ['10.0.0.1'] | null | null | null | null
50+
new MongoClientURI('mongodb://[::1]') | 1 | ['[::1]'] | null | null | null | null
51+
new MongoClientURI('mongodb://foo/bar') | 1 | ['foo'] | 'bar' | null | null | null
52+
new MongoClientURI('mongodb://10.0.0.1/bar') | 1 | ['10.0.0.1'] | 'bar' | null | null | null
53+
new MongoClientURI('mongodb://[::1]/bar') | 1 | ['[::1]'] | 'bar' | null | null | null
54+
new MongoClientURI('mongodb://localhost/' +
55+
'test.my.coll') | 1 | ['localhost'] | 'test' | 'my.coll' | null | null
56+
new MongoClientURI('mongodb://foo/bar.goo') | 1 | ['foo'] | 'bar' | 'goo' | null | null
57+
new MongoClientURI('mongodb://user:pass@' +
58+
'host/bar') | 1 | ['host'] | 'bar' | null | 'user' | 'pass' as char[]
59+
new MongoClientURI('mongodb://user:pass@' +
60+
'host:27011/bar') | 1 | ['host:27011'] | 'bar' | null | 'user' | 'pass' as char[]
61+
new MongoClientURI('mongodb://user:pass@' +
62+
'10.0.0.1:27011/bar') | 1 | ['10.0.0.1:27011'] | 'bar' | null | 'user' | 'pass' as char[]
63+
new MongoClientURI('mongodb://user:pass@' +
64+
'[::1]:27011/bar') | 1 | ['[::1]:27011'] | 'bar' | null | 'user' | 'pass' as char[]
65+
new MongoClientURI('mongodb://user:pass@' +
66+
'host:7,' +
67+
'host2:8,' +
68+
'host3:9/bar') | 3 | ['host:7',
69+
'host2:8',
70+
'host3:9'] | 'bar' | null | 'user' | 'pass' as char[]
71+
new MongoClientURI('mongodb://user:pass@' +
72+
'10.0.0.1:7,' +
73+
'[::1]:8,' +
74+
'host3:9/bar') | 3 | ['10.0.0.1:7',
75+
'[::1]:8',
76+
'host3:9'] | 'bar' | null | 'user' | 'pass' as char[]
77+
}
78+
79+
def 'should correctly parse different write concerns'() {
80+
expect:
81+
uri.getOptions().getWriteConcern() == writeConcern;
82+
83+
where:
84+
uri | writeConcern
85+
new MongoClientURI('mongodb://localhost') | WriteConcern.ACKNOWLEDGED
86+
new MongoClientURI('mongodb://localhost/?safe=true') | WriteConcern.ACKNOWLEDGED
87+
new MongoClientURI('mongodb://localhost/?safe=false') | WriteConcern.UNACKNOWLEDGED
88+
new MongoClientURI('mongodb://localhost/?wTimeout=5') | new WriteConcern(1, 5, false, false)
89+
new MongoClientURI('mongodb://localhost/?fsync=true') | new WriteConcern(1, 0, true, false)
90+
new MongoClientURI('mongodb://localhost/?j=true') | new WriteConcern(1, 0, false, true)
91+
new MongoClientURI('mongodb://localhost/?w=2&wtimeout=5&fsync=true&j=true') | new WriteConcern(2, 5, true, true)
92+
new MongoClientURI('mongodb://localhost/?w=majority&wtimeout=5&fsync=true&j=true') | new WriteConcern('majority', 5, true, true)
93+
}
94+
95+
@Unroll
96+
def 'should correctly parse URI options for #type'() {
97+
expect:
98+
options.getWriteConcern() == new WriteConcern(1, 2500, true)
99+
options.getReadPreference() == ReadPreference.secondary()
100+
options.getConnectionsPerHost() == 10
101+
options.getMinConnectionsPerHost() == 5
102+
options.getMaxWaitTime() == 150
103+
options.getThreadsAllowedToBlockForConnectionMultiplier() == 7
104+
options.getMaxConnectionIdleTime() == 200
105+
options.getMaxConnectionLifeTime() == 300
106+
options.getSocketTimeout() == 5500
107+
options.getConnectTimeout() == 2500
108+
options.getRequiredReplicaSetName() == 'test'
109+
options.isSSLEnabled()
110+
111+
where:
112+
options <<
113+
[new MongoClientURI('mongodb://localhost/?minPoolSize=5&maxPoolSize=10&waitQueueMultiple=7&waitQueueTimeoutMS=150&'
114+
+ 'maxIdleTimeMS=200&maxLifeTimeMS=300&replicaSet=test&'
115+
+ 'connectTimeoutMS=2500&socketTimeoutMS=5500&'
116+
+ 'safe=false&w=1&wtimeout=2500&fsync=true&ssl=true&readPreference=secondary').getOptions(),
117+
new MongoClientURI('mongodb://localhost/?minPoolSize=5;maxPoolSize=10;waitQueueMultiple=7;waitQueueTimeoutMS=150;'
118+
+ 'maxIdleTimeMS=200;maxLifeTimeMS=300;replicaSet=test;'
119+
+ 'connectTimeoutMS=2500;socketTimeoutMS=5500;ssl=true;'
120+
+ 'safe=false;w=1;wtimeout=2500;fsync=true;readPreference=secondary').getOptions(),
121+
new MongoClientURI('mongodb://localhost/test?minPoolSize=5;maxPoolSize=10&waitQueueMultiple=7;waitQueueTimeoutMS=150;'
122+
+ 'maxIdleTimeMS=200&maxLifeTimeMS=300&replicaSet=test;'
123+
+ 'connectTimeoutMS=2500;'
124+
+ 'socketTimeoutMS=5500&'
125+
+ 'safe=false&w=1;wtimeout=2500;fsync=true&ssl=true;readPreference=secondary').getOptions()]
126+
//for documentation, i.e. the Unroll description for each type
127+
type << ['amp', 'semi', 'mixed']
128+
}
129+
130+
def 'should have correct defaults for options'() {
131+
when:
132+
MongoClientOptions options = new MongoClientURI('mongodb://localhost').getOptions();
133+
134+
then:
135+
options.getConnectionsPerHost() == 100;
136+
options.getThreadsAllowedToBlockForConnectionMultiplier() == 5;
137+
options.getMaxWaitTime() == 120000;
138+
options.getConnectTimeout() == 10000;
139+
options.getSocketTimeout() == 0;
140+
!options.isSocketKeepAlive();
141+
options.getDescription() == null;
142+
options.getReadPreference() == ReadPreference.primary();
143+
options.getRequiredReplicaSetName() == null
144+
!options.isSSLEnabled()
145+
}
146+
147+
@Unroll
148+
def 'should support all credential types'() {
149+
expect:
150+
uri.credentials == credentialList
151+
152+
where:
153+
uri | credentialList
154+
new MongoClientURI('mongodb://jeff:123@localhost') | createMongoCRCredential('jeff', 'admin', '123'.toCharArray())
155+
new MongoClientURI('mongodb://jeff:123@localhost/?' +
156+
'authMechanism=MONGODB-CR') | createMongoCRCredential('jeff', 'admin', '123'.toCharArray())
157+
new MongoClientURI('mongodb://jeff:123@localhost/?' +
158+
'authMechanism=MONGODB-CR' +
159+
'&authSource=test') | createMongoCRCredential('jeff', 'test', '123'.toCharArray())
160+
new MongoClientURI('mongodb://jeff@localhost/?' +
161+
'authMechanism=GSSAPI') | createGSSAPICredential('jeff')
162+
new MongoClientURI('mongodb://jeff:123@localhost/?' +
163+
'authMechanism=PLAIN') | createPlainCredential('jeff', 'admin', '123'.toCharArray())
164+
new MongoClientURI('mongodb://jeff@localhost/?' +
165+
'authMechanism=MONGODB-X509') | createMongoX509Credential('jeff')
166+
new MongoClientURI('mongodb://jeff@localhost/?' +
167+
'authMechanism=GSSAPI' +
168+
'&gssapiServiceName=foo') | createGSSAPICredential('jeff').withMechanismProperty('SERVICE_NAME', 'foo')
169+
}
170+
171+
@Unroll
172+
def 'should correct parse read preference for #readPreference'() {
173+
expect:
174+
uri.getOptions().getReadPreference() == readPreference;
175+
176+
where:
177+
uri | readPreference
178+
new MongoClientURI('mongodb://localhost/' +
179+
'?readPreference=secondaryPreferred') | ReadPreference.secondaryPreferred()
180+
new MongoClientURI('mongodb://localhost/' +
181+
'?readPreference=secondaryPreferred' +
182+
'&readPreferenceTags=dc:ny,rack:1' +
183+
'&readPreferenceTags=dc:ny' +
184+
'&readPreferenceTags=') | secondaryPreferred([new Tags('dc', 'ny').append('rack', '1'),
185+
new Tags('dc', 'ny'),
186+
new Tags()])
187+
}
188+
}

driver/src/test/unit/com/mongodb/ConnectionStringSpecification.groovy

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,32 @@ class ConnectionStringSpecification extends Specification {
9999
expect:
100100
connectionString.getMinConnectionPoolSize() == 5
101101
connectionString.getMaxConnectionPoolSize() == 10;
102-
connectionString.getThreadsAllowedToBlockForConnectionMultiplier() == 5;
102+
connectionString.getThreadsAllowedToBlockForConnectionMultiplier() == 7;
103103
connectionString.getMaxWaitTime() == 150;
104104
connectionString.getMaxConnectionIdleTime() == 200
105105
connectionString.getMaxConnectionLifeTime() == 300
106+
connectionString.getConnectTimeout() == 2500;
106107
connectionString.getSocketTimeout() == 5500;
107108
connectionString.getWriteConcern() == new WriteConcern(1, 2500, true);
108109
connectionString.getReadPreference() == ReadPreference.primary() ;
109110
connectionString.getRequiredReplicaSetName() == 'test'
111+
connectionString.getSslEnabled()
110112

111113
where:
112114
connectionString <<
113-
[new ConnectionString('mongodb://localhost/?minPoolSize=5&maxPoolSize=10&waitQueueMultiple=5&waitQueueTimeoutMS=150&'
115+
[new ConnectionString('mongodb://localhost/?minPoolSize=5&maxPoolSize=10&waitQueueMultiple=7&waitQueueTimeoutMS=150&'
114116
+ 'maxIdleTimeMS=200&maxLifeTimeMS=300&replicaSet=test&'
115117
+ 'connectTimeoutMS=2500&socketTimeoutMS=5500&'
116-
+ 'safe=false&w=1&wtimeout=2500&fsync=true&readPreference=primary'),
117-
new ConnectionString('mongodb://localhost/?minPoolSize=5;maxPoolSize=10;waitQueueMultiple=5;waitQueueTimeoutMS=150;'
118+
+ 'safe=false&w=1&wtimeout=2500&fsync=true&readPreference=primary&ssl=true'),
119+
new ConnectionString('mongodb://localhost/?minPoolSize=5;maxPoolSize=10;waitQueueMultiple=7;waitQueueTimeoutMS=150;'
118120
+ 'maxIdleTimeMS=200;maxLifeTimeMS=300;replicaSet=test;'
119121
+ 'connectTimeoutMS=2500;socketTimeoutMS=5500;'
120-
+ 'safe=false;w=1;wtimeout=2500;fsync=true;readPreference=primary'),
121-
new ConnectionString('mongodb://localhost/test?minPoolSize=5;maxPoolSize=10&waitQueueMultiple=5;waitQueueTimeoutMS=150;'
122+
+ 'safe=false;w=1;wtimeout=2500;fsync=true;readPreference=primary;ssl=true'),
123+
new ConnectionString('mongodb://localhost/test?minPoolSize=5;maxPoolSize=10&waitQueueMultiple=7;waitQueueTimeoutMS=150;'
122124
+ 'maxIdleTimeMS=200&maxLifeTimeMS=300&replicaSet=test;'
123125
+ 'connectTimeoutMS=2500;'
124126
+ 'socketTimeoutMS=5500&'
125-
+ 'safe=false&w=1;wtimeout=2500;fsync=true&readPreference=primary')]
127+
+ 'safe=false&w=1;wtimeout=2500;fsync=true&readPreference=primary;ssl=true')]
126128
//for documentation, i.e. the Unroll description for each type
127129
type << ['amp', 'semi', 'mixed']
128130
}
@@ -140,6 +142,7 @@ class ConnectionStringSpecification extends Specification {
140142
connectionString.getWriteConcern() == null;
141143
connectionString.getReadPreference() == null;
142144
connectionString.getRequiredReplicaSetName() == null
145+
connectionString.getSslEnabled() == null
143146
}
144147

145148
@Unroll
@@ -149,7 +152,7 @@ class ConnectionStringSpecification extends Specification {
149152

150153
where:
151154
uri | credentialList
152-
new ConnectionString('mongodb://jeff:123@localhost') | asList(createMongoCRCredential('jeff', 'admin', '123'.toCharArray()))
155+
new ConnectionString('mongodb://jeff:123@localhost') | asList(createMongoCRCredential('jeff', 'admin', '123'.toCharArray()))
153156
new ConnectionString('mongodb://jeff:123@localhost/?' +
154157
'authMechanism=MONGODB-CR') | asList(createMongoCRCredential('jeff', 'admin', '123'.toCharArray()))
155158
new ConnectionString('mongodb://jeff:123@localhost/?' +

0 commit comments

Comments
 (0)