    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.concurrent;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.ui.session.HttpSessionDestroyedEvent;
    21
    22  import org.springframework.mock.web.MockHttpSession;
    23
    24  import java.util.Date;
    25
    26
    27  /***
    28   * Tests {@link SessionRegistryImpl}.
    29   *
    30   * @author Ben Alex
    31   * @version $Id: SessionRegistryImplTests.java,v 1.2 2005/11/17 00:55:48 benalex Exp $
    32   */
    33  public class SessionRegistryImplTests extends TestCase {
    34      //~ Methods ================================================================
    35
    36      public void testEventPublishing() {
    37          MockHttpSession httpSession = new MockHttpSession();
    38          Object principal = "Some principal object";
    39          String sessionId = httpSession.getId();
    40          assertNotNull(sessionId);
    41
    42          SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
    43
    44          // Register new Session
    45          sessionRegistry.registerNewSession(sessionId, principal);
    46
    47          // Deregister session via an ApplicationEvent
    48          sessionRegistry.onApplicationEvent(new HttpSessionDestroyedEvent(
    49                  httpSession));
    50
    51          // Check attempts to retrieve cleared session return null
    52          assertNull(sessionRegistry.getSessionInformation(sessionId));
    53      }
    54
    55      public void testSessionInformationLifecycle() throws Exception {
    56          Object principal = "Some principal object";
    57          String sessionId = "1234567890";
    58          SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
    59
    60          // Register new Session
    61          sessionRegistry.registerNewSession(sessionId, principal);
    62
    63          // Retrieve existing session by session ID
    64          Date currentDateTime = sessionRegistry.getSessionInformation(sessionId)
    65                                                .getLastRequest();
    66          assertEquals(principal,
    67              sessionRegistry.getSessionInformation(sessionId).getPrincipal());
    68          assertEquals(sessionId,
    69              sessionRegistry.getSessionInformation(sessionId).getSessionId());
    70          assertNotNull(sessionRegistry.getSessionInformation(sessionId)
    71                                       .getLastRequest());
    72
    73          // Retrieve existing session by principal
    74          assertEquals(1, sessionRegistry.getAllSessions(principal).length);
    75
    76          // Sleep to ensure SessionRegistryImpl will update time
    77          Thread.sleep(1000);
    78
    79          // Update request date/time
    80          sessionRegistry.refreshLastRequest(sessionId);
    81
    82          Date retrieved = sessionRegistry.getSessionInformation(sessionId)
    83                                          .getLastRequest();
    84          assertTrue(retrieved.after(currentDateTime));
    85
    86          // Check it retrieves correctly when looked up via principal
    87          assertEquals(retrieved,
    88              sessionRegistry.getAllSessions(principal)[0].getLastRequest());
    89
    90          // Clear session information
    91          sessionRegistry.removeSessionInformation(sessionId);
    92
    93          // Check attempts to retrieve cleared session return null
    94          assertNull(sessionRegistry.getSessionInformation(sessionId));
    95          assertNull(sessionRegistry.getAllSessions(principal));
    96      }
    97
    98      public void testTwoSessionsOnePrincipalHandling() throws Exception {
    99          Object principal = "Some principal object";
    100         String sessionId1 = "1234567890";
    101         String sessionId2 = "9876543210";
    102         SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
    103
    104         // Register new Session
    105         sessionRegistry.registerNewSession(sessionId1, principal);
    106         assertEquals(1, sessionRegistry.getAllSessions(principal).length);
    107         assertEquals(sessionId1,
    108             sessionRegistry.getAllSessions(principal)[0].getSessionId());
    109
    110         // Register new Session
    111         sessionRegistry.registerNewSession(sessionId2, principal);
    112         assertEquals(2, sessionRegistry.getAllSessions(principal).length);
    113         assertEquals(sessionId2,
    114             sessionRegistry.getAllSessions(principal)[1].getSessionId());
    115
    116         // Clear session information
    117         sessionRegistry.removeSessionInformation(sessionId1);
    118         assertEquals(1, sessionRegistry.getAllSessions(principal).length);
    119         assertEquals(sessionId2,
    120             sessionRegistry.getAllSessions(principal)[0].getSessionId());
    121
    122         // Clear final session
    123         sessionRegistry.removeSessionInformation(sessionId2);
    124         assertNull(sessionRegistry.getSessionInformation(sessionId2));
    125         assertNull(sessionRegistry.getAllSessions(principal));
    126     }
    127
    128     public void testTwoSessionsOnePrincipalExpiring() throws Exception {
    129         Object principal = "Some principal object";
    130         String sessionId1 = "1234567890";
    131         String sessionId2 = "9876543210";
    132         SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
    133
    134         // Register new Session
    135         sessionRegistry.registerNewSession(sessionId1, principal);
    136         assertEquals(1, sessionRegistry.getAllSessions(principal).length);
    137         assertEquals(sessionId1,
    138             sessionRegistry.getAllSessions(principal)[0].getSessionId());
    139
    140         // Register new Session
    141         sessionRegistry.registerNewSession(sessionId2, principal);
    142         assertEquals(2, sessionRegistry.getAllSessions(principal).length);
    143         assertEquals(sessionId2,
    144             sessionRegistry.getAllSessions(principal)[1].getSessionId());
    145
    146         // Expire one session
    147         SessionInformation session = sessionRegistry.getSessionInformation(sessionId2);
    148         session.expireNow();
    149
    150         // Check retrieval still correct
    151         assertTrue(sessionRegistry.getSessionInformation(sessionId2).isExpired());
    152         assertFalse(sessionRegistry.getSessionInformation(sessionId1).isExpired());
    153     }
    154
    155 }