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