1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.cas.cache;
17
18 import junit.framework.TestCase;
19
20 import net.sf.ehcache.Ehcache;
21
22 import org.acegisecurity.GrantedAuthority;
23 import org.acegisecurity.GrantedAuthorityImpl;
24 import org.acegisecurity.MockApplicationContext;
25
26 import org.acegisecurity.providers.cas.CasAuthenticationToken;
27
28 import org.acegisecurity.userdetails.User;
29
30 import org.springframework.context.ApplicationContext;
31
32 import java.util.List;
33 import java.util.Vector;
34
35
36
37
38
39
40
41
42 public class EhCacheBasedTicketCacheTests extends TestCase {
43
44
45 public EhCacheBasedTicketCacheTests() {
46 }
47
48 public EhCacheBasedTicketCacheTests(String arg0) {
49 super(arg0);
50 }
51
52
53
54 private Ehcache getCache() {
55 ApplicationContext ctx = MockApplicationContext.getContext();
56
57 return (Ehcache) ctx.getBean("eHCacheBackend");
58 }
59
60 private CasAuthenticationToken getToken() {
61 List proxyList = new Vector();
62 proxyList.add("https://localhost/newPortal/j_acegi_cas_security_check");
63
64 User user = new User("marissa", "password", true, true, true, true,
65 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
66
67 return new CasAuthenticationToken("key", user, "ST-0-ER94xMJmn6pha35CQRoZ",
68 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")}, user,
69 proxyList, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
70 }
71
72 public final void setUp() throws Exception {
73 super.setUp();
74 }
75
76 public void testCacheOperation() throws Exception {
77 EhCacheBasedTicketCache cache = new EhCacheBasedTicketCache();
78 cache.setCache(getCache());
79 cache.afterPropertiesSet();
80
81
82 cache.putTicketInCache(getToken());
83 assertEquals(getToken(), cache.getByTicketId("ST-0-ER94xMJmn6pha35CQRoZ"));
84
85
86 cache.removeTicketFromCache(getToken());
87 assertNull(cache.getByTicketId("ST-0-ER94xMJmn6pha35CQRoZ"));
88
89
90 assertNull(cache.getByTicketId(null));
91 assertNull(cache.getByTicketId("UNKNOWN_SERVICE_TICKET"));
92 }
93
94 public void testStartupDetectsMissingCache() throws Exception {
95 EhCacheBasedTicketCache cache = new EhCacheBasedTicketCache();
96
97 try {
98 cache.afterPropertiesSet();
99 fail("Should have thrown IllegalArgumentException");
100 } catch (IllegalArgumentException expected) {
101 assertTrue(true);
102 }
103
104 Ehcache myCache = getCache();
105 cache.setCache(myCache);
106 assertEquals(myCache, cache.getCache());
107 }
108 }