15
15
*/
16
16
package org .springframework .data .mongodb .config ;
17
17
18
- import static org .hamcrest .collection .IsIterableContainingInOrder .*;
19
- import static org .hamcrest .core .IsNull .*;
20
- import static org .junit .Assert .*;
18
+ import static org .assertj .core .api .Assertions .*;
21
19
22
20
import java .io .UnsupportedEncodingException ;
23
21
import java .net .URLEncoder ;
@@ -73,6 +71,7 @@ public class MongoCredentialPropertyEditorUnitTests {
73
71
74
72
static final String USER_5_AUTH_STRING = USER_5_NAME + ":" + USER_5_PWD + "@" + USER_5_DB ;
75
73
static final String USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN" ;
74
+ static final String USER_5_AUTH_STRING_WITH_QUERY_ARGS = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN&foo=&bar" ;
76
75
77
76
static final MongoCredential USER_1_CREDENTIALS = MongoCredential .createCredential (USER_1_NAME , USER_1_DB ,
78
77
USER_1_PWD .toCharArray ());
@@ -91,8 +90,8 @@ public class MongoCredentialPropertyEditorUnitTests {
91
90
92
91
static final MongoCredential USER_5_CREDENTIALS = MongoCredential .createCredential (USER_5_NAME , USER_5_DB ,
93
92
USER_5_PWD .toCharArray ());
94
- static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential .createPlainCredential (USER_5_NAME , USER_5_DB ,
95
- USER_5_PWD .toCharArray ());
93
+ static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential .createPlainCredential (USER_5_NAME ,
94
+ USER_5_DB , USER_5_PWD .toCharArray ());
96
95
97
96
MongoCredentialPropertyEditor editor ;
98
97
@@ -121,15 +120,15 @@ public void shouldReturnNullValueForNullText() {
121
120
122
121
editor .setAsText (null );
123
122
124
- assertThat (editor . getValue (), nullValue () );
123
+ assertThat (getValue ()). isNull ( );
125
124
}
126
125
127
126
@ Test // DATAMONGO-1158
128
127
public void shouldReturnNullValueForEmptyText () {
129
128
130
129
editor .setAsText (" " );
131
130
132
- assertThat (editor . getValue (), nullValue () );
131
+ assertThat (getValue ()). isNull ( );
133
132
}
134
133
135
134
@ Test (expected = IllegalArgumentException .class ) // DATAMONGO-1158
@@ -148,7 +147,7 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenSingleUserNamePassword
148
147
149
148
editor .setAsText (USER_1_AUTH_STRING );
150
149
151
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_1_CREDENTIALS ) );
150
+ assertThat (getValue ()). contains (USER_1_CREDENTIALS );
152
151
}
153
152
154
153
@ Test // DATAMONGO-1158
@@ -157,7 +156,7 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenSingleUserNamePassword
157
156
158
157
editor .setAsText (USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM );
159
158
160
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_1_CREDENTIALS_PLAIN_AUTH ) );
159
+ assertThat (getValue ()). contains (USER_1_CREDENTIALS_PLAIN_AUTH );
161
160
}
162
161
163
162
@ Test // DATAMONGO-1158
@@ -167,38 +166,37 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswo
167
166
editor
168
167
.setAsText (StringUtils .collectionToCommaDelimitedString (Arrays .asList (USER_1_AUTH_STRING , USER_2_AUTH_STRING )));
169
168
170
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_1_CREDENTIALS , USER_2_CREDENTIALS ) );
169
+ assertThat (getValue ()). contains (USER_1_CREDENTIALS , USER_2_CREDENTIALS );
171
170
}
172
171
173
172
@ Test // DATAMONGO-1158
174
173
@ SuppressWarnings ("unchecked" )
175
174
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswordStringWithDatabaseAndAuthOptions () {
176
175
177
- editor .setAsText (StringUtils .collectionToCommaDelimitedString (Arrays . asList (
178
- USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM , USER_2_AUTH_STRING_WITH_MONGODB_CR_AUTH_MECHANISM )));
176
+ editor .setAsText (StringUtils .collectionToCommaDelimitedString (Arrays
177
+ . asList ( USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM , USER_2_AUTH_STRING_WITH_MONGODB_CR_AUTH_MECHANISM )));
179
178
180
- assertThat ((List <MongoCredential >) editor .getValue (),
181
- contains (USER_1_CREDENTIALS_PLAIN_AUTH , USER_2_CREDENTIALS_CR_AUTH ));
179
+ assertThat (getValue ()).contains (USER_1_CREDENTIALS_PLAIN_AUTH , USER_2_CREDENTIALS_CR_AUTH );
182
180
}
183
181
184
182
@ Test // DATAMONGO-1158
185
183
@ SuppressWarnings ("unchecked" )
186
184
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswordStringWithDatabaseAndMixedOptions () {
187
185
188
- editor .setAsText (StringUtils .collectionToCommaDelimitedString (Arrays . asList (
189
- USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM , USER_2_AUTH_STRING )));
186
+ editor .setAsText (StringUtils .collectionToCommaDelimitedString (
187
+ Arrays . asList ( USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM , USER_2_AUTH_STRING )));
190
188
191
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_1_CREDENTIALS_PLAIN_AUTH , USER_2_CREDENTIALS ) );
189
+ assertThat (getValue ()). contains (USER_1_CREDENTIALS_PLAIN_AUTH , USER_2_CREDENTIALS );
192
190
}
193
191
194
192
@ Test // DATAMONGO-1257
195
193
@ SuppressWarnings ("unchecked" )
196
194
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleQuotedUserNamePasswordStringWithDatabaseAndNoOptions () {
197
195
198
- editor .setAsText (StringUtils .collectionToCommaDelimitedString (Arrays . asList ( "'" + USER_1_AUTH_STRING + "'" , "'"
199
- + USER_2_AUTH_STRING + "'" )));
196
+ editor .setAsText (StringUtils .collectionToCommaDelimitedString (
197
+ Arrays . asList ( "'" + USER_1_AUTH_STRING + "'" , "'" + USER_2_AUTH_STRING + "'" )));
200
198
201
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_1_CREDENTIALS , USER_2_CREDENTIALS ) );
199
+ assertThat (getValue ()). contains (USER_1_CREDENTIALS , USER_2_CREDENTIALS );
202
200
}
203
201
204
202
@ Test // DATAMONGO-1257
@@ -207,7 +205,7 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenSingleQuotedUserNamePa
207
205
208
206
editor .setAsText ("'" + USER_1_AUTH_STRING + "'" );
209
207
210
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_1_CREDENTIALS ) );
208
+ assertThat (getValue ()). contains (USER_1_CREDENTIALS );
211
209
}
212
210
213
211
@ Test // DATAMONGO-1257
@@ -216,7 +214,7 @@ public void shouldReturnX509CredentialsCorrectly() {
216
214
217
215
editor .setAsText (USER_3_AUTH_STRING_WITH_X509_AUTH_MECHANISM );
218
216
219
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_3_CREDENTIALS_X509_AUTH ) );
217
+ assertThat (getValue ()). contains (USER_3_CREDENTIALS_X509_AUTH );
220
218
}
221
219
222
220
@ Test // DATAMONGO-1257
@@ -225,49 +223,60 @@ public void shouldReturnX509CredentialsCorrectlyWhenNoDbSpecified() {
225
223
226
224
editor .setAsText ("tyrion?uri.authMechanism=MONGODB-X509" );
227
225
228
- assertThat (( List < MongoCredential >) editor . getValue (), contains (MongoCredential .createMongoX509Credential ("tyrion" ) ));
226
+ assertThat (getValue ()). contains (MongoCredential .createMongoX509Credential ("tyrion" ));
229
227
}
230
228
231
229
@ Test (expected = IllegalArgumentException .class ) // DATAMONGO-1257
232
230
public void shouldThrowExceptionWhenNoDbSpecifiedForMongodbCR () {
233
231
234
232
editor .setAsText ("tyrion?uri.authMechanism=MONGODB-CR" );
235
233
236
- editor . getValue ();
234
+ getValue ();
237
235
}
238
236
239
237
@ Test (expected = IllegalArgumentException .class ) // DATAMONGO-1257
240
238
public void shouldThrowExceptionWhenDbIsEmptyForMongodbCR () {
241
239
242
240
editor .setAsText ("tyrion@?uri.authMechanism=MONGODB-CR" );
243
241
244
- editor . getValue ();
242
+ getValue ();
245
243
}
246
244
247
245
@ Test // DATAMONGO-1317
248
246
@ SuppressWarnings ("unchecked" )
249
- public void encodedUserNameAndPasswrodShouldBeDecoded () throws UnsupportedEncodingException {
247
+ public void encodedUserNameAndPasswordShouldBeDecoded () {
250
248
251
249
editor .setAsText (USER_4_AUTH_STRING );
252
250
253
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_4_CREDENTIALS ) );
251
+ assertThat (getValue ()). contains (USER_4_CREDENTIALS );
254
252
}
255
253
256
- @ Test //DATAMONGO-2016
254
+ @ Test // DATAMONGO-2016
257
255
@ SuppressWarnings ("unchecked" )
258
256
public void passwordWithQuestionMarkShouldNotBeInterpretedAsOptionString () {
259
257
260
258
editor .setAsText (USER_5_AUTH_STRING );
261
259
262
- assertThat (( List < MongoCredential >) editor . getValue (), contains (USER_5_CREDENTIALS ) );
260
+ assertThat (getValue ()). contains (USER_5_CREDENTIALS );
263
261
}
264
262
265
- @ Test //DATAMONGO-2016
263
+ @ Test // DATAMONGO-2016
266
264
@ SuppressWarnings ("unchecked" )
267
265
public void passwordWithQuestionMarkShouldNotBreakParsingOfOptionString () {
268
266
269
267
editor .setAsText (USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM );
270
268
271
- assertThat ((List <MongoCredential >) editor .getValue (), contains (USER_5_CREDENTIALS_PLAIN_AUTH ));
269
+ assertThat (getValue ()).contains (USER_5_CREDENTIALS_PLAIN_AUTH );
270
+ }
271
+
272
+ @ Test (expected = IllegalArgumentException .class ) // DATAMONGO-2016
273
+ @ SuppressWarnings ("unchecked" )
274
+ public void failsGracefullyOnEmptyQueryArgument () {
275
+ editor .setAsText (USER_5_AUTH_STRING_WITH_QUERY_ARGS );
276
+ }
277
+
278
+ @ SuppressWarnings ("unchecked" )
279
+ private List <MongoCredential > getValue () {
280
+ return (List <MongoCredential >) editor .getValue ();
272
281
}
273
282
}
0 commit comments