From 0cb0cb700be154c2ccf5813fe40b8c25ec626ce7 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 4 Jul 2018 11:15:26 +0200 Subject: [PATCH] DATAMONGO-2016 - Polishing. Fail gracefully if query string parameter has no value. Reformat test. Original pull request: #578. --- .../config/MongoCredentialPropertyEditor.java | 6 ++++ ...ongoCredentialPropertyEditorUnitTests.java | 28 ++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java index 5e0d70e36..bc8a76ae2 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java @@ -33,6 +33,7 @@ import com.mongodb.MongoCredential; * @author Christoph Strobl * @author Oliver Gierke * @author Stephen Tyler Conrad + * @author Mark Paluch * @since 1.7 */ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { @@ -167,6 +168,11 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { for (String option : text.substring(optionsSeperationIndex + 1).split(OPTION_VALUE_DELIMINATOR)) { String[] optionArgs = option.split("="); + + if (optionArgs.length == 1) { + throw new IllegalArgumentException(String.format("Query parameter '%s' has no value!", optionArgs[0])); + } + properties.put(optionArgs[0], optionArgs[1]); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java index 386264483..f0a9073b5 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java @@ -63,6 +63,7 @@ public class MongoCredentialPropertyEditorUnitTests { static final String USER_5_AUTH_STRING = USER_5_NAME + ":" + USER_5_PWD + "@" + USER_5_DB; static final String USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN"; + static final String USER_5_AUTH_STRING_WITH_QUERY_ARGS = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN&foo=&bar"; static final MongoCredential USER_1_CREDENTIALS = MongoCredential.createCredential(USER_1_NAME, USER_1_DB, USER_1_PWD.toCharArray()); @@ -78,8 +79,8 @@ public class MongoCredentialPropertyEditorUnitTests { static final MongoCredential USER_5_CREDENTIALS = MongoCredential.createCredential(USER_5_NAME, USER_5_DB, USER_5_PWD.toCharArray()); - static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_5_NAME, USER_5_DB, - USER_5_PWD.toCharArray()); + static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_5_NAME, + USER_5_DB, USER_5_PWD.toCharArray()); MongoCredentialPropertyEditor editor; @@ -146,8 +147,8 @@ public class MongoCredentialPropertyEditorUnitTests { @SuppressWarnings("unchecked") public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswordStringWithDatabaseAndAuthOptions() { - editor.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays.asList( - USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM, USER_2_AUTH_STRING_WITH_MONGODB_CR_AUTH_MECHANISM))); + editor.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays + .asList(USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM, USER_2_AUTH_STRING_WITH_MONGODB_CR_AUTH_MECHANISM))); assertThat((List) editor.getValue(), contains(USER_1_CREDENTIALS_PLAIN_AUTH, USER_2_CREDENTIALS_CR_AUTH)); @@ -157,8 +158,8 @@ public class MongoCredentialPropertyEditorUnitTests { @SuppressWarnings("unchecked") public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleUserNamePasswordStringWithDatabaseAndMixedOptions() { - editor.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays.asList( - USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM, USER_2_AUTH_STRING))); + editor.setAsText(StringUtils.collectionToCommaDelimitedString( + Arrays.asList(USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM, USER_2_AUTH_STRING))); assertThat((List) editor.getValue(), contains(USER_1_CREDENTIALS_PLAIN_AUTH, USER_2_CREDENTIALS)); } @@ -167,8 +168,8 @@ public class MongoCredentialPropertyEditorUnitTests { @SuppressWarnings("unchecked") public void shouldReturnCredentialsValueCorrectlyWhenGivenMultipleQuotedUserNamePasswordStringWithDatabaseAndNoOptions() { - editor.setAsText(StringUtils.collectionToCommaDelimitedString(Arrays.asList("'" + USER_1_AUTH_STRING + "'", "'" - + USER_2_AUTH_STRING + "'"))); + editor.setAsText(StringUtils.collectionToCommaDelimitedString( + Arrays.asList("'" + USER_1_AUTH_STRING + "'", "'" + USER_2_AUTH_STRING + "'"))); assertThat((List) editor.getValue(), contains(USER_1_CREDENTIALS, USER_2_CREDENTIALS)); } @@ -197,7 +198,8 @@ public class MongoCredentialPropertyEditorUnitTests { editor.setAsText("tyrion?uri.authMechanism=MONGODB-X509"); - assertThat((List) editor.getValue(), contains(MongoCredential.createMongoX509Credential("tyrion"))); + assertThat((List) editor.getValue(), + contains(MongoCredential.createMongoX509Credential("tyrion"))); } @Test(expected = IllegalArgumentException.class) // DATAMONGO-1257 @@ -225,7 +227,7 @@ public class MongoCredentialPropertyEditorUnitTests { assertThat((List) editor.getValue(), contains(USER_5_CREDENTIALS)); } - @Test //DATAMONGO-2016 + @Test // DATAMONGO-2016 @SuppressWarnings("unchecked") public void passwordWithQuestionMarkShouldNotBreakParsingOfOptionString() { @@ -233,4 +235,10 @@ public class MongoCredentialPropertyEditorUnitTests { assertThat((List) editor.getValue(), contains(USER_5_CREDENTIALS_PLAIN_AUTH)); } + + @Test(expected = IllegalArgumentException.class) // DATAMONGO-2016 + @SuppressWarnings("unchecked") + public void failsGracefullyOnEmptyQueryArgument() { + editor.setAsText(USER_5_AUTH_STRING_WITH_QUERY_ARGS); + } }