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.cas.populator;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: import org.acegisecurity.userdetails.User;
024: import org.acegisecurity.userdetails.UserDetails;
025: import org.acegisecurity.userdetails.UserDetailsService;
026: import org.acegisecurity.userdetails.UsernameNotFoundException;
027:
028: import org.springframework.dao.DataAccessException;
029: import org.springframework.dao.DataRetrievalFailureException;
030:
031: /**
032: * Tests {@link DaoCasAuthoritiesPopulator}.
033: *
034: * @author Ben Alex
035: * @version $Id: DaoCasAuthoritiesPopulatorTests.java 1829 2007-05-17 12:49:58Z vishalpuri $
036: */
037: public class DaoCasAuthoritiesPopulatorTests extends TestCase {
038: //~ Constructors ===================================================================================================
039:
040: public DaoCasAuthoritiesPopulatorTests() {
041: super ();
042: }
043:
044: public DaoCasAuthoritiesPopulatorTests(String arg0) {
045: super (arg0);
046: }
047:
048: //~ Methods ========================================================================================================
049:
050: public static void main(String[] args) {
051: junit.textui.TestRunner
052: .run(DaoCasAuthoritiesPopulatorTests.class);
053: }
054:
055: public final void setUp() throws Exception {
056: super .setUp();
057: }
058:
059: public void testDetectsMissingAuthenticationDao() throws Exception {
060: DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
061:
062: try {
063: populator.afterPropertiesSet();
064: fail("Should have thrown IllegalArgumentException");
065: } catch (IllegalArgumentException expected) {
066: assertEquals("A UserDetailsService must be set", expected
067: .getMessage());
068: }
069: }
070:
071: public void testGetGrantedAuthoritiesForInvalidUsername()
072: throws Exception {
073: DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
074: populator
075: .setUserDetailsService(new MockAuthenticationDaoUserMarissa());
076: populator.afterPropertiesSet();
077:
078: try {
079: populator.getUserDetails("scott");
080: fail("Should have thrown UsernameNotFoundException");
081: } catch (UsernameNotFoundException expected) {
082: assertTrue(true);
083: }
084: }
085:
086: public void testGetGrantedAuthoritiesForValidUsername()
087: throws Exception {
088: DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
089: populator
090: .setUserDetailsService(new MockAuthenticationDaoUserMarissa());
091: populator.afterPropertiesSet();
092:
093: UserDetails results = populator.getUserDetails("marissa");
094: assertEquals(2, results.getAuthorities().length);
095: assertEquals(new GrantedAuthorityImpl("ROLE_ONE"), results
096: .getAuthorities()[0]);
097: assertEquals(new GrantedAuthorityImpl("ROLE_TWO"), results
098: .getAuthorities()[1]);
099: }
100:
101: public void testGetGrantedAuthoritiesWhenDaoThrowsException()
102: throws Exception {
103: DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
104: populator
105: .setUserDetailsService(new MockAuthenticationDaoSimulateBackendError());
106: populator.afterPropertiesSet();
107:
108: try {
109: populator.getUserDetails("THE_DAO_WILL_FAIL");
110: fail("Should have thrown DataRetrievalFailureException");
111: } catch (DataRetrievalFailureException expected) {
112: assertTrue(true);
113: }
114: }
115:
116: public void testGettersSetters() {
117: DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
118: UserDetailsService dao = new MockAuthenticationDaoUserMarissa();
119: populator.setUserDetailsService(dao);
120: assertEquals(dao, populator.getUserDetailsService());
121: }
122:
123: //~ Inner Classes ==================================================================================================
124:
125: private class MockAuthenticationDaoSimulateBackendError implements
126: UserDetailsService {
127: public long getRefreshDuration() {
128: return 0;
129: }
130:
131: public UserDetails loadUserByUsername(String username)
132: throws UsernameNotFoundException, DataAccessException {
133: throw new DataRetrievalFailureException(
134: "This mock simulator is designed to fail");
135: }
136: }
137:
138: private class MockAuthenticationDaoUserMarissa implements
139: UserDetailsService {
140: public long getRefreshDuration() {
141: return 0;
142: }
143:
144: public UserDetails loadUserByUsername(String username)
145: throws UsernameNotFoundException, DataAccessException {
146: if ("marissa".equals(username)) {
147: return new User("marissa", "koala", true, true, true,
148: true, new GrantedAuthority[] {
149: new GrantedAuthorityImpl("ROLE_ONE"),
150: new GrantedAuthorityImpl("ROLE_TWO") });
151: } else {
152: throw new UsernameNotFoundException("Could not find: "
153: + username);
154: }
155: }
156: }
157: }
|