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.x509.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.x509.X509TestUtils;
24  import org.acegisecurity.userdetails.User;
25  import org.acegisecurity.userdetails.UserDetails;
26  
27  import net.sf.ehcache.Cache;
28  
29  import org.springframework.context.ApplicationContext;
30  
31  
32  /***
33   * DOCUMENT ME!
34   *
35   * @author Luke Taylor
36   * @version $Id: EhCacheBasedX509UserCacheTests.java,v 1.4 2005/11/29 13:10:14 benalex Exp $
37   */
38  public class EhCacheBasedX509UserCacheTests extends TestCase {
39      //~ Constructors ===========================================================
40  
41      public EhCacheBasedX509UserCacheTests() {
42          super();
43      }
44  
45      public EhCacheBasedX509UserCacheTests(String arg0) {
46          super(arg0);
47      }
48  
49      //~ Methods ================================================================
50  
51      public final void setUp() throws Exception {
52          super.setUp();
53      }
54  
55      public void testCacheOperation() throws Exception {
56          EhCacheBasedX509UserCache cache = new EhCacheBasedX509UserCache();
57          cache.setCache(getCache());
58          cache.afterPropertiesSet();
59  
60          // Check it gets stored in the cache
61          cache.putUserInCache(X509TestUtils.buildTestCertificate(), getUser());
62          assertEquals(getUser().getPassword(),
63              cache.getUserFromCache(X509TestUtils.buildTestCertificate())
64                   .getPassword());
65  
66          // Check it gets removed from the cache
67          cache.removeUserFromCache(X509TestUtils.buildTestCertificate());
68          assertNull(cache.getUserFromCache(X509TestUtils.buildTestCertificate()));
69  
70          // Check it doesn't return values for null user
71          assertNull(cache.getUserFromCache(null));
72      }
73  
74      private Cache getCache() {
75          ApplicationContext ctx = MockApplicationContext.getContext();
76  
77          return (Cache) ctx.getBean("eHCacheBackend");
78      }
79  
80      private UserDetails getUser() {
81          return new User("marissa", "password", true, true, true, true,
82              new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
83                      "ROLE_TWO")});
84      }
85  }