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

This commit is contained in:
Oliver Gierke
2011-09-27 16:40:56 +02:00
parent 61bf243467
commit 8ad6fe9d4c
2 changed files with 42 additions and 3 deletions

View File

@@ -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;
}
/**

View File

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