001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.acl.basic.cache;
017:
018: import junit.framework.TestCase;
019:
020: import net.sf.ehcache.Cache;
021:
022: import org.acegisecurity.MockApplicationContext;
023:
024: import org.acegisecurity.acl.basic.AclObjectIdentity;
025: import org.acegisecurity.acl.basic.BasicAclEntry;
026: import org.acegisecurity.acl.basic.NamedEntityObjectIdentity;
027: import org.acegisecurity.acl.basic.SimpleAclEntry;
028:
029: import org.springframework.context.ApplicationContext;
030:
031: /**
032: * Tests {@link EhCacheBasedAclEntryCache}.
033: *
034: * @author Ben Alex
035: * @version $Id: EhCacheBasedAclEntryCacheTests.java 1496 2006-05-23 13:38:33Z benalex $
036: */
037: public class EhCacheBasedAclEntryCacheTests extends TestCase {
038: //~ Static fields/initializers =====================================================================================
039:
040: private static final AclObjectIdentity OBJECT_100 = new NamedEntityObjectIdentity(
041: "OBJECT", "100");
042: private static final AclObjectIdentity OBJECT_200 = new NamedEntityObjectIdentity(
043: "OBJECT", "200");
044: private static final BasicAclEntry OBJECT_100_MARISSA = new SimpleAclEntry(
045: "marissa", OBJECT_100, null, 2);
046: private static final BasicAclEntry OBJECT_100_SCOTT = new SimpleAclEntry(
047: "scott", OBJECT_100, null, 4);
048: private static final BasicAclEntry OBJECT_200_PETER = new SimpleAclEntry(
049: "peter", OBJECT_200, null, 4);
050:
051: //~ Constructors ===================================================================================================
052:
053: public EhCacheBasedAclEntryCacheTests() {
054: super ();
055: }
056:
057: public EhCacheBasedAclEntryCacheTests(String arg0) {
058: super (arg0);
059: }
060:
061: //~ Methods ========================================================================================================
062:
063: private Cache getCache() {
064: ApplicationContext ctx = MockApplicationContext.getContext();
065:
066: return (Cache) ctx.getBean("eHCacheBackend");
067: }
068:
069: public static void main(String[] args) {
070: junit.textui.TestRunner
071: .run(EhCacheBasedAclEntryCacheTests.class);
072: }
073:
074: public final void setUp() throws Exception {
075: super .setUp();
076: }
077:
078: public void testCacheOperation() throws Exception {
079: EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache();
080: cache.setCache(getCache());
081: cache.afterPropertiesSet();
082:
083: cache.putEntriesInCache(new BasicAclEntry[] { OBJECT_100_SCOTT,
084: OBJECT_100_MARISSA });
085: cache
086: .putEntriesInCache(new BasicAclEntry[] { OBJECT_200_PETER });
087:
088: // Check we can get them from cache again
089: assertEquals(OBJECT_100_SCOTT, cache
090: .getEntriesFromCache(new NamedEntityObjectIdentity(
091: "OBJECT", "100"))[0]);
092: assertEquals(OBJECT_100_MARISSA, cache
093: .getEntriesFromCache(new NamedEntityObjectIdentity(
094: "OBJECT", "100"))[1]);
095: assertEquals(OBJECT_200_PETER, cache
096: .getEntriesFromCache(new NamedEntityObjectIdentity(
097: "OBJECT", "200"))[0]);
098: assertNull(cache
099: .getEntriesFromCache(new NamedEntityObjectIdentity(
100: "OBJECT", "NOT_IN_CACHE")));
101:
102: // Check after eviction we cannot get them from cache
103: cache.removeEntriesFromCache(new NamedEntityObjectIdentity(
104: "OBJECT", "100"));
105: assertNull(cache
106: .getEntriesFromCache(new NamedEntityObjectIdentity(
107: "OBJECT", "100")));
108: }
109:
110: public void testStartupDetectsMissingCache() throws Exception {
111: EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache();
112:
113: try {
114: cache.afterPropertiesSet();
115: fail("Should have thrown IllegalArgumentException");
116: } catch (IllegalArgumentException expected) {
117: assertTrue(true);
118: }
119:
120: Cache myCache = getCache();
121: cache.setCache(myCache);
122: assertEquals(myCache, cache.getCache());
123: }
124: }
|