@Test
  public void timeout() throws Exception {
    TestFactory f = new TestFactory();
    SessionManager<Integer> s = new SessionManager<Integer>(f, TEST_LIFESPAN);
    assertEquals(TEST_LIFESPAN, s.getLifespan());

    SessionHolder<Integer> h = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    s.put(TEST_SESSION_ID, h);
    assertSame(h, s.get(TEST_SESSION_ID));
    assertEquals(TEST_SESSION_ID, f.getLastSessionId());
    for (int i = 0; i < 10; i++) {
      Thread.sleep(TEST_LIFESPAN / 2);
      assertSame(h, s.get(TEST_SESSION_ID));
    }
    Thread.sleep(TEST_LIFESPAN * 2);
    assertSingleEvent(
        Level.INFO,
        TEST_CATEGORY,
        "Session "
            + TEST_SESSION_ID.toString()
            + " has expired; creation context: "
            + TEST_CONTEXT.toString(),
        TEST_CATEGORY);
    assertNull(s.get(TEST_SESSION_ID));
    assertEquals(TEST_SESSION, f.getLastRemovedSession());
  }
  @Test
  public void session() throws Exception {
    Client c = new Client();

    Server<Object> s = new Server<Object>(TEST_AUTHENTICATOR, TEST_MANAGER);
    assertEquals(TEST_AUTHENTICATOR, s.getAuthenticator());

    try {
      c.assertValidSession();
      fail();
    } catch (I18NException ex) {
      assertEquals(Messages.NOT_LOGGED_IN, ex.getI18NBoundMessage());
    }

    c.login(TEST_USER, TEST_PASSWORD);
    SessionId id = c.getSessionId();
    assertNotNull(id);
    assertEquals(id, c.getContext().getSessionId());
    assertEquals(TEST_USER, TEST_MANAGER.get(id).getUser());

    try {
      c.login(TEST_USER, TEST_PASSWORD);
      fail();
    } catch (I18NException ex) {
      assertEquals(Messages.ALREADY_LOGGED_IN, ex.getI18NBoundMessage());
    }

    assertEquals(id, c.getSessionId());
    assertEquals(TEST_USER, TEST_MANAGER.get(id).getUser());

    c.logout();
    assertNull(c.getSessionId());
    assertNull(c.getContext().getSessionId());
    assertNull(TEST_MANAGER.get(id));

    try {
      c.login(TEST_USER_D, TEST_PASSWORD);
      fail();
    } catch (RemoteException ex) {
      assertEquals(Messages.BAD_CREDENTIALS, ((I18NException) ex.getCause()).getI18NBoundMessage());
    }
    assertNull(c.getSessionId());

    c.logout();

    s.stop();
    try {
      c.login(TEST_USER, TEST_PASSWORD);
      fail();
    } catch (WebServiceException ex) {
      // Desired.
    }
  }
  @Test
  public void basicsNoFactory() throws Exception {
    SessionManager<Integer> s = new SessionManager<Integer>();
    assertEquals(SessionManager.INFINITE_SESSION_LIFESPAN, s.getLifespan());
    SessionHolder<Integer> h = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    s.put(TEST_SESSION_ID, h);
    assertSame(h, s.get(TEST_SESSION_ID));
    s.remove(TEST_SESSION_ID);
    assertNull(s.get(TEST_SESSION_ID));

    // Removal of nonexistent session ID.

    s.remove(TEST_SESSION_ID);
  }
  @Test
  public void basics() throws Exception {
    TestFactory f = new TestFactory();
    SessionManager<Integer> s = new SessionManager<Integer>(f);
    assertSame(f, s.getSessionFactory());
    assertEquals(SessionManager.INFINITE_SESSION_LIFESPAN, s.getLifespan());

    assertNull(s.getServerId());
    s.setServerId(TEST_SERVER_ID);
    assertEquals(TEST_SERVER_ID, s.getServerId());
    s.setServerId(null);
    assertNull(s.getServerId());

    assertNull(s.get(TEST_SESSION_ID));

    SessionHolder<Integer> hD = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    long time = hD.getLastAccess();
    Thread.sleep(100);
    s.put(TEST_SESSION_ID, hD);
    assertTrue(hD.getLastAccess() > time);
    assertEquals(TEST_SESSION_ID, f.getLastSessionId());

    time = hD.getLastAccess();
    Thread.sleep(100);
    assertSame(hD, s.get(TEST_SESSION_ID));
    assertTrue(hD.getLastAccess() > time);

    SessionHolder<Integer> h = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    s.put(TEST_SESSION_ID, h);
    assertSame(h, s.get(TEST_SESSION_ID));

    assertNull(s.get(TEST_SESSION_ID_D));
    s.put(TEST_SESSION_ID_D, hD);
    assertSame(hD, s.get(TEST_SESSION_ID_D));
    assertSame(h, s.get(TEST_SESSION_ID));
    assertEquals(TEST_SESSION_ID_D, f.getLastSessionId());

    s.remove(TEST_SESSION_ID);
    assertNull(s.get(TEST_SESSION_ID));
    assertSame(hD, s.get(TEST_SESSION_ID_D));
    assertEquals(TEST_SESSION, f.getLastRemovedSession());

    // Removal of nonexistent session ID.

    s.remove(TEST_SESSION_ID);
  }