01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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.Cache;
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: * Tests for {@link EhCacheBasedX509UserCache}.
35: *
36: * @author Luke Taylor
37: * @version $Id: EhCacheBasedX509UserCacheTests.java 1496 2006-05-23 13:38:33Z benalex $
38: */
39: public class EhCacheBasedX509UserCacheTests extends TestCase {
40: //~ Constructors ===================================================================================================
41:
42: public EhCacheBasedX509UserCacheTests() {
43: super ();
44: }
45:
46: public EhCacheBasedX509UserCacheTests(String arg0) {
47: super (arg0);
48: }
49:
50: //~ Methods ========================================================================================================
51:
52: private Cache getCache() {
53: ApplicationContext ctx = MockApplicationContext.getContext();
54:
55: return (Cache) ctx.getBean("eHCacheBackend");
56: }
57:
58: private UserDetails getUser() {
59: return new User("marissa", "password", true, true, true, true,
60: new GrantedAuthority[] {
61: new GrantedAuthorityImpl("ROLE_ONE"),
62: new GrantedAuthorityImpl("ROLE_TWO") });
63: }
64:
65: public final void setUp() throws Exception {
66: super .setUp();
67: }
68:
69: public void testCacheOperation() throws Exception {
70: EhCacheBasedX509UserCache cache = new EhCacheBasedX509UserCache();
71: cache.setCache(getCache());
72: cache.afterPropertiesSet();
73:
74: // Check it gets stored in the cache
75: cache.putUserInCache(X509TestUtils.buildTestCertificate(),
76: getUser());
77: assertEquals(getUser().getPassword(), cache.getUserFromCache(
78: X509TestUtils.buildTestCertificate()).getPassword());
79:
80: // Check it gets removed from the cache
81: cache.removeUserFromCache(X509TestUtils.buildTestCertificate());
82: assertNull(cache.getUserFromCache(X509TestUtils
83: .buildTestCertificate()));
84:
85: // Check it doesn't return values for null user
86: assertNull(cache.getUserFromCache(null));
87: }
88: }
|