1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests {@link EhCacheBasedAclEntryCache}.
34   *
35   * @author Ben Alex
36   * @version $Id: EhCacheBasedAclEntryCacheTests.java 1965 2007-08-27 23:41:59Z luke_t $
37   */
38  public class EhCacheBasedAclEntryCacheTests extends TestCase {
39      //~ Static fields/initializers =====================================================================================
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      //~ Constructors ===================================================================================================
48  
49      public EhCacheBasedAclEntryCacheTests() {
50          super();
51      }
52  
53      public EhCacheBasedAclEntryCacheTests(String arg0) {
54          super(arg0);
55      }
56  
57      //~ Methods ========================================================================================================
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          // Check we can get them from cache again
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          // Check after eviction we cannot get them from cache
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 }