From 8ad6fe9d4ca9a89a9d7bfc62dedad85267d9e6cf Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 27 Sep 2011 16:40:56 +0200 Subject: [PATCH] DATACMNS-79 - UserCredentials treats empty Strings passed as null. --- .../data/authentication/UserCredentials.java | 8 ++-- .../UserCredentialsUnitTests.java | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 spring-data-commons-core/src/test/java/org/springframework/data/authentication/UserCredentialsUnitTests.java diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/authentication/UserCredentials.java b/spring-data-commons-core/src/main/java/org/springframework/data/authentication/UserCredentials.java index b83f8f331..529b50a64 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/authentication/UserCredentials.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/authentication/UserCredentials.java @@ -16,6 +16,7 @@ package org.springframework.data.authentication; import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; /** * Class used to provide credentials for username/password authentication @@ -33,14 +34,15 @@ public class UserCredentials { } /** - * Creates a new {@link UserCredentials} instance from the given username and password. + * Creates a new {@link UserCredentials} instance from the given username and password. Empty {@link String}s provided + * will be treated like no username or password set. * * @param username * @param password */ public UserCredentials(String username, String password) { - this.username = username; - this.password = password; + this.username = StringUtils.hasText(username) ? username : null; + this.password = StringUtils.hasText(password) ? password : null; } /** diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/authentication/UserCredentialsUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/authentication/UserCredentialsUnitTests.java new file mode 100644 index 000000000..03cfe7313 --- /dev/null +++ b/spring-data-commons-core/src/test/java/org/springframework/data/authentication/UserCredentialsUnitTests.java @@ -0,0 +1,37 @@ +/* + * Copyright 2011 by the original author(s). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.authentication; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Unit tests for {@link UserCredentials}. + * + * @author Oliver Gierke + */ +public class UserCredentialsUnitTests { + + @Test + public void treatsEmptyStringAsNull() { + + UserCredentials credentials = new UserCredentials("", ""); + assertThat(credentials.getUsername(), is(nullValue())); + assertThat(credentials.getPassword(), is(nullValue())); + } +}