Skip to content

Commit 46ed58b

Browse files
committed
DATAMONGO-2016 - Polishing.
Fail gracefully if query string parameter has no value. Reformat test. Convert assertions to AssertJ. Original pull request: #578.
1 parent fb8084c commit 46ed58b

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @author Christoph Strobl
3737
* @author Oliver Gierke
3838
* @author Stephen Tyler Conrad
39+
* @author Mark Paluch
3940
* @since 1.7
4041
*/
4142
public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
@@ -174,7 +175,13 @@ private static Properties extractOptions(String text) {
174175
Properties properties = new Properties();
175176

176177
for (String option : text.substring(optionsSeparationIndex + 1).split(OPTION_VALUE_DELIMITER)) {
178+
177179
String[] optionArgs = option.split("=");
180+
181+
if (optionArgs.length == 1) {
182+
throw new IllegalArgumentException(String.format("Query parameter '%s' has no value!", optionArgs[0]));
183+
}
184+
178185
properties.put(optionArgs[0], optionArgs[1]);
179186
}
180187

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.config;
1717

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.*;
2119

2220
import java.io.UnsupportedEncodingException;
2321
import java.net.URLEncoder;
@@ -73,6 +71,7 @@ public class MongoCredentialPropertyEditorUnitTests {
7371

7472
static final String USER_5_AUTH_STRING = USER_5_NAME + ":" + USER_5_PWD + "@" + USER_5_DB;
7573
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";
7675

7776
static final MongoCredential USER_1_CREDENTIALS = MongoCredential.createCredential(USER_1_NAME, USER_1_DB,
7877
USER_1_PWD.toCharArray());
@@ -91,8 +90,8 @@ public class MongoCredentialPropertyEditorUnitTests {
9190

9291
static final MongoCredential USER_5_CREDENTIALS = MongoCredential.createCredential(USER_5_NAME, USER_5_DB,
9392
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());
9695

9796
MongoCredentialPropertyEditor editor;
9897

@@ -121,15 +120,15 @@ public void shouldReturnNullValueForNullText() {
121120

122121
editor.setAsText(null);
123122

124-
assertThat(editor.getValue(), nullValue());
123+
assertThat(getValue()).isNull();
125124
}
126125

127126
@Test // DATAMONGO-1158
128127
public void shouldReturnNullValueForEmptyText() {
129128

130129
editor.setAsText(" ");
131130

132-
assertThat(editor.getValue(), nullValue());
131+
assertThat(getValue()).isNull();
133132
}
134133

135134
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1158
@@ -148,7 +147,7 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenSingleUserNamePassword
148147

149148
editor.setAsText(USER_1_AUTH_STRING);
150149

151-
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_1_CREDENTIALS));
150+
assertThat(getValue()).contains(USER_1_CREDENTIALS);
152151
}
153152

154153
@Test // DATAMONGO-1158
@@ -157,7 +156,7 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenSingleUserNamePassword
157156

158157
editor.setAsText(USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM);
159158

160-
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_1_CREDENTIALS_PLAIN_AUTH));
159+
assertThat(getValue()).contains(USER_1_CREDENTIALS_PLAIN_AUTH);
161160
}
162161

163162
@Test // DATAMONGO-1158
@@ -167,38 +166,37 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswo
167166
editor
168167
.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays.asList(USER_1_AUTH_STRING, USER_2_AUTH_STRING)));
169168

170-
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_1_CREDENTIALS, USER_2_CREDENTIALS));
169+
assertThat(getValue()).contains(USER_1_CREDENTIALS, USER_2_CREDENTIALS);
171170
}
172171

173172
@Test // DATAMONGO-1158
174173
@SuppressWarnings("unchecked")
175174
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswordStringWithDatabaseAndAuthOptions() {
176175

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)));
179178

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);
182180
}
183181

184182
@Test // DATAMONGO-1158
185183
@SuppressWarnings("unchecked")
186184
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswordStringWithDatabaseAndMixedOptions() {
187185

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)));
190188

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);
192190
}
193191

194192
@Test // DATAMONGO-1257
195193
@SuppressWarnings("unchecked")
196194
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleQuotedUserNamePasswordStringWithDatabaseAndNoOptions() {
197195

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 + "'")));
200198

201-
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_1_CREDENTIALS, USER_2_CREDENTIALS));
199+
assertThat(getValue()).contains(USER_1_CREDENTIALS, USER_2_CREDENTIALS);
202200
}
203201

204202
@Test // DATAMONGO-1257
@@ -207,7 +205,7 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenSingleQuotedUserNamePa
207205

208206
editor.setAsText("'" + USER_1_AUTH_STRING + "'");
209207

210-
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_1_CREDENTIALS));
208+
assertThat(getValue()).contains(USER_1_CREDENTIALS);
211209
}
212210

213211
@Test // DATAMONGO-1257
@@ -216,7 +214,7 @@ public void shouldReturnX509CredentialsCorrectly() {
216214

217215
editor.setAsText(USER_3_AUTH_STRING_WITH_X509_AUTH_MECHANISM);
218216

219-
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_3_CREDENTIALS_X509_AUTH));
217+
assertThat(getValue()).contains(USER_3_CREDENTIALS_X509_AUTH);
220218
}
221219

222220
@Test // DATAMONGO-1257
@@ -225,49 +223,60 @@ public void shouldReturnX509CredentialsCorrectlyWhenNoDbSpecified() {
225223

226224
editor.setAsText("tyrion?uri.authMechanism=MONGODB-X509");
227225

228-
assertThat((List<MongoCredential>) editor.getValue(), contains(MongoCredential.createMongoX509Credential("tyrion")));
226+
assertThat(getValue()). contains(MongoCredential.createMongoX509Credential("tyrion"));
229227
}
230228

231229
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1257
232230
public void shouldThrowExceptionWhenNoDbSpecifiedForMongodbCR() {
233231

234232
editor.setAsText("tyrion?uri.authMechanism=MONGODB-CR");
235233

236-
editor.getValue();
234+
getValue();
237235
}
238236

239237
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1257
240238
public void shouldThrowExceptionWhenDbIsEmptyForMongodbCR() {
241239

242240
editor.setAsText("tyrion@?uri.authMechanism=MONGODB-CR");
243241

244-
editor.getValue();
242+
getValue();
245243
}
246244

247245
@Test // DATAMONGO-1317
248246
@SuppressWarnings("unchecked")
249-
public void encodedUserNameAndPasswrodShouldBeDecoded() throws UnsupportedEncodingException {
247+
public void encodedUserNameAndPasswordShouldBeDecoded() {
250248

251249
editor.setAsText(USER_4_AUTH_STRING);
252250

253-
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_4_CREDENTIALS));
251+
assertThat(getValue()).contains(USER_4_CREDENTIALS);
254252
}
255253

256-
@Test //DATAMONGO-2016
254+
@Test // DATAMONGO-2016
257255
@SuppressWarnings("unchecked")
258256
public void passwordWithQuestionMarkShouldNotBeInterpretedAsOptionString() {
259257

260258
editor.setAsText(USER_5_AUTH_STRING);
261259

262-
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_5_CREDENTIALS));
260+
assertThat(getValue()).contains(USER_5_CREDENTIALS);
263261
}
264262

265-
@Test //DATAMONGO-2016
263+
@Test // DATAMONGO-2016
266264
@SuppressWarnings("unchecked")
267265
public void passwordWithQuestionMarkShouldNotBreakParsingOfOptionString() {
268266

269267
editor.setAsText(USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM);
270268

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();
272281
}
273282
}

0 commit comments

Comments
 (0)