1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.dao.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.userdetails.User;
27
28 import org.springframework.context.ApplicationContext;
29
30
31
32
33
34
35
36
37 public class EhCacheBasedUserCacheTests extends TestCase {
38
39
40 public EhCacheBasedUserCacheTests() {
41 }
42
43 public EhCacheBasedUserCacheTests(String arg0) {
44 super(arg0);
45 }
46
47
48
49 private Ehcache getCache() {
50 ApplicationContext ctx = MockApplicationContext.getContext();
51
52 return (Ehcache) ctx.getBean("eHCacheBackend");
53 }
54
55 private User getUser() {
56 return new User("john", "password", true, true, true, true,
57 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
58 }
59
60 public final void setUp() throws Exception {
61 super.setUp();
62 }
63
64 public void testCacheOperation() throws Exception {
65 EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
66 cache.setCache(getCache());
67 cache.afterPropertiesSet();
68
69
70 cache.putUserInCache(getUser());
71 assertEquals(getUser().getPassword(), cache.getUserFromCache(getUser().getUsername()).getPassword());
72
73
74 cache.removeUserFromCache(getUser());
75 assertNull(cache.getUserFromCache(getUser().getUsername()));
76
77
78 assertNull(cache.getUserFromCache(null));
79 assertNull(cache.getUserFromCache("UNKNOWN_USER"));
80 }
81
82 public void testStartupDetectsMissingCache() throws Exception {
83 EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
84
85 try {
86 cache.afterPropertiesSet();
87 fail("Should have thrown IllegalArgumentException");
88 } catch (IllegalArgumentException expected) {
89 assertTrue(true);
90 }
91
92 Ehcache myCache = getCache();
93 cache.setCache(myCache);
94 assertEquals(myCache, cache.getCache());
95 }
96 }