From ee3287430825930f7f2269a3f4ba1f98820b46b8 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Sun, 17 Apr 2005 22:18:01 +0000 Subject: [PATCH] Added X509 EhCache tests and fixed glaring bug in X509 EhCache implementation. --- .../x509/cache/EhCacheBasedX509UserCache.java | 10 +-- .../cache/EhCacheBasedX509UserCacheTests.java | 82 +++++++++++++++++++ 2 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/org/acegisecurity/providers/x509/cache/EhCacheBasedX509UserCacheTests.java diff --git a/core/src/main/java/org/acegisecurity/providers/x509/cache/EhCacheBasedX509UserCache.java b/core/src/main/java/org/acegisecurity/providers/x509/cache/EhCacheBasedX509UserCache.java index 15fdb20461..d2b711d0ca 100644 --- a/core/src/main/java/org/acegisecurity/providers/x509/cache/EhCacheBasedX509UserCache.java +++ b/core/src/main/java/org/acegisecurity/providers/x509/cache/EhCacheBasedX509UserCache.java @@ -55,6 +55,10 @@ public class EhCacheBasedX509UserCache implements X509UserCache, InitializingBea this.cache = cache; } + public void afterPropertiesSet() throws Exception { + Assert.notNull(cache, "cache is mandatory"); + } + public UserDetails getUserFromCache(X509Certificate userCert) { Element element = null; @@ -77,10 +81,6 @@ public class EhCacheBasedX509UserCache implements X509UserCache, InitializingBea } } - public void afterPropertiesSet() throws Exception { - Assert.notNull(cache, "cache is mandatory"); - } - public void putUserInCache(X509Certificate userCert, UserDetails user) { Element element = new Element(userCert, user); @@ -96,6 +96,6 @@ public class EhCacheBasedX509UserCache implements X509UserCache, InitializingBea logger.debug("Cache remove: " + userCert.getSubjectDN()); } - this.removeUserFromCache(userCert); + cache.remove(userCert); } } diff --git a/core/src/test/java/org/acegisecurity/providers/x509/cache/EhCacheBasedX509UserCacheTests.java b/core/src/test/java/org/acegisecurity/providers/x509/cache/EhCacheBasedX509UserCacheTests.java new file mode 100644 index 0000000000..73bc203914 --- /dev/null +++ b/core/src/test/java/org/acegisecurity/providers/x509/cache/EhCacheBasedX509UserCacheTests.java @@ -0,0 +1,82 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.providers.x509.cache; + +import junit.framework.TestCase; + +import net.sf.acegisecurity.providers.dao.User; +import net.sf.acegisecurity.providers.x509.X509TestUtils; +import net.sf.acegisecurity.MockApplicationContext; +import net.sf.acegisecurity.UserDetails; +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.ehcache.Cache; + +import org.springframework.context.ApplicationContext; + +import java.security.cert.X509Certificate; + +/** + * @author Luke Taylor + * @version $Id$ + */ +public class EhCacheBasedX509UserCacheTests extends TestCase { + //~ Constructors =========================================================== + + public EhCacheBasedX509UserCacheTests() { + super(); + } + + public EhCacheBasedX509UserCacheTests(String arg0) { + super(arg0); + } + + //~ Methods ================================================================ + + public final void setUp() throws Exception { + super.setUp(); + } + + public void testCacheOperation() throws Exception { + EhCacheBasedX509UserCache cache = new EhCacheBasedX509UserCache(); + cache.setCache(getCache()); + cache.afterPropertiesSet(); + + // Check it gets stored in the cache + cache.putUserInCache(X509TestUtils.buildTestCertificate(), getUser()); + assertEquals(getUser().getPassword(), + cache.getUserFromCache(X509TestUtils.buildTestCertificate()).getPassword()); + + // Check it gets removed from the cache + cache.removeUserFromCache(X509TestUtils.buildTestCertificate()); + assertNull(cache.getUserFromCache(X509TestUtils.buildTestCertificate())); + + // Check it doesn't return values for null user + assertNull(cache.getUserFromCache(null)); + } + + private Cache getCache() { + ApplicationContext ctx = MockApplicationContext.getContext(); + + return (Cache) ctx.getBean("eHCacheBackend"); + } + + private UserDetails getUser() { + return new User("marissa", "password", true, true, true, true, + new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl( + "ROLE_TWO")}); + } +}