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.cas.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.providers.cas.CasAuthenticationToken;
027:
028: import org.acegisecurity.userdetails.User;
029:
030: import org.springframework.context.ApplicationContext;
031:
032: import java.util.List;
033: import java.util.Vector;
034:
035: /**
036: * Tests {@link EhCacheBasedTicketCache}.
037: *
038: * @author Ben Alex
039: * @version $Id: EhCacheBasedTicketCacheTests.java 1496 2006-05-23 13:38:33Z benalex $
040: */
041: public class EhCacheBasedTicketCacheTests extends TestCase {
042: //~ Constructors ===================================================================================================
043:
044: public EhCacheBasedTicketCacheTests() {
045: super ();
046: }
047:
048: public EhCacheBasedTicketCacheTests(String arg0) {
049: super (arg0);
050: }
051:
052: //~ Methods ========================================================================================================
053:
054: private Cache getCache() {
055: ApplicationContext ctx = MockApplicationContext.getContext();
056:
057: return (Cache) ctx.getBean("eHCacheBackend");
058: }
059:
060: private CasAuthenticationToken getToken() {
061: List proxyList = new Vector();
062: proxyList
063: .add("https://localhost/newPortal/j_acegi_cas_security_check");
064:
065: User user = new User("marissa", "password", true, true, true,
066: true, new GrantedAuthority[] {
067: new GrantedAuthorityImpl("ROLE_ONE"),
068: new GrantedAuthorityImpl("ROLE_TWO") });
069:
070: return new CasAuthenticationToken("key", user,
071: "ST-0-ER94xMJmn6pha35CQRoZ", new GrantedAuthority[] {
072: new GrantedAuthorityImpl("ROLE_ONE"),
073: new GrantedAuthorityImpl("ROLE_TWO") }, user,
074: proxyList,
075: "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
076: }
077:
078: public static void main(String[] args) {
079: junit.textui.TestRunner.run(EhCacheBasedTicketCacheTests.class);
080: }
081:
082: public final void setUp() throws Exception {
083: super .setUp();
084: }
085:
086: public void testCacheOperation() throws Exception {
087: EhCacheBasedTicketCache cache = new EhCacheBasedTicketCache();
088: cache.setCache(getCache());
089: cache.afterPropertiesSet();
090:
091: // Check it gets stored in the cache
092: cache.putTicketInCache(getToken());
093: assertEquals(getToken(), cache
094: .getByTicketId("ST-0-ER94xMJmn6pha35CQRoZ"));
095:
096: // Check it gets removed from the cache
097: cache.removeTicketFromCache(getToken());
098: assertNull(cache.getByTicketId("ST-0-ER94xMJmn6pha35CQRoZ"));
099:
100: // Check it doesn't return values for null or unknown service tickets
101: assertNull(cache.getByTicketId(null));
102: assertNull(cache.getByTicketId("UNKNOWN_SERVICE_TICKET"));
103: }
104:
105: public void testStartupDetectsMissingCache() throws Exception {
106: EhCacheBasedTicketCache cache = new EhCacheBasedTicketCache();
107:
108: try {
109: cache.afterPropertiesSet();
110: fail("Should have thrown IllegalArgumentException");
111: } catch (IllegalArgumentException expected) {
112: assertTrue(true);
113: }
114:
115: Cache myCache = getCache();
116: cache.setCache(myCache);
117: assertEquals(myCache, cache.getCache());
118: }
119: }
|