Added an AppListener to collect events for use in tests

This commit is contained in:
Luke Taylor 2010-09-13 14:20:21 +01:00
parent 62cbd51d54
commit 0217e98bdb
3 changed files with 65 additions and 1 deletions

View File

@ -6,6 +6,12 @@ import org.springframework.security.config.util.InMemoryXmlApplicationContext
import org.springframework.security.core.context.SecurityContextHolder
import spock.lang.Specification
import static org.springframework.security.config.ConfigTestUtils.AUTH_PROVIDER_XML
import org.springframework.context.ApplicationListener
import org.springframework.context.ApplicationEvent
import org.springframework.security.authentication.event.AbstractAuthenticationEvent
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent
import org.springframework.security.access.event.AbstractAuthorizationEvent
import org.springframework.security.CollectingAppListener
/**
*
@ -15,10 +21,12 @@ abstract class AbstractXmlConfigTests extends Specification {
AbstractXmlApplicationContext appContext;
Writer writer;
MarkupBuilder xml;
ApplicationListener appListener;
def setup() {
writer = new StringWriter()
xml = new MarkupBuilder(writer)
appListener = new CollectingAppListener()
}
def cleanup() {
@ -62,5 +70,6 @@ abstract class AbstractXmlConfigTests extends Specification {
def createAppContext(String extraXml) {
appContext = new InMemoryXmlApplicationContext(writer.toString() + extraXml);
appContext.addApplicationListener(appListener);
}
}

View File

@ -498,10 +498,12 @@ class MiscHttpConfigTests extends AbstractHttpConfigTests {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/j_spring_security_check");
request.setServletPath("/j_spring_security_check");
request.addParameter("j_username", "bob");
request.addParameter("j_password", "bob");
request.addParameter("j_password", "bobspassword");
then: "App context creation and login request succeed"
Filter debugFilter = appContext.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN);
debugFilter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain());
appListener.events.size() == 2
appListener.authenticationEvents.size() == 2
}
def eraseCredentialsDefaultsToTrue() {

View File

@ -0,0 +1,53 @@
package org.springframework.security;
import java.util.*;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.security.access.event.AbstractAuthorizationEvent;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
/**
* ApplicationListener which collects events for use in test assertions
*
* @author Luke Taylor
* @since 3.1
*/
public class CollectingAppListener implements ApplicationListener {
Set<ApplicationEvent> events = new HashSet<ApplicationEvent>();
Set<AbstractAuthenticationEvent> authenticationEvents = new HashSet<AbstractAuthenticationEvent>();
Set<AbstractAuthenticationFailureEvent> authenticationFailureEvents = new HashSet<AbstractAuthenticationFailureEvent>();
Set<AbstractAuthorizationEvent> authorizationEvents = new HashSet<AbstractAuthorizationEvent>();
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof AbstractAuthenticationEvent) {
events.add(event);
authenticationEvents.add((AbstractAuthenticationEvent) event);
}
if (event instanceof AbstractAuthenticationFailureEvent) {
events.add(event);
authenticationFailureEvents.add((AbstractAuthenticationFailureEvent) event);
}
if (event instanceof AbstractAuthorizationEvent) {
events.add(event);
authorizationEvents.add((AbstractAuthorizationEvent) event);
}
}
public Set<ApplicationEvent> getEvents() {
return events;
}
public Set<AbstractAuthenticationEvent> getAuthenticationEvents() {
return authenticationEvents;
}
public Set<AbstractAuthenticationFailureEvent> getAuthenticationFailureEvents() {
return authenticationFailureEvents;
}
public Set<AbstractAuthorizationEvent> getAuthorizationEvents() {
return authorizationEvents;
}
}