Răsfoiți Sursa

Added X509 EhCache tests and fixed glaring bug in X509 EhCache implementation.

Luke Taylor 20 ani în urmă
părinte
comite
ee32874308

+ 5 - 5
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);
     }
 }

+ 82 - 0
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")});
+    }
+}