    1   /* Copyright 2004, 2005 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
    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      //~ Methods ================================================================
    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  }