public void testSessionForPortlet() throws Exception {
    String handle = "handle";
    String handle2 = "handle2";
    String sid = "id";
    String sid2 = "id2";

    assertNull(info.getSessionIdForPortlet(handle));
    assertEquals(0, info.getNumberOfSessions());

    addSession(handle, sid, 1);
    addSession(handle2, sid2, 3);

    assertNull(info.getSessionIdForPortlet("unknown"));

    assertEquals(sid, info.getSessionIdForPortlet(handle));
    assertEquals(2, info.getNumberOfSessions());

    Thread.sleep(SLEEP_TIME);
    assertNull(info.getSessionIdForPortlet(handle));
    assertEquals(sid2, info.getSessionIdForPortlet(handle2));
    assertEquals(1, info.getNumberOfSessions());

    info.removeSessionForPortlet(handle2);
    assertEquals(0, info.getNumberOfSessions());
  }
  public void testUserCookie() throws Exception {
    assertNull(info.getUserCookie());

    Cookie[] cookies = new Cookie[] {createCookie("name", "value", 1)};
    info.setUserCookie(cookies);

    assertEquals("name=value", info.getUserCookie());

    // wait for cookie expiration
    Thread.sleep(SLEEP_TIME);
    assertNull(info.getUserCookie()); // we shouldn't have a cookie now

    cookies = new Cookie[] {createCookie("name1", "value1", 1), createCookie("name2", "value2", 3)};
    info.setUserCookie(cookies);
    assertEquals("name1=value1,name2=value2", info.getUserCookie());

    Thread.sleep(SLEEP_TIME);
    assertEquals("name2=value2", info.getUserCookie());

    try {
      info.setUserCookie(null);
      fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException e) {
      // expected
    }
  }
  public void testSetParentSessionId() {
    assertNull(info.getParentSessionId());

    String id = "session";
    info.setParentSessionId(id);
    assertEquals(id, info.getParentSessionId());

    // trying to set the same id should work
    info.setParentSessionId(id);

    try {
      info.setParentSessionId("other");
      fail("Cannot modify session id once it has been set");
    } catch (IllegalStateException expected) {
      // expected
    }
  }
  public void testGroupCookies() throws Exception {
    String groupId = "groupId";

    try {
      info.setGroupCookieFor(
          groupId,
          new Cookie[] {createCookie("name1", "value1", 1), createCookie("name2", "value2", -1)});
      fail("Cannot add group cookie if not perGroup");
    } catch (IllegalStateException e) {
      // expected
    }

    info.setPerGroupCookies(true);
    info.setGroupCookieFor(
        groupId,
        new Cookie[] {
          createCookie("name1", "value1", 1),
          createCookie("name2", "value2", WSRPConstants.SESSION_NEVER_EXPIRES)
        });

    assertEquals("name1=value1,name2=value2", info.getGroupCookieFor(groupId));

    Thread.sleep(SLEEP_TIME);
    assertEquals("name2=value2", info.getGroupCookieFor(groupId));

    info.clearGroupCookies();
    assertNull(info.getGroupCookieFor(groupId));
  }
 private void addSession(String handle, String sid, int expires) {
   info.addSessionForPortlet(handle, WSRPTypeFactory.createSessionContext(sid, expires));
 }
  public void testReleaseSessions() {
    addSession("handle", "id", 1);
    addSession("handle2", "id2", 1);
    addSession("handle3", "id3", 1);

    assertEquals(3, info.getNumberOfSessions());

    info.removeSessions();

    assertEquals(0, info.getNumberOfSessions());

    addSession("handle", "id", 1);
    addSession("handle2", "id2", 2);

    info.removeSession("id2");

    assertEquals(1, info.getNumberOfSessions());
    assertNull(info.getSessionIdForPortlet("handle2"));
    assertEquals("id", info.getSessionIdForPortlet("handle"));

    info.removeSessionForPortlet("handle");

    assertEquals(0, info.getNumberOfSessions());
    assertNull(info.getSessionIdForPortlet("handle"));

    try {
      info.removeSessionForPortlet("handle");
      fail("Session for portlet 'handle' should have already been released!");
    } catch (IllegalArgumentException expected) {
      // expected
    }
  }
  public void testReplaceUserCookies() throws Exception {
    info.setUserCookie(new Cookie[] {createCookie("name", "value", 1)});

    info.replaceUserCookiesWith(null);
    assertEquals("name=value", info.getUserCookie());

    ProducerSessionInformation other = new ProducerSessionInformation();

    info.replaceUserCookiesWith(other);
    assertEquals("name=value", info.getUserCookie());

    other.setUserCookie(new Cookie[] {createCookie("name2", "value2", 1)});
    info.replaceUserCookiesWith(other);
    assertEquals("name2=value2", info.getUserCookie());

    Thread.sleep(SLEEP_TIME);
    info.replaceUserCookiesWith(other);
    assertNull(info.getUserCookie());
  }