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.anonymous;
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 AnonymousAuthenticationToken}.
026: *
027: * @author Ben Alex
028: * @version $Id: AnonymousAuthenticationTokenTests.java 1877 2007-05-25 05:33:06Z benalex $
029: */
030: public class AnonymousAuthenticationTokenTests extends TestCase {
031: //~ Constructors ===================================================================================================
032:
033: public AnonymousAuthenticationTokenTests() {
034: super ();
035: }
036:
037: public AnonymousAuthenticationTokenTests(String arg0) {
038: super (arg0);
039: }
040:
041: //~ Methods ========================================================================================================
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner
045: .run(AnonymousAuthenticationTokenTests.class);
046: }
047:
048: public final void setUp() throws Exception {
049: super .setUp();
050: }
051:
052: public void testConstructorRejectsNulls() {
053: try {
054: new AnonymousAuthenticationToken(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 AnonymousAuthenticationToken("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 AnonymousAuthenticationToken("key", "Test", null);
075: fail("Should have thrown IllegalArgumentException");
076: } catch (IllegalArgumentException expected) {
077: assertTrue(true);
078: }
079:
080: try {
081: new AnonymousAuthenticationToken("key", "Test",
082: new GrantedAuthority[] { null });
083: fail("Should have thrown IllegalArgumentException");
084: } catch (IllegalArgumentException expected) {
085: assertTrue(true);
086: }
087:
088: try {
089: new AnonymousAuthenticationToken("key", "Test",
090: new GrantedAuthority[] {});
091: fail("Should have thrown IllegalArgumentException");
092: } catch (IllegalArgumentException expected) {
093: assertTrue(true);
094: }
095: }
096:
097: public void testEqualsWhenEqual() {
098: AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken(
099: "key", "Test", new GrantedAuthority[] {
100: new GrantedAuthorityImpl("ROLE_ONE"),
101: new GrantedAuthorityImpl("ROLE_TWO") });
102:
103: AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken(
104: "key", "Test", new GrantedAuthority[] {
105: new GrantedAuthorityImpl("ROLE_ONE"),
106: new GrantedAuthorityImpl("ROLE_TWO") });
107:
108: assertEquals(token1, token2);
109: }
110:
111: public void testGetters() {
112: AnonymousAuthenticationToken token = new AnonymousAuthenticationToken(
113: "key", "Test", new GrantedAuthority[] {
114: new GrantedAuthorityImpl("ROLE_ONE"),
115: new GrantedAuthorityImpl("ROLE_TWO") });
116:
117: assertEquals("key".hashCode(), token.getKeyHash());
118: assertEquals("Test", token.getPrincipal());
119: assertEquals("", token.getCredentials());
120: assertEquals("ROLE_ONE", token.getAuthorities()[0]
121: .getAuthority());
122: assertEquals("ROLE_TWO", token.getAuthorities()[1]
123: .getAuthority());
124: assertTrue(token.isAuthenticated());
125: }
126:
127: public void testNoArgConstructorDoesntExist() {
128: Class clazz = AnonymousAuthenticationToken.class;
129:
130: try {
131: clazz.getDeclaredConstructor((Class[]) null);
132: fail("Should have thrown NoSuchMethodException");
133: } catch (NoSuchMethodException expected) {
134: assertTrue(true);
135: }
136: }
137:
138: public void testNotEqualsDueToAbstractParentEqualsCheck() {
139: AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken(
140: "key", "Test", new GrantedAuthority[] {
141: new GrantedAuthorityImpl("ROLE_ONE"),
142: new GrantedAuthorityImpl("ROLE_TWO") });
143:
144: AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken(
145: "key", "DIFFERENT_PRINCIPAL", new GrantedAuthority[] {
146: new GrantedAuthorityImpl("ROLE_ONE"),
147: new GrantedAuthorityImpl("ROLE_TWO") });
148:
149: assertFalse(token1.equals(token2));
150: }
151:
152: public void testNotEqualsDueToDifferentAuthenticationClass() {
153: AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken(
154: "key", "Test", new GrantedAuthority[] {
155: new GrantedAuthorityImpl("ROLE_ONE"),
156: new GrantedAuthorityImpl("ROLE_TWO") });
157:
158: UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken(
159: "Test", "Password", new GrantedAuthority[] {
160: new GrantedAuthorityImpl("ROLE_ONE"),
161: new GrantedAuthorityImpl("ROLE_TWO") });
162:
163: assertFalse(token1.equals(token2));
164: }
165:
166: public void testNotEqualsDueToKey() {
167: AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken(
168: "key", "Test", new GrantedAuthority[] {
169: new GrantedAuthorityImpl("ROLE_ONE"),
170: new GrantedAuthorityImpl("ROLE_TWO") });
171:
172: AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken(
173: "DIFFERENT_KEY", "Test", new GrantedAuthority[] {
174: new GrantedAuthorityImpl("ROLE_ONE"),
175: new GrantedAuthorityImpl("ROLE_TWO") });
176:
177: assertFalse(token1.equals(token2));
178: }
179:
180: public void testSetAuthenticatedIgnored() {
181: AnonymousAuthenticationToken token = new AnonymousAuthenticationToken(
182: "key", "Test", new GrantedAuthority[] {
183: new GrantedAuthorityImpl("ROLE_ONE"),
184: new GrantedAuthorityImpl("ROLE_TWO") });
185: assertTrue(token.isAuthenticated());
186: token.setAuthenticated(false);
187: assertTrue(!token.isAuthenticated());
188: }
189: }
|