JCLOUDS-549: Fix NPE in LoginCredentials.toString

This commit is contained in:
Aled Sage 2014-04-24 10:56:44 +01:00 committed by Andrew Phillips
parent 4a729562dd
commit 25c37fc8ca
2 changed files with 15 additions and 2 deletions

View File

@ -207,7 +207,8 @@ public class LoginCredentials extends Credentials {
@Override
public String toString() {
return "[user=" + getUser() + ", passwordPresent=" + password.isPresent() + ", privateKeyPresent="
+ privateKey.isPresent() + ", shouldAuthenticateSudo=" + authenticateSudo + "]";
return "[user=" + getUser() + ", passwordPresent=" + (password != null ? password.isPresent() : false)
+ ", privateKeyPresent=" + (privateKey != null ? privateKey.isPresent() : false)
+ ", shouldAuthenticateSudo=" + authenticateSudo + "]";
}
}

View File

@ -17,6 +17,7 @@
package org.jclouds.domain;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import org.testng.annotations.Test;
@ -51,4 +52,15 @@ public class LoginCredentialsTest {
assertEquals(toTest.getOptionalPassword(), Optional.of("password"));
assertEquals(toTest.getOptionalPrivateKey(), Optional.of("key"));
}
public void testToStringWhenNullPasswordAndKey() {
LoginCredentials toTest = LoginCredentials.builder().user("myuser").build();
// also verifies that toString() does not blow up with an NPE
assertNotNull(toTest.toString());
}
public void testToString() {
LoginCredentials toTest = LoginCredentials.builder().user("myuser").password("password").privateKey("key").build();
assertNotNull(toTest.toString());
}
}