1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.x509.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.x509.X509TestUtils;
27
28 import org.acegisecurity.userdetails.User;
29 import org.acegisecurity.userdetails.UserDetails;
30
31 import org.springframework.context.ApplicationContext;
32
33
34
35
36
37
38
39
40 public class EhCacheBasedX509UserCacheTests extends TestCase {
41
42
43 public EhCacheBasedX509UserCacheTests() {
44 }
45
46 public EhCacheBasedX509UserCacheTests(String arg0) {
47 super(arg0);
48 }
49
50
51
52 private Ehcache getCache() {
53 ApplicationContext ctx = MockApplicationContext.getContext();
54
55 return (Ehcache) ctx.getBean("eHCacheBackend");
56 }
57
58 private UserDetails getUser() {
59 return new User("marissa", "password", true, true, true, true,
60 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
61 }
62
63 public final void setUp() throws Exception {
64 super.setUp();
65 }
66
67 public void testCacheOperation() throws Exception {
68 EhCacheBasedX509UserCache cache = new EhCacheBasedX509UserCache();
69 cache.setCache(getCache());
70 cache.afterPropertiesSet();
71
72
73 cache.putUserInCache(X509TestUtils.buildTestCertificate(), getUser());
74 assertEquals(getUser().getPassword(), cache.getUserFromCache(X509TestUtils.buildTestCertificate()).getPassword());
75
76
77 cache.removeUserFromCache(X509TestUtils.buildTestCertificate());
78 assertNull(cache.getUserFromCache(X509TestUtils.buildTestCertificate()));
79
80
81 assertNull(cache.getUserFromCache(null));
82 }
83 }