@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.
    }
  }
 private static StatelessClientContext getStatelessContext(Client client) {
   ClientContext context = client.getContext();
   StatelessClientContext statelessContext = new StatelessClientContext();
   statelessContext.setVersionId(context.getVersionId());
   statelessContext.setAppId(context.getAppId());
   statelessContext.setClientId(context.getClientId());
   statelessContext.setLocale(context.getLocale());
   return statelessContext;
 }