001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers.rememberme;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
023:
024: /**
025: * Tests {@link RememberMeAuthenticationToken}.
026: *
027: * @author Ben Alex
028: * @version $Id: RememberMeAuthenticationTokenTests.java 1877 2007-05-25 05:33:06Z benalex $
029: */
030: public class RememberMeAuthenticationTokenTests extends TestCase {
031: //~ Constructors ===================================================================================================
032:
033: public RememberMeAuthenticationTokenTests() {
034: super ();
035: }
036:
037: public RememberMeAuthenticationTokenTests(String arg0) {
038: super (arg0);
039: }
040:
041: //~ Methods ========================================================================================================
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner
045: .run(RememberMeAuthenticationTokenTests.class);
046: }
047:
048: public final void setUp() throws Exception {
049: super .setUp();
050: }
051:
052: public void testConstructorRejectsNulls() {
053: try {
054: new RememberMeAuthenticationToken(null, "Test",
055: new GrantedAuthority[] {
056: new GrantedAuthorityImpl("ROLE_ONE"),
057: new GrantedAuthorityImpl("ROLE_TWO") });
058: fail("Should have thrown IllegalArgumentException");
059: } catch (IllegalArgumentException expected) {
060: assertTrue(true);
061: }
062:
063: try {
064: new RememberMeAuthenticationToken("key", null,
065: new GrantedAuthority[] {
066: new GrantedAuthorityImpl("ROLE_ONE"),
067: new GrantedAuthorityImpl("ROLE_TWO") });
068: fail("Should have thrown IllegalArgumentException");
069: } catch (IllegalArgumentException expected) {
070: assertTrue(true);
071: }
072:
073: try {
074: new RememberMeAuthenticationToken("key", "Test",
075: new GrantedAuthority[] { null });
076: fail("Should have thrown IllegalArgumentException");
077: } catch (IllegalArgumentException expected) {
078: assertTrue(true);
079: }
080: }
081:
082: public void testEqualsWhenEqual() {
083: RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken(
084: "key", "Test", new GrantedAuthority[] {
085: new GrantedAuthorityImpl("ROLE_ONE"),
086: new GrantedAuthorityImpl("ROLE_TWO") });
087:
088: RememberMeAuthenticationToken token2 = new RememberMeAuthenticationToken(
089: "key", "Test", new GrantedAuthority[] {
090: new GrantedAuthorityImpl("ROLE_ONE"),
091: new GrantedAuthorityImpl("ROLE_TWO") });
092:
093: assertEquals(token1, token2);
094: }
095:
096: public void testGetters() {
097: RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(
098: "key", "Test", new GrantedAuthority[] {
099: new GrantedAuthorityImpl("ROLE_ONE"),
100: new GrantedAuthorityImpl("ROLE_TWO") });
101:
102: assertEquals("key".hashCode(), token.getKeyHash());
103: assertEquals("Test", token.getPrincipal());
104: assertEquals("", token.getCredentials());
105: assertEquals("ROLE_ONE", token.getAuthorities()[0]
106: .getAuthority());
107: assertEquals("ROLE_TWO", token.getAuthorities()[1]
108: .getAuthority());
109: assertTrue(token.isAuthenticated());
110: }
111:
112: public void testNoArgConstructorDoesntExist() {
113: Class clazz = RememberMeAuthenticationToken.class;
114:
115: try {
116: clazz.getDeclaredConstructor((Class[]) null);
117: fail("Should have thrown NoSuchMethodException");
118: } catch (NoSuchMethodException expected) {
119: assertTrue(true);
120: }
121: }
122:
123: public void testNotEqualsDueToAbstractParentEqualsCheck() {
124: RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken(
125: "key", "Test", new GrantedAuthority[] {
126: new GrantedAuthorityImpl("ROLE_ONE"),
127: new GrantedAuthorityImpl("ROLE_TWO") });
128:
129: RememberMeAuthenticationToken token2 = new RememberMeAuthenticationToken(
130: "key", "DIFFERENT_PRINCIPAL", new GrantedAuthority[] {
131: new GrantedAuthorityImpl("ROLE_ONE"),
132: new GrantedAuthorityImpl("ROLE_TWO") });
133:
134: assertFalse(token1.equals(token2));
135: }
136:
137: public void testNotEqualsDueToDifferentAuthenticationClass() {
138: RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken(
139: "key", "Test", new GrantedAuthority[] {
140: new GrantedAuthorityImpl("ROLE_ONE"),
141: new GrantedAuthorityImpl("ROLE_TWO") });
142:
143: UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken(
144: "Test", "Password", new GrantedAuthority[] {
145: new GrantedAuthorityImpl("ROLE_ONE"),
146: new GrantedAuthorityImpl("ROLE_TWO") });
147:
148: assertFalse(token1.equals(token2));
149: }
150:
151: public void testNotEqualsDueToKey() {
152: RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken(
153: "key", "Test", new GrantedAuthority[] {
154: new GrantedAuthorityImpl("ROLE_ONE"),
155: new GrantedAuthorityImpl("ROLE_TWO") });
156:
157: RememberMeAuthenticationToken token2 = new RememberMeAuthenticationToken(
158: "DIFFERENT_KEY", "Test", new GrantedAuthority[] {
159: new GrantedAuthorityImpl("ROLE_ONE"),
160: new GrantedAuthorityImpl("ROLE_TWO") });
161:
162: assertFalse(token1.equals(token2));
163: }
164:
165: public void testSetAuthenticatedIgnored() {
166: RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(
167: "key", "Test", new GrantedAuthority[] {
168: new GrantedAuthorityImpl("ROLE_ONE"),
169: new GrantedAuthorityImpl("ROLE_TWO") });
170: assertTrue(token.isAuthenticated());
171: token.setAuthenticated(false);
172: assertTrue(!token.isAuthenticated());
173: }
174: }
|