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