DATACMNS-142 - Polished API of UserCredentials.

Added constant to represent no credentials. Removed empty constructor. Added methods to ask whether username or password is configured.
This commit is contained in:
Oliver Gierke
2012-04-02 15:24:09 +02:00
parent b4d21008e3
commit cb375b250f
2 changed files with 60 additions and 4 deletions

View File

@@ -26,13 +26,11 @@ import org.springframework.util.StringUtils;
*/
public class UserCredentials {
public static final UserCredentials NO_CREDENTIALS = new UserCredentials(null, null);
private final String username;
private final String password;
public UserCredentials() {
this(null, null);
}
/**
* 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.
@@ -63,6 +61,24 @@ public class UserCredentials {
return password;
}
/**
* Returns whether the credentials contain a username.
*
* @return
*/
public boolean hasUsername() {
return this.username != null;
}
/**
* Returns whether the credentials contain a password.
*
* @return
*/
public boolean hasPassword() {
return this.password != null;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)

View File

@@ -32,6 +32,46 @@ public class UserCredentialsUnitTests {
UserCredentials credentials = new UserCredentials("", "");
assertThat(credentials.getUsername(), is(nullValue()));
assertThat(credentials.hasUsername(), is(false));
assertThat(credentials.getPassword(), is(nullValue()));
assertThat(credentials.hasPassword(), is(false));
}
/**
* @see DATACMNS-142
*/
@Test
public void noCredentialsNullsUsernameAndPassword() {
assertThat(UserCredentials.NO_CREDENTIALS.getUsername(), is(nullValue()));
assertThat(UserCredentials.NO_CREDENTIALS.getPassword(), is(nullValue()));
}
/**
* @see DATACMNS-142
*/
@Test
public void configuresUsernameCorrectly() {
UserCredentials credentials = new UserCredentials("username", null);
assertThat(credentials.hasUsername(), is(true));
assertThat(credentials.getUsername(), is("username"));
assertThat(credentials.hasPassword(), is(false));
assertThat(credentials.getPassword(), is(nullValue()));
}
/**
* @see DATACMNS-142
*/
@Test
public void configuresPasswordCorrectly() {
UserCredentials credentials = new UserCredentials(null, "password");
assertThat(credentials.hasUsername(), is(false));
assertThat(credentials.getUsername(), is(nullValue()));
assertThat(credentials.hasPassword(), is(true));
assertThat(credentials.getPassword(), is("password"));
}
}