|
| 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 | +} |
0 commit comments