Fold DefaultWebSession within InMemoryWebSessionStore

InMemoryWebSessionStore is very closely associated to DefaultWebSession
passing it to it several fields and functions. Now that the store also
creates the session, it makes sense to bring the latter in as an inner,
nested class.

Issue: SPR-15875, 15876
This commit is contained in:
Rossen Stoyanchev
2017-09-06 11:43:14 -04:00
parent c7d54c8b52
commit 2fc2dab230
4 changed files with 130 additions and 211 deletions

View File

@@ -16,59 +16,58 @@
package org.springframework.web.server.session;
import org.junit.Test;
import org.springframework.util.IdGenerator;
import org.springframework.util.JdkIdGenerator;
import reactor.core.publisher.Mono;
import java.time.Clock;
import java.time.ZoneId;
import org.springframework.web.server.WebSession;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Unit tests.
* @author Rob Winch
* @since 5.0
*/
public class DefaultWebSessionTests {
private static final Clock CLOCK = Clock.system(ZoneId.of("GMT"));
public class InMemoryWebSessionStoreTests {
private InMemoryWebSessionStore sessionStore = new InMemoryWebSessionStore();
private static final IdGenerator idGenerator = new JdkIdGenerator();
@Test
public void constructorWhenImplicitStartCopiedThenCopyIsStarted() {
DefaultWebSession original = createDefaultWebSession();
WebSession original = this.sessionStore.createWebSession().block();
assertNotNull(original);
original.getAttributes().put("foo", "bar");
DefaultWebSession copy = new DefaultWebSession(original, CLOCK.instant());
WebSession copy = this.sessionStore.updateLastAccessTime(original).block();
assertNotNull(copy);
assertTrue(copy.isStarted());
}
@Test
public void constructorWhenExplicitStartCopiedThenCopyIsStarted() {
DefaultWebSession original = createDefaultWebSession();
WebSession original = this.sessionStore.createWebSession().block();
assertNotNull(original);
original.start();
DefaultWebSession copy = new DefaultWebSession(original, CLOCK.instant());
WebSession copy = this.sessionStore.updateLastAccessTime(original).block();
assertNotNull(copy);
assertTrue(copy.isStarted());
}
@Test
public void startsSessionExplicitly() {
DefaultWebSession session = createDefaultWebSession();
WebSession session = this.sessionStore.createWebSession().block();
assertNotNull(session);
session.start();
assertTrue(session.isStarted());
}
@Test
public void startsSessionImplicitly() {
DefaultWebSession session = createDefaultWebSession();
WebSession session = this.sessionStore.createWebSession().block();
assertNotNull(session);
session.start();
session.getAttributes().put("foo", "bar");
assertTrue(session.isStarted());
}
private DefaultWebSession createDefaultWebSession() {
return new DefaultWebSession(idGenerator, CLOCK, (s, session) -> Mono.empty(), s -> Mono.empty());
}
}

View File

@@ -111,11 +111,9 @@ public class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTe
// Now set the clock of the session back by 31 minutes
InMemoryWebSessionStore store = (InMemoryWebSessionStore) this.sessionManager.getSessionStore();
DefaultWebSession session = (DefaultWebSession) store.retrieveSession(id).block();
WebSession session = store.retrieveSession(id).block();
assertNotNull(session);
Instant lastAccessTime = Clock.offset(store.getClock(), Duration.ofMinutes(-31)).instant();
session = new DefaultWebSession(session, lastAccessTime);
session.save().block();
store.setClock(Clock.offset(store.getClock(), Duration.ofMinutes(31)));
// Third request: expired session, new session created
request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build();