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.adapters;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: /**
024: * Tests {@link AbstractAdapterAuthenticationToken}.
025: *
026: * @author Ben Alex
027: * @version $Id: AbstractAdapterAuthenticationTokenTests.java 1496 2006-05-23 13:38:33Z benalex $
028: */
029: public class AbstractAdapterAuthenticationTokenTests extends TestCase {
030: //~ Constructors ===================================================================================================
031:
032: public AbstractAdapterAuthenticationTokenTests() {
033: super ();
034: }
035:
036: public AbstractAdapterAuthenticationTokenTests(String arg0) {
037: super (arg0);
038: }
039:
040: //~ Methods ========================================================================================================
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner
044: .run(AbstractAdapterAuthenticationTokenTests.class);
045: }
046:
047: public final void setUp() throws Exception {
048: super .setUp();
049: }
050:
051: public void testGetters() throws Exception {
052: MockDecisionManagerImpl token = new MockDecisionManagerImpl(
053: "my_password", "Test", "Password",
054: new GrantedAuthority[] {
055: new GrantedAuthorityImpl("ROLE_ONE"),
056: new GrantedAuthorityImpl("ROLE_TWO") });
057: assertEquals("Test", token.getPrincipal());
058: assertEquals("Password", token.getCredentials());
059: assertEquals("my_password".hashCode(), token.getKeyHash());
060: }
061:
062: public void testIsUserInRole() throws Exception {
063: MockDecisionManagerImpl token = new MockDecisionManagerImpl(
064: "my_password", "Test", "Password",
065: new GrantedAuthority[] {
066: new GrantedAuthorityImpl("ROLE_ONE"),
067: new GrantedAuthorityImpl("ROLE_TWO") });
068: assertTrue(token.isUserInRole("ROLE_ONE"));
069: assertTrue(token.isUserInRole("ROLE_TWO"));
070: assertTrue(!token.isUserInRole(""));
071: assertTrue(!token.isUserInRole("ROLE_ONE "));
072: assertTrue(!token.isUserInRole("role_one"));
073: assertTrue(!token.isUserInRole("ROLE_XXXX"));
074: }
075:
076: public void testObjectsEquals() throws Exception {
077: MockDecisionManagerImpl token1 = new MockDecisionManagerImpl(
078: "my_password", "Test", "Password",
079: new GrantedAuthority[] {
080: new GrantedAuthorityImpl("ROLE_ONE"),
081: new GrantedAuthorityImpl("ROLE_TWO") });
082: MockDecisionManagerImpl token2 = new MockDecisionManagerImpl(
083: "my_password", "Test", "Password",
084: new GrantedAuthority[] {
085: new GrantedAuthorityImpl("ROLE_ONE"),
086: new GrantedAuthorityImpl("ROLE_TWO") });
087: assertEquals(token1, token2);
088:
089: MockDecisionManagerImpl token3 = new MockDecisionManagerImpl(
090: "my_password", "Test", "Password_Changed",
091: new GrantedAuthority[] {
092: new GrantedAuthorityImpl("ROLE_ONE"),
093: new GrantedAuthorityImpl("ROLE_TWO") });
094: assertTrue(!token1.equals(token3));
095:
096: MockDecisionManagerImpl token4 = new MockDecisionManagerImpl(
097: "my_password", "Test_Changed", "Password",
098: new GrantedAuthority[] {
099: new GrantedAuthorityImpl("ROLE_ONE"),
100: new GrantedAuthorityImpl("ROLE_TWO") });
101: assertTrue(!token1.equals(token4));
102:
103: MockDecisionManagerImpl token5 = new MockDecisionManagerImpl(
104: "password_changed", "Test", "Password",
105: new GrantedAuthority[] {
106: new GrantedAuthorityImpl("ROLE_ONE"),
107: new GrantedAuthorityImpl("ROLE_TWO") });
108: assertTrue(!token1.equals(token5));
109:
110: MockDecisionManagerImpl token6 = new MockDecisionManagerImpl(
111: "my_password", "Test", "Password",
112: new GrantedAuthority[] {
113: new GrantedAuthorityImpl("ROLE_ONE"),
114: new GrantedAuthorityImpl("ROLE_TWO_CHANGED") });
115: assertTrue(!token1.equals(token6));
116:
117: MockDecisionManagerImpl token7 = new MockDecisionManagerImpl(
118: "my_password", "Test", "Password",
119: new GrantedAuthority[] { new GrantedAuthorityImpl(
120: "ROLE_ONE") });
121: assertTrue(!token1.equals(token7));
122:
123: assertTrue(!token1.equals(new Integer(100)));
124: }
125:
126: public void testSetAuthenticatedAlwaysReturnsTrue()
127: throws Exception {
128: MockDecisionManagerImpl token = new MockDecisionManagerImpl(
129: "my_password", "Test", "Password",
130: new GrantedAuthority[] {
131: new GrantedAuthorityImpl("ROLE_ONE"),
132: new GrantedAuthorityImpl("ROLE_TWO") });
133: assertTrue(token.isAuthenticated());
134: token.setAuthenticated(false);
135: assertTrue(token.isAuthenticated());
136: }
137:
138: //~ Inner Classes ==================================================================================================
139:
140: private class MockDecisionManagerImpl extends
141: AbstractAdapterAuthenticationToken {
142: private String password;
143: private String username;
144:
145: public MockDecisionManagerImpl(String key, String username,
146: String password, GrantedAuthority[] authorities) {
147: super (key, authorities);
148: this .username = username;
149: this .password = password;
150: }
151:
152: public Object getCredentials() {
153: return this .password;
154: }
155:
156: public Object getPrincipal() {
157: return this.username;
158: }
159: }
160: }
|