1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.ui.session;
17
18 import junit.framework.TestCase;
19
20 import org.springframework.mock.web.MockServletContext;
21 import org.springframework.mock.web.MockHttpSession;
22 import org.springframework.web.context.support.StaticWebApplicationContext;
23 import org.springframework.context.ConfigurableApplicationContext;
24
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.http.HttpSessionEvent;
27
28 import org.acegisecurity.MockApplicationContext;
29
30
31 /***
32 * The HttpSessionEventPublisher tests
33 *
34 * @author Ray Krueger
35 */
36 public class HttpSessionEventPublisherTests extends TestCase {
37
38
39 /***
40 * It's not that complicated so we'll just run it straight through here.
41 * @throws Exception
42 */
43 public void testPublisher() throws Exception {
44 HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
45
46 StaticWebApplicationContext context = new StaticWebApplicationContext();
47
48 MockServletContext servletContext = new MockServletContext();
49 servletContext.setAttribute(StaticWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
50 context);
51
52 context.setServletContext(servletContext);
53 context.registerSingleton("listener", TestListener.class, null);
54 context.refresh();
55
56 publisher.contextInitialized(new ServletContextEvent(servletContext));
57
58 MockHttpSession session = new MockHttpSession();
59 TestListener listener = (TestListener) context.getBean("listener");
60
61 HttpSessionEvent event = new HttpSessionEvent(session);
62 publisher.sessionCreated(event);
63
64 assertNotNull(listener.getCreatedEvent());
65 assertNull(listener.getDestroyedEvent());
66 assertEquals(session, listener.getCreatedEvent().getSession());
67
68 listener.setCreatedEvent(null);
69 listener.setDestroyedEvent(null);
70
71 publisher.sessionDestroyed(event);
72 assertNotNull(listener.getDestroyedEvent());
73 assertNull(listener.getCreatedEvent());
74 assertEquals(session, listener.getDestroyedEvent().getSession());
75
76 publisher.contextDestroyed(new ServletContextEvent(servletContext));
77 }
78
79 public void testContext() throws Exception {
80 HttpSessionEventPublisher pub = new HttpSessionEventPublisher();
81 ConfigurableApplicationContext c = MockApplicationContext.getContext();
82 pub.setContext(c);
83 assertEquals(c, pub.getContext());
84 }
85
86 public void testNullContextCheck() throws Exception {
87 HttpSessionEventPublisher pub = new HttpSessionEventPublisher();
88
89 try {
90 pub.getContext();
91 fail("IllegalArgumentException expected, the context is null");
92 } catch (IllegalArgumentException e) {
93 e.printStackTrace();
94 }
95 }
96 }