HttpSessionEventPublisher now verifies that the ApplicationContext is not null

This commit is contained in:
Ray Krueger 2005-05-02 20:31:18 +00:00
parent e2b7b785e1
commit 47989c11bd
2 changed files with 31 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.util.Assert;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
@ -70,6 +71,8 @@ public class HttpSessionEventPublisher implements HttpSessionListener,
* WebApplicationContext * WebApplicationContext
*/ */
public void contextInitialized(ServletContextEvent event) { public void contextInitialized(ServletContextEvent event) {
if (log.isDebugEnabled())
log.debug("Received ServletContextEvent: " + event);
setContext(WebApplicationContextUtils.getRequiredWebApplicationContext( setContext(WebApplicationContextUtils.getRequiredWebApplicationContext(
event.getServletContext())); event.getServletContext()));
} }
@ -86,7 +89,7 @@ public class HttpSessionEventPublisher implements HttpSessionListener,
log.debug("Publishing event: " + e); 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); log.debug("Publishing event: " + e);
context.publishEvent(e); getContext().publishEvent(e);
} }
/** /**
@ -114,4 +117,9 @@ public class HttpSessionEventPublisher implements HttpSessionListener,
this.context = context; this.context = context;
log.debug("Using context: " + context); log.debug("Using context: " + context);
} }
ApplicationContext getContext() {
Assert.notNull(context, "setContext(...) never called, ApplicationContext must not be null");
return context;
}
} }

View File

@ -20,10 +20,13 @@ import junit.framework.TestCase;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
import org.springframework.mock.web.MockHttpSession; import org.springframework.mock.web.MockHttpSession;
import org.springframework.web.context.support.StaticWebApplicationContext; import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionEvent;
import net.sf.acegisecurity.MockApplicationContext;
/** /**
* The HttpSessionEventPublisher tests * The HttpSessionEventPublisher tests
@ -72,4 +75,22 @@ public class HttpSessionEventPublisherTests extends TestCase {
publisher.contextDestroyed(new ServletContextEvent(servletContext)); 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();
}
}
} }