Prechádzať zdrojové kódy

Refactored inline authority list into member variable.

Luke Taylor 17 rokov pred
rodič
commit
6601b3da5f

+ 25 - 74
cas/src/test/java/org/springframework/security/providers/cas/CasAuthenticationTokenTests.java

@@ -15,17 +15,17 @@
 
 package org.springframework.security.providers.cas;
 
+import java.util.List;
+
 import junit.framework.TestCase;
 
 import org.jasig.cas.client.validation.Assertion;
 import org.jasig.cas.client.validation.AssertionImpl;
 import org.springframework.security.GrantedAuthority;
-import org.springframework.security.GrantedAuthorityImpl;
-
 import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
-
 import org.springframework.security.userdetails.User;
 import org.springframework.security.userdetails.UserDetails;
+import org.springframework.security.util.AuthorityUtils;
 
 /**
  * Tests {@link CasAuthenticationToken}.
@@ -34,29 +34,14 @@ import org.springframework.security.userdetails.UserDetails;
  * @version $Id$
  */
 public class CasAuthenticationTokenTests extends TestCase {
-    //~ Constructors ===================================================================================================
-
-    public CasAuthenticationTokenTests() {
-        super();
-    }
-
-    public CasAuthenticationTokenTests(String arg0) {
-        super(arg0);
-    }
-
-    //~ Methods ========================================================================================================
-
-    public static void main(String[] args) {
-        junit.textui.TestRunner.run(CasAuthenticationTokenTests.class);
-    }
+    private final List<GrantedAuthority> ROLES = AuthorityUtils.createAuthorityList("ROLE_ONE","ROLE_TWO");
 
     private UserDetails makeUserDetails() {
         return makeUserDetails("user");
     }
 
     private UserDetails makeUserDetails(final String name) {
-        return new User(name, "password", true, true, true, true,
-            new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
+        return new User(name, "password", true, true, true, true, ROLES);
     }
 
     public final void setUp() throws Exception {
@@ -66,55 +51,37 @@ public class CasAuthenticationTokenTests extends TestCase {
     public void testConstructorRejectsNulls() {
         final Assertion assertion = new AssertionImpl("test");
         try {
-            new CasAuthenticationToken(null, makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
-                makeUserDetails(), assertion);
+            new CasAuthenticationToken(null, makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion);
             fail("Should have thrown IllegalArgumentException");
         } catch (IllegalArgumentException expected) {
-            assertTrue(true);
         }
 
         try {
-            new CasAuthenticationToken("key", null, "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
-                makeUserDetails(), assertion);
+            new CasAuthenticationToken("key", null, "Password", ROLES, makeUserDetails(), assertion);
             fail("Should have thrown IllegalArgumentException");
         } catch (IllegalArgumentException expected) {
-            assertTrue(true);
         }
 
         try {
-            new CasAuthenticationToken("key", makeUserDetails(), null,
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
-                makeUserDetails(), assertion);
+            new CasAuthenticationToken("key", makeUserDetails(), null, ROLES, makeUserDetails(), assertion);
             fail("Should have thrown IllegalArgumentException");
         } catch (IllegalArgumentException expected) {
-            assertTrue(true);
         }
 
         try {
-            new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
-                makeUserDetails(), null);
+            new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), null);
             fail("Should have thrown IllegalArgumentException");
         } catch (IllegalArgumentException expected) {
-            assertTrue(true);
         }
 
         try {
-            new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
-                null, assertion);
+            new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, null, assertion);
             fail("Should have thrown IllegalArgumentException");
         } catch (IllegalArgumentException expected) {
-            assertTrue(true);
         }
 
-
         try {
-            new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), null, new GrantedAuthorityImpl("ROLE_TWO")},
-                makeUserDetails(), assertion);
+            new CasAuthenticationToken("key", makeUserDetails(), "Password", AuthorityUtils.createAuthorityList("ROLE_1", null), makeUserDetails(), assertion);
             fail("Should have thrown IllegalArgumentException");
         } catch (IllegalArgumentException expected) {
             assertTrue(true);
@@ -124,12 +91,10 @@ public class CasAuthenticationTokenTests extends TestCase {
     public void testEqualsWhenEqual() {
         final Assertion assertion = new AssertionImpl("test");
 
-        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion);
 
-        CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion);
 
         assertEquals(token1, token2);
@@ -138,8 +103,7 @@ public class CasAuthenticationTokenTests extends TestCase {
     public void testGetters() {
         // Build the proxy list returned in the ticket from CAS
         final Assertion assertion = new AssertionImpl("test");
-        CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion);
         assertEquals("key".hashCode(), token.getKeyHash());
         assertEquals(makeUserDetails(), token.getPrincipal());
@@ -151,10 +115,8 @@ public class CasAuthenticationTokenTests extends TestCase {
     }
 
     public void testNoArgConstructorDoesntExist() {
-        Class clazz = CasAuthenticationToken.class;
-
         try {
-            clazz.getDeclaredConstructor((Class[]) null);
+            CasAuthenticationToken.class.getDeclaredConstructor((Class[]) null);
             fail("Should have thrown NoSuchMethodException");
         } catch (NoSuchMethodException expected) {
             assertTrue(true);
@@ -164,13 +126,11 @@ public class CasAuthenticationTokenTests extends TestCase {
     public void testNotEqualsDueToAbstractParentEqualsCheck() {
         final Assertion assertion = new AssertionImpl("test");
 
-        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion);
 
         CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails("OTHER_NAME"), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
-                makeUserDetails(), assertion);
+                ROLES, makeUserDetails(), assertion);
 
         assertTrue(!token1.equals(token2));
     }
@@ -178,26 +138,21 @@ public class CasAuthenticationTokenTests extends TestCase {
     public void testNotEqualsDueToDifferentAuthenticationClass() {
         final Assertion assertion = new AssertionImpl("test");
 
-        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion);
 
-        UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
-
+        UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password", ROLES);
         assertTrue(!token1.equals(token2));
     }
 
     public void testNotEqualsDueToKey() {
         final Assertion assertion = new AssertionImpl("test");
 
-        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion);
 
         CasAuthenticationToken token2 = new CasAuthenticationToken("DIFFERENT_KEY", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
-                makeUserDetails(), assertion);
+                ROLES, makeUserDetails(), assertion);
 
         assertTrue(!token1.equals(token2));
     }
@@ -206,12 +161,10 @@ public class CasAuthenticationTokenTests extends TestCase {
         final Assertion assertion = new AssertionImpl("test");
         final Assertion assertion2 = new AssertionImpl("test");
 
-        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion);
 
-        CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion2);
 
         assertTrue(!token1.equals(token2));
@@ -219,8 +172,7 @@ public class CasAuthenticationTokenTests extends TestCase {
 
     public void testSetAuthenticated() {
         final Assertion assertion = new AssertionImpl("test");
-        CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES,
                 makeUserDetails(), assertion);
         assertTrue(token.isAuthenticated());
         token.setAuthenticated(false);
@@ -229,8 +181,7 @@ public class CasAuthenticationTokenTests extends TestCase {
 
     public void testToString() {
         final Assertion assertion = new AssertionImpl("test");
-        CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",
-                new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")},
+        CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password",ROLES,
                 makeUserDetails(), assertion);
         String result = token.toString();
         assertTrue(result.lastIndexOf("Credentials (Service/Proxy Ticket):") != -1);