import org.testng.annotations.Test;
@Test
public class UsernamePasswordAuthenticationTest {
public void testAuthenticated() {
UsernamePasswordAuthentication token = new UsernamePasswordAuthentication("Test",
"Password", null);
// check default given we passed some GrantedAuthorty[]s (well, we
// passed null)
assertTrue(token.isAuthenticated());
// check explicit set to untrusted (we can safely go from trusted to
// untrusted, but not the reverse)
token.setAuthenticated(false);
assertTrue(!token.isAuthenticated());
// Now let's create a UsernamePasswordAuthentication without any
// GrantedAuthorty[]s (different constructor)
token = new UsernamePasswordAuthentication("Test", "Password");
assertTrue(!token.isAuthenticated());
// check we're allowed to still set it to untrusted
token.setAuthenticated(false);
assertTrue(!token.isAuthenticated());
// check denied changing it to trusted
try {
token.setAuthenticated(true);
fail("Should have prohibited setAuthenticated(true)");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}