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 import org.springframework.mock.web.MockHttpSession;
20 import org.springframework.mock.web.MockServletContext;
21 import org.springframework.web.context.support.StaticWebApplicationContext;
22
23 import javax.servlet.http.HttpSessionEvent;
24
25
26
27
28
29
30
31
32 public class HttpSessionEventPublisherTests extends TestCase {
33
34
35
36
37
38 public void testPublisher() {
39 HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
40
41 StaticWebApplicationContext context = new StaticWebApplicationContext();
42
43 MockServletContext servletContext = new MockServletContext();
44 servletContext.setAttribute(StaticWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
45
46 context.setServletContext(servletContext);
47 context.registerSingleton("listener", TestListener.class, null);
48 context.refresh();
49
50 MockHttpSession session = new MockHttpSession(servletContext);
51 TestListener listener = (TestListener) context.getBean("listener");
52
53 HttpSessionEvent event = new HttpSessionEvent(session);
54
55 publisher.sessionCreated(event);
56
57 assertNotNull(listener.getCreatedEvent());
58 assertNull(listener.getDestroyedEvent());
59 assertEquals(session, listener.getCreatedEvent().getSession());
60
61 listener.setCreatedEvent(null);
62 listener.setDestroyedEvent(null);
63
64 publisher.sessionDestroyed(event);
65 assertNotNull(listener.getDestroyedEvent());
66 assertNull(listener.getCreatedEvent());
67 assertEquals(session, listener.getDestroyedEvent().getSession());
68
69 }
70 }