ServletContextImpl implements ServletContext { public HttpSessionImpl getSession(final String id) { HttpSessionImpl session = null; try { session = sessionStorage.getSession(id); if (session != null) { session.setServletContext(this); if (isSessionExpired(session)) { sessionStorage.removeSession(session); LOGGER.log(Level.FINE, "Removed expired session {0}", new Object[]{session.getId()}); session = null; } } } catch (IOException e) { } return session; } ServletContextImpl(final String contextPath,
                              final List<ServletMapping> servletMappings,
                              final List<FilterMapping> filterMappings,
                              final Map<String, Object> attributes,
                              final ServerConfig serverConfig,
                              final SessionStorage sessionStorage); @Override String getMimeType(final String file); @Override void setAttribute(final String name, final Object value); @Override Object getAttribute(final String name); @Override Enumeration getAttributeNames(); HttpSessionImpl getSession(final String id); HttpSessionImpl createNewSession(); void handleSession(final HttpSessionImpl session, final HttpServletResponseImpl response); @Override List<ServletMapping> getServletMappings(); @Override List<FilterMapping> getFilterMappings(); @Override String getContextPath(); static final int MAX_AGE_IN_PAST; }
@Test public void shouldReturnSessionForValidSID() throws IOException { HttpSessionImpl session = new HttpSessionImpl("123", System.currentTimeMillis()); when(sessionStorage.getSession("123")).thenReturn(session); HttpSessionImpl sessionRead = servletContext.getSession("123"); assertThat(sessionRead, is(not(nullValue()))); assertThat(sessionRead.getServletContext(), is((ServletContext) servletContext)); }
@Test public void shouldRemoveExpiredSession() throws IOException { HttpSessionImpl session = new HttpSessionImpl("123", System.currentTimeMillis()); session.setLastAccessedTime(System.currentTimeMillis() - session.getMaxInactiveInterval() * 1000 - 1); when(sessionStorage.getSession("123")).thenReturn(session); HttpSessionImpl sessionRead = servletContext.getSession("123"); verify(sessionStorage, times(1)).removeSession(session); assertThat(sessionRead, is(nullValue())); }