1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * The HttpSessionEventPublisher tests
28   *
29   * @author Ray Krueger
30   * @version $Id: HttpSessionEventPublisherTests.java 1764 2006-11-20 19:35:11Z raykrueger $
31   */
32  public class HttpSessionEventPublisherTests extends TestCase {
33      //~ Methods ========================================================================================================
34  
35     /**
36       * It's not that complicated so we'll just run it straight through here.
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  }