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