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.providers.dao.cache;
017:
018: import junit.framework.TestCase;
019:
020: import net.sf.ehcache.Cache;
021:
022: import org.acegisecurity.GrantedAuthority;
023: import org.acegisecurity.GrantedAuthorityImpl;
024: import org.acegisecurity.MockApplicationContext;
025:
026: import org.acegisecurity.userdetails.User;
027:
028: import org.springframework.context.ApplicationContext;
029:
030: /**
031: * Tests {@link EhCacheBasedUserCache}.
032: *
033: * @author Ben Alex
034: * @version $Id: EhCacheBasedUserCacheTests.java 1496 2006-05-23 13:38:33Z benalex $
035: */
036: public class EhCacheBasedUserCacheTests extends TestCase {
037: //~ Constructors ===================================================================================================
038:
039: public EhCacheBasedUserCacheTests() {
040: super ();
041: }
042:
043: public EhCacheBasedUserCacheTests(String arg0) {
044: super (arg0);
045: }
046:
047: //~ Methods ========================================================================================================
048:
049: private Cache getCache() {
050: ApplicationContext ctx = MockApplicationContext.getContext();
051:
052: return (Cache) ctx.getBean("eHCacheBackend");
053: }
054:
055: private User getUser() {
056: return new User("john", "password", true, true, true, true,
057: new GrantedAuthority[] {
058: new GrantedAuthorityImpl("ROLE_ONE"),
059: new GrantedAuthorityImpl("ROLE_TWO") });
060: }
061:
062: public static void main(String[] args) {
063: junit.textui.TestRunner.run(EhCacheBasedUserCacheTests.class);
064: }
065:
066: public final void setUp() throws Exception {
067: super .setUp();
068: }
069:
070: public void testCacheOperation() throws Exception {
071: EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
072: cache.setCache(getCache());
073: cache.afterPropertiesSet();
074:
075: // Check it gets stored in the cache
076: cache.putUserInCache(getUser());
077: assertEquals(getUser().getPassword(), cache.getUserFromCache(
078: getUser().getUsername()).getPassword());
079:
080: // Check it gets removed from the cache
081: cache.removeUserFromCache(getUser());
082: assertNull(cache.getUserFromCache(getUser().getUsername()));
083:
084: // Check it doesn't return values for null or unknown users
085: assertNull(cache.getUserFromCache(null));
086: assertNull(cache.getUserFromCache("UNKNOWN_USER"));
087: }
088:
089: public void testStartupDetectsMissingCache() throws Exception {
090: EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
091:
092: try {
093: cache.afterPropertiesSet();
094: fail("Should have thrown IllegalArgumentException");
095: } catch (IllegalArgumentException expected) {
096: assertTrue(true);
097: }
098:
099: Cache myCache = getCache();
100: cache.setCache(myCache);
101: assertEquals(myCache, cache.getCache());
102: }
103: }
|