FileSessionStorage implements SessionStorage { @Override public HttpSessionImpl getSession(final String id) throws IOException { HttpSessionImpl session = null; if (isSessionIdValid(id)) { File file = new File(getSessionStoragePath(id)); if (file.exists()) { session = readSession(id, file); } else { LOGGER.log(Level.FINE, "Session file does not exist {0} under {1}", new Object[]{id, tempPath}); } } return session; } FileSessionStorage(final String tempPath); @Override void persistSession(final HttpSessionImpl session); @Override HttpSessionImpl getSession(final String id); @Override boolean removeSession(final HttpSessionImpl session);  }
@Test public void shouldReturnNullOnInvalidSessionName() throws IOException { assertThat(fileSessionStorage.getSession(null), is(nullValue())); assertThat(fileSessionStorage.getSession("abcX8"), is(nullValue())); assertThat(fileSessionStorage.getSession("/asdfghjklzxasdfghjklzxasdfghjklzxz"), is(nullValue())); }
@Test public void shouldFailSilentlyOnInvalidFileContents() throws IOException { String sid = "asdfghjklzxasdfghjklzxasdfghjklz"; File sessionFile = new File(workingDirectory + sid + "_session"); if (sessionFile.exists() && !sessionFile.delete()) { throw new IOException("Unable to delete file " + sessionFile.getAbsolutePath()); } if (!sessionFile.createNewFile()) { throw new IOException("Unable to create new file " + sessionFile.getAbsolutePath()); } assertThat(fileSessionStorage.getSession(sid), is(nullValue())); }