Refactor tests

Prepare the tests for supporting an application prefix to ensure
there are no regressions.

Issue gh-166
This commit is contained in:
Rob Winch
2015-08-17 11:25:44 -05:00
parent 00d50593c1
commit 6234dd5681
3 changed files with 14 additions and 12 deletions

View File

@@ -20,7 +20,6 @@ import static org.fest.assertions.Assertions.assertThat;
import java.util.Map;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,9 +33,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.session.FindByPrincipalNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
import org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.events.AbstractSessionEvent;
import org.springframework.session.events.SessionCreatedEvent;
@@ -48,9 +46,9 @@ import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class RedisOperationsSessionRepositoryITests<S extends Session> {
public class RedisOperationsSessionRepositoryITests {
@Autowired
private FindByPrincipalNameSessionRepository<S> repository;
private RedisOperationsSessionRepository repository;
@Autowired
private SessionEventRegistry registry;
@@ -62,9 +60,9 @@ public class RedisOperationsSessionRepositoryITests<S extends Session> {
public void saves() throws InterruptedException {
String username = "saves-"+System.currentTimeMillis();
String usernameSessionKey = RedisOperationsSessionRepository.PRINCIPAL_NAME_PREFIX + username;
String usernameSessionKey = "spring:session:index:" + Session.PRINCIPAL_NAME_ATTRIBUTE_NAME + ":" + username;
S toSave = repository.createSession();
RedisSession toSave = repository.createSession();
String expectedAttributeName = "a";
String expectedAttributeValue = "b";
toSave.setAttribute(expectedAttributeName, expectedAttributeValue);
@@ -101,7 +99,7 @@ public class RedisOperationsSessionRepositoryITests<S extends Session> {
@Test
public void putAllOnSingleAttrDoesNotRemoveOld() {
S toSave = repository.createSession();
RedisSession toSave = repository.createSession();
toSave.setAttribute("a", "b");
repository.save(toSave);
@@ -121,12 +119,12 @@ public class RedisOperationsSessionRepositoryITests<S extends Session> {
@Test
public void findByPrincipalName() throws Exception {
String principalName = "findByPrincipalName" + UUID.randomUUID();
S toSave = repository.createSession();
RedisSession toSave = repository.createSession();
toSave.setAttribute(Session.PRINCIPAL_NAME_ATTRIBUTE_NAME, principalName);
repository.save(toSave);
Map<String, S> findByPrincipalName = repository.findByPrincipalName(principalName);
Map<String, RedisSession> findByPrincipalName = repository.findByPrincipalName(principalName);
assertThat(findByPrincipalName).hasSize(1);
assertThat(findByPrincipalName.keySet()).containsOnly(toSave.getId());

View File

@@ -23,7 +23,6 @@ import static org.mockito.Mockito.when;
import static org.springframework.session.data.redis.RedisOperationsSessionRepository.CREATION_TIME_ATTR;
import static org.springframework.session.data.redis.RedisOperationsSessionRepository.LAST_ACCESSED_ATTR;
import static org.springframework.session.data.redis.RedisOperationsSessionRepository.MAX_INACTIVE_ATTR;
import static org.springframework.session.data.redis.RedisOperationsSessionRepository.getKey;
import static org.springframework.session.data.redis.RedisOperationsSessionRepository.getSessionAttrNameKey;
import java.util.Arrays;
@@ -361,12 +360,16 @@ public class RedisOperationsSessionRepositoryTests {
redisRepository.cleanupExpiredSessions();
for(Object id : expiredIds) {
String expiredKey = RedisOperationsSessionRepository.BOUNDED_HASH_KEY_PREFIX + id;
String expiredKey = "spring:session:sessions:" + id;
// https://github.com/spring-projects/spring-session/issues/93
verify(redisOperations).hasKey(expiredKey);
}
}
private String getKey(String id) {
return "spring:session:sessions:" + id;
}
private Map map(Object...objects) {
Map<String,Object> result = new HashMap<String,Object>();
if(objects == null) {

View File

@@ -56,6 +56,7 @@ public class RedisSessionExpirationPolicyTests {
@Before
public void setup() {
RedisOperationsSessionRepository repository = new RedisOperationsSessionRepository(sessionRedisOperations);
policy = new RedisSessionExpirationPolicy(sessionRedisOperations);
session = new MapSession();
session.setLastAccessedTime(1429116694665L);