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.dao.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.userdetails.User;
27  
28  import org.springframework.context.ApplicationContext;
29  
30  
31  /**
32   * Tests {@link EhCacheBasedUserCache}.
33   *
34   * @author Ben Alex
35   * @version $Id: EhCacheBasedUserCacheTests.java 1965 2007-08-27 23:41:59Z luke_t $
36   */
37  public class EhCacheBasedUserCacheTests extends TestCase {
38      //~ Constructors ===================================================================================================
39  
40      public EhCacheBasedUserCacheTests() {
41      }
42  
43      public EhCacheBasedUserCacheTests(String arg0) {
44          super(arg0);
45      }
46  
47      //~ Methods ========================================================================================================
48  
49      private Ehcache getCache() {
50          ApplicationContext ctx = MockApplicationContext.getContext();
51  
52          return (Ehcache) ctx.getBean("eHCacheBackend");
53      }
54  
55      private User getUser() {
56          return new User("john", "password", true, true, true, true,
57              new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
58      }
59  
60      public final void setUp() throws Exception {
61          super.setUp();
62      }
63  
64      public void testCacheOperation() throws Exception {
65          EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
66          cache.setCache(getCache());
67          cache.afterPropertiesSet();
68  
69          // Check it gets stored in the cache
70          cache.putUserInCache(getUser());
71          assertEquals(getUser().getPassword(), cache.getUserFromCache(getUser().getUsername()).getPassword());
72  
73          // Check it gets removed from the cache
74          cache.removeUserFromCache(getUser());
75          assertNull(cache.getUserFromCache(getUser().getUsername()));
76  
77          // Check it doesn't return values for null or unknown users
78          assertNull(cache.getUserFromCache(null));
79          assertNull(cache.getUserFromCache("UNKNOWN_USER"));
80      }
81  
82      public void testStartupDetectsMissingCache() throws Exception {
83          EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
84  
85          try {
86              cache.afterPropertiesSet();
87              fail("Should have thrown IllegalArgumentException");
88          } catch (IllegalArgumentException expected) {
89              assertTrue(true);
90          }
91  
92          Ehcache myCache = getCache();
93          cache.setCache(myCache);
94          assertEquals(myCache, cache.getCache());
95      }
96  }