mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-16 23:33:31 +00:00
Added test for immutability of authorities array. Refactored standard authorities array into an instance field.
This commit is contained in:
parent
2ab5af0a69
commit
6c29a6d17e
@ -28,6 +28,10 @@ import org.acegisecurity.GrantedAuthorityImpl;
|
|||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class AbstractAuthenticationTokenTests extends TestCase {
|
public class AbstractAuthenticationTokenTests extends TestCase {
|
||||||
|
//~ Instance fields ========================================================
|
||||||
|
|
||||||
|
private GrantedAuthority[] authorities = null;
|
||||||
|
|
||||||
//~ Constructors ===========================================================
|
//~ Constructors ===========================================================
|
||||||
|
|
||||||
public AbstractAuthenticationTokenTests() {
|
public AbstractAuthenticationTokenTests() {
|
||||||
@ -46,13 +50,14 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
|||||||
|
|
||||||
public final void setUp() throws Exception {
|
public final void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
||||||
|
authorities = new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"),
|
||||||
|
new GrantedAuthorityImpl("ROLE_TWO")};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetters() throws Exception {
|
public void testGetters() throws Exception {
|
||||||
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
assertEquals("Test", token.getPrincipal());
|
assertEquals("Test", token.getPrincipal());
|
||||||
assertEquals("Password", token.getCredentials());
|
assertEquals("Password", token.getCredentials());
|
||||||
assertEquals("Test", token.getName());
|
assertEquals("Test", token.getName());
|
||||||
@ -60,13 +65,9 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
|||||||
|
|
||||||
public void testHashCode() throws Exception {
|
public void testHashCode() throws Exception {
|
||||||
MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
MockAuthenticationImpl token2 = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token2 = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
MockAuthenticationImpl token3 = new MockAuthenticationImpl(null, null,
|
MockAuthenticationImpl token3 = new MockAuthenticationImpl(null, null,
|
||||||
new GrantedAuthority[] {});
|
new GrantedAuthority[] {});
|
||||||
assertEquals(token1.hashCode(), token2.hashCode());
|
assertEquals(token1.hashCode(), token2.hashCode());
|
||||||
@ -79,25 +80,17 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
|||||||
|
|
||||||
public void testObjectsEquals() throws Exception {
|
public void testObjectsEquals() throws Exception {
|
||||||
MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token1 = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
MockAuthenticationImpl token2 = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token2 = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
assertEquals(token1, token2);
|
assertEquals(token1, token2);
|
||||||
|
|
||||||
MockAuthenticationImpl token3 = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token3 = new MockAuthenticationImpl("Test",
|
||||||
"Password_Changed",
|
"Password_Changed", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
assertTrue(!token1.equals(token3));
|
assertTrue(!token1.equals(token3));
|
||||||
|
|
||||||
MockAuthenticationImpl token4 = new MockAuthenticationImpl("Test_Changed",
|
MockAuthenticationImpl token4 = new MockAuthenticationImpl("Test_Changed",
|
||||||
"Password",
|
"Password", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
assertTrue(!token1.equals(token4));
|
assertTrue(!token1.equals(token4));
|
||||||
|
|
||||||
MockAuthenticationImpl token5 = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token5 = new MockAuthenticationImpl("Test",
|
||||||
@ -121,9 +114,7 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
|||||||
|
|
||||||
public void testSetAuthenticated() throws Exception {
|
public void testSetAuthenticated() throws Exception {
|
||||||
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
assertTrue(!token.isAuthenticated());
|
assertTrue(!token.isAuthenticated());
|
||||||
token.setAuthenticated(true);
|
token.setAuthenticated(true);
|
||||||
assertTrue(token.isAuthenticated());
|
assertTrue(token.isAuthenticated());
|
||||||
@ -131,9 +122,7 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
|||||||
|
|
||||||
public void testToStringWithAuthorities() {
|
public void testToStringWithAuthorities() {
|
||||||
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
||||||
"Password",
|
"Password", authorities);
|
||||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
|
||||||
"ROLE_TWO")});
|
|
||||||
assertTrue(token.toString().lastIndexOf("ROLE_TWO") != -1);
|
assertTrue(token.toString().lastIndexOf("ROLE_TWO") != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,6 +132,23 @@ public class AbstractAuthenticationTokenTests extends TestCase {
|
|||||||
assertTrue(token.toString().lastIndexOf("Not granted any authorities") != -1);
|
assertTrue(token.toString().lastIndexOf("Not granted any authorities") != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testAuthoritiesAreImmutable() {
|
||||||
|
MockAuthenticationImpl token = new MockAuthenticationImpl("Test",
|
||||||
|
"Password", authorities);
|
||||||
|
GrantedAuthority[] gotAuthorities = token.getAuthorities();
|
||||||
|
assertNotSame(authorities, gotAuthorities);
|
||||||
|
|
||||||
|
gotAuthorities[0] = new GrantedAuthorityImpl("ROLE_SUPER_USER");
|
||||||
|
|
||||||
|
// reget them and check nothing has changed
|
||||||
|
gotAuthorities = token.getAuthorities();
|
||||||
|
assertEquals(2, gotAuthorities.length);
|
||||||
|
assertEquals(gotAuthorities[0], authorities[0]);
|
||||||
|
assertEquals(gotAuthorities[1], authorities[1]);
|
||||||
|
assertFalse(gotAuthorities[0].equals("ROLE_SUPER_USER"));
|
||||||
|
assertFalse(gotAuthorities[1].equals("ROLE_SUPER_USER"));
|
||||||
|
}
|
||||||
|
|
||||||
//~ Inner Classes ==========================================================
|
//~ Inner Classes ==========================================================
|
||||||
|
|
||||||
private class MockAuthenticationImpl extends AbstractAuthenticationToken {
|
private class MockAuthenticationImpl extends AbstractAuthenticationToken {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user