@ -15,6 +15,8 @@
package org.springframework.security.providers.anonymous ;
package org.springframework.security.providers.anonymous ;
import java.util.List ;
import junit.framework.TestCase ;
import junit.framework.TestCase ;
import org.springframework.security.GrantedAuthority ;
import org.springframework.security.GrantedAuthority ;
@ -30,60 +32,46 @@ import org.springframework.security.util.AuthorityUtils;
* @version $Id$
* @version $Id$
* /
* /
public class AnonymousAuthenticationTokenTests extends TestCase {
public class AnonymousAuthenticationTokenTests extends TestCase {
private final static List < GrantedAuthority > ROLES_12 = AuthorityUtils . createAuthorityList ( "ROLE_ONE" , "ROLE_TWO" ) ;
//~ Methods ========================================================================================================
//~ Methods ========================================================================================================
public void testConstructorRejectsNulls ( ) {
public void testConstructorRejectsNulls ( ) {
try {
try {
new AnonymousAuthenticationToken ( null , "Test" ,
new AnonymousAuthenticationToken ( null , "Test" , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
} catch ( IllegalArgumentException expected ) {
} catch ( IllegalArgumentException expected ) {
assertTrue ( true ) ;
}
}
try {
try {
new AnonymousAuthenticationToken ( "key" , null ,
new AnonymousAuthenticationToken ( "key" , null , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
} catch ( IllegalArgumentException expected ) {
} catch ( IllegalArgumentException expected ) {
assertTrue ( true ) ;
}
}
// try {
// new AnonymousAuthenticationToken("key", "Test", null);
// fail("Should have thrown IllegalArgumentException");
// } catch (IllegalArgumentException expected) {
// assertTrue(true);
// }
try {
try {
new AnonymousAuthenticationToken ( "key" , "Test" , new GrantedAuthority [ ] { null } ) ;
new AnonymousAuthenticationToken ( "key" , "Test" , new GrantedAuthority [ ] { null } ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
} catch ( IllegalArgumentException expected ) {
} catch ( IllegalArgumentException expected ) {
assertTrue ( true ) ;
}
}
try {
try {
new AnonymousAuthenticationToken ( "key" , "Test" , AuthorityUtils . NO_AUTHORITIES ) ;
new AnonymousAuthenticationToken ( "key" , "Test" , AuthorityUtils . NO_AUTHORITIES ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
fail ( "Should have thrown IllegalArgumentException" ) ;
} catch ( IllegalArgumentException expected ) {
} catch ( IllegalArgumentException expected ) {
assertTrue ( true ) ;
}
}
}
}
public void testEqualsWhenEqual ( ) {
public void testEqualsWhenEqual ( ) {
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken ( "key" , "Test" ,
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken ( "key" , "Test" , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken ( "key" , "Test" , ROLES_12 ) ;
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken ( "key" , "Test" ,
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
assertEquals ( token1 , token2 ) ;
assertEquals ( token1 , token2 ) ;
}
}
public void testGetters ( ) {
public void testGetters ( ) {
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken ( "key" , "Test" ,
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken ( "key" , "Test" , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
assertEquals ( "key" . hashCode ( ) , token . getKeyHash ( ) ) ;
assertEquals ( "key" . hashCode ( ) , token . getKeyHash ( ) ) ;
assertEquals ( "Test" , token . getPrincipal ( ) ) ;
assertEquals ( "Test" , token . getPrincipal ( ) ) ;
@ -94,49 +82,39 @@ public class AnonymousAuthenticationTokenTests extends TestCase {
}
}
public void testNoArgConstructorDoesntExist ( ) {
public void testNoArgConstructorDoesntExist ( ) {
Class clazz = AnonymousAuthenticationToken . class ;
Class < ? > clazz = AnonymousAuthenticationToken . class ;
try {
try {
clazz . getDeclaredConstructor ( ( Class [ ] ) null ) ;
clazz . getDeclaredConstructor ( ( Class [ ] ) null ) ;
fail ( "Should have thrown NoSuchMethodException" ) ;
fail ( "Should have thrown NoSuchMethodException" ) ;
} catch ( NoSuchMethodException expected ) {
} catch ( NoSuchMethodException expected ) {
assertTrue ( true ) ;
}
}
}
}
public void testNotEqualsDueToAbstractParentEqualsCheck ( ) {
public void testNotEqualsDueToAbstractParentEqualsCheck ( ) {
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken ( "key" , "Test" ,
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken ( "key" , "Test" , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken ( "key" , "DIFFERENT_PRINCIPAL" , ROLES_12 ) ;
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken ( "key" , "DIFFERENT_PRINCIPAL" ,
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
assertFalse ( token1 . equals ( token2 ) ) ;
assertFalse ( token1 . equals ( token2 ) ) ;
}
}
public void testNotEqualsDueToDifferentAuthenticationClass ( ) {
public void testNotEqualsDueToDifferentAuthenticationClass ( ) {
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken ( "key" , "Test" ,
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken ( "key" , "Test" , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken ( "Test" , "Password" , ROLES_12 ) ;
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken ( "Test" , "Password" ,
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
assertFalse ( token1 . equals ( token2 ) ) ;
assertFalse ( token1 . equals ( token2 ) ) ;
}
}
public void testNotEqualsDueToKey ( ) {
public void testNotEqualsDueToKey ( ) {
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken ( "key" , "Test" ,
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken ( "key" , "Test" , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken ( "DIFFERENT_KEY" , "Test" ,
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken ( "DIFFERENT_KEY" , "Test" , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
assertFalse ( token1 . equals ( token2 ) ) ;
assertFalse ( token1 . equals ( token2 ) ) ;
}
}
public void testSetAuthenticatedIgnored ( ) {
public void testSetAuthenticatedIgnored ( ) {
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken ( "key" , "Test" ,
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken ( "key" , "Test" , ROLES_12 ) ;
new GrantedAuthority [ ] { new GrantedAuthorityImpl ( "ROLE_ONE" ) , new GrantedAuthorityImpl ( "ROLE_TWO" ) } ) ;
assertTrue ( token . isAuthenticated ( ) ) ;
assertTrue ( token . isAuthenticated ( ) ) ;
token . setAuthenticated ( false ) ;
token . setAuthenticated ( false ) ;
assertTrue ( ! token . isAuthenticated ( ) ) ;
assertTrue ( ! token . isAuthenticated ( ) ) ;