1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.cas.populator;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.GrantedAuthorityImpl;
22
23 import org.acegisecurity.userdetails.User;
24 import org.acegisecurity.userdetails.UserDetails;
25 import org.acegisecurity.userdetails.UserDetailsService;
26 import org.acegisecurity.userdetails.UsernameNotFoundException;
27
28 import org.springframework.dao.DataAccessException;
29 import org.springframework.dao.DataRetrievalFailureException;
30
31
32
33
34
35
36
37
38 public class DaoCasAuthoritiesPopulatorTests extends TestCase {
39
40
41 public DaoCasAuthoritiesPopulatorTests() {
42 super();
43 }
44
45 public DaoCasAuthoritiesPopulatorTests(String arg0) {
46 super(arg0);
47 }
48
49
50
51 public static void main(String[] args) {
52 junit.textui.TestRunner.run(DaoCasAuthoritiesPopulatorTests.class);
53 }
54
55 public final void setUp() throws Exception {
56 super.setUp();
57 }
58
59 public void testDetectsMissingAuthenticationDao() throws Exception {
60 DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
61
62 try {
63 populator.afterPropertiesSet();
64 fail("Should have thrown IllegalArgumentException");
65 } catch (IllegalArgumentException expected) {
66 assertEquals("A UserDetailsService must be set", expected.getMessage());
67 }
68 }
69
70 public void testGetGrantedAuthoritiesForInvalidUsername()
71 throws Exception {
72 DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
73 populator.setUserDetailsService(new MockAuthenticationDaoUserMarissa());
74 populator.afterPropertiesSet();
75
76 try {
77 populator.getUserDetails("scott");
78 fail("Should have thrown UsernameNotFoundException");
79 } catch (UsernameNotFoundException expected) {
80 assertTrue(true);
81 }
82 }
83
84 public void testGetGrantedAuthoritiesForValidUsername()
85 throws Exception {
86 DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
87 populator.setUserDetailsService(new MockAuthenticationDaoUserMarissa());
88 populator.afterPropertiesSet();
89
90 UserDetails results = populator.getUserDetails("marissa");
91 assertEquals(2, results.getAuthorities().length);
92 assertEquals(new GrantedAuthorityImpl("ROLE_ONE"), results.getAuthorities()[0]);
93 assertEquals(new GrantedAuthorityImpl("ROLE_TWO"), results.getAuthorities()[1]);
94 }
95
96 public void testGetGrantedAuthoritiesWhenDaoThrowsException()
97 throws Exception {
98 DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
99 populator.setUserDetailsService(new MockAuthenticationDaoSimulateBackendError());
100 populator.afterPropertiesSet();
101
102 try {
103 populator.getUserDetails("THE_DAO_WILL_FAIL");
104 fail("Should have thrown DataRetrievalFailureException");
105 } catch (DataRetrievalFailureException expected) {
106 assertTrue(true);
107 }
108 }
109
110 public void testGettersSetters() {
111 DaoCasAuthoritiesPopulator populator = new DaoCasAuthoritiesPopulator();
112 UserDetailsService dao = new MockAuthenticationDaoUserMarissa();
113 populator.setUserDetailsService(dao);
114 assertEquals(dao, populator.getUserDetailsService());
115 }
116
117
118
119 private class MockAuthenticationDaoSimulateBackendError implements UserDetailsService {
120 public long getRefreshDuration() {
121 return 0;
122 }
123
124 public UserDetails loadUserByUsername(String username)
125 throws UsernameNotFoundException, DataAccessException {
126 throw new DataRetrievalFailureException("This mock simulator is designed to fail");
127 }
128 }
129
130 private class MockAuthenticationDaoUserMarissa implements UserDetailsService {
131 public long getRefreshDuration() {
132 return 0;
133 }
134
135 public UserDetails loadUserByUsername(String username)
136 throws UsernameNotFoundException, DataAccessException {
137 if ("marissa".equals(username)) {
138 return new User("marissa", "koala", true, true, true, true,
139 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
140 } else {
141 throw new UsernameNotFoundException("Could not find: " + username);
142 }
143 }
144 }
145 }