Browse files

DATACMNS-79 - UserCredentials treats empty Strings passed as null.

  • Loading branch information...
1 parent 61bf243 commit 8ad6fe9d4ca9a89a9d7bfc62dedad85267d9e6cf @olivergierke olivergierke committed Sep 27, 2011
View
8 ...a-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 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;
}
/**
View
37 ...-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()));
+ }
+}

0 comments on commit 8ad6fe9

Please sign in to comment.