Skip to content

Commit 0cb0cb7

Browse files
committed
DATAMONGO-2016 - Polishing.
Fail gracefully if query string parameter has no value. Reformat test. Original pull request: #578.
1 parent 364920e commit 0cb0cb7

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @author Christoph Strobl
3434
* @author Oliver Gierke
3535
* @author Stephen Tyler Conrad
36+
* @author Mark Paluch
3637
* @since 1.7
3738
*/
3839
public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
@@ -167,6 +168,11 @@ private static Properties extractOptions(String text) {
167168

168169
for (String option : text.substring(optionsSeperationIndex + 1).split(OPTION_VALUE_DELIMINATOR)) {
169170
String[] optionArgs = option.split("=");
171+
172+
if (optionArgs.length == 1) {
173+
throw new IllegalArgumentException(String.format("Query parameter '%s' has no value!", optionArgs[0]));
174+
}
175+
170176
properties.put(optionArgs[0], optionArgs[1]);
171177
}
172178

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class MongoCredentialPropertyEditorUnitTests {
6363

6464
static final String USER_5_AUTH_STRING = USER_5_NAME + ":" + USER_5_PWD + "@" + USER_5_DB;
6565
static final String USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN";
66+
static final String USER_5_AUTH_STRING_WITH_QUERY_ARGS = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN&foo=&bar";
6667

6768
static final MongoCredential USER_1_CREDENTIALS = MongoCredential.createCredential(USER_1_NAME, USER_1_DB,
6869
USER_1_PWD.toCharArray());
@@ -78,8 +79,8 @@ public class MongoCredentialPropertyEditorUnitTests {
7879

7980
static final MongoCredential USER_5_CREDENTIALS = MongoCredential.createCredential(USER_5_NAME, USER_5_DB,
8081
USER_5_PWD.toCharArray());
81-
static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_5_NAME, USER_5_DB,
82-
USER_5_PWD.toCharArray());
82+
static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_5_NAME,
83+
USER_5_DB, USER_5_PWD.toCharArray());
8384

8485
MongoCredentialPropertyEditor editor;
8586

@@ -146,8 +147,8 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswo
146147
@SuppressWarnings("unchecked")
147148
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswordStringWithDatabaseAndAuthOptions() {
148149

149-
editor.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays.asList(
150-
USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM, USER_2_AUTH_STRING_WITH_MONGODB_CR_AUTH_MECHANISM)));
150+
editor.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays
151+
.asList(USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM, USER_2_AUTH_STRING_WITH_MONGODB_CR_AUTH_MECHANISM)));
151152

152153
assertThat((List<MongoCredential>) editor.getValue(),
153154
contains(USER_1_CREDENTIALS_PLAIN_AUTH, USER_2_CREDENTIALS_CR_AUTH));
@@ -157,8 +158,8 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswo
157158
@SuppressWarnings("unchecked")
158159
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswordStringWithDatabaseAndMixedOptions() {
159160

160-
editor.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays.asList(
161-
USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM, USER_2_AUTH_STRING)));
161+
editor.setAsText(StringUtils.collectionToCommaDelimitedString(
162+
Arrays.asList(USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM, USER_2_AUTH_STRING)));
162163

163164
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_1_CREDENTIALS_PLAIN_AUTH, USER_2_CREDENTIALS));
164165
}
@@ -167,8 +168,8 @@ public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswo
167168
@SuppressWarnings("unchecked")
168169
public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleQuotedUserNamePasswordStringWithDatabaseAndNoOptions() {
169170

170-
editor.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays.asList("'" + USER_1_AUTH_STRING + "'", "'"
171-
+ USER_2_AUTH_STRING + "'")));
171+
editor.setAsText(StringUtils.collectionToCommaDelimitedString(
172+
Arrays.asList("'" + USER_1_AUTH_STRING + "'", "'" + USER_2_AUTH_STRING + "'")));
172173

173174
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_1_CREDENTIALS, USER_2_CREDENTIALS));
174175
}
@@ -197,7 +198,8 @@ public void shouldReturnX509CredentialsCorrectlyWhenNoDbSpecified() {
197198

198199
editor.setAsText("tyrion?uri.authMechanism=MONGODB-X509");
199200

200-
assertThat((List<MongoCredential>) editor.getValue(), contains(MongoCredential.createMongoX509Credential("tyrion")));
201+
assertThat((List<MongoCredential>) editor.getValue(),
202+
contains(MongoCredential.createMongoX509Credential("tyrion")));
201203
}
202204

203205
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1257
@@ -225,12 +227,18 @@ public void passwordWithQuestionMarkShouldNotBeInterpretedAsOptionString() {
225227
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_5_CREDENTIALS));
226228
}
227229

228-
@Test //DATAMONGO-2016
230+
@Test // DATAMONGO-2016
229231
@SuppressWarnings("unchecked")
230232
public void passwordWithQuestionMarkShouldNotBreakParsingOfOptionString() {
231233

232234
editor.setAsText(USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM);
233235

234236
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_5_CREDENTIALS_PLAIN_AUTH));
235237
}
238+
239+
@Test(expected = IllegalArgumentException.class) // DATAMONGO-2016
240+
@SuppressWarnings("unchecked")
241+
public void failsGracefullyOnEmptyQueryArgument() {
242+
editor.setAsText(USER_5_AUTH_STRING_WITH_QUERY_ARGS);
243+
}
236244
}

0 commit comments

Comments
 (0)