Class MockOorianHttpSession

java.lang.Object
com.oorian.testing.MockOorianHttpSession
All Implemented Interfaces:
OorianHttpSession

public class MockOorianHttpSession extends Object implements OorianHttpSession
Mock implementation of OorianHttpSession for testing without a servlet container.

This mock uses an in-memory HashMap for session attributes and provides configurable session metadata (ID, creation time, etc.). It can be used directly or via OorianTestContext for setting up a complete test environment.

Usage:


 MockOorianHttpSession session = new MockOorianHttpSession();
 session.setAttribute("userId", 12345);

 Object value = session.getAttribute("userId");
 assert value.equals(12345);
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • MockOorianHttpSession

      public MockOorianHttpSession()
      Constructs a new MockOorianHttpSession with a random UUID as the session ID.
    • MockOorianHttpSession

      public MockOorianHttpSession(String sessionId)
      Constructs a new MockOorianHttpSession with the specified session ID.
      Parameters:
      sessionId - the session ID to use
  • Method Details

    • setId

      public void setId(String id)
      Sets the session ID.
      Parameters:
      id - the new session ID
    • setNew

      public void setNew(boolean isNew)
      Sets the new-session flag.
      Parameters:
      isNew - true to mark this session as new
    • getId

      public String getId()
      Description copied from interface: OorianHttpSession
      Returns the unique session identifier.
      Specified by:
      getId in interface OorianHttpSession
      Returns:
      the session ID
    • getCreationTime

      public long getCreationTime()
      Description copied from interface: OorianHttpSession
      Returns the time the session was created.
      Specified by:
      getCreationTime in interface OorianHttpSession
      Returns:
      the creation time in milliseconds since epoch
    • getLastAccessedTime

      public long getLastAccessedTime()
      Description copied from interface: OorianHttpSession
      Returns the time of last client access.
      Specified by:
      getLastAccessedTime in interface OorianHttpSession
      Returns:
      the last access time in milliseconds since epoch
    • setMaxInactiveInterval

      public void setMaxInactiveInterval(int interval)
      Description copied from interface: OorianHttpSession
      Sets the maximum time interval between requests before the session expires.
      Specified by:
      setMaxInactiveInterval in interface OorianHttpSession
      Parameters:
      interval - the interval in seconds
    • getMaxInactiveInterval

      public int getMaxInactiveInterval()
      Description copied from interface: OorianHttpSession
      Returns the maximum inactive interval for this session.
      Specified by:
      getMaxInactiveInterval in interface OorianHttpSession
      Returns:
      the interval in seconds
    • getAttribute

      public Object getAttribute(String name)
      Description copied from interface: OorianHttpSession
      Returns the session attribute with the specified name.
      Specified by:
      getAttribute in interface OorianHttpSession
      Parameters:
      name - the attribute name
      Returns:
      the attribute value, or null if not found
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Description copied from interface: OorianHttpSession
      Returns an enumeration of all attribute names in this session.
      Specified by:
      getAttributeNames in interface OorianHttpSession
      Returns:
      enumeration of attribute names
    • setAttribute

      public void setAttribute(String name, Object value)
      Description copied from interface: OorianHttpSession
      Sets a session attribute.
      Specified by:
      setAttribute in interface OorianHttpSession
      Parameters:
      name - the attribute name
      value - the attribute value
    • removeAttribute

      public void removeAttribute(String name)
      Description copied from interface: OorianHttpSession
      Removes a session attribute.
      Specified by:
      removeAttribute in interface OorianHttpSession
      Parameters:
      name - the attribute name to remove
    • invalidate

      public void invalidate()
      Description copied from interface: OorianHttpSession
      Invalidates this session.
      Specified by:
      invalidate in interface OorianHttpSession
    • isNew

      public boolean isNew()
      Description copied from interface: OorianHttpSession
      Checks if the session was newly created.
      Specified by:
      isNew in interface OorianHttpSession
      Returns:
      true if the session is new
    • isInvalidated

      public boolean isInvalidated()
      Returns whether this session has been invalidated.
      Returns:
      true if invalidate() has been called
    • getAttributeMap

      public Map<String,Object> getAttributeMap()
      Returns the underlying attribute map for direct inspection in tests.
      Returns:
      an unmodifiable view of the attribute map