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.providers.cas.cache;
17  
18  import junit.framework.TestCase;
19  
20  import net.sf.ehcache.Ehcache;
21  
22  import org.acegisecurity.GrantedAuthority;
23  import org.acegisecurity.GrantedAuthorityImpl;
24  import org.acegisecurity.MockApplicationContext;
25  
26  import org.acegisecurity.providers.cas.CasAuthenticationToken;
27  
28  import org.acegisecurity.userdetails.User;
29  
30  import org.springframework.context.ApplicationContext;
31  
32  import java.util.List;
33  import java.util.Vector;
34  
35  
36  /**
37   * Tests {@link EhCacheBasedTicketCache}.
38   *
39   * @author Ben Alex
40   * @version $Id: EhCacheBasedTicketCacheTests.java 1965 2007-08-27 23:41:59Z luke_t $
41   */
42  public class EhCacheBasedTicketCacheTests extends TestCase {
43      //~ Constructors ===================================================================================================
44  
45      public EhCacheBasedTicketCacheTests() {
46      }
47  
48      public EhCacheBasedTicketCacheTests(String arg0) {
49          super(arg0);
50      }
51  
52      //~ Methods ========================================================================================================
53  
54      private Ehcache getCache() {
55          ApplicationContext ctx = MockApplicationContext.getContext();
56  
57          return (Ehcache) ctx.getBean("eHCacheBackend");
58      }
59  
60      private CasAuthenticationToken getToken() {
61          List proxyList = new Vector();
62          proxyList.add("https://localhost/newPortal/j_acegi_cas_security_check");
63  
64          User user = new User("marissa", "password", true, true, true, true,
65                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
66  
67          return new CasAuthenticationToken("key", user, "ST-0-ER94xMJmn6pha35CQRoZ",
68              new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")}, user,
69              proxyList, "PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt");
70      }
71  
72      public final void setUp() throws Exception {
73          super.setUp();
74      }
75  
76      public void testCacheOperation() throws Exception {
77          EhCacheBasedTicketCache cache = new EhCacheBasedTicketCache();
78          cache.setCache(getCache());
79          cache.afterPropertiesSet();
80  
81          // Check it gets stored in the cache
82          cache.putTicketInCache(getToken());
83          assertEquals(getToken(), cache.getByTicketId("ST-0-ER94xMJmn6pha35CQRoZ"));
84  
85          // Check it gets removed from the cache
86          cache.removeTicketFromCache(getToken());
87          assertNull(cache.getByTicketId("ST-0-ER94xMJmn6pha35CQRoZ"));
88  
89          // Check it doesn't return values for null or unknown service tickets
90          assertNull(cache.getByTicketId(null));
91          assertNull(cache.getByTicketId("UNKNOWN_SERVICE_TICKET"));
92      }
93  
94      public void testStartupDetectsMissingCache() throws Exception {
95          EhCacheBasedTicketCache cache = new EhCacheBasedTicketCache();
96  
97          try {
98              cache.afterPropertiesSet();
99              fail("Should have thrown IllegalArgumentException");
100         } catch (IllegalArgumentException expected) {
101             assertTrue(true);
102         }
103 
104         Ehcache myCache = getCache();
105         cache.setCache(myCache);
106         assertEquals(myCache, cache.getCache());
107     }
108 }