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 org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.GrantedAuthorityImpl;
22 import org.acegisecurity.MockApplicationContext;
23 import org.acegisecurity.userdetails.User;
24
25 import net.sf.ehcache.Cache;
26
27 import org.springframework.context.ApplicationContext;
28
29
30 /***
31 * Tests {@link EhCacheBasedUserCache}.
32 *
33 * @author Ben Alex
34 * @version $Id: EhCacheBasedUserCacheTests.java,v 1.6 2005/11/29 13:10:09 benalex Exp $
35 */
36 public class EhCacheBasedUserCacheTests extends TestCase {
37
38
39 public EhCacheBasedUserCacheTests() {
40 super();
41 }
42
43 public EhCacheBasedUserCacheTests(String arg0) {
44 super(arg0);
45 }
46
47
48
49 public final void setUp() throws Exception {
50 super.setUp();
51 }
52
53 public static void main(String[] args) {
54 junit.textui.TestRunner.run(EhCacheBasedUserCacheTests.class);
55 }
56
57 public void testCacheOperation() throws Exception {
58 EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
59 cache.setCache(getCache());
60 cache.afterPropertiesSet();
61
62
63 cache.putUserInCache(getUser());
64 assertEquals(getUser().getPassword(),
65 cache.getUserFromCache(getUser().getUsername()).getPassword());
66
67
68 cache.removeUserFromCache(getUser());
69 assertNull(cache.getUserFromCache(getUser().getUsername()));
70
71
72 assertNull(cache.getUserFromCache(null));
73 assertNull(cache.getUserFromCache("UNKNOWN_USER"));
74 }
75
76 public void testStartupDetectsMissingCache() throws Exception {
77 EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
78
79 try {
80 cache.afterPropertiesSet();
81 fail("Should have thrown IllegalArgumentException");
82 } catch (IllegalArgumentException expected) {
83 assertTrue(true);
84 }
85
86 Cache myCache = getCache();
87 cache.setCache(myCache);
88 assertEquals(myCache, cache.getCache());
89 }
90
91 private Cache getCache() {
92 ApplicationContext ctx = MockApplicationContext.getContext();
93
94 return (Cache) ctx.getBean("eHCacheBackend");
95 }
96
97 private User getUser() {
98 return new User("john", "password", true, true, true, true,
99 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
100 "ROLE_TWO")});
101 }
102 }