HttpSessionEventPublisher now verifies that the ApplicationContext is not null
This commit is contained in:
parent
e2b7b785e1
commit
47989c11bd
|
@ -21,6 +21,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
@ -70,6 +71,8 @@ public class HttpSessionEventPublisher implements HttpSessionListener,
|
|||
* WebApplicationContext
|
||||
*/
|
||||
public void contextInitialized(ServletContextEvent event) {
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Received ServletContextEvent: " + event);
|
||||
setContext(WebApplicationContextUtils.getRequiredWebApplicationContext(
|
||||
event.getServletContext()));
|
||||
}
|
||||
|
@ -86,7 +89,7 @@ public class HttpSessionEventPublisher implements HttpSessionListener,
|
|||
|
||||
log.debug("Publishing event: " + e);
|
||||
|
||||
context.publishEvent(e);
|
||||
getContext().publishEvent(e);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -101,7 +104,7 @@ public class HttpSessionEventPublisher implements HttpSessionListener,
|
|||
|
||||
log.debug("Publishing event: " + e);
|
||||
|
||||
context.publishEvent(e);
|
||||
getContext().publishEvent(e);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,4 +117,9 @@ public class HttpSessionEventPublisher implements HttpSessionListener,
|
|||
this.context = context;
|
||||
log.debug("Using context: " + context);
|
||||
}
|
||||
|
||||
ApplicationContext getContext() {
|
||||
Assert.notNull(context, "setContext(...) never called, ApplicationContext must not be null");
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,10 +20,13 @@ import junit.framework.TestCase;
|
|||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.http.HttpSessionEvent;
|
||||
|
||||
import net.sf.acegisecurity.MockApplicationContext;
|
||||
|
||||
|
||||
/**
|
||||
* The HttpSessionEventPublisher tests
|
||||
|
@ -72,4 +75,22 @@ public class HttpSessionEventPublisherTests extends TestCase {
|
|||
|
||||
publisher.contextDestroyed(new ServletContextEvent(servletContext));
|
||||
}
|
||||
|
||||
public void testContext() throws Exception {
|
||||
HttpSessionEventPublisher pub = new HttpSessionEventPublisher();
|
||||
ConfigurableApplicationContext c = MockApplicationContext.getContext();
|
||||
pub.setContext(c);
|
||||
assertEquals(c, pub.getContext());
|
||||
}
|
||||
|
||||
public void testNullContextCheck() throws Exception {
|
||||
HttpSessionEventPublisher pub = new HttpSessionEventPublisher();
|
||||
|
||||
try {
|
||||
pub.getContext();
|
||||
fail("IllegalArgumentException expected, the context is null");
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue