1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.acl.basic.cache;
17
18 import junit.framework.TestCase;
19
20 import net.sf.ehcache.Ehcache;
21
22 import org.acegisecurity.MockApplicationContext;
23
24 import org.acegisecurity.acl.basic.AclObjectIdentity;
25 import org.acegisecurity.acl.basic.BasicAclEntry;
26 import org.acegisecurity.acl.basic.NamedEntityObjectIdentity;
27 import org.acegisecurity.acl.basic.SimpleAclEntry;
28
29 import org.springframework.context.ApplicationContext;
30
31
32
33
34
35
36
37
38 public class EhCacheBasedAclEntryCacheTests extends TestCase {
39
40
41 private static final AclObjectIdentity OBJECT_100 = new NamedEntityObjectIdentity("OBJECT", "100");
42 private static final AclObjectIdentity OBJECT_200 = new NamedEntityObjectIdentity("OBJECT", "200");
43 private static final BasicAclEntry OBJECT_100_MARISSA = new SimpleAclEntry("marissa", OBJECT_100, null, 2);
44 private static final BasicAclEntry OBJECT_100_SCOTT = new SimpleAclEntry("scott", OBJECT_100, null, 4);
45 private static final BasicAclEntry OBJECT_200_PETER = new SimpleAclEntry("peter", OBJECT_200, null, 4);
46
47
48
49 public EhCacheBasedAclEntryCacheTests() {
50 super();
51 }
52
53 public EhCacheBasedAclEntryCacheTests(String arg0) {
54 super(arg0);
55 }
56
57
58
59 private Ehcache getCache() {
60 ApplicationContext ctx = MockApplicationContext.getContext();
61
62 return (Ehcache) ctx.getBean("eHCacheBackend");
63 }
64
65 public final void setUp() throws Exception {
66 super.setUp();
67 }
68
69 public void testCacheOperation() throws Exception {
70 EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache();
71 cache.setCache(getCache());
72 cache.afterPropertiesSet();
73
74 cache.putEntriesInCache(new BasicAclEntry[] {OBJECT_100_SCOTT, OBJECT_100_MARISSA});
75 cache.putEntriesInCache(new BasicAclEntry[] {OBJECT_200_PETER});
76
77
78 assertEquals(OBJECT_100_SCOTT, cache.getEntriesFromCache(new NamedEntityObjectIdentity("OBJECT", "100"))[0]);
79 assertEquals(OBJECT_100_MARISSA, cache.getEntriesFromCache(new NamedEntityObjectIdentity("OBJECT", "100"))[1]);
80 assertEquals(OBJECT_200_PETER, cache.getEntriesFromCache(new NamedEntityObjectIdentity("OBJECT", "200"))[0]);
81 assertNull(cache.getEntriesFromCache(new NamedEntityObjectIdentity("OBJECT", "NOT_IN_CACHE")));
82
83
84 cache.removeEntriesFromCache(new NamedEntityObjectIdentity("OBJECT", "100"));
85 assertNull(cache.getEntriesFromCache(new NamedEntityObjectIdentity("OBJECT", "100")));
86 }
87
88 public void testStartupDetectsMissingCache() throws Exception {
89 EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache();
90
91 try {
92 cache.afterPropertiesSet();
93 fail("Should have thrown IllegalArgumentException");
94 } catch (IllegalArgumentException expected) {
95 assertTrue(true);
96 }
97
98 Ehcache myCache = getCache();
99 cache.setCache(myCache);
100 assertEquals(myCache, cache.getCache());
101 }
102 }