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;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: /**
024: * Tests {@link AbstractAuthenticationToken}.
025: *
026: * @author Ben Alex
027: * @version $Id: AbstractAuthenticationTokenTests.java 1496 2006-05-23 13:38:33Z benalex $
028: */
029: public class AbstractAuthenticationTokenTests extends TestCase {
030: //~ Instance fields ================================================================================================
031:
032: private GrantedAuthority[] authorities = null;
033:
034: //~ Constructors ===================================================================================================
035:
036: public AbstractAuthenticationTokenTests() {
037: super ();
038: }
039:
040: public AbstractAuthenticationTokenTests(String arg0) {
041: super (arg0);
042: }
043:
044: //~ Methods ========================================================================================================
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner
048: .run(AbstractAuthenticationTokenTests.class);
049: }
050:
051: public final void setUp() throws Exception {
052: super .setUp();
053:
054: authorities = new GrantedAuthority[] {
055: new GrantedAuthorityImpl("ROLE_ONE"),
056: new GrantedAuthorityImpl("ROLE_TWO") };
057: }
058:
059: public void testAuthoritiesAreImmutable() {
060: MockAuthenticationImpl token = new MockAuthenticationImpl(
061: "Test", "Password", authorities);
062: GrantedAuthority[] gotAuthorities = token.getAuthorities();
063: assertNotSame(authorities, gotAuthorities);
064:
065: gotAuthorities[0] = new GrantedAuthorityImpl("ROLE_SUPER_USER");
066:
067: // reget them and check nothing has changed
068: gotAuthorities = token.getAuthorities();
069: assertEquals(2, gotAuthorities.length);
070: assertEquals(gotAuthorities[0], authorities[0]);
071: assertEquals(gotAuthorities[1], authorities[1]);
072: assertFalse(gotAuthorities[0].equals("ROLE_SUPER_USER"));
073: assertFalse(gotAuthorities[1].equals("ROLE_SUPER_USER"));
074: }
075:
076: public void testGetters() throws Exception {
077: MockAuthenticationImpl token = new MockAuthenticationImpl(
078: "Test", "Password", authorities);
079: assertEquals("Test", token.getPrincipal());
080: assertEquals("Password", token.getCredentials());
081: assertEquals("Test", token.getName());
082: }
083:
084: public void testHashCode() throws Exception {
085: MockAuthenticationImpl token1 = new MockAuthenticationImpl(
086: "Test", "Password", authorities);
087: MockAuthenticationImpl token2 = new MockAuthenticationImpl(
088: "Test", "Password", authorities);
089: MockAuthenticationImpl token3 = new MockAuthenticationImpl(
090: null, null, new GrantedAuthority[] {});
091: assertEquals(token1.hashCode(), token2.hashCode());
092: assertTrue(token1.hashCode() != token3.hashCode());
093:
094: token2.setAuthenticated(true);
095:
096: assertTrue(token1.hashCode() != token2.hashCode());
097: }
098:
099: public void testObjectsEquals() throws Exception {
100: MockAuthenticationImpl token1 = new MockAuthenticationImpl(
101: "Test", "Password", authorities);
102: MockAuthenticationImpl token2 = new MockAuthenticationImpl(
103: "Test", "Password", authorities);
104: assertEquals(token1, token2);
105:
106: MockAuthenticationImpl token3 = new MockAuthenticationImpl(
107: "Test", "Password_Changed", authorities);
108: assertTrue(!token1.equals(token3));
109:
110: MockAuthenticationImpl token4 = new MockAuthenticationImpl(
111: "Test_Changed", "Password", authorities);
112: assertTrue(!token1.equals(token4));
113:
114: MockAuthenticationImpl token5 = new MockAuthenticationImpl(
115: "Test", "Password", new GrantedAuthority[] {
116: new GrantedAuthorityImpl("ROLE_ONE"),
117: new GrantedAuthorityImpl("ROLE_TWO_CHANGED") });
118: assertTrue(!token1.equals(token5));
119:
120: MockAuthenticationImpl token6 = new MockAuthenticationImpl(
121: "Test", "Password",
122: new GrantedAuthority[] { new GrantedAuthorityImpl(
123: "ROLE_ONE") });
124: assertTrue(!token1.equals(token6));
125:
126: MockAuthenticationImpl token7 = new MockAuthenticationImpl(
127: "Test", "Password", null);
128: assertTrue(!token1.equals(token7));
129: assertTrue(!token7.equals(token1));
130:
131: assertTrue(!token1.equals(new Integer(100)));
132: }
133:
134: public void testSetAuthenticated() throws Exception {
135: MockAuthenticationImpl token = new MockAuthenticationImpl(
136: "Test", "Password", authorities);
137: assertTrue(!token.isAuthenticated());
138: token.setAuthenticated(true);
139: assertTrue(token.isAuthenticated());
140: }
141:
142: public void testToStringWithAuthorities() {
143: MockAuthenticationImpl token = new MockAuthenticationImpl(
144: "Test", "Password", authorities);
145: assertTrue(token.toString().lastIndexOf("ROLE_TWO") != -1);
146: }
147:
148: public void testToStringWithNullAuthorities() {
149: MockAuthenticationImpl token = new MockAuthenticationImpl(
150: "Test", "Password", null);
151: assertTrue(token.toString().lastIndexOf(
152: "Not granted any authorities") != -1);
153: }
154:
155: //~ Inner Classes ==================================================================================================
156:
157: private class MockAuthenticationImpl extends
158: AbstractAuthenticationToken {
159: private Object credentials;
160: private Object principal;
161:
162: public MockAuthenticationImpl(Object principal,
163: Object credentials, GrantedAuthority[] authorities) {
164: super (authorities);
165: this .principal = principal;
166: this .credentials = credentials;
167: }
168:
169: private MockAuthenticationImpl() {
170: super (null);
171: }
172:
173: public Object getCredentials() {
174: return this .credentials;
175: }
176:
177: public Object getPrincipal() {
178: return this.principal;
179: }
180: }
181: }
|