1
0
mirror of synced 2026-08-04 09:17:02 +00:00

SEC-679: Removed use of MockApplicationContext and improved use of ehcache (shutting down cache managers after tests are run). Upgraded ehcache version to 1.3 as used in Spring pom.

This commit is contained in:
Luke Taylor
2008-02-22 13:34:20 +00:00
parent ca9e64f857
commit 5187f89fe8
15 changed files with 219 additions and 275 deletions
@@ -1,34 +0,0 @@
/* Copyright 2004, 2005, 2006 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 org.springframework.security;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Simply returns an <code>ApplicationContext</code> which has a couple of <code>ApplicationEvent</code> listeners.
*
* @author Ben Alex
* @version $Id$
*/
public class MockApplicationContext {
//~ Methods ========================================================================================================
public static ConfigurableApplicationContext getContext() {
return new ClassPathXmlApplicationContext("org/springframework/security/applicationContext.xml");
}
}
@@ -15,18 +15,19 @@
package org.springframework.security.acl.basic.cache;
import junit.framework.TestCase;
import net.sf.ehcache.Ehcache;
import org.springframework.security.MockApplicationContext;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Cache;
import org.springframework.security.acl.basic.AclObjectIdentity;
import org.springframework.security.acl.basic.BasicAclEntry;
import org.springframework.security.acl.basic.NamedEntityObjectIdentity;
import org.springframework.security.acl.basic.SimpleAclEntry;
import org.springframework.context.ApplicationContext;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
@@ -35,7 +36,7 @@ import org.springframework.context.ApplicationContext;
* @author Ben Alex
* @version $Id$
*/
public class EhCacheBasedAclEntryCacheTests extends TestCase {
public class EhCacheBasedAclEntryCacheTests {
//~ Static fields/initializers =====================================================================================
private static final AclObjectIdentity OBJECT_100 = new NamedEntityObjectIdentity("OBJECT", "100");
@@ -44,29 +45,31 @@ public class EhCacheBasedAclEntryCacheTests extends TestCase {
private static final BasicAclEntry OBJECT_100_SCOTT = new SimpleAclEntry("scott", OBJECT_100, null, 4);
private static final BasicAclEntry OBJECT_200_PETER = new SimpleAclEntry("peter", OBJECT_200, null, 4);
//~ Constructors ===================================================================================================
public EhCacheBasedAclEntryCacheTests() {
super();
}
public EhCacheBasedAclEntryCacheTests(String arg0) {
super(arg0);
}
private static CacheManager cacheManager;
//~ Methods ========================================================================================================
@BeforeClass
public static void initCacheManaer() {
cacheManager = new CacheManager();
cacheManager.addCache(new Cache("ehcachebasedacltests", 500, false, false, 30, 30));
}
@AfterClass
public static void shutdownCacheManager() {
cacheManager.removalAll();
cacheManager.shutdown();
}
private Ehcache getCache() {
ApplicationContext ctx = MockApplicationContext.getContext();
Ehcache cache = cacheManager.getCache("ehcachebasedacltests");
cache.removeAll();
return (Ehcache) ctx.getBean("eHCacheBackend");
return cache;
}
public final void setUp() throws Exception {
super.setUp();
}
public void testCacheOperation() throws Exception {
@Test
public void cacheOperationSucceeds() throws Exception {
EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache();
cache.setCache(getCache());
cache.afterPropertiesSet();
@@ -85,7 +88,8 @@ public class EhCacheBasedAclEntryCacheTests extends TestCase {
assertNull(cache.getEntriesFromCache(new NamedEntityObjectIdentity("OBJECT", "100")));
}
public void testStartupDetectsMissingCache() throws Exception {
@Test
public void startupDetectsMissingCache() throws Exception {
EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache();
try {
@@ -21,11 +21,11 @@ import org.springframework.security.AccessDeniedException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.MockAccessDecisionManager;
import org.springframework.security.MockApplicationContext;
import org.springframework.security.MockAuthenticationManager;
import org.springframework.security.MockJoinPoint;
import org.springframework.security.MockRunAsManager;
import org.springframework.security.TargetObject;
import org.springframework.security.MockApplicationEventPublisher;
import org.springframework.security.context.SecurityContextHolder;
@@ -66,7 +66,7 @@ public class AspectJSecurityInterceptorTests extends TestCase {
public void testCallbackIsInvokedWhenPermissionGranted() throws Exception {
AspectJSecurityInterceptor si = new AspectJSecurityInterceptor();
si.setApplicationEventPublisher(MockApplicationContext.getContext());
si.setApplicationEventPublisher(new MockApplicationEventPublisher(true));
si.setAccessDecisionManager(new MockAccessDecisionManager());
si.setAuthenticationManager(new MockAuthenticationManager());
si.setRunAsManager(new MockRunAsManager());
@@ -97,7 +97,7 @@ public class AspectJSecurityInterceptorTests extends TestCase {
public void testCallbackIsNotInvokedWhenPermissionDenied() throws Exception {
AspectJSecurityInterceptor si = new AspectJSecurityInterceptor();
si.setApplicationEventPublisher(MockApplicationContext.getContext());
si.setApplicationEventPublisher(new MockApplicationEventPublisher(true));
si.setAccessDecisionManager(new MockAccessDecisionManager());
si.setAuthenticationManager(new MockAuthenticationManager());
si.setRunAsManager(new MockRunAsManager());
@@ -25,10 +25,10 @@ import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.MockAccessDecisionManager;
import org.springframework.security.MockApplicationContext;
import org.springframework.security.MockAuthenticationManager;
import org.springframework.security.MockRunAsManager;
import org.springframework.security.RunAsManager;
import org.springframework.security.MockApplicationEventPublisher;
import org.springframework.security.util.AntUrlPathMatcher;
import org.springframework.security.util.RegexUrlPathMatcher;
import org.springframework.security.context.SecurityContextHolder;
@@ -141,7 +141,7 @@ public class FilterSecurityInterceptorTests extends TestCase {
interceptor.setAccessDecisionManager(new MockAccessDecisionManager());
interceptor.setAuthenticationManager(new MockAuthenticationManager());
interceptor.setRunAsManager(new MockRunAsManager());
interceptor.setApplicationEventPublisher(MockApplicationContext.getContext());
interceptor.setApplicationEventPublisher(new MockApplicationEventPublisher(true));
// Setup a mock config attribute definition
ConfigAttributeDefinition def = new ConfigAttributeDefinition("MOCK_OK");
@@ -194,7 +194,7 @@ public class FilterSecurityInterceptorTests extends TestCase {
interceptor.setAccessDecisionManager(new MockAccessDecisionManager());
interceptor.setAuthenticationManager(new MockAuthenticationManager());
interceptor.setRunAsManager(new MockRunAsManager());
interceptor.setApplicationEventPublisher(MockApplicationContext.getContext());
interceptor.setApplicationEventPublisher(new MockApplicationEventPublisher(true));
// Setup a mock config attribute definition
ConfigAttributeDefinition def = new ConfigAttributeDefinition("MOCK_OK");
@@ -15,18 +15,20 @@
package org.springframework.security.providers.dao.cache;
import junit.framework.TestCase;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Cache;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.MockApplicationContext;
import org.springframework.security.userdetails.User;
import org.springframework.context.ApplicationContext;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Tests {@link EhCacheBasedUserCache}.
@@ -34,22 +36,27 @@ import org.springframework.context.ApplicationContext;
* @author Ben Alex
* @version $Id$
*/
public class EhCacheBasedUserCacheTests extends TestCase {
//~ Constructors ===================================================================================================
public EhCacheBasedUserCacheTests() {
}
public EhCacheBasedUserCacheTests(String arg0) {
super(arg0);
}
public class EhCacheBasedUserCacheTests {
private static CacheManager cacheManager;
//~ Methods ========================================================================================================
@BeforeClass
public static void initCacheManaer() {
cacheManager = new CacheManager();
cacheManager.addCache(new Cache("ehcacheusercachetests", 500, false, false, 30, 30));
}
@AfterClass
public static void shutdownCacheManager() {
cacheManager.removalAll();
cacheManager.shutdown();
}
private Ehcache getCache() {
ApplicationContext ctx = MockApplicationContext.getContext();
Ehcache cache = cacheManager.getCache("ehcacheusercachetests");
cache.removeAll();
return (Ehcache) ctx.getBean("eHCacheBackend");
return cache;
}
private User getUser() {
@@ -57,11 +64,8 @@ public class EhCacheBasedUserCacheTests extends TestCase {
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
}
public final void setUp() throws Exception {
super.setUp();
}
public void testCacheOperation() throws Exception {
@Test
public void cacheOperationsAreSuccessful() throws Exception {
EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
cache.setCache(getCache());
cache.afterPropertiesSet();
@@ -79,15 +83,12 @@ public class EhCacheBasedUserCacheTests extends TestCase {
assertNull(cache.getUserFromCache("UNKNOWN_USER"));
}
public void testStartupDetectsMissingCache() throws Exception {
@Test(expected = IllegalArgumentException.class)
public void startupDetectsMissingCache() throws Exception {
EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
try {
cache.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
cache.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
Ehcache myCache = getCache();
cache.setCache(myCache);
@@ -15,20 +15,23 @@
package org.springframework.security.providers.x509.cache;
import junit.framework.TestCase;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Cache;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.MockApplicationContext;
import org.springframework.security.providers.x509.X509TestUtils;
import org.springframework.security.userdetails.User;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.context.ApplicationContext;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
@@ -37,22 +40,28 @@ import org.springframework.context.ApplicationContext;
* @author Luke Taylor
* @version $Id$
*/
public class EhCacheBasedX509UserCacheTests extends TestCase {
//~ Constructors ===================================================================================================
public EhCacheBasedX509UserCacheTests() {
}
public EhCacheBasedX509UserCacheTests(String arg0) {
super(arg0);
}
public class EhCacheBasedX509UserCacheTests {
private static CacheManager cacheManager;
//~ Methods ========================================================================================================
private Ehcache getCache() {
ApplicationContext ctx = MockApplicationContext.getContext();
@BeforeClass
public static void initCacheManaer() {
cacheManager = new CacheManager();
cacheManager.addCache(new Cache("x509cachetests", 500, false, false, 30, 30));
}
return (Ehcache) ctx.getBean("eHCacheBackend");
@AfterClass
public static void shutdownCacheManager() {
cacheManager.removalAll();
cacheManager.shutdown();
}
private Ehcache getCache() {
Ehcache cache = cacheManager.getCache("x509cachetests");
cache.removeAll();
return cache;
}
private UserDetails getUser() {
@@ -60,11 +69,8 @@ public class EhCacheBasedX509UserCacheTests extends TestCase {
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
}
public final void setUp() throws Exception {
super.setUp();
}
public void testCacheOperation() throws Exception {
@Test
public void cacheOperationsAreSucessful() throws Exception {
EhCacheBasedX509UserCache cache = new EhCacheBasedX509UserCache();
cache.setCache(getCache());
cache.afterPropertiesSet();
@@ -15,27 +15,26 @@
package org.springframework.security.util;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.ConfigAttribute;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.MockApplicationContext;
import org.springframework.security.MockFilterConfig;
import org.springframework.security.context.HttpSessionContextIntegrationFilter;
import org.springframework.security.intercept.web.MockFilterInvocationDefinitionSource;
import org.springframework.security.intercept.web.DefaultFilterInvocationDefinitionSource;
import org.springframework.security.ui.webapp.AuthenticationProcessingFilter;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Tests {@link FilterChainProxy}.
@@ -64,7 +63,7 @@ public class FilterChainProxyTests {
@Test
public void testDetectsFilterInvocationDefinitionSourceThatDoesNotReturnAllConfigAttributes() throws Exception {
FilterChainProxy filterChainProxy = new FilterChainProxy();
filterChainProxy.setApplicationContext(MockApplicationContext.getContext());
filterChainProxy.setApplicationContext(new StaticApplicationContext());
try {
filterChainProxy.setFilterInvocationDefinitionSource(new MockFilterInvocationDefinitionSource(false, false));
@@ -77,7 +76,7 @@ public class FilterChainProxyTests {
@Test
public void testDetectsIfConfigAttributeDoesNotReturnValueForGetAttributeMethod() throws Exception {
FilterChainProxy filterChainProxy = new FilterChainProxy();
filterChainProxy.setApplicationContext(MockApplicationContext.getContext());
filterChainProxy.setApplicationContext(new StaticApplicationContext());
ConfigAttributeDefinition cad = new ConfigAttributeDefinition(new MockConfigAttribute());
@@ -95,16 +94,12 @@ public class FilterChainProxyTests {
}
}
@Test
@Test(expected = IllegalArgumentException.class)
public void testDetectsMissingFilterInvocationDefinitionSource() throws Exception {
FilterChainProxy filterChainProxy = new FilterChainProxy();
filterChainProxy.setApplicationContext(MockApplicationContext.getContext());
filterChainProxy.setApplicationContext(new StaticApplicationContext());
try {
filterChainProxy.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
filterChainProxy.afterPropertiesSet();
}
@Test
@@ -1,42 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
* 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.
*
* $Id$
-->
<beans>
<!-- Automatically receives AuthenticationEvent messages from DaoAuthenticationProvider -->
<bean id="authenticationLoggerListener" class="org.springframework.security.event.authentication.LoggerListener"/>
<!-- Automatically receives AuthenticationEvent messages from AbstractSecurityInterceptor -->
<bean id="secureObjectLoggerListener" class="org.springframework.security.event.authorization.LoggerListener"/>
<!-- Setup a cache we can use in tests of the caching layer -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/ehcache-failsafe.xml"/>
</bean>
<bean id="eHCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager"/>
</property>
<property name="cacheName" value="testingCache"/>
</bean>
</beans>